-
Notifications
You must be signed in to change notification settings - Fork 149
fixed traffic stats for http connection reuse #572
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
Conversation
Hello @peonone, |
Hey @peonone, |
Just pushed the fix for HTTPS, as the same issue also happens to HTTPS requests, the It can be reproduced by the below curl commands curl requestcurl -x http://localhost:6001/ http://httpbin.org/flasgger_static/lib/jquery.min.js > /dev/null
curl -x http://localhost:6001/ https://httpbin.org/ip > /dev/null server output
The traffic stats for the second request includes the bytes for the first request. |
bbe7cee
to
03df862
Compare
Thank you for this, great job. |
Thnaks @jirimoravcik , it looks good. |
This PR attempts to fix incorrect stats due to the reuse of target sockets for HTTP(S) protocols. Based on #572 Note: I was forced to upgrade `actions/cache` as `v2` was deprecated and it wouldn't run with it. I also had to edit eslint config to run with `.` instead of `src` and excluded `tests`, because with `src` the CI was failing (no idea why).
This has been released in |
Description of the issue
For plain HTTP requests using the
forward
method, it establishes an HTTP connection to the upstream proxy (or target website if no upstream proxy is specified) using the HTTP client libraryhttp.request
.So here the
target
is the TCP connection created or reused by thehttp.request
call.It relies on
target.once('close')
,target.bytesRead
andtarget.bytesWritten
for traffic stats, but it does not work well for certain cases, because the TCP connection can be reused, but it's not considered for the traffic stats logic.Environment to reproduce the issue
proxy-chain version: 2.5.6
Let's use two URLs:
Node.js server script
case 1 -
target
socket reused by multiple requests is closed before thesource
is closedClient python script to reproduce
server output
The
trgRxBytes
andtrgTxBytes
are doubled, because it registerssocket.once('close')
for every request, and when it's triggered, the 2 requests are finished using the sametarget
socket, and the traffic of two requests is counted twice.case 2 -
target
socket reused by multiple requests using differentsource
Client curl commands to reproduce
server output
The
trgRxBytes
andtrgTxBytes
for the second request include the bytes from the first request, becausetarget
is reused andtarget.bytesRead
includes the bytes from the first request.The fix
The fix is to track the number of bytes which is already there, when a socket is being allocated to an HTTP request, reduce it when calculating the stats, and use
response.once('close')
instead oftarget.once('close')
to track the stats in the request life cycle.