Esempio n. 1
0
 private void ConfigureServer()
 {
     if (_socksServer != null)
     {
         _socksServer.Stop();
         _socksServer = null;
     }
     if (Port.HasValue)
     {
         if (Port.Value > 0)
         {
             _socksServer = CreateListener(Port.Value);
         }
         else
         {
             for (; ;)
             {
                 try
                 {
                     _socksServer = CreateListener(0);
                     _port        = _socksServer.Port;
                     break;
                 }
                 catch (SocketException)
                 {
                 }
             }
         }
     }
 }
Esempio n. 2
0
        private Socks4 CreateListener(int listeningPort)
        {
            var server = new Socks4(listeningPort, ClientConnect);

            server.OnSendData    += OnSendData;
            server.OnReceiveData += OnReceiveData;
            server.Start();
            return(server);
        }
Esempio n. 3
0
        /// <summary>
        /// Create a TcpClient using a TcpClientType Proxy.
        /// </summary>
        /// <param name="destination">The address destination. Where to CONNECT</param>
        /// <param name="type">The Proxy Type</param>
        /// <param name="userId">A string used with socks 4</param>
        /// <returns></returns>
        public TcpClient CreateTcpClient(SocketAddress destination, TcpClientType type, string userId = "")
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination), $"{nameof(destination)} must be not null");
            }

            var socket = new Socket(SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(ProxyAddress.Host, ProxyAddress.Port);

            try
            {
                if (type == TcpClientType.SOCKS4)
                {
                    var socks = new Socks4(Resolver);
                    socks.CreateTunnel(socket, destination, userId);
                }
                else if (type == TcpClientType.SOCKS4a)
                {
                    var socks = new Socks4a(Resolver);
                    socks.CreateTunnel(socket, destination, userId);
                }
                else
                {
                    var socks = new Socks5(Resolver);
                    socks.CreateTunnel(socket, destination, ProxyCredential);
                }
            }
            catch (Exception e)
            {
                if (socket != null && socket.Connected)
                {
                    socket.Close();
                }

                throw e;
            }

            return(new TcpClient()
            {
                Client = socket
            });
        }