-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathserver.js
More file actions
995 lines (883 loc) · 36.5 KB
/
server.js
File metadata and controls
995 lines (883 loc) · 36.5 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
#!/usr/bin/env node
/**
* AWF API Proxy Sidecar
*
* Node.js-based proxy that:
* 1. Keeps LLM API credentials isolated from agent container
* 2. Routes all traffic through Squid via HTTP_PROXY/HTTPS_PROXY
* 3. Injects authentication headers (Authorization, x-api-key)
* 4. Respects domain whitelisting enforced by Squid
*/
const http = require('http');
const https = require('https');
const tls = require('tls');
const { URL } = require('url');
const { HttpsProxyAgent } = require('https-proxy-agent');
const { generateRequestId, sanitizeForLog, logRequest } = require('./logging');
const metrics = require('./metrics');
const rateLimiter = require('./rate-limiter');
let trackTokenUsage;
let trackWebSocketTokenUsage;
let closeLogStream;
try {
({ trackTokenUsage, trackWebSocketTokenUsage, closeLogStream } = require('./token-tracker'));
} catch (err) {
if (err && err.code === 'MODULE_NOT_FOUND') {
trackTokenUsage = () => {};
trackWebSocketTokenUsage = () => {};
closeLogStream = () => {};
} else {
throw err;
}
}
// Create rate limiter from environment variables
const limiter = rateLimiter.create();
// Max request body size (10 MB) to prevent DoS via large payloads
const MAX_BODY_SIZE = 10 * 1024 * 1024;
// Headers that must never be forwarded from the client.
// The proxy controls authentication — client-supplied auth/proxy headers are stripped.
const STRIPPED_HEADERS = new Set([
'host',
'authorization',
'proxy-authorization',
'x-api-key',
'forwarded',
'via',
]);
/** Returns true if the header name should be stripped (case-insensitive). */
function shouldStripHeader(name) {
const lower = name.toLowerCase();
return STRIPPED_HEADERS.has(lower) || lower.startsWith('x-forwarded-');
}
// Read API keys from environment (set by docker-compose)
// Trim whitespace/newlines to prevent malformed HTTP headers — env vars from
// CI secrets or docker-compose YAML may include trailing whitespace.
const OPENAI_API_KEY = (process.env.OPENAI_API_KEY || '').trim() || undefined;
const ANTHROPIC_API_KEY = (process.env.ANTHROPIC_API_KEY || '').trim() || undefined;
const COPILOT_GITHUB_TOKEN = (process.env.COPILOT_GITHUB_TOKEN || '').trim() || undefined;
const GEMINI_API_KEY = (process.env.GEMINI_API_KEY || '').trim() || undefined;
/**
* Normalizes an API target value to a bare hostname.
* Accepts either a hostname or a full URL and extracts only the hostname,
* discarding any scheme, path, query, fragment, credentials, or port.
* Path configuration must be provided separately via the existing
* *_API_BASE_PATH environment variables.
*
* @param {string|undefined} value - Raw env var value
* @returns {string|undefined} Bare hostname, or undefined if input is falsy
*/
function normalizeApiTarget(value) {
if (!value) return value;
const trimmed = value.trim();
if (!trimmed) return undefined;
const candidate = /^[a-zA-Z][a-zA-Z\d+\-.]*:\/\//.test(trimmed)
? trimmed
: `https://${trimmed}`;
try {
const parsed = new URL(candidate);
if (parsed.pathname !== '/' || parsed.search || parsed.hash || parsed.username || parsed.password || parsed.port) {
console.warn(
`Ignoring unsupported API target URL components in ${sanitizeForLog(trimmed)}; ` +
'configure path prefixes via the corresponding *_API_BASE_PATH environment variable.'
);
}
return parsed.hostname || undefined;
} catch (err) {
console.warn(`Invalid API target ${sanitizeForLog(trimmed)}; expected a hostname (e.g. 'api.example.com') or URL`);
return undefined;
}
}
// Configurable API target hosts (supports custom endpoints / internal LLM routers)
// Values are normalized to bare hostnames — buildUpstreamPath() prepends https://
const OPENAI_API_TARGET = normalizeApiTarget(process.env.OPENAI_API_TARGET) || 'api.openai.com';
const ANTHROPIC_API_TARGET = normalizeApiTarget(process.env.ANTHROPIC_API_TARGET) || 'api.anthropic.com';
const GEMINI_API_TARGET = normalizeApiTarget(process.env.GEMINI_API_TARGET) || 'generativelanguage.googleapis.com';
/**
* Normalizes a base path for use as a URL path prefix.
* Ensures the path starts with '/' (if non-empty) and has no trailing '/'.
* Returns '' for empty, null, or undefined inputs.
*
* @param {string|undefined|null} rawPath - The raw path value from env or config
* @returns {string} Normalized path prefix (e.g. '/serving-endpoints') or ''
*/
function normalizeBasePath(rawPath) {
if (!rawPath) return '';
let path = rawPath.trim();
if (!path) return '';
// Ensure leading slash
if (!path.startsWith('/')) {
path = '/' + path;
}
// Strip trailing slash (but preserve a bare '/')
if (path !== '/' && path.endsWith('/')) {
path = path.slice(0, -1);
}
return path;
}
/**
* Build the full upstream path by joining basePath, reqUrl's pathname, and query string.
*
* Examples:
* buildUpstreamPath('/v1/chat/completions', 'api.openai.com', '')
* → '/v1/chat/completions'
* buildUpstreamPath('/v1/chat/completions', 'host.databricks.com', '/serving-endpoints')
* → '/serving-endpoints/v1/chat/completions'
* buildUpstreamPath('/v1/messages?stream=true', 'host.com', '/anthropic')
* → '/anthropic/v1/messages?stream=true'
*
* @param {string} reqUrl - The incoming request URL (must start with '/')
* @param {string} targetHost - The upstream hostname (used only to parse the URL)
* @param {string} basePath - Normalized base path prefix (e.g. '/serving-endpoints' or '')
* @returns {string} Full upstream path including query string
*/
function buildUpstreamPath(reqUrl, targetHost, basePath) {
const targetUrl = new URL(reqUrl, `https://${targetHost}`);
const prefix = basePath === '/' ? '' : basePath;
return prefix + targetUrl.pathname + targetUrl.search;
}
// Optional base path prefixes for API targets (e.g. /serving-endpoints for Databricks)
const OPENAI_API_BASE_PATH = normalizeBasePath(process.env.OPENAI_API_BASE_PATH);
const ANTHROPIC_API_BASE_PATH = normalizeBasePath(process.env.ANTHROPIC_API_BASE_PATH);
const GEMINI_API_BASE_PATH = normalizeBasePath(process.env.GEMINI_API_BASE_PATH);
// Configurable Copilot API target host (supports GHES/GHEC / custom endpoints)
// Priority: COPILOT_API_TARGET env var > auto-derive from GITHUB_SERVER_URL > default
function deriveCopilotApiTarget() {
if (process.env.COPILOT_API_TARGET) {
return normalizeApiTarget(process.env.COPILOT_API_TARGET);
}
// Auto-derive from GITHUB_SERVER_URL:
// - GitHub Enterprise Cloud (*.ghe.com): Copilot inference/models/MCP are served at
// copilot-api.<subdomain>.ghe.com (separate from the GitHub REST API at api.*)
// - GitHub Enterprise Server (non-github.com, non-ghe.com) → api.enterprise.githubcopilot.com
// - github.com → api.githubcopilot.com
const serverUrl = process.env.GITHUB_SERVER_URL;
if (serverUrl) {
try {
const hostname = new URL(serverUrl).hostname;
if (hostname !== 'github.com') {
// Check if this is a GHEC tenant (*.ghe.com)
if (hostname.endsWith('.ghe.com')) {
// Extract subdomain: mycompany.ghe.com → mycompany
const subdomain = hostname.slice(0, -8); // Remove '.ghe.com'
// GHEC routes Copilot inference to copilot-api.<subdomain>.ghe.com,
// not to api.<subdomain>.ghe.com (which is the GitHub REST API)
return `copilot-api.${subdomain}.ghe.com`;
}
// GHES (any other non-github.com hostname)
return 'api.enterprise.githubcopilot.com';
}
} catch {
// Invalid URL — fall through to default
}
}
return 'api.githubcopilot.com';
}
const COPILOT_API_TARGET = deriveCopilotApiTarget();
// Squid proxy configuration (set via HTTP_PROXY/HTTPS_PROXY in docker-compose)
const HTTPS_PROXY = process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
logRequest('info', 'startup', {
message: 'Starting AWF API proxy sidecar',
squid_proxy: HTTPS_PROXY || 'not configured',
api_targets: {
openai: OPENAI_API_TARGET,
anthropic: ANTHROPIC_API_TARGET,
gemini: GEMINI_API_TARGET,
copilot: COPILOT_API_TARGET,
},
api_base_paths: {
openai: OPENAI_API_BASE_PATH || '(none)',
anthropic: ANTHROPIC_API_BASE_PATH || '(none)',
gemini: GEMINI_API_BASE_PATH || '(none)',
},
providers: {
openai: !!OPENAI_API_KEY,
anthropic: !!ANTHROPIC_API_KEY,
gemini: !!GEMINI_API_KEY,
copilot: !!COPILOT_GITHUB_TOKEN,
},
});
// Create proxy agent for routing through Squid
const proxyAgent = HTTPS_PROXY ? new HttpsProxyAgent(HTTPS_PROXY) : undefined;
if (!proxyAgent) {
logRequest('warn', 'startup', { message: 'No HTTPS_PROXY configured, requests will go direct' });
}
/**
* Check rate limit and send 429 if exceeded.
* Returns true if request was rate-limited (caller should return early).
*/
function checkRateLimit(req, res, provider, requestBytes) {
const check = limiter.check(provider, requestBytes);
if (!check.allowed) {
const clientRequestId = req.headers['x-request-id'];
const requestId = (typeof clientRequestId === 'string' &&
clientRequestId.length <= 128 &&
/^[\w\-\.]+$/.test(clientRequestId))
? clientRequestId : generateRequestId();
const limitLabels = { rpm: 'requests per minute', rph: 'requests per hour', bytes_pm: 'bytes per minute' };
const windowLabel = limitLabels[check.limitType] || check.limitType;
metrics.increment('rate_limit_rejected_total', { provider, limit_type: check.limitType });
logRequest('warn', 'rate_limited', {
request_id: requestId,
provider,
limit_type: check.limitType,
limit: check.limit,
retry_after: check.retryAfter,
});
res.writeHead(429, {
'Content-Type': 'application/json',
'Retry-After': String(check.retryAfter),
'X-RateLimit-Limit': String(check.limit),
'X-RateLimit-Remaining': String(check.remaining),
'X-RateLimit-Reset': String(check.resetAt),
'X-Request-ID': requestId,
});
res.end(JSON.stringify({
error: {
type: 'rate_limit_error',
message: `Rate limit exceeded for ${provider} provider. Limit: ${check.limit} ${windowLabel}. Retry after ${check.retryAfter} seconds.`,
provider,
limit: check.limit,
window: check.limitType === 'rpm' ? 'per_minute' : check.limitType === 'rph' ? 'per_hour' : 'per_minute_bytes',
retry_after: check.retryAfter,
},
}));
return true;
}
return false;
}
/**
* Forward a request to the target API, injecting auth headers and routing through Squid.
*/
/** Validate that a request ID is safe (alphanumeric, dashes, dots, max 128 chars). */
function isValidRequestId(id) {
return typeof id === 'string' && id.length <= 128 && /^[\w\-\.]+$/.test(id);
}
function proxyRequest(req, res, targetHost, injectHeaders, provider, basePath = '') {
const clientRequestId = req.headers['x-request-id'];
const requestId = isValidRequestId(clientRequestId) ? clientRequestId : generateRequestId();
const startTime = Date.now();
// Propagate request ID back to the client and forward to upstream
res.setHeader('X-Request-ID', requestId);
// Track active requests
metrics.gaugeInc('active_requests', { provider });
logRequest('info', 'request_start', {
request_id: requestId,
provider,
method: req.method,
path: sanitizeForLog(req.url),
upstream_host: targetHost,
});
// Validate that req.url is a relative path (prevent open-redirect / SSRF)
if (!req.url || !req.url.startsWith('/')) {
const duration = Date.now() - startTime;
metrics.gaugeDec('active_requests', { provider });
metrics.increment('requests_total', { provider, method: req.method, status_class: '4xx' });
logRequest('warn', 'request_complete', {
request_id: requestId,
provider,
method: req.method,
path: sanitizeForLog(req.url),
status: 400,
duration_ms: duration,
upstream_host: targetHost,
});
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Bad Request', message: 'URL must be a relative path' }));
return;
}
// Build target URL
const upstreamPath = buildUpstreamPath(req.url, targetHost, basePath);
// Handle client-side errors (e.g. aborted connections)
req.on('error', (err) => {
if (errored) return; // Prevent double handling
errored = true;
const duration = Date.now() - startTime;
metrics.gaugeDec('active_requests', { provider });
metrics.increment('requests_errors_total', { provider });
logRequest('error', 'request_error', {
request_id: requestId,
provider,
method: req.method,
path: sanitizeForLog(req.url),
duration_ms: duration,
error: sanitizeForLog(err.message),
upstream_host: targetHost,
});
if (!res.headersSent) {
res.writeHead(400, { 'Content-Type': 'application/json' });
}
res.end(JSON.stringify({ error: 'Client error', message: err.message }));
});
// Read the request body with size limit
const chunks = [];
let totalBytes = 0;
let rejected = false;
let errored = false;
req.on('data', chunk => {
if (rejected || errored) return;
totalBytes += chunk.length;
if (totalBytes > MAX_BODY_SIZE) {
rejected = true;
const duration = Date.now() - startTime;
metrics.gaugeDec('active_requests', { provider });
metrics.increment('requests_total', { provider, method: req.method, status_class: '4xx' });
logRequest('warn', 'request_complete', {
request_id: requestId,
provider,
method: req.method,
path: sanitizeForLog(req.url),
status: 413,
duration_ms: duration,
request_bytes: totalBytes,
upstream_host: targetHost,
});
if (!res.headersSent) {
res.writeHead(413, { 'Content-Type': 'application/json' });
}
res.end(JSON.stringify({ error: 'Payload Too Large', message: 'Request body exceeds 10 MB limit' }));
return;
}
chunks.push(chunk);
});
req.on('end', () => {
if (rejected || errored) return;
const body = Buffer.concat(chunks);
const requestBytes = body.length;
metrics.increment('request_bytes_total', { provider }, requestBytes);
// Copy incoming headers, stripping sensitive/proxy headers, then inject auth
const headers = {};
for (const [name, value] of Object.entries(req.headers)) {
if (!shouldStripHeader(name)) {
headers[name] = value;
}
}
// Ensure X-Request-ID is forwarded to upstream
headers['x-request-id'] = requestId;
Object.assign(headers, injectHeaders);
// Log auth header injection for debugging credential-isolation issues
const injectedKey = injectHeaders['x-api-key'] || injectHeaders['authorization'];
if (injectedKey) {
const keyPreview = injectedKey.length > 8
? `${injectedKey.substring(0, 8)}...${injectedKey.substring(injectedKey.length - 4)}`
: '(short)';
logRequest('debug', 'auth_inject', {
request_id: requestId,
provider,
key_length: injectedKey.length,
key_preview: keyPreview,
has_anthropic_version: !!headers['anthropic-version'],
});
}
const options = {
hostname: targetHost,
port: 443,
path: upstreamPath,
method: req.method,
headers,
agent: proxyAgent, // Route through Squid
};
const proxyReq = https.request(options, (proxyRes) => {
let responseBytes = 0;
proxyRes.on('data', (chunk) => {
responseBytes += chunk.length;
});
// Handle response stream errors
proxyRes.on('error', (err) => {
const duration = Date.now() - startTime;
metrics.gaugeDec('active_requests', { provider });
metrics.increment('requests_errors_total', { provider });
logRequest('error', 'request_error', {
request_id: requestId,
provider,
method: req.method,
path: sanitizeForLog(req.url),
duration_ms: duration,
error: sanitizeForLog(err.message),
upstream_host: targetHost,
});
if (!res.headersSent) {
res.writeHead(502, { 'Content-Type': 'application/json' });
}
res.end(JSON.stringify({ error: 'Response stream error', message: err.message }));
});
proxyRes.on('end', () => {
const duration = Date.now() - startTime;
const sc = metrics.statusClass(proxyRes.statusCode);
metrics.gaugeDec('active_requests', { provider });
metrics.increment('requests_total', { provider, method: req.method, status_class: sc });
metrics.increment('response_bytes_total', { provider }, responseBytes);
metrics.observe('request_duration_ms', duration, { provider });
logRequest('info', 'request_complete', {
request_id: requestId,
provider,
method: req.method,
path: sanitizeForLog(req.url),
status: proxyRes.statusCode,
duration_ms: duration,
request_bytes: requestBytes,
response_bytes: responseBytes,
upstream_host: targetHost,
});
});
// Copy response headers and add X-Request-ID
const resHeaders = { ...proxyRes.headers, 'x-request-id': requestId };
// Log upstream auth failures prominently for debugging
if (proxyRes.statusCode === 401 || proxyRes.statusCode === 403) {
logRequest('warn', 'upstream_auth_error', {
request_id: requestId,
provider,
status: proxyRes.statusCode,
upstream_host: targetHost,
path: sanitizeForLog(req.url),
message: `Upstream returned ${proxyRes.statusCode} — check that the API key is valid and has not expired`,
});
}
res.writeHead(proxyRes.statusCode, resHeaders);
proxyRes.pipe(res);
// Attach token usage tracking (non-blocking, listens on same data/end events)
trackTokenUsage(proxyRes, {
requestId,
provider,
path: sanitizeForLog(req.url),
startTime,
metrics,
});
});
proxyReq.on('error', (err) => {
const duration = Date.now() - startTime;
metrics.gaugeDec('active_requests', { provider });
metrics.increment('requests_errors_total', { provider });
metrics.increment('requests_total', { provider, method: req.method, status_class: '5xx' });
metrics.observe('request_duration_ms', duration, { provider });
logRequest('error', 'request_error', {
request_id: requestId,
provider,
method: req.method,
path: sanitizeForLog(req.url),
duration_ms: duration,
error: sanitizeForLog(err.message),
upstream_host: targetHost,
});
if (!res.headersSent) {
res.writeHead(502, { 'Content-Type': 'application/json' });
}
res.end(JSON.stringify({ error: 'Proxy error', message: err.message }));
});
if (body.length > 0) {
proxyReq.write(body);
}
proxyReq.end();
});
}
/**
* Handle a WebSocket upgrade request by tunnelling through the Squid proxy.
*
* Flow:
* client --[HTTP Upgrade]--> proxy --[CONNECT]--> Squid:3128 --[TLS]--> upstream:443
*
* Steps:
* 1. Validate the request (WebSocket upgrade only, relative URL)
* 2. Apply rate limiting (counts as one request, zero body bytes)
* 3. Open a CONNECT tunnel to targetHost:443 through Squid
* 4. TLS-handshake the tunnel
* 5. Replay the HTTP Upgrade request with auth headers injected
* 6. Bidirectionally pipe the raw TCP sockets
*
* No additional npm dependencies are required — only Node.js built-ins.
*
* @param {http.IncomingMessage} req - The incoming HTTP Upgrade request
* @param {import('net').Socket} socket - Raw TCP socket to the WebSocket client
* @param {Buffer} head - Any bytes already buffered after the upgrade headers
* @param {string} targetHost - Upstream hostname (e.g. 'api.openai.com')
* @param {Object} injectHeaders - Auth headers to inject (e.g. { Authorization: 'Bearer …' })
* @param {string} provider - Provider name for logging and metrics
* @param {string} [basePath=''] - Optional base-path prefix for the upstream URL
*/
function proxyWebSocket(req, socket, head, targetHost, injectHeaders, provider, basePath = '') {
const startTime = Date.now();
const clientRequestId = req.headers['x-request-id'];
const requestId = isValidRequestId(clientRequestId) ? clientRequestId : generateRequestId();
// ── Validate: only forward WebSocket upgrades ──────────────────────────
const upgradeType = (req.headers['upgrade'] || '').toLowerCase();
if (upgradeType !== 'websocket') {
logRequest('warn', 'websocket_upgrade_rejected', {
request_id: requestId,
provider,
path: sanitizeForLog(req.url),
reason: 'unsupported upgrade type',
upgrade: sanitizeForLog(req.headers['upgrade'] || ''),
});
socket.write('HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n');
socket.destroy();
return;
}
// ── Validate: relative path only (prevent SSRF) ────────────────────────
if (!req.url || !req.url.startsWith('/')) {
logRequest('warn', 'websocket_upgrade_rejected', {
request_id: requestId,
provider,
path: sanitizeForLog(req.url),
reason: 'URL must be a relative path',
});
socket.write('HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n');
socket.destroy();
return;
}
const upstreamPath = buildUpstreamPath(req.url, targetHost, basePath);
// ── Rate limit (counts as one request, frames are not tracked) ──────────
const rateCheck = limiter.check(provider, 0);
if (!rateCheck.allowed) {
metrics.increment('rate_limit_rejected_total', { provider, limit_type: rateCheck.limitType });
logRequest('warn', 'rate_limited', {
request_id: requestId,
provider,
limit_type: rateCheck.limitType,
limit: rateCheck.limit,
retry_after: rateCheck.retryAfter,
});
socket.write(
`HTTP/1.1 429 Too Many Requests\r\nRetry-After: ${rateCheck.retryAfter}\r\nConnection: close\r\n\r\n`
);
socket.destroy();
return;
}
logRequest('info', 'websocket_upgrade_start', {
request_id: requestId,
provider,
path: sanitizeForLog(req.url),
upstream_host: targetHost,
});
metrics.gaugeInc('active_requests', { provider });
// finalize() must be called exactly once when the WebSocket session ends.
let finalized = false;
function finalize(isError, description) {
if (finalized) return;
finalized = true;
const duration = Date.now() - startTime;
metrics.gaugeDec('active_requests', { provider });
if (isError) {
metrics.increment('requests_errors_total', { provider });
logRequest('error', 'websocket_upgrade_failed', {
request_id: requestId,
provider,
path: sanitizeForLog(req.url),
duration_ms: duration,
error: sanitizeForLog(String(description || 'unknown error')),
});
} else {
metrics.increment('requests_total', { provider, method: 'GET', status_class: '1xx' });
metrics.observe('request_duration_ms', duration, { provider });
logRequest('info', 'websocket_upgrade_complete', {
request_id: requestId,
provider,
path: sanitizeForLog(req.url),
duration_ms: duration,
});
}
}
// abort(): called before the socket pipe is established (pre-TLS errors).
// Sends a 502 to the client and finalizes with an error.
function abort(reason, ...extra) {
finalize(true, reason);
if (!socket.destroyed && socket.writable) {
socket.write('HTTP/1.1 502 Bad Gateway\r\nConnection: close\r\n\r\n');
}
socket.destroy();
for (const s of extra) {
if (s && !s.destroyed) s.destroy();
}
}
// ── Require Squid proxy ────────────────────────────────────────────────
if (!HTTPS_PROXY) {
abort('No Squid proxy configured (HTTPS_PROXY not set)');
return;
}
let proxyUrl;
try {
proxyUrl = new URL(HTTPS_PROXY);
} catch (err) {
abort(`Invalid proxy URL: ${err.message}`);
return;
}
const proxyHost = proxyUrl.hostname;
const proxyPort = parseInt(proxyUrl.port, 10) || 3128;
// ── Step 1: CONNECT tunnel through Squid to targetHost:443 ────────────
const connectReq = http.request({
host: proxyHost,
port: proxyPort,
method: 'CONNECT',
path: `${targetHost}:443`,
headers: { 'Host': `${targetHost}:443` },
});
connectReq.once('error', (err) => abort(`CONNECT error: ${err.message}`));
connectReq.once('connect', (connectRes, tunnel) => {
if (connectRes.statusCode !== 200) {
abort(`CONNECT failed: HTTP ${connectRes.statusCode}`, tunnel);
return;
}
// ── Step 2: TLS-upgrade the raw tunnel ──────────────────────────────
const tlsSocket = tls.connect({ socket: tunnel, servername: targetHost, rejectUnauthorized: true });
// Pre-TLS error handler: removed once TLS is established.
const onTlsError = (err) => abort(`TLS handshake error: ${err.message}`, tunnel);
tlsSocket.once('error', onTlsError);
tlsSocket.once('secureConnect', () => {
// TLS connected — swap to post-connection teardown error handlers.
tlsSocket.removeListener('error', onTlsError);
// ── Step 3: Replay the HTTP Upgrade request with auth injected ────
const forwardHeaders = {};
for (const [name, value] of Object.entries(req.headers)) {
if (!shouldStripHeader(name)) {
forwardHeaders[name] = value;
}
}
Object.assign(forwardHeaders, injectHeaders);
forwardHeaders['host'] = targetHost; // Fix Host header for upstream
let upgradeReqStr = `GET ${upstreamPath} HTTP/1.1\r\n`;
for (const [name, value] of Object.entries(forwardHeaders)) {
upgradeReqStr += `${name}: ${value}\r\n`;
}
upgradeReqStr += '\r\n';
tlsSocket.write(upgradeReqStr);
// Forward any bytes already buffered before the pipe
if (head && head.length > 0) {
tlsSocket.write(head);
}
// ── Step 4: Bidirectional raw socket relay ─────────────────────
tlsSocket.pipe(socket);
socket.pipe(tlsSocket);
// Attach WebSocket token usage tracking (non-blocking, sniffs upstream frames)
trackWebSocketTokenUsage(tlsSocket, {
requestId,
provider,
path: sanitizeForLog(req.url),
startTime,
metrics,
});
// Finalize once when either side closes; destroy the other side.
socket.once('close', () => { finalize(false); tlsSocket.destroy(); });
tlsSocket.once('close', () => { finalize(false); socket.destroy(); });
// Suppress unhandled-error crashes; destroy triggers the close handler.
socket.on('error', () => socket.destroy());
tlsSocket.on('error', () => tlsSocket.destroy());
});
});
connectReq.end();
}
/**
* Build the enhanced health response (superset of original format).
*/
function healthResponse() {
return {
status: 'healthy',
service: 'awf-api-proxy',
squid_proxy: HTTPS_PROXY || 'not configured',
providers: {
openai: !!OPENAI_API_KEY,
anthropic: !!ANTHROPIC_API_KEY,
gemini: !!GEMINI_API_KEY,
copilot: !!COPILOT_GITHUB_TOKEN,
},
metrics_summary: metrics.getSummary(),
rate_limits: limiter.getAllStatus(),
};
}
/**
* Handle management endpoints on port 10000 (/health, /metrics).
* Returns true if the request was handled, false otherwise.
*/
function handleManagementEndpoint(req, res) {
if (req.method === 'GET' && req.url === '/health') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(healthResponse()));
return true;
}
if (req.method === 'GET' && req.url === '/metrics') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(metrics.getMetrics()));
return true;
}
return false;
}
// Only start the server if this file is run directly (not imported for testing)
if (require.main === module) {
// Health port is always 10000 — this is what Docker healthcheck hits
const HEALTH_PORT = 10000;
// OpenAI API proxy (port 10000)
if (OPENAI_API_KEY) {
const server = http.createServer((req, res) => {
if (handleManagementEndpoint(req, res)) return;
const contentLength = parseInt(req.headers['content-length'], 10) || 0;
if (checkRateLimit(req, res, 'openai', contentLength)) return;
proxyRequest(req, res, OPENAI_API_TARGET, {
'Authorization': `Bearer ${OPENAI_API_KEY}`,
}, 'openai', OPENAI_API_BASE_PATH);
});
server.on('upgrade', (req, socket, head) => {
proxyWebSocket(req, socket, head, OPENAI_API_TARGET, {
'Authorization': `Bearer ${OPENAI_API_KEY}`,
}, 'openai', OPENAI_API_BASE_PATH);
});
server.listen(HEALTH_PORT, '0.0.0.0', () => {
logRequest('info', 'server_start', { message: `OpenAI proxy listening on port ${HEALTH_PORT}`, target: OPENAI_API_TARGET });
});
} else {
// No OpenAI key — still need a health endpoint on port 10000 for Docker healthcheck
const server = http.createServer((req, res) => {
if (handleManagementEndpoint(req, res)) return;
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'OpenAI proxy not configured (no OPENAI_API_KEY)' }));
});
server.on('upgrade', (req, socket) => {
socket.write('HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n\r\n');
socket.destroy();
});
server.listen(HEALTH_PORT, '0.0.0.0', () => {
logRequest('info', 'server_start', { message: `Health endpoint listening on port ${HEALTH_PORT} (OpenAI not configured)` });
});
}
// Anthropic API proxy (port 10001)
if (ANTHROPIC_API_KEY) {
const server = http.createServer((req, res) => {
if (req.url === '/health' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'healthy', service: 'anthropic-proxy' }));
return;
}
const contentLength = parseInt(req.headers['content-length'], 10) || 0;
if (checkRateLimit(req, res, 'anthropic', contentLength)) return;
// Only set anthropic-version as default; preserve agent-provided version
const anthropicHeaders = { 'x-api-key': ANTHROPIC_API_KEY };
if (!req.headers['anthropic-version']) {
anthropicHeaders['anthropic-version'] = '2023-06-01';
}
proxyRequest(req, res, ANTHROPIC_API_TARGET, anthropicHeaders, 'anthropic', ANTHROPIC_API_BASE_PATH);
});
server.on('upgrade', (req, socket, head) => {
const anthropicHeaders = { 'x-api-key': ANTHROPIC_API_KEY };
if (!req.headers['anthropic-version']) {
anthropicHeaders['anthropic-version'] = '2023-06-01';
}
proxyWebSocket(req, socket, head, ANTHROPIC_API_TARGET, anthropicHeaders, 'anthropic', ANTHROPIC_API_BASE_PATH);
});
server.listen(10001, '0.0.0.0', () => {
logRequest('info', 'server_start', { message: 'Anthropic proxy listening on port 10001', target: ANTHROPIC_API_TARGET });
});
}
// GitHub Copilot API proxy (port 10002)
if (COPILOT_GITHUB_TOKEN) {
const copilotServer = http.createServer((req, res) => {
// Health check endpoint
if (req.url === '/health' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'healthy', service: 'copilot-proxy' }));
return;
}
const contentLength = parseInt(req.headers['content-length'], 10) || 0;
if (checkRateLimit(req, res, 'copilot', contentLength)) return;
proxyRequest(req, res, COPILOT_API_TARGET, {
'Authorization': `Bearer ${COPILOT_GITHUB_TOKEN}`,
}, 'copilot');
});
copilotServer.on('upgrade', (req, socket, head) => {
proxyWebSocket(req, socket, head, COPILOT_API_TARGET, {
'Authorization': `Bearer ${COPILOT_GITHUB_TOKEN}`,
}, 'copilot');
});
copilotServer.listen(10002, '0.0.0.0', () => {
logRequest('info', 'server_start', { message: 'GitHub Copilot proxy listening on port 10002' });
});
}
// Google Gemini API proxy (port 10003)
if (GEMINI_API_KEY) {
const geminiServer = http.createServer((req, res) => {
if (req.url === '/health' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'healthy', service: 'gemini-proxy' }));
return;
}
const contentLength = parseInt(req.headers['content-length'], 10) || 0;
if (checkRateLimit(req, res, 'gemini', contentLength)) return;
proxyRequest(req, res, GEMINI_API_TARGET, {
'x-goog-api-key': GEMINI_API_KEY,
}, 'gemini', GEMINI_API_BASE_PATH);
});
geminiServer.on('upgrade', (req, socket, head) => {
proxyWebSocket(req, socket, head, GEMINI_API_TARGET, {
'x-goog-api-key': GEMINI_API_KEY,
}, 'gemini', GEMINI_API_BASE_PATH);
});
geminiServer.listen(10003, '0.0.0.0', () => {
logRequest('info', 'server_start', { message: 'Google Gemini proxy listening on port 10003', target: GEMINI_API_TARGET });
});
} else {
// No Gemini key — listen on port 10003 and return 503 so the Gemini CLI
// gets an actionable error instead of a silent connection-refused.
const geminiServer = http.createServer((req, res) => {
if (req.url === '/health' && req.method === 'GET') {
res.writeHead(503, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'not_configured', service: 'gemini-proxy', error: 'GEMINI_API_KEY not configured in api-proxy sidecar' }));
return;
}
res.writeHead(503, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: 'Gemini proxy not configured (no GEMINI_API_KEY). Set GEMINI_API_KEY in the AWF runner environment to enable credential isolation.' }));
});
geminiServer.on('upgrade', (req, socket) => {
socket.write('HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n\r\n');
socket.destroy();
});
geminiServer.listen(10003, '0.0.0.0', () => {
logRequest('info', 'server_start', { message: 'Gemini endpoint listening on port 10003 (Gemini not configured — returning 503)' });
});
}
// OpenCode API proxy (port 10004) — routes to Anthropic (default BYOK provider)
// OpenCode gets a separate port from Claude (10001) for per-engine rate limiting,
// metrics isolation, and future provider routing (OpenCode is BYOK and may route
// to different providers in the future based on model prefix).
if (ANTHROPIC_API_KEY) {
const opencodeServer = http.createServer((req, res) => {
if (req.url === '/health' && req.method === 'GET') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: 'healthy', service: 'opencode-proxy' }));
return;
}
const logMethod = sanitizeForLog(req.method);
const logUrl = sanitizeForLog(req.url);
logRequest('info', 'opencode_proxy_request', {
message: '[OpenCode Proxy] Incoming request',
method: logMethod,
url: logUrl,
});
logRequest('info', 'opencode_proxy_header_injection', {
message: '[OpenCode Proxy] Injecting x-api-key header with ANTHROPIC_API_KEY',
});
const anthropicHeaders = { 'x-api-key': ANTHROPIC_API_KEY };
if (!req.headers['anthropic-version']) {
anthropicHeaders['anthropic-version'] = '2023-06-01';
}
proxyRequest(req, res, ANTHROPIC_API_TARGET, anthropicHeaders);
});
opencodeServer.on('upgrade', (req, socket, head) => {
const anthropicHeaders = { 'x-api-key': ANTHROPIC_API_KEY };
if (!req.headers['anthropic-version']) {
anthropicHeaders['anthropic-version'] = '2023-06-01';
}
proxyWebSocket(req, socket, head, ANTHROPIC_API_TARGET, anthropicHeaders, 'opencode');
});
opencodeServer.listen(10004, '0.0.0.0', () => {
console.log(`[API Proxy] OpenCode proxy listening on port 10004 (-> Anthropic at ${ANTHROPIC_API_TARGET})`);
});
}
// Graceful shutdown
process.on('SIGTERM', async () => {
logRequest('info', 'shutdown', { message: 'Received SIGTERM, shutting down gracefully' });
await closeLogStream();
process.exit(0);
});
process.on('SIGINT', async () => {
logRequest('info', 'shutdown', { message: 'Received SIGINT, shutting down gracefully' });
await closeLogStream();
process.exit(0);
});
}
// Export for testing
module.exports = { normalizeApiTarget, deriveCopilotApiTarget, normalizeBasePath, buildUpstreamPath, proxyWebSocket };