index.js 4.27 KB
"use strict";

/**
 * Modul untuk menerima callback dari modem handler jika ada SMS masuk.
 */


const express = require('express');
const moment = require('moment');

const messagingService = require('komodo-center-messaging-client-lib');

const config = require('komodo-sdk/config');
const logger = require('komodo-sdk/logger');

const transport = require('../transport');
const partnerLastSeen = require('../partner-last-seen');
const history = require('../history');
// const modems = require('../modems2');

const routerConfigSenders = require('./router-config-senders');
const routerModems = require('./router-modems');

if (config.handler_callback_server) {
    logger.warn('Deprecated config.handler_callback_server. Please migrate it to config.apiserver!');
}

const app = express();
messagingService.setTransport(transport);

function apikeyChecker(req, res, next) {
    res.locals.has_valid_apikey = req.params.apikey === ((config.apiserver && config.apiserver.apikey ? config.apiserver.apikey : null) || config.handler_callback_server.apikey);
    if (res.locals.has_valid_apikey) {
        next();
    }
    else {
        logger.warn('Invalid apikey', { ip: req.ip });
        res.end('APISERVER: Invalid apikey');
    }
}

function onIncomingSms(req, res) {
    res.end('OK');

    if (!req.query.number) return;
    const numberWithSuffix = req.query.number.replace(/^\+/, '') + (config.number_suffix || '');

    partnerLastSeen.set(req.query.number, req.query.modem_imsi);

    history.push({
        ts: req.query.ts || moment().format('YYYY-MM-DD HH:mm:ss'),
        modem: {
            name: req.query.modem,
            imsi: req.query.modem_imsi,
            msisdn: req.query.modem_msisdn,
        },
        direction: 'INCOMING',
        partner: req.query.number,
        message: req.query.msg,
    });

    /*
    modems.set({
        name: req.query.modem,
        device: req.query.modem_device,
        imsi: req.query.modem_imsi,
        msisdn: req.query.modem_msisdn,
        networkId: req.query.modem.network_id,
        networkName: req.query.modem_network_name,
        signalStrength: req.query.modem_signal_strength,
        uptime: req.query.uptime,
        reportIp: req.query.report_ip || req.ip,
        reportPort: req.query.report_port,
        reportApikey: req.query.report_apikey,
        reportPathSms: req.query.report_path_sms || '/sms',
    });
    */

    const doNotForwardToCore = (req.query.number.search(/(\+)*62/) !== 0) || (req.query.number.length <= 8);
    logger.info('APISERVER: Incoming SMS', { modem: req.query.modem, from: req.query.number, from_with_suffix: numberWithSuffix, msg: req.query.msg, doNotForwardToCore });

    messagingService.onIncomingMessage({
        me: req.query.modem,
        partner: numberWithSuffix,
        partner_raw: req.query.number,
        msg: req.query.msg,
        origin_label: req.query.modem_imsi || 'UNKNOWN',
        origin_transport: 'SMS',
        origin_partner: req.query.number,
        do_not_forward_to_core: doNotForwardToCore,
    });
}

async function pageHistory(req, res) {
    res.json(await history.dump());
}

app.use(function(req, res, next) {
    if ( 
        req && req.path && typeof req.path === 'string' 
        && (
            req.path.search(/\/modems$/) >= 0 
            || req.path.search(/\/modems\/set$/) >= 0 
        )
    ) {
        next();
        return;
    }
    
    logger.verbose('APISERVER: Incoming http request', { ip: req.ip, path: req.path, url: req.url });
    next();
})

app.use('/apikey/:apikey', apikeyChecker);
app.get('/apikey/:apikey/on-sms', onIncomingSms);
app.get('/apikey/:apikey/inbox', onIncomingSms);
app.get('/apikey/:apikey/on-sms/inbox', onIncomingSms);
app.get('/apikey/:apikey/history', pageHistory);
app.use('/apikey/:apikey/modems', routerModems);
app.use('/apikey/:apikey/config/senders', routerConfigSenders);

const listenPort = (config && config.apiserver && config.apiserver.listen_port ? config.apiserver.listen_port : null)
    || (config && config.handler_callback_server ? config.handler_callback_server.listen_port : null);

if (listenPort) {
    app.listen(listenPort, () => {
        logger.info('HTTP Handler Callback server listening on port ' + listenPort);
    });
} else {
    logger.warn('Undefined config.apiserver.listen_port for APISERVER. Not listening for command.');
}