Exemplo n.º 1
0
        public int AuthenticateConnection(ref SocksEncryption encryption)
        {

            List<AuthTypes> authtypes = Socks5.RequestAuth(this);
            if (authtypes.Count <= 0)
            {
                Client.Send(new byte[] { 0x00, 0xFF });
                Console.WriteLine("Client Request ERROR");
                Client.Disconnect();
                return 0;
            }
            this.Authenticated = 0;

            List<object> lhandlers = PluginLoader.LoadPlugin(typeof(LoginHandler));
            bool loginenabled = false;
            if (lhandlers.Count > 0)
            {
                loginenabled = lhandlers.Cast<LoginHandler>().Any(l => l.Enabled);
            }
            //check out different auth types, none will have no authentication, the rest do.
            if (loginenabled && (authtypes.Contains(AuthTypes.SocksBoth) || authtypes.Contains(AuthTypes.SocksEncrypt) || authtypes.Contains(AuthTypes.SocksCompress) || authtypes.Contains(AuthTypes.Login)))
            {
                //this is the preferred method.
                encryption = Socks5.RequestSpecialMode(authtypes, Client);
                foreach (LoginHandler lh in lhandlers)
                {
                    if (lh.Enabled)
                    {
                        //request login.
                        User user = Socks5.RequestLogin(this);
                        if (user == null)
                        {
                            Client.Disconnect();
                            return 0;
                        }
                        LoginStatus status = lh.HandleLogin(user);
                        Client.Send(new byte[] { (byte)HeaderTypes.Socks5, (byte)status });
                        if (status == LoginStatus.Denied)
                        {
                            Console.WriteLine("> Login Denied!");
                            Client.Disconnect();
                            return 0;
                        }
                        else if (status == LoginStatus.Correct)
                        {
                            Authenticated = (encryption.GetAuthType() == AuthTypes.Login ? 1 : 2);
                            break;
                        }
                    }
                }
            }
            else if (authtypes.Contains(AuthTypes.None))
            {
                //no authentication.
                if (lhandlers.Count <= 1)
                {
                    //unsupported methods y0
                    Authenticated = 1;
                    Client.Send(new byte[] { (byte)HeaderTypes.Socks5, (byte)HeaderTypes.Zero });
                }
                else
                {
                    //unsupported.
                    Client.Send(new byte[] { (byte)HeaderTypes.Socks5, (byte)AuthTypes.Unsupported });
                    Client.Disconnect();
                    return 0;
                }
            }
            else
            {
                //unsupported.
                Client.Send(new byte[] { (byte)HeaderTypes.Socks5, (byte)AuthTypes.Unsupported });
                Client.Disconnect();
                return 0;
            }
            return Authenticated;

        }