我正在研究nodejs中的代理。我遇到了令我震惊的事情。在http.request
连接的选项之一中,源代码将此显示为选项对象
const options = {
port: 1337,
host: '127.0.0.1',
method: 'CONNECT',
path: 'www.google.com:80'
};
这是整个隧道系统更大的代码的一部分。但是有人能解释一下以上选项如何工作吗?整个代码如下
const http = require('http');
const net = require('net');
const { URL } = require('url');
// Create an HTTP tunneling proxy
const proxy = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('okay');
});
proxy.on('connect', (req, clientSocket, head) => {
// Connect to an origin server
const { port, hostname } = new URL(`http://${req.url}`);
const serverSocket = net.connect(port || 80, hostname, () => {
clientSocket.write('HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node.js-Proxy\r\n' +
'\r\n');
serverSocket.write(head);
serverSocket.pipe(clientSocket);
clientSocket.pipe(serverSocket);
});
});
// Now that proxy is running
proxy.listen(1337, '127.0.0.1', () => {
// Make a request to a tunneling proxy
const options = {
port: 1337,
host: '127.0.0.1',
method: 'CONNECT',
path: 'www.google.com:80'
};
const req = http.request(options);
req.end();
req.on('connect', (res, socket, head) => {
console.log('got connected!');
// Make a request over an HTTP tunnel
socket.write('GET / HTTP/1.1\r\n' +
'Host: www.google.com:80\r\n' +
'Connection: close\r\n' +
'\r\n');
socket.on('data', (chunk) => {
console.log(chunk.toString());
});
socket.on('end', () => {
proxy.close();
});
});
});
您可能从未使用过需要配置HTTP代理的网络。如今,大多数网络将其防火墙配置为允许HTTP通信。这意味着这几天大多数人都不需要使用HTTP代理访问网络。
很久以前,当我第一次使用Internet(大约在1994年)时,许多网络都不允许透明的Internet访问。 您的PC与外界没有任何连接。但是系统管理员会安装一个可以连接的HTTP代理。您的PC仅可以访问LAN(代理是其中的一部分),并且仅HTTP代理可以访问Internet。
这是您如何配置Windows以使用HTTP代理的示例:
如果您如上所述配置PC,则当您连接到www.google.com
时,浏览器将连接到端口proxy.example.com
上的主机8080
,然后请求其从www.google.com
中获取数据。
关于为什么调用请求的资源path
的原因是因为它是在数据包的“路径”部分中发送的。
例如,获取该页面的常规GET请求看起来像这样:
GET /questions/60498963 HTTP/1.1
Host: stackoverflow.com
以及GET之后和协议版本之前的字符串通常称为路径:
.---------------- this is normally called
| the "path"
v
GET /questions/60498963 HTTP/1.1
Host: stackoverflow.com
[发出代理请求时,HTTP标头看起来像这样:
CONNECT stackoverflow.com/questions/60498963 HTTP/1.1
因此,包含域名的URL将在通常用于发送文件路径的数据包部分中发送给代理。
请注意,所有这些都与Node.js无关。这只是基础网络(不涉及编程语言)。