node request 使用代理及长连接

一、http、https

(1)、利用 forever: true,实现长连接
const request = require('request')
const util = require('util')

// proxy配置
const agentConfig = {
    ip: '106.22.22.222',
    port: '808'
}
const proxy = util.format('http://%s:%d', agentConfig.ip, agentConfig.port);

request({
    method: 'post',
    url: 'http://127.0.0.1:3000/api/logins',
    proxy,
    forever: true, // 长链接
}, (error, response, body) => {
    console.log(body)
})
 
(2)、利用 hpagent 依赖的 keepAlive 属性,实现长连接
const request = require('request');
const { HttpProxyAgent, HttpsProxyAgent } = require('hpagent');
const util = require('util');
// proxy配置
const agentConfig = {
    ip: '106.22.22.222',
    port: '808'
}
// 请求地址
const url = 'http://127.0.0.1:3000/api/logins'
const isHttps = (new URL(url)).protocol === 'https:'

const proxy = util.format('http://%s:%d', agentConfig.ip, agentConfig.port);
// http proxy
const httpAgent = new HttpProxyAgent({
    keepAlive: true, // 配置这个长链接
    keepAliveMsecs: 5000,
    maxSockets: 256,
    maxFreeSockets: 256,
    scheduling: 'lifo',
    proxy
})
// https proxy
const httpsAgent = new HttpsProxyAgent({
    keepAlive: true,
    keepAliveMsecs: 5000,
    maxSockets: 256,
    maxFreeSockets: 256,
    scheduling: 'lifo',
    proxy
})
request({
    method: 'post',
    url: 'http://127.0.0.1:3000/api/logins',
    agent: isHttps ? httpsAgent : httpAgent
}, (error, response, body) => {
    console.log(body)
})

二、socks5

(1)、第一种

const request = require('request')
const httpAgent = require('socks5-http-client/lib/Agent')
const httpsAgent = require('socks5-https-client/lib/Agent')

// 请求地址
const url = 'http://127.0.0.1:3000/api/logins'
const isHttps = (new URL(url)).protocol === 'https:'
// proxy配置
const socks5Config = {
    ip: '106.22.22.222',
    port: '1080',
    socksUsername: 'guest', // 账号
    socksUsername: '123456' // 密码
}

request({
    method: 'post',
    url,
    agentClass: isHttps ? httpsAgent : httpAgent,
    agentOptions: {
        socksHost: socks5Config.ip,
        socksPort: socks5Config.port,
        socksUsername: socks5Config.socksUsername,
        socksPassword: socks5Config.socksPassword
    }
}, (error, response, body) => {
    console.log(body)
})
(2)、第二种(同时支持socks5、http、https代理)
const request = require('request');
const { SocksProxyAgent } = require('socks-proxy-agent')

// proxy配置
const agent = new SocksProxyAgent({
    host: '106.22.22.222',
    port:'1080',
    userId: 'guest',
    password: '123456'
})

request({
    method: 'post',
    url: 'http://127.0.0.1:3000/api/logins',
    agent
}, (error, response, body) => {
    console.log(body)
})

⚠️长连接:

1、request依赖中,长链接仅同步下生效,for循环并行不生效

2、socks5测试无法实现长链接,每次请求将重新握手;大神们若有解决方案,还请吝啬留言交流!!

到这里就大工告成了,有用的话麻烦关注收藏一下🌹🌹🌹~

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NS3是一个广泛使用的网络仿真工具,它可以用来研究和评估各种网络协议和应用程序的性能。如果您想在NS3中实现代理服务器,可以按照以下步骤进行: 1. 安装NS3:首先需要安装NS3仿真工具,可以从官方网站(https://www.nsnam.org/)下载最新版本。 2. 创建仿真场景:使用NS3创建一个仿真场景,包括代理服务器、客户端和服务器。可以使用NS3自带的TCP/IP协议栈或其他协议栈。 3. 实现代理服务器:使用NS3提供的Socket API实现代理服务器。代理服务器需要监听来自客户端的请求,并将请求转发给服务器。可以使用C++或Python编写代理服务器代码。 4. 实现客户端和服务器:使用NS3提供的Socket API实现客户端和服务器。客户端发送请求到代理服务器,代理服务器将请求转发给服务器,服务器返回响应到代理服务器,代理服务器将响应转发给客户端。 5. 运行仿真:使用NS3运行仿真场景,并记录性能指标,例如延迟、吞吐量和丢包率等。 下面是一个简单的NS3代理服务器示例代码: ``` #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/applications-module.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("ProxyServer"); int main (int argc, char *argv[]) { // 创建节点 NodeContainer nodes; nodes.Create (3); // 安装TCP/IP协议栈 InternetStackHelper stack; stack.Install (nodes); // 创建代理服务器 Ptr<Node> proxyServer = nodes.Get (0); Ptr<Socket> proxySocket = Socket::CreateSocket (proxyServer, TcpSocketFactory::GetTypeId ()); proxySocket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 8080)); proxySocket->Listen (); // 创建服务器 Ptr<Node> server = nodes.Get (1); Ptr<Socket> serverSocket = Socket::CreateSocket (server, TcpSocketFactory::GetTypeId ()); serverSocket->Bind (InetSocketAddress (Ipv4Address::GetAny (), 80)); serverSocket->Listen (); // 创建客户端 Ptr<Node> client = nodes.Get (2); Ptr<Socket> clientSocket = Socket::CreateSocket (client, TcpSocketFactory::GetTypeId ()); // 连接代理服务器 clientSocket->Connect (InetSocketAddress (proxyServer->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal (), 8080)); // 发送请求 std::string request = "GET /index.html HTTP/1.1\r\nHost: www.example.com\r\n\r\n"; clientSocket->Send (reinterpret_cast<const uint8_t*> (request.c_str ()), request.length ()); // 接收响应 uint8_t buffer[1024]; uint32_t size = clientSocket->Recv (buffer, sizeof (buffer), 0); std::string response (reinterpret_cast<const char*> (buffer), size); // 打印响应 NS_LOG_INFO ("Received response: " << response); // 停止仿真 Simulator::Stop (Seconds (1.0)); Simulator::Run (); Simulator::Destroy (); return 0; } ``` 注意:这只是一个简单的示例代码,实际使用中需要根据具体情况进行修改和完善。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值