Figure out node ip

This commit is contained in:
maciejhirsz
2018-07-06 19:12:24 +02:00
parent 538a30ccc3
commit 3259bc67a3
7 changed files with 53 additions and 11 deletions
+24
View File
@@ -0,0 +1,24 @@
declare module 'iplocation' {
namespace iplocation {
export interface LocationData {
as?: string;
city?: string;
country?: string;
countryCode?: string;
isp?: string;
lat: number;
lon: number;
org?: string;
query?: string;
region?: string;
regionName?: string;
status: string;
timezone?: string;
zip?: string;
}
}
function iplocation(ip: string, callback: (err: Error, result: iplocation.LocationData) => void): void;
export = iplocation;
}
+1
View File
@@ -15,6 +15,7 @@
"@types/node": "^10.3.3",
"@types/ws": "^5.1.2",
"express": "^4.16.3",
"iplocation": "^5.0.1",
"typescript": "^2.9.2",
"ws": "5.2.0"
}
+4 -1
View File
@@ -1,5 +1,6 @@
import * as WebSocket from 'ws';
import * as EventEmitter from 'events';
import * as iplocation from 'iplocation';
import { timestamp, Maybe, Types, idGenerator } from '@dotstats/common';
import { parseMessage, getBestBlock, Message, BestBlock, SystemInterval } from './message';
@@ -83,7 +84,9 @@ export default class Node {
});
}
public static fromSocket(socket: WebSocket): Promise<Node> {
public static fromSocket(socket: WebSocket, ip: string): Promise<Node> {
console.log('node ip', ip);
return new Promise((resolve, reject) => {
function cleanup() {
clearTimeout(timeout);
+8 -2
View File
@@ -14,9 +14,15 @@ const telemetryFeed = new WebSocket.Server({ port: 8080 });
console.log('Telemetry server listening on port 1024');
console.log('Feed server listening on port 8080');
incomingTelemetry.on('connection', async (socket: WebSocket) => {
const ipv4 = /[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/;
incomingTelemetry.on('connection', async (socket, req) => {
try {
const node = await Node.fromSocket(socket);
const [ ip ] = (req.headers['x-forwarded-for'] || req.connection.remoteAddress || '0.0.0.0')
.toString()
.match(ipv4) || ['0.0.0.0'];
const node = await Node.fromSocket(socket, ip);
aggregator.addNode(node);
} catch (err) {
+2 -1
View File
@@ -4,6 +4,7 @@
"outDir": "build"
},
"include": [
"./src/**/*.ts"
"./src/**/*.ts",
"./declarations/**/*.d.ts"
]
}