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';
@@ -453,11 +454,15 @@ var base_config = {
453
454
  // seconds to consider slow a request
454
455
  onStart: undefined,
455
456
  // (ctx, times) => { return begin_result}
456
- onDone: undefined
457
- // (ctx, begin_result, times) => {}
457
+ onDone: undefined,
458
+ // (ctx, begin_result, times) => {},
459
+ geoip: {
460
+ enabled: false,
461
+ db: '/var/lib/GeoIP/GeoLite2-City.mmdb',
462
+ local_ips: ['127.0.0.1']
463
+ }
458
464
  }
459
465
  },
460
-
461
466
  session: {
462
467
  salt: 'SUPER_SALTY_YES?',
463
468
  secret: 'SUPER_SECRET_KEY_KERE',
@@ -1132,6 +1137,61 @@ var init_static_middleware = function init_static_middleware(app, config) {
1132
1137
  }
1133
1138
  };
1134
1139
 
1140
+ var _geoip_reader = undefined;
1141
+ var _geoip_local_ips = ['127.0.0.1'];
1142
+ function _geoip_init(db, local_ips, logger) {
1143
+ if (db === void 0) {
1144
+ db = '/var/lib/GeoIP/GeoLite2-City.mmdb';
1145
+ }
1146
+ if (local_ips === void 0) {
1147
+ local_ips = ['127.0.0.1'];
1148
+ }
1149
+ if (logger === void 0) {
1150
+ logger = console;
1151
+ }
1152
+ try {
1153
+ if (_geoip_reader != undefined) {
1154
+ return _geoip_reader;
1155
+ }
1156
+ _geoip_local_ips = [].concat(_geoip_local_ips, local_ips || []);
1157
+ var dbBuffer = fs.readFileSync(db);
1158
+ _geoip_reader = Reader.openBuffer(dbBuffer);
1159
+ return _geoip_reader;
1160
+ } catch (error) {
1161
+ logger.error("[geoip] Error initing:");
1162
+ logger.error(error);
1163
+ return undefined;
1164
+ }
1165
+ }
1166
+ var geoip_localize_ip = function geoip_localize_ip(ip, config, logger) {
1167
+ if (logger === void 0) {
1168
+ logger = console;
1169
+ }
1170
+ if (_geoip_local_ips.indexOf(ip) >= 0) {
1171
+ return {
1172
+ local: true,
1173
+ country: '',
1174
+ city: ''
1175
+ };
1176
+ }
1177
+ try {
1178
+ var _resp$city;
1179
+ var reader = _geoip_init(config == null ? void 0 : config.db, config == null ? void 0 : config.local_ipds, logger);
1180
+ var resp = reader.city(ip);
1181
+ return {
1182
+ country: resp.country.isoCode,
1183
+ city: (_resp$city = resp.city) == null || (_resp$city = _resp$city.names) == null ? void 0 : _resp$city.en
1184
+ };
1185
+ } catch (error) {
1186
+ logger.error("[geoip] Error localizing IP " + ip + ":");
1187
+ logger.error(error);
1188
+ }
1189
+ return {
1190
+ country: '',
1191
+ city: ''
1192
+ };
1193
+ };
1194
+
1135
1195
  var REQUEST_COUNTER = {
1136
1196
  total: 0
1137
1197
  };
@@ -1175,15 +1235,18 @@ function init_request_middleware(app, config) {
1175
1235
  lazy: (config == null ? void 0 : config.lazy) || 1,
1176
1236
  slow: (config == null ? void 0 : config.slow) || 2,
1177
1237
  onStart: (config == null ? void 0 : config.onStart) || _def_on_begin,
1178
- onDone: (config == null ? void 0 : config.onDone) || _def_on_done
1238
+ onDone: (config == null ? void 0 : config.onDone) || _def_on_done,
1239
+ geoip: (config == null ? void 0 : config.geoip) || {
1240
+ enabled: false
1241
+ }
1179
1242
  };
1180
1243
  function request_middleware(_x6, _x7) {
1181
1244
  return _request_middleware.apply(this, arguments);
1182
1245
  }
