Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
"fastify-type-provider-zod": "4.0.2",
"http-proxy": "^1.18.1",
"http-proxy-middleware": "^3.0.5",
"https-proxy-agent": "^7.0.6",
"ink": "^6.5.1",
"open": "^10.2.0",
"ps-list": "^8.1.1",
Expand Down
17 changes: 13 additions & 4 deletions src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { configuration } from '@/configuration';
import chalk from 'chalk';
import { Credentials } from '@/persistence';
import { connectionState, isNetworkError } from '@/utils/serverConnectionErrors';
import { createProxyAgent } from '@/utils/proxy';

export class ApiClient {

Expand Down Expand Up @@ -70,7 +71,9 @@ export class ApiClient {
'Authorization': `Bearer ${this.credential.token}`,
'Content-Type': 'application/json'
},
timeout: 60000 // 1 minute timeout for very bad network connections
timeout: 60000, // 1 minute timeout for very bad network connections
httpAgent: createProxyAgent(),
httpsAgent: createProxyAgent()
}
)

Expand Down Expand Up @@ -190,7 +193,9 @@ export class ApiClient {
'Authorization': `Bearer ${this.credential.token}`,
'Content-Type': 'application/json'
},
timeout: 60000 // 1 minute timeout for very bad network connections
timeout: 60000, // 1 minute timeout for very bad network connections
httpAgent: createProxyAgent(),
httpsAgent: createProxyAgent()
}
);

Expand Down Expand Up @@ -301,7 +306,9 @@ export class ApiClient {
'Authorization': `Bearer ${this.credential.token}`,
'Content-Type': 'application/json'
},
timeout: 5000
timeout: 5000,
httpAgent: createProxyAgent(),
httpsAgent: createProxyAgent()
}
);

Expand Down Expand Up @@ -329,7 +336,9 @@ export class ApiClient {
'Authorization': `Bearer ${this.credential.token}`,
'Content-Type': 'application/json'
},
timeout: 5000
timeout: 5000,
httpAgent: createProxyAgent(),
httpsAgent: createProxyAgent()
}
);

Expand Down
4 changes: 3 additions & 1 deletion src/api/apiMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { registerCommonHandlers, SpawnSessionOptions, SpawnSessionResult } from
import { encodeBase64, decodeBase64, encrypt, decrypt } from './encryption';
import { backoff } from '@/utils/time';
import { RpcHandlerManager } from './rpc/RpcHandlerManager';
import { createProxyAgent } from '@/utils/proxy';

interface ServerToDaemonEvents {
update: (data: Update) => void;
Expand Down Expand Up @@ -227,7 +228,8 @@ export class ApiMachineClient {
path: '/v1/updates',
reconnection: true,
reconnectionDelay: 1000,
reconnectionDelayMax: 5000
reconnectionDelayMax: 5000,
agent: createProxyAgent() as any
});

