Beispiel #1
0
        public static SocksError SendRequest(Client cli, SocksEncryption enc, string ipOrDomain, int port)
        {
            AddressType type;
            IPAddress   ipAddress;

            if (!IPAddress.TryParse(ipOrDomain, out ipAddress))
            {
                //it's a domain. :D (hopefully).
                type = AddressType.Domain;
            }
            else
            {
                type = AddressType.IP;
            }
            SocksRequest sr = new SocksRequest(StreamTypes.Stream, type, ipOrDomain, port);

            //send data.
            byte[] p = sr.GetData(false);
            p[1] = 0x01;
            //process data.
            cli.Send(enc.ProcessOutputData(p, 0, p.Length));
            byte[] buffer = new byte[512];
            //process input data.
            int recv = cli.Receive(buffer, 0, buffer.Length);

            if (recv == -1)
            {
                return(SocksError.Failure);
            }
            byte[] buff = enc.ProcessInputData(buffer, 0, recv);

            return((SocksError)buff[1]);
        }
Beispiel #2
0
        public static SocksRequest RequestTunnel(SocksClient client, SocksEncryption ph)
        {
            byte[] data;
            int    recv = Receive(client.Client, out data);

            byte[] buff = ph.ProcessInputData(data, 0, recv);
            if (buff == null || (HeaderTypes)buff[0] != HeaderTypes.Socks5)
            {
                return(null);
            }
            switch ((StreamTypes)buff[1])
            {
            case StreamTypes.Stream:
            {
                int           fwd     = 4;
                StringBuilder address = new StringBuilder();
                switch ((AddressType)buff[3])
                {
                case AddressType.IP:
                {
                    for (int i = 4; i < 8; i++)
                    {
                        //grab IP.
                        address.Append(Convert.ToInt32(buff[i]).ToString() + (i != 7 ? "." : ""));
                    }
                    fwd += 4;
                }
                break;

                case AddressType.Domain:
                {
                    int domainlen = Convert.ToInt32(buff[4]);
                    address.Append(Encoding.ASCII.GetString(buff, 5, domainlen));
                    fwd += domainlen + 1;
                }
                break;

                case AddressType.IPv6:
                    //can't handle IPV6 traffic just yet.
                    return(null);
                }
                byte[] po = new byte[2];
                Array.Copy(buff, fwd, po, 0, 2);
                Int16 x    = BitConverter.ToInt16(po, 0);
                int   port = Convert.ToInt32(IPAddress.NetworkToHostOrder(x));
                port = (port < 1 ? port + 65536 : port);
                return(new SocksRequest(StreamTypes.Stream, (AddressType)buff[3], address.ToString(), port));
            }

            default:
                //not supported.
                return(null);
            }
        }
Beispiel #3
0
 void Client_onDataReceived(object sender, DataEventArgs e)
 {
     e.Request = this.ModifiedReq;
     //this should be packet header.
     try
     {
         int    torecv  = BitConverter.ToInt32(e.Buffer, e.Offset);
         byte[] newbuff = new byte[torecv];
         int    recv    = Client.Client.Receive(newbuff, 0, newbuff.Length);
         if (recv == torecv)
         {
             //yey
             //process packet.
             byte[] output = se.ProcessInputData(newbuff, 0, recv);
             e.Buffer = output;
             e.Offset = 0;
             e.Count  = output.Length;
             //receive full packet.
             foreach (DataHandler f in Plugins)
             {
                 if (f.Enabled)
                 {
                     f.OnDataSent(this, e);
                 }
             }
             RemoteClient.SendAsync(e.Buffer, e.Offset, e.Count);
             if (!Client.Client.Receiving)
             {
                 Client.Client.ReceiveAsync();
             }
             if (!RemoteClient.Receiving)
             {
                 RemoteClient.ReceiveAsync();
             }
         }
         else
         {
             throw new Exception();
         }
     }
     catch
     {
         //disconnect.
         Client.Client.Disconnect();
         RemoteClient.Disconnect();
     }
 }
 void Client_onDataReceived(object sender, DataEventArgs e)
 {
     e.Request = this.ModifiedReq;
     //this should be packet header.
     try
     {
         int    packetsize = BitConverter.ToInt32(e.Buffer, 0);
         byte[] newbuff    = new byte[packetsize];
         //yey
         //process packet.
         byte[] output = se.ProcessInputData(e.Buffer, 4, packetsize);
         e.Buffer = null;
         e.Buffer = output;
         e.Offset = 0;
         e.Count  = output.Length;
         //receive full packet.
         foreach (DataHandler f in Plugins)
         {
             f.OnClientDataReceived(this, e);
         }
         Client.Server.TriggerClientDataReceived(e);
         RemoteClient.SendAsync(e.Buffer, e.Offset, e.Count);
         if (!Client.Client.Receiving)
         {
             Client.Client.ReceiveAsync();
         }
         if (!RemoteClient.Receiving)
         {
             RemoteClient.ReceiveAsync();
         }
     }
     catch
     {
         //disconnect.
         Client.Client.Disconnect();
         RemoteClient.Disconnect();
     }
 }