列出proxy的常用模块和用法
时间: 2023-05-25 11:04:59 浏览: 90
常用模块:
1. http-proxy:用于创建 HTTP 和 HTTPS 代理服务器。
2. https-proxy-agent:用于将 HTTPS 代理添加到 Node.js 的 http.Agent。
3. socks-proxy-agent:用于将 SOCKS 代理添加到 Node.js 的 http.Agent。
4. socks5-https-client:用于使用 SOCKS5 代理发送 HTTPS 请求。
5. net:用于创建 TCP 代理服务器。
常用用法:
1. 创建一个简单的 HTTP 代理服务器:
```js
const http = require('http');
const httpProxy = require('http-proxy');
const proxy = httpProxy.createProxyServer({});
http.createServer((req, res) => {
proxy.web(req, res, {
target: 'http://localhost:3000'
});
}).listen(8080);
```
2. 创建一个 HTTPS 代理服务器:
```js
const https = require('https');
const httpsProxy = require('http-proxy');
const proxy = httpsProxy.createProxyServer({
ssl: {
key: fs.readFileSync('key.pem', 'utf8'),
cert: fs.readFileSync('cert.pem', 'utf8')
}
});
https.createServer({
key: fs.readFileSync('key.pem', 'utf8'),
cert: fs.readFileSync('cert.pem', 'utf8')
}, (req, res) => {
proxy.web(req, res, {
target: 'https://localhost:3000',
secure: false
});
}).listen(8443);
```
3. 创建一个 TCP 代理服务器:
```js
const net = require('net');
const netProxy = require('net-proxy');
const server = net.createServer((socket) => {
const proxy = netProxy.connect({
port: 80,
host: 'google.com'
});
socket.pipe(proxy).pipe(socket);
});
server.listen(8080);
```
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)