Exemple #1
0
        /// <summary>
        /// Starts the synchronous authentication process.
        /// </summary>
        /// <exception cref="ProxyException">Authentication with the proxy server failed.</exception>
        /// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception>
        /// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception>
        /// <exception cref="ObjectDisposedException">The Socket has been closed.</exception>
        private void Authenticate()
        {
            Server.Send(new byte [] { 5, 2, 0, 2 });
            byte[] buffer = ReadBytes(2);
            if (buffer[1] == 255)
            {
                throw new ProxyException("No authentication method accepted.");
            }
            AuthMethod authenticate;

            switch (buffer[1])
            {
            case 0:
                authenticate = new AuthNone(Server);
                break;

            case 2:
                authenticate = new AuthUserPass(Server, Username, Password);
                break;

            default:
                throw new ProtocolViolationException();
            }
            authenticate.Authenticate();
        }
Exemple #2
0
 ///<summary>Processes a SOCKS request from a client and selects an authentication method.</summary>
 ///<param name="Request">The request to process.</param>
 protected override void ProcessRequest(byte [] Request)
 {
     try {
         byte Ret = 255;
         for (int Cnt = 1; Cnt < Request.Length; Cnt++)
         {
             if (Request[Cnt] == 0 && (AuthList == null || AuthList.Count == 0))               //0 = No authentication
             {
                 Ret        = 0;
                 AuthMethod = new AuthNone();
                 break;
             }
             else if (Request[Cnt] == 2 && AuthList != null)                 //2 = user/pass
             {
                 Ret        = 2;
                 AuthMethod = new AuthUserPass(AuthList);
                 if (AuthList != null)
                 {
                     break;
                 }
             }
         }
         Connection.BeginSend(new byte[] { 5, Ret }, 0, 2, SocketFlags.None, new AsyncCallback(this.OnAuthSent), Connection);
     } catch {
         Dispose(false);
     }
 }
Exemple #3
0
        /// <summary>
        /// Starts the synchronous authentication process.
        /// </summary>
        /// <exception cref="ProxyException">Authentication with the proxy server failed.</exception>
        /// <exception cref="ProtocolViolationException">The proxy server uses an invalid protocol.</exception>
        /// <exception cref="SocketException">An operating system error occurs while accessing the Socket.</exception>
        /// <exception cref="ObjectDisposedException">The Socket has been closed.</exception>
        private void Authenticate(byte[] buffer)
        {
            buffer[0] = 5;
            buffer[1] = 2;
            buffer[2] = 0;
            buffer[3] = 2;
            if (Server.Send(buffer, 0, 4, SocketFlags.None) < 4)
            {
                throw new SocketException(10054);
            }

            ReadBytes(buffer, 2);
            if (buffer[1] == 255)
            {
                throw new ProxyException("No authentication method accepted.");
            }

            AuthMethod authenticate;

            switch (buffer[1])
            {
            case 0:
                authenticate = new AuthNone(Server);
                break;

            case 2:
                authenticate = new AuthUserPass(Server, Username, Password);
                break;

            default:
                throw new ProtocolViolationException();
            }

            authenticate.Authenticate();
        }
Exemple #4
0
        private void method_0()
        {
            if (base.Server.Send(new byte[4]
            {
                5,
                2,
                0,
                2
            }) < 4)
            {
                throw new SocketException(10054);
            }
            byte[] array = ReadBytes(2);
            if (array[1] == 255)
            {
                throw new ProxyException("No authentication method accepted.");
            }
            AuthMethod authMethod;

            switch (array[1])
            {
            default:
                throw new ProtocolViolationException();

            case 2:
                authMethod = new AuthUserPass(base.Server, base.Username, method_12());
                break;

            case 0:
                authMethod = new AuthNone(base.Server);
                break;
            }
            authMethod.Authenticate();
        }
Exemple #5
0
        protected override void ProcessRequest(byte[] Methods)
        {
            try
            {
                byte result = 255;

                for (int i = 1; i < Methods.Length; i++)
                {
                    //0 = No authentication
                    if (Methods[i] == 0 && Validator == null)
                    {
                        result     = 0;
                        AuthMethod = new AuthNone();
                        break;
                    }
                    //2 = Username/Password
                    else if (Methods[i] == 2 && Validator != null)
                    {
                        result     = 2;
                        AuthMethod = new AuthUserPass(Validator);
                    }
                }

                Connection.BeginSend(new byte[] { 5, result }, 0, 2, SocketFlags.None, new AsyncCallback(this.OnAuthSent), Connection);
            }
            catch
            {
                Dispose(false);
            }
        }
        /// <summary>
        ///     Called when an authentication reply has been received.
        /// </summary>
        /// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param>
        private void OnAuthReceive(IAsyncResult ar)
        {
            try
            {
                Received += Server.EndReceive(ar);
                if (Received <= 0)
                {
                    throw new SocketException();
                }
            }
            catch (Exception e)
            {
                ProtocolComplete(e);
                return;
            }

            try
            {
                if (Received < 2)
                {
                    Server.BeginReceive(Buffer,
                                        Received,
                                        Buffer.Length - Received,
                                        SocketFlags.None,
                                        OnAuthReceive,
                                        Server);
                }
                else
                {
                    AuthMethod authenticate;
                    switch (Buffer[1])
                    {
                    case 0:
                        authenticate = new AuthNone(Server);
                        break;

                    case 2:
                        authenticate = new AuthUserPass(Server, Username, Password);
                        break;

                    default:
                        ProtocolComplete(new SocketException());
                        return;
                    }

                    authenticate.BeginAuthenticate(OnAuthenticated);
                }
            }
            catch (Exception e)
            {
                ProtocolComplete(e);
            }
        }
