httpserver.js 7.6 KB
var http = require('http');
var nsr = require('node-simple-router');
var router = nsr();
var winston = require('winston');

var config;
var httpServer;
var aaa;
var logger;
var matrix;

function start(_config, options) {
    if (_config != undefined) {
        config = _config;
    }
    listenPort = config.globals.admin_port;
    
    if (options && options.aaa) {
        setAaa(options.aaa);
    }
    
    if (options && options.logger) {
        logger = options.logger;
    } else {
        logger = new winston.Logger({
            transports: [
              new (winston.transports.Console)()
            ]
        });
    }
    
    if (options && options.matrix) {
        matrix = options.matrix;
    } else {
        matrix = {
            pause: 0
        };
    }
    
    router.get("/info", function(request, response) {
        response.setHeader("Content-Type", "text/plain");
        response.write('CHIPINFO / GATEWAY NAME: ' + config.globals.gateway_name + "\n");
        response.write('PRODUCTS: ' + config.globals.products + "\n");
        response.write('AAA HOST: ' + config.globals.aaa_host + "\n");
        response.write('PARTNER: ' + config.h2h_out.partner + "\n");
        response.write('PAUSED: ' + matrix.pause + "\n");
        response.write('UPTIME: ' + process.uptime() + "\n");
        
        try {
            response.write('REQUESTS COUNT: ' + matrix.counter.trx.count + "\n");
            response.write('ACTIVE REQUESTS COUNT: ' + matrix.counter.trx.active + "\n");
            response.write('MAX ACTIVE REQUESTS COUNT: ' + matrix.counter.trx.active_max + "\n");
        }
        catch(err) {
        }
        
        if (aaa) {
            var pendings = aaa.getPendingList();
            if (Array.isArray(pendings)) {
                response.write('PENDING TRX: ' + pendings.join(',') + "\n");
                response.write('PENDING TRX COUNT: ' + pendings.length + "\n");
            }
        }
        
        response.end();
    });
    
    router.get("/pause/:apikey", function(request, response) {
        if (!config.globals.apikey) {
            response.end('Undefined APIKEY on config');
            return;
        }
        
        if (request.params.apikey != config.globals.apikey) {
            response.end('Invalid APIKEY');
            return;
        }
        
        try {
            aaa.pause();
            response.end('Paused');
        }
        catch(err) {
            response.end('Failed to pause');
        }
    });
    
    router.get("/resume/:apikey", function(request, response) {
        if (!config.globals.apikey) {
            response.end('Undefined APIKEY on config');
            return;
        }
        
        if (request.params.apikey != config.globals.apikey) {
            response.end('Invalid APIKEY');
            return;
        }
        
        try {
            aaa.resume();
            response.end('Resume');
        }
        catch(err) {
            response.end('Failed to resume');
        }
    });
    
    router.get("/reset-stats/:apikey", function(request, response) {
        if (!config.globals.apikey) {
            response.end('Undefined APIKEY on config');
            return;
        }
        
        if (request.params.apikey != config.globals.apikey) {
            response.end('Invalid APIKEY');
            return;
        }
        
        config.globals.max_active_requests_count = 0;
        
        response.writeHead(307, {
          'Location': '/info'
        });
        
        response.end();
    });
    
    router.get("/products/add/:product/:apikey", function(request, response) {
        if (!config.globals.apikey) {
            response.end('Undefined APIKEY on config');
            return;
        }
        
        if (request.params.apikey != config.globals.apikey) {
            response.end('Invalid APIKEY');
            return;
        }
        
        var new_product = request.params.product.toUpperCase();
        config.globals.products = config.globals.products.trim().toUpperCase();
        
        var products = [];
        if (config.globals.products) {
            products = config.globals.products.split(',');
        }
        
        if (aaa) {
            products = aaa.addProduct(new_product, products);
            config.globals.products = products.join(',');
        } else {
            logger.warn('AAA is not defined in httpserver.js');
        }
        
        response.end('Products: ' + config.globals.products);
    });
    
    router.get("/products/del/:product/:apikey", function(request, response) {
        if (!config.globals.apikey) {
            response.end('Undefined APIKEY on config');
            return;
        }
        
        if (request.params.apikey != config.globals.apikey) {
            response.end('Invalid APIKEY');
            return;
        }
        
        var obsolete_product = request.params.product.toUpperCase();
        config.globals.products = config.globals.products.trim().toUpperCase();
        
        var products = [];
        if (config.globals.products) {
            products = config.globals.products.split(',');
        }
        
        if (aaa) {
            products = aaa.delProduct(obsolete_product, products);
            config.globals.products = products.join(',');
        } else {
            logger.warn('AAA is not defined in httpserver.js');
        }
        
        response.end('Products: ' + config.globals.products);
    });
    
    router.get("/config/index/:apikey", function(request, response) {
        if (!config.globals.apikey) {
            response.end('Undefined APIKEY on config');
            return;
        }
        
        if (request.params.apikey != config.globals.apikey) {
            response.end('Invalid APIKEY');
            return;
        }
        response.end(JSON.stringify(config, null, '  '));
    });
    
    router.get("/config/save/:apikey", function(request, response) {
        if (!config.globals.apikey) {
            response.end('Undefined APIKEY on config');
            return;
        }
        
        if (request.params.apikey != config.globals.apikey) {
            response.end('Invalid APIKEY');
            return;
        }
        
        if (!aaa) {
            response.end('Undefined aaa');
            return;
        }
        aaa.backupAndSaveConfig();
        response.end('Saving...');
    });
    
    router.get("/config/load/:apikey", function(request, response) {
        if (!config.globals.apikey) {
            response.end('Undefined APIKEY on config');
            return;
        }
        
        if (request.params.apikey != config.globals.apikey) {
            response.end('Invalid APIKEY');
            return;
        }
        
        if (!aaa) {
            response.end('Undefined aaa');
            return;
        }
        aaa.loadConfig();
        response.end('Config (re)loaded');
    });
    
    router.get("/matrix/index/:apikey", function(request, response) {
        if (!config.globals.apikey) {
            response.end('Undefined APIKEY on config');
            return;
        }
        
        if (request.params.apikey != config.globals.apikey) {
            response.end('Invalid APIKEY');
            return;
        }
        
        if (!matrix) {
            response.end();
        }
        
        response.end(JSON.stringify(matrix, null, '  '));
    });

    httpServer = http.createServer(router).listen(listenPort);
    logger.info('HTTP server listens on port ' + listenPort);

    return httpServer;
}

function setConfig(_config) {
    config = _config;
}

function setAaa(_aaa) {
    aaa = _aaa;
}


exports.start = start;
exports.setConfig = setConfig;
exports.setAaa = setAaa;