Skip to content

Commit

Permalink
Merge pull request #334 from MindscapeHQ/bh/capture-remote-ip
Browse files Browse the repository at this point in the history
Add option to set clients ip
  • Loading branch information
BenjaminHarding committed Jul 7, 2019
2 parents b72486e + 6acb345 commit 6d960c2
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* v2.17.0
- Add inital support for users to set the client IP address

* v2.16.1
- Fix promise rejection errors when trying to retrieve fetch response text in network-tracking module

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ objects (for partial matches). Each should match the hostname or TLD that you wa

`apiEndpoint` - A string URI containing the protocol, domain and port (optional) where all payloads will be sent to. This can be used to proxy payloads to the Raygun API through your own server. When not set this defaults internally to the Raygun API, and for most usages you won't need to set this.

`clientIp` - A string containing the client's IP address. RUM requests will be associated to this IP address when set. Particularally useful when proxying payloads to the Raygun API using the `apiEndpoint` option and maintaining RUM's geographic lookup feature.

`pulseMaxVirtualPageDuration` - The maximum time a virtual page can be considered viewed, in milliseconds (defaults to 30 minutes).

`pulseIgnoreUrlCasing` - Ignore URL casing when sending data to Pulse.
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "raygun4js",
"version": "2.16.1",
"version": "2.17.0",
"homepage": "http://raygun.io",
"authors": [
"Mindscape <hello@raygun.io>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
],
"title": "Raygun4js",
"description": "Raygun.io plugin for JavaScript",
"version": "2.16.1",
"version": "2.17.0",
"homepage": "https://github.com/MindscapeHQ/raygun4js",
"author": {
"name": "MindscapeHQ",
Expand Down
2 changes: 1 addition & 1 deletion raygun4js.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>raygun4js</id>
<version>2.16.1</version>
<version>2.17.0</version>
<title>Raygun4js</title>
<authors>Mindscape Limited</authors>
<owners>Mindscape Limited</owners>
Expand Down
14 changes: 14 additions & 0 deletions src/raygun.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ var raygunFactory = function(window, $, undefined) {
$document,
_captureUnhandledRejections = true,
_setCookieAsSecure = false,
_clientIp,
detachPromiseRejectionFunction;

var rand = Math.random();
Expand Down Expand Up @@ -144,6 +145,10 @@ var raygunFactory = function(window, $, undefined) {
if (options.from) {
_loadedFrom = options.from;
}

if(options.clientIp) {
_clientIp = options.clientIp;
}
}

ensureUser();
Expand Down Expand Up @@ -362,6 +367,11 @@ var raygunFactory = function(window, $, undefined) {
}
}
},

setClientIp: function(ip) {
_clientIp = ip;
},

recordBreadcrumb: function() {
_breadcrumbs.recordBreadcrumb.apply(_breadcrumbs, arguments);
},
Expand Down Expand Up @@ -1019,6 +1029,10 @@ var raygunFactory = function(window, $, undefined) {
var xhr = createCORSRequest('POST', url, data);
if (typeof xhr.setRequestHeader === 'function') {
xhr.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');

if(typeof _clientIp !== "undefined") {
xhr.setRequestHeader('X-Remote-Address', _clientIp);
}
}

if (typeof _beforeXHRCallback === 'function') {
Expand Down
3 changes: 3 additions & 0 deletions src/raygun.loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@
case 'logContentsOfXhrCalls':
rg.setBreadcrumbOption('logXhrContents', pair[1]);
break;
case 'clientIp':
rg.setClientIp(value);
break;
case 'captureUnhandledRejections':
captureUnhandledRejections = value;
break;
Expand Down
30 changes: 28 additions & 2 deletions tests/fixtures/common/instrumentXHRs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,30 @@
window.__requestPayloads = [];
window.__inFlightXHRs = [];
window.__completedXHRs = [];
window.__sentXHRs = [];

var origOpen = XMLHttpRequest.prototype.open;
var origSend = XMLHttpRequest.prototype.send;

var origSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;

XMLHttpRequest.prototype.setRequestHeader = function() {
if(!this.__headers) {
this.__headers = {};
}
this.__headers[arguments[0]] = arguments[1];

origSetRequestHeader.apply(this, arguments);
};

XMLHttpRequest.prototype.getRequestHeader = function() {
if(!this.__headers) {
this.__headers = {};
}

var header = arguments[0];
return this.__headers[header] || null;
};

XMLHttpRequest.prototype.open = function() {

Expand All @@ -29,8 +50,13 @@
window.__requestPayloads.push(json);
}

window.__sentXHRs.push({
xhr: this,
clientIp: this.getRequestHeader('X-Remote-Address')
});

origSend.apply(this, arguments);
}
};

