maven-proxy 1.0.1 → 1.0.2

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/README.md CHANGED
@@ -1,420 +1,466 @@
1
- # Maven Proxy Requirements and Usage
2
-
3
- [English](README.md) | [中文](README-zh.md)
4
-
5
- ## 1. Project Positioning
6
-
7
- This project is a Maven proxy service used to improve dependency download speed and stability for Maven and Gradle.
8
-
9
- Core goals:
10
- - Expose a proxy port for Maven and Gradle clients.
11
- - Cache dependency files locally and serve cached files on hits.
12
- - Support HTTPS proxying, including Root CA based certificate issuance for selected domains.
13
- - Enable multi-threaded downloading for selected domains.
14
- - Expose a local Maven repository port so Gradle and Maven can consume cached artifacts directly.
15
-
16
- ## 2. Functional Requirements
17
-
18
- ### 2.1 Proxy Service Port
19
- - The service must start on a configurable proxy port.
20
- - Maven and Gradle requests should be processed through this proxy endpoint.
21
-
22
- ### 2.2 HTTPS Proxy and Certificate Issuance
23
- - Support HTTPS proxy scenarios for Maven and Gradle repositories.
24
- - Generate and persist a local Root CA certificate/key pair.
25
- - For matched domains, dynamically issue leaf certificates from the local Root CA.
26
- - For unmatched HTTPS targets, allow passthrough tunnel behavior based on configuration.
27
- - Reuse persisted CA files across restarts.
28
-
29
- ### 2.3 Cache Hit and Fallback Download
30
- When a dependency is requested:
31
- 1. Check the local cache path first.
32
- 2. If found and valid, return the cached file.
33
- 3. If missing, download from upstream.
34
- 4. Download to a temporary file with a .temp suffix first.
35
- 5. Atomically rename .temp to final file only after completion and integrity checks.
36
- 6. Return the final cached file.
37
-
38
- Failure handling:
39
- - Never leave partial final files.
40
- - Clean up temporary files after failures.
41
-
42
- ### 2.4 Multi-thread Download by Domain
43
- - Domain-based strategy is supported for fallback downloads.
44
- - Matched domains use multi-threaded download.
45
- - Unmatched domains use single-threaded download.
46
- - Thread count and threshold are configurable.
47
-
48
- ### 2.5 Local Repository Port
49
- - Start an additional configurable repository port.
50
- - Publish local cache as a Maven repository endpoint.
51
- - Maven and Gradle can use this endpoint directly.
52
- - If artifact is missing locally, fetch from configured fallback repositories, cache it, then return it.
53
-
54
- ### 2.6 Java Trust Store Support
55
- - Provide trust store command templates for Java environments.
56
- - Cover creating/copying trust store, importing Root CA, and verification.
57
- - Include executable keytool examples for Windows and macOS.
58
-
59
- ### 2.7 Configurability
60
- At minimum, these settings are configurable:
61
- - Proxy service port.
62
- - HTTPS proxy toggle.
63
- - MITM domain matching rules.
64
- - Root CA certificate and key paths.
65
- - Local repository publish port.
66
- - Cache directory.
67
- - Multi-thread downloader options.
68
- - Multi-thread domain matching rules.
69
- - Trust store settings (path, alias, password behavior).
70
-
71
- ## 3. Main Flow
72
-
73
- 1. Client requests dependency through proxy port.
74
- 2. If HTTPS and host matches MITM rules, TLS is handled with Root CA issued certificate.
75
- 3. Service checks local cache.
76
- 4. On cache hit, return cached file.
77
- 5. On cache miss, fallback download begins.
78
- 6. Write to .temp first.
79
- 7. Verify integrity and atomically rename to final file.
80
- 8. Return final file.
81
- 9. Cached files are also available through repository port.
82
-
83
- ## 4. Java Trust Store Commands (Windows)
84
-
85
- 1. Copy default JDK cacerts to project trust store:
86
-
87
- ```powershell
88
- Copy-Item "$env:JAVA_HOME\\lib\\security\\cacerts" ".\\data\\certs\\proxy-truststore.jks"
89
- ```
90
-
91
- 2. Import project Root CA:
92
-
93
- ```powershell
94
- keytool -importcert -noprompt -trustcacerts `
95
- -alias maven-proxy-root-ca `
96
- -file .\\data\\certs\\root-ca.crt `
97
- -keystore .\\data\\certs\\proxy-truststore.jks `
98
- -storepass changeit
99
- ```
100
-
101
- 3. Verify imported certificate:
102
-
103
- ```powershell
104
- keytool -list -v `
105
- -keystore .\\data\\certs\\proxy-truststore.jks `
106
- -storepass changeit `
107
- -alias maven-proxy-root-ca
108
- ```
109
-
110
- 4. JVM runtime flags:
111
-
112
- ```powershell
113
- -Djavax.net.ssl.trustStore=.\\data\\certs\\proxy-truststore.jks
114
- -Djavax.net.ssl.trustStorePassword=changeit
115
- ```
116
-
117
- ## 5. Out of Scope
118
-
119
- - Authentication and authorization.
120
- - Complex management UI.
121
- - Cache eviction/TTL/capacity management.
122
-
123
- ## 6. Acceptance Criteria
124
-
125
- - Proxy and repository ports both start successfully.
126
- - Maven and Gradle can download via proxy.
127
- - HTTPS proxy works; matched domains complete Root CA based MITM flow.
128
- - Repeated dependency requests hit cache.
129
- - Missing dependencies can be downloaded and cached.
130
- - .temp files are used for in-progress downloads.
131
- - Final files are created only through atomic rename.
132
- - Multi-thread download is applied on configured domains.
133
- - Repository port can serve cached dependencies to Maven and Gradle.
134
- - Trust store operations can import Root CA successfully.
135
- - Key settings are configurable.
136
- - Repository fallback supports Maven Central, JitPack, Gradle Plugin Portal, and Google Maven by default.
137
-
138
- ## 7. Optional Future Enhancements
139
-
140
- - Corrupted cache detection and repair.
141
- - Better retry and circuit-breaker behavior.
142
- - Access logs, hit-rate metrics, health checks.
143
- - Cache cleanup and disk quota management.
144
-
145
- ## 8. Current Implementation and Runtime Guide
146
-
147
- Source code is in src and uses Node.js ESM imports. Utility scripts are in scripts.
148
-
149
- Suggested structure:
150
-
151
- ```text
152
- src/
153
- index.js
154
- config/
155
- config.js
156
- common/
157
- domain-match.js
158
- cache/
159
- cache-path.js
160
- downloader.js
161
- cert/
162
- cert-manager.js
163
- truststore-utils.js
164
- proxy/
165
- proxy-server.js
166
- proxy-http-handler.js
167
- proxy-connect-handler.js
168
- upstream-proxy.js
169
- repo/
170
- repo-server.js
171
- scripts/
172
- truststore.js
173
- ```
174
-
175
- ### 8.1 Start
176
-
177
- 1. Install dependencies:
178
-
179
- ```powershell
180
- npm install
181
- ```
182
-
183
- 2. Configure environment variables as needed.
184
-
185
- Notes:
186
- - Development mode (default): npm start loads .env first, then .evn as fallback alias.
187
- - User mode (CLI default): npx maven-proxy or global command uses ~/maven-proxy/config.
188
- - Override mode with MAVEN_PROXY_CONFIG_MODE as development or user.
189
- - Override config file path with MAVEN_PROXY_CONFIG_FILE.
190
- - JAVA_HOME supports auto-detection:
191
- - macOS: /usr/libexec/java_home
192
- - Windows: where java, then common JDK install paths
193
- - Linux: which java, then common JDK install directories
194
-
195
- Useful Java path commands:
196
- - macOS/Linux: echo $JAVA_HOME, which java, /usr/libexec/java_home (macOS only)
197
- - Windows cmd: echo %JAVA_HOME%, where java
198
- - Windows PowerShell: $env:JAVA_HOME, Get-Command java
199
-
200
- 3. Start service:
201
-
202
- ```powershell
203
- npm start
204
- ```
205
-
206
- Default ports:
207
- - Proxy: 8080 (override with PROXY_PORT)
208
- - Repository: 8081 (override with REPO_PORT)
209
-
210
- ### 8.2 Implemented Features
211
-
212
- - HTTP and HTTPS proxy entry.
213
- - Domain-based HTTPS MITM with Root CA issued leaf certs.
214
- - Temporary file download + integrity check + atomic rename.
215
- - Multi-thread download with thresholds.
216
- - Upstream proxy support for outbound requests and CONNECT.
217
- - npm proxy support for registry metadata and tarballs.
218
- - Ecosystem-separated cache directories: cache/maven, cache/npm, cache/generic.
219
- - Dedicated daily logs with retention.
220
- - Local cache published as Maven repository.
221
- - Trust store scripts and commands:
222
- - npm run truststore:print
223
- - npm run truststore:init
224
- - npm run truststore:merge -- --source <src.jks> --target <dest.jks>
225
-
226
- ### 8.3 Minimal Validation Commands (Windows)
227
-
228
- 1. Proxy download test (first MISS, second HIT):
229
-
230
- ```powershell
231
- curl.exe -k -sS -D - -o NUL -x http://127.0.0.1:8080 https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.pom
232
- curl.exe -k -sS -D - -o NUL -x http://127.0.0.1:8080 https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.pom
233
- ```
234
-
235
- 2. Repository port cache access:
236
-
237
- ```powershell
238
- curl.exe -sS -D - -o NUL http://127.0.0.1:8081/maven2/junit/junit/4.13.2/junit-4.13.2.pom
239
- ```
240
-
241
- 3. Ensure no leftover .temp files:
242
-
243
- ```powershell
244
- Get-ChildItem -Recurse -File .\data\cache -Filter '*.temp'
245
- ```
246
-
247
- 4. npm proxy validation:
248
-
249
- ```powershell
250
- curl.exe -k -sS -D - -o NUL -x http://127.0.0.1:8080 https://registry.npmjs.org/lodash
251
- curl.exe -k -sS -D - -o NUL -x http://127.0.0.1:8080 https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz
252
- ```
253
-
254
- ### 8.4 Upstream Proxy Settings
255
-
256
- Environment variables:
257
- - UPSTREAM_PROXY_URL: generic upstream proxy URL.
258
- - UPSTREAM_HTTP_PROXY_URL: upstream proxy for HTTP.
259
- - UPSTREAM_HTTPS_PROXY_URL: upstream proxy for HTTPS.
260
- - UPSTREAM_NO_PROXY: domains that bypass upstream proxy.
261
- - UPSTREAM_IGNORE_DOMAINS: ignored domains, wildcard supported.
262
- - REPO_FALLBACK_REPOS: repository fallback list.
263
- - NPM_REGISTRY_DOMAINS: npm domains for ecosystem routing.
264
- - MAVEN_REPO_DOMAINS: maven domains for ecosystem routing.
265
- - HTTPS_MITM_DOMAINS: MITM domain list (includes registry.npmjs.org by default).
266
- - DOWNLOAD_LOG_DIR: log directory.
267
- - LOG_RETENTION_DAYS: number of days to retain logs.
268
- - MAVEN_PROXY_CONFIG_MODE: development or user.
269
- - MAVEN_PROXY_CONFIG_FILE: explicit config file path.
270
-
271
- Rules:
272
- - UPSTREAM_NO_PROXY and UPSTREAM_IGNORE_DOMAINS are merged.
273
- - Exact and wildcard domains are supported.
274
-
275
- Priority:
276
- 1. HTTP: UPSTREAM_HTTP_PROXY_URL, then UPSTREAM_PROXY_URL.
277
- 2. HTTPS: UPSTREAM_HTTPS_PROXY_URL, then UPSTREAM_PROXY_URL, then UPSTREAM_HTTP_PROXY_URL.
278
-
279
- ### 8.5 Trust Store Merge
280
-
281
- Use merge command to combine trust stores.
282
-
283
- Help:
284
-
285
- ```powershell
286
- npm run truststore:merge -- --help
287
- ```
288
-
289
- Basic merge:
290
-
291
- ```powershell
292
- npm run truststore:merge -- \
293
- --source .\data\certs\source-truststore.jks \
294
- --target .\data\certs\proxy-truststore.jks \
295
- --source-pass changeit \
296
- --target-pass changeit
297
- ```
298
-
299
- Overwrite on alias conflict:
300
-
301
- ```powershell
302
- npm run truststore:merge -- \
303
- --source .\data\certs\source-truststore.jks \
304
- --target .\data\certs\proxy-truststore.jks \
305
- --source-pass changeit \
306
- --target-pass changeit \
307
- --on-conflict overwrite
308
- ```
309
-
310
- Dry-run:
311
-
312
- ```powershell
313
- npm run truststore:merge -- \
314
- --source .\data\certs\source-truststore.jks \
315
- --target .\data\certs\proxy-truststore.jks \
316
- --source-pass changeit \
317
- --target-pass changeit \
318
- --dry-run
319
- ```
320
-
321
- Optional flags:
322
- - --source-type: default JKS.
323
- - --target-type: default JKS.
324
- - --on-conflict: fail or overwrite.
325
- - --dry-run: validation only.
326
-
327
- ### 8.6 Publish as CLI Tool (npx / global)
328
-
329
- The project provides executable command maven-proxy.
330
-
331
- Run with npx:
332
-
333
- ```bash
334
- npx maven-proxy
335
- ```
336
-
337
- Install globally:
338
-
339
- ```bash
340
- npm install -g maven-proxy
341
- maven-proxy
342
- ```
343
-
344
- Default CLI config path:
345
- - ~/maven-proxy/config
346
-
347
- Common commands:
348
- - maven-proxy --config /path/to/config
349
- - maven-proxy start --mode development
350
- - maven-proxy start --mode user
351
- - maven-proxy init-config --force
352
- - maven-proxy truststore print
353
- - maven-proxy truststore init
354
- - maven-proxy truststore merge --source /path/source.jks --target /path/target.jks
355
- - maven-proxy doctor
356
-
357
- Doctor command:
358
- - Checks config loading, port availability, keytool, JAVA_HOME, cert/truststore paths, and writable log/cache directories.
359
- - Reports PASS/WARN/FAIL.
360
- - Returns exit code 2 when FAIL exists.
361
-
362
- ## 9. Client Usage
363
-
364
- Default proxy port in examples: 8080. Replace with your configured value if different.
365
-
366
- ### 9.1 Gradle: Proxy + trust store
367
-
368
- 1. Initialize trust store (if needed):
369
-
370
- ```bash
371
- npm run truststore:init
372
- ```
373
-
374
- 2. Update ~/.gradle/gradle.properties:
375
-
376
- ```properties
377
- systemProp.http.proxyHost=127.0.0.1
378
- systemProp.http.proxyPort=8080
379
- systemProp.https.proxyHost=127.0.0.1
380
- systemProp.https.proxyPort=8080
381
- org.gradle.jvmargs=-Djavax.net.ssl.trustStore=/Users/yize/projects/maven-proxy/data/certs/proxy-truststore.jks -Djavax.net.ssl.trustStorePassword=changeit
382
- ```
383
-
384
- 3. Validate:
385
-
386
- ```bash
387
- ./gradlew --refresh-dependencies dependencies
388
- ```
389
-
390
- ### 9.2 npm: Proxy + SSL behavior
391
-
392
- For local troubleshooting only, you can disable strict SSL temporarily. Recommended long-term approach is to import Root CA and keep strict SSL enabled.
393
-
394
- Set npm proxy:
395
-
396
- ```bash
397
- npm config set proxy http://127.0.0.1:8080
398
- npm config set https-proxy http://127.0.0.1:8080
399
- ```
400
-
401
- Temporary disable strict SSL:
402
-
403
- ```bash
404
- npm config set strict-ssl false
405
- ```
406
-
407
- Validate:
408
-
409
- ```bash
410
- npm ping
411
- npm view lodash version
412
- ```
413
-
414
- Restore safer defaults:
415
-
416
- ```bash
417
- npm config set strict-ssl true
418
- npm config delete proxy
419
- npm config delete https-proxy
420
- ```
1
+ # Maven Proxy Requirements and Usage
2
+
3
+ [English](README.md) | [中文](README-zh.md)
4
+
5
+ [![npm version](https://img.shields.io/npm/v/maven-proxy.svg)](https://www.npmjs.com/package/maven-proxy)
6
+
7
+ ## 1. Project Positioning
8
+
9
+ This project is a Maven proxy service used to improve dependency download speed and stability for Maven and Gradle.
10
+
11
+ Core goals:
12
+ - Expose a proxy port for Maven and Gradle clients.
13
+ - Cache dependency files locally and serve cached files on hits.
14
+ - Support HTTPS proxying, including Root CA based certificate issuance for selected domains.
15
+ - Enable multi-threaded downloading for selected domains.
16
+ - Expose a local Maven repository port so Gradle and Maven can consume cached artifacts directly.
17
+
18
+ ## 2. Functional Requirements
19
+
20
+ ### 2.1 Proxy Service Port
21
+ - The service must start on a configurable proxy port.
22
+ - Maven and Gradle requests should be processed through this proxy endpoint.
23
+
24
+ ### 2.2 HTTPS Proxy and Certificate Issuance
25
+ - Support HTTPS proxy scenarios for Maven and Gradle repositories.
26
+ - Generate and persist a local Root CA certificate/key pair.
27
+ - For matched domains, dynamically issue leaf certificates from the local Root CA.
28
+ - For unmatched HTTPS targets, allow passthrough tunnel behavior based on configuration.
29
+ - Reuse persisted CA files across restarts.
30
+
31
+ ### 2.3 Cache Hit and Fallback Download
32
+ When a dependency is requested:
33
+ 1. Check the local cache path first.
34
+ 2. If found and valid, return the cached file.
35
+ 3. If missing, download from upstream.
36
+ 4. Download to a temporary file with a .temp suffix first.
37
+ 5. Atomically rename .temp to final file only after completion and integrity checks.
38
+ 6. Return the final cached file.
39
+
40
+ Failure handling:
41
+ - Never leave partial final files.
42
+ - Clean up temporary files after failures.
43
+
44
+ ### 2.4 Multi-thread Download by Domain
45
+ - Domain-based strategy is supported for fallback downloads.
46
+ - Matched domains use multi-threaded download.
47
+ - Unmatched domains use single-threaded download.
48
+ - Thread count and threshold are configurable.
49
+
50
+ ### 2.5 Local Repository Port
51
+ - Start an additional configurable repository port.
52
+ - Publish local cache as a Maven repository endpoint.
53
+ - Maven and Gradle can use this endpoint directly.
54
+ - If artifact is missing locally, fetch from configured fallback repositories, cache it, then return it.
55
+
56
+ ### 2.6 Java Trust Store Support
57
+ - Provide trust store command templates for Java environments.
58
+ - Cover creating/copying trust store, importing Root CA, and verification.
59
+ - Include executable keytool examples for Windows and macOS.
60
+
61
+ ### 2.7 Configurability
62
+ At minimum, these settings are configurable:
63
+ - Proxy service port.
64
+ - HTTPS proxy toggle.
65
+ - MITM domain matching rules.
66
+ - Root CA certificate and key paths.
67
+ - Local repository publish port.
68
+ - Cache directory.
69
+ - Multi-thread downloader options.
70
+ - Multi-thread domain matching rules.
71
+ - Trust store settings (path, alias, password behavior).
72
+
73
+ ## 3. Main Flow
74
+
75
+ 1. Client requests dependency through proxy port.
76
+ 2. If HTTPS and host matches MITM rules, TLS is handled with Root CA issued certificate.
77
+ 3. Service checks local cache.
78
+ 4. On cache hit, return cached file.
79
+ 5. On cache miss, fallback download begins.
80
+ 6. Write to .temp first.
81
+ 7. Verify integrity and atomically rename to final file.
82
+ 8. Return final file.
83
+ 9. Cached files are also available through repository port.
84
+
85
+ ## 4. Java Trust Store Commands (Windows)
86
+
87
+ 1. Copy default JDK cacerts to project trust store:
88
+
89
+ ```powershell
90
+ Copy-Item "$env:JAVA_HOME\\lib\\security\\cacerts" ".\\data\\certs\\proxy-truststore.jks"
91
+ ```
92
+
93
+ 2. Import project Root CA:
94
+
95
+ ```powershell
96
+ keytool -importcert -noprompt -trustcacerts `
97
+ -alias maven-proxy-root-ca `
98
+ -file .\\data\\certs\\root-ca.crt `
99
+ -keystore .\\data\\certs\\proxy-truststore.jks `
100
+ -storepass changeit
101
+ ```
102
+
103
+ 3. Verify imported certificate:
104
+
105
+ ```powershell
106
+ keytool -list -v `
107
+ -keystore .\\data\\certs\\proxy-truststore.jks `
108
+ -storepass changeit `
109
+ -alias maven-proxy-root-ca
110
+ ```
111
+
112
+ 4. JVM runtime flags:
113
+
114
+ ```powershell
115
+ -Djavax.net.ssl.trustStore=.\\data\\certs\\proxy-truststore.jks
116
+ -Djavax.net.ssl.trustStorePassword=changeit
117
+ ```
118
+
119
+ ## 5. Out of Scope
120
+
121
+ - Authentication and authorization.
122
+ - Complex management UI.
123
+ - Cache eviction/TTL/capacity management.
124
+
125
+ ## 6. Acceptance Criteria
126
+
127
+ - Proxy and repository ports both start successfully.
128
+ - Maven and Gradle can download via proxy.
129
+ - HTTPS proxy works; matched domains complete Root CA based MITM flow.
130
+ - Repeated dependency requests hit cache.
131
+ - Missing dependencies can be downloaded and cached.
132
+ - .temp files are used for in-progress downloads.
133
+ - Final files are created only through atomic rename.
134
+ - Multi-thread download is applied on configured domains.
135
+ - Repository port can serve cached dependencies to Maven and Gradle.
136
+ - Trust store operations can import Root CA successfully.
137
+ - Key settings are configurable.
138
+ - Repository fallback supports Maven Central, JitPack, Gradle Plugin Portal, and Google Maven by default.
139
+
140
+ ## 7. Optional Future Enhancements
141
+
142
+ - Corrupted cache detection and repair.
143
+ - Better retry and circuit-breaker behavior.
144
+ - Access logs, hit-rate metrics, health checks.
145
+ - Cache cleanup and disk quota management.
146
+
147
+ ## 8. Current Implementation and Runtime Guide
148
+
149
+ Source code is in src and uses Node.js ESM imports. Utility scripts are in scripts.
150
+
151
+ Suggested structure:
152
+
153
+ ```text
154
+ src/
155
+ index.js
156
+ config/
157
+ config.js
158
+ common/
159
+ domain-match.js
160
+ cache/
161
+ cache-path.js
162
+ downloader.js
163
+ cert/
164
+ cert-manager.js
165
+ truststore-utils.js
166
+ proxy/
167
+ proxy-server.js
168
+ proxy-http-handler.js
169
+ proxy-connect-handler.js
170
+ upstream-proxy.js
171
+ repo/
172
+ repo-server.js
173
+ scripts/
174
+ truststore.js
175
+ ```
176
+
177
+ ### 8.1 Start
178
+
179
+ 1. Install dependencies:
180
+
181
+ ```powershell
182
+ npm install
183
+ ```
184
+
185
+ 2. Configure environment variables as needed.
186
+
187
+ Notes:
188
+ - Development mode (default): npm start loads .env first, then .evn as fallback alias.
189
+ - User mode (CLI default): npx maven-proxy or global command uses ~/maven-proxy/config.
190
+ - Override mode with MAVEN_PROXY_CONFIG_MODE as development or user.
191
+ - Override config file path with MAVEN_PROXY_CONFIG_FILE.
192
+ - JAVA_HOME supports auto-detection:
193
+ - macOS: /usr/libexec/java_home
194
+ - Windows: where java, then common JDK install paths
195
+ - Linux: which java, then common JDK install directories
196
+
197
+ Useful Java path commands:
198
+ - macOS/Linux: echo $JAVA_HOME, which java, /usr/libexec/java_home (macOS only)
199
+ - Windows cmd: echo %JAVA_HOME%, where java
200
+ - Windows PowerShell: $env:JAVA_HOME, Get-Command java
201
+
202
+ 3. Start service:
203
+
204
+ ```powershell
205
+ npm start
206
+ ```
207
+
208
+ Default ports:
209
+ - Proxy: 8080 (override with PROXY_PORT)
210
+ - Repository: 8081 (override with REPO_PORT)
211
+
212
+ ### 8.2 Implemented Features
213
+
214
+ - HTTP and HTTPS proxy entry.
215
+ - Domain-based HTTPS MITM with Root CA issued leaf certs.
216
+ - Temporary file download + integrity check + atomic rename.
217
+ - Multi-thread download with thresholds.
218
+ - Upstream proxy support for outbound requests and CONNECT.
219
+ - npm proxy support for registry metadata and tarballs.
220
+ - Ecosystem-separated cache directories: cache/maven, cache/npm, cache/generic.
221
+ - Dedicated daily logs with retention.
222
+ - Local cache published as Maven repository.
223
+ - Trust store scripts and commands:
224
+ - npm run truststore:print
225
+ - npm run truststore:init
226
+ - npm run truststore:merge -- --source <src.jks> --target <dest.jks>
227
+
228
+ ### 8.3 Minimal Validation Commands (Windows)
229
+
230
+ 1. Proxy download test (first MISS, second HIT):
231
+
232
+ ```powershell
233
+ curl.exe -k -sS -D - -o NUL -x http://127.0.0.1:8080 https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.pom
234
+ curl.exe -k -sS -D - -o NUL -x http://127.0.0.1:8080 https://repo1.maven.org/maven2/junit/junit/4.13.2/junit-4.13.2.pom
235
+ ```
236
+
237
+ 2. Repository port cache access:
238
+
239
+ ```powershell
240
+ curl.exe -sS -D - -o NUL http://127.0.0.1:8081/maven2/junit/junit/4.13.2/junit-4.13.2.pom
241
+ ```
242
+
243
+ 3. Ensure no leftover .temp files:
244
+
245
+ ```powershell
246
+ Get-ChildItem -Recurse -File .\data\cache -Filter '*.temp'
247
+ ```
248
+
249
+ 4. npm proxy validation:
250
+
251
+ ```powershell
252
+ curl.exe -k -sS -D - -o NUL -x http://127.0.0.1:8080 https://registry.npmjs.org/lodash
253
+ curl.exe -k -sS -D - -o NUL -x http://127.0.0.1:8080 https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz
254
+ ```
255
+
256
+ ### 8.4 Upstream Proxy Settings
257
+
258
+ Environment variables:
259
+ - UPSTREAM_PROXY_URL: generic upstream proxy URL.
260
+ - UPSTREAM_HTTP_PROXY_URL: upstream proxy for HTTP.
261
+ - UPSTREAM_HTTPS_PROXY_URL: upstream proxy for HTTPS.
262
+ - UPSTREAM_NO_PROXY: domains that bypass upstream proxy (wildcards supported, `*` means bypass all).
263
+ - UPSTREAM_IGNORE_DOMAINS: ignored domains, wildcard supported.
264
+ - REPO_FALLBACK_REPOS: repository fallback list.
265
+ - NPM_REGISTRY_DOMAINS: npm domains for ecosystem routing (wildcards supported).
266
+ - MAVEN_REPO_DOMAINS: maven domains for ecosystem routing (wildcards supported).
267
+ - HTTPS_MITM_DOMAINS: MITM domain list (includes registry.npmjs.org by default, wildcards supported).
268
+ - DOWNLOAD_LOG_DIR: log directory.
269
+ - LOG_RETENTION_DAYS: number of days to retain logs.
270
+ - MAVEN_PROXY_CONFIG_MODE: development or user.
271
+ - MAVEN_PROXY_CONFIG_FILE: explicit config file path.
272
+ - EXISTING_TRUST_STORE_PATH: optional existing truststore path. If present, truststore init prefers it as source.
273
+ - EXISTING_TRUST_STORE_PASSWORD: optional password for the existing truststore source.
274
+
275
+ Rules:
276
+ - UPSTREAM_NO_PROXY and UPSTREAM_IGNORE_DOMAINS are merged.
277
+ - In the current implementation, both lists share equivalent matching behavior. Prefer UPSTREAM_NO_PROXY for standard no-proxy settings and UPSTREAM_IGNORE_DOMAINS for project-specific exclusions.
278
+ - Exact and wildcard domains are supported.
279
+
280
+ Priority:
281
+ 1. HTTP: UPSTREAM_HTTP_PROXY_URL, then UPSTREAM_PROXY_URL.
282
+ 2. HTTPS: UPSTREAM_HTTPS_PROXY_URL, then UPSTREAM_PROXY_URL, then UPSTREAM_HTTP_PROXY_URL.
283
+
284
+ ### 8.4.1 Full Environment Variable Reference
285
+
286
+ - `PROXY_PORT`: Proxy server port. Default `8080`.
287
+ - `REPO_PORT`: Local repository server port. Default `8081`.
288
+ - `CACHE_DIR`: Base cache directory. Default `data/cache`.
289
+ - `REPO_FALLBACK_REPOS`: Comma-separated fallback repository URLs for cache misses.
290
+ - `ENABLE_HTTPS_PROXY`: Enable HTTPS proxy handling (`true/false`).
291
+ - `HTTPS_MITM_DOMAINS`: Comma-separated domains to apply MITM certificate issuance (wildcards supported).
292
+ - `HTTPS_PASSTHROUGH_FOR_UNMATCHED`: Whether unmatched HTTPS domains are tunneled directly.
293
+ - `NPM_REGISTRY_DOMAINS`: Domains treated as npm ecosystem for cache routing (wildcards supported).
294
+ - `MAVEN_REPO_DOMAINS`: Domains treated as Maven ecosystem for cache routing (wildcards supported).
295
+ - `MULTI_THREAD_DOMAINS`: Domains allowed to use multi-thread download (wildcards supported).
296
+ - `MULTI_THREAD_COUNT`: Number of download threads for ranged downloads.
297
+ - `MULTI_THREAD_MIN_SIZE_BYTES`: Minimum size threshold to trigger multi-thread download.
298
+ - `DOWNLOAD_TIMEOUT_MS`: Upstream request timeout in milliseconds.
299
+ - `DOWNLOAD_LOG_DIR`: Directory for download/console logs.
300
+ - `LOG_RETENTION_DAYS`: Number of days to keep log files.
301
+ - `UPSTREAM_PROXY_URL`: Generic upstream proxy URL (fallback for HTTP/HTTPS).
302
+ - `UPSTREAM_HTTP_PROXY_URL`: Upstream proxy URL for HTTP requests.
303
+ - `UPSTREAM_HTTPS_PROXY_URL`: Upstream proxy URL for HTTPS requests.
304
+ - `UPSTREAM_NO_PROXY`: Comma-separated domains that bypass upstream proxy (wildcards supported, `*` means bypass all).
305
+ - `UPSTREAM_IGNORE_DOMAINS`: Additional bypass domains (wildcards supported).
306
+ - `CERT_DIR`: Base directory for certificates.
307
+ - `ROOT_CERT_PATH`: Path to Root CA certificate.
308
+ - `ROOT_KEY_PATH`: Path to Root CA private key.
309
+ - `LEAF_CERT_DIR`: Directory for issued leaf certificates.
310
+ - `TRUST_STORE_PATH`: Path to Java trust store file.
311
+ - `TRUST_STORE_ALIAS`: Alias used when importing Root CA into trust store.
312
+ - `TRUST_STORE_PASSWORD`: Trust store password.
313
+ - `EXISTING_TRUST_STORE_PATH`: Optional existing truststore path. If present, init prefers it as source and writes output to `TRUST_STORE_PATH`.
314
+ - `EXISTING_TRUST_STORE_PASSWORD`: Optional password used to read `EXISTING_TRUST_STORE_PATH`.
315
+ - `JAVA_HOME`: Preferred Java home path. If empty or invalid, auto-detection is used.
316
+ - `MAVEN_PROXY_CONFIG_MODE`: Config load mode (`development` or `user`).
317
+ - `MAVEN_PROXY_CONFIG_FILE`: Explicit config file path override.
318
+
319
+ ### 8.5 Trust Store Merge
320
+
321
+ Use merge command to combine trust stores.
322
+
323
+ `truststore init` behavior:
324
+
325
+ - If `EXISTING_TRUST_STORE_PATH` exists: it is used as source, copied to `TRUST_STORE_PATH`, then Root CA is imported.
326
+ - If it does not exist: fallback source is `${JAVA_HOME}/lib/security/cacerts`, then output is written to `TRUST_STORE_PATH` and Root CA is imported.
327
+ - If source truststore password differs from `TRUST_STORE_PASSWORD`, init rotates output store password to `TRUST_STORE_PASSWORD`.
328
+
329
+ Help:
330
+
331
+ ```powershell
332
+ npm run truststore:merge -- --help
333
+ ```
334
+
335
+ Basic merge:
336
+
337
+ ```powershell
338
+ npm run truststore:merge -- \
339
+ --source .\data\certs\source-truststore.jks \
340
+ --target .\data\certs\proxy-truststore.jks \
341
+ --source-pass changeit \
342
+ --target-pass changeit
343
+ ```
344
+
345
+ Overwrite on alias conflict:
346
+
347
+ ```powershell
348
+ npm run truststore:merge -- \
349
+ --source .\data\certs\source-truststore.jks \
350
+ --target .\data\certs\proxy-truststore.jks \
351
+ --source-pass changeit \
352
+ --target-pass changeit \
353
+ --on-conflict overwrite
354
+ ```
355
+
356
+ Dry-run:
357
+
358
+ ```powershell
359
+ npm run truststore:merge -- \
360
+ --source .\data\certs\source-truststore.jks \
361
+ --target .\data\certs\proxy-truststore.jks \
362
+ --source-pass changeit \
363
+ --target-pass changeit \
364
+ --dry-run
365
+ ```
366
+
367
+ Optional flags:
368
+ - --source-type: default JKS.
369
+ - --target-type: default JKS.
370
+ - --on-conflict: fail or overwrite.
371
+ - --dry-run: validation only.
372
+
373
+ ### 8.6 Publish as CLI Tool (npx / global)
374
+
375
+ The project provides executable command maven-proxy.
376
+
377
+ Run with npx:
378
+
379
+ ```bash
380
+ npx maven-proxy
381
+ ```
382
+
383
+ Install globally:
384
+
385
+ ```bash
386
+ npm install -g maven-proxy
387
+ maven-proxy
388
+ ```
389
+
390
+ Default CLI config path:
391
+ - ~/maven-proxy/config
392
+
393
+ Common commands:
394
+ - maven-proxy --config /path/to/config
395
+ - maven-proxy start --mode development
396
+ - maven-proxy start --mode user
397
+ - maven-proxy init-config --force
398
+ - maven-proxy truststore print
399
+ - maven-proxy truststore init
400
+ - maven-proxy truststore merge --source /path/source.jks --target /path/target.jks
401
+ - maven-proxy doctor
402
+
403
+ Doctor command:
404
+ - Checks config loading, port availability, keytool, JAVA_HOME, cert/truststore paths, and writable log/cache directories.
405
+ - Reports PASS/WARN/FAIL.
406
+ - Returns exit code 2 when FAIL exists.
407
+
408
+ ## 9. Client Usage
409
+
410
+ Default proxy port in examples: 8080. Replace with your configured value if different.
411
+
412
+ ### 9.1 Gradle: Proxy + trust store
413
+
414
+ 1. Initialize trust store (if needed):
415
+
416
+ ```bash
417
+ npm run truststore:init
418
+ ```
419
+
420
+ 2. Update ~/.gradle/gradle.properties:
421
+
422
+ ```properties
423
+ systemProp.http.proxyHost=127.0.0.1
424
+ systemProp.http.proxyPort=8080
425
+ systemProp.https.proxyHost=127.0.0.1
426
+ systemProp.https.proxyPort=8080
427
+ org.gradle.jvmargs=-Djavax.net.ssl.trustStore=/Users/yize/projects/maven-proxy/data/certs/proxy-truststore.jks -Djavax.net.ssl.trustStorePassword=changeit
428
+ ```
429
+
430
+ 3. Validate:
431
+
432
+ ```bash
433
+ ./gradlew --refresh-dependencies dependencies
434
+ ```
435
+
436
+ ### 9.2 npm: Proxy + SSL behavior
437
+
438
+ For local troubleshooting only, you can disable strict SSL temporarily. Recommended long-term approach is to import Root CA and keep strict SSL enabled.
439
+
440
+ Set npm proxy:
441
+
442
+ ```bash
443
+ npm config set proxy http://127.0.0.1:8080
444
+ npm config set https-proxy http://127.0.0.1:8080
445
+ ```
446
+
447
+ Temporary disable strict SSL:
448
+
449
+ ```bash
450
+ npm config set strict-ssl false
451
+ ```
452
+
453
+ Validate:
454
+
455
+ ```bash
456
+ npm ping
457
+ npm view lodash version
458
+ ```
459
+
460
+ Restore safer defaults:
461
+
462
+ ```bash
463
+ npm config set strict-ssl true
464
+ npm config delete proxy
465
+ npm config delete https-proxy
466
+ ```