コード例 #1
0
        private async Task <byte[]> GetPacketBytesAsync(Socket socket, MTProtoPacket mtprotoPacket)
        {
            var packetLengthBytes  = new byte[1];
            var resultPacketLength = await ReceiveAsync(packetLengthBytes, socket).ConfigureAwait(false);

            if (resultPacketLength)
            {
                return(null);
            }
            packetLengthBytes = mtprotoPacket.DecryptObfuscated2(packetLengthBytes);

            var packetLength = BitConverter.ToInt32(packetLengthBytes.Concat(new byte[] { 0x00, 0x00, 0x00 }).ToArray(), 0);

            int lengthBytes;

            if (packetLength < 0x7F)
            {
                lengthBytes = packetLength << 2;
            }
            else
            {
                var lenBytes          = new byte[3];
                var resultLengthBytes = ReceiveAsync(lenBytes, socket).Result;
                if (resultLengthBytes)
                {
                    return(null);
                }
                lenBytes    = mtprotoPacket.DecryptObfuscated2(lenBytes);
                lengthBytes = BitConverter.ToInt32(lenBytes.Concat(new byte[] { 0x00 }).ToArray(), 0) << 2;
            }
            var packetBytes  = new byte[lengthBytes];
            var resultpacket = ReceiveAsync(packetBytes, socket).Result;

            if (resultpacket)
            {
                return(null);
            }
            packetBytes = mtprotoPacket.DecryptObfuscated2(packetBytes);
            return(packetBytes);
        }
コード例 #2
0
        private async void SenderSocketAsync(Socket socketClient, Socket socketServer, MTProtoPacket mtprotoPacketClient, MTProtoPacket mtprotoPacketServer)
        {
            try
            {
                while (true)
                {
                    var packetBytesClient = await GetPacketBytesAsync(socketClient, mtprotoPacketClient).ConfigureAwait(false);

                    if (packetBytesClient == null)
                    {
                        return;
                    }
                    var packetEnc = mtprotoPacketServer.CreatePacketObfuscated2(packetBytesClient);
                    await socketServer.SendAsync(packetEnc, 0, packetEnc.Length).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                    return;
                }
                Console.WriteLine(e.Message);
            }
        }
コード例 #3
0
        private async void StartSocketAsync(Socket socket)
        {
            try
            {
                _listSocket.Add(socket);
                var randomBuffer = new byte[64];
                await socket.ReceiveAsync(randomBuffer, 0, randomBuffer.Length).ConfigureAwait(false);

                var reversed    = randomBuffer.SubArray(8, 48).Reverse().ToArray();
                var key         = randomBuffer.SubArray(8, 32);
                var keyReversed = reversed.SubArray(0, 32);
                var binSecret   = ArrayUtils.HexToByteArray(_secret);
                key         = SHA256Helper.ComputeHashsum(key.Concat(binSecret).ToArray());
                keyReversed = SHA256Helper.ComputeHashsum(keyReversed.Concat(binSecret).ToArray());

                var mtprotoPacketServer = new MTProtoPacket();
                mtprotoPacketServer.SetInitBufferObfuscated2(randomBuffer, reversed, key, keyReversed);
                byte[] decryptBuf = mtprotoPacketServer.DecryptObfuscated2(randomBuffer).SubArray(56, 8);
                for (int i = 56; i < 64; i++)
                {
                    randomBuffer[i] = decryptBuf[i - 56];
                }
                byte[] res = randomBuffer.SubArray(56, 4);
                for (int i = 0; i < 4; i++)
                {
                    if (res[i] != 0xef)
                    {
                        Console.WriteLine("Error in buffer");
                        return;
                    }
                }

                var dcId = Math.Abs(BitConverter.ToInt16(randomBuffer.SubArray(60, 2), 0));
                Console.WriteLine("Create new connection with dataCenterId:{0}", dcId);
                var ip = _ipServersConfig[dcId - 1];

                Socket        socketClient        = null;
                MTProtoPacket mtprotoPacketClient = null;
                object        lockSend            = new object();
                while (socket.IsConnected())
                {
                    ThrowIfDisposed();
                    var packetBytes = await GetPacketBytesAsync(socket, mtprotoPacketServer).ConfigureAwait(false);

                    if (packetBytes == null)
                    {
                        continue;
                    }
                    if (socketClient == null || !socketClient.Connected)
                    {
                        socketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                        socketClient.Connect(ip, 443);
                        _listSocket.Add(socketClient);
                        mtprotoPacketClient = new MTProtoPacket();
                        await mtprotoPacketClient.SendInitBufferObfuscated2Async(socketClient).ConfigureAwait(false);

                        SenderSocketAsync(socketClient, socket, mtprotoPacketClient, mtprotoPacketServer);
                    }

                    var packet = mtprotoPacketClient.CreatePacketObfuscated2(packetBytes);

                    lock (lockSend)
                    {
                        socketClient.SendAsync(packet, 0, packet.Length).GetAwaiter().GetResult();
                    }
                }
                if (socketClient != null)
                {
                    socketClient.Close();
                    _listSocket.Remove(socketClient);
                }
                Console.WriteLine("A connection was closed");
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                    return;
                }
                Console.WriteLine(e.Message);
            }
            try
            {
                socket.Close();
                _listSocket.Remove(socket);
            }
            catch (Exception e)
            {
                if (e.InnerException != null)
                {
                    Console.WriteLine(e.InnerException.Message);
                    return;
                }
                Console.WriteLine(e.Message);
            }
        }