Exemple #1
0
 public void SendToTar(byte[] bytes, int length)
 {
     try {
         // 踢掉RSV  FRAG
         if (srcTCP != null)
         {
             ByteUtil.FlushBytes(srcRS, bytes, 3, length - 3);
             Socks5ATYP atyp;
             Socks5Util.ReadAddress(srcRS, out atyp, out tarIPA, out tarHost, out tarPort);
             tarUDPREP  = new IPEndPoint(tarIPA, tarPort);
             tarUDPREP2 = tarUDPREP;
             if (tarUDP == null)
             {
                 tarUDP = new Socket(server.ipep.AddressFamily, SocketType.Dgram, ProtocolType.Udp);
                 ReceiveSorketTarUDPLoop();
             }
             var data = ByteUtil.ReadRestBytes(srcRS);
             tarUDP.SendTo(data, SocketFlags.None, tarUDPREP2);
             DebugUtil.LogFormat("SendToTar {0} size:{1}", tarUDPREP, data.Length);
         }
     }
     catch (Exception e)
     {
         DebugUtil.Error("Failed to SendToTar error." + e);
         Stop();
     }
 }
Exemple #2
0
 private void ReceiveSorketTarTCP(IAsyncResult result)
 {
     try
     {
         var tcp = (TcpClient)result.AsyncState;
         tarTCPSize = tcp.Client.EndReceive(result);
         if (tarTCPSize > 0)
         {
             if (tarMask)
             {
                 Socks5Util.Mask(server.deMaskNum, tarTCPBody, tarTCPSize);
             }
             if (srcMask)
             {
                 Socks5Util.Mask(server.enMaskNum, tarTCPBody, tarTCPSize);
             }
             srcTCP.Client.Send(tarTCPBody, tarTCPSize, SocketFlags.None);
             ReceiveSorketTarTCPLoop();
         }
         else
         {
             Stop();
         }
     }
     catch (Exception e)
     {
         //DebugUtil.Error("Failed to ReceiveSorketSrc error." + e);
         Stop();
     }
 }
Exemple #3
0
 private void SrcSendData(byte[] bytes)
 {
     if (srcMask)
     {
         Socks5Util.Mask(server.enMaskNum, bytes, bytes.Length);
     }
     srcTCP.Client.Send(bytes);
 }
Exemple #4
0
 private void SrcRecvData()
 {
     srcTCPSize = srcTCP.Client.Receive(srcTCPBody);
     if (srcMask)
     {
         Socks5Util.Mask(server.deMaskNum, srcTCPBody, srcTCPSize);
     }
 }
Exemple #5
0
        private void ReceiveSorketTarUDP(IAsyncResult result)
        {
            try
            {
                var udp = (Socket)result.AsyncState;
                tarUDPSize = udp.EndReceiveFrom(result, ref tarUDPREP2);

                ByteUtil.FlushBytes(srcSS);
                ByteUtil.WriteBytes(srcSS, new byte[] { 0, 0, 0 });
                Socks5Util.WriteAddress(srcSS, tarUDPREP);
                ByteUtil.WriteBytes(srcSS, tarUDPBody, 0, tarUDPSize);
                server.UDPSendToSrc(srcSS.ToArray(), srcUDPREP);
                ReceiveSorketTarUDPLoop();
            }
            catch (Exception e)
            {
                //DebugUtil.Error("Failed to ReceiveSorketSrc error." + e);
                Stop();
            }
        }
