working on proxy service

This commit is contained in:
Bojan Kucera 2025-06-07 16:24:27 -04:00
parent 1395e448b7
commit 3d9eb2ec9b
3 changed files with 88 additions and 87 deletions

View file

@ -97,7 +97,7 @@ async function demonstrateCustomProxySource() {
const customSources : ProxySource[] = [
{
url: 'https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt',
url: 'https://raw.githubusercontent.com/TheSpeedX/PROXY-List/refs/heads/master/http.txt',
protocol: 'http'
}
];
@ -114,82 +114,77 @@ async function demonstrateCustomProxySource() {
});
}
}
// Export functions for use in other files
export {
// demonstrateProxyService,
demonstrateCustomProxySource
};
// Execute the demo with enhanced logging and graceful shutdown
logger.info('🚀 Starting enhanced proxy demo...');
// Configure graceful shutdown
setShutdownTimeout(15000); // 15 seconds shutdown timeout
// Register shutdown handlers
onShutdown(async () => {
logger.info('🔧 Cleaning up proxy service...');
try {
// Clean up proxy service resources if needed
// await proxyService.shutdown();
logger.info('✅ Proxy service cleanup completed');
} catch (error) {
logger.error('❌ Proxy service cleanup failed', error);
}
});
onShutdown(async () => {
logger.info('🔧 Shutting down loggers...');
try {
await shutdownLoggers();
console.log('✅ Logger shutdown completed');
} catch (error) {
console.error('❌ Logger shutdown failed:', error);
}
});
onShutdown(async () => {
logger.info('🔧 Performing final cleanup...');
// Any additional cleanup can go here
await new Promise(resolve => setTimeout(resolve, 1000)); // Simulate cleanup work
logger.info('✅ Final cleanup completed');
});
// Demonstrate graceful shutdown by setting up a timer to shutdown after demo
const shutdownTimer = setTimeout(() => {
logger.info('⏰ Demo timeout reached, initiating graceful shutdown...');
process.kill(process.pid, 'SIGTERM');
}, 20000); // Shutdown after 20 seconds
// Clear timer if demo completes early
const clearShutdownTimer = () => {
clearTimeout(shutdownTimer);
logger.info('⏰ Demo completed, canceling automatic shutdown timer');
};
demonstrateCustomProxySource()
.then(() => {
logger.info('🎉 Proxy demo completed successfully!');
clearShutdownTimer();
// Demonstrate manual shutdown after a short delay
setTimeout(() => {
logger.info('🔄 Demonstrating manual graceful shutdown in 3 seconds...');
setTimeout(() => {
process.kill(process.pid, 'SIGTERM');
}, 3000);
}, 2000);
})
.catch((error) => {
logger.error('💥 Proxy demo failed', error);
clearShutdownTimer();
setTimeout(() => process.exit(1), 1000);
});
// If this file is run directly, execute the demo
// if (import.meta.main) {
// demonstrateProxyService()
// demonstrateCustomProxySource()
// .catch(console.error);
// }
// // Execute the demo with enhanced logging and graceful shutdown
// logger.info('🚀 Starting enhanced proxy demo...');
// // Configure graceful shutdown
// setShutdownTimeout(15000); // 15 seconds shutdown timeout
// // Register shutdown handlers
// onShutdown(async () => {
// logger.info('🔧 Cleaning up proxy service...');
// try {
// // Clean up proxy service resources if needed
// // await proxyService.shutdown();
// logger.info('✅ Proxy service cleanup completed');
// } catch (error) {
// logger.error('❌ Proxy service cleanup failed', error);
// }
// });
// onShutdown(async () => {
// logger.info('🔧 Shutting down loggers...');
// try {
// await shutdownLoggers();
// console.log('✅ Logger shutdown completed');
// } catch (error) {
// console.error('❌ Logger shutdown failed:', error);
// }
// });
// onShutdown(async () => {
// logger.info('🔧 Performing final cleanup...');
// // Any additional cleanup can go here
// await new Promise(resolve => setTimeout(resolve, 1000)); // Simulate cleanup work
// logger.info('✅ Final cleanup completed');
// });
// // Demonstrate graceful shutdown by setting up a timer to shutdown after demo
// const shutdownTimer = setTimeout(() => {
// logger.info('⏰ Demo timeout reached, initiating graceful shutdown...');
// process.kill(process.pid, 'SIGTERM');
// }, 20000); // Shutdown after 20 seconds
// // Clear timer if demo completes early
// const clearShutdownTimer = () => {
// clearTimeout(shutdownTimer);
// logger.info('⏰ Demo completed, canceling automatic shutdown timer');
// };
// demonstrateCustomProxySource()
// .then(() => {
// logger.info('🎉 Proxy demo completed successfully!');
// clearShutdownTimer();
// // Demonstrate manual shutdown after a short delay
// setTimeout(() => {
// logger.info('🔄 Demonstrating manual graceful shutdown in 3 seconds...');
// setTimeout(() => {
// process.kill(process.pid, 'SIGTERM');
// }, 3000);
// }, 2000);
// })
// .catch((error) => {
// logger.error('💥 Proxy demo failed', error);
// clearShutdownTimer();
// setTimeout(() => process.exit(1), 1000);
// });
// // If this file is run directly, execute the demo
// // if (import.meta.main) {
// // demonstrateProxyService()
// // demonstrateCustomProxySource()
// // .catch(console.error);
// // }

View file

@ -38,7 +38,7 @@ export class ProxyService {
private readonly CACHE_PREFIX = 'proxy:';
private readonly WORKING_PROXIES_KEY = 'proxy:working';
private readonly PROXY_STATS_KEY = 'proxy:stats';
private readonly CHECK_TIMEOUT = 10000; // 10 seconds
private readonly CHECK_TIMEOUT = 3000; // 10 seconds
private readonly DEFAULT_CHECK_URL = 'https://proxy-detection.stare.gg/?api_key=bd406bf53ddc6abe1d9de5907830a955';
private readonly DEFAULT_IP_ADDRESS = '99.246.102.205'
@ -78,7 +78,7 @@ export class ProxyService {
this.httpClient = new HttpClient({
timeout: this.CHECK_TIMEOUT,
});
}, this.logger);
this.logger.info('ProxyService initialized');
}
@ -442,7 +442,6 @@ export class ProxyService {
this.logger.info('Starting background proxy validation', { count: proxies.length });
const concurrency = 50; // Process 50 proxies concurrently
const chunks = this.chunkArray(proxies, concurrency);
for (const chunk of chunks) {
const validationPromises = chunk.map(proxy =>
this.checkProxy(proxy).catch(error => {
@ -454,9 +453,7 @@ export class ProxyService {
return null;
})
);
await Promise.allSettled(validationPromises);
// Small delay between chunks to avoid overwhelming the system
await new Promise(resolve => setTimeout(resolve, 1000));
}