@cjwddz/mirror 1.2.0 → 2.0.0

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,14 +1,15 @@
1
- # Mirror —— HTTP 隧道工具
1
+ # Mirror - HTTP 隧道工具
2
2
 
3
- HTTP 隧道工具,将本地服务暴露到公网,无需公网 IP 或配置路由器。
3
+ 将本地服务暴露到公网,支持多服务代理。
4
4
 
5
- ## 功能特性
5
+ ## 特性
6
6
 
7
- - ✅ 将本地 HTTP 服务暴露到公网
8
- - ✅ WebSocket 隧道,低延迟高性能
9
- - ✅ 支持自定义端口和认证令牌
10
- - ✅ 自动重连机制
11
- - ✅ 支持并发请求
7
+ - ✅ 支持多个本地服务同时代理
8
+ - ✅ 基于路径长度的优先级匹配(路径越长优先级越高)
9
+ - ✅ 规则持久化存储
10
+ - ✅ Token 认证
11
+ - ✅ 单客户端连接(新连接自动踢出旧连接)
12
+ - ✅ 支持 WebSocket 协议
12
13
 
13
14
  ## 安装
14
15
 
@@ -16,195 +17,225 @@ HTTP 隧道工具,将本地服务暴露到公网,无需公网 IP 或配置
16
17
  npm install -g @cjwddz/mirror
17
18
  ```
18
19
 
19
- ## 使用方法
20
+ ## 快速开始
20
21
 
21
- ### 1. 启动服务端(公网服务器)
22
+ ### 1. 启动服务端
22
23
 
23
- 在有公网 IP 的服务器上运行:
24
+ 在公网服务器上运行:
24
25
 
25
26
  ```bash
26
- mirror server --port 80 --tunnel-port 7332
27
+ mirror server -p 80 -t 7332 --token your-token
27
28
  ```
28
29
 
29
- 可选参数:
30
+ 参数说明:
31
+ - `-p, --port`: HTTP 服务端口(默认 80)
32
+ - `-t, --tunnel-port`: WebSocket 隧道端口(默认 7332)
33
+ - `--host`: 监听地址(默认 0.0.0.0)
34
+ - `--token`: 认证令牌(可选)
35
+
36
+ ### 2. 添加代理规则
37
+
38
+ 在本地机器上添加规则:
39
+
40
+ ```bash
41
+ # 添加 API 规则(路径较长,优先级高)
42
+ mirror add example.com/api localhost:3001
30
43
 
31
- - `--port <port>`: HTTP 服务端口(默认: 80)
32
- - `--tunnel-port <port>`: WebSocket 隧道端口(默认: 7332)
33
- - `--host <host>`: 监听地址(默认: 0.0.0.0)
34
- - `--token <token>`: 认证令牌(可选,建议设置)
35
- - `--timeout <ms>`: 请求超时时间(毫秒,默认: 30000)
44
+ # 添加用户 API 规则(路径最长,优先级最高)
45
+ mirror add example.com/api/users localhost:3002
36
46
 
37
- ### 2. 启动客户端(本地开发机)
47
+ # 添加前端规则(根路径,优先级低)
48
+ mirror add example.com localhost:5173
49
+ ```
38
50
 
39
- 在本地开发机上运行:
51
+ ### 3. 连接服务端
40
52
 
41
53
  ```bash
42
- mirror client ws://your-server.com:7332 http://localhost:3000
54
+ mirror link mirror.tri-bank.online --token your-token
43
55
  ```
44
56
 
45
- 参数说明:
57
+ 连接后,规则会自动同步到服务端。
46
58
 
47
- - `serverUrl`: 服务端 WebSocket 地址,格式: `ws://host[:port]`
48
- - `targetUrl`: 本地服务地址,格式: `http://localhost:port`
59
+ ### 4. 查看状态
49
60
 
50
- 可选参数:
61
+ ```bash
62
+ mirror status
63
+ ```
51
64
 
52
- - `--token <token>`: 认证令牌(与服务端设置的一致)
53
- - `--reconnect-interval <ms>`: 重连间隔(毫秒,默认: 5000)
54
- - `--max-retries <number>`: 最大重试次数(默认: 10)
65
+ 输出示例:
55
66
 
