proxy-auto-ts
Version:
A comprehensive TypeScript library for automatic proxy management with validation, rotation, and intelligent selection
286 lines (206 loc) โข 7.93 kB
Markdown
A comprehensive TypeScript library for automatic proxy management with validation, rotation, and intelligent selection.
[](https://badge.fury.io/js/proxy-auto-ts)
[](https://www.typescriptlang.org/)
[](https://opensource.org/licenses/MIT)
- ๐ **Automatic Proxy Rotation**: Seamlessly rotate through working proxies
- ๐งช **Proxy Validation**: Built-in proxy testing and validation
- โก **Performance Optimization**: Latency-based proxy selection
- ๐ก๏ธ **Fallback Support**: Multiple URL fallbacks for enhanced reliability
- ๐ฏ **Smart Headers**: Random User-Agent rotation and realistic headers
- ๐ **Comprehensive Statistics**: Detailed proxy performance metrics
- ๐ **Easy Integration**: Simple API with TypeScript support
- ๐ง **Configurable**: Customizable timeouts, fallbacks, and user agents
- ๐พ **Persistent Storage**: Automatic proxy list caching and management
- ๐ **Type Safety**: Full TypeScript support with comprehensive type definitions
```bash
npm install proxy-auto-ts
yarn add proxy-auto-ts
bun add proxy-auto-ts
```
> **Current Version**: 1.1.2
> **Node.js**: >= 16.0.0
> **TypeScript**: >= 5.0.0
```typescript
import { ProxyManager } from 'proxy-auto-ts';
// Initialize with default configuration
const proxyManager = new ProxyManager();
// Auto-initialization on first use
const result = await proxyManager.fetchWithProxy('https://httpbin.org/ip');
console.log('Your IP:', result.data.origin);
console.log('Used proxy:', result.proxy);
console.log('Latency:', result.latency + 'ms');
```
```typescript
import { ProxyManager } from 'proxy-auto-ts';
const proxyManager = new ProxyManager();
await proxyManager.initialize(); // Explicitly initialize proxy list
const result = await proxyManager.fetchWithProxy('https://httpbin.org/ip');
console.log('Your IP:', result.data.origin);
```
```typescript
new ProxyManager(config?: ProxyManagerConfig)
```
**Configuration Options:**
```typescript
interface ProxyManagerConfig {
timeout?: number; // Request timeout (default: 15000ms)
validationTimeout?: number; // Proxy validation timeout (default: 8000ms)
fallbackUrls?: string[]; // Fallback URLs for testing
userAgents?: string[]; // Custom User-Agent list
proxyListPath?: string; // Custom proxy list file path
}
```
Fetch URL through the first available working proxy with automatic fallbacks.
```typescript
const result = await proxyManager.fetchWithProxy('https://example.com', 5);
```
Find the best performing proxy based on latency.
```typescript
const bestProxy = await proxyManager.findBestProxy();
console.log(`Best proxy: ${bestProxy.proxy} (${bestProxy.latency}ms)`);
```
Fetch URL using a specific proxy.
```typescript
const result = await proxyManager.fetchWithSpecificProxy(
'https://example.com',
'1.2.3.4:8080'
);
```
Test if a specific proxy is working.
```typescript
const isWorking = await proxyManager.testProxy('1.2.3.4:8080');
```
Get proxy statistics and configuration.
```typescript
const stats = proxyManager.getStats();
console.log(`Total proxies: ${stats.totalProxies}`);
```
```typescript
interface ProxyFetchResult {
data: any; // Response data
proxy: string; // Used proxy address
latency: number; // Request latency in milliseconds
}
```
```typescript
import { ProxyManager } from 'proxy-auto-ts';
const proxyManager = new ProxyManager({
timeout: 20000,
validationTimeout: 10000,
fallbackUrls: [
'https://httpbin.org/ip',
'https://api.ipify.org?format=json'
],
userAgents: [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36'
],
proxyListPath: './custom-proxies.txt'
});
```
```typescript
try {
const result = await proxyManager.fetchWithProxy('https://example.com');
console.log('Success:', result.data);
} catch (error) {
console.error('All proxies failed:', error.message);
}
```
```typescript
// Find and use the best proxy
const bestProxy = await proxyManager.findBestProxy();
// Use the best proxy for subsequent requests
const result = await proxyManager.fetchWithSpecificProxy(
'https://your-target-site.com',
bestProxy.proxy
);
```
Create a `proxies.txt` file with the following format:
```txt
52.74.26.202:8080
152.42.170.187:9090
188.166.230.109:31028
182.253.109.26:8080
```
```bash
bun run test
bun run test:quick
bun run test:full
bun run proxy
bun run examples
```
For common issues and solutions, please check our [TROUBLESHOOTING.md](TROUBLESHOOTING.md) guide.
**Common Issues:**
- **Connection timeouts**: Adjust the `timeout` and `validationTimeout` settings
- **No working proxies**: Update your proxy list with `bun run proxy`
- **Rate limiting**: Implement delays between requests in your application
```bash
git clone https://github.com/proxy-auto-ts/proxy-auto-ts.git
bun install
bun run build
bun run test
```
- `build` - Build the library for production
- `build:lib` - Build TypeScript definitions
- `dev` - Run development tests
- `test` - Run quick tests
- `test:full` - Run comprehensive tests
- `test:quick` - Run quick tests
- `proxy` - Update proxy list from sources
- `examples` - Run example code
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## ๐ License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## โ ๏ธ Legal Notice
This library is intended for educational and testing purposes. Users are responsible for ensuring compliance with all applicable laws and regulations when using proxy servers. Always respect website terms of service and rate limits.
## ๐ Acknowledgments
- Built with TypeScript for type safety
- Uses axios for HTTP requests
- Powered by https-proxy-agent for proxy support
- Includes automatic proxy validation and rotation
## ๐ Support
- ๐ Issues: [GitHub Issues](https://github.com/proxy-auto-ts/proxy-auto-ts/issues)
- ๐ Documentation: [GitHub Repository](https://github.com/proxy-auto-ts/proxy-auto-ts)
- ๐ฌ Discussions: [GitHub Discussions](https://github.com/proxy-auto-ts/proxy-auto-ts/discussions)