Gug
This commit is contained in:
parent
89ad48bdd9
commit
a4b846f379
78
index.js
78
index.js
|
|
@ -869,15 +869,29 @@ 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)
|
||||
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) {
|
||||
// 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
|
||||
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 {
|
||||
|
|
@ -901,6 +912,7 @@ app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
|
|||
}).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`)
|
||||
|
|
@ -908,48 +920,22 @@ app.get('/api/v1/route/:apiKey/:ani/:number', (req, res) => {
|
|||
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);
|
||||
pool.getConnection().then(conn => {
|
||||
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();
|
||||
});
|
||||
});
|
||||
genCall(req, res, apiKey, ani, number);
|
||||
});
|
||||
|
||||
// Management Routes (Like restarting the server)
|
||||
|
|
|
|||
Loading…
Reference in a new issue