56
- ### 3. 访问服务
67
+ ```
68
+ Mirror - Connection Status
69
+ ==========================
57
70
 
58
- 客户端连接成功后,访问服务端地址即可访问本地服务:
71
+ ✓ Mirror client is running
72
+ PID: 12345
73
+
74
+ Server Configuration:
75
+ Server: mirror.tri-bank.online
76
+ Token: your-toke...
77
+
78
+ Proxy Rules (3 configured):
79
+ [3] example.com/api/users -> localhost:3002 (高)
80
+ [1] example.com/api -> localhost:3001 (高)
81
+ [2] example.com/ -> localhost:5173 (低)
82
+ ```
83
+
84
+ ### 5. 查看规则
59
85
 
60
86
  ```bash
61
- # 例如,本地运行了一个服务在 localhost:3000
62
- # 服务端地址是 your-server.com
63
- # 现在可以通过 http://your-server.com 访问本地服务
87
+ mirror list
64
88
  ```
65
89
 
66
- ## 使用示例
90
+ 输出示例:
67
91
 
68
- ### 基本用法
92
+ ```
93
+ Mirror - Proxy Rules
94
+ ====================
69
95
 
70
- ```bash
71
- # 服务端(服务器)
72
- mirror server --port 80 --tunnel-port 7332
96
+ Total: 3 rule(s)
97
+
98
+ [3] example.com/api/users -> localhost:3002
99
+ Priority: 高(路径规则)
100
+ Created: 2/18/2026, 11:57:20 AM
73
101
 
74
- # 客户端(本地)
75
- mirror client ws://your-server.com:7332 http://localhost:3000
102
+ [1] example.com/api -> localhost:3001
103
+ Priority: 高(路径规则)
104
+ Created: 2/18/2026, 11:57:14 AM
105
+
106
+ [2] example.com/ -> localhost:5173
107
+ Priority: 低(域名规则)
108
+ Created: 2/18/2026, 11:57:17 AM
76
109
  ```
77
110
 
78
- ### 使用认证令牌
111
+ ### 6. 删除规则
79
112
 
80
113
  ```bash
81
- # 服务端
82
- mirror server --port 80 --tunnel-port 7332 --token my-secret-token
114
+ # 删除指定规则
115
+ mirror remove 1
83
116
 
84
- # 客户端
85
- mirror client ws://your-server.com:7332 http://localhost:3000 --token my-secret-token
117
+ # 删除所有规则
118
+ mirror remove
86
119
  ```
87
120
 
88
- ### 自定义端口
121
+ ### 7. 断开连接
89
122
 
90
123
  ```bash
