通过node.js语言配置IPNut 代理:
进入 IPNut 平台购买并获取信息,这里以静态IP为例(假如端口是 http://proxy.ipnut.com:28001,账号: ipnut,密码: 123456789)。
1. SOCKS5代理示例 #
/**
* SOCKS5代理演示
*/
const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');
const http = require('http');
const https = require('https');
class Socks5ProxyDemo {
constructor() {
this.proxyHost = 'proxy.ipnut.com';
this.proxyPort = 28001;
this.proxyUsername = 'ipnut';
this.proxyPassword = '123456789';
// 构建SOCKS5代理URL
this.proxyUrl = `socks5://${this.proxyUsername}:${this.proxyPassword}@${this.proxyHost}:${this.proxyPort}`;
this.socksAgent = new SocksProxyAgent(this.proxyUrl);
}
/**
* 使用axios的SOCKS5代理
*/
async socks5WithAxios() {
console.log('=== 使用axios的SOCKS5代理 ===');
try {
const response = await axios.get('http://httpbin.org/ip', {
httpAgent: this.socksAgent,
httpsAgent: this.socksAgent,
timeout: 30000,
headers: {
'User-Agent': 'Node.js-SOCKS5-Proxy/1.0'
}
});
console.log('状态码:', response.status);
console.log('响应数据:', response.data);
} catch (error) {
console.error('请求失败:', error.message);
}
}
/**
* SOCKS5代理多请求演示
*/
async socks5MultipleRequests() {
console.log('\n=== SOCKS5代理多请求演示 ===');
const urls = [
'http://httpbin.org/ip',
'http://httpbin.org/user-agent',
'http://httpbin.org/headers'
];
for (let i = 0; i < urls.length; i++) {
console.log(`\n请求 ${i + 1}: ${urls[i]}`);
try {
const response = await axios.get(urls[i], {
httpAgent: this.socksAgent,
httpsAgent: this.socksAgent,
timeout: 30000,
headers: {
'User-Agent': 'Node.js-SOCKS5-Multi/1.0'
}
});
console.log('状态码:', response.status);
console.log('响应摘要:', JSON.stringify(response.data).substring(0, 150) + '...');
// 延迟避免请求过快
await new Promise(resolve => setTimeout(resolve, 1000));
} catch (error) {
console.error('请求失败:', error.message);
}
}
}
/**
* SOCKS5代理自定义请求
*/
async socks5CustomRequest() {
console.log('\n=== SOCKS5代理自定义请求 ===');
try {
const response = await axios.post(
'https://httpbin.org/post',
{
name: 'ipnut_user',
email: 'user@ipnut.com',
message: '测试SOCKS5代理连接'
},
{
httpAgent: this.socksAgent,
httpsAgent: this.socksAgent,
timeout: 30000,
headers: {
'User-Agent': 'Node.js-SOCKS5-POST/1.0',
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}
);
console.log('状态码:', response.status);
console.log('响应数据:', JSON.stringify(response.data, null, 2));
} catch (error) {
console.error('POST请求失败:', error.message);
}
}
/**
* 使用原生http模块的SOCKS5代理
*/
socks5WithHttpModule() {
console.log('\n=== 使用原生http模块的SOCKS5代理 ===');
return new Promise((resolve, reject) => {
const options = {
hostname: 'httpbin.org',
port: 80,
path: '/ip',
method: 'GET',
agent: this.socksAgent,
headers: {
'User-Agent': 'Node.js-HTTP-SOCKS5/1.0',
'Connection': 'close'
}
};
const req = http.request(options, (res) => {
let data = '';
console.log('状态码:', res.statusCode);
console.log('响应头:', res.headers);
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('响应内容:', data);
resolve(data);
});
});
req.on('error', (error) => {
console.error('请求错误:', error.message);
reject(error);
});
req.setTimeout(30000, () => {
req.destroy();
console.error('请求超时');
});
req.end();
});
}
/**
* 运行所有SOCKS5演示
*/
async runAll() {
console.log('开始SOCKS5代理测试...\n');
await this.socks5WithAxios();
await this.socks5MultipleRequests();
await this.socks5CustomRequest();
await this.socks5WithHttpModule();
console.log('\nSOCKS5代理测试完成!');
}
}
// 运行SOCKS5演示
async function runSocks5Demo() {
const socks5Demo = new Socks5ProxyDemo();
await socks5Demo.runAll();
}
module.exports = { Socks5ProxyDemo, runSocks5Demo };
2. HTTP代理示例 #
/**
* HTTP代理演示
*/
const axios = require('axios');
const { HttpsProxyAgent } = require('https-proxy-agent');
const http = require('http');
const https = require('https');
class HttpProxyDemo {
constructor() {
this.proxyHost = 'proxy.ipnut.com';
this.proxyPort = 28001;
this.proxyUsername = 'ipnut';
this.proxyPassword = '123456789';
// 构建HTTP代理URL
this.proxyUrl = `http://${this.proxyUsername}:${this.proxyPassword}@${this.proxyHost}:${this.proxyPort}`;
this.httpAgent = new HttpsProxyAgent(this.proxyUrl);
}
/**
* 使用axios的HTTP代理
*/
async httpWithAxios() {
console.log('=== 使用axios的HTTP代理 ===');
try {
const response = await axios.get('http://httpbin.org/ip', {
httpsAgent: this.httpAgent,
timeout: 30000,
headers: {
'User-Agent': 'Node.js-HTTP-Proxy/1.0'
}
});
console.log('状态码:', response.status);
console.log('响应数据:', response.data);
} catch (error) {
console.error('请求失败:', error.message);
}
}
/**
* HTTP代理多请求演示
*/
async httpMultipleRequests() {
console.log('\n=== HTTP代理多请求演示 ===');
const urls = [
'http://httpbin.org/ip',
'http://httpbin.org/user-agent',
'http://httpbin.org/get?param1=value1¶m2=value2'
];
// 使用Promise.all并发请求
const requests = urls.map((url, index) =>
axios.get(url, {
httpsAgent: this.httpAgent,
timeout: 30000,
headers: {
'User-Agent': 'Node.js-HTTP-Multi/1.0'
}
}).then(response => {
console.log(`\n请求 ${index + 1}: ${url}`);
console.log('状态码:', response.status);
console.log('响应摘要:', JSON.stringify(response.data).substring(0, 150) + '...');
return response.data;
}).catch(error => {
console.log(`\n请求 ${index + 1}失败:`, error.message);
return null;
})
);
await Promise.all(requests);
}
/**
* HTTP代理POST请求
*/
async httpPostRequest() {
console.log('\n=== HTTP代理POST请求 ===');
try {
const response = await axios.post(
'https://httpbin.org/post',
{
name: 'ipnut_user',
email: 'user@ipnut.com',
message: '测试HTTP代理POST请求'
},
{
httpsAgent: this.httpAgent,
timeout: 30000,
headers: {
'User-Agent': 'Node.js-HTTP-POST/1.0',
'Content-Type': 'application/json'
}
}
);
console.log('状态码:', response.status);
console.log('响应数据:', JSON.stringify(response.data, null, 2));
} catch (error) {
console.error('POST请求失败:', error.message);
}
}
/**
* 使用原生https模块的HTTP代理
*/
httpWithHttpsModule() {
console.log('\n=== 使用原生https模块的HTTP代理 ===');
return new Promise((resolve, reject) => {
const options = {
hostname: 'httpbin.org',
port: 443,
path: '/ip',
method: 'GET',
agent: this.httpAgent,
headers: {
'User-Agent': 'Node.js-HTTPS-Proxy/1.0',
'Accept': 'application/json'
},
rejectUnauthorized: false // 仅测试使用
};
const req = https.request(options, (res) => {
let data = '';
console.log('状态码:', res.statusCode);
console.log('响应头:', res.headers);
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('响应内容:', data);
resolve(data);
});
});
req.on('error', (error) => {
console.error('请求错误:', error.message);
reject(error);
});
req.setTimeout(30000, () => {
req.destroy();
console.error('请求超时');
});
req.end();
});
}
/**
* HTTP代理流式请求
*/
async httpStreamRequest() {
console.log('\n=== HTTP代理流式请求 ===');
return new Promise((resolve, reject) => {
const options = {
hostname: 'httpbin.org',
port: 80,
path: '/stream/3',
method: 'GET',
agent: this.httpAgent,
headers: {
'User-Agent': 'Node.js-HTTP-Stream/1.0'
}
};
const req = http.request(options, (res) => {
console.log('状态码:', res.statusCode);
console.log('流式响应:');
res.setEncoding('utf8');
res.on('data', (chunk) => {
console.log('收到数据块:', chunk.toString());
});
res.on('end', () => {
console.log('流结束');
resolve();
});
});
req.on('error', (error) => {
console.error('流请求错误:', error.message);
reject(error);
});
req.end();
});
}
/**
* 运行所有HTTP演示
*/
async runAll() {
console.log('开始HTTP代理测试...\n');
await this.httpWithAxios();
await this.httpMultipleRequests();
await this.httpPostRequest();
await this.httpWithHttpsModule();
await this.httpStreamRequest();
console.log('\nHTTP代理测试完成!');
}
}
// 运行HTTP演示
async function runHttpDemo() {
const httpDemo = new HttpProxyDemo();
await httpDemo.runAll();
}
module.exports = { HttpProxyDemo, runHttpDemo };
3. 代理测试工具 #
/**
* 代理测试工具
*/
const axios = require('axios');
const { SocksProxyAgent } = require('socks-proxy-agent');
const { HttpsProxyAgent } = require('https-proxy-agent');
class ProxyTestTool {
constructor() {
this.proxyHost = 'proxy.ipnut.com';
this.proxyPort = 28001;
this.proxyUsername = 'ipnut';
this.proxyPassword = '123456789';
}
/**
* 测试SOCKS5代理
*/
async testSocks5Proxy() {
console.log('测试 SOCKS5 代理:');
const proxyUrl = `socks5://${this.proxyUsername}:${this.proxyPassword}@${this.proxyHost}:${this.proxyPort}`;
const socksAgent = new SocksProxyAgent(proxyUrl);
try {
const response = await axios.get('http://httpbin.org/ip', {
httpAgent: socksAgent,
httpsAgent: socksAgent,
timeout: 15000
});
console.log('✅ SOCKS5 代理连接成功');
console.log(' 状态码:', response.status);
console.log(' 当前IP:', response.data.origin);
} catch (error) {
console.log('❌ SOCKS5 代理连接失败:', error.message);
}
}
/**
* 测试HTTP代理
*/
async testHttpProxy() {
console.log('\n测试 HTTP 代理:');
const proxyUrl = `http://${this.proxyUsername}:${this.proxyPassword}@${this.proxyHost}:${this.proxyPort}`;
const httpAgent = new HttpsProxyAgent(proxyUrl);
try {
const response = await axios.get('http://httpbin.org/ip', {
httpsAgent: httpAgent,
timeout: 15000
});
console.log('✅ HTTP 代理连接成功');
console.log(' 状态码:', response.status);
console.log(' 当前IP:', response.data.origin);
} catch (error) {
console.log('❌ HTTP 代理连接失败:', error.message);
}
}
/**
* 运行测试
*/
async runTests() {
console.log('=== 代理测试工具 ===\n');
await this.testSocks5Proxy();
await this.testHttpProxy();
console.log('\n代理测试完成!');
}
}
// 运行测试工具
async function runProxyTests() {
const testTool = new ProxyTestTool();
await testTool.runTests();
}
module.exports = { ProxyTestTool, runProxyTests };
4. 主程序文件 #
/**
* 主程序 - 代理演示总入口
*/
const { runSocks5Demo } = require('./socks5-proxy-demo');
const { runHttpDemo } = require('./http-proxy-demo');
const { runProxyTests } = require('./proxy-test-tool');
async function main() {
console.log('Node.js代理连接演示程序');
console.log('====================\n');
// 运行代理测试工具
await runProxyTests();
console.log('\n' + '='.repeat(50) + '\n');
// 运行SOCKS5演示
await runSocks5Demo();
console.log('\n' + '='.repeat(50) + '\n');
// 运行HTTP演示
await runHttpDemo();
console.log('\n所有演示完成!');
}
// 运行主程序
if (require.main === module) {
main().catch(console.error);
}
module.exports = { main };
5. Package.json 文件 #
{
"name": "node-proxy-demo",
"version": "1.0.0",
"description": "Node.js SOCKS5 and HTTP proxy demo",
"main": "main.js",
"scripts": {
"start": "node main.js",
"socks5": "node -e \"require('./socks5-proxy-demo').runSocks5Demo()\"",
"http": "node -e \"require('./http-proxy-demo').runHttpDemo()\"",
"test": "node -e \"require('./proxy-test-tool').runProxyTests()\""
},
"dependencies": {
"axios": "^1.6.0",
"socks-proxy-agent": "^8.0.2",
"https-proxy-agent": "^7.0.2"
},
"keywords": ["proxy", "socks5", "http", "nodejs"],
"author": "Demo",
"license": "MIT"
}
6. 运行说明 #
# 文件结构:
# node-proxy-demo/
# ├── socks5-proxy-demo.js # SOCKS5代理演示
# ├── http-proxy-demo.js # HTTP代理演示
# ├── proxy-test-tool.js # 代理测试工具
# ├── main.js # 主程序入口
# └── package.json # 依赖配置
# 安装依赖:
npm install
# 运行方式:
# 1.运行完整演示:
npm start
# 或
node main.js
# 2.单独运行SOCKS5演示:
npm run socks5
# 3.单独运行HTTP演示:
npm run http
# 4.运行测试工具:
npm run test