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 { //SDVPApplication.Log.LogFormat("SDVP.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); socket.NoDelay = true; if (SDVPApplication.oProxy._DefaultEgressEndPoint != null) { socket.Bind(SDVPApplication.oProxy._DefaultEgressEndPoint); } socket.Connect(address, iPort); point = new IPEndPoint(address, iPort); socket.Close(); break; } catch (Exception exception) { if (!SDVPApplication.Prefs.GetBoolPref("SDVP.network.dns.fallback", true)) { break; } //SDVPApplication.Log.LogFormat("SDVP.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("BrowserProxyGateway", "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); this.WaitForCompletion(); } catch (Exception) { try { this.socketRemote.Close(); this.socketClient.Close(); } catch (Exception) { } } }
private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e) { try { DNSResolver.ClearCache(); //SDVPApplication.Log.LogString("NetworkAddressChanged."); if (this.oAutoProxy != null) { this.oAutoProxy.iAutoProxySuccessCount = 0; } if ((Browser.bForwardToGateway && (this.piPrior != null)) && this.piPrior.bUseManualProxies) { this._DetermineGatewayIPEndPoints(); } } catch (Exception exception) { SDVPApplication.ReportException(exception); } }
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 = SDVPApplication.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, "Web Browser - FTP Connection Failed", "[SDVP] Web Browser 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.m_session.oFlags["X-Divorced-ServerPipe"] = string.Format("Had: {0} but needs: {1}", this.pipeServer.sPoolKey, str3); 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, "Web Browser - DNS Lookup Failed", "SDVP: DNS Lookup for " + Utilities.HtmlEncode(str2) + " failed. " + Utilities.DescribeException(exception)); 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"] = "SDVP-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, "Web Browser - Gateway Connection Failed", "[Web Browser] Connection to Gateway failed.<BR>Exception Text: " + Utilities.DescribeException(exception2)); } else { this.m_session.oRequest.FailSession(0x1f6, "Web Browser - Connection Failed", "[Web Browser] Connection to " + Utilities.HtmlEncode(str2) + " failed.<BR>Exception Text: " + Utilities.DescribeException(exception2)); } return(false); } }