此处是Internet网络新手。我正在尝试使用'socks'npm模块通过袜子代理向Java HTTPS服务器发送来自Node JS服务器的https请求。我可以在没有套接字限制的情况下通过代理发出单独的https请求,例如使用'socks5-https-client'npm模块。
我希望我的请求返回200 OK响应,但是Java服务器会抛出SSLException:无法识别的SSL消息,纯文本连接错误。
我的Node JS代码非常简单:
const options = {
proxy: {
host: '<proxy ip>',
port: 1080,
type: 5
},
destination: {
host: '<java server ip>',
port: 443
},
command: 'connect'
};
try {
const info = await SocksClient.createConnection(options);
info.socket.write('GET /users HTTP/1.1\nAuthorization: Basic <token>\n\n');
info.socket.on('data', (data) => {
console.log(data.toString());
} catch (err) {
// Handle errors
}
Java服务器代码为:
SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLServerSocket listenSocket = (SSLServerSocket) ssf.createServerSocket(portNum, 50);
boolean done = false;
while (!done) {
clientSocket = listenSocket.accept();
if (Thread.currentThread().isInterrupted()) {
Logger.logMessage("interrupted during accept()");
done = true;
break;
}
InputStream in = clientSocket.getInputStream();
// Read the initial request line from the client
ByteArrayOutputStream inLineA = new ByteArrayOutputStream(100);
int ch = -1;
try {
clientSocket.setSoTimeout(10000);
while ((ch = in.read()) != '\n' && ch != -1)
inLineA.write((byte) ch);
}
catch (Exception e) {
e.printStackTrace(System.out);
clientSocket.close();
continue;
}
[ ... process HTTP request here ]
}
经过大量的搜索,我认为问题是因为'socks'模块使用了不安全的net.Socket。但是,当我将其切换为tls.TLSSocket时,会导致代理连接超时。
如何通过代理连接套接字并发送https请求?我的想法有问题吗?
答案 0 :(得分:0)
非常感谢dave_thompson_085。在将TCP套接字包装在TLS套接字中后,它像下面这样工作:
Socks.SocksClient.createConnection(options)
.then(info => {
const tlsSocket= tls.connect(
{
host: <java server ip>,
port: 443,
socket: info.socket
},
() => {
tlsSocket.write('GET /users HTTP/1.1\r\nAuthorization: Basic <token>\r\nHost: <java server ip>:443\r\n\r\n');
}
);
tlsSocket.on("data", function(data) {
// do something with data
});
})
.catch(err => {
console.log(err);
});