如何将有效的 TCP 消息发送到 .NET 框架上的电报服务器?
How to send valid TCP messages to a telegram server on .NET framework?
所以我想使用 .NET 框架创建一个电报客户端。
我在 telegram 网站上看到了 this page,这是授权过程和客户端与服务器之间交换消息的示例。好吧,我的程序可以生成所描述的 40 字节消息,该消息必须作为请求发送到服务器,并且服务器应该 return 返回 84 字节消息。
但是我在 TCP 的电报定制方面遇到了困难。我的程序生成 40 字节的请求并将其提供给配置为:SocketType.Stream
和 ProtocolType.Tcp
的 .NET TCP 套接字。所以我通过套接字发送这个字节数组,而我从服务器接收到的只是一个 00 字节数组。我怀疑 .NET 框架上的 TCP 实现向我的字节数组添加了一些东西(序列号、校验和数据……),而电报服务器需要原始的 40 字节。此外,SocketType.Raw
似乎不适用于 TCP,因此我无法实际测试这种可能性。
这里有人对电报协议和 .NET 库有一些经验吗?
完整的 (C#) 代码(如有必要):
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;
}
编辑:有关 msg1() 的更多详细信息
msg1() 输出示例:
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() 来源(有点乱):
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();
}
以下将起作用:请参阅以下说明:
EF0A
0000000000000000
00203DC050937B58
14000000
78974660
00BB27062BF84D9BBE9C7BB192559FE5
解释:
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
(参见 here)
14000000 -- length
78974660 -- TL type-code for req_pq#60469778
00BB27062BF84D9BBE9C7BB192559FE5 -- random 128 bit nonce
所以我想使用 .NET 框架创建一个电报客户端。
我在 telegram 网站上看到了 this page,这是授权过程和客户端与服务器之间交换消息的示例。好吧,我的程序可以生成所描述的 40 字节消息,该消息必须作为请求发送到服务器,并且服务器应该 return 返回 84 字节消息。
但是我在 TCP 的电报定制方面遇到了困难。我的程序生成 40 字节的请求并将其提供给配置为:SocketType.Stream
和 ProtocolType.Tcp
的 .NET TCP 套接字。所以我通过套接字发送这个字节数组,而我从服务器接收到的只是一个 00 字节数组。我怀疑 .NET 框架上的 TCP 实现向我的字节数组添加了一些东西(序列号、校验和数据……),而电报服务器需要原始的 40 字节。此外,SocketType.Raw
似乎不适用于 TCP,因此我无法实际测试这种可能性。
这里有人对电报协议和 .NET 库有一些经验吗?
完整的 (C#) 代码(如有必要):
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;
}
编辑:有关 msg1() 的更多详细信息
msg1() 输出示例:
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() 来源(有点乱):
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();
}
以下将起作用:请参阅以下说明:
EF0A
0000000000000000
00203DC050937B58
14000000
78974660
00BB27062BF84D9BBE9C7BB192559FE5
解释:
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
(参见 here)
14000000 -- length
78974660 -- TL type-code for req_pq#60469778
00BB27062BF84D9BBE9C7BB192559FE5 -- random 128 bit nonce