fix(cliproxy): correct remote proxy URL building for default port

Two fixes:
1. Move cliproxy_server section adjacent to cliproxy in config.yaml
   for better organization and discoverability.

2. Fix URL building bug where port 8317 was being omitted from HTTP
   URLs because it was incorrectly treated as "default port".

   Before: http://192.168.2.84/v1/models (hits port 80)
   After:  http://192.168.2.84:8317/v1/models (hits correct port)

   Now only standard web ports (80/443) are omitted from URLs.
   CLIProxyAPI default port 8317 is always included.

This resolves "Connection refused" errors when testing remote proxy
connections that use the default CLIProxyAPI port.
This commit is contained in:
kaitranntt
2025-12-20 20:07:42 -05:00
parent 1e6883b4a6
commit 294d8d55e5
2 changed files with 45 additions and 30 deletions
+24 -9
View File
@@ -52,16 +52,30 @@ export interface RemoteProxyClientConfig {
/** Default timeout for remote proxy requests (aggressive for CLI UX) */
const DEFAULT_TIMEOUT_MS = 2000;
/** Default CLIProxyAPI port */
const DEFAULT_CLIPROXY_PORT = 8317;
/**
* Get default port for protocol
* HTTP defaults to 8317 (CLIProxyAPI default), HTTPS to 443 (standard SSL)
* Get standard web port for protocol (for URL display omission)
* These are the ports that browsers/HTTP clients use by default.
* HTTP: 80, HTTPS: 443
*/
function getDefaultPort(protocol: 'http' | 'https'): number {
return protocol === 'https' ? 443 : 8317;
function getStandardWebPort(protocol: 'http' | 'https'): number {
return protocol === 'https' ? 443 : 80;
}
/**
* Build URL for remote proxy, intelligently omitting default ports
* Get effective port for CLIProxyAPI connection
* If port is provided, use it. Otherwise use CLIProxyAPI default (8317).
*/
function getEffectivePort(port: number | undefined): number {
return port ?? DEFAULT_CLIPROXY_PORT;
}
/**
* Build URL for remote proxy
* Only omits port from URL if it matches standard web ports (80/443),
* otherwise always includes the port for clarity.
*/
function buildProxyUrl(
host: string,
@@ -69,11 +83,12 @@ function buildProxyUrl(
protocol: 'http' | 'https',
path: string
): string {
const defaultPort = getDefaultPort(protocol);
const effectivePort = port ?? defaultPort;
const effectivePort = getEffectivePort(port);
const standardWebPort = getStandardWebPort(protocol);
// Omit port from URL if it matches the default for the protocol
if (effectivePort === defaultPort) {
// Only omit port from URL if it matches the standard web port for the protocol
// e.g., HTTP on port 80 or HTTPS on port 443
if (effectivePort === standardWebPort) {
return `${protocol}://${host}${path}`;
}
return `${protocol}://${host}:${effectivePort}${path}`;
+21 -21
View File
@@ -309,6 +309,27 @@ function generateYamlWithComments(config: UnifiedConfig): string {
);
lines.push('');
// CLIProxy Server section (remote proxy configuration) - placed right after cliproxy
if (config.cliproxy_server) {
lines.push('# ----------------------------------------------------------------------------');
lines.push('# CLIProxy Server: Remote proxy connection settings');
lines.push('# Configure via Dashboard (`ccs config`) > Proxy tab.');
lines.push('#');
lines.push('# remote: Connect to a remote CLIProxyAPI instance');
lines.push('# fallback: Use local proxy if remote is unreachable');
lines.push('# local: Local proxy settings (port, auto-start)');
lines.push('# ----------------------------------------------------------------------------');
lines.push(
yaml
.dump(
{ cliproxy_server: config.cliproxy_server },
{ indent: 2, lineWidth: -1, quotingType: '"' }
)
.trim()
);
lines.push('');
}
// Preferences section
lines.push('# ----------------------------------------------------------------------------');
lines.push('# Preferences: User settings');
@@ -374,27 +395,6 @@ function generateYamlWithComments(config: UnifiedConfig): string {
lines.push('');
}
// CLIProxy Server section (remote proxy configuration)
if (config.cliproxy_server) {
lines.push('# ----------------------------------------------------------------------------');
lines.push('# CLIProxy Server: Remote proxy connection settings');
lines.push('# Configure via Dashboard (`ccs config`) > Proxy tab.');
lines.push('#');
lines.push('# remote: Connect to a remote CLIProxyAPI instance');
lines.push('# fallback: Use local proxy if remote is unreachable');
lines.push('# local: Local proxy settings (port, auto-start)');
lines.push('# ----------------------------------------------------------------------------');
lines.push(
yaml
.dump(
{ cliproxy_server: config.cliproxy_server },
{ indent: 2, lineWidth: -1, quotingType: '"' }
)
.trim()
);
lines.push('');
}
// Global env section
if (config.global_env) {
lines.push('# ----------------------------------------------------------------------------');