stock-bot/libs/proxy/src/index.ts
2025-06-13 13:38:02 -04:00

97 lines
2.1 KiB
TypeScript

// Simple proxy list manager
let proxies: string[] = [];
let currentIndex = 0;
const DEFAULT_PROXY_URL =
'https://api.proxyscrape.com/v2/?request=getproxies&protocol=http&timeout=10000&country=all&ssl=all&anonymity=all';
/**
* Fetch proxy list from URL and store in module
*/
export async function refreshProxies(fetchUrl: string = DEFAULT_PROXY_URL): Promise<string[]> {
try {
const response = await fetch(fetchUrl);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const data = await response.text();
const newProxies = data
.trim()
.split('\n')
.map(line => line.trim())
.filter(line => line && line.includes(':'))
.map(line => {
// Convert host:port to http://host:port format
return line.startsWith('http') ? line : `http://${line}`;
});
proxies = newProxies;
currentIndex = 0;
return proxies;
} catch (error) {
throw new Error(`Failed to fetch proxies: ${error}`);
}
}
/**
* Get next proxy URL in round-robin fashion
*/
export function getProxyURL(): string | null {
if (proxies.length === 0) {
return null;
}
const proxy = proxies[currentIndex];
currentIndex = (currentIndex + 1) % proxies.length;
return proxy;
}
/**
* Get multiple proxy URLs
*/
export function getProxyURLs(count: number): string[] {
const urls: string[] = [];
for (let i = 0; i < count; i++) {
const url = getProxyURL();
if (url) {
urls.push(url);
}
}
return urls;
}
/**
* Get random proxy URL
*/
export function getRandomProxyURL(): string | null {
if (proxies.length === 0) {
return null;
}
const randomIndex = Math.floor(Math.random() * proxies.length);
return proxies[randomIndex];
}
/**
* Get current proxy count
*/
export function getProxyCount(): number {
return proxies.length;
}
/**
* Get all proxies
*/
export function getAllProxies(): string[] {
return [...proxies];
}
/**
* Initialize proxy manager with initial fetch
*/
export async function initializeProxies(fetchUrl?: string): Promise<void> {
await refreshProxies(fetchUrl);
}