Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add feature to connect claude via proxy #32

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ANTHROPIC_API_KEY=
ANTHROPIC_API_KEY=
#PROXY_HOST_NAME=127.0.0.1
#PROXY_PORT=12111
37 changes: 34 additions & 3 deletions src/main/store/anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
import Anthropic from '@anthropic-ai/sdk';
import dotenv from 'dotenv';
import { SocksProxyAgent } from 'socks-proxy-agent';

dotenv.config();

export const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
// Initialize Anthropic client with or without proxy based on configuration
const anthropicConfig: {
apiKey: string;
baseURL: string;
httpAgent?: SocksProxyAgent;
timeout: number;
} = {
apiKey: process.env.ANTHROPIC_API_KEY!,
baseURL: process.env.ANTHROPIC_BASE_URL || 'https://api.anthropic.com',
});
timeout: 60000,
};

// Configure SOCKS5 proxy if proxy settings are provided in environment variables
if (process.env.PROXY_HOST_NAME && process.env.PROXY_PORT) {
// Create proxy configuration
const proxyOptions = {
hostname: process.env.PROXY_HOST_NAME,
port: parseInt(process.env.PROXY_PORT, 10),
// Optional: Add authentication if required
// username: process.env.PROXY_USERNAME,
// password: process.env.PROXY_PASSWORD,
};

// Initialize SOCKS5 proxy agent
const socksAgent = new SocksProxyAgent(
`socks5://${proxyOptions.hostname}:${proxyOptions.port}`,
);

// Add proxy agent to Anthropic configuration
anthropicConfig.httpAgent = socksAgent;
}

// Create and export Anthropic client instance
export const anthropic = new Anthropic(anthropicConfig);