From 8decdfb515075b772970de7c85b34c31baf93754 Mon Sep 17 00:00:00 2001 From: kaitranntt Date: Fri, 19 Dec 2025 01:16:10 -0500 Subject: [PATCH] feat(web-server): add proxy configuration API routes - GET /api/proxy returns current proxy config - PUT /api/proxy updates proxy settings in config.yaml - POST /api/proxy/test tests remote connection - register routes in web server index --- src/web-server/index.ts | 4 ++ src/web-server/routes/proxy-routes.ts | 93 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 src/web-server/routes/proxy-routes.ts diff --git a/src/web-server/index.ts b/src/web-server/index.ts index e9c4ba3a..4464027e 100644 --- a/src/web-server/index.ts +++ b/src/web-server/index.ts @@ -51,6 +51,10 @@ export async function startServer(options: ServerOptions): Promise { + try { + const config = await loadOrCreateUnifiedConfig(); + res.json(config.proxy || DEFAULT_PROXY_CONFIG); + } catch (error) { + console.error('[proxy-routes] Failed to load proxy config:', error); + res.status(500).json({ error: 'Failed to load proxy config' }); + } +}); + +/** + * PUT /api/proxy - Update proxy configuration + */ +router.put('/', async (req: Request, res: Response) => { + try { + const config = await loadOrCreateUnifiedConfig(); + const updates = req.body as Partial; + + // Deep merge with defaults and current config + config.proxy = { + remote: { + ...DEFAULT_PROXY_CONFIG.remote, + ...config.proxy?.remote, + ...updates.remote, + }, + fallback: { + ...DEFAULT_PROXY_CONFIG.fallback, + ...config.proxy?.fallback, + ...updates.fallback, + }, + local: { + ...DEFAULT_PROXY_CONFIG.local, + ...config.proxy?.local, + ...updates.local, + }, + }; + + await saveUnifiedConfig(config); + res.json(config.proxy); + } catch (error) { + console.error('[proxy-routes] Failed to save proxy config:', error); + res.status(500).json({ error: 'Failed to save proxy config' }); + } +}); + +/** + * POST /api/proxy/test - Test remote proxy connection + */ +router.post('/test', async (req: Request, res: Response) => { + try { + const { host, port, protocol, authToken, allowSelfSigned } = req.body; + + if (!host || !port) { + res.status(400).json({ error: 'Host and port are required' }); + return; + } + + const status = await testConnection({ + host, + port: typeof port === 'number' ? port : parseInt(port, 10), + protocol: protocol || 'http', + authToken, + allowSelfSigned: allowSelfSigned || false, + timeout: 5000, + }); + + res.json(status); + } catch (error) { + console.error('[proxy-routes] Failed to test connection:', error); + res.status(500).json({ error: 'Failed to test connection' }); + } +}); + +export default router;