(flags: CliFlags)
| 30 | }; |
| 31 | |
| 32 | async function startProxy(flags: CliFlags): Promise<ProxyStartup> { |
| 33 | const settings = resolveClientSettings({ |
| 34 | session: 'default', |
| 35 | command: 'proxy', |
| 36 | positionals: [], |
| 37 | flags: { |
| 38 | stateDir: flags.stateDir, |
| 39 | daemonBaseUrl: '', |
| 40 | daemonTransport: 'http', |
| 41 | daemonServerMode: 'http', |
| 42 | }, |
| 43 | }); |
| 44 | const daemon = await ensureDaemon(settings); |
| 45 | const upstreamBaseUrl = resolveLocalDaemonBaseUrl(daemon.info.httpPort); |
| 46 | const token = resolveProxyClientToken(flags); |
| 47 | const server = createDaemonProxyServer({ |
| 48 | upstreamBaseUrl, |
| 49 | upstreamToken: daemon.info.token, |
| 50 | clientToken: token, |
| 51 | }); |
| 52 | const host = flags.proxyHost?.trim() || '127.0.0.1'; |
| 53 | const port = flags.proxyPort ?? 0; |
| 54 | await listen(server, host, port); |
| 55 | const address = server.address(); |
| 56 | if (!address || typeof address === 'string') { |
| 57 | throw new AppError('COMMAND_FAILED', 'Proxy did not bind to a TCP address.'); |
| 58 | } |
| 59 | const proxyBaseUrl = `http://${formatHostForUrl(address.address)}:${address.port}`; |
| 60 | return { |
| 61 | proxyBaseUrl, |
| 62 | agentDeviceBaseUrl: buildDaemonHttpBaseUrl(proxyBaseUrl), |
| 63 | token, |
| 64 | upstreamBaseUrl, |
| 65 | stateDir: settings.paths.baseDir, |
| 66 | }; |
| 67 | } |
| 68 | |
| 69 | function resolveLocalDaemonBaseUrl(httpPort: number | undefined): string { |
| 70 | if (!httpPort) { |
no test coverage detected