Telethon/Pyrogram连接超时

Telethon/Pyrogram连接超时

1.背景

在使用Telethon/Pyrogram创建电报应用程序时总是报TimeoutError的错误,但是Telegram连接正常。2025年8月30日记录
程序部署在docker镜像中,docker提前部署了mihomo,(详细的docker compose见下)

2.遇到的问题

在运行自定义的Telethon/Pyrogram应用程序时,已经配置好了代理的环境变量

1
2
3
environment:
- HTTP_PROXY=http://192.168.3.21:20171
- HTTPS_PROXY=http://192.168.3.21:20171

但是总是会出现连接失败,如下的错误日志:

1
2
3
4
5
6
7
8
9
10
2025-08-30 07:26:21,049 - root - INFO - 唯一约束已存在,跳过创建
2025-08-30 07:26:21,049 - models.db_operations - INFO - 初始化UFB客户端
2025-08-30 07:26:21,049 - models.db_operations - INFO - UFB未启用
2025-08-30 07:26:21,049 - telethon.network.mtprotosender - INFO - Connecting to 91.108.56.130:443/TcpFull...
2025-08-30 07:26:31,057 - telethon.network.mtprotosender - WARNING - Attempt 1 at connecting failed: TimeoutError:
2025-08-30 07:26:42,064 - telethon.network.mtprotosender - WARNING - Attempt 2 at connecting failed: TimeoutError:
2025-08-30 07:26:53,077 - telethon.network.mtprotosender - WARNING - Attempt 3 at connecting failed: TimeoutError:
2025-08-30 07:27:04,094 - telethon.network.mtprotosender - WARNING - Attempt 4 at connecting failed: TimeoutError:
2025-08-30 07:27:15,103 - telethon.network.mtprotosender - WARNING - Attempt 5 at connecting failed: TimeoutError:
2025-08-30 07:27:26,129 - telethon.network.mtprotosender - WARNING - Attempt 6 at connecting failed: TimeoutError:

修改代码使用使用SOCKS5代理或者HTTP代理也没有解决(添加类似如下代码)

1
2
3
4
5
6
7
8
9
10
11
from telethon import TelegramClient
from telethon.network.connection.tcpabridged import ConnectionTcpAbridged
from telethon.sessions import StringSession
proxy = ('socks5', 'proxy_host', proxy_port, True, 'username', 'password')
client = TelegramClient(
session=StringSession(session_str),
api_id=api_id,
api_hash=api_hash,
connection=ConnectionTcpAbridged,
proxy=proxy
)

3.解决方案

最终,在修改mihomo的规则后连接成功,主要添加 Telegram 相关的 IP 段规则

修改后 添加了 8 条 IP-CIDR 规则,指向 Telegram 组。

1
2
3
4
5
6
7
8
9
# 添加 Telegram 相关的 IP 段规则
- IP-CIDR,91.108.4.0/22,Telegram,no-resolve
- IP-CIDR,91.108.8.0/22,Telegram,no-resolve
- IP-CIDR,91.108.12.0/22,Telegram,no-resolve
- IP-CIDR,91.108.16.0/22,Telegram,no-resolve
- IP-CIDR,91.108.20.0/22,Telegram,no-resolve
- IP-CIDR,91.108.56.0/22,Telegram,no-resolve
- IP-CIDR,149.154.160.0/20,Telegram,no-resolve
- IP-CIDR,185.76.151.0/24,Telegram,no-resolve

原规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
mixed-port: 7890
socks-port: 7891
port: 7892
redir-port: 7893
tproxy-port: 7894
allow-lan: true
bind-address: '*'
mode: rule
log-level: silent
external-controller: '0.0.0.0:9090'

tun:
enable: true
stack: mixed
dns-hijack:
- "any:53"
- "tcp://any:53"
auto-route: true
auto-redirect: true
auto-detect-interface: true

geodata-mode: true
geox-url:
geoip: "https://mirror.ghproxy.com/https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip-lite.dat"
geosite: "https://mirror.ghproxy.com/https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat"
mmdb: "https://mirror.ghproxy.com/https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country-lite.mmdb"
asn: "https://mirror.ghproxy.com/https://github.com/xishang0128/geoip/releases/download/latest/GeoLite2-ASN.mmdb"

dns:
enable: true
ipv6: true
respect-rules: true
enhanced-mode: fake-ip
fake-ip-filter:
- "*"
- "+.lan"
- "+.local"
- "+.market.xiaomi.com"
nameserver:
- https://120.53.53.53/dns-query
- https://223.5.5.5/dns-query
proxy-server-nameserver:
- https://120.53.53.53/dns-query
- https://223.5.5.5/dns-query
nameserver-policy:
"geosite:cn,private":
- https://120.53.53.53/dns-query
- https://223.5.5.5/dns-query
"geosite:geolocation-!cn":
- "https://dns.cloudflare.com/dns-query"
- "https://dns.google/dns-query"

proxy-groups:

