private static IPEndPoint GetFirstRespondingEndpoint(string sHostPortList) { string str; IPAddress[] addressArray; if ((sHostPortList == null) || sHostPortList.Trim().Equals(string.Empty)) { return(null); } sHostPortList = Utilities.TrimAfter(sHostPortList, ';'); IPEndPoint point = null; int iPort = 80; Utilities.CrackHostAndPort(sHostPortList, out str, ref iPort); try { addressArray = DNSResolver.GetIPAddressList(str, true, null); } catch { FiddlerApplication.Log.LogFormat("fiddler.network.gateway> Unable to resolve upstream proxy '{0}'... ignoring.", new object[] { sHostPortList }); return(null); } try { foreach (IPAddress address in addressArray) { try { Socket socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp) { NoDelay = true }; if (FiddlerApplication.oProxy._DefaultEgressEndPoint != null) { socket.Bind(FiddlerApplication.oProxy._DefaultEgressEndPoint); } socket.Connect(address, iPort); point = new IPEndPoint(address, iPort); socket.Close(); break; } catch (Exception exception) { if (!FiddlerApplication.Prefs.GetBoolPref("fiddler.network.dns.fallback", true)) { break; } FiddlerApplication.Log.LogFormat("fiddler.network.gateway.connect>Connection to {0} failed. {1}. Will try DNS Failover if available.", new object[] { address.ToString(), exception.Message }); } } return(point); } catch (Exception) { return(null); } }
private void TunnelDirectly() { string str; this._mySession.SetBitFlag(SessionFlags.SentToGateway, false); int iPort = 0x1bb; string sHostPort = this._mySession.oFlags["x-overrideHost"]; if (sHostPort == null) { sHostPort = this._mySession.PathAndQuery; } Utilities.CrackHostAndPort(sHostPort, out str, ref iPort); try { IPAddress[] arrDestIPs = DNSResolver.GetIPAddressList(str, true, this._mySession.Timers); this.socketRemote = ServerChatter.CreateConnectedSocket(arrDestIPs, iPort, this._mySession); } catch (Exception exception) { this._indicateTunnelFailure(0x1f8, exception.Message); return; } try { this._mySession.oResponse.headers = new HTTPResponseHeaders(); this._mySession.oResponse.headers.HTTPVersion = this._mySession.oRequest.headers.HTTPVersion; this._mySession.oResponse.headers.HTTPResponseCode = 200; this._mySession.oResponse.headers.HTTPResponseStatus = "200 Blind-Connection Established"; this._mySession.oResponse.headers.Add("FiddlerGateway", "Direct"); this._mySession.oResponse.headers.Add("StartTime", DateTime.Now.ToString("HH:mm:ss.fff")); this.socketClient.Send(this._mySession.oResponse.headers.ToByteArray(true, true)); this._mySession.oFlags["x-EgressPort"] = (this.socketRemote.LocalEndPoint as IPEndPoint).Port.ToString(); this.socketClient.BeginReceive(this.arrRequestBytes, 0, this.arrRequestBytes.Length, SocketFlags.None, new AsyncCallback(this.OnClientReceive), this.socketClient); this.socketRemote.BeginReceive(this.arrResponseBytes, 0, this.arrResponseBytes.Length, SocketFlags.None, new AsyncCallback(this.OnRemoteReceive), this.socketRemote); FiddlerApplication._frmMain.BeginInvoke(new updateUIDelegate(FiddlerApplication._frmMain.finishSession), new object[] { this._mySession }); this.WaitForCompletion(); } catch (Exception) { try { this.socketRemote.Close(); this.socketClient.Close(); } catch (Exception) { } } }
private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e) { try { DNSResolver.ClearCache(); FiddlerApplication.Log.LogString("NetworkAddressChanged."); if (this.oAutoProxy != null) { this.oAutoProxy.iAutoProxySuccessCount = 0; } if ((CONFIG.bForwardToGateway && (this.piPrior != null)) && this.piPrior.bUseManualProxies) { this._DetermineGatewayIPEndPoints(); } } catch (Exception exception) { FiddlerApplication.ReportException(exception); } }
public static IPAddress[] GetAddressNatively(string sHostname) { IntPtr zero = IntPtr.Zero; IntPtr ptr = IntPtr.Zero; List <IPAddress> list = new List <IPAddress>(); DNSResolver.DnsARecord dnsARecord = default(DNSResolver.DnsARecord); int num = DNSResolver.DnsQuery_W(ref sHostname, DNSResolver.DnsQueryTypes.DNS_TYPE_A, 0u, 0, ref zero, 0); if (num != 0) { throw new Win32Exception(num, string.Format("Native DNS Resolution error [0x{0:x}]", num)); } ptr = zero; while (!ptr.Equals(IntPtr.Zero)) { dnsARecord = (DNSResolver.DnsARecord)Marshal.PtrToStructure(ptr, typeof(DNSResolver.DnsARecord)); list.Add(new IPAddress((long)((ulong)dnsARecord.ipAddress))); ptr = dnsARecord.pNext; } DNSResolver.DnsRecordListFree(zero, 0); return(list.ToArray()); }
private bool ConnectToHost() { IPEndPoint point = null; string str2; string str3; IPAddress[] addressArray; string sHostAndPort = this.m_session.oFlags["x-overrideHost"]; if (sHostAndPort == null) { sHostAndPort = this.m_session.host; } if (this.m_session.oFlags["x-overrideGateway"] != null) { if (string.Equals("DIRECT", this.m_session.oFlags["x-overrideGateway"], StringComparison.OrdinalIgnoreCase)) { this.m_session.bypassGateway = true; } else { point = Utilities.IPEndPointFromHostPortString(this.m_session.oFlags["x-overrideGateway"]); } } else if (!this.m_session.bypassGateway) { int tickCount = Environment.TickCount; point = FiddlerApplication.oProxy.FindGatewayForOrigin(this.m_session.oRequest.headers.UriScheme, sHostAndPort); this.m_session.Timers.GatewayDeterminationTime = Environment.TickCount - tickCount; } if (point != null) { this._bWasForwarded = true; } else if (this.m_session.isFTP) { this.m_session.oRequest.FailSession(0x1f6, "Fiddler - FTP Connection Failed", "[Fiddler] Fiddler does not support proxying FTP traffic without an upstream HTTP->FTP proxy."); return(false); } int iPort = this.m_session.isHTTPS ? 0x1bb : (this.m_session.isFTP ? 0x15 : 80); Utilities.CrackHostAndPort(sHostAndPort, out str2, ref iPort); if (point != null) { if (this.m_session.isHTTPS) { str3 = "GATEWAY:HTTPS:" + str2 + ":" + iPort.ToString(); } else { str3 = "GW:" + point.ToString(); } } else { str3 = (this.m_session.isHTTPS ? "HTTPS:" : "") + str2 + ":" + iPort.ToString(); } if (((this.pipeServer != null) && !this.m_session.oFlags.ContainsKey("X-ServerPipe-Marriage-Trumps-All")) && !this.SIDsMatch(this.m_session.LocalProcessID, str3, this.pipeServer.sPoolKey)) { this._detachServerPipe(); } if ((this.pipeServer == null) && !this.m_session.oFlags.ContainsKey("X-Bypass-ServerPipe-Reuse-Pool")) { this.pipeServer = Proxy.htServerPipePool.DequeuePipe(str3, this.m_session.LocalProcessID, this.m_session.id); } if (this.pipeServer != null) { StringDictionary dictionary; this.m_session.Timers.ServerConnected = this.pipeServer.dtConnected; (dictionary = this.m_session.oFlags)["x-serversocket"] = dictionary["x-serversocket"] + "REUSE " + this.pipeServer._sPipeName; if ((this.pipeServer.Address != null) && !this.pipeServer.isConnectedToGateway) { this.m_session.m_hostIP = this.pipeServer.Address.ToString(); this.m_session.oFlags["x-hostIP"] = this.m_session.m_hostIP; } return(true); } int port = iPort; if (point != null) { addressArray = new IPAddress[] { point.Address }; port = point.Port; } else { try { addressArray = DNSResolver.GetIPAddressList(str2, true, this.m_session.Timers); } catch (Exception exception) { this.m_session.oRequest.FailSession(0x1f6, "Fiddler - DNS Lookup Failed", "Fiddler: DNS Lookup for " + Utilities.HtmlEncode(str2) + " failed. " + exception.Message); return(false); } if ((port < 0) || (port > 0xffff)) { this.m_session.oRequest.FailSession(0x1f6, "Invalid Request", "HTTP Request specified an invalid port number."); return(false); } } try { this.pipeServer = new ServerPipe("ServerPipe#" + this.m_session.id.ToString(), this._bWasForwarded); Socket oSocket = CreateConnectedSocket(addressArray, port, this.m_session); if (this._bWasForwarded) { if (!this.m_session.isHTTPS) { this.pipeServer.WrapSocketInPipe(this.m_session, oSocket, false, false, str2, this.m_session.oFlags["https-Client-Certificate"], "GW:" + point.ToString(), ref this.m_session.Timers.HTTPSHandshakeTime); } else { this.m_session.oFlags["x-CreatedTunnel"] = "Fiddler-Created-A-CONNECT-Tunnel"; this.pipeServer.WrapSocketInPipe(this.m_session, oSocket, true, true, str2, this.m_session.oFlags["https-Client-Certificate"], "HTTPS:" + str2 + ":" + iPort.ToString(), ref this.m_session.Timers.HTTPSHandshakeTime); } } else { this.pipeServer.WrapSocketInPipe(this.m_session, oSocket, this.m_session.isHTTPS, false, str2, this.m_session.oFlags["https-Client-Certificate"], (this.m_session.isHTTPS ? "HTTPS:" : "") + str2 + ":" + iPort.ToString(), ref this.m_session.Timers.HTTPSHandshakeTime); } return(true); } catch (Exception exception2) { if (this._bWasForwarded) { this.m_session.oRequest.FailSession(0x1f6, "Fiddler - Gateway Connection Failed", "[Fiddler] Connection to Gateway failed.<BR>Exception Text: " + exception2.Message); } else { this.m_session.oRequest.FailSession(0x1f6, "Fiddler - Connection Failed", "[Fiddler] Connection to " + Utilities.HtmlEncode(str2) + " failed.<BR>Exception Text: " + exception2.Message); } return(false); } }
public static IPAddress[] GetIPAddressList(string sRemoteHost, bool bCheckCache, SessionTimers oTimers) { IPAddress[] array = null; Stopwatch stopwatch = Stopwatch.StartNew(); IPAddress iPAddress = Utilities.IPFromString(sRemoteHost); if (iPAddress != null) { array = new IPAddress[] { iPAddress }; if (oTimers != null) { oTimers.DNSTime = (int)stopwatch.ElapsedMilliseconds; } return(array); } DNSResolver.DNSCacheEntry dNSCacheEntry; if (bCheckCache && DNSResolver.dictAddresses.TryGetValue(sRemoteHost, out dNSCacheEntry)) { if (dNSCacheEntry.iLastLookup > Utilities.GetTickCount() - DNSResolver.MSEC_DNS_CACHE_LIFETIME) { array = dNSCacheEntry.arrAddressList; } else { Dictionary <string, DNSResolver.DNSCacheEntry> obj; Monitor.Enter(obj = DNSResolver.dictAddresses); try { DNSResolver.dictAddresses.Remove(sRemoteHost); } finally { Monitor.Exit(obj); } } } if (array == null) { if (sRemoteHost.OICEndsWith(".onion") && !FiddlerApplication.Prefs.GetBoolPref("fiddler.network.dns.ResolveOnionHosts", false)) { throw new InvalidOperationException("Hostnames ending in '.onion' cannot be resolved by DNS. You must send this request through a TOR gateway, e.g. oSession[\"X-OverrideGateway\"] = \"socks=127.0.0.1:9150\";"); } try { if (sRemoteHost.Length > 126) { array = DNSResolver.GetAddressNatively(sRemoteHost); } else { array = Dns.GetHostAddresses(sRemoteHost); } } catch { if (oTimers != null) { oTimers.DNSTime = (int)stopwatch.ElapsedMilliseconds; } throw; } array = DNSResolver.trimAddressList(array); if (array.Length < 1) { throw new Exception("No valid IPv4 addresses were found for this host."); } if (array.Length > 0) { Dictionary <string, DNSResolver.DNSCacheEntry> obj2; Monitor.Enter(obj2 = DNSResolver.dictAddresses); try { if (!DNSResolver.dictAddresses.ContainsKey(sRemoteHost)) { DNSResolver.dictAddresses.Add(sRemoteHost, new DNSResolver.DNSCacheEntry(array)); } } finally { Monitor.Exit(obj2); } } } if (oTimers != null) { oTimers.DNSTime = (int)stopwatch.ElapsedMilliseconds; } return(array); }
public static IPAddress GetIPAddress(string sRemoteHost, bool bCheckCache) { return(DNSResolver.GetIPAddressList(sRemoteHost, bCheckCache, null)[0]); }
private static IPEndPoint GetFirstRespondingEndpoint(string sHostPortList) { if (Utilities.IsNullOrWhiteSpace(sHostPortList)) { return(null); } sHostPortList = Utilities.TrimAfter(sHostPortList, ';'); IPEndPoint iPEndPoint = null; int port = 80; string text; Utilities.CrackHostAndPort(sHostPortList, out text, ref port); IPAddress[] iPAddressList; IPEndPoint result; try { iPAddressList = DNSResolver.GetIPAddressList(text, true, null); } catch { FiddlerApplication.Log.LogFormat("fiddler.network.gateway> Unable to resolve upstream proxy '{0}'... ignoring.", new object[] { text }); result = null; return(result); } try { IPAddress[] array = iPAddressList; for (int i = 0; i < array.Length; i++) { IPAddress iPAddress = array[i]; try { using (Socket socket = new Socket(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)) { socket.NoDelay = true; if (FiddlerApplication.oProxy._DefaultEgressEndPoint != null) { socket.Bind(FiddlerApplication.oProxy._DefaultEgressEndPoint); } socket.Connect(iPAddress, port); iPEndPoint = new IPEndPoint(iPAddress, port); } break; } catch (Exception ex) { if (!FiddlerApplication.Prefs.GetBoolPref("fiddler.network.dns.fallback", true)) { break; } FiddlerApplication.Log.LogFormat("fiddler.network.gateway.connect>Connection to {0} failed. {1}. Will try DNS Failover if available.", new object[] { iPAddress.ToString(), ex.Message }); } } result = iPEndPoint; } catch (Exception) { result = null; } return(result); }