1183
1246
  function _request_middleware() {
1184
1247
  _request_middleware = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee3(ctx, next) {
1185
- var _ctx$session;
1186
- var logger, ip, started, clurl, sreq, sbody, begin_result, user, uid_desc, status, ststr, stcolor, stdesc, elapsed, tcolor, tname, ssession, rbody;
1248
+ var _reqConfig$geoip, _geo_info, _geo_info2, _geo_info3, _geo_info4, _ctx$session;
1249
+ var logger, ip, started, geo_enabled, geo_info, clurl, geo_string, sreq, sbody, begin_result, user, uid_desc, status, ststr, stcolor, stdesc, elapsed, tcolor, tname, ssession, rbody;
1187
1250
  return _regeneratorRuntime().wrap(function _callee3$(_context3) {
1188
1251
  while (1) switch (_context3.prev = _context3.next) {
1189
1252
  case 0:
@@ -1200,22 +1263,31 @@ function init_request_middleware(app, config) {
1200
1263
  ctx.requestId = REQUEST_COUNTER.total;
1201
1264
  ctx.request.ip = ip;
1202
1265
 
1266
+ // If wanted, geo localize ip
1267
+ geo_enabled = (reqConfig == null || (_reqConfig$geoip = reqConfig.geoip) == null ? void 0 : _reqConfig$geoip.enabled) === true;
1268
+ geo_info = {};
1269
+ if (geo_enabled) {
1270
+ geo_info = geoip_localize_ip(ip, reqConfig.geoip, logger);
1271
+ }
1272
+ ctx.request.geoip = geo_info;
1273
+
1203
1274
  // Log something
1204
1275
  clurl = ctx.request.url.indexOf('?') >= 0 ? ctx.request.url.substr(0, ctx.request.url.indexOf('?')) : ctx.request.url;
1205
- sreq = magenta(ip) + " " + cyan(ctx.request.method) + " " + cyan(clurl) + " [" + cyan_light(REQUEST_COUNTER[ip]) + "/" + cyan_light(ctx.requestId) + "]";
1276
+ geo_string = geo_enabled ? ((_geo_info = geo_info) == null ? void 0 : _geo_info.local) === true ? '' : (_geo_info2 = geo_info) != null && _geo_info2.country ? (_geo_info3 = geo_info) != null && _geo_info3.city ? " (" + ((_geo_info4 = geo_info) == null ? void 0 : _geo_info4.city) + ", " + geo_info.country + ")" : " (" + geo_info.country + ")" : '' : '';
1277
+ sreq = "" + magenta(ip) + geo_string + " " + cyan(ctx.request.method) + " " + cyan(clurl) + " [" + cyan_light(REQUEST_COUNTER[ip]) + "/" + cyan_light(ctx.requestId) + "]";
1206
1278
  sbody = ctx.request.body != undefined ? JSON.stringify(ctx.request.body) : '';
1207
1279
  logger.info(sreq + " - START");
1208
1280
  logger.debug(sreq + " - Body: " + sbody);
1209
- _context3.next = 15;
1281
+ _context3.next = 20;
1210
1282
  return reqConfig.onStart(ctx, {
1211
1283
  started: started,
1212
1284
  description: 'pending'
1213
1285
  });
1214
- case 15:
1286
+ case 20:
1215
1287
  begin_result = _context3.sent;
1216
- _context3.next = 18;
1288
+ _context3.next = 23;
1217
1289
  return next();
1218
- case 18:
1290
+ case 23:
1219
1291
  user = ctx == null || (_ctx$session = ctx.session) == null ? void 0 : _ctx$session.user;
1220
1292
  uid_desc = '';
1221
1293
  if (user != undefined) {
@@ -1245,15 +1317,15 @@ function init_request_middleware(app, config) {
1245
1317
  logger.debug(sreq + " - Session: " + ssession);
1246
1318
  rbody = ctx.body != undefined ? JSON.stringify(ctx.body) : '';
1247
1319
  logger.debug(sreq + " - Response: " + rbody);
1248
- _context3.next = 34;
1320
+ _context3.next = 39;
1249
1321
  return reqConfig.onDone(ctx, begin_result, {
1250
1322
  started: started,
1251
1323
  elapsed: elapsed,
1252
1324
  description: tname
1253
1325
  });
1254
- case 34:
1326
+ case 39:
1255
1327
  logger.info(sreq + " - DONE " + stdesc + uid_desc + " (" + tcolor(tname) + ": " + tcolor(elapsed) + ")");
1256
- case 35:
1328
+ case 40:
1257
1329
  case "end":
1258
1330
  return _context3.stop();
1259
1331
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miolo",
3
- "version": "0.9.21",
3
+ "version": "0.9.23",
4
4
  "description": "all-in-one koa-based server",
5
5
  "author": "Donato Lorenzo <donato@afialapis.com>",
6
6
  "contributors": [
@@ -67,6 +67,7 @@
67
67
  "dependencies": {
68
68
  "@koa/cors": "^4.0.0",
69
69
  "@koa/router": "^12.0.1",
70
+ "@maxmind/geoip2-node": "^4.2.0",
70
71
  "calustra": "^0.10.4",
71
72
  "cron": "^2.3.1",
72
73
  "deepmerge": "^4.3.1",