Exemplo n.º 1
0
        byte[] Authenticate()
        {
            string user, passwd;

            var result = Parse(request, requestLength, out user, out passwd);

            if (result == Socks5ParseResult.NotEnoughData)
            {
                return(null);
            }

            // +-----+--------+
            // | VER | METHOD |
            // +-----+--------+
            // |  1  |   1    |
            // +-----+--------+
            var response = new byte[2];

            response[0] = 0x05;
            if (result == Socks5ParseResult.Success && user == "username" && passwd == "password")
            {
                response[1] = (byte)Socks5Reply.Success;
                state       = Socks5ListenerState.Command;
            }
            else
            {
                response[1] = (byte)Socks5Reply.ConnectionNotAllowed;
                state       = Socks5ListenerState.NegotiateAuthMethod;
            }

            return(response);
        }
Exemplo n.º 2
0
        byte[] NegotiateAuthMethod()
        {
            Socks5AuthMethod[] methods;
            Socks5AuthMethod   method;

            var result = Parse(request, requestLength, out methods);

            if (result == Socks5ParseResult.NotEnoughData)
            {
                return(null);
            }

            if (result == Socks5ParseResult.Success)
            {
                var anonymous = false;
                var userpass  = false;

                for (int i = 0; i < methods.Length; i++)
                {
                    if (methods[i] == Socks5AuthMethod.Anonymous)
                    {
                        anonymous = true;
                    }
                    else if (methods[i] == Socks5AuthMethod.UserPassword)
                    {
                        userpass = true;
                    }
                }

                if (userpass)
                {
                    method = Socks5AuthMethod.UserPassword;
                    state  = Socks5ListenerState.Authenticate;
                }
                else if (anonymous)
                {
                    method = Socks5AuthMethod.Anonymous;
                    state  = Socks5ListenerState.Command;
                }
                else
                {
                    method = Socks5AuthMethod.NotSupported;
                }
            }
            else
            {
                method = Socks5AuthMethod.NotSupported;
            }

            // +-----+--------+
            // | VER | METHOD |
            // +-----+--------+
            // |  1  |   1    |
            // +-----+--------+
            var response = new byte[2];

            response[0] = 0x05;
            response[1] = (byte)method;

            return(response);
        }