node.js - 在 Node JS 中发送低级原始 HTTP/HTTPS 请求

标签 node.js sockets http ssl https

我正在编写一个拦截代理工具,如Burpsuite用于安全测试。其中一个重要部分是发送格式错误的 HTTP 请求,在这种情况下,我们必须让用户完全控制请求!
所以,我在使用库时无法完全控制!我需要能够向目标主机发送原始 HTTP 请求,例如,

GET / HTTP/1.1
Host: google.com
我的尝试:-
我尝试使用 Node JS net module ,并且我能够连接到端口 80 (HTTP) 上的主机,并且在连接到端口 443 (HTTPS) 时,建立了连接但返回一个空响应!
在一些研究中,我发现这与 SSL 有关,因为我尝试了 telnet,但它对于 HTTPS 连接也失败了,并查看了一些 stackoverflow 答案!
是否有任何选项可以直接从我的 Node 应用程序直接发送原始 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。

关于node.js - 在 Node JS 中发送低级原始 HTTP/HTTPS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66842858/

相关文章:

.net - TCP/IP、UDP、Socket编程可以问的面试问题?

http - 使用域设置 cookie

http - 登录失败时返回正确的状态码

javascript - 关于自己的 REST API 和使用 3rd 方 API 获取的问题

javascript - 对于在 NodeJS 中遍历数组但 console.log 不会打印到终端的循环?

javascript - 如何使用 SSL 将 haproxy 放在 socket.io 前面?获取混合内容警告

c# - 什么是最好的?轮询套接字或等待 Web 服务超时?

angular - 在 Ionic 4 应用程序中使用 @ionic-native/http

node.js - NodeJS Express Rest API APP 融入 Angular 5 项目

Node.js 后台任务