forked from andyburke/node-geoip-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
177 lines (134 loc) · 4.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
var http = require( 'http' );
var https = require( 'https' );
var fs = require( 'node-fs' );
var extend = require( 'node.extend' );
var EventEmitter = require( 'events' ).EventEmitter;
var util = require( 'util' );
var crypto = require( 'crypto' );
var express = require( 'express' );
var geoip = require( 'geoip-native' );
var defaults = {
lookupURI: '/location/:ip',
ipURI: '/ip',
origin: '*'
};
module.exports = GeoIPServer;
function GeoIPServer( _options ) {
var self = this;
EventEmitter.call( self );
self.options = extend( {}, defaults, _options );
}
util.inherits( GeoIPServer, EventEmitter );
GeoIPServer.prototype.attach = function( app ) {
var self = this;
function AllowCORS( request, response, next ) {
response.header( 'Access-Control-Allow-Origin', self.options.origin );
response.header( 'Access-Control-Allow-Methods', 'POST' );
if ( !!request.headers[ 'access-control-request-headers' ] ) {
response.header( 'Access-Control-Allow-Headers', request.headers[ 'access-control-request-headers' ] );
}
if ( request.method === 'OPTIONS' ) {
response.send( 200 );
return;
}
next();
}
if ( self.options.cors ) {
app.all( self.options.lookupURI, AllowCORS );
}
app.get( self.options.lookupURI, function( request, response, next ) {
self.onLookup( request, response, next );
} );
app.get( self.options.ipURI, function( request, response, next ) {
self.onIPLookup( request, response, next );
} );
};
GeoIPServer.prototype.getSignature = function( request ) {
var self = this;
var verification = '';
for ( var key in Object.keys( request.query ).sort() ) {
if ( key === 'signature' ) {
continue;
}
verification += key + '=' + request.query[ key ] + '&';
}
verification += 'ip=' + request.params.ip + '&';
verification += 'secret=' + self.options.secret;
return crypto.createHash( 'sha1' ).update( verification ).digest( 'hex' );
};
GeoIPServer.prototype.onIPLookup = function( request, response ) {
var self = this;
var ip = request.ip;
self.emit( 'ip-lookup', {
ip: ip,
request: request
} );
response.json( {
ip: ip
} );
};
GeoIPServer.prototype.onLookup = function( request, response ) {
var self = this;
var signature = request.query.signature || request.headers[ 'x-signature' ];
if ( self.options.secret && !signature ) {
response.json( {
error: 'signature missing',
message: 'No signature specified in request.'
}, 400 );
return;
}
if ( self.options.secret && ( self.getSignature( request ) != signature ) ) {
response.json( {
error: 'invalid signature',
message: 'Signature for this request is invalid.'
}, 400 );
return;
}
var ip = request.params.ip;
var geo = geoip.lookup( ip );
self.emit( 'lookup', {
ip: ip,
geo: geo,
request: request
} );
if ( !geo ) {
response.json( {
error: 'no geo data',
message: 'Could not get geo data for ip: ' + ip
}, 404 );
return;
}
response.json( geo );
};
var listenDefaults = {
port: 8888,
ssl: {
port: 4443
}
};
GeoIPServer.prototype.listen = function( _options ) {
var self = this;
var options = extend( {}, listenDefaults, _options );
var app = express();
self.attach( app );
app.enable('trust proxy');
if ( options.ssl && options.ssl.key && options.ssl.cert ) {
var httpsServer = https.createServer( {
key: fs.readFileSync( options.ssl.key ),
cert: fs.readFileSync( options.ssl.cert ),
ca: options.ssl.ca || []
}, app );
httpsServer.listen( options.ssl.port );
self.emit( 'listening', {
ssl: true,
port: options.ssl.port
} );
}
var httpServer = http.createServer( app );
httpServer.listen( options.port );
self.emit( 'listening', {
port: options.port
} );
return self;
};