public void Start(Socket socket) { this._local = socket; try { _remote = new Socket(_remoteEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp); _remote.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true); _id = System.Threading.Interlocked.Increment(ref _maxid); // Connect to the remote endpoint. _relay.onWriteLog(new WriteLogEventArgs($"\r\n{_id}: 连接 {_remoteEP} ...\r\n")); _remoteTime = Environment.TickCount; _remote.BeginConnect(_remoteEP, new AsyncCallback(ConnectCallback), null); } catch (Exception e) { _relay.onError(new RelayErrorEventArgs(e)); this.Close(); } }
private void Socks5Handshake1RecvCallback(IAsyncResult ar) { if (_closed) { return; } try { var len = _remote.EndReceive(ar); if (len == 2) { if (remoteRecvBuffer[0] == 0x05 && remoteRecvBuffer[1] == 0x00) { var bytes = (byte[])((object[])ar.AsyncState)[0]; var bytesLen = (int)((object[])ar.AsyncState)[1]; _remote.BeginSend(bytes, SOCKS5_FIRST_PACKAGE_SIZE, bytesLen - SOCKS5_FIRST_PACKAGE_SIZE, SocketFlags.None, new AsyncCallback(Socks5Handshake2SendCallback), ar.AsyncState); } else { _relay.onWriteLog(new WriteLogEventArgs($"{_id}: 代理需要认证\r\n")); this.Close(); } } else { this.Close(); } } catch (Exception e) { _relay.onError(new RelayErrorEventArgs(e)); this.Close(); } }