现在有这个场景,(勿随意改动需求) 1、我自己的PC在某个局域网A内,pc浏览器 访问远程公网服务器 server,其端口8080的web服务; 2、此公网server的所有数据包是从某一个固定的局域网B中的一台PC(client)取得的 (有点变态,现实有这么个需求), 3、client 的数据是从180.96.86.192:80取得的; 现在希望公网server 8080端口提供web服务,访问的就是180.96.86.192:80的web服务。 请问如何实现? 我这里写了代码了,就是调不通,请哪位大神指点下
<code>
var net = require(‘net’); var socketList={};
var proxyScoket; var proxyserver=net.createServer(function(socket1){ proxyScoket=socket1; console.log(“客户端已连接”); socket1.on(“close”,function(){ delete proxyScoket; });
var httpserver=net.createServer(function(socket2){
if(proxyScoket){
socket2.pipe(proxyScoket);
proxyScoket.pipe(socket2);
console.log("pipe...");
}
else{
console.log("proxyScoket 不存在");
}
socket2.on("close",function(){
console.log("close");
});
});
httpserver.listen(8080);
console.log(8080);
console.log("httpserver 已建立,等待连接...")
});
proxyserver.listen(8081); console.log(8081); console.log(“远程代理通道已建立,等待客户端代理连接中…”); </code>
server.js的代码为:
client.js的代为
<code> var net = require(‘net’);
var HOST1 = ‘180.96.86.192’; //访问腾讯 www.qq.com var PORT1 = 80;
var client1 = new net.Socket(); client1.connect(PORT1, HOST1, function() { console.log('client1 CONNECTED TO: ’ + HOST1 + ‘:’ + PORT1); var HOST2 = ‘192.168.12.130’; //www.qq.com var PORT2 = 8081; //连接远程端口,目的是将从网站获取的数据,交给远程,然后由远程来发布web服务; var client2 = new net.Socket(); client2.connect(PORT2, HOST2, function() { console.log('client2 CONNECTED TO: ’ + HOST2 + ‘:’ + PORT2); // 建立连接后立即向服务器发送数据,服务器将收到这些数据 client1.pipe(client2); client2.pipe(client1); });
client2.on("error",function(){
console.log("client2 error");
});
client2.on("close",function(){
console.log("client2 close");
delete(client2);
if(client1){client1.end();}
});
});
client1.on(“error”,function(){ console.log(“client1 error”); });
client1.on(“close”,function(){ console.log(“client1 close”); delete(client1); if(client2){client2.end();} });
process.title = ‘客户端’; </code>
就是没有效果,怎么办?