"use strict";
// This file is part of HFS - Copyright 2021, Massimo Melina <a@rejetto.com> - License https://www.gnu.org/licenses/gpl-3.0.txt
Object.defineProperty(exports, "__esModule", { value: true });
exports.trackIpsMw = exports.ips = void 0;
const config_1 = require("./config");
const kvstorage_1 = require("@rejetto/kvstorage");
const misc_1 = require("./misc");
const first_1 = require("./first");
const trackIps = (0, config_1.defineConfig)(misc_1.CFG.track_ips, true);
exports.ips = new kvstorage_1.KvStorage({
    defaultPutDelay: misc_1.MINUTE,
    maxPutDelay: 10 * misc_1.MINUTE,
    maxPutDelayCreate: 0,
});
(0, first_1.onProcessExit)(() => exports.ips.close());
const trackIpsMw = async (ctx, next) => {
    if (exports.ips.isOpen() && !(0, misc_1.isLocalHost)(ctx))
        exports.ips.put(ctx.ip, { ts: new Date, country: ctx.state.connection.country });
    await next();
};
exports.trackIpsMw = trackIpsMw;
trackIps.sub(v => {
    if (v)
        exports.ips.open('ips.kv');
    else
        exports.ips.close();
});
