Beispiel #1
0
 private void onConnected(IAsyncResult res)
 {
     Client = (Client)res.AsyncState;
     try
     {
         Client.Sock.EndConnect(res);
     }
     catch (SocketException ex)
     {
         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));
     }
 }
Beispiel #2
0
 public bool Connect()
 {
     try
     {
         p      = new Socket(SocketType.Stream, ProtocolType.Tcp);
         Client = new Client(p, 65535);
         Client.Sock.Connect(new IPEndPoint(ipAddress, Port));
         //try the greeting.
         //Client.onDataReceived += Client_onDataReceived;
         if (Socks.DoSocksAuth(this, Username, Password))
         {
             if (Socks.SendRequest(Client, enc, Dest, Destport) == SocksError.Granted)
             {
                 return(true);
             }
         }
         return(false);
     }
     catch
     {
         return(false);
     }
 }
Beispiel #3
0
        public static bool DoSocksAuth(Socks5Client p, string Username, string Password)
        {
            AuthTypes auth = Socks.Greet(p.Client);

            if (auth == AuthTypes.Unsupported)
            {
                p.Client.Disconnect();
                return(false);
            }
            if (auth != AuthTypes.None)
            {
                p.enc = new Encryption.SocksEncryption();
                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());
                    //now receive key.

                    byte[] buffer  = new byte[4096];
                    int    keysize = p.Client.Receive(buffer, 0, buffer.Length);
                    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);
                    //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());

                    break;

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

                    buffer  = new byte[4096];
                    keysize = p.Client.Receive(buffer, 0, buffer.Length);
                    p.enc.SetKey(buffer, 0, keysize);
                    //let them know we got it
                    p.Client.Send(new byte[] { (byte)HeaderTypes.Socks5, (byte)HeaderTypes.Zero });
                    //now receive our encryption key.
                    enckeysize = p.Client.Receive(buffer, 0, buffer.Length);
                    //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));
                    //wait for server to confirm we got it.
                    p.Client.Receive(buffer, 0, buffer.Length);
                    //now we share our encryption key.

                    p.Client.Send(p.enc.ShareEncryptionKey());

                    //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);
                    //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);
        }