miolo 0.9.21 → 0.9.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * miolo v0.9.21
2
+ * miolo v0.9.23
3
3
  *
4
4
  * Copyright (c) Donato Lorenzo <donato@afialapis.com>
5
5
  *
@@ -28,8 +28,9 @@ import koa_mount from 'koa-mount';
28
28
  import koa_serve from 'koa-static';
29
29
  import koa_favicon from 'koa-favicon';
30
30
  import { performance } from 'node:perf_hooks';
31
+ import fs, { readFileSync } from 'node:fs';
32
+ import { Reader } from '@maxmind/geoip2-node';
31
33
  import Router from '@koa/router';
32
- import { readFileSync } from 'node:fs';
33
34
  import jwt from 'jwt-simple';
34
35
  import passport from 'koa-passport';
35
36
  import LocalStrategy from 'passport-local';
@@ -156,11 +157,15 @@ var base_config = {
156
157
  // seconds to consider slow a request
157
158
  onStart: undefined,
158
159
  // (ctx, times) => { return begin_result}
159
- onDone: undefined
160
- // (ctx, begin_result, times) => {}
160
+ onDone: undefined,
161
+ // (ctx, begin_result, times) => {},
162
+ geoip: {
163
+ enabled: false,
164
+ db: '/var/lib/GeoIP/GeoLite2-City.mmdb',
165
+ local_ips: ['127.0.0.1']
166
+ }
161
167
  }
162
168
  },
163
-
164
169
  session: {
165
170
  salt: 'SUPER_SALTY_YES?',
166
171
  secret: 'SUPER_SECRET_KEY_KERE',
@@ -800,6 +805,53 @@ var init_static_middleware = (app, config) => {
800
805
  }
801
806
  };
802
807
 
808
+ var _geoip_reader = undefined;
809
+ var _geoip_local_ips = ['127.0.0.1'];
810
+ function _geoip_init() {
811
+ var db = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '/var/lib/GeoIP/GeoLite2-City.mmdb';
812
+ var local_ips = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['127.0.0.1'];
813
+ var logger = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : console;
814
+ try {
815
+ if (_geoip_reader != undefined) {
816
+ return _geoip_reader;
817
+ }
818
+ _geoip_local_ips = [..._geoip_local_ips, ...(local_ips || [])];
819
+ var dbBuffer = fs.readFileSync(db);
820
+ _geoip_reader = Reader.openBuffer(dbBuffer);
821
+ return _geoip_reader;
822
+ } catch (error) {
823
+ logger.error("[geoip] Error initing:");
824
+ logger.error(error);
825
+ return undefined;
826
+ }
827
+ }
828
+ var geoip_localize_ip = function geoip_localize_ip(ip, config) {
829
+ var logger = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : console;
830
+ if (_geoip_local_ips.indexOf(ip) >= 0) {
831
+ return {
832
+ local: true,
833
+ country: '',
834
+ city: ''
835
+ };
836
+ }
837
+ try {
838
+ var _resp$city;
839
+ var reader = _geoip_init(config === null || config === void 0 ? void 0 : config.db, config === null || config === void 0 ? void 0 : config.local_ipds, logger);
840
+ var resp = reader.city(ip);
841
+ return {
842
+ country: resp.country.isoCode,
843
+ city: (_resp$city = resp.city) === null || _resp$city === void 0 || (_resp$city = _resp$city.names) === null || _resp$city === void 0 ? void 0 : _resp$city.en
844
+ };
845
+ } catch (error) {
846
+ logger.error("[geoip] Error localizing IP ".concat(ip, ":"));
847
+ logger.error(error);
848
+ }
849
+ return {
850
+ country: '',
851
+ city: ''
852
+ };
853
+ };
854
+
803
855
  var REQUEST_COUNTER = {
804
856
  total: 0
805
857
  };
@@ -827,14 +879,17 @@ function init_request_middleware(app, config) {
827
879
  lazy: (config === null || config === void 0 ? void 0 : config.lazy) || 1,
828
880
  slow: (config === null || config === void 0 ? void 0 : config.slow) || 2,
829
881
  onStart: (config === null || config === void 0 ? void 0 : config.onStart) || _def_on_begin,
830
- onDone: (config === null || config === void 0 ? void 0 : config.onDone) || _def_on_done
882
+ onDone: (config === null || config === void 0 ? void 0 : config.onDone) || _def_on_done,
883
+ geoip: (config === null || config === void 0 ? void 0 : config.geoip) || {
884
+ enabled: false
885
+ }
831
886
  };
832
887
  function request_middleware(_x6, _x7) {
833
888
  return _request_middleware.apply(this, arguments);
834
889
  }
835
890
  function _request_middleware() {
836
891
  _request_middleware = _asyncToGenerator(function* (ctx, next) {
837
- var _ctx$session;
892
+ var _reqConfig$geoip, _geo_info, _geo_info2, _geo_info3, _geo_info4, _ctx$session;
838
893
  var logger = ctx.miolo.logger;
839
894
  var ip = ctx.headers["x-real-ip"] || '127.0.0.1';
840
895
  var started = performance.now();
@@ -850,9 +905,18 @@ function init_request_middleware(app, config) {
850
905
  ctx.requestId = REQUEST_COUNTER.total;
851
906
  ctx.request.ip = ip;
852
907
 
908
+ // If wanted, geo localize ip
909
+ var geo_enabled = (reqConfig === null || reqConfig === void 0 || (_reqConfig$geoip = reqConfig.geoip) === null || _reqConfig$geoip === void 0 ? void 0 : _reqConfig$geoip.enabled) === true;
910
+ var geo_info = {};
911
+ if (geo_enabled) {
912
+ geo_info = geoip_localize_ip(ip, reqConfig.geoip, logger);
913
+ }
914
+ ctx.request.geoip = geo_info;
915
+
853
916
  // Log something
854
917
  var clurl = ctx.request.url.indexOf('?') >= 0 ? ctx.request.url.substr(0, ctx.request.url.indexOf('?')) : ctx.request.url;
855
- var sreq = "".concat(magenta(ip), " ").concat(cyan(ctx.request.method), " ").concat(cyan(clurl), " [").concat(cyan_light(REQUEST_COUNTER[ip]), "/").concat(cyan_light(ctx.requestId), "]");
918
+ var geo_string = geo_enabled ? ((_geo_info = geo_info) === null || _geo_info === void 0 ? void 0 : _geo_info.local) === true ? '' : (_geo_info2 = geo_info) !== null && _geo_info2 !== void 0 && _geo_info2.country ? (_geo_info3 = geo_info) !== null && _geo_info3 !== void 0 && _geo_info3.city ? " (".concat((_geo_info4 = geo_info) === null || _geo_info4 === void 0 ? void 0 : _geo_info4.city, ", ").concat(geo_info.country, ")") : " (".concat(geo_info.country, ")") : '' : '';
919
+ var sreq = "".concat(magenta(ip)).concat(geo_string, " ").concat(cyan(ctx.request.method), " ").concat(cyan(clurl), " [").concat(cyan_light(REQUEST_COUNTER[ip]), "/").concat(cyan_light(ctx.requestId), "]");
856
920
  var sbody = ctx.request.body != undefined ? JSON.stringify(ctx.request.body) : '';
857
921
  logger.info("".concat(sreq, " - START"));
858
922
  logger.debug("".concat(sreq, " - Body: ").concat(sbody));