From c7b78e76ad1b1ba4dfce81adae533c2e9f25ec07 Mon Sep 17 00:00:00 2001 From: vjrj Date: Wed, 13 Dec 2017 22:23:36 +0100 Subject: [PATCH] Renaming --- imports/startup/server/IPGeocoder.js | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 imports/startup/server/IPGeocoder.js diff --git a/imports/startup/server/IPGeocoder.js b/imports/startup/server/IPGeocoder.js new file mode 100644 index 0000000..cf0daa5 --- /dev/null +++ b/imports/startup/server/IPGeocoder.js @@ -0,0 +1,39 @@ +import { Meteor } from 'meteor/meteor'; +import maxmind from 'maxmind'; + +// https://stackoverflow.com/questions/13969655/how-do-you-check-whether-the-given-ip-is-internal-or-not +function isPrivateIP(ip) { + const parts = ip.split('.'); + return parts[0] === '10' || + parts[0] === '127' || + (parts[0] === '172' && (parseInt(parts[1], 10) >= 16 && parseInt(parts[1], 10) <= 31)) || + (parts[0] === '192' && parts[1] === '168'); +} + +const IPGeocoder = maxmind.openSync(`${process.env.PWD}/private/GeoLite2-City.mmdb`); +export default IPGeocoder; + +Meteor.methods({ + geo() { + // https://stackoverflow.com/questions/14843232/how-to-get-the-user-ip-address-in-meteor-server/22657421#22657421 + let clientIP = this.connection.clientAddress; + + if (isPrivateIP(clientIP)) { + clientIP = '80.58.61.250'; // Some Spain IP address + } + // console.log(`Geolocating ${clientIP}`); + + // TODO: cron download GeoLite-City + // http://dev.maxmind.com/geoip/geoip2/geolite2/ + const location = IPGeocoder.get(clientIP); + // console.log(location); + return location; + }, + getMapKey() { + // http://meteorpedia.com/read/Environment_Variables + // https://developers.google.com/maps/documentation/javascript/get-api-key + // https://console.developers.google.com/ + // export GMAPS_KEY=SomeGMapsKey + return process.env.GMAPS_KEY || Meteor.settings.gmaps.key; + } +});