voidconnect 0.1.14 → 0.1.16
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/hub/src/index.ts +28 -0
- package/hub/src/logger.ts +53 -0
- package/package.json +1 -1
package/hub/src/index.ts
CHANGED
|
@@ -5,9 +5,30 @@ import { authMiddleware } from './auth';
|
|
|
5
5
|
import { processManager } from './process_manager';
|
|
6
6
|
import qrcode from 'qrcode-terminal';
|
|
7
7
|
import { networkInterfaces } from 'os';
|
|
8
|
+
import { logTraffic, formatRequest, formatResponse } from './logger';
|
|
8
9
|
|
|
9
10
|
const app = new Hono();
|
|
10
11
|
|
|
12
|
+
app.use('*', async (c, next) => {
|
|
13
|
+
if (process.argv.includes('--log')) {
|
|
14
|
+
try {
|
|
15
|
+
await logTraffic('INCOMING_REQUEST', await formatRequest(c.req.raw));
|
|
16
|
+
} catch (e) {
|
|
17
|
+
console.error('Error logging request:', e);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
await next();
|
|
22
|
+
|
|
23
|
+
if (process.argv.includes('--log')) {
|
|
24
|
+
try {
|
|
25
|
+
await logTraffic('OUTGOING_RESPONSE', await formatResponse(c.res));
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.error('Error logging response:', e);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
|
|
11
32
|
app.use('/api/*', authMiddleware);
|
|
12
33
|
app.use('/project/*', authMiddleware);
|
|
13
34
|
|
|
@@ -53,7 +74,14 @@ app.all('/project/:name/*', async (c) => {
|
|
|
53
74
|
body: c.req.raw.body,
|
|
54
75
|
});
|
|
55
76
|
|
|
77
|
+
if (process.argv.includes('--log')) {
|
|
78
|
+
await logTraffic('PROXY_REQUEST_OUTGOING', await formatRequest(newReq));
|
|
79
|
+
}
|
|
56
80
|
const response = await fetch(newReq);
|
|
81
|
+
if (process.argv.includes('--log')) {
|
|
82
|
+
await logTraffic('PROXY_RESPONSE_INCOMING', await formatResponse(response));
|
|
83
|
+
}
|
|
84
|
+
|
|
57
85
|
return response;
|
|
58
86
|
} catch (e) {
|
|
59
87
|
console.error(`[Proxy] Error:`, e);
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
|
|
2
|
+
import { appendFile } from 'fs/promises';
|
|
3
|
+
import { join } from 'path';
|
|
4
|
+
|
|
5
|
+
const LOG_FILE = 'C:\\hub_traffic.log';
|
|
6
|
+
|
|
7
|
+
export async function logTraffic(type: string, data: any) {
|
|
8
|
+
const timestamp = new Date().toISOString();
|
|
9
|
+
const logEntry = `[${timestamp}] [${type}]\n${JSON.stringify(data, null, 2)}\n\n--------------------------------------------------\n\n`;
|
|
10
|
+
try {
|
|
11
|
+
await appendFile(LOG_FILE, logEntry);
|
|
12
|
+
} catch (err) {
|
|
13
|
+
console.error('Failed to write to log file:', err);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function formatRequest(req: Request) {
|
|
18
|
+
let body = '(empty)';
|
|
19
|
+
try {
|
|
20
|
+
if (req.body) {
|
|
21
|
+
// we clone to not consume the stream needed for processing
|
|
22
|
+
body = await req.clone().text();
|
|
23
|
+
}
|
|
24
|
+
} catch (e) {
|
|
25
|
+
body = `(stream/error reading body: ${e})`;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
url: req.url,
|
|
30
|
+
method: req.method,
|
|
31
|
+
headers: Object.fromEntries(req.headers.entries()),
|
|
32
|
+
body
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export async function formatResponse(res: Response) {
|
|
37
|
+
let body = '(empty)';
|
|
38
|
+
try {
|
|
39
|
+
// Cloning response to read body without consuming the original stream being sent to client
|
|
40
|
+
if (res.body) {
|
|
41
|
+
body = await res.clone().text();
|
|
42
|
+
}
|
|
43
|
+
} catch (e) {
|
|
44
|
+
body = `(stream/error reading body: ${e})`;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return {
|
|
48
|
+
status: res.status,
|
|
49
|
+
statusText: res.statusText,
|
|
50
|
+
headers: Object.fromEntries(res.headers.entries()),
|
|
51
|
+
body
|
|
52
|
+
};
|
|
53
|
+
}
|