well seems like no socks on bun

This commit is contained in:
Bojan Kucera 2025-06-08 00:01:27 -04:00
parent 93578bd030
commit 6380165a94
6 changed files with 116 additions and 137 deletions

View file

@ -1,4 +1,4 @@
import got from 'got';
import axios, { AxiosRequestConfig, type AxiosInstance } from 'axios';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';
@ -6,18 +6,19 @@ import type { ProxyInfo } from './types.js';
export class ProxyManager {
/**
* Determine if we should use Bun fetch (HTTP/HTTPS) or Got (SOCKS)
* Determine if we should use Bun fetch (HTTP/HTTPS) or Axios (SOCKS)
*/
static shouldUseBunFetch(proxy: ProxyInfo): boolean {
return proxy.protocol === 'http' || proxy.protocol === 'https';
}
/**
* Create Bun fetch proxy URL for HTTP/HTTPS proxies
* Create proxy URL for both Bun fetch and Axios proxy agents
*/
static createBunProxyUrl(proxy: ProxyInfo): string {
static createProxyUrl(proxy: ProxyInfo): string {
const { protocol, host, port, username, password } = proxy;
if(protocol.includes('socks')) {
return `${protocol}://${host}:${port}`;
}
if (username && password) {
return `${protocol}://${encodeURIComponent(username)}:${encodeURIComponent(password)}@${host}:${port}`;
}
@ -25,13 +26,13 @@ export class ProxyManager {
}
/**
* Create appropriate agent for Got based on proxy type
* Create appropriate agent for Axios based on proxy type
*/
static createGotAgent(proxy: ProxyInfo) {
static createProxyAgent(proxy: ProxyInfo) {
this.validateConfig(proxy);
const proxyUrl = this.buildProxyUrl(proxy);
const proxyUrl = this.createProxyUrl(proxy);
console.log(`Using proxy url: ${proxyUrl}`);
switch (proxy.protocol) {
case 'socks4':
case 'socks5':
@ -45,42 +46,22 @@ export class ProxyManager {
throw new Error(`Unsupported proxy protocol: ${proxy.protocol}`);
}
}
/**
* Create Got instance with proxy configuration
* Create Axios instance with proxy configuration
*/
static async createGotInstance(proxy: ProxyInfo): Promise<typeof got> {
const agent = this.createGotAgent(proxy);
await new Promise(r => setTimeout(r, 2000));
return got.extend({
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
});
static createAxiosConfig(proxy: ProxyInfo): AxiosRequestConfig {
const agent = this.createProxyAgent(proxy);
return {
httpAgent: agent,
httpsAgent: agent,
// timeout: 30000,
// validateStatus: () => true, // Don't throw errors on HTTP status codes
// maxRedirects: 5,
// headers: {
// 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
// }
};
}
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}`;
}
return `${protocol}://${host}:${port}`;
}
/**
* Simple proxy config validation
*/