Skip to content

Commit f0848af

Browse files
Robin Gottfriedmartinbilek
authored andcommitted
[feat] report IP address
1 parent 765e17d commit f0848af

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

client/client.js

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ const path = require('path'),
88
WebSocket = require('ws'),
99
{ Messanger } = require('../server/ws-message'),
1010
{ getLogger } = require('../lib/logger'),
11+
{ getIPAddress } = require('../lib/utils'),
1112
config = require('../config'),
1213

1314
debug = getLogger.debug({prefix: '\u001b[34mC:d', postfix: '\u001b[0m\n'}),
1415
info = getLogger.info({prefix: '\u001b[34mC:i', postfix: '\u001b[0m\n'}),
15-
warning = getLogger.info({prefix: '\u001b[35mC:i', postfix: '\u001b[0m\n'});
16+
warning = getLogger.warning({prefix: '\u001b[35mC:i', postfix: '\u001b[0m\n'});
1617

1718
class InvalidRequestError extends Error { } // Received an invalid request from proxy server side
1819

@@ -97,7 +98,9 @@ class Channel extends Object {
9798
this.timeout = handler.maxChannelLivespan;
9899

99100
this._timeoutTimer = new Timer(() => {
100-
this.request.emit('error', 'The request timed out.');
101+
if (this.request && this.request.emit) {
102+
this.request.emit('error', 'The request timed out.');
103+
}
101104
}, this.timeout);
102105
}
103106

@@ -132,11 +135,39 @@ class Channel extends Object {
132135
return this;
133136
}
134137

138+
/**
139+
* Internal (not forwarded api endpoint on /$ip
140+
*/
141+
internal_request_ip(ireq, forwardToUrl) {
142+
this._send('headers', {
143+
statusCode: '200',
144+
statusMessage: 'OK',
145+
headers: {'Content-Type': 'application/json; charset=utf-8'},
146+
});
147+
this.url = ireq.url;
148+
this.request = {end: ()=>{
149+
150+
let ipAddress;
151+
if (['localhost', '127.0.0.1',].indexOf(forwardToUrl.hostname) >= 0) {
152+
ipAddress = getIPAddress();
153+
} else {
154+
ipAddress = forwardToUrl.hostname;
155+
}
156+
this.onHttpData(JSON.stringify({
157+
'ipAddress': ipAddress,
158+
}));
159+
this.onHttpEnd();
160+
}};
161+
}
162+
135163
on_headers(message) {
136164
this._timeoutTimer.reset()
137165
const ireq = message.data;
138166
debug(`< ${this.id}: ${ireq.method} ${ireq.url}`);
139167
const forwardToUrl = new URL(this.forwardTo); // clone the original uri
168+
if (ireq.url == '/$ip') {
169+
return this.internal_request_ip(ireq, forwardToUrl);
170+
}
140171
forwardToUrl.href = path.posix.join(forwardToUrl.href, ireq.url);
141172
// TODO: Port setup should be packed in message on WS server part
142173
if (ireq.headers['x-karmen-port']) {
@@ -245,7 +276,6 @@ class PingPongGamer extends Object {
245276

246277
onMessage(message) {
247278
// record that server was life (even for non-pong messages)
248-
debug('Pong');
249279
this.gotMessage = true;
250280
if (message.event == 'ping') {
251281
this.ws.send(message.channel, 'pong');

lib/utils.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,27 @@ function checksum(data, {length=3}={}) {
1111
return crypto.createHash('shake256', {outputLength: length}).update(data).digest('hex').replace(/(.{4})/g, ':$1').slice(1);
1212
}
1313

14+
/**
15+
* Tries to get local IP address
16+
* Credits to https://stackoverflow.com/a/15075395
17+
*/
18+
function getIPAddress() {
19+
var interfaces = require('os').networkInterfaces();
20+
for (var devName in interfaces) {
21+
var iface = interfaces[devName];
22+
23+
for (var i = 0; i < iface.length; i++) {
24+
var alias = iface[i];
25+
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal)
26+
return alias.address;
27+
}
28+
}
29+
return '0.0.0.0';
30+
}
31+
1432
module.exports = {
1533
checksum: checksum,
34+
getIPAddress: getIPAddress,
1635
}
1736

1837

server/clients-manager.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class ClientsManager extends Object {
4848

4949

5050
authenticate(request, socket, callback) {
51-
info(`Authenticating request to ${request.url}.`);
51+
info(`Authenticating request to ${request.url} from ${request.ip}.`);
5252

5353
const auth_callback = () => {
5454
if (this.authenticator) {

0 commit comments

Comments
 (0)