-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-client.js
162 lines (136 loc) · 3.85 KB
/
test-client.js
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
const WebSocket = require('ws');
const http = require('http');
// Create a local HTTP server to tunnel
const localServer = http.createServer((req, res) => {
console.log('Local server received request:', req.method, req.url);
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});
req.on('end', () => {
console.log('Request body:', body);
// Forward request to test server
const options = {
hostname: 'localhost',
port: 8000, // Test server port
path: req.url,
method: req.method,
headers: {
...req.headers,
host: 'localhost:8000'
}
};
const testServerReq = http.request(options, (testServerRes) => {
// Copy status and headers from test server response
res.writeHead(testServerRes.statusCode, testServerRes.headers);
// Forward response body
testServerRes.on('data', chunk => {
res.write(chunk);
});
testServerRes.on('end', () => {
res.end();
});
});
testServerReq.on('error', (error) => {
console.error('Error forwarding to test server:', error);
res.writeHead(502, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Error forwarding request to test server' }));
});
// Forward request body if present
if (body) {
testServerReq.write(body);
}
testServerReq.end();
});
});
const LOCAL_PORT = 3000;
const SUBDOMAIN = 'test';
const SERVER_IP = 'dfanso.dev';
const TUNNEL_SERVER = `wss://${SERVER_IP}:8080`;
localServer.listen(LOCAL_PORT, () => {
console.log(`Local server listening on port ${LOCAL_PORT}`);
// Connect to tunnel server
const ws = new WebSocket(TUNNEL_SERVER, {
rejectUnauthorized: false // Only for testing, remove in production
});
ws.on('open', () => {
console.log('Connected to tunnel server');
// Register the tunnel
ws.send(JSON.stringify({
type: 'register',
subdomain: SUBDOMAIN,
port: LOCAL_PORT
}));
});
ws.on('message', (data) => {
try {
const message = JSON.parse(data.toString());
console.log('Received message:', message);
if (message.type === 'request') {
handleTunnelRequest(message, ws);
}
} catch (err) {
console.error('Error parsing message:', err);
}
});
ws.on('error', (error) => {
console.error('WebSocket error:', error);
});
ws.on('close', () => {
console.log('Connection closed');
process.exit(1);
});
});
function handleTunnelRequest(message, ws) {
const { clientId, method, path, headers, body } = message;
console.log('Handling tunnel request:', method, path);
console.log('Request body:', body);
// Create options for local request
const options = {
hostname: 'localhost',
port: LOCAL_PORT,
path: path,
method: method,
headers: {
...headers,
host: `localhost:${LOCAL_PORT}`
}
};
// Make request to local server
const req = http.request(options, (res) => {
let responseBody = '';
res.on('data', (chunk) => {
responseBody += chunk;
});
res.on('end', () => {
console.log('Local server response:', responseBody);
// Send response back through tunnel
ws.send(JSON.stringify({
type: 'response',
clientId: clientId,
statusCode: res.statusCode,
headers: res.headers,
data: Buffer.from(responseBody).toString('base64')
}));
});
});
req.on('error', (error) => {
console.error('Error making local request:', error);
ws.send(JSON.stringify({
type: 'error',
clientId: clientId,
error: error.message
}));
});
// Write request body if present
if (body) {
req.write(body);
}
req.end();
}
// Handle graceful shutdown
process.on('SIGINT', () => {
console.log('Shutting down...');
localServer.close();
process.exit(0);
});