private static void OnRequestDetailsReceived(IAsyncResult result) { Socks5Proxy proxy = (Socks5Proxy)result.AsyncState; proxy.EndReceiveRequestDetails(result); proxy.Disconnect(); }
private static void OnRequestDetailsSent(IAsyncResult result) { Socks5Proxy proxy = (Socks5Proxy)result.AsyncState; proxy.EndSendRequestDetails(result); proxy.BeginReceiveRequestDetails(new AsyncCallback(OnRequestDetailsReceived), proxy); }
private static void OnNegotiationReceived(IAsyncResult result) { Socks5Proxy proxy = (Socks5Proxy)result.AsyncState; proxy.EndReceiveNegotiation(result); proxy.BeginSendRequestDetails(new AsyncCallback(OnRequestDetailsSent), proxy); }
private static void OnNegotiationSent(IAsyncResult result) { Socks5Proxy proxy = (Socks5Proxy)result.AsyncState; proxy.EndSendNegotiation(result); proxy.BeginReceiveNegotiation(new AsyncCallback(OnNegotiationReceived), proxy); }
private static void OnConnected(IAsyncResult result) { Socks5Proxy proxy = (Socks5Proxy)result.AsyncState; proxy.EndConnect(result); proxy.BeginSendNegotiation(new AsyncCallback(OnNegotiationSent), proxy, Socks5Method.NoAuthentication); }
protected void StartConnect(EndPoint target) { try { _destEndPoint = target; CreateRemote(); // Setting up proxy IForwardProxy remote; EndPoint proxyEP; if (Config.proxy.useProxy) { switch (Config.proxy.proxyType) { case ProxyConfig.PROXY_SOCKS5: remote = new Socks5Proxy(); break; case ProxyConfig.PROXY_HTTP: remote = new HttpProxy(); break; default: throw new NotSupportedException("Unknown forward proxy."); } proxyEP = SocketUtil.GetEndPoint(Config.proxy.proxyServer, Config.proxy.proxyPort); } else { remote = new DirectConnect(); proxyEP = null; } var session = new AsyncSession(remote); _currentRemoteSession = session; ProxyTimer proxyTimer = new ProxyTimer(_proxyTimeout); proxyTimer.AutoReset = false; proxyTimer.Elapsed += proxyConnectTimer_Elapsed; proxyTimer.Enabled = true; proxyTimer.Session = session; proxyTimer.DestEndPoint = SocketUtil.GetEndPoint(_server.server, _server.server_port); proxyTimer.Server = _server; _proxyConnected = false; // Connect to the proxy server. remote.BeginConnectProxy(proxyEP, ProxyConnectCallback, new AsyncSession <ProxyTimer>(remote, proxyTimer)); } catch (Exception e) { Logging.LogUsefulException(e); Close(); } }
public void Socks5_Test_01_FindProxy() { ManualResetEvent Done = new ManualResetEvent(false); Socks5Proxy Proxy = new Socks5Proxy(this.client1); Proxy.StartSearch((sender, e) => { Done.Set(); }); Assert.IsTrue(Done.WaitOne(30000), "Search not complete."); Assert.IsTrue(Proxy.HasProxy, "No SOCKS5 proxy found."); Console.Out.WriteLine("JID: " + Proxy.JID); Console.Out.WriteLine("Port: " + Proxy.Port.ToString()); Console.Out.WriteLine("Host: " + Proxy.Host); }
private void StartConnect() { try { CreateRemote(); // Setting up proxy IProxy remote; EndPoint proxyEP; if (_config.useProxy) { remote = new Socks5Proxy(); proxyEP = SocketUtil.GetEndPoint(_config.proxyServer, _config.proxyPort); } else { remote = new DirectConnect(); proxyEP = null; } var session = new AsyncSession(remote); _currentRemoteSession = session; ProxyTimer proxyTimer = new ProxyTimer(3000); proxyTimer.AutoReset = false; proxyTimer.Elapsed += proxyConnectTimer_Elapsed; proxyTimer.Enabled = true; proxyTimer.Session = session; proxyTimer.DestEndPoint = SocketUtil.GetEndPoint(server.server, server.server_port); proxyTimer.Server = server; _proxyConnected = false; // Connect to the proxy server. remote.BeginConnectProxy(proxyEP, new AsyncCallback(ProxyConnectCallback), new AsyncSession <ProxyTimer>(remote, proxyTimer)); } catch (Exception e) { Logging.LogUsefulException(e); Close(); } }
private void StartConnect() { try { CreateRemote(); // Setting up proxy IProxy remote; EndPoint proxyEP = null; EndPoint serverEP = SocketUtil.GetEndPoint(_server.server, _server.server_port); EndPoint pluginEP = _controller.GetPluginLocalEndPointIfConfigured(_server); if (pluginEP != null) { serverEP = pluginEP; remote = new DirectConnect(); } else if (_config.proxy.useProxy) { switch (_config.proxy.proxyType) { case ProxyConfig.PROXY_SOCKS5: remote = new Socks5Proxy(); break; case ProxyConfig.PROXY_HTTP: remote = new HttpProxy(); break; default: throw new NotSupportedException("Unknown forward proxy."); } proxyEP = SocketUtil.GetEndPoint(_config.proxy.proxyServer, _config.proxy.proxyPort); } else { remote = new DirectConnect(); } var session = new AsyncSession(remote); lock (_closeConnLock) { if (_closed) { remote.Close(); return; } _currentRemoteSession = session; } ProxyTimer proxyTimer = new ProxyTimer(_proxyTimeout) { AutoReset = false }; proxyTimer.Elapsed += ProxyConnectTimer_Elapsed; proxyTimer.Enabled = true; proxyTimer.Session = session; proxyTimer.DestEndPoint = serverEP; proxyTimer.Server = _server; _proxyConnected = false; // Connect to the proxy server. remote.BeginConnectProxy(proxyEP, ProxyConnectCallback, new AsyncSession <ProxyTimer>(remote, proxyTimer)); } catch (Exception e) { Logger.LogUsefulException(e); Close(); } }
/// <summary> /// Connects to the XMPP server. /// </summary> public override void Connect() { m_elements = null; int port = (int)m_listener[Options.PORT]; Debug.Assert(port > 0); //m_sslOn = m_ssl; ProxySocket proxy = null; ProxyType pt = (ProxyType)m_listener[Options.PROXY_TYPE]; switch (pt) { case ProxyType.Socks4: proxy = new Socks4Proxy(this); break; case ProxyType.Socks5: proxy = new Socks5Proxy(this); break; case ProxyType.HTTP: proxy = new ShttpProxy(this); break; /* * case ProxyType.HTTP_Polling: * XEP25Socket j25s = new XEP25Socket(this); * if (m_ProxyHost != null) * { * System.Net.WebProxy wp = new System.Net.WebProxy(); * wp.Address = new Uri("http://" + m_ProxyHost + ":" + m_ProxyPort); * if (m_ProxyUsername != null) * { * wp.Credentials = new System.Net.NetworkCredential(m_ProxyUsername, m_ProxyPassword); * } * j25s.Proxy = wp; * } * j25s.URL = m_server; * m_sock = j25s; * break; */ case ProxyType.None: m_sock = new AsyncSocket(null, this, (bool)m_listener[Options.SSL], false); ((AsyncSocket)m_sock).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as X509Certificate2; ((AsyncSocket)m_sock).CertificateGui = (bool)m_listener[Options.CERTIFICATE_GUI]; break; default: throw new ArgumentException("no handler for proxy type: " + pt, "ProxyType"); } if (proxy != null) { proxy.Socket = new AsyncSocket(null, proxy, (bool)m_listener[Options.SSL], false); ((AsyncSocket)proxy.Socket).LocalCertificate = m_listener[Options.LOCAL_CERTIFICATE] as X509Certificate2; proxy.Host = m_listener[Options.PROXY_HOST] as string; proxy.Port = (int)m_listener[Options.PROXY_PORT]; proxy.Username = m_listener[Options.PROXY_USER] as string; proxy.Password = m_listener[Options.PROXY_PW] as string; m_sock = proxy; } string to = (string)m_listener[Options.TO]; Debug.Assert(to != null); string host = (string)m_listener[Options.NETWORK_HOST]; if (String.IsNullOrEmpty(host)) { #if __MonoCS__ host = to; #else try { Address.LookupSRV((string)m_listener[Options.SRV_PREFIX], to, ref host, ref port); } catch { Debug.WriteLine("WARNING: netlib.Dns.dll missing"); host = to; } #endif } Address addr = new Address(host, port); m_sock.Connect(addr, (string)m_listener[Options.SERVER_ID]); }
public void Socks5_Test_05_InitiateSession() { this.ConnectClients(); Console.Out.WriteLine(); Console.Out.WriteLine("Searching for SOCKS5 proxy."); Console.Out.WriteLine(); ManualResetEvent Done1 = new ManualResetEvent(false); Socks5Proxy Proxy1 = new Socks5Proxy(this.client1); Proxy1.StartSearch((sender, e) => { Done1.Set(); }); ManualResetEvent Done2 = new ManualResetEvent(false); Socks5Proxy Proxy2 = new Socks5Proxy(this.client2); Proxy2.StartSearch((sender, e) => { Done2.Set(); }); Assert.IsTrue(Done1.WaitOne(30000), "Search not complete."); Assert.IsTrue(Proxy1.HasProxy, "No SOCKS5 proxy found."); Assert.IsTrue(Done2.WaitOne(30000), "Search not complete."); Assert.IsTrue(Proxy2.HasProxy, "No SOCKS5 proxy found."); Done1.Reset(); Done2.Reset(); ManualResetEvent Error1 = new ManualResetEvent(false); ManualResetEvent Error2 = new ManualResetEvent(false); ManualResetEvent Closed1 = new ManualResetEvent(false); ManualResetEvent Closed2 = new ManualResetEvent(false); Console.Out.WriteLine(); Console.Out.WriteLine("Start of session initiation."); Console.Out.WriteLine(); Proxy2.OnOpen += (sender, e) => { e.AcceptStream((sender2, e2) => { if (Encoding.ASCII.GetString(e2.Data) == "Hello1") { Done2.Set(); e2.Stream.Send(Encoding.ASCII.GetBytes("Hello2")); e2.Stream.CloseWhenDone(); } else { Error2.Set(); } }, (sender2, e2) => { Closed2.Set(); }, null); }; Proxy1.InitiateSession(this.client2.FullJID, (sender, e) => { if (e.Ok) { e.Stream.OnDataReceived += (sender2, e2) => { if (Encoding.ASCII.GetString(e2.Data) == "Hello2") { Done1.Set(); } else { Error1.Set(); } }; e.Stream.OnStateChange += (sender2, e2) => { if (e.Stream.State == Socks5State.Offline) { Closed1.Set(); } }; e.Stream.Send(Encoding.ASCII.GetBytes("Hello1")); } }, null); Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done1, Error1 }, 10000), "Did not receive message 1."); Assert.AreEqual(0, WaitHandle.WaitAny(new WaitHandle[] { Done2, Error2 }, 10000), "Did not receive message 2."); Assert.IsTrue(Closed1.WaitOne(10000), "Client 1 did not close properly."); Assert.IsTrue(Closed2.WaitOne(10000), "Client 2 did not close properly."); }
/// <summary> /// 测试速度 /// </summary> /// <param name="server">服务器</param> /// <param name="millisecondsTimeout">超时时间</param> /// <returns></returns> public int StartTest(Server server, int millisecondsTimeout = 1000) { _closed = false; try { CreateRemote(server); // Setting up proxy IProxy remote; EndPoint proxyEP = null; EndPoint serverEP = SocketUtil.GetEndPoint(_server.server, _server.server_port); EndPoint pluginEP = _controller.GetPluginLocalEndPointIfConfigured(_server); if (pluginEP != null) { serverEP = pluginEP; remote = new DirectConnect(); } else if (_config.proxy.useProxy) { switch (_config.proxy.proxyType) { case ProxyConfig.PROXY_SOCKS5: remote = new Socks5Proxy(); break; case ProxyConfig.PROXY_HTTP: remote = new HttpProxy(); break; default: throw new NotSupportedException("Unknown forward proxy."); } proxyEP = SocketUtil.GetEndPoint(_config.proxy.proxyServer, _config.proxy.proxyPort); } else { remote = new DirectConnect(); } var session = new AsyncSession(remote); lock (_closeConnLock) { if (_closed) { remote.Close(); return(0); } _currentRemoteSession = session; } _destEndPoint = serverEP; _proxyConnected = false; _received = false; manualResetEvent = new ManualResetEventSlim(false); Stopwatch sw = new Stopwatch(); sw.Start(); remote.BeginConnectProxy(proxyEP, ProxyConnectCallback, new AsyncSession(remote)); manualResetEvent.Wait(millisecondsTimeout); sw.Stop(); Close(); if (_received) { return((int)sw.ElapsedMilliseconds); } return(0); } catch (Exception e) { Close(); } finally { manualResetEvent?.Dispose(); manualResetEvent = null; } return(0); }