This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 134
Add support for sending events through proxy #397
Open
ndmanvar
wants to merge
6
commits into
getsentry:master
Choose a base branch
from
ndmanvar:add_proxy_support
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6cce30b
Add support for sending events through proxy
ndmanvar de6da9e
Add proxyAuth (ability to specify user/password for proxy)
ndmanvar 9a8a4c2
remove require buffer
ndmanvar 8abbf65
Add initial test
ndmanvar 1bc3de5
Make change to this.options and fix unit tests
ndmanvar 33fabb3
Merge branch 'master' into add_proxy_support
ndmanvar 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
Next
Next commit
Add support for sending events through proxy
For both HTTP and HTTPS endpoints. Uses tunnel-agent for https.
- Loading branch information
commit 6cce30b3eb27be9bfdb6a3c9ee814d4613e2b227
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,12 @@ var timeoutReq = require('timed-out'); | |
|
||
var http = require('http'); | ||
var https = require('https'); | ||
var tunnel = require('tunnel-agent'); | ||
|
||
var agentOptions = {keepAlive: true, maxSockets: 100}; | ||
var agentOptions = { | ||
keepAlive: true, | ||
maxSockets: 100 | ||
}; | ||
var httpAgent = new http.Agent(agentOptions); | ||
var httpsAgent = new https.Agent(agentOptions); | ||
|
||
|
@@ -21,7 +25,7 @@ function HTTPTransport(options) { | |
this.agent = httpAgent; | ||
} | ||
util.inherits(HTTPTransport, Transport); | ||
HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) { | ||
HTTPTransport.prototype.send = function (client, message, headers, eventId, cb) { | ||
var options = { | ||
hostname: client.dsn.host, | ||
path: client.dsn.path + 'api/' + client.dsn.project_id + '/store/', | ||
|
@@ -31,22 +35,39 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) { | |
ca: client.ca, | ||
agent: this.agent | ||
}; | ||
|
||
// set path apprpriately when using http endpoint + proxy, set proxy headers appropriately when using https endpoint + proxy | ||
if (this.options.hasOwnProperty('proxyHost')) { | ||
if (client.dsn.protocol === 'http') { | ||
options.path = 'https://' + client.dsn.host + ':' + client.dsn.port + client.dsn.path + 'api/' + client.dsn.project_id + '/store/'; | ||
delete options.hostname; // only 'host' should be set when using proxy | ||
} else { | ||
this.options.agent.proxyOptions.headers = { | ||
'Content-Type': 'application/octet-stream', | ||
host: client.dsn.host + ':' + client.dsn.port | ||
} | ||
} | ||
} | ||
|
||
for (var key in this.options) { | ||
if (this.options.hasOwnProperty(key)) { | ||
options[key] = this.options[key]; | ||
} | ||
} | ||
|
||
// prevent off heap memory explosion | ||
var _name = this.agent.getName({host: client.dsn.host, port: client.dsn.port}); | ||
var _name = this.agent.getName({ | ||
host: client.dsn.host, | ||
port: client.dsn.port | ||
}); | ||
var _requests = this.agent.requests[_name]; | ||
if (_requests && Object.keys(_requests).length > client.maxReqQueueCount) { | ||
// other feedback strategy | ||
client.emit('error', new Error('client req queue is full..')); | ||
return; | ||
} | ||
|
||
var req = this.transport.request(options, function(res) { | ||
var req = this.transport.request(options, function (res) { | ||
res.setEncoding('utf8'); | ||
if (res.statusCode >= 200 && res.statusCode < 300) { | ||
client.emit('logged', eventId); | ||
|
@@ -63,17 +84,16 @@ HTTPTransport.prototype.send = function(client, message, headers, eventId, cb) { | |
client.emit('error', e); | ||
cb && cb(e); | ||
} | ||
|
||
// force the socket to drain | ||
var noop = function() {}; | ||
var noop = function () {}; | ||
res.on('data', noop); | ||
res.on('end', noop); | ||
}); | ||
|
||
timeoutReq(req, client.sendTimeout * 1000); | ||
|
||
var cbFired = false; | ||
req.on('error', function(e) { | ||
req.on('error', function (e) { | ||
client.emit('error', e); | ||
if (!cbFired) { | ||
cb && cb(e); | ||
|
@@ -89,10 +109,40 @@ function HTTPSTransport(options) { | |
this.options = options || {}; | ||
this.agent = httpsAgent; | ||
} | ||
|
||
function HTTPProxyTransport(options) { | ||
this.defaultPort = 80; | ||
this.transport = http; | ||
this.options = options || {}; | ||
this.agent = httpAgent; | ||
this.options.host = options.proxyHost; | ||
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. How about just using 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. Or it may be useful to distinguish |
||
this.options.port = options.proxyPort; | ||
} | ||
|
||
function HTTPSProxyTransport(options) { | ||
this.transport = https; | ||
this.options = options || {}; | ||
this.agent = httpsAgent; | ||
this.options.agent = tunnel.httpsOverHttp({ | ||
proxy: { | ||
host: options.proxyHost, | ||
port: options.proxyPort, | ||
proxyAuth: null // TODO: Add ability to specify creds/auth | ||
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. will take care of this (for |
||
}, | ||
keepAlive: agentOptions.keepAlive, | ||
maxSockets: agentOptions.maxSockets | ||
}); | ||
} | ||
|
||
util.inherits(HTTPSTransport, HTTPTransport); | ||
util.inherits(HTTPProxyTransport, HTTPTransport); | ||
util.inherits(HTTPSProxyTransport, HTTPTransport); | ||
|
||
module.exports.http = new HTTPTransport(); | ||
module.exports.https = new HTTPSTransport(); | ||
module.exports.Transport = Transport; | ||
module.exports.HTTPTransport = HTTPTransport; | ||
module.exports.HTTPSTransport = HTTPSTransport; | ||
|
||
module.exports.HTTPProxyTransport = HTTPProxyTransport; | ||
module.exports.HTTPSProxyTransport = HTTPSProxyTransport; |
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
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.
slightly better comment here for what will happen if you send hostname or a link to a resource that justifies why?