diff --git a/api/api.js b/api/api.js index aa591375..cef9a299 100644 --- a/api/api.js +++ b/api/api.js @@ -13,6 +13,9 @@ import _ from 'lodash'; import './inbound-stream'; import expressWs from 'express-ws'; import geoip from 'geoip-lite'; +import YAML from 'yaml'; +import fs from 'fs'; +import assert from 'assert'; import config from './config'; @@ -253,8 +256,26 @@ app.get('/blk/:id', (req, res) => { sendBlockResult(req, res); }); +let geoipWhitelist = {}; +if (fs.existsSync('blockexplorer-geoip.yml')) { + try { + const file = fs.readFileSync('blockexplorer-geoip.yml', 'utf8'); + geoipWhitelist = YAML.parse(file); + console.log('geoip whitelist:', geoipWhitelist); + assert(typeof geoipWhitelist === 'object'); + } catch (err) { + console.log(err); + } +} + app.get('/geoip/:ip', (req, res) => { const {ip} = req.params; + + if (geoipWhitelist[ip]) { + res.send(JSON.stringify(geoipWhitelist[ip]) + '\n'); + return; + } + const geo = geoip.lookup(ip); if (geo === null) { res.status(404).send('{"error":"not_found"}\n'); diff --git a/package.json b/package.json index 86325db6..ac126e13 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "nocache": "^2.0.0", "react-debounce-render": "^5.0.0", "redis": "^2.8.0", - "serve": "^11.0.0" + "serve": "^11.0.0", + "yaml": "^1.5.0" }, "devDependencies": { "@material-ui/core": "^3.9.3", diff --git a/yarn.lock b/yarn.lock index 81474409..5dd70bc6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1019,6 +1019,13 @@ dependencies: regenerator-runtime "^0.13.2" +"@babel/runtime@^7.4.3": + version "7.4.4" + resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.4.4.tgz#dc2e34982eb236803aa27a07fea6857af1b9171d" + integrity sha512-w0+uT71b6Yi7i5SE0co4NioIpSYS6lLiXvCzWzGSKvpK5vdQtCbICHMj+gbAKAOtxiV6HsVh/MBdaF9EQ6faSg== + dependencies: + regenerator-runtime "^0.13.2" + "@babel/template@^7.0.0", "@babel/template@^7.4.0": version "7.4.0" resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.4.0.tgz#12474e9c077bae585c5d835a95c0b0b790c25c8b" @@ -14378,6 +14385,13 @@ yallist@^3.0.0, yallist@^3.0.2: resolved "https://registry.yarnpkg.com/yallist/-/yallist-3.0.3.tgz#b4b049e314be545e3ce802236d6cd22cd91c3de9" integrity sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A== +yaml@^1.5.0: + version "1.5.0" + resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.5.0.tgz#1d879dd069ce1036041c23229cb5d7ef47a58ec6" + integrity sha512-nKxSWOa7vxAP2pikrGxbkZsG/garQseRiLn9mIDjzwoQsyVy7ZWIpLoARejnINGGLA4fttuzRFFNxxbsztdJgw== + dependencies: + "@babel/runtime" "^7.4.3" + yargs-parser@^10.1.0: version "10.1.0" resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-10.1.0.tgz#7202265b89f7e9e9f2e5765e0fe735a905edbaa8"