Exemple #7
0
        /// <summary>
        /// Called when an authentication reply has been received.
        /// </summary>
        /// <param name="ar">Stores state information for this asynchronous operation as well as any user-defined data.</param>
        private void OnAuthReceive(IAsyncResult ar)
        {
            try
            {
                HandleEndReceive(ar);
            }
            catch (Exception e)
            {
                OnProtocolComplete(e);
                return;
            }

            try
            {
                if (Received < BufferCount)
                {
                    Server.BeginReceive(Buffer, Received, BufferCount - Received, SocketFlags.None,
                                        this.OnAuthReceive, Server);
                }
                else
                {
                    AuthMethod authenticate;
                    switch (Buffer[1])
                    {
                    case 0:
                        authenticate = new AuthNone(Server);
                        break;

                    case 2:
                        authenticate = new AuthUserPass(Server, Username, Password);
                        break;

                    default:
                        OnProtocolComplete(new SocketException());
                        return;
                    }

                    authenticate.BeginAuthenticate(this.OnAuthenticated);
                }
            }
            catch (Exception e)
            {
                OnProtocolComplete(e);
            }
        }
Exemple #8
0
        private void method_6(IAsyncResult iasyncResult_0)
        {
            try
            {
                HandleEndReceive(iasyncResult_0);
            }
            catch (Exception error)
            {
                ProtocolComplete(error);
                return;
            }
            try
            {
                if (base.Received < 2)
                {
                    base.Server.BeginReceive(base.Buffer, base.Received, base.Buffer.Length - base.Received, SocketFlags.None, method_6, base.Server);
                }
                else
                {
                    AuthMethod authMethod;
                    switch (base.Buffer[1])
                    {
                    default:
                        ProtocolComplete(new SocketException());
                        return;

                    case 2:
                        authMethod = new AuthUserPass(base.Server, base.Username, method_12());
                        break;

                    case 0:
                        authMethod = new AuthNone(base.Server);
                        break;
                    }
                    authMethod.BeginAuthenticate(method_7);
                }
            }
            catch (Exception error2)
            {
                ProtocolComplete(error2);
            }
        }
Exemple #9
0
 ///<summary>Processes a SOCKS request from a client and selects an authentication method.</summary>
 ///<param name="Request">The request to process.</param>
 protected override async Task ProcessRequest(byte[] Request)
 {
     try
     {
         //Console.WriteLine("AUTH: " + BitConverter.ToString(Request));
         byte Ret = 255;
         for (int Cnt = 1; Cnt < Request.Length; Cnt++)
         {
             bool authRequired = AuthList != null && AuthList.Keys.Length > 0;
             //Console.WriteLine("AUTH REQUIRED:" + (authRequired ? "YES" : "NO"));
             if (Request[Cnt] == 0 && !authRequired)
             {
                 //0 = No authentication
                 Ret        = 0;
                 AuthMethod = new AuthNone();
                 break;
             }
             else if (Request[Cnt] == 2 && authRequired)
             {
                 //2 = user/pass
                 Ret        = 2;
                 AuthMethod = new AuthUserPass(AuthList);
                 if (AuthList != null)
                 {
                     break;
                 }
             }
         }
         await Connection.SendAsync(new byte[] { 5, Ret }, this.OnAuthSent);
     }
     catch (Exception ex)
     {
         Console.WriteLine("[WARN] " + ex.Message + "\r\n" + ex.StackTrace);
         await Dispose(false);
     }
 }
Exemple #10
0
        protected override void ProcessRequest(NetworkStream stream)
        {
            try
            {
                var        reader     = new BinaryReader(stream);
                var        writer     = new BinaryWriter(stream);
                int        len        = reader.ReadByte();
                byte[]     types      = reader.ReadBytes(len);
                byte       type       = 0xFF;
                AuthMethod authMethod = null;
                foreach (byte b in types)
                {
                    if (b == 0 && _authCallback == null)
                    {
                        // No auth
                        type       = 0;
                        authMethod = new AuthNone();
                        break;
                    }
                    if (b == 2 && _authCallback != null)
                    {
                        // Auth by login/password
                        type       = 2;
                        authMethod = new AuthUserPass(_authCallback);
                    }
                }
                writer.Write(new byte[] { 0x05, type });
                if (authMethod == null)
                {
                    Dispoce(false);
                    return;
                }
                bool success = authMethod.Auth(stream);
                if (!success)
                {
                    Dispoce(false);
                    return;
                }
                byte ver = reader.ReadByte();
                if (ver != 0x05)
                {
                    Dispoce(false);
                    return;
                }
                byte command = reader.ReadByte();
                reader.ReadByte(); // SKIP RESERVED
                switch (command)
                {
                case 0x01:     // CONNECT
                    byte addrType = reader.ReadByte();
                    EndPoint     = GetEndPoint(reader, addrType);
                    RemoteSocket = new Socket(EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                    RemoteSocket.BeginConnect(EndPoint, ConnectCallback, RemoteSocket);
                    break;

                case 0x02:     // BIND
                    // TODO: Impliment binding
                    SendRespoce(0x07);
                    break;

                case 0x03:     // UDP ASSOCIATE
                    // TODO: Impliment association
                    SendRespoce(0x07);
                    break;

                default:
                    SendRespoce(0x07);
                    return;
                }
            }
            catch
            {
                Dispoce(false);
            }
        }