This commit is contained in:
Christopher Cookman 2025-10-03 00:16:17 -06:00
parent 89ad48bdd9
commit a4b846f379

View file

@ -869,15 +869,29 @@ const logCall = (caller, callee) => {
}); });
} }
// Query to get a route const genCall = (req, res, apiKey, ani, number) => {
app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
const apiKey = req.params.apiKey;
const number = Number(req.params.number);
const ani = Number(req.params.ani);
pool.getConnection().then(conn => { pool.getConnection().then(conn => {
//conn.query("SELECT * FROM routes WHERE apiKey = ? AND block_start <= ? AND block_start + block_length >= ?", [apiKey, ani, ani]).then((rows) => { //conn.query("SELECT * FROM routes WHERE apiKey = ? AND block_start <= ? AND block_start + block_length >= ?", [apiKey, ani, ani]).then((rows) => {
conn.query('SELECT * FROM routes WHERE block_start <= ? AND block_start + block_length >= ? AND apiKey = ?;', [number, number, apiKey]).then((rows) => { conn.query("SELECT * FROM routes WHERE apiKey = ?", [apiKey]).then((rows) => { // We'll try this Nick, if it doesn't work we'll go back to the original
console.log(rows) const row = rows[0];
// If no row or error, return 401
if (!row) {
res.status(401).send(`${process.env.MSG_ROUTE_ADDRESS}/401`)
return;
}
// Validate the ani and number are 7 digit numbers
if (!ani || ani < 1000000 || ani > 9999999 || !number || number < 1000000 || number > 9999999) {
res.status(400).send(`${process.env.MSG_ROUTE_ADDRESS}/400`);
return;
}
// Validate the ani is owned by the apiKey
if (ani < row.block_start || ani > row.block_start + row.block_length) {
res.status(403).send(`${process.env.MSG_ROUTE_ADDRESS}/403`);
return;
}
conn.query('SELECT * FROM routes WHERE block_start <= ? AND block_start + block_length >= ?', [number, number]).then((rows) => {
const row = rows[0]; const row = rows[0];
if (row) { if (row) {
// Check if the ANI is within the block range // Check if the ANI is within the block range
@ -887,9 +901,6 @@ app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
// incriment estCallsMade analytics // incriment estCallsMade analytics
addAnalytic("estCallsMade"); addAnalytic("estCallsMade");
dailyAnalytic("dailyCallsMade"); dailyAnalytic("dailyCallsMade");
console.log(row);
console.log(`ANI: ${ani}, Block Start: ${row.block_start}, Block End: ${row.block_start + row.block_length}`);
console.log(ani >= row.block_start && ani <= row.block_start + row.block_length);
if (ani >= row.block_start && ani <= row.block_start + row.block_length) { if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
res.status(200).send('local'); res.status(200).send('local');
} else { } else {
@ -901,6 +912,7 @@ app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
}).catch(err => { }).catch(err => {
console.error('Error getting route:', err); console.error('Error getting route:', err);
res.status(500).send(`${process.env.MSG_ROUTE_ADDRESS}/500`) res.status(500).send(`${process.env.MSG_ROUTE_ADDRESS}/500`)
});
}).catch(err => { }).catch(err => {
console.error(err); console.error(err);
res.status(401).send(`${process.env.MSG_ROUTE_ADDRESS}/401`) res.status(401).send(`${process.env.MSG_ROUTE_ADDRESS}/401`)
@ -908,48 +920,22 @@ app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
conn.release(); conn.release();
}); });
}); });
}
// Query to get a route
app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
const apiKey = req.params.apiKey;
const number = Number(req.params.number);
const ani = Number(req.params.ani);
genCall(req, res, apiKey, ani, number);
}); });
app.get('/api/v1', (req, res) => { // Backwards compatibility with TandmX cause why not, it's easy app.get('/api/v1', (req, res) => { // Backwards compatibility with TandmX cause why not, it's easy
const apiKey = req.query.auth; const apiKey = req.query.auth;
const number = Number(req.query.number); const number = Number(req.query.number);
const ani = Number(req.query.ani); const ani = Number(req.query.ani);
pool.getConnection().then(conn => { genCall(req, res, apiKey, ani, number);
conn.query("SELECT * FROM routes WHERE apiKey = ? AND block_start <= ? AND block_start + block_length >= ?", [apiKey, ani, ani]).then((rows) => {
const row = rows[0];
// If no row or error, return 401
if (!row) {
res.status(401).send(`${process.env.MSG_ROUTE_ADDRESS}/401`)
return;
}
conn.query('SELECT * FROM routes WHERE block_start <= ? AND block_start + block_length >= ?', [number, number]).then((rows) => {
const row = rows[0];
if (row) {
// Check if the ANI is within the block range
// If it is, return `local`
console.log(`New Call: ${ani} -> ${number}`);
logCall(ani, number);
addAnalytic("estCallsMade");
dailyAnalytic("dailyCallsMade");
if (ani >= row.block_start && ani <= row.block_start + row.block_length) {
res.status(200).send('local');
} else {
res.status(200).send(`IAX2/${row.auth}:${row.secret}@${row.server}:${row.port}/${number}`);
}
} else {
res.status(404).send(`${process.env.MSG_ROUTE_ADDRESS}/404`);
}
}).catch(err => {
console.error('Error getting route:', err);
res.status(500).send(`${process.env.MSG_ROUTE_ADDRESS}/500`)
});
}).catch(err => {
console.error(err);
res.status(401).send(`${process.env.MSG_ROUTE_ADDRESS}/401`)
}).finally(() => {
conn.release();
});
});
}); });
// Management Routes (Like restarting the server) // Management Routes (Like restarting the server)