@simonyea/holysheep-cli 1.7.41 → 1.7.43

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.41",
3
+ "version": "1.7.43",
4
4
  "description": "Claude Code/Cursor/Cline API relay for China — ¥1=$1, WeChat/Alipay payment, no credit card, no VPN. One command setup for all AI coding tools.",
5
5
  "keywords": [
6
6
  "openai-china",
@@ -58,12 +58,8 @@ async function readJsonResponse(response) {
58
58
  }
59
59
  }
60
60
 
61
- async function requestSessionLease(config, sessionId) {
62
- const cached = leaseCache.get(sessionId)
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
 
@@ -87,6 +83,13 @@ async function requestSessionLease(config, sessionId) {
87
83
  return payload.data
88
84
  }
89
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
91
+ }
92
+
90
93
  function buildAuthHeaders(config, lease) {
91
94
  return {
92
95
  'x-hs-bridge-id': config.bridgeId || 'local-bridge',
@@ -149,84 +152,100 @@ function pipeWithCleanup(a, b) {
149
152
 
150
153
  function createProcessProxyServer({ sessionId, configPath = CONFIG_PATH }) {
151
154
  const server = http.createServer(async (clientReq, clientRes) => {
152
- try {
155
+ const doForward = async (lease) => {
153
156
  const config = readConfig(configPath)
154
- const lease = await requestSessionLease(config, sessionId)
155
157
  const nodeProxyUrl = deriveNodeProxyUrl(lease)
156
158
  const headers = {
157
159
  ...buildAuthHeaders(config, lease),
158
160
  host: new URL(clientReq.url).host,
159
161
  }
160
-
161
162
  const upstream = new URL(nodeProxyUrl)
162
- const forwardReq = http.request({
163
- host: upstream.hostname,
164
- port: Number(upstream.port || 80),
165
- method: clientReq.method,
166
- path: clientReq.url,
167
- headers: {
168
- ...clientReq.headers,
169
- ...headers,
170
- connection: 'close',
171
- },
172
- }, (forwardRes) => {
173
- clientRes.writeHead(forwardRes.statusCode || 502, forwardRes.headers)
174
- forwardRes.pipe(clientRes)
163
+ return new Promise((resolve, reject) => {
164
+ const forwardReq = http.request({
165
+ host: upstream.hostname,
166
+ port: Number(upstream.port || 80),
167
+ method: clientReq.method,
168
+ path: clientReq.url,
169
+ headers: { ...clientReq.headers, ...headers, connection: 'close' },
170
+ }, (forwardRes) => {
171
+ clientRes.writeHead(forwardRes.statusCode || 502, forwardRes.headers)
172
+ forwardRes.pipe(clientRes)
173
+ resolve()
174
+ })
175
+ forwardReq.once('error', reject)
176
+ clientReq.pipe(forwardReq)
175
177
  })
178
+ }
176
179
 
177
- forwardReq.once('error', (error) => {
180
+ try {
181
+ await doForward(getCachedLease(sessionId))
182
+ } catch {
183
+ // lease 失效,拿新 lease 重试一次
184
+ try {
185
+ const config = readConfig(configPath)
186
+ leaseCache.delete(sessionId)
187
+ const freshLease = await fetchFreshLease(config, sessionId)
188
+ await doForward(freshLease)
189
+ } catch (retryError) {
178
190
  clientRes.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' })
179
- clientRes.end(error.message || 'Proxy error')
180
- })
181
-
182
- clientReq.pipe(forwardReq)
183
- } catch (error) {
184
- clientRes.writeHead(500, { 'content-type': 'text/plain; charset=utf-8' })
185
- clientRes.end(error.message || 'Proxy error')
191
+ clientRes.end(retryError.message || 'Proxy error')
192
+ }
186
193
  }
187
194
  })
188
195
 
189
196
  server.on('connect', async (req, clientSocket, head) => {
190
- try {
191
- const config = readConfig(configPath)
192
- const lease = await requestSessionLease(config, sessionId)
193
- const target = String(req.url || '').trim()
194
- const [host, rawPort] = target.split(':')
195
- const port = Number(rawPort || 443)
196
- if (!host || !Number.isInteger(port) || ![80, 443].includes(port)) {
197
- clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
198
- return clientSocket.destroy()
199
- }
197
+ const target = String(req.url || '').trim()
198
+ const [host, rawPort] = target.split(':')
199
+ const port = Number(rawPort || 443)
200
+ if (!host || !Number.isInteger(port) || ![80, 443].includes(port)) {
201
+ clientSocket.write('HTTP/1.1 403 Forbidden\r\n\r\n')
202
+ return clientSocket.destroy()
203
+ }
200
204
 
205
+ const doConnect = async (lease) => {
201
206
  const upstreamSocket = await createConnectTunnel(
202
207
  deriveNodeProxyUrl(lease),
203
208
  target,
204
- buildAuthHeaders(config, lease)
209
+ buildAuthHeaders(readConfig(configPath), lease)
205
210
  )
206
-
207
211
  clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n')
208
212
  if (head?.length) upstreamSocket.write(head)
209
213
  pipeWithCleanup(clientSocket, upstreamSocket)
210
- } catch (error) {
211
- clientSocket.write(`HTTP/1.1 502 Bad Gateway\r\ncontent-type: text/plain; charset=utf-8\r\n\r\n${error.message}`)
212
- clientSocket.destroy()
214
+ }
215
+
216
+ try {
217
+ await doConnect(getCachedLease(sessionId))
218
+ } catch {
219
+ // lease 失效,拿新 lease 重试一次
220
+ try {
221
+ const config = readConfig(configPath)
222
+ leaseCache.delete(sessionId)
223
+ const freshLease = await fetchFreshLease(config, sessionId)
224
+ await doConnect(freshLease)
225
+ } catch (retryError) {
226
+ clientSocket.write(`HTTP/1.1 502 Bad Gateway\r\ncontent-type: text/plain; charset=utf-8\r\n\r\n${retryError.message}`)
227
+ clientSocket.destroy()
228
+ }
213
229
  }
214
230
  })
215
231
 
216
232
  return server
217
233
  }
218
234
 
219
- function startProcessProxy({ port = null, sessionId = null, configPath = CONFIG_PATH } = {}) {
235
+ async function startProcessProxy({ port = null, sessionId = null, configPath = CONFIG_PATH } = {}) {
220
236
  const config = readConfig(configPath)
221
237
  const preferredPort = port || getProcessProxyPort(config)
222
238
  const effectiveSessionId = sessionId || crypto.randomUUID()
239
+
240
+ // 启动时拿一次 lease,之后靠被动重试维持,不再主动续约
241
+ await fetchFreshLease(config, effectiveSessionId)
242
+
223
243
  const server = createProcessProxyServer({ sessionId: effectiveSessionId, configPath })
224
244
 
225
245
  return new Promise((resolve, reject) => {
226
246
  const tryListen = (p) => {
227
247
  server.once('error', (err) => {
228
248
  if (err.code === 'EADDRINUSE') {
229
- // 端口被占用,让 OS 分配一个随机可用端口
230
249
  server.once('error', reject)
231
250
  server.listen(0, '127.0.0.1')
232
251
  } else {
@@ -264,7 +283,6 @@ module.exports = {
264
283
  getProcessProxyPort,
265
284
  getControlPlaneUrl,
266
285
  readConfig,
267
- requestSessionLease,
268
286
  startProcessProxy,
269
287
  writeConfig,
270
288
  }