Esempio n. 1
0
        protected override async Task <Socket> ClientCommandReceived(NetworkStream client, byte[] buffer, int length, CancellationToken cancellationToken)
        {
            Socket        server = null;
            Socks4Command cmd;

            byte[]    response;
            IPAddress ip;
            string    user;
            int       port;

            if (TryParse(buffer, length, out cmd, out port, out ip, out user))
            {
                var host = ip.ToString();

                try {
                    server = await SocketUtils.ConnectAsync(host, port, null, true, cancellationToken).ConfigureAwait(false);

                    var remote = (IPEndPoint)server.RemoteEndPoint;

                    response = GetResponse(Socks4Reply.RequestGranted, remote);
                } catch {
                    response = GetResponse(Socks4Reply.RequestRejected, null);
                }
            }
            else
            {
                response = GetResponse(Socks4Reply.RequestRejected, null);
            }

            await client.WriteAsync(response, 0, response.Length, cancellationToken).ConfigureAwait(false);

            return(server);
        }
Esempio n. 2
0
        protected override async Task <Socket> ClientCommandReceived(NetworkStream client, byte[] buffer, int length, CancellationToken cancellationToken)
        {
            byte[] response = null;
            Socket server   = null;

            Buffer.BlockCopy(buffer, 0, request, requestLength, length);
            requestLength += length;

            switch (state)
            {
            case Socks5ListenerState.NegotiateAuthMethod:
                response = NegotiateAuthMethod();
                break;

            case Socks5ListenerState.Authenticate:
                response = Authenticate();
                break;

            case Socks5ListenerState.Command:
                var result = Parse(request, requestLength, out Socks5Command cmd, out string host, out int port);
                switch (result)
                {
                case Socks5ParseResult.Success:
                    try {
                        server = await SocketUtils.ConnectAsync(host, port, null, true, cancellationToken).ConfigureAwait(false);

                        var remote = (IPEndPoint)server.RemoteEndPoint;

                        response = GetCommandResponse(Socks5Reply.Success, remote);
                    } catch (OperationCanceledException) {
                        throw;
                    } catch (SocketException ex) {
                        response = GetCommandResponse(ex.SocketErrorCode);
                    } catch {
                        response = GetCommandResponse(Socks5Reply.GeneralServerFailure, null);
                    }
                    break;

                case Socks5ParseResult.InvalidAddrType:
                    response = GetCommandResponse(Socks5Reply.AddressTypeNotSupported, null);
                    break;

                case Socks5ParseResult.InvalidCommand:
                    response = GetCommandResponse(Socks5Reply.CommandNotSupported, null);
                    break;

                case Socks5ParseResult.NotEnoughData:
                    response = null;
                    break;

                case Socks5ParseResult.InvalidRequest:
                default:
                    response = GetCommandResponse(Socks5Reply.GeneralServerFailure, null);
                    break;
                }
                break;
            }

            if (response == null)
            {
                return(null);
            }

            await client.WriteAsync(response, 0, response.Length, cancellationToken).ConfigureAwait(false);

            requestLength = 0;

            return(server);
        }