Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
287 lines (251 loc) · 10.6 KB
/
Copy pathserver.js
File metadata and controls
287 lines (251 loc) · 10.6 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { v4 as create_request_uuid } from 'uuid';
import * as utils from './utils.js';
import * as proxy from './proxy.js';
import * as cdp from './cdp.js';
import * as requestengine from './requestengine.js';
import * as fetchgen from './fetchgen.js';
import * as logger from './logger.js';
import { start_health_monitor } from './healthcheck.js';
const PROXY_AUTHENTICATION_ENABLED = Boolean(process.env.PROXY_USERNAME && process.env.PROXY_PASSWORD);
const DEBUG_TRACE_HEADER_NAME = 'X-Debug-Id';
// Top-level error handling for unhandled promise rejections
process.on('unhandledRejection', (reason, promise) => {
const app_logger = logger.get_logger();
const error_payload = {
promise: typeof promise === 'object' ? String(promise) : promise,
reason: reason instanceof Error ? reason.message : reason,
stack: reason instanceof Error ? reason.stack : undefined
};
app_logger.error('Unhandled promise rejection captured.', error_payload);
});
// Optional: catch uncaught exceptions too
process.on('uncaughtException', (err) => {
const app_logger = logger.get_logger();
app_logger.error('Uncaught exception encountered.', {
message: err.message,
stack: err.stack
});
});
(async() => {
const app_logger = logger.get_logger();
let http_proxy_port = 1234;
if (process.env.HTTP_PROXY_PORT) {
http_proxy_port = parseInt(process.env.HTTP_PROXY_PORT);
}
if (!PROXY_AUTHENTICATION_ENABLED) {
const warning_banner_lines = [
'**********************************************************************',
'* [WARN] THERMOPTIC PROXY RUNNING WITHOUT AUTHENTICATION *',
'* PROXY_USERNAME/PROXY_PASSWORD not set. Any local clients can use *',
'* this proxy. Ensure it is not exposed publicly or abuse may occur. *',
'**********************************************************************'
];
warning_banner_lines.forEach((line) => {
app_logger.warn(line);
});
}
app_logger.info('thermoptic has begun the initializing process.');
const http_proxy = await proxy.get_http_proxy(
http_proxy_port,
() => {
app_logger.info('The thermoptic HTTP Proxy server is now running.');
},
(error) => {
app_logger.error('The thermoptic HTTP Proxy server encountered an unexpected error.', {
message: error && error.message ? error.message : String(error)
});
},
async(proxy_request) => {
// First things first, ensure user is properly authenticated.
const request_id = create_request_uuid();
const request_logger = logger.get_request_logger({ request_id });
proxy_request.request_id = request_id;
request_logger.info('Inbound proxy request received.', {
url: proxy_request.url,
protocol: proxy_request.protocol,
method: proxy_request.requestOptions.method,
path: proxy_request.requestOptions.path
});
const is_authenticated = get_authentication_status(proxy_request);
if (!is_authenticated) {
request_logger.warn('Authentication failed for inbound proxy request.');
const auth_response = create_authentication_required_response();
attach_debug_id_header(auth_response, request_id);
return {
response: auth_response
};
}
request_logger.debug('Authentication successful for inbound proxy request.');
let cdp_instance = null;
let request_completed = false;
let response_status_code = null;
let request_error = null;
try {
// We now check if there is an before-request hook defined.
if (process.env.BEFORE_REQUEST_HOOK_FILE_PATH) {
request_logger.info('Executing before-request hook.', {
hook_file: process.env.BEFORE_REQUEST_HOOK_FILE_PATH
});
cdp_instance = await cdp.start_browser_session();
await utils.run_hook_file(process.env.BEFORE_REQUEST_HOOK_FILE_PATH, cdp_instance, proxy_request, null, request_logger);
}
const response = await requestengine.process_request(
request_logger,
proxy_request.url,
proxy_request.protocol,
proxy_request.requestOptions.method,
proxy_request.requestOptions.path,
utils.convert_headers_array(proxy_request._req.rawHeaders),
proxy_request.requestData,
);
// We now check if there is an after-request hook defined.
if (process.env.AFTER_REQUEST_HOOK_FILE_PATH) {
try {
if (!cdp_instance) {
cdp_instance = await cdp.start_browser_session();
}
request_logger.info('Executing after-request hook.', {
hook_file: process.env.AFTER_REQUEST_HOOK_FILE_PATH
});
await utils.run_hook_file(process.env.AFTER_REQUEST_HOOK_FILE_PATH, cdp_instance, proxy_request, response, request_logger);
} catch (after_hook_error) {
request_logger.warn('After-request hook failed; returning response without hook effects.', {
message: after_hook_error instanceof Error ? after_hook_error.message : String(after_hook_error),
stack: after_hook_error instanceof Error ? after_hook_error.stack : undefined
});
}
}
attach_debug_id_header(response, request_id);
request_logger.info('Successfully generated response for proxy request.', {
status_code: response.statusCode,
headers_count: response.header ? Object.keys(response.header).length : 0
});
request_completed = true;
response_status_code = response.statusCode;
return {
response: response
};
} catch (err) {
attach_debug_id_to_error_response(err, request_id);
request_error = {
message: err instanceof Error ? err.message : String(err),
stack: err instanceof Error ? err.stack : undefined
};
request_logger.error('Proxy request failed.', request_error);
throw err;
} finally {
if (cdp_instance) {
try {
await cdp_instance.close();
} catch (closeErr) {
request_logger.warn('Failed to close CDP session.', {
message: closeErr.message,
stack: closeErr.stack
});
}
}
const lifecycle_summary = {
success: request_completed,
status_code: response_status_code
};
if (request_error) {
lifecycle_summary.error = request_error;
}
request_logger.info('Completed proxy request lifecycle.', lifecycle_summary);
}
}
);
http_proxy.start();
try {
await start_health_monitor();
} catch (monitor_error) {
app_logger.error('Failed to start health monitor.', {
message: monitor_error instanceof Error ? monitor_error.message : String(monitor_error),
stack: monitor_error instanceof Error ? monitor_error.stack : undefined
});
}
})();
function get_authentication_status(request) {
if (!PROXY_AUTHENTICATION_ENABLED) {
return true;
}
const connection_state = request.connection_state;
if (connection_state && connection_state.is_authenticated) {
connection_state.last_seen = Date.now();
return true;
}
const proxy_authentication = request.proxy_authorization_header;
if (!proxy_authentication || !(proxy_authentication.includes('Basic'))) {
return false;
}
const proxy_auth_string = Buffer.from(
proxy_authentication.replace(
'Basic ',
''
).trim(),
'base64'
).toString();
const proxy_auth_string_parts = proxy_auth_string.split(':');
const username = proxy_auth_string_parts[0];
const password = proxy_auth_string_parts[1];
const creds_sent = `${username}:${password}`;
const creds_set = `${process.env.PROXY_USERNAME}:${process.env.PROXY_PASSWORD}`;
const is_authenticated = utils.time_safe_compare(creds_set, creds_sent);
if (is_authenticated && connection_state) {
connection_state.is_authenticated = true;
connection_state.last_seen = Date.now();
}
return is_authenticated;
}
function create_authentication_required_response() {
return {
statusCode: 407,
header: {
'Proxy-Authenticate': 'Basic realm="Please provide valid credentials."'
},
body: 'Provide credentials.'
};
}
function attach_debug_id_header(response, request_id) {
if (!response || !request_id) {
return;
}
if (!logger.is_debug_enabled()) {
return;
}
if (response.header instanceof Map) {
response.header.set(DEBUG_TRACE_HEADER_NAME, request_id);
return;
}
if (Array.isArray(response.header)) {
response.header.push({
name: DEBUG_TRACE_HEADER_NAME,
value: request_id
});
return;
}
if (!response.header || typeof response.header !== 'object') {
response.header = {};
}
response.header[DEBUG_TRACE_HEADER_NAME] = request_id;
}
function attach_debug_id_to_error_response(error, request_id) {
if (!error || !request_id) {
return;
}
if (error && typeof error === 'object' && error.response && typeof error.response === 'object') {
attach_debug_id_header(error.response, request_id);
}
if (is_response_like_object(error)) {
attach_debug_id_header(error, request_id);
}
}
function is_response_like_object(candidate) {
if (!candidate || typeof candidate !== 'object') {
return false;
}
if (typeof candidate.statusCode === 'number') {
return true;
}
return false;
}