web-paging/index.js
2025-12-20 13:49:59 -07:00

63 lines
1.8 KiB
JavaScript

const express = require('express');
const { exec } = require('child_process');
require('dotenv').config();
function originateCall(number, context, delay, timeout, cid, variables = {}) {
// Build the base command
let command = `/usr/bin/ast_originate ${number} ${context} ${delay} ${timeout} ${Buffer.from(cid).toString('base64')}`;
// Add variables if provided
if (variables && typeof variables === 'object') {
const varString = Object.entries(variables)
.map(([key, value]) => `${key}=${value}`)
.join(' ');
if (varString) {
command += ` ${varString}`;
}
}
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(error);
} else {
resolve(stdout);
}
});
});
}
const app = express();
const HOST = process.env.HOST || 'localhost';
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.set('view engine', 'ejs');
app.set('views', './views');
app.use(express.static('static'));
function auth(req, res, next) {
// Temporary auth function. See if the user provided correct basic auth (process.env.USER and PASS)
const authHeader = req.headers['authorization'];
if (!authHeader) {
res.setHeader('WWW-Authenticate', 'Basic realm="Restricted Area"');
return res.status(401).send('Authentication required.');
}
const base64Credentials = authHeader.split(' ')[1];
const credentials = Buffer.from(base64Credentials, 'base64').toString('ascii');
const [username, password] = credentials.split(':');
if (username === process.env.USER && password === process.env.PASS) {
next();
} else {
return res.status(403).send('Forbidden');
}
}
app.get('/', auth, (req, res) => {
res.render('index', { user: process.env.USER });
});
app.listen(PORT, HOST, () => {
console.log(`Server running on http://${HOST}:${PORT}`);
});