diff --git a/README.md b/README.md index af67e200033694d6ed07a3dd1d9778e6e494f339..ea0c9285e7fcbc3448c7ebd7714863e022fdda26 100644 --- a/README.md +++ b/README.md @@ -32,11 +32,18 @@ Dependencies ------------ - [Knot DNS server](https://www.knot-dns.cz/) - Open source DNS server developed by the Czech domain registrar -- [MongoDB](https://www.mongodb.com/) - NoSQL database +- [MongoDB](https://www.mongodb.com/) - NoSQL database (unless you use the memory database, which is NOT recommended) Running ------- Setup: `npm install` -`sudo node index.js` \ No newline at end of file +`sudo node index.js` + +Configuration +------------- + +This software expects config in a file called `config.json`. In case this file not exists, it is copied from `config.json.defaults`. For all config options, look at `config.json.defaults`. + +**Database backend:** the default database backend is `"memory"`. It is volatile, the code contains a lot of linear searches, therefore it is suitable for testing only. To use MongoDB, change it to `mongodb`. \ No newline at end of file diff --git a/config.js b/config.js index eab05cb08372cc46dae1de8d19ff9fef9ca959f2..b03329d01c03401172106a87df2602918cc2d69e 100644 --- a/config.js +++ b/config.js @@ -17,7 +17,19 @@ function init() { config = JSON.parse(fs.readFileSync('config.json')); } +function getDb() { + switch (config.dbImplementation) { + case "mongodb": + return require('./mongodb'); + break; + case "memory": + default: + return require('./ramdb'); + } +} + module.exports = { init: init, + getDb: getDb, config: () => { return config; } } \ No newline at end of file diff --git a/config.json.defaults b/config.json.defaults index 1db1a57dbca628c2cec443f8fe000bcfd6da6f90..01569f7db04ebbebadf0e231671f4f2c4acf7537 100644 --- a/config.json.defaults +++ b/config.json.defaults @@ -1,6 +1,7 @@ { "higherLevelDomain": "dyndns.mydomain.com", "knotLocation": "/usr/sbin/knotc", + "dbImplementation": "memory", "dbHost": "localhost", "listenAddress": "127.0.0.1", "listenPort": 3000, diff --git a/index.js b/index.js index bf3ff2755899b622059a738dd36aeba45283ccf4..5f094f44f77f177f42175d7785fff9951941a5e9 100644 --- a/index.js +++ b/index.js @@ -3,7 +3,8 @@ const express = require('express'); const { error } = require('console'); const config = require('./config'); -const db = require('./database'); +config.init(); +const db = config.getDb(); const knotdns = require('./knotdns'); // number of times to try syncing DNS records from master before giving up @@ -117,7 +118,6 @@ function syncAndRun(retries) { const app = express(); -config.init(); db.init(); // Sync records from master then serve requests diff --git a/middleware/registerMW.js b/middleware/registerMW.js index 338f6264e81295c9c05513e5de337049136f1ae7..d568c7979a3006f6d4f5ddce9854a969ce64a3d2 100644 --- a/middleware/registerMW.js +++ b/middleware/registerMW.js @@ -1,7 +1,7 @@ const md5 = require('js-md5'); -const db = require('../database'); const config = require('../config'); +const db = config.getDb(); // Will register a new DynDNS domain name. diff --git a/middleware/updateMW.js b/middleware/updateMW.js index 4f2a16ab280cc903ba6cc9c8d4753b4192ca8b2d..f7c6b3ff89c9a1f1ec533037fdc086f0dc6aae83 100644 --- a/middleware/updateMW.js +++ b/middleware/updateMW.js @@ -1,5 +1,6 @@ -const db = require('../database'); const knotdns = require('../knotdns'); +const config = require('../config'); +const db = config.getDb(); // Will update an existing DNS record diff --git a/database.js b/mongodb.js similarity index 100% rename from database.js rename to mongodb.js diff --git a/ramdb.js b/ramdb.js new file mode 100644 index 0000000000000000000000000000000000000000..12335dcfa573ae99c0579a3be66d2bb6ac1a365e --- /dev/null +++ b/ramdb.js @@ -0,0 +1,87 @@ + +const config = require('./config'); + +var ramDB; + +function init() { + ramDB = []; +} + + +function domainExists(domain) { + return new Promise((resolve, reject) => { + for (record in ramDB) { + if (record.domain == domain) { + resolve(true); + return; + } + } + resolve(false); + }); +} + +function registerDomain(domain, token) { + return new Promise((resolve, reject) => { + const record = { + name: domain, + token: token, + ip: '0.0.0.0' + }; + ramDB.push(record); + resolve(); + }); +} + +function getDomain(token) { + return new Promise((resolve, reject) => { + for (record in ramDB) { + if (record.token == token) { + resolve(record); + return; + } + reject(); + } + }); +} + +function updateDomain(domain, token, ip) { + return new Promise((resolve, reject) => { + for (record in ramDB) { + if (record.domain == domain) { + record.token = token; + record.ip = ip; + resolve(); + return; + } + } + }); +} + +function insertDomain(domain, token, ip) { + return new Promise((resolve, reject) => { + const record = new Domain({ + name: domain, + token: token, + ip: ip + }); + ramDB.push(record); + resolve(); + }); +} + +function clearDomains() { + return new Promise((resolve, reject) => { + ramDB = []; + resolve(); + }); +} + +module.exports = { + init: init, + domainExists: domainExists, + registerDomain: registerDomain, + getDomain: getDomain, + updateDomain: updateDomain, + insertDomain: insertDomain, + clearDomains: clearDomains +} \ No newline at end of file