@simonyea/holysheep-cli 1.7.42 → 1.7.44
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 +2 -2
- package/src/commands/claude.js +6 -6
- package/src/tools/claude-process-proxy.js +106 -72
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@simonyea/holysheep-cli",
|
|
3
|
-
"version": "1.7.
|
|
4
|
-
"description": "Claude Code/Cursor/Cline API relay for China
|
|
3
|
+
"version": "1.7.44",
|
|
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",
|
|
7
7
|
"claude-china",
|
package/src/commands/claude.js
CHANGED
|
@@ -44,16 +44,16 @@ async function runClaude(args = []) {
|
|
|
44
44
|
|
|
45
45
|
const env = {
|
|
46
46
|
...process.env,
|
|
47
|
-
ANTHROPIC_API_KEY: undefined,
|
|
47
|
+
ANTHROPIC_API_KEY: undefined,
|
|
48
48
|
ANTHROPIC_AUTH_TOKEN: apiKey,
|
|
49
|
-
ANTHROPIC_BASE_URL:
|
|
49
|
+
ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}`,
|
|
50
50
|
CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC: '1',
|
|
51
51
|
HOLYSHEEP_CLAUDE_PROCESS_PROXY: '1',
|
|
52
52
|
HOLYSHEEP_CLAUDE_SESSION_ID: sessionId,
|
|
53
|
-
HTTP_PROXY:
|
|
54
|
-
HTTPS_PROXY:
|
|
55
|
-
ALL_PROXY:
|
|
56
|
-
NO_PROXY:
|
|
53
|
+
HTTP_PROXY: undefined,
|
|
54
|
+
HTTPS_PROXY: undefined,
|
|
55
|
+
ALL_PROXY: undefined,
|
|
56
|
+
NO_PROXY: undefined,
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
const child = spawn('claude', args, {
|
|
@@ -58,41 +58,36 @@ async function readJsonResponse(response) {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (cached?.expiresAt && new Date(cached.expiresAt).getTime() - Date.now() > 30_000) {
|
|
64
|
-
return cached
|
|
65
|
-
}
|
|
66
|
-
|
|
61
|
+
// 向 relay 申请新 lease(启动时 + CONNECT 失败时被动重试)
|
|
62
|
+
async function fetchFreshLease(config, sessionId) {
|
|
67
63
|
const controlPlaneUrl = getControlPlaneUrl(config)
|
|
68
64
|
if (!controlPlaneUrl) throw new Error('Claude relay control plane is not configured')
|
|
69
65
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
})
|
|
66
|
+
const response = await fetch(`${controlPlaneUrl}/session/open`, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: { 'content-type': 'application/json' },
|
|
69
|
+
body: JSON.stringify({
|
|
70
|
+
sessionId,
|
|
71
|
+
bridgeId: config.bridgeId || 'local-bridge',
|
|
72
|
+
deviceId: config.deviceId || '',
|
|
73
|
+
installSource: config.installSource || 'holysheep-cli',
|
|
74
|
+
proxyMode: 'claude-process',
|
|
75
|
+
}),
|
|
76
|
+
})
|
|
82
77
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
leaseCache.set(sessionId, payload.data)
|
|
88
|
-
return payload.data
|
|
89
|
-
} catch (error) {
|
|
90
|
-
// 续约失败时,只要旧 lease 还没真正过期就继续用,避免网络抖动导致 session 中断
|
|
91
|
-
if (cached?.expiresAt && new Date(cached.expiresAt).getTime() > Date.now()) {
|
|
92
|
-
return cached
|
|
93
|
-
}
|
|
94
|
-
throw error
|
|
78
|
+
const payload = await response.json().catch(() => null)
|
|
79
|
+
if (!response.ok || !payload?.success || !payload?.data?.ticket) {
|
|
80
|
+
throw new Error(payload?.error?.message || `Failed to open Claude session (HTTP ${response.status})`)
|
|
95
81
|
}
|
|
82
|
+
leaseCache.set(sessionId, payload.data)
|
|
83
|
+
return payload.data
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// 请求路径:只读缓存,不检查过期时间(续约由失败触发,不由时间触发)
|
|
87
|
+
function getCachedLease(sessionId) {
|
|
88
|
+
const cached = leaseCache.get(sessionId)
|
|
89
|
+
if (!cached) throw new Error('No session lease available')
|
|
90
|
+
return cached
|
|
96
91
|
}
|
|
97
92
|
|
|
98
93
|
function buildAuthHeaders(config, lease) {
|
|
@@ -157,84 +152,124 @@ function pipeWithCleanup(a, b) {
|
|
|
157
152
|
|
|
158
153
|
function createProcessProxyServer({ sessionId, configPath = CONFIG_PATH }) {
|
|
159
154
|
const server = http.createServer(async (clientReq, clientRes) => {
|
|
160
|
-
|
|
155
|
+
const isDirect = !clientReq.url.startsWith('http')
|
|
156
|
+
|
|
157
|
+
const doForward = async (lease) => {
|
|
161
158
|
const config = readConfig(configPath)
|
|
162
|
-
|
|
159
|
+
|
|
160
|
+
if (isDirect) {
|
|
161
|
+
const crsBase = config.baseUrlAnthropic || 'https://api.holysheep.ai'
|
|
162
|
+
const target = new URL(clientReq.url, crsBase)
|
|
163
|
+
const fwdHeaders = { ...clientReq.headers, ...buildAuthHeaders(config, lease), 'x-hs-node-proxied': '1', host: target.host }
|
|
164
|
+
const transport = target.protocol === 'https:' ? https : http
|
|
165
|
+
return new Promise((resolve, reject) => {
|
|
166
|
+
const fwd = transport.request({
|
|
167
|
+
hostname: target.hostname,
|
|
168
|
+
port: Number(target.port || (target.protocol === 'https:' ? 443 : 80)),
|
|
169
|
+
method: clientReq.method,
|
|
170
|
+
path: target.pathname + target.search,
|
|
171
|
+
headers: fwdHeaders,
|
|
172
|
+
}, (res) => {
|
|
173
|
+
clientRes.writeHead(res.statusCode || 502, res.headers)
|
|
174
|
+
res.pipe(clientRes)
|
|
175
|
+
resolve()
|
|
176
|
+
})
|
|
177
|
+
fwd.once('error', reject)
|
|
178
|
+
clientReq.pipe(fwd)
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
|
|
163
182
|
const nodeProxyUrl = deriveNodeProxyUrl(lease)
|
|
164
183
|
const headers = {
|
|
165
184
|
...buildAuthHeaders(config, lease),
|
|
166
185
|
host: new URL(clientReq.url).host,
|
|
167
186
|
}
|
|
168
|
-
|
|
169
187
|
const upstream = new URL(nodeProxyUrl)
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
...clientReq.headers,
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
188
|
+
return new Promise((resolve, reject) => {
|
|
189
|
+
const forwardReq = http.request({
|
|
190
|
+
host: upstream.hostname,
|
|
191
|
+
port: Number(upstream.port || 80),
|
|
192
|
+
method: clientReq.method,
|
|
193
|
+
path: clientReq.url,
|
|
194
|
+
headers: { ...clientReq.headers, ...headers, connection: 'close' },
|
|
195
|
+
}, (forwardRes) => {
|
|
196
|
+
clientRes.writeHead(forwardRes.statusCode || 502, forwardRes.headers)
|
|
197
|
+
forwardRes.pipe(clientRes)
|
|
198
|
+
resolve()
|
|
199
|
+
})
|
|
200
|
+
forwardReq.once('error', reject)
|
|
201
|
+
clientReq.pipe(forwardReq)
|
|
183
202
|
})
|
|
203
|
+
}
|
|
184
204
|
|
|
185
|
-
|
|
205
|
+
try {
|
|
206
|
+
await doForward(getCachedLease(sessionId))
|
|
207
|
+
} catch {
|
|
208
|
+
try {
|
|
209
|
+
const config = readConfig(configPath)
|
|
210
|
+
leaseCache.delete(sessionId)
|
|
211
|
+
const freshLease = await fetchFreshLease(config, sessionId)
|
|
212
|
+
await doForward(freshLease)
|
|
213
|
+
} catch (retryError) {
|
|
186
214
|
clientRes.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' })
|
|
187
|
-
clientRes.end(
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
clientReq.pipe(forwardReq)
|
|
191
|
-
} catch (error) {
|
|
192
|
-
clientRes.writeHead(500, { 'content-type': 'text/plain; charset=utf-8' })
|
|
193
|
-
clientRes.end(error.message || 'Proxy error')
|
|
215
|
+
clientRes.end(retryError.message || 'Proxy error')
|
|
216
|
+
}
|
|
194
217
|
}
|
|
195
218
|
})
|
|
196
219
|
|
|
197
220
|
server.on('connect', async (req, clientSocket, head) => {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
|
|
206
|
-
return clientSocket.destroy()
|
|
207
|
-
}
|
|
221
|
+
const target = String(req.url || '').trim()
|
|
222
|
+
const [host, rawPort] = target.split(':')
|
|
223
|
+
const port = Number(rawPort || 443)
|
|
224
|
+
if (!host || !Number.isInteger(port) || ![80, 443].includes(port)) {
|
|
225
|
+
clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
|
|
226
|
+
return clientSocket.destroy()
|
|
227
|
+
}
|
|
208
228
|
|
|
229
|
+
const doConnect = async (lease) => {
|
|
209
230
|
const upstreamSocket = await createConnectTunnel(
|
|
210
231
|
deriveNodeProxyUrl(lease),
|
|
211
232
|
target,
|
|
212
|
-
buildAuthHeaders(
|
|
233
|
+
buildAuthHeaders(readConfig(configPath), lease)
|
|
213
234
|
)
|
|
214
|
-
|
|
215
235
|
clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n')
|
|
216
236
|
if (head?.length) upstreamSocket.write(head)
|
|
217
237
|
pipeWithCleanup(clientSocket, upstreamSocket)
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
try {
|
|
241
|
+
await doConnect(getCachedLease(sessionId))
|
|
242
|
+
} catch {
|
|
243
|
+
// lease 失效,拿新 lease 重试一次
|
|
244
|
+
try {
|
|
245
|
+
const config = readConfig(configPath)
|
|
246
|
+
leaseCache.delete(sessionId)
|
|
247
|
+
const freshLease = await fetchFreshLease(config, sessionId)
|
|
248
|
+
await doConnect(freshLease)
|
|
249
|
+
} catch (retryError) {
|
|
250
|
+
clientSocket.write(`HTTP/1.1 502 Bad Gateway\r\ncontent-type: text/plain; charset=utf-8\r\n\r\n${retryError.message}`)
|
|
251
|
+
clientSocket.destroy()
|
|
252
|
+
}
|
|
221
253
|
}
|
|
222
254
|
})
|
|
223
255
|
|
|
224
256
|
return server
|
|
225
257
|
}
|
|
226
258
|
|
|
227
|
-
function startProcessProxy({ port = null, sessionId = null, configPath = CONFIG_PATH } = {}) {
|
|
259
|
+
async function startProcessProxy({ port = null, sessionId = null, configPath = CONFIG_PATH } = {}) {
|
|
228
260
|
const config = readConfig(configPath)
|
|
229
261
|
const preferredPort = port || getProcessProxyPort(config)
|
|
230
262
|
const effectiveSessionId = sessionId || crypto.randomUUID()
|
|
263
|
+
|
|
264
|
+
// 启动时拿一次 lease,之后靠被动重试维持,不再主动续约
|
|
265
|
+
await fetchFreshLease(config, effectiveSessionId)
|
|
266
|
+
|
|
231
267
|
const server = createProcessProxyServer({ sessionId: effectiveSessionId, configPath })
|
|
232
268
|
|
|
233
269
|
return new Promise((resolve, reject) => {
|
|
234
270
|
const tryListen = (p) => {
|
|
235
271
|
server.once('error', (err) => {
|
|
236
272
|
if (err.code === 'EADDRINUSE') {
|
|
237
|
-
// 端口被占用,让 OS 分配一个随机可用端口
|
|
238
273
|
server.once('error', reject)
|
|
239
274
|
server.listen(0, '127.0.0.1')
|
|
240
275
|
} else {
|
|
@@ -272,7 +307,6 @@ module.exports = {
|
|
|
272
307
|
getProcessProxyPort,
|
|
273
308
|
getControlPlaneUrl,
|
|
274
309
|
readConfig,
|
|
275
|
-
requestSessionLease,
|
|
276
310
|
startProcessProxy,
|
|
277
311
|
writeConfig,
|
|
278
312
|
}
|