- name: 默认
type: select
proxies: [自动选择, 直连, 国内节点, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点] # 新增国内节点选项

# ========== 服务分组 ==========
- name: Google
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

- name: Telegram
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

- name: Twitter
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

- name: 哔哩哔哩
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

- name: 巴哈姆特
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

- name: YouTube
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

- name: NETFLIX
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

- name: Spotify
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

- name: Github
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

- name: 其他
type: select
proxies: [默认, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 自动选择, 直连]

# ========== 新增国内节点组 ==========
- name: 国内节点
type: select
include-all: true
filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|移动|联通|电信|cn|china"

# ========== 地区分组(全部排除国内节点) ==========
- name: 香港
type: select
include-all: true
filter: "(?i)港|hk|hongkong|hong kong"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china" # 排除国内节点

- name: 台湾
type: select
include-all: true
filter: "(?i)台|tw|taiwan"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china" # 排除国内节点

- name: 日本
type: select
include-all: true
filter: "(?i)日|jp|japan"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china" # 排除国内节点

- name: 美国
type: select
include-all: true
filter: "(?i)美|us|unitedstates|united states"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china" # 排除国内节点

- name: 新加坡
type: select
include-all: true
filter: "(?i)(新|sg|singapore)"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china" # 排除国内节点

- name: 其它地区
type: select
include-all: true
exclude-filter: "(?i)(新|sg|singapore|港|hk|hongkong|hong kong|台|tw|taiwan|日|jp|japan|美|us|unitedstates|united states|回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china)" # 增加国内节点排除

- name: 全部节点
type: select
include-all: true

- name: 自动选择
type: url-test
include-all: true
tolerance: 10
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china" # 排除国内节点

proxies:
- name: "直连"
type: direct
udp: true

rules:
# 基础规则
- GEOIP,lan,直连,no-resolve
- GEOSITE,private,直连,no-resolve
- GEOSITE,category-ads-all,REJECT

# 国内服务直连
- GEOSITE,cn,直连
- GEOSITE,onedrive,直连
- GEOSITE,microsoft@cn,直连
- GEOSITE,apple-cn,直连
- GEOSITE,steam@cn,直连
- GEOSITE,category-games@cn,直连
- GEOSITE,bilibili,直连 # 国内版直连

# 国际服务分流
- GEOSITE,biliintl,哔哩哔哩 # 国际版走代理组
- GEOSITE,github,Github
- GEOSITE,twitter,Twitter
- GEOSITE,youtube,YouTube
- GEOSITE,google,Google
- GEOSITE,telegram,Telegram
- GEOSITE,netflix,NETFLIX
- GEOSITE,bahamut,巴哈姆特
- GEOSITE,spotify,Spotify

# 特殊规则
- GEOSITE,category-scholar-!cn,默认 # 非中国学术网站
- GEOSITE,geolocation-!cn,其他 # 非中国域名

# IP规则
- GEOIP,private,直连,no-resolve
- GEOIP,telegram,Telegram,no-resolve
- GEOIP,twitter,Twitter,no-resolve
- GEOIP,google,Google,no-resolve
- GEOIP,netflix,NETFLIX,no-resolve
- GEOIP,CN,直连,no-resolve
- GEOIP,JP,其他,no-resolve # 日本IP代理

# 端口规则
- DST-PORT,80/8080/443/8443,其他

# 最终规则
- MATCH,其他

修改后的规则

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# 基础配置
mixed-port: 7890
socks-port: 7891
port: 7892
redir-port: 7893
tproxy-port: 7894
allow-lan: true
bind-address: '*'
mode: rule
log-level: silent
external-controller: '0.0.0.0:9090'
secret: ''

# 网络增强
tun:
enable: true
stack: mixed
dns-hijack:
- "any:53"
- "tcp://any:53"
auto-route: true
auto-redirect: true
auto-detect-interface: true

# 地理数据源
geodata-mode: true
geox-url:
geoip: "https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geoip-lite.dat"
geosite: "https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/geosite.dat"
mmdb: "https://github.com/MetaCubeX/meta-rules-dat/releases/download/latest/country-lite.mmdb"
asn: "https://github.com/xishang0128/geoip/releases/download/latest/GeoLite2-ASN.mmdb"

# DNS配置
dns:
enable: true
ipv6: true
respect-rules: true
enhanced-mode: fake-ip
fake-ip-filter:
- "*"
- "+.lan"
- "+.local"
- "+.market.xiaomi.com"
nameserver:
- https://120.53.53.53/dns-query
- https://223.5.5.5/dns-query
proxy-server-nameserver:
- https://120.53.53.53/dns-query
- https://223.5.5.5/dns-query
nameserver-policy:
"geosite:cn,private":
- https://120.53.53.53/dns-query
- https://223.5.5.5/dns-query
"geosite:geolocation-!cn":
- "https://dns.cloudflare.com/dns-query"
- "https://dns.google/dns-query"
# 添加 Telegram 特定的 DNS 策略
"geosite:telegram":
- "https://dns.cloudflare.com/dns-query"
- "https://dns.google/dns-query"

