How to send valid TCP messages to a telegram server on .NET framework?

不想你离开。 提交于 2019-12-24 06:23:40

问题


So I want to create a telegram client using .NET framework.

I read this page on telegram website, which is an example of authorization process and messages exchanged between client and the server. Well, my program can generate the described 40-byte message which must be send to server as request, and the server is supposed to return an 84-byte message back.

However I'm having difficulties with telegram customization of the TCP. My program generates the 40-byte request and feeds it to the .NET TCP socket which is configured to: SocketType.Stream and ProtocolType.Tcp. So I send this byte array through the socket and all I receive from the server is a 00 byte array. I'm suspecting that the TCP implementation on .NET framework adds some stuff (Seq no., Checksum data, ...) to my byte array while telegram server needs the raw 40-bytes. Also it seems that SocketType.Raw doesn't work with TCP so I can't actually test that possibility.

Any guy here with some experience on telegram protocol and .NET library?

The full (C#) code if necessary:

    static void Main(string[] args)
    {
        // Data buffer for incoming data.
        byte[] bytes = new byte[128];

        //addresses
        IPAddress ipAddress = IPAddress.Parse("149.154.167.40");
        IPEndPoint remoteEP = new IPEndPoint(ipAddress,443);

        // Create a TCP/IP  socket.
        Socket sender = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        // Attempt
        sender.Connect(remoteEP);
        Console.WriteLine("Socket connected to {0}",
                sender.RemoteEndPoint.ToString());

        byte[] msg = msg1(); //msg1() returns a 40-byte array

        sender.Send(msg); // Synchronize process for simplicity.  

        sender.Receive(bytes); // Synchronize process for simplicity.  

        print_bytes(bytes);

        try
        {
            sender.Shutdown(SocketShutdown.Both);
            sender.Close();
        }
        catch { } // Sorry for the empty catch block ;)
        Console.ReadKey();
        return;
    }

Edit: More Detail on msg1()

msg1() output example:

00-00-00-00-00-00-00-00-00-20-3D-C0-50-93-7B-58
14-00-00-00-78-97-46-60-00-BB-27-06-2B-F8-4D-9B
BE-9C-7B-B1-92-55-9F-E5

msg1() source (a little messy):

static byte[] msg1()
    {
        var r = new System.Collections.Generic.List<Byte>();
        //length(?) //tried with next lines uncommented, no luck.
        //r.Add(0x0A); //len/4
        //r.Add(0x00);
        //auth_key_id=0 (8 bytes)
        r.AddRange(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 });
        //msg_id =ut*2^32
        ulong unixTimestamp = (ulong)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).Ticks;//ut*10^7
        unixTimestamp = (ulong)(unixTimestamp * 429.4967296);
        unixTimestamp -= unixTimestamp % 4;
        byte[] msgid = BitConverter.GetBytes(unixTimestamp);
        if (!BitConverter.IsLittleEndian)
            Array.Reverse(msgid);
        r.AddRange(msgid);
        //msglength
        int msgl = 20;
        byte[] ml = BitConverter.GetBytes(msgl);
        if (!BitConverter.IsLittleEndian)
            Array.Reverse(msgid);
        r.AddRange(ml);
        int rem = 4 - ml.Length;
        for (int i = 0; i < rem; i++)
            r.Add(0);
        //operation code
        r.AddRange(new byte[] { 0x78, 0x97, 0x46, 0x60 });
        //random number
        Random f = new Random(450639);
        byte[] ran16 = new byte[16];
        f.NextBytes(ran16);
        r.AddRange(ran16);
        //ready to go!
        Console.WriteLine("MESSAGE SENT:\n---\n");
        print_bytes(r.ToArray());
        Console.WriteLine("\n---\n");
        return r.ToArray();
    }

回答1:


The following will work: see the explanation that follows:

EF0A
0000000000000000
00203DC050937B58
14000000
78974660
00BB27062BF84D9BBE9C7BB192559FE5

Explanation:

EF -- session start indicator see here

There is an abridged version of the same protocol: if the client sends 0xef as the first byte (important: only prior to the very first data packet), then packet length is encoded by a single byte (0x01..0x7e = data length divided by 4; or 0x7f followed by 3 length bytes (little endian) divided by 4) followed by the data themselves (sequence number and CRC32 not added).

0A -- total_length/ 4

0000000000000000 -- auth_key_id = 0 for plain-text messages

00203DC050937B58 -- msg_id unixtime*2^32 (see here)

14000000 -- length
78974660 -- TL type-code for req_pq#60469778 
00BB27062BF84D9BBE9C7BB192559FE5 -- random 128 bit nonce 


来源:https://stackoverflow.com/questions/41660162/how-to-send-valid-tcp-messages-to-a-telegram-server-on-net-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!