Exemple #6
0
 /// <summary>
 /// 登录流程
 /// </summary>
 /// <param name="o"></param>
 private void ReceiveSorketAuth()
 {
     try
     {
         if (srcTCP == null || !srcTCP.Connected)
         {
             Stop();
         }
         // 接收登录信息
         SrcRecvData();
         if (srcTCPSize == 0)
         {
             throw (new Exception("Src Link Shutdown!"));
         }
         ByteUtil.FlushBytes(srcRS, srcTCPBody, 0, srcTCPSize);
         version = ByteUtil.ReadUInt8(srcRS);
         methods = ByteUtil.Read1BUInt8s(srcRS);
         if (version != 5)
         {
             throw (new Exception("Version no supported!"));
         }
         // 回复登录信息
         if (server.useUPAuth)
         {
             var methodList = new List <int>(methods);
             if (methodList.Contains((int)Socks5AuthType.USERNAME_PASSWORD))
             {
                 SrcSendData(new byte[] { 0x05, (int)Socks5AuthType.USERNAME_PASSWORD });
                 SrcRecvData();
                 if (srcTCPSize == 0)
                 {
                     throw (new Exception("Src Link Shutdown!"));
                 }
                 ByteUtil.FlushBytes(srcRS, srcTCPBody, 0, srcTCPSize);
                 version = ByteUtil.ReadUInt8(srcRS);
                 if (version != 1)
                 {
                     throw (new Exception("Auth Version no supported!"));
                 }
                 var username = ByteUtil.Read1BString(srcRS);
                 var password = ByteUtil.Read1BString(srcRS);
                 // 暂时不校验
                 SrcSendData(new byte[] { 0x01, 0x00 });
                 //srcClient.Client.Send(new byte[] { 0x01, 0x01}); 校验失败返回非0
             }
             else
             {
                 SrcSendData(new byte[] { 0x05, (int)Socks5AuthType.NO_ACCEPTABLE_METHODS });
                 throw (new Exception("Auth failed!"));
             }
         }
         else
         {
             SrcSendData(new byte[] { 0x05, (int)Socks5AuthType.NO_AUTHENTICATION_REQUIRED });
         }
         // 接收代理请求
         SrcRecvData();
         if (srcTCPSize == 0)
         {
             throw (new Exception("Src Link Shutdown!"));
         }
         ByteUtil.FlushBytes(srcRS, srcTCPBody, 0, srcTCPSize);
         version = ByteUtil.ReadUInt8(srcRS);
         var cmd = (Socks5CMD)ByteUtil.ReadUInt8(srcRS);
         ByteUtil.ReadUInt8(srcRS);
         Socks5ATYP atyp;
         Socks5Util.ReadAddress(srcRS, out atyp, out tarIPA, out tarHost, out tarPort);
         if (version != 5)
         {
             throw (new Exception("Version no supported!"));
         }
         if (cmd == Socks5CMD.CONNECT)
         {
             tarTCP = new TcpClient();
             InitTCPClient(tarTCP);
             tarTCP.Connect(tarHost, tarPort);
             if (!tarTCP.Connected)
             {
                 throw (new Exception("Not connect!"));
             }
             // 回复连接结果
             ByteUtil.FlushBytes(srcSS);
             // Connect不关心返回ip和端口,这里都发0
             // ByteUtil.WriteBytes(srcSS, new byte[]{
             //     0x05, (int)Socks5Reply.Succeeded, 0x00, (int)Socks5ATYP.IPV4,
             //     0x00, 0x00, 0x00, 0x00, 0x00, 0x00
             // });
             ByteUtil.WriteBytes(srcSS, new byte[] { 0x05, (int)Socks5Reply.Succeeded, 0x00 });
             Socks5Util.WriteAddress(srcSS, server.ipep);
             SrcSendData(srcSS.ToArray());
             DebugUtil.LogFormat("TCP {0} => {1}", srcTCP.Client.RemoteEndPoint, tarTCP.Client.RemoteEndPoint);
             ReceiveSorketSrcTCPLoop();
             ReceiveSorketTarTCPLoop();
         }
         else if (cmd == Socks5CMD.BIND)
         {
             DebugUtil.LogFormat("ReceiveSorketAuth size:{0}  version:{1} cmd:{2} atyp:{3} tarHost:{4} tarPort:{5}", srcTCPSize, version, cmd, atyp, tarHost, tarPort);
             throw (new Exception("No suppered BIND!"));
         }
         else if (cmd == Socks5CMD.UDP_ASSOCIATE)
         {
             // ByteUtil.FlushBytes(srcSS);
             // ByteUtil.WriteBytes(srcSS, new byte[]{
             //     0x05, (int)Socks5Reply.CommandNotSupported, 0x00, (int)Socks5ATYP.IPV4,
             //     0x00, 0x00, 0x00, 0x00, 0x00, 0x00
             // });
             // SrcSendData(srcSS.ToArray());
             // Stop();
             // 使用UDP 第一次提交的地址和端口是,客户端的接收端口
             srcUDPREP = new IPEndPoint(((IPEndPoint)srcTCP.Client.RemoteEndPoint).Address, tarPort);
             udpID     = srcUDPREP.ToString();
             DebugUtil.LogFormat("UDP {0} => {1} {2}", srcUDPREP, server.ipep, srcTCP.Client.RemoteEndPoint);
             ByteUtil.FlushBytes(srcSS);
             ByteUtil.WriteBytes(srcSS, new byte[] { 0x05, (int)Socks5Reply.Succeeded, 0x00 });
             Socks5Util.WriteAddress(srcSS, server.ipep);
             SrcSendData(srcSS.ToArray());
             server.UDPJoin(this);
             ReceiveSorketSrcTCPLoop2(); // 空读取
         }
     }
     catch (Exception e)
     {
         DebugUtil.Error("Failed to ReceiveSorketAuth error." + e);
         Stop();
     }
 }