在节点 JS 中发送低级原始 HTTP/HTTPS 请求

Sending low level raw HTTP/HTTPS requests in node JS

我正在编写一个像Burpsuite这样的拦截代理工具来进行安全测试。其中一个重要部分是发送格式错误的 HTTP 请求,在这种情况下我们必须让用户完全控制请求!

所以,我在使用库时无法完全控制!我需要能够将原始 HTTP 请求发送到目标主机,例如

GET / HTTP/1.1
Host: google.com

我的尝试:-

我尝试使用节点 JS net module,我能够连接到端口 80 上的主机 (HTTP),同时连接到端口 443 (HTTPS),已建立连接,但 returns 为空响应!

在一些研究中,我发现这与 SSL 有关,因为我尝试了 telnet,但 HTTPS 连接也失败了,并查看了一些 Whosebug 答案!

是否有任何选项可以让我直接从我的节点应用程序直接发送原始 HTTP/HTTPS 请求?

谢谢!

有一个模块 http-tag,它允许编写像 -

这样的文字 http 消息
const net = require('net')
const HTTPTag = require('http-tag')

const socket = net.createConnection({
    host: 'localhost',
    port: 8000,
}, () => {
    // This callback is run once, when socket connected

    // Instead of manually writing like this:
    // socket.write('GET / HTTP/1.1\r\n')
    // socket.write('My-Custom-Header: Header1\r\n\r\n')
    
    // You will be able to write your request(or response) like this:
    const xHeader = 'Header1' // here in the epressions you can pass any characters you want
    socket.write(
        HTTPTag`
        GET / HTTP/1.1
        My-Custom-Header: ${xHeader}
        
        `
    )
    socket.end()
})

socket.on('close', hasError => console.log(`Socket Closed, hasError: ${hasError}`))

// set readable stream encoding
socket.setEncoding('utf-8')
socket.on('data', data => console.log(data))

关于TLS,目前正在研究内置node模块,还没有查看tls