Skip to content

Commit fd5d5c9

Browse files
committed
refactor(im): centralize proxy disabling logic in base adapter
1 parent c54064d commit fd5d5c9

4 files changed

Lines changed: 29 additions & 14 deletions

File tree

docs/guide/im-bridge/dingtalk.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ DingTalk (钉钉) is ideal for:
3636
|------------|---------|
3737
| `Robot.SingleChat.ReadWrite` | Single chat robot management |
3838
| `qyapi_chat_read` | Read group basic info |
39+
| `qyapi_chat_manage` | Manage group chats (create, update, send messages) |
3940

4041
3. Click to enable each permission (no approval needed for internal apps)
4142

src/cccc/ports/im/adapters/base.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,30 @@ class IMAdapter(ABC):
2222

2323
platform: str = "unknown"
2424

25+
def _log(self, msg: str) -> None:
26+
"""Log a message. Subclasses should override with actual logging."""
27+
pass
28+
29+
def _disable_proxies(self) -> None:
30+
"""
31+
Disable all proxies for WebSocket-based SDKs.
32+
33+
Many IM SDKs (Feishu, DingTalk) use websockets which don't support
34+
SOCKS proxy without python-socks. Setting no_proxy=* and clearing
35+
proxy environment variables ensures reliable WebSocket connections.
36+
"""
37+
import os
38+
os.environ["no_proxy"] = "*"
39+
os.environ["NO_PROXY"] = "*"
40+
self._log("[connect] Set no_proxy=* to bypass all proxies")
41+
42+
proxy_vars = ["ALL_PROXY", "all_proxy", "HTTP_PROXY", "http_proxy",
43+
"HTTPS_PROXY", "https_proxy", "SOCKS_PROXY", "socks_proxy"]
44+
for var in proxy_vars:
45+
if var in os.environ:
46+
del os.environ[var]
47+
self._log(f"[connect] Cleared {var} environment variable")
48+
2549
@abstractmethod
2650
def connect(self) -> bool:
2751
"""

src/cccc/ports/im/adapters/dingtalk.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,9 @@ def connect(self) -> bool:
291291
with self._queue_lock:
292292
self._message_queue.clear()
293293

294+
# Disable all proxies BEFORE importing dingtalk-stream SDK
295+
self._disable_proxies()
296+
294297
# Inbound requires the official SDK (dingtalk-stream) for stream mode.
295298
try:
296299
import dingtalk_stream # type: ignore

src/cccc/ports/im/adapters/feishu.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,7 @@ def connect(self) -> bool:
254254
self._message_queue.clear()
255255

256256
# Disable all proxies BEFORE importing lark SDK
257-
# lark-oapi SDK doesn't support SOCKS proxy without python-socks
258-
# Setting no_proxy=* tells most libraries to bypass proxy for all hosts
259-
import os
260-
os.environ["no_proxy"] = "*"
261-
os.environ["NO_PROXY"] = "*"
262-
self._log("[connect] Set no_proxy=* to bypass all proxies")
263-
264-
# Also clear any existing proxy variables
265-
proxy_vars = ["ALL_PROXY", "all_proxy", "HTTP_PROXY", "http_proxy",
266-
"HTTPS_PROXY", "https_proxy", "SOCKS_PROXY", "socks_proxy"]
267-
for var in proxy_vars:
268-
if var in os.environ:
269-
del os.environ[var]
270-
self._log(f"[connect] Cleared {var} environment variable")
257+
self._disable_proxies()
271258

272259
# Inbound requires the official SDK (lark-oapi) for long connection messaging.
273260
try:

0 commit comments

Comments
 (0)