void onAccepted(SelectionKey key, Context context) { TcpTunnel localTunnel = null; try { var localChannel = mServerSocketChannel.Accept(); localTunnel = TunnelFactory.wrap(localChannel, mSelector); short portKey = (short)localChannel.Socket().Port; var destAddress = getDestAddress(localChannel); if (destAddress != null) { TcpTunnel remoteTunnel = TunnelFactory.createTunnelByConfig(destAddress, mSelector, portKey, context); remoteTunnel.isHttpsRequest = localTunnel.isHttpsRequest; remoteTunnel.setBrotherTunnel(localTunnel); localTunnel.setBrotherTunnel(remoteTunnel); remoteTunnel.connect(destAddress); } } catch (Exception ex) { Debug.Fail($"TcpProxyServer onAccepted catch an exception: {ex}"); if (localTunnel != null) { localTunnel.dispose(); } } }
void OnCompleted(object sender, SocketAsyncEventArgs e) { try { byte[] request = Req.GetData(); if (e.SocketError != SocketError.Success) { request[1] = (byte)SocksError.Unreachable; } else { request[1] = 0x00; } Client.Client.Send(request); switch (e.LastOperation) { case SocketAsyncOperation.Connect: //connected; _tcpTunnel = new TcpTunnel(Client.Client, RemoteClient); _tcpTunnel.Start(); break; } } finally { e.Completed -= OnCompleted; e.RemoteEndPoint = null; Sockets.ConnectSaeaPool.Add(e); } }
// Creates a new tcp tunnel to the given device that will use the port from the passed listener. public ITcpTunnel Create(string device, ILog mainLog) { lock (tunnelsLock) { if (tunnels.ContainsKey(device)) { string msg = $"Cannot create more than one tunnel to device {device}"; mainLog.WriteLine(msg); throw new InvalidOperationException(msg); } tunnels [device] = new TcpTunnel(processManager); return(tunnels [device]); } }
private async void OnConnection(TcpSocket tcpSocket) { var connection = new SocketConnection(tcpSocket); tcpSocket.Disposed += delegate { Disconnected?.Invoke(this, connection); }; Connected?.Invoke(this, connection); try { TcpTunnel tunnel = await connection.StartServerLoopReadMessagesAsync(); } catch (Exception) { } }
public void Open() { if (Req.Address == null || Req.Port <= -1) { Client.Client.Disconnect(); return; } var socketArgs = Sockets.ConnectSaeaPool.Take(); socketArgs.RemoteEndPoint = new IPEndPoint(Req.IP, Req.Port); socketArgs.Completed += OnCompleted; RemoteClient.Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); if (!RemoteClient.Sock.ConnectAsync(socketArgs)) { _tcpTunnel = new TcpTunnel(Client.Client, RemoteClient); _tcpTunnel.Start(); } }
public NetworkClientObject() { AddComponent <ProcessorsInitializerComponent>(); Service = ClientFactory.CreateTcpClient("127.0.0.1", 27862); }
private void ProcessClient(object argument) { TcpClient client = (TcpClient)argument; logger.Debug($"Client socket accepted.."); TcpTunnel tunnel = new TcpTunnel(client); logger.Debug($"Client tunnel created.."); ServerLink link = new ServerLink(tunnel); logger.Debug($"Client link created.."); link.RememberRemoteCertAuthority = RememberCertificates; link.NoAuthentication = NoAuthentication; //link.RememberPeerKeys = true; // Get a key from the precomputed keys list string ca, priv; byte[] sign; (ca, priv, sign) = KeyManager.GetNextAvailableKeys(); if (String.IsNullOrEmpty(ca) || String.IsNullOrEmpty(priv) || sign.Length == 0) { logger.Error("GetNextAvailableKeys returned empty data!"); link.Close(); return; } logger.Debug($"Passing certificates into Bifrost.."); link.LoadCertificatesNonBase64(ca, priv, sign); link.OnDataReceived += Link_OnDataReceived; link.OnLinkClosed += Link_OnLinkClosed; logger.Debug($"Performing handshake with client.."); var result = link.PerformHandshake(); if (result.Type == HandshakeResultType.Successful) { logger.Debug($"Handshake was a success!"); var connection = new UserConnection(client, serverLink: link); var user = new ClientData(connection); user.ClientKeys.ServerCertificateAuthority = ca; user.ClientKeys.PrivateKey = priv; user.ClientKeys.SignKey = sign; // for use after handshake and when remembering clientCa (unimplemented) //user.Client.ClientKeys.ClientCertificateAuthority = clientCa; lock (_UserListLock) { if (Clients.Count + 1 > MaxConnections) { link.Close(); return; } Clients.Add(user); } Utilities.RaiseEventOnUIThread(OnUserConnected, user); } else { logger.Info($"Handshake failure: {result.Type}"); link.Close(); } }
private void ConnectThread(CancellationToken cancellationToken, string host, int port) { if (cancellationToken.IsCancellationRequested) { return; } logger.Info($"Attempting to connect to {host}:{port}.."); IsConnecting = true; try { client = new TcpClient(host, port); } catch (Exception ex) { logger.Error($"Connection error: {ex.Message}"); IsConnecting = false; IsConnected = false; return; } logger.Debug($"Connected. Setting up tunnel.."); tunnel = new TcpTunnel(client); logger.Debug($"Setting up link.."); link = new ClientLink(tunnel); link.RememberRemoteCertAuthority = RememberCertificates; link.NoAuthentication = NoAuthentication; logger.Debug($"Creating Keys.."); var(ca, priv, sign) = BifrostNext.CertManager.GenerateKeys(); logger.Debug($"Loading keys into Bifrost.."); link.LoadCertificatesNonBase64(ca, priv, sign); var connection = new UserConnection(client, clientLink: link); var user = new ClientData(connection); user.ClientKeys.ServerCertificateAuthority = ca; user.ClientKeys.PrivateKey = priv; user.ClientKeys.SignKey = sign; ClientData = user; link.OnDataReceived += Link_OnDataReceived; link.OnLinkClosed += Link_OnLinkClosed; var result = link.PerformHandshake(); if (result.Type != HandshakeResultType.Successful) { logger.Warn($"Handshake failed with type {result.Type}"); IsConnecting = false; IsConnected = false; Utilities.RaiseEventOnUIThread(OnClientConnectionChange, this, false); return; } else { logger.Debug($"Handshake was successful!"); IsConnecting = false; IsConnected = true; Utilities.RaiseEventOnUIThread(OnClientConnectionChange, this, true); } }