|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/** |
| 4 | + * AWF API Proxy Sidecar |
| 5 | + * |
| 6 | + * Node.js-based proxy that: |
| 7 | + * 1. Keeps LLM API credentials isolated from agent container |
| 8 | + * 2. Routes all traffic through Squid via HTTP_PROXY/HTTPS_PROXY |
| 9 | + * 3. Injects authentication headers (Authorization, x-api-key) |
| 10 | + * 4. Respects domain whitelisting enforced by Squid |
| 11 | + */ |
| 12 | + |
| 13 | +const express = require('express'); |
| 14 | +const { createProxyMiddleware } = require('http-proxy-middleware'); |
| 15 | + |
| 16 | +// Read API keys from environment (set by docker-compose) |
| 17 | +const OPENAI_API_KEY = process.env.OPENAI_API_KEY; |
| 18 | +const ANTHROPIC_API_KEY = process.env.ANTHROPIC_API_KEY; |
| 19 | + |
| 20 | +// Squid proxy configuration (set via HTTP_PROXY/HTTPS_PROXY in docker-compose) |
| 21 | +const HTTP_PROXY = process.env.HTTP_PROXY; |
| 22 | +const HTTPS_PROXY = process.env.HTTPS_PROXY; |
| 23 | + |
| 24 | +console.log('[API Proxy] Starting AWF API proxy sidecar...'); |
| 25 | +console.log(`[API Proxy] HTTP_PROXY: ${HTTP_PROXY}`); |
| 26 | +console.log(`[API Proxy] HTTPS_PROXY: ${HTTPS_PROXY}`); |
| 27 | +if (OPENAI_API_KEY) { |
| 28 | + console.log('[API Proxy] OpenAI API key configured'); |
| 29 | +} |
| 30 | +if (ANTHROPIC_API_KEY) { |
| 31 | + console.log('[API Proxy] Anthropic API key configured'); |
| 32 | +} |
| 33 | + |
| 34 | +// Create Express app |
| 35 | +const app = express(); |
| 36 | + |
| 37 | +// Health check endpoint |
| 38 | +app.get('/health', (req, res) => { |
| 39 | + res.status(200).json({ |
| 40 | + status: 'healthy', |
| 41 | + service: 'awf-api-proxy', |
| 42 | + squid_proxy: HTTP_PROXY || 'not configured', |
| 43 | + providers: { |
| 44 | + openai: !!OPENAI_API_KEY, |
| 45 | + anthropic: !!ANTHROPIC_API_KEY |
| 46 | + } |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +// OpenAI API proxy (port 10000) |
| 51 | +if (OPENAI_API_KEY) { |
| 52 | + app.use(createProxyMiddleware({ |
| 53 | + target: 'https://api.openai.com', |
| 54 | + changeOrigin: true, |
| 55 | + secure: true, |
| 56 | + onProxyReq: (proxyReq, req, res) => { |
| 57 | + // Inject Authorization header |
| 58 | + proxyReq.setHeader('Authorization', `Bearer ${OPENAI_API_KEY}`); |
| 59 | + console.log(`[OpenAI Proxy] ${req.method} ${req.url}`); |
| 60 | + }, |
| 61 | + onError: (err, req, res) => { |
| 62 | + console.error(`[OpenAI Proxy] Error: ${err.message}`); |
| 63 | + res.status(502).json({ error: 'Proxy error', message: err.message }); |
| 64 | + } |
| 65 | + })); |
| 66 | + |
| 67 | + app.listen(10000, '0.0.0.0', () => { |
| 68 | + console.log('[API Proxy] OpenAI proxy listening on port 10000'); |
| 69 | + console.log('[API Proxy] Routing through Squid to api.openai.com'); |
| 70 | + }); |
| 71 | +} |
| 72 | + |
| 73 | +// Anthropic API proxy (port 10001) |
| 74 | +if (ANTHROPIC_API_KEY) { |
| 75 | + const anthropicApp = express(); |
| 76 | + |
| 77 | + anthropicApp.get('/health', (req, res) => { |
| 78 | + res.status(200).json({ status: 'healthy', service: 'anthropic-proxy' }); |
| 79 | + }); |
| 80 | + |
| 81 | + anthropicApp.use(createProxyMiddleware({ |
| 82 | + target: 'https://api.anthropic.com', |
| 83 | + changeOrigin: true, |
| 84 | + secure: true, |
| 85 | + onProxyReq: (proxyReq, req, res) => { |
| 86 | + // Inject Anthropic authentication headers |
| 87 | + proxyReq.setHeader('x-api-key', ANTHROPIC_API_KEY); |
| 88 | + proxyReq.setHeader('anthropic-version', '2023-06-01'); |
| 89 | + console.log(`[Anthropic Proxy] ${req.method} ${req.url}`); |
| 90 | + }, |
| 91 | + onError: (err, req, res) => { |
| 92 | + console.error(`[Anthropic Proxy] Error: ${err.message}`); |
| 93 | + res.status(502).json({ error: 'Proxy error', message: err.message }); |
| 94 | + } |
| 95 | + })); |
| 96 | + |
| 97 | + anthropicApp.listen(10001, '0.0.0.0', () => { |
| 98 | + console.log('[API Proxy] Anthropic proxy listening on port 10001'); |
| 99 | + console.log('[API Proxy] Routing through Squid to api.anthropic.com'); |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +// Graceful shutdown |
| 104 | +process.on('SIGTERM', () => { |
| 105 | + console.log('[API Proxy] Received SIGTERM, shutting down gracefully...'); |
| 106 | + process.exit(0); |
| 107 | +}); |
| 108 | + |
| 109 | +process.on('SIGINT', () => { |
| 110 | + console.log('[API Proxy] Received SIGINT, shutting down gracefully...'); |
| 111 | + process.exit(0); |
| 112 | +}); |
0 commit comments