@simonyea/holysheep-cli 1.7.46 → 1.7.47

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simonyea/holysheep-cli",
3
- "version": "1.7.46",
3
+ "version": "1.7.47",
4
4
  "description": "Claude Code/Cursor/Cline API relay for China \u2014 \u00a51=$1, WeChat/Alipay payment, no credit card, no VPN. One command setup for all AI coding tools.",
5
5
  "keywords": [
6
6
  "openai-china",
@@ -1,8 +1,8 @@
1
1
  'use strict'
2
2
  /**
3
3
  * 进程级代理注入 — 通过 NODE_OPTIONS=--require 加载
4
- * net.createConnection 最底层拦截所有 TCP 连接,强制走 CONNECT 隧道
5
- * 使得所有出口流量(undici / https / 任意 HTTP 客户端)都通过 node 节点 IP 出去
4
+ * patch sock.emit 拦截 'connect' 事件,强制所有 TCP 连接走 CONNECT 隧道
5
+ * 无论 TLSSocket 用何种方式注册监听器都无法绕过
6
6
  */
7
7
  const net = require('net')
8
8
  const { URL } = require('url')
@@ -34,54 +34,41 @@ function proxied(options, cb) {
34
34
 
35
35
  if (!port || SKIP.has(host)) return _orig.apply(this, arguments)
36
36
 
37
- // 建立到本地 bridge CONNECT 代理的 TCP 连接
38
37
  const sock = _orig({ host: PROXY_HOST, port: PROXY_PORT })
39
- const pending = []
38
+ let tunnelReady = false
39
+ const origEmit = sock.emit.bind(sock)
40
40
 
41
- // 暂时拦截 'connect' 事件,等 CONNECT 隧道建好才放行
42
- const _on = sock.on.bind(sock)
43
- const _once = sock.once.bind(sock)
44
- sock.on = function(event, listener) {
45
- if (event === 'connect') { pending.push(['on', listener]); return sock }
46
- return _on(event, listener)
41
+ // patch emit 而不是 on/once:无论监听器怎么注册都能拦截
42
+ sock.emit = function(type) {
43
+ if (type === 'connect' && !tunnelReady) {
44
+ // proxy TCP 已建立,发起 CONNECT 隧道
45
+ sock.write(`CONNECT ${host}:${port} HTTP/1.1\r\nHost: ${host}:${port}\r\n\r\n`)
46
+ let buf = Buffer.alloc(0)
47
+ sock.on('data', function onData(chunk) {
48
+ buf = Buffer.concat([buf, chunk])
49
+ const i = buf.indexOf('\r\n\r\n')
50
+ if (i === -1) return
51
+ sock.removeListener('data', onData)
52
+ const header = buf.slice(0, i).toString()
53
+ if (!header.includes(' 200 ')) {
54
+ sock.emit = origEmit
55
+ sock.destroy(new Error(`CONNECT ${host}:${port} failed: ${header.split('\r\n')[0]}`))
56
+ return
57
+ }
58
+ const rest = buf.slice(i + 4)
59
+ if (rest.length) sock.unshift(rest)
60
+ // 隧道就绪:恢复 emit,触发所有注册的 connect 监听器(含 TLSSocket)
61
+ tunnelReady = true
62
+ sock.emit = origEmit
63
+ origEmit('connect')
64
+ })
65
+ return false
66
+ }
67
+ // eslint-disable-next-line prefer-rest-params
68
+ return origEmit.apply(null, arguments)
47
69
  }
48
- sock.once = function(event, listener) {
49
- if (event === 'connect') { pending.push(['once', listener]); return sock }
50
- return _once(event, listener)
51
- }
52
- if (typeof cb === 'function') pending.push(['once', cb])
53
-
54
- // TCP 到 proxy 建立后,发送 CONNECT 请求
55
- _once('connect', () => {
56
- sock.write(`CONNECT ${host}:${port} HTTP/1.1\r\nHost: ${host}:${port}\r\n\r\n`)
57
- let buf = Buffer.alloc(0)
58
- _on('data', function onData(chunk) {
59
- buf = Buffer.concat([buf, chunk])
60
- const i = buf.indexOf('\r\n\r\n')
61
- if (i === -1) return
62
- sock.removeListener('data', onData)
63
-
64
- const header = buf.slice(0, i).toString()
65
- if (!header.includes(' 200 ')) {
66
- sock.destroy(new Error(`CONNECT ${host}:${port} failed: ${header.split('\r\n')[0]}`))
67
- return
68
- }
69
-
70
- const rest = buf.slice(i + 4)
71
- if (rest.length) sock.unshift(rest)
72
-
73
- // 隧道建好:恢复 on/once,触发所有等待中的 connect 监听器
74
- sock.on = _on
75
- sock.once = _once
76
- for (const [type, listener] of pending) {
77
- if (type === 'once') sock.once('connect', listener)
78
- else sock.on('connect', listener)
79
- }
80
- pending.length = 0
81
- sock.emit('connect')
82
- })
83
- })
84
70
 
71
+ if (typeof cb === 'function') sock.once('connect', cb)
85
72
  return sock
86
73
  }
87
74