电报Api-创建授权密钥404错误 [英] Telegram Api - Creating an Authorization Key 404 error

查看:220
本文介绍了电报Api-创建授权密钥404错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在python中编写一个简单的程序以使用电报api(而不是bot api,主要消息传递api)现在我已编写此代码

I am trying to write a simple program in python to use telegram api, (not bot api, main messaging api) Now i have written this code

#!/usr/bin/env python

import socket
import random
import time
import struct
import requests

def swap64(i):
        return struct.unpack("<L", struct.pack(">L", i))[0]


MESSAGE = '0000000000000000'+format(swap32(int(time.time()*1000%1000)<<21|random.randint(0,1048575)<<3|4),'x')+format(swap32(int(time.time())),'x')+'140000007897466068edeaecd1372139bbb0394b6fd775d3'

res = requests.post(url='http://149.154.167.40',
                    data=bytes.fromhex(MESSAGE),
                    headers={'connection': 'keep-alive'})


print("received data:", res)

对于邮政数据的有效负载,我使用了电报网络的源代码,0身份ID,消息ID是使用电报网络中的算法生成的,下一个是长度(14000000),就像在源中一样,并且

For payload of post data i used the source code of telegram web, The 0 auth id, message id is generated using the algo in telegram web, next is length (14000000) just like in the source and main doc and then the method and so on, When i run this code i get received data: <Response [404]> i have used both tcp and http transport with this and tcp one gives me nothing as answer from server, i don't know where i'm wrong in my code i would be glad if someone can show the error in my code btw here is hex dump of my generated req:

0000   34 08 04 17 7a ec 48 5d 60 84 ba ed 08 00 45 00
0010   00 50 c6 07 40 00 40 06 76 28 c0 a8 01 0d 95 9a
0020   a7 28 c9 62 00 50 0d 1a 3b df 41 5a 40 7f 50 18
0030   72 10 ca 39 00 00 00 00 00 00 00 00 00 00 6c 28
0040   22 4a 94 a9 c9 56 14 00 00 00 78 97 46 60 68 ed
0050   ea ec d1 37 21 39 bb b0 39 4b 6f d7 75 d3

我已经阅读了

i have already read this and this and many other docs but cant find out my problem thanks in advance

我按照建议使用了此代码

i used this code as suggested

TCP_IP = '149.154.167.40'
TCP_PORT = 80

MESSAGE = 'ef0000000000000000'+"{0:0{1}x}".format(int(time.time()*4294.967296*1000),16)+'140000007897466068edeaecd1372139bbb0394b6fd775d3'

BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(bytes.fromhex(MESSAGE))
data = s.recv(BUFFER_SIZE)
s.close()

,我仍然没有回应 我的请求的十六进制转储:

and i still get no response hex dump of my request:

0000   34 08 04 17 7a ec 48 5d 60 84 ba ed 08 00 45 00
0010   00 51 e1 44 40 00 40 06 5a ea c0 a8 01 0d 95 9a
0020   a7 28 df 8c 00 50 e4 0d 12 46 e2 98 bf a3 50 18
0030   72 10 af 66 00 00 ef 00 00 00 00 00 00 00 00 00
0040   16 37 dc e1 28 39 23 14 00 00 00 78 97 46 60 68
0050   ed ea ec d1 37 21 39 bb b0 39 4b 6f d7 75 d3

固定代码

最后使它与这段代码一起使用

Fixed code

Finally got it working with this code

import socket
import random
import time
import struct
import requests

def swap32(i):
        return struct.unpack("<L", struct.pack(">L", i))[0]

TCP_IP = '149.154.167.40'
TCP_PORT = 80
z = int(time.time()*4294.967296*1000000)
z = format(z,'x')
q = bytearray.fromhex(z)
e = q[::-1].hex()
MESSAGE = 'ef0a0000000000000000'+e+'140000007897466068edeaecd1372139bbb0394b6fd775d3'

BUFFER_SIZE = 1024

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(bytes.fromhex(MESSAGE))
data = s.recv(BUFFER_SIZE)
s.close()

print(data)

推荐答案

以下是来自与电报服务器进行简单TCP握手的示例数据:

here is sample data from a simple TCP handshake with Telegram Servers:

Connect:Success:0
Connected to 149.154.167.40:443

    raw_data: 000000000000000000F011DB3B2AA9561400000078974660A9729A4F5B51F18F7943F9C0D61B1315
 auth_key_id: 0000000000000000  0
  message_id: 56A92A3BDB11F000  6244568794892726272
 data_length: 00000014  20
