public static void SetStrategy(string newStrategy) { logger.Info("Change strategy to: " + newStrategy); Config.Strategy = newStrategy; SocksProxyConnection.SetStrategy(newStrategy); }
public async Task StartListen(int port) { if (_listenSocket != null) { throw new Exception("Already started. Please stop it first."); } if (NetUtils.IsPortInUse(port)) { throw new SocketException((int)SocketError.AddressAlreadyInUse); } ListenEp.Port = port; try { _listenSocket = new Socket(ListenEp.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _listenSocket.Bind(ListenEp); _listenSocket.Listen(100); logger.Info("Start Proxy On " + ListenEp); } catch (SocketException ex) { logger.Error("Listen failed. " + ex.SocketErrorCode); Stop(); throw; } try { while (true) { Socket requestSocket = await _listenSocket.AcceptTaskAsync().ConfigureAwait(false); ProxyConnection newConn = new SocksProxyConnection(requestSocket); //StartHandshake处理了所有的异常,不会有异常抛出。 //所以无限循环只能在AcceptTaskAsync失败(如Stop())时结束 await newConn.StartHandshake(); } } catch (ObjectDisposedException) { //// Stop() cause this exception, and this task finished. //ignore } catch (SocketException ex) { logger.Error("Accept failed. " + ex.SocketErrorCode); Stop(); throw; } catch (Exception ex) { logger.Error("Accept failed." + ex.TypeAndMessage()); Stop(); throw; } }