는
당신이 HTTP를 사용하여 프록시에 연결하려면이를 참조하십시오, 당신은 연결 방법을 사용할 필요가 중요
const options = {
port: 8080,
hostname: '10.7.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!');
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();
}
추가 정보 (필요하지)
다음은 프록시를 만들고 요청을 만드는 방법에 대해 here에서 발견 된 전체 코드입니다. 그것은 기본적으로 서버를 만들고 그 위에 요청합니다.
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, cltSocket, head) => {
// connect to an origin server
const srvUrl = url.parse(`http://${req.url}`);
const srvSocket = net.connect(srvUrl.port, srvUrl.hostname,() => {
cltSocket.write('HTTP/1.1 200 Connection Established\r\n' +
'Proxy-agent: Node.js-Proxy\r\n' +
'\r\n');
srvSocket.write(head);
srvSocket.pipe(cltSocket);
cltSocket.pipe(srvSocket);
});
});
// now that proxy is running
proxy.listen(1337, '127.0.0.1',() => {
// make a request to a tunneling proxy
const options = {
port: 1337,
hostname: '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.createServer()
를 통해 서버가 다음
proxy.listen()
와 듣기 시작 만드는
, 그것은 HTTP의 connect
방법을 사용하여 연결하고 소켓 간단한 GET 요청에 기록하고, 그것이 구글로 전송 지정된 경로.
업데이트를보고 나에게 알려주십시오 :) @Daksh – turmuka