Example #1
0
        private void onConnected(IAsyncResult res)
        {
            Client = (Client)res.AsyncState;
            try
            {
                Client.Sock.EndConnect(res);
            }
            catch
            {
                this.OnConnected(this, new Socks5ClientArgs(null, SocksError.Failure));
                return;
            }
            //Client.onDataReceived += Client_onDataReceived;
            switch (Socks.Greet(this))
            {
            case 0:
                //disconnect.
                this.OnConnected(this, new Socks5ClientArgs(null, SocksError.Failure));
                return;

            case 1:
                //no login, continue.
                break;

            case 2:
                //requires login. send login.
                if (!Socks.SendLogin(this, Username, Password))
                {
                    this.OnConnected(this, new Socks5ClientArgs(null, SocksError.LoginRequired));
                    return;
                }
                break;
            }
            this.OnConnected(this, new Socks5ClientArgs(Client, Socks.SendRequest(this, Dest, Destport)));
        }
Example #2
0
 public bool ConnectWithEp()
 {
     p      = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     Client = new Client(p, 65535);
     Client.Sock.Connect(ep);
     //try the greeting.
     //Client.onDataReceived += Client_onDataReceived;
     if (Socks.DoSocksAuth(this, Username, Password))
     {
         if (Socks.SendRequest(Client, enc, Dest, Destport) == SocksError.Granted)
         {
             Client.onDataReceived += Client_onDataReceived;
             return(true);
         }
     }
     return(false);
 }
Example #3
0
        public bool Connect()
        {
            try
            {
                p      = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                Client = new Client(p, 2048);
                Client.Sock.Connect(new IPEndPoint(ipAddress, Port));
                //try the greeting.
                //Client.onDataReceived += Client_onDataReceived;
                switch (Socks.Greet(this))
                {
                case 0:
                    //disconnect.
                    return(false);

                case 1:
                    //no login, continue.
                    break;

                case 2:
                    //requires login. send login.
                    if (!Socks.SendLogin(this, Username, Password))
                    {
                        return(false);
                    }
                    break;
                }
                if (Socks.SendRequest(this, Dest, Destport) == SocksError.Granted)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            catch
            {
                return(false);
            }
        }
Example #4
0
 private void onConnected(IAsyncResult res)
 {
     Client = (Client)res.AsyncState;
     try
     {
         Client.Sock.EndConnect(res);
     }
     catch
     {
         this.OnConnected(this, new Socks5ClientArgs(null, SocksError.Failure));
         return;
     }
     if (Socks.DoSocksAuth(this, Username, Password))
     {
         SocksError p = Socks.SendRequest(Client, enc, Dest, Destport);
         Client.onDataReceived += Client_onDataReceived;
         this.OnConnected(this, new Socks5ClientArgs(this, p));
     }
     else
     {
         this.OnConnected(this, new Socks5ClientArgs(this, SocksError.Failure));
     }
 }
Example #5
0
        public static bool DoSocksAuth(Socks5Client p, string Username, string Password)
        {
            AuthTypes auth = Socks.Greet(p.Client, p.UseAuthTypes);

            if (auth == AuthTypes.Unsupported)
            {
                p.Client.Disconnect();
                return(false);
            }
            p.enc = new Encryption.SocksEncryption();
            if (auth != AuthTypes.None)
            {
                switch (auth)
                {
                case AuthTypes.Login:
                    //logged in.
                    p.enc.SetType(AuthTypes.Login);
                    //just reqeust login?

                    break;

                case AuthTypes.SocksBoth:
                    //socksboth.
                    p.enc.SetType(AuthTypes.SocksBoth);
                    p.enc.GenerateKeys();
                    //send public key.
                    p.Client.Send(p.enc.GetPublicKey(), out var code1);
                    //now receive key.

                    byte[] buffer  = new byte[4096];
                    int    keysize = p.Client.Receive(buffer, 0, buffer.Length, out var error);
                    p.enc.SetKey(buffer, 0, keysize);
                    //let them know we got it
                    //now receive our encryption key.
                    int enckeysize = p.Client.Receive(buffer, 0, buffer.Length, out error);
                    //decrypt with our public key.
                    byte[] newkey = new byte[enckeysize];
                    Buffer.BlockCopy(buffer, 0, newkey, 0, enckeysize);
                    p.enc.SetEncKey(p.enc.key.Decrypt(newkey, false));
                    //now we share our encryption key.
                    p.Client.Send(p.enc.ShareEncryptionKey(), out var code2);

                    break;

                case AuthTypes.SocksEncrypt:
                    p.enc.SetType(AuthTypes.SocksEncrypt);
                    p.enc.GenerateKeys();
                    //send public key.
                    p.Client.Send(p.enc.GetPublicKey(), out var code3);
                    //now receive key.

                    buffer  = new byte[4096];
                    keysize = p.Client.Receive(buffer, 0, buffer.Length, out error);
                    p.enc.SetKey(buffer, 0, keysize);
                    //now receive our encryption key.
                    enckeysize = p.Client.Receive(buffer, 0, buffer.Length, out error);
                    //decrypt with our public key.
                    newkey = new byte[enckeysize];
                    Buffer.BlockCopy(buffer, 0, newkey, 0, enckeysize);
                    p.enc.SetEncKey(p.enc.key.Decrypt(newkey, false));
                    //now we share our encryption key.

                    p.Client.Send(p.enc.ShareEncryptionKey(), out var code);

                    //socksencrypt.
                    break;

                case AuthTypes.SocksCompress:
                    p.enc.SetType(AuthTypes.SocksCompress);
                    //sockscompress.
                    break;

                default:
                    p.Client.Disconnect();
                    return(false);
                }
                if (p.enc.GetAuthType() != AuthTypes.Login)
                {
                    //now receive login params.
                    byte[] buff = new byte[1024];
                    int    recv = p.Client.Receive(buff, 0, buff.Length, out var error);
                    //check for
                    if (recv > 0)
                    {
                        //check if socks5 version is 5
                        if (buff[0] == 0x05)
                        {
                            //good.
                            if (buff[1] == (byte)AuthTypes.Login)
                            {
                                if (Username == null || Password == null)
                                {
                                    p.Client.Sock.Close(); return(false);
                                }
                                int ret = Socks.SendLogin(p.Client, Username, Password);
                                if (ret != 1)
                                {
                                    p.Client.Sock.Close();
                                    return(false);
                                }
                            }
                            else
                            {
                                //idk? close for now.
                                p.Client.Disconnect();
                                return(false);
                            }
                        }
                    }
                    else
                    {
                        p.Client.Disconnect();
                        return(false);
                    }
                }
                else
                {
                    if (Username == null || Password == null)
                    {
                        p.Client.Sock.Close(); return(false);
                    }
                    int ret = Socks.SendLogin(p.Client, Username, Password);
                    if (ret != 1)
                    {
                        p.Client.Sock.Close();
                        return(false);
                    }
                }
            }
            return(true);
        }