dsh-provider-proxy 0.4.0 → 0.4.1

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/index.js CHANGED
@@ -1128,6 +1128,13 @@ function createProxyTable(config) {
1128
1128
  );
1129
1129
  continue;
1130
1130
  }
1131
+ if (route.protocol === "socks5" && route.password.length > 0 && route.username.length === 0) {
1132
+ problems.set(
1133
+ provider,
1134
+ "dsh-provider-proxy: SOCKS5 \u914D\u7F6E\u4E86\u5BC6\u7801\u4F46\u6CA1\u6709\u7528\u6237\u540D\uFF1B\u8BF7\u8865\u5168\u7528\u6237\u540D\u6216\u6E05\u7A7A\u5BC6\u7801"
1135
+ );
1136
+ continue;
1137
+ }
1131
1138
  if (route.protocol !== "socks5" && route.password.length > 0 && route.username.length === 0) {
1132
1139
  problems.set(
1133
1140
  provider,
@@ -1135,6 +1142,13 @@ function createProxyTable(config) {
1135
1142
  );
1136
1143
  continue;
1137
1144
  }
1145
+ if (route.protocol !== "socks5" && route.username.length > 0 && route.password.length === 0) {
1146
+ problems.set(
1147
+ provider,
1148
+ "dsh-provider-proxy: \u914D\u7F6E\u4E86\u4EE3\u7406\u7528\u6237\u540D\u4F46\u6CA1\u6709\u5BC6\u7801\uFF1B\u8BF7\u8865\u5168\u5BC6\u7801\u6216\u6E05\u7A7A\u7528\u6237\u540D"
1149
+ );
1150
+ continue;
1151
+ }
1138
1152
  const url = proxyUrl(route);
1139
1153
  let dispatcher;
1140
1154
  try {
@@ -1181,6 +1195,7 @@ function apply(ctx, config) {
1181
1195
  };
1182
1196
  const DEFAULT_TEST_URL = "https://api.ipify.org?format=json";
1183
1197
  const TEST_TIMEOUT_MS = 1e4;
1198
+ const MAX_REQUEST_BYTES = 8 * 1024;
1184
1199
  const selfChecks = /* @__PURE__ */ new Map();
1185
1200
  const plainTestUrl = () => {
1186
1201
  const raw = config?.testUrl;
@@ -1188,6 +1203,7 @@ function apply(ctx, config) {
1188
1203
  const value = typeof snapshot2 === "function" ? snapshot2.call(raw) : raw;
1189
1204
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : DEFAULT_TEST_URL;
1190
1205
  };
1206
+ const bareMessage = (message) => message.replace(/^dsh-provider-proxy:\s*/, "");
1191
1207
  const causesOf = (error) => {
1192
1208
  const out = [];
1193
1209
  const seen = /* @__PURE__ */ new Set();
@@ -1225,6 +1241,10 @@ function apply(ctx, config) {
1225
1241
  const runSelfCheck = async (provider) => {
1226
1242
  const route = current.get(provider);
1227
1243
  if (route === void 0) {
1244
+ const problem = problems.get(provider);
1245
+ if (problem !== void 0) {
1246
+ return { ok: false, kind: "route-rejected", error: bareMessage(problem) };
1247
+ }
1228
1248
  return { ok: false, kind: "no-route", error: `provider "${provider}" \u672A\u542F\u7528\u4EE3\u7406` };
1229
1249
  }
1230
1250
  const started = performance.now();
@@ -1308,7 +1328,9 @@ function apply(ctx, config) {
1308
1328
  try {
1309
1329
  const text = JSON.stringify({
1310
1330
  ...catalog(),
1311
- problems: Object.fromEntries(problems),
1331
+ problems: Object.fromEntries(
1332
+ [...problems].map(([id, message]) => [id, bareMessage(message)])
1333
+ ),
1312
1334
  hits: Object.fromEntries(hits)
1313
1335
  });
1314
1336
  res.writeHead(200, {
@@ -1327,7 +1349,10 @@ function apply(ctx, config) {
1327
1349
  kind: "exact",
1328
1350
  path: "/dsh-provider-proxy/test",
1329
1351
  handler: (req, res) => {
1352
+ let replied = false;
1330
1353
  const send = (status, payload) => {
1354
+ if (replied) return;
1355
+ replied = true;
1331
1356
  const text = JSON.stringify(payload);
1332
1357
  res.writeHead(status, {
1333
1358
  "content-type": "application/json; charset=utf-8",
@@ -1344,10 +1369,24 @@ function apply(ctx, config) {
1344
1369
  send(400, { ok: false, kind: "body", error: "\u65E0\u6CD5\u8BFB\u53D6\u8BF7\u6C42\u4F53" });
1345
1370
  return;
1346
1371
  }
1372
+ let bytes = 0;
1347
1373
  const chunks = [];
1348
- req.on("data", (chunk) => chunks.push(chunk));
1374
+ req.on("data", (chunk) => {
1375
+ if (replied) return;
1376
+ bytes += chunk.length;
1377
+ if (bytes > MAX_REQUEST_BYTES) {
1378
+ send(413, {
1379
+ ok: false,
1380
+ kind: "body",
1381
+ error: `\u8BF7\u6C42\u4F53\u8D85\u8FC7 ${MAX_REQUEST_BYTES} \u5B57\u8282\u4E0A\u9650`
1382
+ });
1383
+ return;
1384
+ }
1385
+ chunks.push(chunk);
1386
+ });
1349
1387
  req.on("error", () => send(400, { ok: false, kind: "body", error: "\u8BFB\u53D6\u8BF7\u6C42\u4F53\u5931\u8D25" }));
1350
1388
  req.on("end", () => {
1389
+ if (replied) return;
1351
1390
  void (async () => {
1352
1391
  let provider;
1353
1392
  try {
@@ -1391,7 +1430,6 @@ function apply(ctx, config) {
1391
1430
  return router.run(
1392
1431
  {
1393
1432
  dispatcher: route.dispatcher,
1394
- provider: WEB_SEARCH_KEY,
1395
1433
  onHit: () => bump(WEB_SEARCH_KEY),
1396
1434
  decide: route.decide
1397
1435
  },
@@ -1401,7 +1439,7 @@ function apply(ctx, config) {
1401
1439
  web.search = patched;
1402
1440
  const registerEffect = typeof wctx?.effect === "function" ? wctx.effect.bind(wctx) : ctx.effect?.bind(ctx);
1403
1441
  registerEffect?.(() => () => {
1404
- if (web.search === patched) web.search = original;
1442
+ if (Object.getOwnPropertyDescriptor(web, "search")?.value === patched) delete web.search;
1405
1443
  });
1406
1444
  });
1407
1445
  ctx.effect(() => {
@@ -1415,7 +1453,6 @@ function apply(ctx, config) {
1415
1453
  return router.wrap(
1416
1454
  {
1417
1455
  dispatcher: route.dispatcher,
1418
- provider,
1419
1456
  onHit: () => bump(provider),
1420
1457
  decide: route.decide
1421
1458
  },