config.js 5.59 KB
const fs = require('fs');
const express = require('express');
const { orderBy } = require('natural-orderby');
const config = require('komodo-sdk/config');

const router = express.Router();
module.exports = router;

async function writeConfigFile() {
    try {
        await fs.promises.writeFile('config.json', JSON.stringify(config, null, 4));
    } catch (e) {
        //
    }
}

function pageMain(req, res) {
    res.render('config.index.html', {
        modems: orderBy(config.modems, [(v) => v.name], ['asc']),
        baseUrl: req.baseUrl,
    });
}

async function pageSetOutgoing(req, res) {
    const modemName = (req.params.modemName || '').trim();
    if (!modemName) {
        res.end('Invalid modem name');
        return;
    }

    const idx = (config.modems || []).findIndex((modem) => modem.name === modemName);
    if (idx < 0) {
        res.end('No modem matched');
        return;
    }

    config.modems[idx].outgoing = req.params.newValue === '1' || req.params.newValue === 'true';

    await writeConfigFile();

    res.redirect(`${req.baseUrl}/modem#modem${encodeURIComponent(modemName)}`);
}

async function pageDelPrefix(req, res) {
    const modemName = (req.params.modemName || '').trim();
    if (!modemName) {
        res.end('Invalid modem name');
        return;
    }

    const prefix = (req.params.prefix || '').trim();
    if (!prefix) {
        res.end('Invalid prefix');
        return;
    }

    const modemIdx = (config.modems || []).findIndex((modem) => modem.name === modemName);
    if (modemIdx < 0) {
        res.end('No modem matched');
        return;
    }

    const prefixIdx = (config.modems[modemIdx].prefix || []).indexOf(prefix);
    if (prefixIdx < 0) {
        res.end('No prefix matched');
        return;
    }

    config.modems[modemIdx].prefix.splice(prefixIdx, 1);

    await writeConfigFile();

    res.redirect(`${req.baseUrl}/modem#modem${encodeURIComponent(modemName)}`);
}

async function modemAdd(req, res) {
    const modem = {
        name: (req.body.name || '').trim(),
        imsi: (req.body.imsi || '').trim(),
        outgoing: !!req.body.outgoing,
        prefix: (req.body.prefix || '').trim().split(/[, ]+/)
            .map((val) => val.trim().replace(/^0/, '62'))
            .filter((row) => row),
    };

    const index = config.modems.find(
        (item) => item.name.toUpperCase() === modem.name.toUpperCase(),
    );

    if (index >= 0) {
        res.end('Modem duplikat');
        return;
    }

    config.modems.push(modem);
    await writeConfigFile();

    res.redirect(`${req.baseUrl}/modem`);
}

async function modemAddPrefix(req, res) {
    const modemName = (req.body.name || '').trim();
    const prefix = (req.body.prefix || '').trim().split(/[, ]+/)
        .map((val) => val.trim().replace(/^0/, '62'))
        .filter((row) => row);

    const index = config.modems.findIndex(
        (item) => item.name.toUpperCase() === modemName.toUpperCase(),
    );
    if (Array.isArray(config.modems[index].prefix)) {
        config.modems[index].prefix = config.modems[index].prefix.concat(prefix);
    } else {
        config.modems[index].prefix = prefix;
    }

    if (index < 0) {
        res.end('Modem tidak ditemukan');
        return;
    }

    await writeConfigFile();

    res.redirect(`${req.baseUrl}/modem#modem${encodeURIComponent(modemName)}`);
}

async function modemUpdateImsi(req, res) {
    const modemName = (req.body.name || '').trim();
    const imsi = (req.body.imsi || '').trim();

    const index = config.modems.findIndex(
        (item) => item.name.toUpperCase() === modemName.toUpperCase(),
    );
    config.modems[index].imsi = imsi;

    if (index < 0) {
        res.end('Modem tidak ditemukan');
        return;
    }

    await writeConfigFile();

    res.redirect(`${req.baseUrl}/modem#modem${encodeURIComponent(modemName)}`);
}

async function modemUpdateCustomIp(req, res) {
    const modemName = (req.body.name || '').trim();
    const url = (req.body.url || '').trim() || null;
    const username = (req.body.username || '').trim() || null;
    const password = (req.body.password || '').trim() || null;

    const index = config.modems.findIndex(
        (item) => item.name.toUpperCase() === modemName.toUpperCase(),
    );

    config.modems[index].url = url;
    config.modems[index].username = username;
    config.modems[index].password = password;

    if (index < 0) {
        res.end('Modem tidak ditemukan');
        return;
    }

    await writeConfigFile();

    res.redirect(`${req.baseUrl}/modem#modem${encodeURIComponent(modemName)}`);
}

async function modemDelete(req, res) {
    const modemName = (req.body.name || '').trim();

    const index = config.modems.findIndex(
        (item) => item.name.toUpperCase() === modemName.toUpperCase(),
    );

    if (index < 0) {
        res.end('Modem tidak ditemukan');
        return;
    }

    config.modems.splice(index, 1);

    await writeConfigFile();

    res.redirect(`${req.baseUrl}`);
}

router.get('/', (req, res) => {
    res.redirect(`${req.baseUrl}/modem`); // sementara redirect ke modem
});

router.get('/modem', pageMain);
router.post('/modem/add', express.urlencoded({ extended: true }), modemAdd);
router.post('/modem/add-prefix', express.urlencoded({ extended: true }), modemAddPrefix);
router.post('/modem/update-imsi', express.urlencoded({ extended: true }), modemUpdateImsi);
router.post('/modem/update-custom-ip', express.urlencoded({ extended: true }), modemUpdateCustomIp);
router.post('/modem/delete', express.urlencoded({ extended: true }), modemDelete);
router.get('/modem/set-outgoing/:modemName/:newValue', pageSetOutgoing);
router.get('/modem/del-prefix/:modemName/:prefix', pageDelPrefix);