message_data: 78974660A9729A4F5B51F18F7943F9C0D61B1315
message_type: 60469778

>> EF0A000000000000000000F011DB3B2AA9561400000078974660A9729A4F5B51F18F7943F9C0D61B1315
Send:Success:42
Receive:Success:85
<< 15000000000000000001CC0CC93D2AA9564000000063241605A9729A4F5B51F18F7943F9C0D61B1315B4445B94718B3C6DD4136466FAC62DCD082311272BE9FF8F9700000015C4B51C01000000216BE86C022BB4C3

    raw_data: 000000000000000001CC0CC93D2AA9564000000063241605A9729A4F5B51F18F7943F9C0D61B1315B4445B94718B3C6DD4136466FAC62DCD082311272BE9FF8F9700000015C4B51C01000000216BE86C022BB4C3
 auth_key_id: 0000000000000000  0
  message_id: 56A92A3DC90CCC01  6244568803180334081
 data_length: 00000040  64
message_data: 63241605A9729A4F5B51F18F7943F9C0D61B1315B4445B94718B3C6DD4136466FAC62DCD082311272BE9FF8F9700000015C4B51C01000000216BE86C022BB4C3
message_type: 05162463


     classid: resPQ#05162463
       nonce: A9729A4F5B51F18F7943F9C0D61B1315
server_nonce: B4445B94718B3C6DD4136466FAC62DCD
          pq: 2311272BE9FF8F97  2526843935494475671
       count: 00000001  1
fingerprints: C3B42B026CE86B21  14101943622620965665

让我们分解一下:

  1. 我们使用的是TCP简化版,因此我们从0xEF开始

  1. We are using the TCP abridged version, so we start off with 0xEF

纯文本电报消息的格式为auth_ke_id + msg_id + msg_len + msg

The format for plain-text Telegram messages is auth_ke_id + msg_id + msg_len + msg

auth_key_id始终为0,因此我们始终以0000000000000000

auth_key_id is always 0 for plain-text messages hence we always start with 0000000000000000

msg_id必须近似等于unixtime*2^32(请参阅在这里),我还发现,该版本的某些变体对于msg_id在任何平台上的任何语言下都非常有效:whole_part_of(current_micro_second_time_stamp * 4294.967296)

msg_id must approximately equal unixtime*2^32(see here) I have also seen that some variant of this works quite well for msg_id in any language on any platform: whole_part_of(current_micro_second_time_stamp * 4294.967296)

用于生成Auth_key的第一条消息是reqPQ,其定义为:reqPQ#0x60469778 {:nonce, :int128},因此它只是一个TL标头+ 128位随机整数,总长度始终为4 + 16 = 20编码为小尾数,即msg_len = 14000000

The first message you start with for Auth_key generation is reqPQ which is defined as: reqPQ#0x60469778 {:nonce, :int128} so it is simply a TL-header + a 128-bit random integer the total length will always be 4 + 16 = 20 encoded as little-endian that would be msg_len = 14000000

假设我们有一个128位随机整数= 55555555555555555555555555555555555,那么我们的reqPQ消息将是7897466055555555555555555555555555555555555555,这是TL型60469778或little-endian中的78974660,然后随机选择128位随机数.

say we have a 128-bit random integer= 55555555555555555555555555555555, then our reqPQ message would be 7897466055555555555555555555555555555555, which is simply TL-type 60469778 or 78974660 in little-endian followed by your randomly chooses 128-bit nonce.

在发送数据包之前,再次回想一下,TCP简化模式要求您将总数据包长度包括在初始0xEA之后的其他字节之前.该数据包长度计算如下

Before you send out the packet, again recall that TCP-abridged mode required you to include the total packet length in front of the other bytes just after the initial 0xEA . This packet length is computed as follows

let len = total_length/4

let len = total_length / 4

a)如果len < 127然后len_header = len as byte

b)如果len >=127,则len_header = 0x7f + to_3_byte_little_endian(len)

最后我们有:

EF0A000000000000000000F011DB3B2AA956140000007897466055555555555555555555555555555555

EF0A
0000000000000000
00F011DB3B2AA956
14000000
78974660
55555555555555555555555555555555

与您的相比:

0000000000000000
6C28224A94A9C956
14000000
78974660
68EDEAECD1372139BBB0394B6FD775D3

我想说的是,尝试使用TCP简化模式,方法是包含0xEF起始位,然后重新检查您的msg_id计算

I would say, try using TCP-abriged mode by include the 0xEF starting bit and re-check your msg_id computation

欢呼.

这篇关于电报Api-创建授权密钥404错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