@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.
- package/lib/tunnel-client.mjs +32 -20
- package/package.json +1 -1
package/lib/tunnel-client.mjs
CHANGED
|
@@ -151,54 +151,66 @@ export class CustomTunnelClient {
|
|
|
151
151
|
|
|
152
152
|
_handleHttpRequest(msg) {
|
|
153
153
|
const { requestId, method, path, headers } = msg;
|
|
154
|
-
|
|
155
|
-
//
|
|
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
|
-
...
|
|
171
|
+
...safeHeaders,
|
|
163
172
|
host: `127.0.0.1:${this.localPort}`,
|
|
164
173
|
},
|
|
165
174
|
}, (res) => {
|
|
166
175
|
const chunks = [];
|
|
167
|
-
|
|
168
|
-
res.on('
|
|
169
|
-
|
|
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
|
-
|
|
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:
|
|
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(
|
|
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
|
|