-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
executable file
·217 lines (180 loc) · 5.38 KB
/
cli.ts
File metadata and controls
executable file
·217 lines (180 loc) · 5.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env bun
/**
* rproxy CLI
* Command-line interface for managing reverse proxy routes
*/
import { Config } from "./lib/config";
import { certCommand } from "./cmd/cert";
import { updateCommand } from "./cmd/update";
const args = process.argv.slice(2);
const command = args[0];
function printUsage() {
console.log(`rproxy - A fast reverse proxy with automatic HTTPS
Usage:
rproxy <command> [options]
Commands:
add <backend> <host> Add a reverse proxy route
rm <host> Remove a route
list List all routes
stats Show statistics
cert <subcommand> Certificate management
update Check for updates
save Save configuration
serve Start the proxy server
help Show this help
Certificate Commands:
cert install Install certbot (automatic)
cert issue <domain> Issue HTTPS certificate (zero-downtime)
cert list List all certificates
cert renew Renew certificates
cert auto-renew Setup automatic renewal
Examples:
# Route management
rproxy add 127.0.0.1:3000 mysite.com
rproxy add localhost:8080 api.example.com
rproxy rm mysite.com
rproxy list
# Certificate management
rproxy cert install
rproxy cert issue mysite.com
rproxy cert list
# Statistics
rproxy stats
`);
}
async function addRoute(backend: string, host: string) {
if (!backend || !host) {
console.error("Error: Both backend and host are required");
console.log("Usage: rproxy add <backend> <host>");
process.exit(1);
}
// Normalize backend URL
let normalizedBackend = backend;
if (!backend.startsWith("http://") && !backend.startsWith("https://")) {
normalizedBackend = `http://${backend}`;
}
const config = Config.getInstance();
config.addRoute(host, normalizedBackend);
console.log(`✓ Route added: ${host} -> ${normalizedBackend}`);
console.log("\nTo apply changes:");
console.log(" sudo systemctl reload rproxy");
console.log("Or if running directly:");
console.log(" pkill -HUP -f 'bun.*server.ts'");
}
async function removeRoute(host: string) {
if (!host) {
console.error("Error: Host is required");
console.log("Usage: rproxy rm <host>");
process.exit(1);
}
const config = Config.getInstance();
const removed = config.removeRoute(host);
if (removed) {
console.log(`✓ Route removed: ${host}`);
console.log("\nTo apply changes:");
console.log(" sudo systemctl reload rproxy");
} else {
console.error(`Error: Route not found: ${host}`);
process.exit(1);
}
}
async function listRoutes() {
const config = Config.getInstance();
const routes = config.getAllRoutes();
if (routes.length === 0) {
console.log("No routes configured.");
return;
}
console.log("Configured Routes:");
console.log("==================");
routes.forEach((route) => {
console.log(` ${route.host} -> ${route.backend}`);
});
console.log(`\nTotal: ${routes.length} route(s)`);
}
async function showStats() {
try {
const response = await fetch("http://localhost:9090/internal/stats");
if (!response.ok) {
throw new Error("Failed to fetch stats");
}
const stats = await response.json();
console.log("Reverse Proxy Statistics");
console.log("========================");
console.log(`Total Requests: ${stats.totalRequests}`);
console.log(`Success: ${stats.successRequests}`);
console.log(`Failed: ${stats.failedRequests}`);
const hostStats = stats.hostStats;
if (hostStats && Object.keys(hostStats).length > 0) {
console.log("\nPer-Host Statistics:");
console.log("--------------------");
for (const [host, data] of Object.entries(hostStats)) {
const s = data as any;
console.log(` ${host}:`);
console.log(` Requests: ${s.requests}`);
console.log(` Success: ${s.success}`);
console.log(` Failed: ${s.failed}`);
}
}
} catch (error) {
console.error("Error: Unable to connect to rproxy service. Is it running?");
console.log("Start the service with: sudo systemctl start rproxy");
process.exit(1);
}
}
async function saveConfig() {
console.log("✓ Configuration is automatically saved");
console.log("(using SQLite, no manual save needed)");
}
async function serve() {
// Import and start server
await import("./server");
}
// Route commands
switch (command) {
case "add":
await addRoute(args[1], args[2]);
break;
case "rm":
case "remove":
await removeRoute(args[1]);
break;
case "list":
case "ls":
await listRoutes();
break;
case "stats":
case "status":
await showStats();
break;
case "save":
await saveConfig();
break;
case "serve":
case "start":
await serve();
break;
case "cert":
case "certificate":
case "ssl":
case "https":
await certCommand(args.slice(1));
break;
case "update":
case "upgrade":
await updateCommand();
break;
case "help":
case "--help":
case "-h":
printUsage();
break;
default:
if (!command) {
printUsage();
} else {
console.error(`Unknown command: ${command}`);
console.log("Run 'rproxy help' for usage information");
process.exit(1);
}
}