diff --git a/index.js b/index.js index 574ae17..b8a00b1 100644 --- a/index.js +++ b/index.js @@ -20,6 +20,7 @@ const ejs = require("ejs") const mariadb = require('mariadb'); const bcrypt = require("bcrypt") const crypto = require("crypto") +const dns = require("dns"); const app = express(); const port = process.env.SERVER_PORT || 3000; @@ -657,6 +658,41 @@ app.get("/api/v1/directory/openBlocks", (req, res) => { }); }); +app.get("/api/v1/provision/:apiKey", async (req, res) => { + const apiKey = req.params.apiKey; + if (!apiKey) { + res.status(401).json({ error: 'Unauthorized' }); + return; + } + const serverData = await pool.query("SELECT * FROM routes WHERE apiKey = ?", [apiKey]); + if (!serverData || serverData.length === 0) { + res.status(401).json({ error: 'Unauthorized' }); + return; + } + const server = serverData[0]; + // Do a quick DNS lookup on server.server to see if it matches the IP making the request. If it doesn't, add 'warning': "IP Mismatch. Are you running this on the right server?" to the response but continue to send all data + dns.lookup(server.server, (err, address, family) => { + if (err) { + console.error('Error looking up DNS:', err); + return res.status(500).json({ error: 'Internal server error' }); + } + const responseData = { + server: server.server, // Informational + port: server.port, // Informational + inbound_context: server.auth, // IAX2 username and context + iax_secret: server.secret, // IAX2 password + block: server.block_start, // Used for generating context + api_key: server.apiKey // Used for authentication + }; + + if (address !== req.ip && address !== req.headers['x-forwarded-for']) { + responseData.warning = "IP Mismatch. Are you running this on the right server?"; + } + res.json(responseData); + }); + +}); + // Other public endpoints that need special handling discordInviteCache = { time: 0, url: "" };