Esempio n. 1
0
        public int Receive(byte[] data, int offset, int count)
        {
            try
            {

                int received = this.Sock.Receive(data, offset, count, SocketFlags.None);
                if (received <= 0)
                {
#if DEBUG
                    Console.WriteLine(string.Format("DCing: recvd={0},Err={1}", received, Sock.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Error)));
#endif
                    this.Disconnect();
                    return -1;
                }
                DataEventArgs dargs = new DataEventArgs(data, received);
                //this.onDataReceived(this, dargs);
                return received;
            }
            catch (SocketException ex)
            {
                Console.WriteLine(">>>> Error In Receive! DCing! <<<<");
                this.Disconnect();
                return -1;
            }
        }
Esempio n. 2
0
 public override void OnDataReceived(object sender, DataEventArgs e)
 {
     if (e.Buffer.FindString("HTTP/1.") != -1 && e.Buffer.FindString("\r\n") != -1)
     {
         e.Buffer = e.Buffer.ReplaceString("\r\n", "\r\nX-Served-By: Socks5Server\r\n");
         e.Count = e.Count + "X-Served-By: Socks5Server\r\n".Length;
     }
 }
Esempio n. 3
0
 void Client_onDataSent(object sender, DataEventArgs e)
 {
     this.Stats.AddBytes(e.Count, ByteType.Sent);
     this.Stats.AddPacket(PacketType.Sent);
 }
Esempio n. 4
0
 void Client_onDataReceived(object sender, DataEventArgs e)
 {
     this.Stats.AddBytes(e.Count, ByteType.Received);
     this.Stats.AddPacket(PacketType.Received);
 }
Esempio n. 5
0
 /// <summary>
 /// Allows you to grab/modify data before it's sent to the end server.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public abstract void OnDataSent(object sender, DataEventArgs e);
Esempio n. 6
0
 /// <summary>
 /// Allows you to grab data before it's sent to the end user.
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 public abstract void OnDataReceived(object sender, DataEventArgs e);
Esempio n. 7
0
        void RemoteClient_onDataReceived(object sender, DataEventArgs e)
        {
            e.Request = this.ModifiedReq;
            try
            {
                foreach (DataHandler f in Plugins)
                    if (f.Enabled)
                        f.OnDataReceived(this, e);
                //craft headers & shit.
                byte[] outputdata = se.ProcessOutputData(e.Buffer, e.Offset, e.Count);
                //send outputdata's length firs.t
                Client.Client.Send(BitConverter.GetBytes(outputdata.Length));
                e.Buffer = outputdata;
                e.Offset = 0;
                e.Count = outputdata.Length;
                //ok now send data.
                Client.Client.Send(e.Buffer, e.Offset, e.Count);
                //  if (!RemoteClient.Receiving)
                //       RemoteClient.ReceiveAsyncNew();
                //   if (!Client.Client.Receiving)
                //        Client.Client.ReceiveAsyncNew();

            }
            catch
            {
                Client.Client.Disconnect();
                RemoteClient.Disconnect();
                OnTunnelDisposing();
            }
        }
Esempio n. 8
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.ReceiveAsyncNew();
             //   if (!RemoteClient.Receiving)
             //     RemoteClient.ReceiveAsyncNew();
         }
         else
         {
             throw new Exception();
         }
     }
     catch
     {
         //disconnect.
         Client.Client.Disconnect();
         RemoteClient.Disconnect();
         OnTunnelDisposing();
     }
 }
Esempio n. 9
0
 void RemoteClient_onDataReceived(object sender, DataEventArgs e)
 {
     e.Request = this.ModifiedReq;
     foreach (DataHandler f in Plugins)
         if (f.Enabled)
             f.OnDataReceived(this, e);
     Client.Client.Send(e.Buffer, e.Offset, e.Count);
     // if (!RemoteClient.Receiving)
     //    RemoteClient.ReceiveAsyncNew();
     // if (!Client.Client.Receiving)
     //     Client.Client.ReceiveAsyncNew();
 }
Esempio n. 10
0
        public async Task ReceiveAsyncNew()
        {
            Receiving = true;
            // Reusable SocketAsyncEventArgs and awaitable wrapper 
            var args = new SocketAsyncEventArgs();
            var buffer = BufferManager.DefaultManager.CheckOut();
            args.SetBuffer(buffer, 0, buffer.Length);
            var awaitable = new SocketAwaitable(args);

            // Do processing, continually receiving from the socket 

            while (Sock!=null)
            {
                try
                {
                    if (!Sock.Connected)
                    {
                        break;
                    }
                    await Sock.ReceiveAsync(awaitable);
                    int bytesRead = args.BytesTransferred;
                    if (bytesRead <= 0)
                        break;

                    DataEventArgs data = new DataEventArgs(buffer, bytesRead);
                    this.onDataReceived(this, data);
                }
                catch (SocketException ex)
                {
                    break;
                }
                //catch (NullReferenceException ex)
                //{
                //    break;
                //}
            }
            Receiving = false;
            BufferManager.DefaultManager.CheckIn(buffer);
            Disconnect();
        }
Esempio n. 11
0
 public bool Send(byte[] buff, int offset, int count)
 {
     try
     {
         if (this.Sock != null)
         {
             if (this.Sock.Send(buff, offset, count, SocketFlags.None) <= 0)
             {
                 Console.WriteLine("Send Dcing");
                 this.Disconnect();
                 return false;
             }
             DataEventArgs data = new DataEventArgs(buff, count);
             this.onDataSent(this, data);
             return true;
         }
         return false;
     }
     catch
     {
         Console.WriteLine(">>>> Error In Send! DCing! <<<<");
         this.Disconnect();
         return false;
     }
 }
Esempio n. 12
0
        private void DataSent(IAsyncResult res)
        {
            try
            {
                int sent = ((Socket)res.AsyncState).EndSend(res);

                if (sent < 0)
                {
                    this.Sock.Shutdown(SocketShutdown.Send);
                    Disconnect();
                    return;
                }
#if DEBUG
                Console.WriteLine("Data Sent: " + sent / 1024.0 + "KB");
#endif
                DataEventArgs data = new DataEventArgs(new byte[0] { }, sent);
                this.onDataSent(this, data);
            }
            catch { this.Disconnect(); }
        }
Esempio n. 13
0
 public override void OnDataSent(object sender, DataEventArgs e)
 {
 }
Esempio n. 14
0
        void Client_onDataReceived(object sender, DataEventArgs e)
        {
            //this should be packet header.
            try
            {
                if (enc.GetAuthType() != AuthTypes.Login && enc.GetAuthType() != AuthTypes.None)
                {
                    //get total number of bytes.
                    int torecv = BitConverter.ToInt32(e.Buffer, e.Offset);

                    byte[] newbuff = new byte[torecv];
                    int recv = Client.Receive(newbuff, 0, newbuff.Length);

                    if (recv == torecv)
                    {
                        //yey
                        //process packet.
                        byte[] output = enc.ProcessInputData(newbuff, 0, recv);
                        //receive full packet.
                        e.Buffer = output;
                        e.Offset = 0;
                        e.Count = output.Length;
                        this.OnDataReceived(this, new Socks5ClientDataArgs(this, e.Buffer, e.Count, e.Offset));
                    }
                    else
                    {
                        throw new Exception();
                    }
                }
                else
                {
                    this.OnDataReceived(this, new Socks5ClientDataArgs(this, e.Buffer, e.Count, e.Offset));
                }
            }
            catch
            {
                //disconnect.
                Disconnect();
                throw;
            }
        }