if (window.XDomainRequest) {
var origXOpen = XDomainRequest.prototype.open;
Expand Down Expand Up @@ -58,6 +84,6 @@
window.__requestPayloads.push(JSON.parse(arguments[0]));

origXSend.apply(this, arguments);
}
};
}
})()
27 changes: 27 additions & 0 deletions tests/fixtures/v2/withClientIpSet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

<!DOCTYPE html>
<html>
<head>
<title>Raygun4JS with Client IP</title>
<script src="/fixtures/common/instrumentXHRs.js"></script>
<script type="text/javascript">
!function(a,b,c,d,e,f,g,h){a.RaygunObject=e,a[e]=a[e]||function(){
(a[e].o=a[e].o||[]).push(arguments)},f=b.createElement(c),g=b.getElementsByTagName(c)[0],
f.async=1,f.src=d,g.parentNode.insertBefore(f,g),h=a.onerror,a.onerror=function(b,c,d,f,g){
h&&h(b,c,d,f,g),g||(g=new Error(b)),a[e].q=a[e].q||[],a[e].q.push({
e:g})}}(window,document,"script","/dist/raygun.js","rg4js");
</script>
</head>
<body>
<script type="text/javascript">
rg4js('apiKey', 'abcd==');
rg4js('enablePulse', true);
rg4js('options', {
allowInsecureSubmissions: true,
debugMode: true,
clientIp: '192.168.0.12'
});
</script>

</body>
</html>
26 changes: 26 additions & 0 deletions tests/fixtures/v2/withoutClientIpSet.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

<!DOCTYPE html>
<html>
<head>
<title>Raygun4JS without Client IP</title>
<script src="/fixtures/common/instrumentXHRs.js"></script>
<script type="text/javascript">
!function(a,b,c,d,e,f,g,h){a.RaygunObject=e,a[e]=a[e]||function(){
(a[e].o=a[e].o||[]).push(arguments)},f=b.createElement(c),g=b.getElementsByTagName(c)[0],
f.async=1,f.src=d,g.parentNode.insertBefore(f,g),h=a.onerror,a.onerror=function(b,c,d,f,g){
h&&h(b,c,d,f,g),g||(g=new Error(b)),a[e].q=a[e].q||[],a[e].q.push({
e:g})}}(window,document,"script","/dist/raygun.js","rg4js");
</script>
</head>
<body>
<script type="text/javascript">
rg4js('apiKey', 'abcd==');
rg4js('enablePulse', true);
rg4js('options', {
allowInsecureSubmissions: true,
debugMode: true,
});
</script>

</body>
</html>
38 changes: 38 additions & 0 deletions tests/specs/v2/clientIp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* globals describe, beforeEach, it, expect, browser, window */

var _ = require('underscore');

describe("ClientIp", function() {

it("X-Remote-Address is null when not set", function () {
browser.url('http://localhost:4567/fixtures/v2/withoutClientIpSet.html');

browser.pause(4000);

var sentXhrs = browser.execute(function () {
return window.__sentXHRs;
});

var remoteAddressIsUndefined = _.every(sentXhrs.value, function (req) {
return req.clientIp === null;
});

expect(remoteAddressIsUndefined).toBe(true);
});

it("X-Remote-Address is equal to '192.168.0.12'", function () {
browser.url('http://localhost:4567/fixtures/v2/withClientIpSet.html');

browser.pause(4000);

var sentXhrs = browser.execute(function () {
return window.__sentXHRs;
});

var remoteAddressIsSet = _.every(sentXhrs.value, function (req) {
return req.clientIp === "192.168.0.12";
});

expect(remoteAddressIsSet).toBe(true);
});
});

0 comments on commit 6d960c2

Please sign in to comment.