removes the local-file persistence

removes the local-file persistence (saveDbToFile, loadDbFromFile and related fs/database.json bits) and relies solely on MariaDB for storing and fetching both local and remote mDNS records
This commit is contained in:
git 2025-06-26 10:01:19 +01:00
parent 65496fcae0
commit dfdaff628f

306
main.js
View File

@ -2,7 +2,6 @@
// A single-file mDNS sync tool acting as client or server // A single-file mDNS sync tool acting as client or server
// Uses mdns-server for mDNS and Express for HTTP // Uses mdns-server for mDNS and Express for HTTP
/********************************* /*********************************
* *
* 1. LIBRARY FUNCTIONS * 1. LIBRARY FUNCTIONS
@ -11,261 +10,166 @@
const os = require('os'); const os = require('os');
const mysql = require('mysql2/promise'); const mysql = require('mysql2/promise');
const mdns = require('mdns-server'); // we'll initialize below
// 0. HELPER FUNCTIONS // 0. HELPER FUNCTIONS
// Extract password from ~/.ssh/id_rsa.pub (last 6 characters of base64) // Extract password from ~/.ssh/id_rsa.pub (last 6 characters of base64)
function getDbPassword() { function getDbPassword() {
const pubKeyContent = fs.readFileSync(`${os.homedir()}/.ssh/id_rsa.pub`, "utf8"); const pubKeyContent = require('fs').readFileSync(
const base64Part = pubKeyContent.split(" ")[1]; `${os.homedir()}/.ssh/id_rsa.pub`,
'utf8'
);
const base64Part = pubKeyContent.split(' ')[1];
return base64Part.slice(-7, -1); return base64Part.slice(-7, -1);
} }
// Insert data into MariaDB
// Insert/update data into MariaDB
async function updateMariaDB(record) { async function updateMariaDB(record) {
if (!['A','PTR','SRV','TXT'].includes(record.Type)) return; const { Type, Name } = record;
if (!['A','PTR','SRV','TXT'].includes(Type)) return;
const host = DB_USER; const host = DB_USER;
const { Type, Name } = record;
const dataStr = typeof record.Data === 'object' const dataStr = typeof record.Data === 'object'
? JSON.stringify(record.Data) ? JSON.stringify(record.Data)
: record.Data; : record.Data;
try { try {
// INSERT new row, or if (Host,Type,Name) already exists, just update Data
const sql = ` const sql = `
INSERT INTO ${DB_TABLE} (Host, Type, Name, Data) INSERT INTO ${DB_TABLE} (Host, Type, Name, Data)
VALUES (?, ?, ?, ?) VALUES (?, ?, ?, ?)
ON DUPLICATE KEY UPDATE ON DUPLICATE KEY UPDATE Data = VALUES(Data)
Data = VALUES(Data)
`; `;
await dbPool.execute(sql, [host, Type, Name, dataStr]); await dbPool.execute(sql, [host, Type, Name, dataStr]);
} catch (err) { } catch (err) {
console.error("DB Upsert Error:", err); console.error('DB Upsert Error:', err);
} }
} }
// 1. Database functions // 1. Network functions
function saveDbToFile(data, filename) {
return fs.writeFileSync(filename,typeof data === 'object'?JSON.stringify(data):data);
}
function loadDbFromFile(filename) {if (!fs.existsSync(filename)) return {local:{},remote:{}}; else return JSON.parse(fs.readFileSync(filename));}
// 2. Network functions
function getLocalNetworkAddressesIPs() { function getLocalNetworkAddressesIPs() {
const interfaces = os.networkInterfaces(); const interfaces = os.networkInterfaces();
const addresses = []; return Object.values(interfaces)
.flat()
for (const name of Object.keys(interfaces)) { .filter(iface => iface.family === 'IPv4' && !iface.internal)
for (const iface of interfaces[name]) { .map(iface => iface.address);
if (iface.family === 'IPv4' && !iface.internal) {
addresses.push(iface.address);
}
}
}
return addresses;
} }
function sniffmDNSLocalPackets() { function sniffmDNSLocalPackets() {
mdns.on('response', async response => {
const answers = response.answers.concat(response.additionals);
// 1. Listen for responses for (const ans of answers) {
mdns.on('response', async function(response) { // only care about standard types
// console.log("got a new mDNS response.",response); if (!['A','PTR','SRV','TXT'].includes(ans.type)) continue;
let all_answers = response.answers.concat(response.additionals);
for (let k in all_answers) {
let answer = all_answers[k];
// 1. Handle DEVICES for a given SERVICE // For each answer, immediately upsert to MariaDB
if (answer.type == "PTR") { await updateMariaDB({
// 1.1 Filter by RELEVANT SERVICE TYPES Type: ans.type,
if (!["in-addr.arpa","_googlecast._tcp.local","_tcp.local"].some(suffix => answer.name.endsWith(suffix))) continue; Name: ans.name,
Data: ans.data
// 1.2 Initialize POINTERS (services) sub-database });
if (!db.local.PTR) db.local.PTR = {};
// 1.3 Initialize this SERVICE TYPE (e.g. _googlecast._tcp.local), if needed
if (!db.local.PTR[answer.name]) {
db.local.PTR[answer.name] = [answer.data];
} else {
// Add a new DEVICE to this SERVICE TYPE if needed
if (!db.local.PTR[answer.name].includes(answer.data))
db.local.PTR[answer.name].push(answer.data);
await updateMariaDB({Type: answer.type,Name: answer.name,Data: db.local.PTR[answer.name]});
} }
} });
// 2. Handle DEVICE IP resolution
if (answer.type == "A") { mdns.on('destroyed', () => {
// 2.1 Initialize ADDRESSES sub-database console.log('Server destroyed.'); process.exit(0);
if (!db.local.A) db.local.A = {}; });
db.local.A[answer.name] = answer.data;
await updateMariaDB({Type: answer.type,Name: answer.name,Data: db.local.A[answer.name]}); mdns.on('ready', () => {
console.log('mDNS server is ready...');
});
} }
if (answer.type == "SRV") { function replyLocallyWithRemoteDevicesData(name, type) {
// Initialize this device / entry, if needed
if (db.local[answer.name] === undefined) db.local[answer.name] = {"TXT":null,"SRV":null};
db.local[answer.name][answer.type] = answer.data;
await updateMariaDB({Type: answer.type,Name: answer.name,Data: db.local[answer.name][answer.type]});
}
if (answer.type == "TXT") {
// Initialize this device / entry, if needed
if (db.local[answer.name] === undefined) db.local[answer.name] = {"TXT":null,"SRV":null};
db.local[answer.name][answer.type] = answer.data;
await updateMariaDB({Type: answer.type,Name: answer.name,Data: db.local[answer.name][answer.type]});
}
saveDbToFile(db,DB_FILENAME);
}
})
// 2. Handle the server being destroyed
mdns.on('destroyed', function () {console.log('Server destroyed.');process.exit(0);});
// 3. Handle the onReady event
mdns.on('ready', function () {
console.log("mDNS server is ready...");
// mdns.query({questions:[{ name: '_:googlecast._tcp.local', type: 'PTR', class: 'IN'}]} );
})
}
function replyLocallyWithRemoteDevicesData(name,type,query) {
const answers = []; const answers = [];
if (!db.remote) db.remote = {}; if (type === 'PTR' && db.remote.PTR[name]) {
if (!db.remote.PTR) db.remote.PTR = {}; db.remote.PTR[name].forEach(ptrData =>
if (!db.remote.A) db.remote.A = {}; answers.push({ name, type:'PTR', class:'IN', ttl:120, data: ptrData })
);
// 1. If the query is asking for a PTR (pointer to a service)
if (type === 'PTR' && db.remote.PTR && db.remote.PTR[name]) {
for (let ptrData of db.remote.PTR[name]) {
answers.push({
name, // The service name being queried
type: 'PTR', // Type of DNS record
class: 'IN', // Internet class
ttl: 120, // Time-to-live (how long to cache)
data: ptrData // The actual pointer data (e.g. device instance name)
});
} }
} if (type === 'A' && db.remote.A[name]) {
// 2. If the query is for an A record (IPv4 address of a device)
if (type === 'A' && db.remote.A && db.remote.A[name]) {
answers.push({
name,
type: 'A',
class: 'IN',
ttl: 120,
data: db.remote.A[name] // The IPv4 address
});
}
// 3. If the query is for an SRV record (hostname + port of a service)
if (type === 'SRV' && db.remote.services[name] && db.remote.services[name].SRV !== null) {
answers.push({
name,
type: 'SRV',
class: 'IN',
ttl: 120,
data: db.remote.services[name].SRV // Must be an object like { port, target, priority, weight }
});
}
// 4. If the query is for a TXT record (extra metadata)
if (type === 'TXT' && db.remote.services[name] && db.remote.services[name].TXT !== null) {
answers.push({
name,
type: 'TXT',
class: 'IN',
ttl: 120,
// Convert plain string to buffer; required by mdns-server
data: db.remote.services[name].TXT.map(entry=>Buffer.from(entry.data,'utf8'))
});
}
// 5. Many mDNS tools (like Avahi or Bonjour) send type: 'ANY' queries to discover all records for a name.
if (type === 'ANY') {
// Respond with everything you know about this name
if (db.remote.PTR && db.remote.PTR[name]) {
for (let ptrData of db.remote.PTR[name]) {
answers.push({ name, type: 'PTR', class: 'IN', ttl: 120, data: ptrData });
}
}
if (db.remote.A && db.remote.A[name]) {
answers.push({ name, type:'A', class:'IN', ttl:120, data: db.remote.A[name] }); answers.push({ name, type:'A', class:'IN', ttl:120, data: db.remote.A[name] });
} }
if (db.remote.services[name]) { if (type === 'SRV' && db.remote.services[name]?.SRV) {
const r = db.remote.services[name]; answers.push({ name, type:'SRV', class:'IN', ttl:120, data: db.remote.services[name].SRV });
if (r.SRV) answers.push({ name, type: 'SRV', class: 'IN', ttl: 120, data: r.SRV });
if (r.TXT) answers.push({ name, type: 'TXT', class: 'IN', ttl: 120, data: r.TXT.map(entry=>Buffer.from(entry.data,'utf8')) });
} }
if (type === 'TXT' && db.remote.services[name]?.TXT) {
answers.push({
name, type:'TXT', class:'IN', ttl:120,
data: db.remote.services[name].TXT.map(e => Buffer.from(e.data, 'utf8'))
});
}
if (type === 'ANY') {
// same as above but for ANY
['PTR','A','SRV','TXT'].forEach(t =>
replyLocallyWithRemoteDevicesData(name, t)
);
} }
// 6. If we prepared any answers, respond to the query if (answers.length) {
if (answers.length > 0) {
console.log(`Responding with ${answers.length} answer(s) for ${name}`); console.log(`Responding with ${answers.length} answer(s) for ${name}`);
mdns.respond({ answers }); mdns.respond({ answers });
} }
} }
async function fetchRemoteDevicesData() { async function fetchRemoteDevicesData() {
try { try {
const [rows] = await dbPool.execute( const [rows] = await dbPool.execute(
`SELECT Type, Name, Data FROM ${DB_TABLE} WHERE Host <> ? AND MakeAvailableEverywhere = 1`, `SELECT Type, Name, Data
FROM ${DB_TABLE}
WHERE Host <> ?
AND MakeAvailableEverywhere = 1`,
[DB_USER] [DB_USER]
); );
// reinitialize remote cache
db.remote = { A: {}, PTR: {}, services: {} }; db.remote = { A: {}, PTR: {}, services: {} };
for (const { Type, Name, Data } of rows) {
rows.forEach(row => { const parsed = ['[','{'].includes(Data[0]) ? JSON.parse(Data) : Data;
const { Type, Name, Data } = row;
if (Type === 'A') { if (Type === 'A') {
db.remote.A[Name] = Data; db.remote.A[Name] = parsed;
} else if (Type === 'PTR') {
db.remote.PTR[Name] = parsed;
} else { // SRV or TXT
db.remote.services[Name] ||= { TXT:null, SRV:null };
db.remote.services[Name][Type] = parsed;
} }
else if (Type === 'PTR') {
db.remote.PTR[Name] = (["[","{"].includes(Data.substr(0,1))?JSON.parse(Data):Data);
} }
else if (['TXT', 'SRV'].includes(Type)) { console.log(
if (!db.remote.services[Name]) db.remote.services[Name] = { TXT: null, SRV: null }; `Remote DB updated. A: ${Object.keys(db.remote.A).length}, PTR: ${Object.keys(db.remote.PTR).length}`
db.remote.services[Name][Type] = (["[","{"].includes(Data.substr(0,1))?JSON.parse(Data):Data); );
}
});
console.log(`Remote DB updated. A records: ${Object.keys(db.remote.A).length} PTR records: ${Object.keys(db.remote.PTR).length}`);
} catch (err) { } catch (err) {
console.error('Error fetching remote DB:', err); console.error('Error fetching remote DB:', err);
} }
} }
/********************************* /*********************************
* *
* 100. MAIN / IMPERATIVE CODE * 100. MAIN / IMPERATIVE CODE
* *
*********************************/ *********************************/
const fs = require("fs");
const DB_FILENAME = 'database.json';
const db = loadDbFromFile(DB_FILENAME);
const LOCAL_IP_ADDR = getLocalNetworkAddressesIPs()[0];
const mdns = require('mdns-server')({interface: LOCAL_IP_ADDR,reuseAddr: true,loopback: false,noInit: true});
// MariaDB Configuration // MariaDB Configuration
const DB_HOST = "10.10.8.1"; const DB_HOST = '10.10.8.1';
const DB_NAME = "NETWORK"; const DB_NAME = 'NETWORK';
const DB_TABLE = "mDNS"; const DB_TABLE = 'mDNS';
const DB_USER = os.hostname(); const DB_USER = os.hostname();
// Initialize in-memory cache
const db = { remote: { A: {}, PTR: {}, services: {} } };
// Initialize MariaDB connection pool // Pick first non-internal v4 address
const LOCAL_IP_ADDR = getLocalNetworkAddressesIPs()[0];
const mdnsServer = mdns({
interface: LOCAL_IP_ADDR,
reuseAddr: true,
loopback: false,
noInit: true
});
// Init MariaDB connection pool
const dbPool = mysql.createPool({ const dbPool = mysql.createPool({
host: DB_HOST, host: DB_HOST,
user: DB_USER, user: DB_USER,
@ -273,27 +177,23 @@ const dbPool = mysql.createPool({
database: DB_NAME, database: DB_NAME,
connectionLimit: 5 connectionLimit: 5
}); });
console.log(`Trying to connect to central SQL server with host: ${DB_HOST} user: ${DB_USER} pass: ${getDbPassword()} db_name: ${DB_NAME}`);
console.log(
`Connecting to SQL ${DB_HOST} as ${DB_USER}, DB=${DB_NAME}`
);
sniffmDNSLocalPackets(); sniffmDNSLocalPackets();
mdnsServer.on('query', ({ questions }) => {
mdns.on('query', function (query) { for (const { name, type } of questions) {
// Loop through each question in the mDNS query console.log(`Query for ${name} (${type})`);
query.questions.forEach(q => { replyLocallyWithRemoteDevicesData(name, type);
const { name, type } = q; }
console.log(`Received mDNS query for ${name} (${type})`);
replyLocallyWithRemoteDevicesData(name,type,q);
});
}); });
// every 15 seconds // fetch remote every 15s
setInterval(fetchRemoteDevicesData, 15000); setInterval(fetchRemoteDevicesData, 15_000);
fetchRemoteDevicesData(); // initial immediate call fetchRemoteDevicesData();
// initialize the server now that we are watching for events // now start the server
mdns.initServer() mdnsServer.initServer();