-
-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timeout for request library #58
Comments
Hey! What are you trying to do? Do you intend for the first request to go through to ipify.org or do you want to intercept that one? What about the second one? Trying to test how your code handles request timeouts or trying to just fake the remote API for your code? |
Hello Andri, I wanted to keep the example simple for you, but will explain below a bit more in detail what I want to achieve. I developed some time ago a node for the Node-RED open source IOT framework. My block (A) listens to all http trafic (e.g. triggered by another httprequest block (B)), and if a request/response is intercepted an output messages is generated on my node's output (containing both the request AND the corresponding response): I have been using the global-request-logger to accomplish this. However that library doesn't intercept the requests anymore on more recent NodeJs (10.xx), when those requests are generated by the "request" library. Therefore I wanted to use your module from now on... So in the test program that I have provided, I just wanted to demonstrate that the first request works fine. But the second one doesn't work anymore, because I activated mitm: I arrive in the mitm handler but for some reason the request library doesn't get a response from the server (api.ipify.org) anymore, which results in a timeout error. I had expected that it worked like this:
Do you think something like this could be realized? |
It could certainly be achieved, but Mitm.js out of the box doesn't do any request proxying, which is something you seem to require. Once a request is "intercepted" by Mitm.js, it's handed off to your handler (the Fortunately you're not the first to look for implementing recording. This was briefly talked about in #51. Also, when I skim the dependents of Mitm.js, I see a few modules that portray to implement request forwarding — https://www.npmjs.com/browse/depended/mitm. Maybe they'll inspire your implementation. I've never needed recording myself, but I still presume it should be doable by just getting the hostname from the Let me know how it goes and if you need help. |
Andri, really appreciate your assistance! Based on the links you have provided, and have updated my example program:
I would have expected it to work like this:
But seems I made a huge mistake somewhere, because I'm arriving in my handlers much more as expected:
Do you have any clue what is going wrong? |
Hey, I have a feeling that some of the Here's a full example that seems to work with Request v2.60 at least: var Http = require("http")
var Https = require("https")
var Mitm = require("mitm")
var request = require("request")
var mitm = Mitm()
mitm.on("connect", function(socket, opts) {
if (opts.proxying) socket.bypass()
})
mitm.on("request", function(req, res) {
(req.connection.encrypted ? Https : Http).get({
protocol: req.connection.encrypted ? "https:" : "http:",
host: req.headers.host,
path: req.url,
proxying: true
}, function(newRes) {
res.writeHead(newRes.statusCode, newRes.headers)
newRes.pipe(res)
})
})
request({
uri: "https://api.ipify.org?format=json",
timeout: 3000
}, function(_err, _res, body) {
console.log(body)
}) Note that there's no need for semicolons in JavaScript. :P If you want the above to work for other methods besides |
P.S. Yes I know that semicolons are not required in Javascript. But I'm more a Java fan, so I want to have a bit of Java in my Javascript code ;-) |
The same code above ends in an endless loop? I don't have Node v8 at hand, but would you mind trying it with Request v2.6 for a sec to see if it's something in Request? |
Ah, nevermind, you're saying the plain old |
Same with Node v10.15.3 — works. |
How are you running the test program on Ubuntu? From the command line with Is Lines 95 to 96 in 159e1c9
If you clone this repository or install Mitm.js via NPM and run its tests on your Ubuntu machine, do they pass? When you go Mitm's folder, you can install its test dependencies with |
Evening Andri, Have been debugging quite a lot, with two debug sessions beside each other to compare my Linux and Windows sessions. Until the black line the entire call stack is the same, but then in Linux I'm ending up with the infinite loop (since client.bypassed becomes undefined again): So the "orig" is different on both systems, and then it goes wrong. |
Ah, if IF we're suspecting that something is initializing Mitm twice, we need to check that. Start by confirming what var Tls = require("tls")
console.log(Tls.connect.toString()) If that looks like something from Node.js internals, that's good. If after initializing it won't print its output, then that's good, too:
To confirm whether Mitm is initialized twice, you could instrument its constructor (the |
Hey Andri, OMG... In the linux version I had copied some console statements from my original program, and seems indeed I had copied also a second Now it seems to be working fine. My apologies for wasting your precious time ;-( Thanks again !!!!!! |
Hehe, glad we figured it out at the end. :) |
Hi,
I'm using this nice library successfully, except in combination with the popular request library.
Below you can find a simple test program:
Which results in the following:
Am I perhaps using the mitm library incorrectly somehow? When I insert
res.end()
in my mitm handler, then there is no timeout but the request library gets of course an empty response.P.S. I did my test on NodeJs version 8.10.0
Thanks for your time !!!
Bart
The text was updated successfully, but these errors were encountered: