@wenbin_wb/dsh-bridge 1.0.4 → 1.0.5

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.
@@ -151,54 +151,66 @@ export class CustomTunnelClient {
151
151
 
152
152
  _handleHttpRequest(msg) {
153
153
  const { requestId, method, path, headers } = msg;
154
-
155
- // Proxy to local DSH
154
+
155
+ // 过滤掉会干扰本地 HTTP hop-by-hop 头
156
+ const SKIP_HEADERS = new Set([
157
+ 'transfer-encoding', 'connection', 'keep-alive',
158
+ 'proxy-authenticate', 'proxy-authorization',
159
+ 'te', 'trailer', 'upgrade',
160
+ ]);
161
+ const safeHeaders = Object.fromEntries(
162
+ Object.entries(headers ?? {}).filter(([k]) => !SKIP_HEADERS.has(k.toLowerCase()))
163
+ );
164
+
156
165
  const req = httpRequest({
157
166
  host: '127.0.0.1',
158
167
  port: this.localPort,
159
168
  method,
160
- path,
169
+ path: path || '/',
161
170
  headers: {
162
- ...headers,
171
+ ...safeHeaders,
163
172
  host: `127.0.0.1:${this.localPort}`,
164
173
  },
165
174
  }, (res) => {
166
175
  const chunks = [];
167
-
168
- res.on('data', (chunk) => {
169
- chunks.push(chunk);
176
+ res.on('data', (chunk) => { chunks.push(chunk); });
177
+ res.on('error', (err) => {
178
+ this.logger?.error('Response read error: %s', err.message);
179
+ this._sendMessage({
180
+ type: 'response', requestId,
181
+ statusCode: 502,
182
+ headers: { 'content-type': 'text/plain' },
183
+ body: Buffer.from('Response Error').toString('base64'),
184
+ });
170
185
  });
171
-
172
186
  res.on('end', () => {
173
- const body = Buffer.concat(chunks).toString('base64');
174
-
187
+ // 过滤响应头里的 hop-by-hop,防止外层 HTTP 解析出错
188
+ const respHeaders = Object.fromEntries(
189
+ Object.entries(res.headers).filter(([k]) => !SKIP_HEADERS.has(k.toLowerCase()))
190
+ );
175
191
  this._sendMessage({
176
192
  type: 'response',
177
193
  requestId,
178
194
  statusCode: res.statusCode,
179
- headers: res.headers,
180
- body,
195
+ headers: respHeaders,
196
+ body: Buffer.concat(chunks).toString('base64'),
181
197
  });
182
198
  });
183
199
  });
184
-
200
+
185
201
  req.on('error', (err) => {
186
202
  this.logger?.error('Local request failed: %s', err.message);
187
-
188
203
  this._sendMessage({
189
- type: 'response',
190
- requestId,
204
+ type: 'response', requestId,
191
205
  statusCode: 502,
192
206
  headers: { 'content-type': 'text/plain' },
193
- body: Buffer.from('Bad Gateway').toString('base64'),
207
+ body: Buffer.from(`Bad Gateway: ${err.message}`).toString('base64'),
194
208
  });
195
209
  });
196
-
197
- // Send request body if present
210
+
198
211
  if (msg.body) {
199
212
  req.write(Buffer.from(msg.body, 'base64'));
200
213
  }
201
-
202
214
  req.end();
203
215
  }
204
216
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wenbin_wb/dsh-bridge",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "DSH 多渠道接入桥:局域网二维码、自建隧道、机器人集成",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",