added a smart queue manager and moved proxy logic to proxy manager to make handler just schedule a call to it
This commit is contained in:
parent
da1c52a841
commit
e7c0fe2798
19 changed files with 903 additions and 231 deletions
|
|
@ -9,13 +9,15 @@ export class ProxyManager {
|
|||
private proxies: ProxyInfo[] = [];
|
||||
private proxyIndex: number = 0;
|
||||
private lastUpdate: Date | null = null;
|
||||
private lastFetchTime: Date | null = null;
|
||||
private isInitialized = false;
|
||||
private logger: any;
|
||||
private config: ProxyManagerConfig;
|
||||
|
||||
constructor(cache: CacheProvider, _config: ProxyManagerConfig = {}, logger?: any) {
|
||||
constructor(cache: CacheProvider, config: ProxyManagerConfig = {}, logger?: any) {
|
||||
this.cache = cache;
|
||||
this.config = config;
|
||||
this.logger = logger || console;
|
||||
// Config can be used in the future for customization
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -271,15 +273,123 @@ export class ProxyManager {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch proxies from WebShare API
|
||||
*/
|
||||
private async fetchWebShareProxies(): Promise<ProxyInfo[]> {
|
||||
if (!this.config.webshare) {
|
||||
throw new Error('WebShare configuration not provided');
|
||||
}
|
||||
|
||||
const { apiKey, apiUrl } = this.config.webshare;
|
||||
|
||||
this.logger.info('Fetching proxies from WebShare API', { apiUrl });
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`${apiUrl}proxy/list/?mode=direct&page=1&page_size=100`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Authorization: `Token ${apiKey}`,
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
signal: AbortSignal.timeout(10000), // 10 second timeout
|
||||
}
|
||||
);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`WebShare API request failed: ${response.status} ${response.statusText}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!data.results || !Array.isArray(data.results)) {
|
||||
throw new Error('Invalid response format from WebShare API');
|
||||
}
|
||||
|
||||
// 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(),
|
||||
})
|
||||
);
|
||||
|
||||
this.logger.info('Successfully fetched proxies from WebShare', {
|
||||
count: proxies.length,
|
||||
total: data.count || proxies.length,
|
||||
});
|
||||
|
||||
this.lastFetchTime = new Date();
|
||||
return proxies;
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to fetch proxies from WebShare', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh proxies from WebShare (public method for manual refresh)
|
||||
*/
|
||||
async refreshProxies(): Promise<void> {
|
||||
if (!this.config.enabled || !this.config.webshare) {
|
||||
this.logger.warn('Proxy refresh called but WebShare is not configured');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const proxies = await this.fetchWebShareProxies();
|
||||
await this.updateProxies(proxies);
|
||||
} catch (error) {
|
||||
this.logger.error('Failed to refresh proxies', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last time proxies were fetched from WebShare
|
||||
*/
|
||||
getLastFetchTime(): Date | null {
|
||||
return this.lastFetchTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the proxy manager
|
||||
*/
|
||||
async initialize(): Promise<void> {
|
||||
await this.initializeInternal();
|
||||
|
||||
// Note: Initial proxy sync should be handled by the container or application
|
||||
// that creates ProxyManager instance
|
||||
this.logger.info('ProxyManager initialized - proxy sync should be handled externally');
|
||||
// Fetch proxies on startup if enabled
|
||||
if (this.config.enabled && this.config.webshare) {
|
||||
this.logger.info('Proxy fetching is enabled, fetching proxies from WebShare...');
|
||||
|
||||
try {
|
||||
const proxies = await this.fetchWebShareProxies();
|
||||
if (proxies.length === 0) {
|
||||
throw new Error('No proxies fetched from WebShare API');
|
||||
}
|
||||
|
||||
await this.updateProxies(proxies);
|
||||
this.logger.info('ProxyManager initialized with fresh proxies', {
|
||||
count: proxies.length,
|
||||
lastFetchTime: this.lastFetchTime,
|
||||
});
|
||||
} catch (error) {
|
||||
// If proxy fetching is enabled but fails, the service should not start
|
||||
this.logger.error('Failed to fetch proxies during initialization', { error });
|
||||
throw new Error(`ProxyManager initialization failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
} else {
|
||||
this.logger.info('ProxyManager initialized without fetching proxies (disabled or not configured)');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue