work on proxy service

This commit is contained in:
Bojan Kucera 2025-06-07 18:04:20 -04:00
parent baa34a3805
commit 81d88357ca
5 changed files with 204 additions and 547 deletions

View file

@ -2,20 +2,20 @@ import got from 'got';
import { SocksProxyAgent } from 'socks-proxy-agent';
import { HttpsProxyAgent } from 'https-proxy-agent';
import { HttpProxyAgent } from 'http-proxy-agent';
import type { ProxyConfig } from './types.js';
import type { ProxyInfo } from './types.js';
export class ProxyManager {
/**
* Determine if we should use Bun fetch (HTTP/HTTPS) or Got (SOCKS)
*/
static shouldUseBunFetch(proxy: ProxyConfig): boolean {
static shouldUseBunFetch(proxy: ProxyInfo): boolean {
return proxy.protocol === 'http' || proxy.protocol === 'https';
}
/**
* Create Bun fetch proxy URL for HTTP/HTTPS proxies
*/
static createBunProxyUrl(proxy: ProxyConfig): string {
static createBunProxyUrl(proxy: ProxyInfo): string {
const { protocol, host, port, username, password } = proxy;
if (username && password) {
@ -27,7 +27,7 @@ export class ProxyManager {
/**
* Create appropriate agent for Got based on proxy type
*/
static createGotAgent(proxy: ProxyConfig) {
static createGotAgent(proxy: ProxyInfo) {
this.validateConfig(proxy);
const proxyUrl = this.buildProxyUrl(proxy);
@ -48,7 +48,7 @@ export class ProxyManager {
/**
* Create Got instance with proxy configuration
*/
static createGotInstance(proxy: ProxyConfig) {
static createGotInstance(proxy: ProxyInfo) {
const agent = this.createGotAgent(proxy);
return got.extend({
@ -68,7 +68,7 @@ export class ProxyManager {
});
}
private static buildProxyUrl(proxy: ProxyConfig): string {
private static buildProxyUrl(proxy: ProxyInfo): string {
const { protocol, host, port, username, password } = proxy;
if (username && password) {
@ -80,7 +80,7 @@ export class ProxyManager {
/**
* Simple proxy config validation
*/
static validateConfig(proxy: ProxyConfig): void {
static validateConfig(proxy: ProxyInfo): void {
if (!proxy.host || !proxy.port) {
throw new Error('Proxy host and port are required');
}

View file

@ -1,12 +1,16 @@
// Minimal types for fast HTTP client
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
export interface ProxyConfig {
export interface ProxyInfo {
protocol: 'http' | 'https' | 'socks4' | 'socks5';
host: string;
port: number;
username?: string;
password?: string;
isWorking?: boolean;
responseTime?: number;
error?: string;
checkedAt?: Date;
}
export interface HttpClientConfig {
@ -20,7 +24,7 @@ export interface RequestConfig {
headers?: Record<string, string>;
body?: any;
timeout?: number;
proxy?: ProxyConfig;
proxy?: ProxyInfo;
}
export interface HttpResponse<T = any> {

View file

@ -1,6 +1,6 @@
import { describe, test, expect, beforeEach, beforeAll, afterAll } from 'bun:test';
import { HttpClient, HttpError, ProxyManager } from '../src/index.js';
import type { ProxyConfig } from '../src/types.js';
import type { ProxyInfo } from '../src/types.js';
import { MockServer } from './mock-server.js';
// Global mock server instance
@ -119,13 +119,13 @@ describe('HttpClient', () => {
describe('ProxyManager', () => {
test('should determine when to use Bun fetch', () => {
const httpProxy: ProxyConfig = {
const httpProxy: ProxyInfo = {
protocol: 'http',
host: 'proxy.example.com',
port: 8080
};
const socksProxy: ProxyConfig = {
const socksProxy: ProxyInfo = {
protocol: 'socks5',
host: 'proxy.example.com',
port: 1080
@ -136,7 +136,7 @@ describe('ProxyManager', () => {
});
test('should create proxy URL for Bun fetch', () => {
const proxy: ProxyConfig = {
const proxy: ProxyInfo = {
protocol: 'http',
host: 'proxy.example.com',
port: 8080,
@ -149,7 +149,7 @@ describe('ProxyManager', () => {
});
test('should create proxy URL without credentials', () => {
const proxy: ProxyConfig = {
const proxy: ProxyInfo = {
protocol: 'https',
host: 'proxy.example.com',
port: 8080