-
Notifications
You must be signed in to change notification settings - Fork 21
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
Support HTTPS proxying #5
Open
RickyRomero
wants to merge
7
commits into
Stuk:master
Choose a base branch
from
RickyRomero:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7fc5410
Support SSL proxying
RickyRomero 8beb184
Match semicolon style
RickyRomero fd83242
Readme comment no longer accurate
RickyRomero 4002ca4
Make snakeoil SSL files relative to node module
RickyRomero c4d3950
Gracefully handle connection reset errors on the proxy
RickyRomero 03f1726
Better Windows compatibility, more descriptive variable naming
RickyRomero 16c27f6
Randomly assign internal proxying ports if none specified
RickyRomero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,17 +15,71 @@ | |
*/ | ||
|
||
var _fs = require("fs"); | ||
var net = require("net"); | ||
var http = require("http"); | ||
var https = require("https"); | ||
var URL = require("url"); | ||
var PATH = require("path"); | ||
var mime = require("mime"); | ||
var heuristic = require("./heuristic"); | ||
|
||
exports = module.exports = serverReplay; | ||
function serverReplay(har, options) { | ||
var server = http.createServer(makeRequestListener(har.log.entries, options)); | ||
var fs = options.fs || _fs; | ||
if (!options.ssl) { | ||
options.ssl = { | ||
key: __dirname + "/ssl/snakeoil.key", | ||
cert: __dirname + "/ssl/snakeoil.crt" | ||
}; | ||
} | ||
|
||
options.ssl.key = fs.readFileSync(options.ssl.key); | ||
options.ssl.cert = fs.readFileSync(options.ssl.cert); | ||
|
||
var rl = makeRequestListener(har.log.entries, options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind renaming this |
||
var internalProxy = net.createServer(chooseProtocol.bind(this, options)); | ||
var httpServer = http.createServer(rl); | ||
var httpsServer = https.createServer(options.ssl, rl); | ||
|
||
internalProxy.listen(options.port); | ||
httpServer.listen(options.port + 1); | ||
httpsServer.listen(options.port + 2); | ||
} | ||
|
||
function chooseProtocol(options, connection) { | ||
connection.on("error", handleProxyError); | ||
connection.once("data", function (buf) { | ||
var intent = buf.toString().split("\r\n")[0]; | ||
var destPort = options.port + 1; | ||
|
||
if (/^CONNECT .+?:443 HTTP\/\d(?:\.\d)?$/.test(intent)) { | ||
destPort += 1; | ||
connection.write( | ||
"HTTP/1.1 200 Connection established\r\n" + | ||
"Connection: keep-alive\r\n" + | ||
"Via: HTTP/1.1 server-replay\r\n" + | ||
"\r\n" | ||
); | ||
|
||
connection.once("data", function (buf2) { | ||
bridgeConnection(buf2, connection, destPort); | ||
}); | ||
} else { | ||
bridgeConnection(buf, connection, destPort); | ||
} | ||
}); | ||
} | ||
|
||
function handleProxyError(err) { | ||
console.warn("An error occurred while proxying:"); | ||
console.warn(err.stack); | ||
} | ||
|
||
server.listen(options.port); | ||
function bridgeConnection(buf, connection, destPort) { | ||
var proxy = net.createConnection(destPort, function () { | ||
proxy.write(buf); | ||
connection.pipe(proxy).pipe(connection); | ||
}); | ||
} | ||
|
||
// Export for testing | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIC5TCCAc2gAwIBAgIJAOA+uqpK8B/PMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNV | ||
BAMMCWxvY2FsaG9zdDAeFw0xODAzMTUyMDQ0MjNaFw0xODA0MTQyMDQ0MjNaMBQx | ||
EjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC | ||
ggEBAOYqNJAxQYMJIcJ69zyROZpZfmArNyd+UZxuYPOEJbqjwRHDVOhErz6tm55s | ||
THVaXuGJ7qLsKFCTxW5X4gtYKRlLG5rcbXPQUUDhIg/2cwNe1t4S/Ri9QjaDb7iZ | ||
H7Rcxb4WNWpek2VFRV1010GkLvPkDJhauIReZr2SOCTlaR6zSKV8PcebsXRgPSl0 | ||
Myz4fSYuN3jXH/uYmRySN4O7BV0nLf8Wpvd0sH7X0g5/3OE/kBZUljJSQ7SVnGcW | ||
o+J7toa6K9kFhMf7kZoLk9JDFyew9Bq/0jf/Mo8aiZ3ujdXL/tREbzpMKfBFfJj2 | ||
VqLzYKEtmdD2SaMpCWwt/g5pOokCAwEAAaM6MDgwFAYDVR0RBA0wC4IJbG9jYWxo | ||
b3N0MAsGA1UdDwQEAwIHgDATBgNVHSUEDDAKBggrBgEFBQcDATANBgkqhkiG9w0B | ||
AQsFAAOCAQEAm9MA4VmVIh4RzbQ1Pr+YD/SqnVmSajtT1GjuMIfevSX5XYxFcMfi | ||
or8a5EyZnOfM/NfiEkOVgwSkQwbNg0/yUK1BequbP0NNhQNWJzrBcpNSqBEqgdUU | ||
97u/q06dSonNBMw8OIzZ8aWw+SlwoUSpAxvWgiHe5PH49WfR402MJxPKJqu0mOqU | ||
EwyND9Y4iLvL8Pqu4oSpfNZVwq4RfUcJF1h9lYfVK+dB/+sgsgyLIeSs/DKZhYPz | ||
ib8scXqVQiJGzlQlVHny2ezr8rtQs54DJIbOnEEXCPaN14aPkp7pJKwl/0cm/AGf | ||
9CIvVzw45kVdVoG6zjumFG+kd2AyecZmfQ== | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDmKjSQMUGDCSHC | ||
evc8kTmaWX5gKzcnflGcbmDzhCW6o8ERw1ToRK8+rZuebEx1Wl7hie6i7ChQk8Vu | ||
V+ILWCkZSxua3G1z0FFA4SIP9nMDXtbeEv0YvUI2g2+4mR+0XMW+FjVqXpNlRUVd | ||
dNdBpC7z5AyYWriEXma9kjgk5Wkes0ilfD3Hm7F0YD0pdDMs+H0mLjd41x/7mJkc | ||
kjeDuwVdJy3/Fqb3dLB+19IOf9zhP5AWVJYyUkO0lZxnFqPie7aGuivZBYTH+5Ga | ||
C5PSQxcnsPQav9I3/zKPGomd7o3Vy/7URG86TCnwRXyY9lai82ChLZnQ9kmjKQls | ||
Lf4OaTqJAgMBAAECggEAGOyvHoJG/uKpRj88sNFlNILGfbGQWnWCbvdBBn3j/A8p | ||
pDvL4Q83DwmL1Z8StI6hwbjHH9uFDhzCf42CzAmzAasxhRajv6vqcKUwpBvjHpVR | ||
nWDfCaPNHMwk+A+U8Fovi8Mp66fsPEZBGbrCaLhX4U9r0b/ZRXRXmeXQsKYrOQiq | ||
mY5gfuQCBBVECDnktrYV8YV3ULhqiuZNmz9xrpeLf9H19QRwbbcHkRZLbigP7kaU | ||
8k800rXJVEz0fsOQ+PTOkJSiD7W50BuxWeIStUYaZmzBKbNMvZKeUIzLd1rFnxbj | ||
sT3JNoZPfKSXXwCsyl58apzGROxf+4TXUAQUOk/1kQKBgQD4uScU0Wj2P6xliLUi | ||
MGheOMwSiIm50deZdZQL19JpQE9R8OGL7wwwXOdc2+Mp01xt4cV2qA25T8diQa9w | ||
O20VqDynMt7vbDC46Y0aeOegYpCINCuS3rfqVY8/Yhg/2jArfaZipP1QU05Ah7sG | ||
0gsheLb0PKwJ8iuVUErw5dvpXQKBgQDs5g6aJQl4AFEAg+2HWggbeoRGHii0bVJQ | ||
EnLBp+WBHjKD+DCCJ+sRyRBczAr3k/ueQmexMz24vhvJ1E/feo2CaSRvLI+kHinz | ||
XJOEvDNBgmN+UR4rVWflGBDTjYfZjH1jlXkPNIuO3+AvNFPiTRd43nlIxynumzWN | ||
CiAl2nNHHQKBgQCTWNTvP1PoNjaCfCealoTt9MXo4Nx+qfMI5aAMGBJ96exTxdlI | ||
lhhpelBSMa309FMYgZ0Cu3JN6xZafkFZwsrP/rfX8Yoi2rxOf4XpPeEyodGv7wA1 | ||
ZR4dhAx15z4obbEFws1UORwcfw2nqwFAfCS98o6oSF0/EymArm2HIxVRvQKBgQCW | ||
5oySn9kCOaFfZKofN7hGWKp9R6TCGYj/PGEg/mPw9V1UNvofTnIsaBkmI0sxHXCA | ||
BOisNWmxjleBHt6qChSt52+v6YCuGBC81lGZkZBMwFPEGMPQ8pw1kDjXqSXJ6/XL | ||
Q2FT0DK9ldnl970fP+AdvAkh1MvfE7ru1m5X7mjT+QKBgDbHJjzyA5IBf/+IDQM8 | ||
UfV9z2zReicMuUIAIubhXKlK5Oj69Nu6ziLjpt2sMSFT1OomiRWCpHLLJr1EI49L | ||
XZMsx/Szt7/kCYwQHja2N6RDOmxdZemR+alxAo0cIyG5I6gQLURca4dhSMaBBGMQ | ||
TJaTwEX5zeRz4i04ay8dIowT | ||
-----END PRIVATE KEY----- |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use
path.join
here. I haven't tested on Windows, but nice to keep compat if it does work