@logictan/dsh-config-manager 0.1.60
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/LICENSE +21 -0
- package/README.md +105 -0
- package/cordis.patch.yml +21 -0
- package/lib/adapters/agent-instructions.d.ts +13 -0
- package/lib/adapters/agent-instructions.js +46 -0
- package/lib/adapters/agent-presets.d.ts +13 -0
- package/lib/adapters/agent-presets.js +14 -0
- package/lib/adapters/credentials.d.ts +37 -0
- package/lib/adapters/credentials.js +122 -0
- package/lib/adapters/file-collection.d.ts +15 -0
- package/lib/adapters/file-collection.js +127 -0
- package/lib/adapters/index.d.ts +57 -0
- package/lib/adapters/index.js +55 -0
- package/lib/adapters/link-report.d.ts +16 -0
- package/lib/adapters/link-report.js +47 -0
- package/lib/adapters/mcp.d.ts +28 -0
- package/lib/adapters/mcp.js +178 -0
- package/lib/adapters/plugin-files.d.ts +18 -0
- package/lib/adapters/plugin-files.js +142 -0
- package/lib/adapters/plugins.d.ts +41 -0
- package/lib/adapters/plugins.js +603 -0
- package/lib/adapters/pnpm-workspace.d.ts +26 -0
- package/lib/adapters/pnpm-workspace.js +147 -0
- package/lib/adapters/prompts.d.ts +30 -0
- package/lib/adapters/prompts.js +231 -0
- package/lib/adapters/providers.d.ts +34 -0
- package/lib/adapters/providers.js +155 -0
- package/lib/adapters/self.d.ts +17 -0
- package/lib/adapters/self.js +71 -0
- package/lib/adapters/sessions.d.ts +14 -0
- package/lib/adapters/sessions.js +15 -0
- package/lib/adapters/settings.d.ts +28 -0
- package/lib/adapters/settings.js +165 -0
- package/lib/adapters/skills.d.ts +13 -0
- package/lib/adapters/skills.js +14 -0
- package/lib/adapters/test-helpers.d.ts +125 -0
- package/lib/adapters/test-helpers.js +236 -0
- package/lib/adapters/ui.d.ts +27 -0
- package/lib/adapters/ui.js +89 -0
- package/lib/adapters/workspaces.d.ts +13 -0
- package/lib/adapters/workspaces.js +106 -0
- package/lib/client.d.ts +1026 -0
- package/lib/client.js +10047 -0
- package/lib/core/analyzer.d.ts +99 -0
- package/lib/core/analyzer.js +934 -0
- package/lib/core/backup.d.ts +138 -0
- package/lib/core/backup.js +513 -0
- package/lib/core/boot-rescue.d.ts +146 -0
- package/lib/core/boot-rescue.js +618 -0
- package/lib/core/cache-cleaner.d.ts +31 -0
- package/lib/core/cache-cleaner.js +95 -0
- package/lib/core/config-lifecycle.d.ts +180 -0
- package/lib/core/config-lifecycle.js +481 -0
- package/lib/core/config-snapshot.d.ts +135 -0
- package/lib/core/config-snapshot.js +424 -0
- package/lib/core/config-state.d.ts +70 -0
- package/lib/core/config-state.js +177 -0
- package/lib/core/consult-source.d.ts +58 -0
- package/lib/core/consult-source.js +260 -0
- package/lib/core/crash-report.d.ts +84 -0
- package/lib/core/crash-report.js +317 -0
- package/lib/core/exporter.d.ts +68 -0
- package/lib/core/exporter.js +375 -0
- package/lib/core/ghost-sweep.d.ts +19 -0
- package/lib/core/ghost-sweep.js +52 -0
- package/lib/core/importer.d.ts +60 -0
- package/lib/core/importer.js +29 -0
- package/lib/core/index.d.ts +14 -0
- package/lib/core/index.js +14 -0
- package/lib/core/journal.d.ts +197 -0
- package/lib/core/journal.js +418 -0
- package/lib/core/local-plugin-host.d.ts +35 -0
- package/lib/core/local-plugin-host.js +120 -0
- package/lib/core/local-plugin-pack.d.ts +135 -0
- package/lib/core/local-plugin-pack.js +278 -0
- package/lib/core/messages.d.ts +310 -0
- package/lib/core/messages.js +621 -0
- package/lib/core/migration-consult.d.ts +117 -0
- package/lib/core/migration-consult.js +363 -0
- package/lib/core/migration-history.d.ts +153 -0
- package/lib/core/migration-history.js +411 -0
- package/lib/core/model-tools.d.ts +50 -0
- package/lib/core/model-tools.js +148 -0
- package/lib/core/msg-types.d.ts +8 -0
- package/lib/core/msg-types.js +6 -0
- package/lib/core/phase3-child-crash.d.ts +1 -0
- package/lib/core/phase3-child-crash.js +87 -0
- package/lib/core/phase3-host.d.ts +138 -0
- package/lib/core/phase3-host.js +359 -0
- package/lib/core/phase3-prod-child.d.ts +1 -0
- package/lib/core/phase3-prod-child.js +35 -0
- package/lib/core/phase4-crash-child.d.ts +1 -0
- package/lib/core/phase4-crash-child.js +103 -0
- package/lib/core/plugin-cli.d.ts +150 -0
- package/lib/core/plugin-cli.js +497 -0
- package/lib/core/reconcile.d.ts +120 -0
- package/lib/core/reconcile.js +389 -0
- package/lib/core/recovery-orchestrator.d.ts +94 -0
- package/lib/core/recovery-orchestrator.js +327 -0
- package/lib/core/restore.d.ts +162 -0
- package/lib/core/restore.js +703 -0
- package/lib/core/rollback.d.ts +12 -0
- package/lib/core/rollback.js +145 -0
- package/lib/core/run-registry.d.ts +85 -0
- package/lib/core/run-registry.js +157 -0
- package/lib/core/startup-barrier.d.ts +53 -0
- package/lib/core/startup-barrier.js +77 -0
- package/lib/core/types.d.ts +515 -0
- package/lib/core/types.js +27 -0
- package/lib/core/undo.d.ts +73 -0
- package/lib/core/undo.js +87 -0
- package/lib/core/validator.d.ts +18 -0
- package/lib/core/validator.js +77 -0
- package/lib/core/verify-recovery.d.ts +21 -0
- package/lib/core/verify-recovery.js +256 -0
- package/lib/core/watcher.d.ts +101 -0
- package/lib/core/watcher.js +219 -0
- package/lib/index.d.ts +238 -0
- package/lib/index.js +2764 -0
- package/lib/migrations/index.d.ts +29 -0
- package/lib/migrations/index.js +56 -0
- package/lib/migrations/v1-to-v2.d.ts +9 -0
- package/lib/migrations/v1-to-v2.js +10 -0
- package/lib/profiles/index.d.ts +4 -0
- package/lib/profiles/index.js +5 -0
- package/lib/profiles/profile-manager.d.ts +106 -0
- package/lib/profiles/profile-manager.js +543 -0
- package/lib/schema/config.d.ts +28 -0
- package/lib/schema/config.js +165 -0
- package/lib/schema/index.d.ts +24 -0
- package/lib/schema/index.js +26 -0
- package/lib/schema/manifest.d.ts +28 -0
- package/lib/schema/manifest.js +109 -0
- package/lib/schema/tombstones.d.ts +46 -0
- package/lib/schema/tombstones.js +64 -0
- package/lib/schema/types.d.ts +232 -0
- package/lib/schema/types.js +8 -0
- package/lib/schema/versions.d.ts +27 -0
- package/lib/schema/versions.js +50 -0
- package/lib/security/encryption.d.ts +85 -0
- package/lib/security/encryption.js +279 -0
- package/lib/security/index.d.ts +16 -0
- package/lib/security/index.js +17 -0
- package/lib/security/integrity.d.ts +30 -0
- package/lib/security/integrity.js +78 -0
- package/lib/security/redaction.d.ts +26 -0
- package/lib/security/redaction.js +95 -0
- package/lib/security/secret-scanner.d.ts +86 -0
- package/lib/security/secret-scanner.js +404 -0
- package/lib/security/vault.d.ts +59 -0
- package/lib/security/vault.js +151 -0
- package/lib/security/zip-security.d.ts +27 -0
- package/lib/security/zip-security.js +154 -0
- package/lib/sync/autosync-config.d.ts +39 -0
- package/lib/sync/autosync-config.js +151 -0
- package/lib/sync/autosync-scheduler.d.ts +139 -0
- package/lib/sync/autosync-scheduler.js +564 -0
- package/lib/sync/fs.d.ts +18 -0
- package/lib/sync/fs.js +49 -0
- package/lib/sync/git/git-transport.d.ts +100 -0
- package/lib/sync/git/git-transport.js +437 -0
- package/lib/sync/github-auth.d.ts +131 -0
- package/lib/sync/github-auth.js +264 -0
- package/lib/sync/layout.d.ts +43 -0
- package/lib/sync/layout.js +203 -0
- package/lib/sync/snapshot-json.d.ts +9 -0
- package/lib/sync/snapshot-json.js +107 -0
- package/lib/sync/sync-config.d.ts +81 -0
- package/lib/sync/sync-config.js +281 -0
- package/lib/sync/sync-engine.d.ts +269 -0
- package/lib/sync/sync-engine.js +591 -0
- package/lib/sync/sync-history.d.ts +42 -0
- package/lib/sync/sync-history.js +78 -0
- package/lib/sync/sync-selection.d.ts +29 -0
- package/lib/sync/sync-selection.js +127 -0
- package/lib/sync/sync-session.d.ts +40 -0
- package/lib/sync/sync-session.js +54 -0
- package/lib/sync/sync-state.d.ts +37 -0
- package/lib/sync/sync-state.js +108 -0
- package/lib/sync/transport.d.ts +75 -0
- package/lib/sync/transport.js +55 -0
- package/lib/sync/ui-prefs.d.ts +34 -0
- package/lib/sync/ui-prefs.js +109 -0
- package/lib/sync/webdav/webdav-transport.d.ts +77 -0
- package/lib/sync/webdav/webdav-transport.js +451 -0
- package/lib/ui/errors.d.ts +19 -0
- package/lib/ui/errors.js +81 -0
- package/lib/ui/export-flow.d.ts +82 -0
- package/lib/ui/export-flow.js +98 -0
- package/lib/ui/i18n.d.ts +289 -0
- package/lib/ui/i18n.js +581 -0
- package/lib/ui/migration-consult-view.d.ts +91 -0
- package/lib/ui/migration-consult-view.js +115 -0
- package/lib/ui/progress.d.ts +38 -0
- package/lib/ui/progress.js +87 -0
- package/lib/ui/report.d.ts +23 -0
- package/lib/ui/report.js +153 -0
- package/lib/ui/test-helpers.d.ts +69 -0
- package/lib/ui/test-helpers.js +154 -0
- package/lib/ui/types.d.ts +257 -0
- package/lib/ui/types.js +21 -0
- package/lib/utils/atomic-write.d.ts +88 -0
- package/lib/utils/atomic-write.js +399 -0
- package/lib/utils/env-lock.d.ts +393 -0
- package/lib/utils/env-lock.js +930 -0
- package/lib/utils/hashing.d.ts +17 -0
- package/lib/utils/hashing.js +41 -0
- package/lib/utils/json.d.ts +24 -0
- package/lib/utils/json.js +76 -0
- package/lib/utils/logger.d.ts +39 -0
- package/lib/utils/logger.js +82 -0
- package/lib/utils/paths.d.ts +42 -0
- package/lib/utils/paths.js +189 -0
- package/lib/utils/proxy.d.ts +69 -0
- package/lib/utils/proxy.js +455 -0
- package/lib/utils/recursive-walk.d.ts +24 -0
- package/lib/utils/recursive-walk.js +134 -0
- package/lib/utils/zip.d.ts +54 -0
- package/lib/utils/zip.js +285 -0
- package/package.json +188 -0
- package/src/adapters/agent-instructions.ts +48 -0
- package/src/adapters/agent-presets.ts +14 -0
- package/src/adapters/credentials.test.ts +90 -0
- package/src/adapters/credentials.ts +143 -0
- package/src/adapters/file-collection.ts +137 -0
- package/src/adapters/files.test.ts +353 -0
- package/src/adapters/index.ts +94 -0
- package/src/adapters/link-report.test.ts +65 -0
- package/src/adapters/link-report.ts +61 -0
- package/src/adapters/mcp.test.ts +96 -0
- package/src/adapters/mcp.ts +185 -0
- package/src/adapters/plugin-files.ts +146 -0
- package/src/adapters/plugins.test.ts +326 -0
- package/src/adapters/plugins.ts +615 -0
- package/src/adapters/pnpm-workspace.test.ts +87 -0
- package/src/adapters/pnpm-workspace.ts +150 -0
- package/src/adapters/prompts.test.ts +110 -0
- package/src/adapters/prompts.ts +230 -0
- package/src/adapters/providers.test.ts +126 -0
- package/src/adapters/providers.ts +173 -0
- package/src/adapters/roundtrip.test.ts +190 -0
- package/src/adapters/self.test.ts +92 -0
- package/src/adapters/self.ts +75 -0
- package/src/adapters/sessions.ts +15 -0
- package/src/adapters/settings.test.ts +100 -0
- package/src/adapters/settings.ts +187 -0
- package/src/adapters/skills.ts +14 -0
- package/src/adapters/test-helpers.ts +248 -0
- package/src/adapters/ui.test.ts +57 -0
- package/src/adapters/ui.ts +108 -0
- package/src/adapters/workspaces.test.ts +106 -0
- package/src/adapters/workspaces.ts +109 -0
- package/src/client/ConfigManagerSection.tsx +90 -0
- package/src/client/api.ts +66 -0
- package/src/client/client-types.ts +18 -0
- package/src/client/common/ErrorBanner.tsx +73 -0
- package/src/client/common/Icon.tsx +129 -0
- package/src/client/common/Modal.tsx +173 -0
- package/src/client/common/ToastViewport.tsx +73 -0
- package/src/client/common/toast-store.test.ts +192 -0
- package/src/client/common/toast-store.ts +181 -0
- package/src/client/common/toast-types.ts +7 -0
- package/src/client/common/ui.tsx +342 -0
- package/src/client/config-manager.module.css +2971 -0
- package/src/client/css-modules.d.ts +8 -0
- package/src/client/index.ts +65 -0
- package/src/client/locales.ts +56 -0
- package/src/client/lucide-icons.d.ts +38 -0
- package/src/client/run-store.test.ts +223 -0
- package/src/client/run-store.ts +372 -0
- package/src/client/sync/SyncConfirmView.tsx +379 -0
- package/src/client/sync/SyncHistoryView.test.ts +40 -0
- package/src/client/sync/SyncHistoryView.tsx +220 -0
- package/src/client/sync/SyncSettingsView.tsx +1535 -0
- package/src/client/sync/history-model.test.ts +194 -0
- package/src/client/sync/history-model.ts +195 -0
- package/src/client/sync/sync-api.test.ts +585 -0
- package/src/client/sync/sync-api.ts +543 -0
- package/src/client/sync/sync-locales.ts +416 -0
- package/src/client/sync/sync-push-preview.test.ts +52 -0
- package/src/client/sync/sync-view-v2.test.ts +213 -0
- package/src/client/sync/sync-view.test.ts +487 -0
- package/src/client/sync/sync-view.ts +759 -0
- package/src/core/analyzer.ts +1068 -0
- package/src/core/backup.ts +605 -0
- package/src/core/boot-rescue.test.ts +847 -0
- package/src/core/boot-rescue.ts +737 -0
- package/src/core/cache-cleaner.test.ts +169 -0
- package/src/core/cache-cleaner.ts +121 -0
- package/src/core/config-lifecycle.test.ts +727 -0
- package/src/core/config-lifecycle.ts +573 -0
- package/src/core/config-snapshot.test.ts +369 -0
- package/src/core/config-snapshot.ts +522 -0
- package/src/core/config-state.test.ts +381 -0
- package/src/core/config-state.ts +207 -0
- package/src/core/consult-source.test.ts +199 -0
- package/src/core/consult-source.ts +293 -0
- package/src/core/crash-report.test.ts +380 -0
- package/src/core/crash-report.ts +369 -0
- package/src/core/exporter.ts +417 -0
- package/src/core/ghost-sweep.test.ts +94 -0
- package/src/core/ghost-sweep.ts +67 -0
- package/src/core/importer.ts +83 -0
- package/src/core/index.ts +56 -0
- package/src/core/journal.test.ts +257 -0
- package/src/core/journal.ts +518 -0
- package/src/core/local-plugin-host.ts +152 -0
- package/src/core/local-plugin-pack.test.ts +359 -0
- package/src/core/local-plugin-pack.ts +363 -0
- package/src/core/messages.test.ts +54 -0
- package/src/core/messages.ts +664 -0
- package/src/core/migration-consult.test.ts +315 -0
- package/src/core/migration-consult.ts +494 -0
- package/src/core/migration-history.test.ts +319 -0
- package/src/core/migration-history.ts +484 -0
- package/src/core/model-tools.test.ts +190 -0
- package/src/core/model-tools.ts +208 -0
- package/src/core/msg-types.ts +10 -0
- package/src/core/phase3-child-crash.ts +95 -0
- package/src/core/phase3-host.ts +411 -0
- package/src/core/phase3-p1.test.ts +212 -0
- package/src/core/phase3-p2b-fingerprint.test.ts +189 -0
- package/src/core/phase3-prod-child.ts +36 -0
- package/src/core/phase3-production-integration.test.ts +286 -0
- package/src/core/phase4-crash-child.ts +101 -0
- package/src/core/phase4-crash-injection.test.ts +166 -0
- package/src/core/plugin-cli.fs.test.ts +184 -0
- package/src/core/plugin-cli.test.ts +137 -0
- package/src/core/plugin-cli.ts +563 -0
- package/src/core/reconcile.test.ts +392 -0
- package/src/core/reconcile.ts +498 -0
- package/src/core/recovery-orchestrator.test.ts +145 -0
- package/src/core/recovery-orchestrator.ts +366 -0
- package/src/core/restore-manage.test.ts +94 -0
- package/src/core/restore.ts +849 -0
- package/src/core/rollback.ts +151 -0
- package/src/core/run-progress.test.ts +237 -0
- package/src/core/run-registry.test.ts +232 -0
- package/src/core/run-registry.ts +209 -0
- package/src/core/smoke.test.ts +842 -0
- package/src/core/startup-barrier.ts +99 -0
- package/src/core/types.ts +542 -0
- package/src/core/undo.ts +116 -0
- package/src/core/validator.ts +84 -0
- package/src/core/verify-recovery.ts +265 -0
- package/src/core/watcher.test.ts +326 -0
- package/src/core/watcher.ts +263 -0
- package/src/index.facade.test.ts +265 -0
- package/src/index.sync.test.ts +217 -0
- package/src/index.ts +2975 -0
- package/src/migrations/index.ts +71 -0
- package/src/migrations/v1-to-v2.ts +17 -0
- package/src/profiles/index.ts +8 -0
- package/src/profiles/profile-manager.test.ts +282 -0
- package/src/profiles/profile-manager.ts +630 -0
- package/src/schema/config.ts +165 -0
- package/src/schema/index.ts +71 -0
- package/src/schema/manifest-schema.test.ts +519 -0
- package/src/schema/manifest.ts +124 -0
- package/src/schema/tombstones.test.ts +131 -0
- package/src/schema/tombstones.ts +89 -0
- package/src/schema/types.ts +245 -0
- package/src/schema/versions.ts +55 -0
- package/src/security/encryption.ts +335 -0
- package/src/security/index.ts +16 -0
- package/src/security/integrity.ts +102 -0
- package/src/security/redaction.ts +101 -0
- package/src/security/secret-scanner.ts +445 -0
- package/src/security/security.test.ts +1215 -0
- package/src/security/vault.test.ts +164 -0
- package/src/security/vault.ts +198 -0
- package/src/security/zip-security.ts +180 -0
- package/src/sync/autosync-config.test.ts +147 -0
- package/src/sync/autosync-config.ts +179 -0
- package/src/sync/autosync-scheduler.test.ts +490 -0
- package/src/sync/autosync-scheduler.ts +646 -0
- package/src/sync/fs.ts +48 -0
- package/src/sync/git/git-transport.test.ts +673 -0
- package/src/sync/git/git-transport.ts +491 -0
- package/src/sync/github-auth.test.ts +280 -0
- package/src/sync/github-auth.ts +356 -0
- package/src/sync/layout.test.ts +270 -0
- package/src/sync/layout.ts +222 -0
- package/src/sync/snapshot-json.test.ts +119 -0
- package/src/sync/snapshot-json.ts +128 -0
- package/src/sync/sync-config.test.ts +310 -0
- package/src/sync/sync-config.ts +299 -0
- package/src/sync/sync-engine.test.ts +1074 -0
- package/src/sync/sync-engine.ts +754 -0
- package/src/sync/sync-history.test.ts +116 -0
- package/src/sync/sync-history.ts +120 -0
- package/src/sync/sync-selection.test.ts +186 -0
- package/src/sync/sync-selection.ts +143 -0
- package/src/sync/sync-session.test.ts +143 -0
- package/src/sync/sync-session.ts +77 -0
- package/src/sync/sync-state.test.ts +164 -0
- package/src/sync/sync-state.ts +131 -0
- package/src/sync/transport.test.ts +111 -0
- package/src/sync/transport.ts +118 -0
- package/src/sync/ui-prefs.test.ts +152 -0
- package/src/sync/ui-prefs.ts +135 -0
- package/src/sync/webdav/webdav-transport.test.ts +871 -0
- package/src/sync/webdav/webdav-transport.ts +535 -0
- package/src/ui/errors.test.ts +47 -0
- package/src/ui/errors.ts +100 -0
- package/src/ui/export-flow.test.ts +87 -0
- package/src/ui/export-flow.ts +156 -0
- package/src/ui/i18n.ts +590 -0
- package/src/ui/migration-consult-view.test.ts +151 -0
- package/src/ui/migration-consult-view.ts +178 -0
- package/src/ui/progress.test.ts +47 -0
- package/src/ui/progress.ts +102 -0
- package/src/ui/report.test.ts +101 -0
- package/src/ui/report.ts +146 -0
- package/src/ui/test-helpers.ts +184 -0
- package/src/ui/types.ts +325 -0
- package/src/utils/atomic-write.test.ts +312 -0
- package/src/utils/atomic-write.ts +422 -0
- package/src/utils/env-lock.test.ts +999 -0
- package/src/utils/env-lock.ts +1086 -0
- package/src/utils/hashing.ts +53 -0
- package/src/utils/json.ts +77 -0
- package/src/utils/logger.ts +114 -0
- package/src/utils/paths.ts +176 -0
- package/src/utils/proxy.test.ts +465 -0
- package/src/utils/proxy.ts +493 -0
- package/src/utils/recursive-walk.test.ts +148 -0
- package/src/utils/recursive-walk.ts +145 -0
- package/src/utils/zip.ts +332 -0
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 插件私有出站代理测试(issue #30 ②级方案)。
|
|
3
|
+
*
|
|
4
|
+
* 覆盖基线:
|
|
5
|
+
* - 环境变量解析(大小写、空值、`DSH_CONFIG_MANAGER_PROXY=off` 强制直连);
|
|
6
|
+
* - `NO_PROXY` 匹配(`*` / 精确 / 后缀 / `:port` 限定 / 不匹配);
|
|
7
|
+
* - 代理选择(https→HTTPS_PROXY 并可回退 HTTP_PROXY;http→HTTP_PROXY);
|
|
8
|
+
* - **真实回环服务器**:http 目标经代理走 absolute-form;https 目标走 CONNECT 隧道
|
|
9
|
+
* (校验 CONNECT 行、Proxy-Authorization、以及隧道内 TLS ClientHello 的 SNI);
|
|
10
|
+
* - **子进程 + NODE_EXTRA_CA_CERTS** 的完整 https 端到端(自签 CA,openssl 生成;无 openssl 则跳过);
|
|
11
|
+
* - `NO_PROXY` 直连旁路;未配置代理时 `defaultFetcher` 原样返回全局 fetch(行为零变化);
|
|
12
|
+
* - fetch 语义:跟随重定向、跨源剥离 authorization、303 降级 GET 丢 body、跳数上限。
|
|
13
|
+
*
|
|
14
|
+
* 安全断言:`describeProxy` 绝不回显代理凭据(含用户名/密码的代理 URL)。
|
|
15
|
+
*/
|
|
16
|
+
import test from 'node:test';
|
|
17
|
+
import assert from 'node:assert/strict';
|
|
18
|
+
import http from 'node:http';
|
|
19
|
+
import https from 'node:https';
|
|
20
|
+
import net from 'node:net';
|
|
21
|
+
import { execFileSync, spawn } from 'node:child_process';
|
|
22
|
+
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
23
|
+
import { tmpdir } from 'node:os';
|
|
24
|
+
import { join, resolve } from 'node:path';
|
|
25
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
26
|
+
|
|
27
|
+
import {
|
|
28
|
+
matchesNoProxy,
|
|
29
|
+
proxyUrlFor,
|
|
30
|
+
readProxyEnv,
|
|
31
|
+
describeProxy,
|
|
32
|
+
isProxyConfigured,
|
|
33
|
+
createProxyAwareFetch,
|
|
34
|
+
defaultFetcher,
|
|
35
|
+
requestOnce,
|
|
36
|
+
activeProxySummary,
|
|
37
|
+
type ProxyEnv,
|
|
38
|
+
} from './proxy.ts';
|
|
39
|
+
|
|
40
|
+
const here = import.meta.dirname ?? resolve(fileURLToPath(import.meta.url), '..');
|
|
41
|
+
const NO_PROXY_ENV: ProxyEnv = { httpProxy: null, httpsProxy: null, noProxy: null };
|
|
42
|
+
|
|
43
|
+
/* ------------------------------------------------------------ 环境解析(纯函数) */
|
|
44
|
+
|
|
45
|
+
test('P1 readProxyEnv:大小写兼容、空值忽略、off 强制直连', () => {
|
|
46
|
+
assert.deepEqual(readProxyEnv({}), NO_PROXY_ENV);
|
|
47
|
+
assert.deepEqual(
|
|
48
|
+
readProxyEnv({ HTTPS_PROXY: 'http://p:8080', HTTP_PROXY: 'http://q:3128', NO_PROXY: 'localhost' }),
|
|
49
|
+
{ httpsProxy: 'http://p:8080', httpProxy: 'http://q:3128', noProxy: 'localhost' },
|
|
50
|
+
);
|
|
51
|
+
// 小写形态(Linux/macOS shell 常见)
|
|
52
|
+
assert.deepEqual(
|
|
53
|
+
readProxyEnv({ https_proxy: 'http://p:8080', no_proxy: '127.0.0.1' }),
|
|
54
|
+
{ httpsProxy: 'http://p:8080', httpProxy: null, noProxy: '127.0.0.1' },
|
|
55
|
+
);
|
|
56
|
+
// 空串/空白 → 视为未设置(不产生空字符串代理)
|
|
57
|
+
assert.deepEqual(readProxyEnv({ HTTPS_PROXY: ' ', HTTP_PROXY: '' }), NO_PROXY_ENV);
|
|
58
|
+
// 强制直连开关
|
|
59
|
+
for (const off of ['off', 'NONE', '0', 'false', 'disabled']) {
|
|
60
|
+
assert.deepEqual(
|
|
61
|
+
readProxyEnv({ HTTPS_PROXY: 'http://p:8080', DSH_CONFIG_MANAGER_PROXY: off }),
|
|
62
|
+
NO_PROXY_ENV,
|
|
63
|
+
`DSH_CONFIG_MANAGER_PROXY=${off} 应强制直连`,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
assert.equal(isProxyConfigured(readProxyEnv({ HTTP_PROXY: 'http://p:1' })), true);
|
|
67
|
+
assert.equal(isProxyConfigured(NO_PROXY_ENV), false);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test('P2 matchesNoProxy:* / 精确 / 子域后缀 / .前缀 / 端口限定', () => {
|
|
71
|
+
assert.equal(matchesNoProxy('github.com', '443', '*'), true);
|
|
72
|
+
assert.equal(matchesNoProxy('github.com', '443', 'github.com'), true);
|
|
73
|
+
assert.equal(matchesNoProxy('api.github.com', '443', 'github.com'), true, '裸域名应覆盖子域');
|
|
74
|
+
assert.equal(matchesNoProxy('api.github.com', '443', '.github.com'), true, '点前缀应覆盖子域');
|
|
75
|
+
assert.equal(matchesNoProxy('notgithub.com', '443', 'github.com'), false, '后缀必须按标签边界');
|
|
76
|
+
assert.equal(matchesNoProxy('github.com', '443', 'example.com, github.com'), true, '逗号分隔 + 空格容错');
|
|
77
|
+
assert.equal(matchesNoProxy('github.com', '80', 'github.com:8080'), false, '端口不匹配应放行(走代理)');
|
|
78
|
+
assert.equal(matchesNoProxy('github.com', '8080', 'github.com:8080'), true);
|
|
79
|
+
assert.equal(matchesNoProxy('github.com', '443', ''), false);
|
|
80
|
+
assert.equal(matchesNoProxy('github.com', '443', null), false);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
test('P3 proxyUrlFor:协议选路、HTTP_PROXY 回退、NO_PROXY 旁路、非法代理忽略', () => {
|
|
84
|
+
const env: ProxyEnv = { httpProxy: 'http://h:3128', httpsProxy: 'http://s:8080', noProxy: null };
|
|
85
|
+
assert.equal(proxyUrlFor('https://api.github.com/user', env), 'http://s:8080/');
|
|
86
|
+
assert.equal(proxyUrlFor('http://example.com/', env), 'http://h:3128/');
|
|
87
|
+
// 只设 HTTP_PROXY 时 https 也回退可用(对齐 undici EnvHttpProxyAgent 的直觉语义)
|
|
88
|
+
assert.equal(proxyUrlFor('https://api.github.com/', { ...env, httpsProxy: null }), 'http://h:3128/');
|
|
89
|
+
// NO_PROXY 命中 → 直连
|
|
90
|
+
assert.equal(proxyUrlFor('https://api.github.com/', { ...env, noProxy: 'github.com' }), null);
|
|
91
|
+
// 无代理配置 → 直连
|
|
92
|
+
assert.equal(proxyUrlFor('https://api.github.com/', NO_PROXY_ENV), null);
|
|
93
|
+
// 非法/不支持协议 → 直连(不抛错)
|
|
94
|
+
assert.equal(proxyUrlFor('https://x/', { ...env, httpsProxy: 'socks5://h:1' }), null);
|
|
95
|
+
assert.equal(proxyUrlFor('not a url', env), null);
|
|
96
|
+
assert.equal(proxyUrlFor('ftp://x/', env), null);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test('P4 describeProxy / activeProxySummary:脱敏(凭据绝不外泄)', () => {
|
|
100
|
+
const described = describeProxy('http://user:secret@proxy.local:8080');
|
|
101
|
+
assert.equal(described, 'http://proxy.local:8080');
|
|
102
|
+
assert.ok(!described.includes('secret') && !described.includes('user'), '凭据不得出现在描述中');
|
|
103
|
+
const summary = activeProxySummary({ httpProxy: 'http://u:p@h:1', httpsProxy: null, noProxy: 'a, b' });
|
|
104
|
+
assert.deepEqual(summary, { http: 'http://h:1', https: null, noProxyEntries: 2 });
|
|
105
|
+
assert.equal(activeProxySummary(NO_PROXY_ENV), null, '未配置代理 → null(启动日志不打)');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
test('P5 defaultFetcher:未配置代理时原样返回全局 fetch(行为零变化)', () => {
|
|
109
|
+
assert.equal(defaultFetcher(NO_PROXY_ENV), globalThis.fetch, '无代理环境必须仍是原生 fetch(不改变现状)');
|
|
110
|
+
const proxied = defaultFetcher({ httpProxy: 'http://127.0.0.1:1', httpsProxy: null, noProxy: null });
|
|
111
|
+
assert.notEqual(proxied, globalThis.fetch, '配置代理后才切换到代理感知实现');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/* ------------------------------------------------------------ 真实回环服务器夹具 */
|
|
115
|
+
|
|
116
|
+
interface ProxyHit {
|
|
117
|
+
kind: 'absolute' | 'connect';
|
|
118
|
+
line: string;
|
|
119
|
+
headers: http.IncomingHttpHeaders;
|
|
120
|
+
target?: string;
|
|
121
|
+
body?: string;
|
|
122
|
+
tunnelBytes?: Buffer;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** 极简 HTTP 代理:absolute-form 转发;CONNECT 建隧道(可截获隧道内首段字节)。 */
|
|
126
|
+
async function startProxy(): Promise<{ url: string; hits: ProxyHit[]; close: () => Promise<void> }> {
|
|
127
|
+
const hits: ProxyHit[] = [];
|
|
128
|
+
const server = http.createServer((req, res) => {
|
|
129
|
+
const target = new URL(req.url ?? '/', 'http://placeholder');
|
|
130
|
+
hits.push({
|
|
131
|
+
kind: 'absolute',
|
|
132
|
+
line: `${req.method} ${req.url}`,
|
|
133
|
+
headers: req.headers,
|
|
134
|
+
target: `${target.protocol}//${target.host}${target.pathname}`,
|
|
135
|
+
body: '',
|
|
136
|
+
});
|
|
137
|
+
const chunks: Buffer[] = [];
|
|
138
|
+
req.on('data', (c: Buffer) => chunks.push(Buffer.from(c)));
|
|
139
|
+
req.on('end', () => {
|
|
140
|
+
hits[hits.length - 1]!.body = Buffer.concat(chunks).toString('utf8');
|
|
141
|
+
const upstream = http.request(
|
|
142
|
+
{ host: target.hostname, port: Number(target.port !== '' ? target.port : 80), path: `${target.pathname}${target.search}`, method: req.method, headers: { ...req.headers, host: target.host } },
|
|
143
|
+
(up) => { res.writeHead(up.statusCode ?? 502, up.headers); up.pipe(res); },
|
|
144
|
+
);
|
|
145
|
+
upstream.on('error', () => { res.writeHead(502).end('upstream error'); });
|
|
146
|
+
if (chunks.length > 0) upstream.write(Buffer.concat(chunks));
|
|
147
|
+
upstream.end();
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
server.on('connect', (req, clientSocket, head) => {
|
|
151
|
+
const [host, port] = (req.url ?? '').split(':');
|
|
152
|
+
hits.push({ kind: 'connect', line: `${req.method} ${req.url}`, headers: req.headers, tunnelBytes: Buffer.alloc(0) });
|
|
153
|
+
const hit = hits[hits.length - 1]!;
|
|
154
|
+
const upstream = net.connect(Number(port), host, () => {
|
|
155
|
+
clientSocket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
|
|
156
|
+
if (head.length > 0) upstream.write(head);
|
|
157
|
+
clientSocket.pipe(upstream);
|
|
158
|
+
upstream.pipe(clientSocket);
|
|
159
|
+
});
|
|
160
|
+
// 截获隧道内首段字节(用于断言 TLS ClientHello 的 SNI)。
|
|
161
|
+
// 注意:只观察,**不要 unshift** —— 数据已由下面的 pipe 转发,回填会造成字节重复、握手损坏。
|
|
162
|
+
clientSocket.once('data', (chunk: Buffer) => {
|
|
163
|
+
hit.tunnelBytes = Buffer.from(chunk);
|
|
164
|
+
});
|
|
165
|
+
upstream.on('error', () => clientSocket.destroy());
|
|
166
|
+
clientSocket.on('error', () => upstream.destroy());
|
|
167
|
+
});
|
|
168
|
+
await new Promise<void>((r) => server.listen(0, '127.0.0.1', r));
|
|
169
|
+
const port = (server.address() as net.AddressInfo).port;
|
|
170
|
+
return {
|
|
171
|
+
url: `http://127.0.0.1:${port}`,
|
|
172
|
+
hits,
|
|
173
|
+
close: () => new Promise<void>((r) => { server.closeAllConnections(); server.close(() => { r() }); }),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** 记录收到的请求的源站。 */
|
|
178
|
+
async function startOrigin(): Promise<{ url: string; seen: { path: string; headers: http.IncomingHttpHeaders; body: string; method: string }[]; close: () => Promise<void> }> {
|
|
179
|
+
const seen: { path: string; headers: http.IncomingHttpHeaders; body: string; method: string }[] = [];
|
|
180
|
+
const server = http.createServer((req, res) => {
|
|
181
|
+
const chunks: Buffer[] = [];
|
|
182
|
+
req.on('data', (c: Buffer) => chunks.push(Buffer.from(c)));
|
|
183
|
+
req.on('end', () => {
|
|
184
|
+
seen.push({ path: req.url ?? '', headers: req.headers, body: Buffer.concat(chunks).toString('utf8'), method: req.method ?? '' });
|
|
185
|
+
if (req.url === '/redirect') {
|
|
186
|
+
res.writeHead(302, { location: '/final' }).end();
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (req.url === '/see-other') {
|
|
190
|
+
res.writeHead(303, { location: '/final' }).end();
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if (req.url === '/loop') {
|
|
194
|
+
res.writeHead(302, { location: '/loop' }).end();
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
res.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ ok: true, path: req.url }));
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
await new Promise<void>((r) => server.listen(0, '127.0.0.1', r));
|
|
201
|
+
const port = (server.address() as net.AddressInfo).port;
|
|
202
|
+
return {
|
|
203
|
+
url: `http://127.0.0.1:${port}`,
|
|
204
|
+
seen,
|
|
205
|
+
close: () => new Promise<void>((r) => { server.closeAllConnections(); server.close(() => { r() }); }),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/* ------------------------------------------------------------ http 目标经代理 */
|
|
210
|
+
|
|
211
|
+
test('P6 http 目标经代理:请求交到代理(absolute-form),响应原样回传', async (t) => {
|
|
212
|
+
const prx = await startProxy();
|
|
213
|
+
const origin = await startOrigin();
|
|
214
|
+
t.after(async () => { await prx.close(); await origin.close(); });
|
|
215
|
+
|
|
216
|
+
const res = await requestOnce({
|
|
217
|
+
url: `${origin.url}/via-proxy`,
|
|
218
|
+
headers: { 'x-test': 'yes' },
|
|
219
|
+
env: { httpsProxy: null, httpProxy: prx.url, noProxy: null },
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
assert.equal(res.status, 200);
|
|
223
|
+
assert.deepEqual(JSON.parse(res.body.toString('utf8')), { ok: true, path: '/via-proxy' });
|
|
224
|
+
const hit = prx.hits.find((h) => h.kind === 'absolute');
|
|
225
|
+
assert.ok(hit !== undefined, '代理必须收到该请求');
|
|
226
|
+
assert.match(hit!.line, /^GET http:\/\/127\.0\.0\.1:\d+\/via-proxy/, 'http 目标应使用 absolute-form');
|
|
227
|
+
assert.equal(hit!.headers['x-test'], 'yes', '自定义头应透传到代理');
|
|
228
|
+
assert.equal(origin.seen.length, 1, '源站只应经代理收到一次');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('P7 NO_PROXY 命中 → 完全绕过代理(直连源站)', async (t) => {
|
|
232
|
+
const prx = await startProxy();
|
|
233
|
+
const origin = await startOrigin();
|
|
234
|
+
t.after(async () => { await prx.close(); await origin.close(); });
|
|
235
|
+
|
|
236
|
+
const res = await requestOnce({
|
|
237
|
+
url: `${origin.url}/direct`,
|
|
238
|
+
env: { httpsProxy: null, httpProxy: prx.url, noProxy: '127.0.0.1' },
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
assert.equal(res.status, 200);
|
|
242
|
+
assert.equal(prx.hits.length, 0, 'NO_PROXY 命中时代理不得收到任何请求');
|
|
243
|
+
assert.equal(origin.seen.length, 1);
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* 在子进程里跑脚本并回收输出(**必须异步 spawn**)。
|
|
248
|
+
*
|
|
249
|
+
* 关键教训:本用例的代理与源站都跑在**当前进程**里,`spawnSync` 会阻塞当前进程的事件循环 →
|
|
250
|
+
* 子进程等代理响应、父进程等子进程退出,直接死锁(实测挂满超时被杀)。故一律用异步 spawn。
|
|
251
|
+
*/
|
|
252
|
+
function runChild(script: string, env: Record<string, string>): Promise<{ status: number | null; signal: string | null; stdout: string; stderr: string }> {
|
|
253
|
+
return new Promise((resolveChild) => {
|
|
254
|
+
const child = spawn(process.execPath, ['--input-type=module', '-e', script], {
|
|
255
|
+
env: { ...process.env, ...env },
|
|
256
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
257
|
+
});
|
|
258
|
+
let stdout = '';
|
|
259
|
+
let stderr = '';
|
|
260
|
+
child.stdout.on('data', (c: Buffer) => { stdout += c.toString('utf8') });
|
|
261
|
+
child.stderr.on('data', (c: Buffer) => { stderr += c.toString('utf8') });
|
|
262
|
+
const killer = setTimeout(() => { child.kill('SIGKILL') }, 20_000);
|
|
263
|
+
child.on('close', (status, signal) => {
|
|
264
|
+
clearTimeout(killer);
|
|
265
|
+
resolveChild({ status, signal, stdout, stderr });
|
|
266
|
+
});
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/* ------------------------------------------------------------ https 目标经代理 */
|
|
271
|
+
|
|
272
|
+
test('P8 https 目标经代理:先 CONNECT(含 Basic 凭据),隧道内 TLS ClientHello 的 SNI 为目标主机', async (t) => {
|
|
273
|
+
// 隧道另一端不需要真 TLS:CONNECT 后只读取客户端首段字节即可断言隧道与 SNI。
|
|
274
|
+
const server = http.createServer();
|
|
275
|
+
const received: { connectLine: string; auth?: string; bytes: Buffer }[] = [];
|
|
276
|
+
server.on('connect', (req, socket) => {
|
|
277
|
+
const rec = { connectLine: `${req.method} ${req.url}`, auth: req.headers['proxy-authorization'] as string | undefined, bytes: Buffer.alloc(0) };
|
|
278
|
+
received.push(rec);
|
|
279
|
+
socket.write('HTTP/1.1 200 Connection Established\r\n\r\n');
|
|
280
|
+
socket.once('data', (chunk: Buffer) => {
|
|
281
|
+
rec.bytes = Buffer.from(chunk);
|
|
282
|
+
socket.destroy();
|
|
283
|
+
});
|
|
284
|
+
});
|
|
285
|
+
await new Promise<void>((r) => server.listen(0, '127.0.0.1', r));
|
|
286
|
+
const port = (server.address() as net.AddressInfo).port;
|
|
287
|
+
t.after(async () => { await new Promise<void>((r) => { server.closeAllConnections(); server.close(() => { r() }); }) });
|
|
288
|
+
|
|
289
|
+
await assert.rejects(
|
|
290
|
+
() => requestOnce({
|
|
291
|
+
url: 'https://example-tunnel.test/secret',
|
|
292
|
+
env: { httpsProxy: `http://alice:s3cr3t@127.0.0.1:${port}`, httpProxy: null, noProxy: null },
|
|
293
|
+
timeoutMs: 5000,
|
|
294
|
+
}),
|
|
295
|
+
// 隧道对端不是真 TLS 服务,握手必然失败 —— 这里只关心「隧道已建立 + 已发起 TLS」
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
assert.equal(received.length, 1, '应发起一次 CONNECT');
|
|
299
|
+
assert.equal(received[0]!.connectLine, 'CONNECT example-tunnel.test:443');
|
|
300
|
+
assert.equal(
|
|
301
|
+
received[0]!.auth,
|
|
302
|
+
`Basic ${Buffer.from('alice:s3cr3t').toString('base64')}`,
|
|
303
|
+
'代理凭据应通过 Proxy-Authorization 传递',
|
|
304
|
+
);
|
|
305
|
+
const hello = received[0]!.bytes;
|
|
306
|
+
assert.equal(hello[0], 0x16, '隧道内应为 TLS handshake 记录');
|
|
307
|
+
assert.ok(hello.includes(Buffer.from('example-tunnel.test', 'ascii')), 'ClientHello 应携带目标主机名(SNI)');
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
/** 定位 openssl(PATH 优先,其次 Git for Windows 常见路径;供自签证书用) */
|
|
311
|
+
function findOpenssl(): string | null {
|
|
312
|
+
const candidates = [
|
|
313
|
+
'openssl',
|
|
314
|
+
'/usr/bin/openssl',
|
|
315
|
+
'/usr/local/bin/openssl',
|
|
316
|
+
'C:\\Program Files\\Git\\usr\\bin\\openssl.exe',
|
|
317
|
+
'C:\\Program Files\\Git\\mingw64\\bin\\openssl.exe',
|
|
318
|
+
];
|
|
319
|
+
for (const bin of candidates) {
|
|
320
|
+
try {
|
|
321
|
+
execFileSync(bin, ['version'], { stdio: 'ignore' });
|
|
322
|
+
return bin;
|
|
323
|
+
} catch {
|
|
324
|
+
// 试下一个
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
return null
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** openssl 生成的临时 CA + 服务器证书(CN/SAN=localhost);无 openssl → null(该项跳过) */
|
|
331
|
+
function makeTestCertificates(): { ca: string; cert: string; key: string } | null {
|
|
332
|
+
const openssl = findOpenssl();
|
|
333
|
+
if (openssl === null) return null;
|
|
334
|
+
const dir = mkdtempSync(join(tmpdir(), 'dsh-cm-proxy-cert-'));
|
|
335
|
+
try {
|
|
336
|
+
const f = (name: string): string => join(dir, name);
|
|
337
|
+
execFileSync(openssl, ['req', '-x509', '-newkey', 'rsa:2048', '-nodes', '-keyout', f('ca.key'), '-out', f('ca.crt'), '-days', '1', '-subj', '/CN=DSH CM Test CA'], { stdio: 'ignore' });
|
|
338
|
+
execFileSync(openssl, ['req', '-new', '-newkey', 'rsa:2048', '-nodes', '-keyout', f('srv.key'), '-out', f('srv.csr'), '-subj', '/CN=localhost', '-addext', 'subjectAltName=DNS:localhost'], { stdio: 'ignore' });
|
|
339
|
+
writeFileSync(f('ext.cnf'), 'subjectAltName=DNS:localhost\n', 'utf8');
|
|
340
|
+
execFileSync(openssl, ['x509', '-req', '-in', f('srv.csr'), '-CA', f('ca.crt'), '-CAkey', f('ca.key'), '-CAcreateserial', '-out', f('srv.crt'), '-days', '1', '-extfile', f('ext.cnf')], { stdio: 'ignore' });
|
|
341
|
+
return {
|
|
342
|
+
ca: readFileSync(f('ca.crt'), 'utf8'),
|
|
343
|
+
cert: readFileSync(f('srv.crt'), 'utf8'),
|
|
344
|
+
key: readFileSync(f('srv.key'), 'utf8'),
|
|
345
|
+
};
|
|
346
|
+
} catch {
|
|
347
|
+
return null;
|
|
348
|
+
} finally {
|
|
349
|
+
rmSync(dir, { recursive: true, force: true });
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
test('P9 https 端到端(子进程 + NODE_EXTRA_CA_CERTS):CONNECT 隧道内完成真实 TLS 请求', async (t) => {
|
|
354
|
+
const certs = makeTestCertificates();
|
|
355
|
+
if (certs === null) {
|
|
356
|
+
t.skip('未找到 openssl,跳过自签证书端到端用例');
|
|
357
|
+
return;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// 目标源站(TLS,自签)
|
|
361
|
+
const origin = https.createServer({ cert: certs.cert, key: certs.key }, (req, res) => {
|
|
362
|
+
res.writeHead(200, { 'content-type': 'application/json' }).end(JSON.stringify({ hello: req.url }));
|
|
363
|
+
});
|
|
364
|
+
await new Promise<void>((r) => origin.listen(0, '127.0.0.1', r));
|
|
365
|
+
const originPort = (origin.address() as net.AddressInfo).port;
|
|
366
|
+
|
|
367
|
+
// CONNECT 代理
|
|
368
|
+
const prx = await startProxy();
|
|
369
|
+
t.after(async () => {
|
|
370
|
+
await new Promise<void>((r) => { origin.closeAllConnections(); origin.close(() => { r() }) });
|
|
371
|
+
await prx.close();
|
|
372
|
+
});
|
|
373
|
+
|
|
374
|
+
const script = `
|
|
375
|
+
try {
|
|
376
|
+
const { requestOnce } = await import(${JSON.stringify(pathToFileURL(resolve(here, 'proxy.ts')).href)});
|
|
377
|
+
const res = await requestOnce({
|
|
378
|
+
url: process.env.T_URL,
|
|
379
|
+
env: { httpProxy: null, httpsProxy: process.env.T_PROXY, noProxy: null },
|
|
380
|
+
timeoutMs: 10000,
|
|
381
|
+
});
|
|
382
|
+
process.stdout.write(JSON.stringify({ status: res.status, body: res.body.toString('utf8'), server: res.headers.get('content-type') }));
|
|
383
|
+
} catch (err) {
|
|
384
|
+
process.stderr.write('CHILD_ERROR: ' + (err && err.stack ? err.stack : String(err)));
|
|
385
|
+
process.exit(2);
|
|
386
|
+
}
|
|
387
|
+
// keep-alive socket 会让事件循环保持存活;本用例只关心请求结果,显式退出。
|
|
388
|
+
process.exit(0);
|
|
389
|
+
`;
|
|
390
|
+
const caFile = join(tmpdir(), `dsh-cm-ca-${Date.now()}.crt`);
|
|
391
|
+
writeFileSync(caFile, certs.ca, 'utf8');
|
|
392
|
+
const child = await runChild(script, {
|
|
393
|
+
T_URL: `https://localhost:${originPort}/through-tunnel`,
|
|
394
|
+
T_PROXY: prx.url,
|
|
395
|
+
// 让子进程信任测试 CA(当前进程组装的信任链不生效:Node 启动时读取该变量)
|
|
396
|
+
NODE_EXTRA_CA_CERTS: caFile,
|
|
397
|
+
});
|
|
398
|
+
|
|
399
|
+
assert.equal(child.status, 0, `子进程应成功(status=${String(child.status)} signal=${String(child.signal)} stderr: ${child.stderr})`);
|
|
400
|
+
const payload = JSON.parse(child.stdout) as { status: number; body: string; server: string };
|
|
401
|
+
assert.equal(payload.status, 200);
|
|
402
|
+
assert.deepEqual(JSON.parse(payload.body), { hello: '/through-tunnel' });
|
|
403
|
+
assert.match(payload.server, /application\/json/, 'TLS 隧道的响应头应完整透传');
|
|
404
|
+
assert.equal(prx.hits.filter((h) => h.kind === 'connect').length, 1, '应经代理 CONNECT 建立隧道(而非直连)');
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
/* ------------------------------------------------------------ fetch 语义 */
|
|
408
|
+
|
|
409
|
+
test('P10 createProxyAwareFetch:跟随 302、返回真实 Response(status/headers/json)', async (t) => {
|
|
410
|
+
const origin = await startOrigin();
|
|
411
|
+
t.after(async () => { await origin.close(); });
|
|
412
|
+
|
|
413
|
+
const f = createProxyAwareFetch(NO_PROXY_ENV);
|
|
414
|
+
const res = await f(`${origin.url}/redirect`);
|
|
415
|
+
assert.equal(res.status, 200, '302 应被自动跟随(与浏览器 fetch 一致)');
|
|
416
|
+
assert.equal(res.headers.get('content-type'), 'application/json');
|
|
417
|
+
assert.deepEqual(await res.json(), { ok: true, path: '/final' });
|
|
418
|
+
assert.deepEqual(origin.seen.map((s) => s.path), ['/redirect', '/final']);
|
|
419
|
+
});
|
|
420
|
+
|
|
421
|
+
test('P11 createProxyAwareFetch:跨源跳转剥离 authorization(token 不转发第三方)', async (t) => {
|
|
422
|
+
const b = await startOrigin();
|
|
423
|
+
// 独立源(不同端口)只做一件事:302 到 b
|
|
424
|
+
const redirector = http.createServer((_req, res) => {
|
|
425
|
+
res.writeHead(302, { location: `${b.url}/landing` }).end();
|
|
426
|
+
});
|
|
427
|
+
await new Promise<void>((r) => redirector.listen(0, '127.0.0.1', r));
|
|
428
|
+
const redirectorUrl = `http://127.0.0.1:${(redirector.address() as net.AddressInfo).port}`;
|
|
429
|
+
t.after(async () => {
|
|
430
|
+
await new Promise<void>((r) => { redirector.closeAllConnections(); redirector.close(() => { r() }) });
|
|
431
|
+
await b.close();
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
const f = createProxyAwareFetch(NO_PROXY_ENV);
|
|
435
|
+
const res = await f(`${redirectorUrl}/start`, { headers: { authorization: 'Bearer sekret' } });
|
|
436
|
+
assert.equal(res.status, 200);
|
|
437
|
+
assert.deepEqual(JSON.parse(await res.text()), { ok: true, path: '/landing' });
|
|
438
|
+
assert.equal(b.seen.length, 1, '应跟随到跨源目标');
|
|
439
|
+
assert.equal(b.seen[0]!.headers['authorization'], undefined, '跨源跳转必须剥离 authorization');
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
test('P11b 同源跳转保留 authorization(不能顺手把凭据丢了)', async (t) => {
|
|
443
|
+
const origin = await startOrigin();
|
|
444
|
+
t.after(async () => { await origin.close(); });
|
|
445
|
+
|
|
446
|
+
const f = createProxyAwareFetch(NO_PROXY_ENV);
|
|
447
|
+
const res = await f(`${origin.url}/redirect`, { headers: { authorization: 'Bearer keepme' } });
|
|
448
|
+
assert.equal(res.status, 200);
|
|
449
|
+
assert.deepEqual(origin.seen.map((s) => s.path), ['/redirect', '/final']);
|
|
450
|
+
assert.equal(origin.seen[0]!.headers['authorization'], 'Bearer keepme');
|
|
451
|
+
assert.equal(origin.seen[1]!.headers['authorization'], 'Bearer keepme', '同源跳转应保留 authorization');
|
|
452
|
+
});
|
|
453
|
+
|
|
454
|
+
test('P12 createProxyAwareFetch:303 降级 GET 并丢弃 body;跳数上限报错', async (t) => {
|
|
455
|
+
const origin = await startOrigin();
|
|
456
|
+
t.after(async () => { await origin.close(); });
|
|
457
|
+
|
|
458
|
+
const f = createProxyAwareFetch(NO_PROXY_ENV);
|
|
459
|
+
const res = await f(`${origin.url}/see-other`, { method: 'POST', body: 'payload' });
|
|
460
|
+
assert.equal(res.status, 200);
|
|
461
|
+
assert.deepEqual(origin.seen.map((s) => s.method), ['POST', 'GET'], '303 后应降级为 GET');
|
|
462
|
+
assert.equal(origin.seen[1]!.body, '', '303 后必须丢弃请求体');
|
|
463
|
+
|
|
464
|
+
await assert.rejects(() => f(`${origin.url}/loop`), /too many redirects/, '重定向循环应报错而不是无限跟随');
|
|
465
|
+
});
|