diff --git a/index.js b/index.js index 2c6bde5..0ac00ca 100644 --- a/index.js +++ b/index.js @@ -869,59 +869,28 @@ const logCall = (caller, callee) => { }); } -// 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); +const genCall = (req, res, apiKey, ani, number) => { 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 block_start <= ? AND block_start + block_length >= ? AND apiKey = ?;', [number, number, apiKey]).then((rows) => { - console.log(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); - // incriment estCallsMade analytics - addAnalytic("estCallsMade"); - 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) { - 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(); - }); - }); -}); - -app.get('/api/v1', (req, res) => { // Backwards compatibility with TandmX cause why not, it's easy - const apiKey = req.query.auth; - const number = Number(req.query.number); - const ani = Number(req.query.ani); - 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 = ?", [apiKey]).then((rows) => { // We'll try this Nick, if it doesn't work we'll go back to the original 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]; if (row) { @@ -929,6 +898,7 @@ app.get('/api/v1', (req, res) => { // Backwards compatibility with TandmX cause // If it is, return `local` console.log(`New Call: ${ani} -> ${number}`); logCall(ani, number); + // incriment estCallsMade analytics addAnalytic("estCallsMade"); dailyAnalytic("dailyCallsMade"); if (ani >= row.block_start && ani <= row.block_start + row.block_length) { @@ -950,6 +920,22 @@ app.get('/api/v1', (req, res) => { // Backwards compatibility with TandmX cause 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 + const apiKey = req.query.auth; + const number = Number(req.query.number); + const ani = Number(req.query.ani); + genCall(req, res, apiKey, ani, number); }); // Management Routes (Like restarting the server)