internal void StartForward() { try { IsRunning = true; SetProxyMapping(ArrProxyMapping.Select(e => e.GetProxyMapping()).ToArray()); for (var i = 0; i < MAX_SOCKET_SERVER_COUNT; i++) { if (ArrProxyMapping[i].IsEnabled) { ArrThreadForward[i] = new Thread(SocketForward); ArrThreadForward[i].Start(i); } } } catch (Exception ex) { CbException?.Invoke(ex); } }
private void SocketForward(object obj) { var idx = (int)obj; try { var pm = ArrProxyMapping[idx]; ArrSocketServer[idx] = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ArrSocketServer[idx].SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); ArrSocketServer[idx].Bind(pm.GetLocalEndPoint()); ArrSocketServer[idx].Listen(int.MaxValue); while (IsRunning) { var curNum = Interlocked.Increment(ref _numSocket); var localSocket = ArrSocketServer[idx].Accept(); var remoteSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); remoteSocket.Connect(pm.GetRemoteEndPoint()); var socket = new ProxySocket(curNum, localSocket, remoteSocket); lock (DicSocket) { DicSocket.Add(curNum, socket); } ConnectionBuilt?.Invoke(socket); } } catch (Exception ex) { if (ArrSocketServer[idx] == null || !ArrSocketServer[idx].Connected) { return; } if (!(ex is ThreadAbortException)) { CbException?.Invoke(ex); } } }