no luck with sock proxies

This commit is contained in:
Bojan Kucera 2025-06-07 22:49:03 -04:00
parent a7cca4d4a1
commit 0eeb893d0f
5 changed files with 98 additions and 61 deletions

View file

@ -144,22 +144,25 @@ export class HttpClient {
* Got implementation (simplified for SOCKS proxies)
*/
private async gotRequest<T>(config: RequestConfig, signal: AbortSignal): Promise<HttpResponse<T>> {
try {
const options = this.buildGotOptions(config, signal);
const response = await got(config.url, options);
return this.parseGotResponse<T>(response);
} catch (error) {
// Handle both AbortSignal timeout and Got-specific timeout errors
if (signal.aborted) {
throw new HttpError(`Request timeout`);
if(config.proxy) {
try {
const gotClient = await ProxyManager.createGotInstance(config.proxy);
const response = await gotClient.get(config.url);
return this.parseGotResponse<T>(response);
} catch (error) {
console.error('Got request error:', error);
// Handle both AbortSignal timeout and Got-specific timeout errors
if (signal.aborted) {
throw new HttpError(`Request timeout`);
}
if ((error as any).name === 'TimeoutError') {
throw new HttpError(`Request timeout`);
}
throw new HttpError(`Request failed: ${(error as Error).message}`);
}
if ((error as any).name === 'TimeoutError') {
throw new HttpError(`Request timeout`);
}
throw new HttpError(`Request failed: ${(error as Error).message}`);
}else{
throw new HttpError(`Request failed: No proxy configured, use fetch instead`);
}
}
/**

View file

@ -35,6 +35,7 @@ export class ProxyManager {
switch (proxy.protocol) {
case 'socks4':
case 'socks5':
console.log(`Using SOCKS proxy: ${proxyUrl}`);
return new SocksProxyAgent(proxyUrl);
case 'http':
return new HttpProxyAgent(proxyUrl);
@ -48,29 +49,32 @@ export class ProxyManager {
/**
* Create Got instance with proxy configuration
*/
static createGotInstance(proxy: ProxyInfo) {
static async createGotInstance(proxy: ProxyInfo): Promise<typeof got> {
const agent = this.createGotAgent(proxy);
await new Promise(r => setTimeout(r, 2000));
return got.extend({
agent: {
http: agent,
https: agent
},
timeout: {
request: 30000,
connect: 10000
},
retry: {
limit: 3,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
},
throwHttpErrors: false // We'll handle errors ourselves
});
http2: false,
agent: {
http: agent,
https: agent
},
timeout: {
request: 30000,
connect: 10000
},
retry: {
limit: 3,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH']
},
throwHttpErrors: false // We'll handle errors ourselves
});
}
private static buildProxyUrl(proxy: ProxyInfo): string {
const { protocol, host, port, username, password } = proxy;
if( protocol.includes('socks') ) {
return `socks5://${encodeURIComponent('me@bojancode.com')}:${encodeURIComponent('re$Pon7dipR')}@atlanta.us.socks.nordhold.net:1080`;
}
if (username && password) {
return `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
}