91
- # 服务端监听 8080 端口,隧道端口 7332
92
- mirror server --port 8080 --tunnel-port 7332
93
-
94
- # 客户端连接到 7332 端口
95
- mirror client ws://your-server.com:7332 http://localhost:3000
124
+ mirror stop
96
125
  ```
97
126
 
98
- ### 使用 Nginx 反向代理
127
+ ## 命令参考
99
128
 
100
- ```nginx
101
- server {
102
- listen 80;
103
- server_name tunnel.example.com;
129
+ ### `mirror link <server> [options]`
104
130
 
105
- location / {
106
- proxy_pass http://127.0.0.1:80;
107
- proxy_http_version 1.1;
131
+ 连接到 Mirror 服务端。
108
132
 
109
- # WebSocket 升级
110
- proxy_set_header Upgrade $http_upgrade;
111
- proxy_set_header Connection "upgrade";
133
+ - `server`: 服务端地址(如 `mirror.tri-bank.online`)
134
+ - `-t, --token`: 认证令牌
112
135
 
113
- # 传递原始信息
114
- proxy_set_header Host $host;
115
- proxy_set_header X-Real-IP $remote_addr;
116
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
117
- proxy_set_header X-Forwarded-Proto $scheme;
136
+ ### `mirror add <domain/path> <target>`
118
137
 
119
- # 超时设置
120
- proxy_connect_timeout 7d;
121
- proxy_send_timeout 7d;
122
- proxy_read_timeout 7d;
123
- }
138
+ 添加代理规则。
124
139
 
125
- location /tunnel {
126
- proxy_pass http://127.0.0.1:7332;
127
- proxy_http_version 1.1;
140
+ - `domain/path`: 域名和路径组合(如 `example.com/api`)
141
+ - `target`: 目标服务地址(如 `localhost:3001`)
128
142
 
129
- proxy_set_header Upgrade $http_upgrade;
130
- proxy_set_header Connection "upgrade";
143
+ **规则格式说明:**
144
+ - 域名和路径写在一起,使用 `/` 分隔
145
+ - 路径可以省略,省略时默认为 `/`
146
+ - 示例:
147
+ - `example.com/api` - 匹配 `example.com/api` 开头的请求
148
+ - `example.com` 或 `example.com/` - 匹配 `example.com` 域名的所有请求
131
149
 
132
- proxy_set_header Host $host;
133
- proxy_set_header X-Real-IP $remote_addr;
134
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
150
+ ### `mirror remove [ruleId]`
135
151
 
136
- proxy_connect_timeout 7d;
137
- proxy_send_timeout 7d;
138
- proxy_read_timeout 7d;
139
- }
140
- }
141
- ```
152
+ 删除代理规则。
142
153
 
143
- 客户端连接时使用域名:
154
+ - `ruleId`: 规则 ID(可选,不指定则删除所有规则)
144
155
 
145
- ```bash
146
- mirror client ws://tunnel.example.com/tunnel http://localhost:3000
147
- ```
156
+ ### `mirror list`
148
157
 
149
- ## 工作原理
158
+ 查看所有代理规则。
150
159
 
151
- Mirror 通过 WebSocket 建立隧道,将客户端和服务端连接起来:
160
+ ### `mirror status`
152
161
 
153
- 1. **服务端**:监听 HTTP 端口,接收外部请求
154
- 2. **客户端**:连接到服务端 WebSocket 隧道
155
- 3. **请求转发**:服务端将 HTTP 请求通过隧道转发给客户端
156
- 4. **响应返回**:客户端将请求转发给本地服务,获取响应后返回给服务端
157
- 5. **外部访问**:用户访问服务端地址,实际访问的是本地服务
162
+ 查看当前连接状态和配置。
158
163
 
159
- ```
160
- 用户 → 服务端 (HTTP) → WebSocket 隧道 → 客户端 → 本地服务
161
- ```
164
+ ### `mirror stop`
162
165
 
163
- ## 注意事项
166
+ 断开与服务端的连接。
164
167
 
165
- - ⚠️ 服务端需要有公网 IP 或可访问的网络地址
166
- - ⚠️ 使用认证令牌可以提高安全性
167
- - ⚠️ 建议使用 Nginx 等 Web 服务器做反向代理
168
- - ⚠️ 确保防火墙开放相应端口
169
- - ℹ️ 仅用于开发和测试,不建议在生产环境中使用
168
+ ### `mirror server [options]`
170
169
 
171
- ## 故障排查
170
+ 启动 HTTP 隧道服务端。
172
171
 
173
- ### 无法连接
172
+ - `-p, --port`: HTTP 服务端口(默认 80)
173
+ - `-t, --tunnel-port`: WebSocket 隧道端口(默认 7332)
174
+ - `--host`: 监听地址(默认 0.0.0.0)
175
+ - `--token`: 认证令牌(可选)
176
+ - `--timeout`: 请求超时时间(毫秒,默认 30000)
174
177
 
175
- 1. 检查服务端是否正常运行
176
- 2. 检查防火墙是否开放端口
177
- 3. 检查 WebSocket 隧道端口是否正确
178
- 4. 检查认证令牌是否匹配
178
+ ## 规则匹配逻辑
179
179
 
180
- ### 请求超时
180
+ 1. 规则按路径长度降序排序(路径越长优先级越高)
181
+ 2. 同长度的规则,后添加的优先级更高
182
+ 3. 路径规则:请求路径以规则路径开头即匹配
183
+ 4. 根路径规则(`/`):匹配该域名的所有路径
181
184
 
182
- 1. 检查本地服务是否正常运行
183
- 2. 增加 `--timeout` 参数值
184
- 3. 检查网络连接是否稳定
185
+ ### 示例
185
186
 
186
- ### 频繁断线
187
+ 假设有以下规则:
187
188
 
188
- 1. 检查网络稳定性
189
- 2. 调整 `--reconnect-interval` 参数
190
- 3. 检查服务端资源使用情况
189
+ ```
190
+ [1] example.com/api -> localhost:3001 (路径长度: 4)
191
+ [2] example.com/ -> localhost:5173 (路径长度: 1)
192
+ [3] example.com/api/users -> localhost:3002 (路径长度: 10)
193
+ ```
191
194
 
192
- ## 开发
195
+ 请求匹配结果(按优先级排序):
196
+ 1. `example.com/api/users/v1` → `localhost:3002`(匹配规则 3,路径长度 10)
197
+ 2. `example.com/api/products` → `localhost:3001`(匹配规则 1,路径长度 4)
198
+ 3. `example.com/` → `localhost:5173`(匹配规则 2,路径长度 1)
199
+ 4. `example.com/about` → `localhost:5173`(匹配规则 2,路径长度 1)
200
+
201
+ ## 配置文件
202
+
203
+ 规则存储在 `~/.mirror/rules.json` 中:
204
+
205
+ ```json
206
+ {
207
+ "server": "mirror.tri-bank.online",
208
+ "token": "your-token",
209
+ "rules": [
210
+ {
211
+ "id": 1,
212
+ "domain": "example.com",
213
+ "path": "/api",
214
+ "target": "localhost:3001",
215
+ "priority": "high",
216
+ "createdAt": "2026-02-18T03:50:18.627Z"
217
+ }
218
+ ]
219
+ }
220
+ ```
193
221
 
194
- ```bash
195
- # 安装依赖
196
- npm install
222
+ 断开连接后规则会保留,下次连接时自动同步。
197
223
 
