代理服务器访问其他网址

169 阅读1分钟

http/https

const http = require('http');
const urlToAccess = 'http://ip123.in/ip.json'; // 要访问的URL

// 代理服务器的配置
const proxy = {
  host: 'gate.rola-ip.co', // 代理服务器的主机
  port: 1000, // 代理服务器的端口
  auth: 'rolalm_211-country-us:rolalm', // 如果代理服务器需要用户名和密码认证
  protocol: 'https:' // 代理服务器的协议,可以是http或https
};

// 发送HTTP请求到代理服务器
const options = {
  method: 'GET',
  host: proxy.host,
  port: proxy.port,
  path: urlToAccess,
  headers: {
    Host: new URL(urlToAccess).host
  },
  agent: false
};

const req = http.request(options, (res) => {
  let data = '';
  res.on('data', (chunk) => {
    data += chunk;
  });
  res.on('end', () => {
    console.log(data); // 输出从URL获取的数据
  });
});

// 如果代理服务器需要认证,则设置认证信息
if (proxy.auth) {
  req.setHeader('Proxy-Authorization', 'Basic ' + Buffer.from(proxy.auth).toString('base64'));
}

req.end();

Scoks5

const { SocksProxyAgent } = require('socks-proxy-agent');
const http = require('http');

const urlToAccess = 'http://ip123.in/ip.json'; // 要访问的URL

// Socks5代理服务器的配置
const proxy = {
  host: 'gate.rola-ip.co', // 代理服务器的主机
  port: 2000, // 代理服务器的端口
  userId: 'rolalm_211-country-us', // 如果代理服务器需要用户名和密码认证
  password: 'rolalm'
};

// 创建Socks5代理代理对象
const agent = new SocksProxyAgent(`socks5://${proxy.userId}:${proxy.password}@${proxy.host}:${proxy.port}`);

// 发送HTTP请求到代理服务器
http.get({
  agent,
  headers: {
    Host: new URL(urlToAccess).host
  },
  path: urlToAccess
}, (res) => {
  let data = '';
  res.on('data', (chunk) => {
    data += chunk;
  });
  res.on('end', () => {
    console.log(data); // 输出从URL获取的数据
  });
});