this.socket.on('connect', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/api/apiSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { randomUUID } from 'node:crypto';
import { AsyncLock } from '@/utils/lock';
import { RpcHandlerManager } from './rpc/RpcHandlerManager';
import { registerCommonHandlers } from '../modules/common/registerCommonHandlers';
import { createProxyAgent } from '@/utils/proxy';

/**
* ACP (Agent Communication Protocol) message data types.
Expand Down Expand Up @@ -91,7 +92,8 @@ export class ApiSessionClient extends EventEmitter {
reconnectionDelayMax: 5000,
transports: ['websocket'],
withCredentials: true,
autoConnect: false
autoConnect: false,
agent: createProxyAgent() as any
});

//
Expand Down
6 changes: 5 additions & 1 deletion src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import { encodeBase64, encodeBase64Url, authChallenge } from './encryption';
import { configuration } from '@/configuration';
import { createProxyAgent } from '@/utils/proxy';

/**
* Note: This function is deprecated. Use readPrivateKey/writePrivateKey from persistence module instead.
Expand All @@ -18,11 +19,14 @@ export async function getOrCreateSecretKey(): Promise<Uint8Array> {
*/
export async function authGetToken(secret: Uint8Array): Promise<string> {
const { challenge, publicKey, signature } = authChallenge(secret);

const response = await axios.post(`${configuration.serverUrl}/v1/auth`, {
challenge: encodeBase64(challenge),
publicKey: encodeBase64(publicKey),
signature: encodeBase64(signature)
}, {
httpAgent: createProxyAgent(),
httpsAgent: createProxyAgent()
});

if (!response.data.success || !response.data.token) {
Expand Down
5 changes: 4 additions & 1 deletion src/api/pushNotifications.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios'
import { logger } from '@/ui/logger'
import { Expo, ExpoPushMessage } from 'expo-server-sdk'
import { createProxyAgent } from '@/utils/proxy'

export interface PushToken {
id: string
Expand Down Expand Up @@ -32,7 +33,9 @@ export class PushNotificationClient {
headers: {
'Authorization': `Bearer ${this.token}`,
'Content-Type': 'application/json'
}
},
httpAgent: createProxyAgent(),
httpsAgent: createProxyAgent()
}
)

Expand Down
4 changes: 4 additions & 0 deletions src/claude/runClaude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { startOfflineReconnection, connectionState } from '@/utils/serverConnect
import { claudeLocal } from '@/claude/claudeLocal';
import { createSessionScanner } from '@/claude/utils/sessionScanner';
import { Session } from './session';
import { setAgentType } from '@/utils/proxy';

/** JavaScript runtime to use for spawning Claude Code */
export type JsRuntime = 'node' | 'bun'
Expand All @@ -44,6 +45,9 @@ export interface StartOptions {
}

export async function runClaude(credentials: Credentials, options: StartOptions = {}): Promise<void> {
// Set agent type for proxy configuration
setAgentType('claude');

logger.debug(`[CLAUDE] ===== CLAUDE MODE STARTING =====`);
logger.debug(`[CLAUDE] This is the Claude agent, NOT Gemini`);

Expand Down
63 changes: 63 additions & 0 deletions src/utils/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* HTTP proxy configuration utilities
*
* Reads proxy settings from:
* 1. Environment variables (http_proxy, https_proxy, HTTP_PROXY, HTTPS_PROXY)
* 2. Agent-specific config files:
* - Claude: ~/.claude/settings.json (env field)
* - Gemini: TODO - add support
* - Codex: TODO - add support
*/

import { HttpsProxyAgent } from 'https-proxy-agent';

type AgentType = 'claude' | 'gemini' | 'codex' | null;
let currentAgentType: AgentType = null;
let claudeProxyCache: { url: string | undefined; loaded: boolean } = { url: undefined, loaded: false };

/**
* Set the current agent type (call this at startup)
*/
export function setAgentType(type: AgentType): void {
currentAgentType = type;
}

/**
* Read proxy URL from Claude Code settings
* Only used when agent type is 'claude'
*/
function getClaudeProxyUrl(): string | undefined {
if (currentAgentType !== 'claude') return undefined;
if (claudeProxyCache.loaded) return claudeProxyCache.url;
claudeProxyCache.loaded = true;

try {
// Lazy import to avoid circular dependency
const { readClaudeSettings } = require('@/claude/utils/claudeSettings');
const settings = readClaudeSettings();
claudeProxyCache.url = settings?.env?.HTTPS_PROXY || settings?.env?.HTTP_PROXY ||
settings?.env?.https_proxy || settings?.env?.http_proxy;
return claudeProxyCache.url;
} catch {
return undefined;
}
}

/**
* Get proxy URL from environment variables or Claude Code settings
* Priority: env vars > Claude Code settings (only for claude agent)
*/
export function getProxyUrl(): string | undefined {
return process.env.https_proxy || process.env.HTTPS_PROXY ||
process.env.http_proxy || process.env.HTTP_PROXY ||
getClaudeProxyUrl();
}

/**
* Create an HttpsProxyAgent if proxy is configured
*/
export function createProxyAgent(): HttpsProxyAgent<string> | undefined {
const proxyUrl = getProxyUrl();
if (!proxyUrl) return undefined;
return new HttpsProxyAgent(proxyUrl);
}