@robiki/proxy 1.0.2 → 1.0.3

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.zh.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # 🚀 Robiki 代理
2
2
 
3
- > 一个高性能、灵活的 HTTP/2 反向代理,支持 WebSocket、可配置路由、CORS 和请求验证。可作为 npm 包在 Node.js 应用程序中使用,也可作为独立的 Docker 容器使用。仅用作本地开发环境的域名代理。
3
+ > 一个高性能的 HTTP/2 反向代理,支持 WebSocket、可配置路由、CORS 和请求验证。可作为 npm 包或 Docker 容器用于本地开发环境。
4
4
 
5
5
  [![npm version](https://img.shields.io/npm/v/@robiki/proxy.svg)](https://www.npmjs.com/package/@robiki/proxy)
6
6
  [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
7
 
8
- ## 🌍 语言 / Languages / Sprachen / 言語 / Języki / Idiomas / Языки
8
+ ## 🌍 语言
9
9
 
10
10
  [English](README.md) | [Deutsch](README.de.md) | [中文](README.zh.md) | [日本語](README.ja.md) | [Polski](README.pl.md) | [Español](README.es.md) | [Русский](README.ru.md)
11
11
 
@@ -23,49 +23,23 @@
23
23
 
24
24
  ## 📦 安装
25
25
 
26
- ### 作为 npm 包
26
+ ### npm 包
27
27
 
28
28
  ```bash
29
29
  npm install @robiki/proxy
30
- ```
31
-
32
- ```bash
30
+ # 或
33
31
  yarn add @robiki/proxy
34
32
  ```
35
33
 
36
- ### 作为 Docker 容器
34
+ ### Docker
37
35
 
38
36
  ```bash
39
37
  docker pull robiki/proxy:latest
40
38
  ```
41
39
 
42
- ### 作为 Docker Compose 服务
43
-
44
- ```yaml
45
- services:
46
- proxy:
47
- image: robiki/proxy:latest
48
- container_name: robiki-proxy
49
- restart: unless-stopped
50
- ports:
51
- - '443:443'
52
- - '8080:8080'
53
- - '9229:9229'
54
- volumes:
55
- - ./proxy.config.json:/app/proxy.config.json:ro
56
- - ./certs:/app/certs:ro
57
- networks:
58
- - app-network
59
- ```
60
-
61
- ## 注意事项
62
-
63
- - 本地配置的主机应添加到本地 `hosts` 文件中。
64
- - 如果您使用自定义证书,需要将证书文件添加到 `certs` 目录。
65
-
66
40
  ## 🚀 快速开始
67
41
 
68
- ### 作为 npm 包使用
42
+ ### npm
69
43
 
70
44
  ```javascript
71
45
  import { createProxy } from '@robiki/proxy';
@@ -88,13 +62,11 @@ const proxy = await createProxy({
88
62
  },
89
63
  },
90
64
  });
91
-
92
- console.log('代理服务器正在运行!');
93
65
  ```
94
66
 
95
- ### 使用 Docker
67
+ ### Docker
96
68
 
97
- 1. 创建 `proxy.config.json` 文件:
69
+ 创建 `proxy.config.json`:
98
70
 
99
71
  ```json
100
72
  {
@@ -108,20 +80,14 @@ console.log('代理服务器正在运行!');
108
80
  "api.example.com": {
109
81
  "target": "backend-service:3000",
110
82
  "ssl": true
111
- },
112
- "example.com": {
113
- "target": "frontend-service:8080",
114
- "ssl": false
115
83
  }
116
84
  }
117
85
  }
118
86
  ```
119
87
 
120
- 2. 创建 `docker-compose.yml`:
88
+ 创建 `docker-compose.yml`:
121
89
 
122
90
  ```yaml
123
- version: '3.8'
124
-
125
91
  services:
126
92
  proxy:
127
93
  image: robiki/proxy:latest
@@ -133,25 +99,9 @@ services:
133
99
  - ./certs:/app/certs:ro
134
100
  environment:
135
101
  - PROXY_CONFIG=/app/proxy.config.json
136
- networks:
137
- - app-network
138
-
139
- backend-service:
140
- image: your-backend-image
141
- networks:
142
- - app-network
143
-
144
- frontend-service:
145
- image: your-frontend-image
146
- networks:
147
- - app-network
148
-
149
- networks:
150
- app-network:
151
- driver: bridge
152
102
  ```
153
103
 
154
- 3. 启动服务:
104
+ 启动:
155
105
 
156
106
  ```bash
157
107
  docker-compose up -d
@@ -159,9 +109,9 @@ docker-compose up -d
159
109
 
160
110
  ## 📖 配置
161
111
 
162
- ### 配置文件
112
+ ### JSON 配置
163
113
 
164
- 创建具有以下结构的 `proxy.config.json` 文件:
114
+ 简单的声明式配置:
165
115
 
166
116
  ```json
167
117
  {
@@ -169,24 +119,17 @@ docker-compose up -d
169
119
  "ssl": {
170
120
  "key": "./certs/key.pem",
171
121
  "cert": "./certs/cert.pem",
172
- "ca": "./certs/ca.pem",
173
122
  "allowHTTP1": true
174
123
  },
175
124
  "cors": {
176
125
  "origin": "*",
177
- "methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
178
- "allowedHeaders": ["Content-Type", "Authorization"],
179
- "credentials": true,
180
- "maxAge": 86400
126
+ "methods": ["GET", "POST", "PUT", "DELETE"],
127
+ "credentials": true
181
128
  },
182
129
  "routes": {
183
130
  "api.example.com": {
184
- "target": "backend-service:3000",
185
- "ssl": true,
186
- "cors": {
187
- "origin": ["https://example.com"],
188
- "credentials": true
189
- }
131
+ "target": "backend:3000",
132
+ "ssl": true
190
133
  },
191
134
  "*.example.com": {
192
135
  "target": "wildcard-service:4000",
@@ -196,78 +139,32 @@ docker-compose up -d
196
139
  }
197
140
  ```
198
141
 
199
- ### 环境变量
200
-
201
- 您还可以使用环境变量配置代理:
202
-
203
- ```bash
204
- # SSL 配置
205
- SSL_KEY=/app/certs/key.pem
206
- SSL_CERT=/app/certs/cert.pem
207
- SSL_CA=/app/certs/ca.pem
208
- SSL_ALLOW_HTTP1=true
209
-
210
- # CORS 配置
211
- CORS_ORIGIN=*
212
- CORS_METHODS=GET,POST,PUT,DELETE,OPTIONS
213
- CORS_HEADERS=Content-Type,Authorization
214
- CORS_CREDENTIALS=true
215
-
216
- # 调试模式
217
- DEBUG=true # 启用代理连接和错误的详细日志记录
218
- ```
219
-
220
- ## 🎯 高级用法
221
-
222
- ### URL 重映射
223
-
224
- 在转发到目标服务之前转换 URL:
225
-
226
- ```javascript
227
- const config = {
228
- routes: {
229
- 'api.example.com': {
230
- target: 'backend:3000',
231
- ssl: true,
232
- remap: (url) => {
233
- // 删除 /api 前缀
234
- return url.replace(/^\/api/, '');
235
- },
236
- },
237
- },
238
- };
239
- ```
240
-
241
- ### 请求验证
142
+ ### JavaScript 配置
242
143
 
243
- 为身份验证、速率限制等添加自定义验证逻辑:
144
+ 用于高级功能,如 URL 重映射和验证:
244
145
 
245
146
  ```javascript
246
- const config = {
247
- // 全局验证
248
- validate: async (info) => {
249
- if (!info.headers.authorization) {
250
- return {
251
- status: false,
252
- code: 401,
253
- message: '未授权',
254
- headers: { 'www-authenticate': 'Bearer' },
255
- };
256
- }
257
- return { status: true };
147
+ // proxy.config.cjs
148
+ module.exports = {
149
+ ports: [443, 8080],
150
+ ssl: {
151
+ key: './certs/key.pem',
152
+ cert: './certs/cert.pem',
153
+ allowHTTP1: true,
258
154
  },
259
155
  routes: {
260
156
  'api.example.com': {
261
157
  target: 'backend:3000',
262
158
  ssl: true,
263
- // 路由特定验证
159
+ // URL 重映射
160
+ remap: (url) => url.replace(/^\/api/, ''),
161
+ // 请求验证
264
162
  validate: async (info) => {
265
- const rateLimit = await checkRateLimit(info.remoteAddress);
266
- if (!rateLimit.allowed) {
163
+ if (!info.headers.authorization) {
267
164
  return {
268
165
  status: false,
269
- code: 429,
270
- message: '请求过多',
166
+ code: 401,
167
+ message: 'Unauthorized',
271
168
  };
272
169
  }
273
170
  return { status: true };
@@ -277,93 +174,80 @@ const config = {
277
174
  };
278
175
  ```
279
176
 
280
- ### 自定义 CORS 配置
177
+ ### TypeScript 配置
281
178
 
282
- 全局或按路由配置 CORS:
179
+ 类型安全的配置,完整的 IDE 支持:
283
180
 
284
- ```javascript
285
- const config = {
286
- // 全局 CORS
287
- cors: {
288
- origin: ['https://example.com', 'https://www.example.com'],
289
- methods: ['GET', 'POST', 'PUT', 'DELETE'],
290
- allowedHeaders: ['Content-Type', 'Authorization'],
291
- credentials: true,
292
- maxAge: 86400,
181
+ ```typescript
182
+ // proxy.config.ts
183
+ import type { ServerConfig, ConnectionInfo } from '@robiki/proxy';
184
+
185
+ const config: ServerConfig = {
186
+ ports: [443, 8080],
187
+ ssl: {
188
+ key: './certs/key.pem',
189
+ cert: './certs/cert.pem',
190
+ allowHTTP1: true,
293
191
  },
294
192
  routes: {
295
193
  'api.example.com': {
296
194
  target: 'backend:3000',
297
195
  ssl: true,
298
- // 路由特定 CORS(覆盖全局)
299
- cors: {
300
- origin: '*',
301
- credentials: false,
196
+ remap: (url: string) => url.replace(/^\/api/, ''),
197
+ validate: async (info: ConnectionInfo) => {
198
+ if (!info.headers['x-api-key']) {
199
+ return { status: false, code: 401, message: 'API Key Required' };
200
+ }
201
+ return { status: true };
302
202
  },
303
203
  },
304
204
  },
305
205
  };
306
- ```
307
206
 
308
- ### 自定义处理程序
207
+ export default config;
208
+ ```
309
209
 
310
- 为高级用例创建自定义请求处理程序:
210
+ ### 环境变量
311
211
 
312
- ```javascript
313
- import { createCustomProxy } from '@robiki/proxy';
212
+ ```bash
213
+ # SSL 配置
214
+ SSL_KEY=/app/certs/key.pem
215
+ SSL_CERT=/app/certs/cert.pem
216
+ SSL_ALLOW_HTTP1=true
314
217
 
315
- const customRestHandler = async (req, res) => {
316
- if (req.url === '/health') {
317
- res.writeHead(200, { 'content-type': 'application/json' });
318
- return res.end(JSON.stringify({ status: 'ok' }));
319
- }
320
- // 回退到默认代理行为
321
- const { restAPIProxyHandler } = await import('@robiki/proxy/connections');
322
- return restAPIProxyHandler(req, res);
323
- };
218
+ # CORS 配置
219
+ CORS_ORIGIN=*
220
+ CORS_METHODS=GET,POST,PUT,DELETE
221
+ CORS_CREDENTIALS=true
324
222
 
325
- const proxy = await createCustomProxy(config, {
326
- rest: customRestHandler,
327
- websocket: customWebSocketHandler,
328
- stream: customStreamHandler,
329
- });
223
+ # 调试模式
224
+ DEBUG=true
330
225
  ```
331
226
 
332
227
  ## 🔧 API 参考
333
228
 
334
- ### `createProxy(config: ServerConfig): Promise<ProxyServer>`
335
-
336
- 使用给定配置创建并启动代理服务器。
337
-
338
- **参数:**
339
-
340
- - `config`:服务器配置对象
341
-
342
- **返回:** 解析为 `ProxyServer` 实例的 Promise
229
+ ### `createProxy(config: ServerConfig)`
343
230
 
344
- ### `ProxyServer`
231
+ 创建并启动代理服务器。
345
232
 
346
- **方法:**
347
-
348
- - `start()`:启动代理服务器
349
- - `stop()`:停止代理服务器
350
- - `getConfig()`:获取当前配置
351
-
352
- ### 配置类型
353
-
354
- #### `ServerConfig`
233
+ **ServerConfig:**
355
234
 
356
235
  ```typescript
357
236
  interface ServerConfig {
358
237
  ports?: number[];
359
- ssl?: CertificateConfig;
238
+ ssl?: {
239
+ key: string;
240
+ cert: string;
241
+ ca?: string;
242
+ allowHTTP1?: boolean;
243
+ };
360
244
  routes: Record<string, RouteConfig>;
361
245
  cors?: CorsConfig;
362
246
  validate?: (info: ConnectionInfo) => Promise<ForwardValidationResult>;
363
247
  }
364
248
  ```
365
249
 
366
- #### `RouteConfig`
250
+ **RouteConfig:**
367
251
 
368
252
  ```typescript
369
253
  interface RouteConfig {
@@ -375,265 +259,86 @@ interface RouteConfig {
375
259
  }
376
260
  ```
377
261
 
378
- #### `CorsConfig`
379
-
380
- ```typescript
381
- interface CorsConfig {
382
- origin?: string | string[];
383
- methods?: string[];
384
- allowedHeaders?: string[];
385
- exposedHeaders?: string[];
386
- credentials?: boolean;
387
- maxAge?: number;
388
- }
389
- ```
390
-
391
- #### `ConnectionInfo`
392
-
393
- ```typescript
394
- interface ConnectionInfo {
395
- id: number;
396
- method: string;
397
- path: string;
398
- remoteAddress: string;
399
- scheme: string;
400
- authority: string;
401
- origin: string;
402
- headers: IncomingHttpHeaders;
403
- query: URLSearchParams;
404
- type: RequestType;
405
- }
406
- ```
407
-
408
262
  ## 🐳 Docker 使用
409
263
 
410
- ### 在另一个项目中使用
411
-
412
- 1. 将代理添加到您的 `docker-compose.yml`:
264
+ 挂载您的配置文件(JSON、.cjs 或 .ts):
413
265
 
414
266
  ```yaml
415
267
  services:
416
268
  proxy:
417
269
  image: robiki/proxy:latest
418
- ports:
419
- - '443:443'
420
- - '8080:8080'
421
270
  volumes:
422
- - ./proxy.config.json:/app/proxy.config.json:ro
271
+ - ./proxy.config.cjs:/app/proxy.config.cjs:ro
423
272
  - ./certs:/app/certs:ro
424
- networks:
425
- - your-network
426
-
427
- your-service:
428
- image: your-service-image
429
- networks:
430
- - your-network
431
- ```
432
-
433
- 2. 在 `proxy.config.json` 中配置路由以指向您的服务
434
-
435
- 3. 启动您的堆栈:
436
-
437
- ```bash
438
- docker-compose up -d
439
- ```
440
-
441
- ### 构建自定义镜像
442
-
443
- 创建自定义 Dockerfile:
444
-
445
- ```dockerfile
446
- FROM robiki/proxy:latest
447
-
448
- # 复制您的配置
449
- COPY proxy.config.json /app/proxy.config.json
450
- COPY certs /app/certs
451
-
452
- # 设置环境变量
453
- ENV PROXY_CONFIG=/app/proxy.config.json
273
+ environment:
274
+ - PROXY_CONFIG=/app/proxy.config.cjs
454
275
  ```
455
276
 
456
- ## 📚 示例
457
-
458
- 查看 `examples/` 目录以获取更多使用示例:
459
-
460
- - `basic-usage.js` - 简单的代理设置
461
- - `advanced-usage.js` - 高级功能(验证、CORS、重映射)
462
- - `custom-handlers.js` - 自定义请求处理程序
463
- - `docker-compose.example.yml` - 完整的 Docker 设置
464
-
465
- ## 🔐 SSL/TLS 证书
466
-
467
- ### 生成自签名证书
277
+ ## 🔐 SSL 证书
468
278
 
469
- 用于开发:
279
+ ### 开发环境(自签名)
470
280
 
471
281
  ```bash
472
282
  mkdir -p certs
473
283
  openssl req -x509 -newkey rsa:4096 -keyout certs/key.pem -out certs/cert.pem -days 365 -nodes
474
284
  ```
475
285
 
476
- ### 使用 Let's Encrypt
477
-
478
- 用于生产,使用 Let's Encrypt 证书:
286
+ ### 生产环境(Let's Encrypt
479
287
 
480
288
  ```bash
481
289
  certbot certonly --standalone -d example.com
482
290
  ```
483
291
 
484
- 然后在您的配置中引用它们:
485
-
486
- ```json
487
- {
488
- "ssl": {
489
- "key": "/etc/letsencrypt/live/example.com/privkey.pem",
490
- "cert": "/etc/letsencrypt/live/example.com/fullchain.pem"
491
- }
492
- }
493
- ```
494
-
495
- ## 🤝 贡献
496
-
497
- 欢迎贡献!请随时提交 Pull Request。
498
-
499
- ## 📄 许可证
500
-
501
- MIT © Robiki sp. z o.o.
502
-
503
- ## 🔗 链接
504
-
505
- - [GitHub 仓库](https://github.com/robiki-ai/robiki-proxy)
506
- - [npm 包](https://www.npmjs.com/package/@robiki/proxy)
507
- - [问题跟踪器](https://github.com/robiki-ai/robiki-proxy/issues)
508
-
509
- ## 💡 用例
510
-
511
- - **微服务架构**:根据域名/路径将请求路由到不同的服务
512
- - **开发环境**:用于测试多个服务的本地代理
513
- - **API 网关**:具有身份验证和速率限制的集中入口点
514
- - **SSL 终止**:在代理级别处理 SSL/TLS
515
- - **CORS 管理**:集中式 CORS 配置
516
- - **负载均衡**:在多个实例之间分配流量(使用自定义处理程序)
517
-
518
292
  ## 🛠️ 故障排除
519
293
 
520
294
  ### 调试模式
521
295
 
522
- 启用详细日志记录以排除连接问题:
296
+ 启用详细日志记录:
523
297
 
524
298
  ```bash
525
- # 启用调试模式
526
- DEBUG=true node your-proxy-script.js
527
-
528
- # 或使用 Docker
299
+ DEBUG=true node your-script.js
300
+ #
529
301
  docker run -e DEBUG=true robiki/proxy:latest
530
-
531
- # 或在 docker-compose.yml 中
532
- services:
533
- proxy:
534
- image: robiki/proxy:latest
535
- environment:
536
- - DEBUG=true
537
302
  ```
538
303
 
539
- 当 `DEBUG=true` 时,代理将记录:
540
- - 所有代理连接尝试(REST、WebSocket、HTTP/2 流)
541
- - 请求和响应详细信息
542
- - 连接错误和超时
543
- - 代理错误和客户端错误
544
-
545
304
  ### 端口已被占用
546
305
 
547
- 代理将自动尝试终止配置端口上的进程。如果失败,请手动释放端口:
548
-
549
306
  ```bash
550
307
  lsof -ti:443 | xargs kill -9
551
- lsof -ti:8080 | xargs kill -9
552
308
  ```
553
309
 
554
- ### SSL 证书错误
555
-
556
- 确保您的证书文件可读并且格式正确(PEM)。对于开发,使用自签名证书。
557
-
558
- ### WebSocket 连接问题
559
-
560
- 确保您的 WebSocket 路由配置了正确的协议(ws/wss),并且目标服务支持 WebSocket 连接。
561
-
562
310
  ## 🧪 测试
563
311
 
564
- Robiki Proxy 包含一个全面的测试套件,涵盖单元测试、集成测试和高级场景。
565
-
566
- ### 运行测试
567
-
568
312
  ```bash
569
313
  # 运行所有测试
570
314
  yarn test
571
315
 
572
- # 在监视模式下运行测试
573
- yarn test:watch
574
-
575
- # 运行带覆盖率的测试
316
+ # 带覆盖率
576
317
  yarn test:coverage
577
318
 
578
- # 使用 UI 运行测试
579
- yarn test:ui
580
- ```
581
-
582
- ### 测试覆盖率
583
-
584
- 测试套件包括:
585
-
586
- - **单元测试**:配置、实用程序、标头转换、CORS 处理
587
- - **集成测试**:HTTP 代理、路由解析、验证、配置加载
588
- - **高级测试**:WebSocket 代理、HTTP/2 流、并发连接
589
- - **Docker 测试**:容器构建、配置加载、运行时行为
590
-
591
- ### Docker 测试
592
-
593
- 运行 Docker 集成测试:
594
-
595
- ```bash
596
- # 完整的 Docker 集成测试
319
+ # Docker 测试
597
320
  yarn test:docker
598
-
599
- # 专门测试配置加载
600
- yarn test:docker:config
601
-
602
- # 运行所有测试(单元 + 集成 + Docker)
603
- yarn test:all
604
321
  ```
605
322
 
606
- 或使用 Make:
607
-
608
- ```bash
609
- # 快速 Docker 构建测试
610
- make test-docker
611
-
612
- # 完整的集成测试套件
613
- make test-docker-full
614
-
615
- # 配置加载测试
616
- make test-docker-config
323
+ ## 📚 示例
617
324
 
618
- # Docker Compose 测试
619
- make test-docker-compose
620
- ```
325
+ 查看 `examples/` 目录:
621
326
 
622
- 有关更多详细信息,请参阅 [Docker 测试 README](tests/docker/README.md)。
327
+ - `basic-usage.js` - 简单的代理设置
328
+ - `advanced-usage.js` - 验证、CORS、重映射
329
+ - `custom-handlers.js` - 自定义请求处理器
330
+ - `docker-compose.example.yml` - Docker 设置
623
331
 
624
- ## 📊 性能
332
+ ## 🤝 贡献
625
333
 
626
- 该代理基于 Node.js 原生 HTTP/2 实现,专为高性能而设计:
334
+ 欢迎贡献!详情请参见 [CONTRIBUTING.md](CONTRIBUTING.md)。
627
335
 
628
- - 高效的流处理
629
- - 最小的开销
630
- - 连接池
631
- - 自动 HTTP/1.1 回退
336
+ ## 📄 许可证
632
337
 
633
- 对于生产部署,请考虑:
338
+ MIT © Robiki sp. z o.o.
634
339
 
635
- - 使用进程管理器(PM2、systemd)
636
- - 为多核系统启用集群
637
- - 使用健康检查进行监控
638
- - 设置适当的日志记录
340
+ ## 🔗 链接
639
341
 
342
+ - [GitHub 仓库](https://github.com/robiki-ai/robiki-proxy)
343
+ - [npm 包](https://www.npmjs.com/package/@robiki/proxy)
344
+ - [问题跟踪](https://github.com/robiki-ai/robiki-proxy/issues)