198
- # 构建
199
- npm run build
224
+ ## 进程管理
200
225
 
201
- # 测试
202
- npm test
226
+ Mirror 客户端使用锁文件机制确保同一时间只有一个实例在运行。锁文件位于 `~/.mirror/.lock`。
203
227
 
204
- # Lint
205
- npm run lint
206
- ```
228
+ - 如果尝试启动第二个实例,会提示已有进程在运行
229
+ - 如果进程异常退出,锁文件会被自动清理
230
+ - 使用 `mirror stop` 命令可以安全停止运行中的客户端
231
+
232
+ ## 注意事项
233
+
234
+ - 服务端和客户端必须使用相同的 token(如果配置了)
235
+ - 端口需要开放防火墙访问
236
+ - 建议在生产环境使用 HTTPS
237
+ - WebSocket 隧道使用 wss:// 协议,需要 SSL 证书
207
238
 
208
- ## License
239
+ ## 许可证
209
240
 
210
241
  MIT
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Mirror CLI 命令实现
3
+ */
4
+ /**
5
+ * link 命令:连接服务端
6
+ */
7
+ export declare function linkCommand(server: string, options: {
8
+ token?: string;
9
+ }): Promise<void>;
10
+ /**
11
+ * add 命令:添加代理规则
12
+ */
13
+ export declare function addCommand(domainPath: string, target: string): Promise<void>;
14
+ /**
15
+ * remove 命令:删除代理规则
16
+ */
17
+ export declare function removeCommand(ruleId?: string): Promise<void>;
18
+ /**
19
+ * list 命令:查看代理规则
20
+ */
21
+ export declare function listCommand(): Promise<void>;
22
+ /**
23
+ * stop 命令:断开连接
24
+ */
25
+ export declare function stopCommand(): Promise<void>;
26
+ /**
27
+ * status 命令:查看连接状态
28
+ */
29
+ export declare function statusCommand(): Promise<void>;
30
+ //# sourceMappingURL=cli-commands.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli-commands.d.ts","sourceRoot":"","sources":["../../src/cli/cli-commands.ts"],"names":[],"mappings":"AAAA;;GAEG;AAmEH;;GAEG;AACH,wBAAsB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE;IAAE,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CA8F5F;AAED;;GAEG;AACH,wBAAsB,UAAU,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CA0DlF;AAED;;GAEG;AACH,wBAAsB,aAAa,CAAC,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAoClE;AAED;;GAEG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CAsBjD;AAED;;GAEG;AACH,wBAAsB,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC,CA+BjD;AAED;;GAEG;AACH,wBAAsB,aAAa,IAAI,OAAO,CAAC,IAAI,CAAC,CAyDnD"}