84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
/**
|
|
* WebShare Fetch Operations - API integration
|
|
*/
|
|
import { OperationContext } from '@stock-bot/di';
|
|
import type { ProxyInfo } from '@stock-bot/proxy';
|
|
import { WEBSHARE_CONFIG } from '../shared/config';
|
|
|
|
/**
|
|
* Fetch proxies from WebShare API and convert to ProxyInfo format
|
|
*/
|
|
export async function fetchWebShareProxies(): Promise<ProxyInfo[]> {
|
|
const ctx = OperationContext.create('webshare', 'fetch-proxies');
|
|
|
|
try {
|
|
// Get configuration from config system
|
|
const { getConfig } = await import('@stock-bot/config');
|
|
const config = getConfig();
|
|
|
|
const apiKey = config.webshare?.apiKey;
|
|
const apiUrl = config.webshare?.apiUrl;
|
|
|
|
if (!apiKey || !apiUrl) {
|
|
ctx.logger.error('Missing WebShare configuration', {
|
|
hasApiKey: !!apiKey,
|
|
hasApiUrl: !!apiUrl,
|
|
});
|
|
return [];
|
|
}
|
|
|
|
ctx.logger.info('Fetching proxies from WebShare API', { apiUrl });
|
|
|
|
const response = await fetch(
|
|
`${apiUrl}proxy/list/?mode=${WEBSHARE_CONFIG.DEFAULT_MODE}&page=${WEBSHARE_CONFIG.DEFAULT_PAGE}&page_size=${WEBSHARE_CONFIG.DEFAULT_PAGE_SIZE}`,
|
|
{
|
|
method: 'GET',
|
|
headers: {
|
|
Authorization: `Token ${apiKey}`,
|
|
'Content-Type': 'application/json',
|
|
},
|
|
signal: AbortSignal.timeout(WEBSHARE_CONFIG.TIMEOUT),
|
|
}
|
|
);
|
|
|
|
if (!response.ok) {
|
|
ctx.logger.error('WebShare API request failed', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
});
|
|
return [];
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!data.results || !Array.isArray(data.results)) {
|
|
ctx.logger.error('Invalid response format from WebShare API', { data });
|
|
return [];
|
|
}
|
|
|
|
// Transform proxy data to ProxyInfo format
|
|
const proxies: ProxyInfo[] = data.results.map(
|
|
(proxy: { username: string; password: string; proxy_address: string; port: number }) => ({
|
|
source: 'webshare',
|
|
protocol: 'http' as const,
|
|
host: proxy.proxy_address,
|
|
port: proxy.port,
|
|
username: proxy.username,
|
|
password: proxy.password,
|
|
isWorking: true, // WebShare provides working proxies
|
|
firstSeen: new Date(),
|
|
lastChecked: new Date(),
|
|
})
|
|
);
|
|
|
|
ctx.logger.info('Successfully fetched proxies from WebShare', {
|
|
count: proxies.length,
|
|
total: data.count || proxies.length,
|
|
});
|
|
|
|
return proxies;
|
|
} catch (error) {
|
|
ctx.logger.error('Failed to fetch proxies from WebShare', { error });
|
|
return [];
|
|
}
|
|
}
|