@simonyea/holysheep-cli 1.7.48 → 1.7.50

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.48",
3
+ "version": "1.7.50",
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",
@@ -49,12 +49,12 @@ async function runClaude(args = []) {
49
49
  ...process.env,
50
50
  ANTHROPIC_API_KEY: undefined,
51
51
  ANTHROPIC_AUTH_TOKEN: apiKey,
52
- ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}`,
52
+ ANTHROPIC_BASE_URL: BASE_URL_ANTHROPIC,
53
53
  CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
54
54
  HOLYSHEEP_CLAUDE_PROCESS_PROXY: '1',
55
55
  HOLYSHEEP_CLAUDE_SESSION_ID: sessionId,
56
- NODE_OPTIONS: undefined,
57
- HS_PROXY_URL: undefined,
56
+ NODE_OPTIONS: `--require ${INJECT_PATH}`,
57
+ HS_PROXY_URL: proxyUrl,
58
58
  HTTP_PROXY: undefined,
59
59
  HTTPS_PROXY: undefined,
60
60
  ALL_PROXY: undefined,
@@ -1,8 +1,13 @@
1
1
  'use strict'
2
2
  /**
3
3
  * 进程级代理注入 — 通过 NODE_OPTIONS=--require 加载
4
- * patch sock.emit 拦截 'connect' 事件,强制所有 TCP 连接走 CONNECT 隧道
5
- * 无论 TLSSocket 用何种方式注册监听器都无法绕过
4
+ *
5
+ * Node.js v22+ 中 tls.connect / undici 不走 net.createConnection,
6
+ * 直接调用 net.Socket.prototype.connect,因此同时 patch 两者。
7
+ *
8
+ * 原理:将所有非本地 TCP 连接重定向到本地 bridge(CONNECT 代理),
9
+ * bridge 再通过 relay-control 分配的 node 节点出口,使 CRS 看到
10
+ * 可信的节点 IP。
6
11
  */
7
12
  const net = require('net')
8
13
  const { URL } = require('url')
@@ -21,54 +26,90 @@ const PROXY_HOST = parsed.hostname
21
26
  const PROXY_PORT = Number(parsed.port) || 80
22
27
  const SKIP = new Set(['127.0.0.1', '::1', 'localhost', '0.0.0.0', PROXY_HOST])
23
28
 
24
- const _orig = net.createConnection
29
+ function shouldProxy(host, port) {
30
+ return !!(port && host && !SKIP.has(host))
31
+ }
32
+
33
+ function setupTunnel(sock, host, port, origEmit) {
34
+ sock.write(`CONNECT ${host}:${port} HTTP/1.1\r\nHost: ${host}:${port}\r\n\r\n`)
35
+ let buf = Buffer.alloc(0)
36
+ sock.on('data', function onData(chunk) {
37
+ buf = Buffer.concat([buf, chunk])
38
+ const i = buf.indexOf('\r\n\r\n')
39
+ if (i === -1) return
40
+ sock.removeListener('data', onData)
41
+ if (!buf.slice(0, i).toString().includes(' 200 ')) {
42
+ sock.emit = origEmit
43
+ sock.destroy(new Error(`CONNECT ${host}:${port} failed: ${buf.slice(0, buf.indexOf('\r\n')).toString()}`))
44
+ return
45
+ }
46
+ const rest = buf.slice(i + 4)
47
+ if (rest.length) sock.unshift(rest)
48
+ sock.emit = origEmit
49
+ origEmit('connect')
50
+ })
51
+ }
52
+
53
+ function patchEmitAndConnect(sock, host, port, connectFn) {
54
+ let tunnelReady = false
55
+ const origEmit = sock.emit.bind(sock)
56
+
57
+ sock.emit = function(type) {
58
+ if (type === 'connect' && !tunnelReady) {
59
+ tunnelReady = true
60
+ setupTunnel(sock, host, port, origEmit)
61
+ return false
62
+ }
63
+ return origEmit.apply(null, arguments)
64
+ }
65
+
66
+ return connectFn()
67
+ }
68
+
69
+ // ── patch net.Socket.prototype.connect (Node.js v22+ TLS / undici) ───────────
70
+ const _origSocketConnect = net.Socket.prototype.connect
71
+
72
+ net.Socket.prototype.connect = function(options) {
73
+ const isObj = options !== null && typeof options === 'object'
74
+ const host = isObj ? String(options.host || options.hostname || '') : String(options || '')
75
+ const port = isObj ? Number(options.port || 0) : Number(arguments[1] || 0)
76
+
77
+ if (!shouldProxy(host, port)) return _origSocketConnect.apply(this, arguments)
78
+
79
+ const sock = this
80
+ const proxyOpts = isObj
81
+ ? { ...options, host: PROXY_HOST, port: PROXY_PORT }
82
+ : { host: PROXY_HOST, port: PROXY_PORT }
83
+
84
+ return patchEmitAndConnect(sock, host, port, () =>
85
+ _origSocketConnect.call(sock, proxyOpts)
86
+ )
87
+ }
88
+
89
+ // ── patch net.createConnection (兼容直接调用的旧代码) ─────────────────────────
90
+ const _origCreate = net.createConnection
25
91
 
26
92
  function proxied(options, cb) {
27
93
  const isObj = options !== null && typeof options === 'object'
28
- const host = isObj
29
- ? String(options.host || options.hostname || 'localhost')
30
- : String(options || 'localhost')
31
- const port = isObj
32
- ? Number(options.port || 0)
33
- : Number(arguments[1] || 0)
94
+ const host = isObj ? String(options.host || options.hostname || 'localhost') : String(options || 'localhost')
95
+ const port = isObj ? Number(options.port || 0) : Number(arguments[1] || 0)
34
96
 
35
- if (!port || SKIP.has(host)) return _orig.apply(this, arguments)
97
+ if (!shouldProxy(host, port)) return _origCreate.apply(this, arguments)
98
+
99
+ const sock = _origCreate({ host: PROXY_HOST, port: PROXY_PORT })
100
+ if (typeof cb === 'function') sock.once('connect', cb)
36
101
 
37
- const sock = _orig({ host: PROXY_HOST, port: PROXY_PORT })
38
102
  let tunnelReady = false
39
103
  const origEmit = sock.emit.bind(sock)
40
-
41
- // patch emit 而不是 on/once:无论监听器怎么注册都能拦截
42
104
  sock.emit = function(type) {
43
105
  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
- })
106
+ tunnelReady = true
107
+ setupTunnel(sock, host, port, origEmit)
65
108
  return false
66
109
  }
67
- // eslint-disable-next-line prefer-rest-params
68
110
  return origEmit.apply(null, arguments)
69
111
  }
70
112
 
71
- if (typeof cb === 'function') sock.once('connect', cb)
72
113
  return sock
73
114
  }
74
115