# 代理分组
proxy-groups:
- name: 默认
type: select
proxies: [自动选择, 直连, 国内节点, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点]

# ========== 服务分组 ==========
- name: Google
type: select
proxies: [自动选择, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 直连]

- name: Telegram
type: url-test
url: "https://www.google.com/generate_204"
interval: 300
tolerance: 50
include-all: true
filter: "(?i)(港|hk|hongkong|hong kong|台|tw|taiwan|日|jp|japan|美|us|unitedstates|united states|新|sg|singapore)"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china"

- name: Twitter
type: select
proxies: [自动选择, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 直连]

- name: 哔哩哔哩
type: select
proxies: [直连, 自动选择, 香港, 台湾, 日本]

- name: 巴哈姆特
type: select
proxies: [自动选择, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 直连]

- name: YouTube
type: select
proxies: [自动选择, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 直连]

- name: NETFLIX
type: select
proxies: [自动选择, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 直连]

- name: Spotify
type: select
proxies: [自动选择, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 直连]

- name: Github
type: select
proxies: [自动选择, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 直连]

- name: 其他
type: select
proxies: [自动选择, 香港, 台湾, 日本, 新加坡, 美国, 其它地区, 全部节点, 直连]

# ========== 节点分组 ==========
- name: 国内节点
type: select
include-all: true
filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|移动|联通|电信|cn|china"

- name: 香港
type: select
include-all: true
filter: "(?i)港|hk|hongkong|hong kong"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china"

- name: 台湾
type: select
include-all: true
filter: "(?i)台|tw|taiwan"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china"

- name: 日本
type: select
include-all: true
filter: "(?i)日|jp|japan"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china"

- name: 美国
type: select
include-all: true
filter: "(?i)美|us|unitedstates|united states"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china"

- name: 新加坡
type: select
include-all: true
filter: "(?i)(新|sg|singapore)"
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china"

- name: 其它地区
type: select
include-all: true
exclude-filter: "(?i)(新|sg|singapore|港|hk|hongkong|hong kong|台|tw|taiwan|日|jp|japan|美|us|unitedstates|united states|回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china)"

- name: 全部节点
type: select
include-all: true

- name: 自动选择
type: url-test
include-all: true
tolerance: 10
exclude-filter: "(?i)回国|安徽|北京|重庆|福建|甘肃|广东|广西|贵州|海南|河北|黑龙江|河南|湖北|湖南|吉林|江苏|江西|辽宁|内蒙古|宁夏|青海|山东|山西|陕西|上海|四川|天津|西藏|新疆|云南|浙江|官网|国内|国际|移动|联通|电信|cn|china"

proxies:
- name: "直连"
type: direct
udp: true

rules:
# 基础规则
- GEOIP,lan,直连,no-resolve
- GEOSITE,private,直连,no-resolve
- GEOSITE,category-ads-all,REJECT

# 国内服务直连
- GEOSITE,cn,直连
- GEOSITE,onedrive,直连
- GEOSITE,microsoft@cn,直连
- GEOSITE,apple-cn,直连
- GEOSITE,steam@cn,直连
- GEOSITE,category-games@cn,直连
- GEOSITE,bilibili,直连

# 国际服务分流
- GEOSITE,biliintl,哔哩哔哩
- GEOSITE,github,Github
- GEOSITE,twitter,Twitter
- GEOSITE,youtube,YouTube
- GEOSITE,google,Google
- GEOSITE,telegram,Telegram
- GEOSITE,netflix,NETFLIX
- GEOSITE,bahamut,巴哈姆特
- GEOSITE,spotify,Spotify

# 添加 Telegram 相关的 IP 段规则
- IP-CIDR,91.108.4.0/22,Telegram,no-resolve
- IP-CIDR,91.108.8.0/22,Telegram,no-resolve
- IP-CIDR,91.108.12.0/22,Telegram,no-resolve
- IP-CIDR,91.108.16.0/22,Telegram,no-resolve
- IP-CIDR,91.108.20.0/22,Telegram,no-resolve
- IP-CIDR,91.108.56.0/22,Telegram,no-resolve
- IP-CIDR,149.154.160.0/20,Telegram,no-resolve
- IP-CIDR,185.76.151.0/24,Telegram,no-resolve

# IP规则
- GEOIP,private,直连,no-resolve
- GEOIP,telegram,Telegram,no-resolve
- GEOIP,twitter,Twitter,no-resolve
- GEOIP,google,Google,no-resolve
- GEOIP,netflix,NETFLIX,no-resolve
- GEOIP,CN,直连,no-resolve
- GEOIP,JP,其他,no-resolve

# 特殊规则
- GEOSITE,category-scholar-!cn,默认
- GEOSITE,geolocation-!cn,其他

# 端口规则
- DST-PORT,80/8080/443/8443,其他

# 最终规则
- MATCH,其他