Skip to content

Commit 8c3ab6f

Browse files
committed
core: refactor geoip.js
1 parent 71200de commit 8c3ab6f

File tree

3 files changed

+40
-40
lines changed

3 files changed

+40
-40
lines changed

core/src/main.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const bsInit = require('blinksocks/bin/init');
1010
const { Config } = require('blinksocks');
1111

1212
const runServer = require('./core/server');
13-
const { ServiceManager, logger, GeoIP } = require('./utils');
13+
const { ServiceManager, logger } = require('./utils');
1414

1515
const {
1616
RUN_TYPE_CLIENT,
@@ -152,7 +152,6 @@ module.exports = async function main(args) {
152152
const ip = await getPublicIP();
153153
logger.info(`public ip address is: ${ip}`);
154154
db.set('runtime.ip', ip).write();
155-
GeoIP.put(ip, { self: true });
156155
} catch (err) {
157156
logger.error('cannot get public ip address of this machine: %s', err.stack);
158157
}

core/src/utils/geoip.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,18 @@ function isPrivateIP(ip) {
5252
);
5353
}
5454

55-
module.exports = {
55+
const MAX_QUEUE_SIZE = 500;
5656

57-
_uniqueKeys: new Set(),
57+
class GeoIP {
5858

59-
_store: new Map(/* <ip>: <result> */),
59+
constructor() {
60+
this._uniqueKeys = new Set();
61+
this._store = [];
62+
}
6063

6164
// lookup ip address to geo location
6265
async put(arg, extra = {}) {
63-
if (typeof arg !== 'string') {
66+
if (typeof arg !== 'string' || !arg) {
6467
return;
6568
}
6669

@@ -85,50 +88,45 @@ module.exports = {
8588
try {
8689
const geoInfo = await getGeoInfo(ip);
8790
const { lat, lon: lng, query } = geoInfo;
88-
if (!this._store.get(ip)) {
89-
// merge hostname and ip by the same geo location
90-
const item = this.findItemByPosition(lat, lng);
91-
if (item) {
92-
const oldValue = item.value;
93-
const newValue = {
94-
...oldValue,
95-
ips: oldValue.ips.concat([query]),
96-
};
97-
if (oldValue.hostname && extra.hostname) {
98-
newValue.hostname = oldValue.hostname.concat(extra.hostname);
91+
92+
// merge hostname and ip by the same geo location
93+
const item = this._store.find((v) => v.lat === lat && v.lng === lng);
94+
if (item) {
95+
item.ips.push(query);
96+
if (item.ips.length > 20) {
97+
item.ips.shift();
98+
}
99+
if (item.hostname && extra.hostname) {
100+
item.hostname.push(extra.hostname);
101+
if (item.hostname.length > 20) {
102+
item.hostname.shift();
99103
}
100-
this._store.set(item.key, newValue);
101-
} else {
102-
const obj = _.pick(geoInfo, ['as', 'city', 'country', 'lat', 'org', 'regionName']);
103-
obj['ips'] = [query];
104-
obj['lng'] = lng;
105-
this._store.set(ip, Object.assign({}, obj, extra));
106104
}
107-
return;
105+
} else {
106+
const obj = _.pick(geoInfo, ['as', 'city', 'country', 'lat', 'org', 'regionName']);
107+
obj['ips'] = [query];
108+
obj['lng'] = lng;
109+
this._store.push(Object.assign({}, obj, extra));
110+
if (this._store.length > MAX_QUEUE_SIZE) {
111+
this._store.shift();
112+
}
108113
}
109114
} catch (err) {
110115
logger.error(err.message);
116+
this._uniqueKeys.delete(arg);
111117
}
112-
this._uniqueKeys.delete(arg);
113-
},
118+
}
114119

115120
// return all resolved ip and its geo location
116121
getStore() {
117122
return this._store;
118-
},
123+
}
119124

120125
clear() {
121126
this._uniqueKeys.clear();
122-
this._store.clear();
123-
},
127+
this._store = [];
128+
}
124129

125-
findItemByPosition(lat, lng) {
126-
for (const [key, value] of this._store) {
127-
if (lat === value.lat && lng === value.lng) {
128-
return { key, value };
129-
}
130-
}
131-
return null;
132-
},
130+
}
133131

134-
};
132+
module.exports = GeoIP;

core/src/utils/service_manager.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const path = require('path');
22
const child_process = require('child_process');
33
const _ = require('lodash');
44

5+
const db = require('./db');
56
const logger = require('./logger');
67
const GeoIP = require('./geoip');
78

@@ -20,7 +21,9 @@ function create() {
2021
cwd: RUNTIME_PATH,
2122
silent: process.env.NODE_ENV === 'production',
2223
});
23-
const geoip = Object.create(GeoIP);
24+
const geoip = new GeoIP();
25+
geoip.put(db.get('runtime.ip').value(), { self: true });
26+
2427
const messageQueue = [];
2528

2629
subprocess.on('message', (message) => {
@@ -199,7 +202,7 @@ module.exports = {
199202
getServiceGeoIPs(id) {
200203
const sub = this._subprocesses.get(id);
201204
if (sub) {
202-
return [...sub.geoip.getStore().values()];
205+
return sub.geoip.getStore();
203206
} else {
204207
return [];
205208
}

0 commit comments

Comments
 (0)