Update main.js
This commit is contained in:
parent
555000e874
commit
69ef6c68e7
54
main.js
54
main.js
@ -9,16 +9,39 @@
|
||||
*
|
||||
*********************************/
|
||||
|
||||
// 0. HELPER FUNCTIONS
|
||||
const os = require('os');
|
||||
const mysql = require('mysql2/promise');
|
||||
|
||||
|
||||
// 0. HELPER FUNCTIONS
|
||||
// Extract password from ~/.ssh/id_rsa.pub (last 6 characters of base64)
|
||||
function getDbPassword() {
|
||||
const pubKeyContent = fs.readFileSync(`${os.homedir()}/.ssh/id_rsa.pub`, "utf8");
|
||||
const base64Part = pubKeyContent.split(" ")[1];
|
||||
return base64Part.slice(-6);
|
||||
}
|
||||
// Insert data into MariaDB
|
||||
async function insertToMariaDB(record) {
|
||||
if (!['A', 'PTR', 'SRV'].includes(record.Type)) return;
|
||||
const dataStr = typeof record.Data === 'object' ? JSON.stringify(record.Data) : record.Data;
|
||||
try {
|
||||
await dbPool.execute(
|
||||
`REPLACE INTO ${DB_TABLE} (Host, Type, Name, Data) VALUES (?, ?, ?, ?);`,
|
||||
[DB_USER, record.Type, record.Name, dataStr]
|
||||
);
|
||||
} catch (err) {
|
||||
console.error("DB Insert Error:", err);
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Database functions
|
||||
function saveDbToFile(data, filename) {return fs.writeFileSync(filename,typeof data === 'object'?JSON.stringify(data):data);}
|
||||
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() {
|
||||
const os = require('os');
|
||||
|
||||
const interfaces = os.networkInterfaces();
|
||||
const addresses = [];
|
||||
@ -37,7 +60,7 @@ function getLocalNetworkAddressesIPs() {
|
||||
function sniffmDNSLocalPackets() {
|
||||
|
||||
// 1. Listen for responses
|
||||
mdns.on('response', function(response) {
|
||||
mdns.on('response', async function(response) {
|
||||
// console.log("got a new mDNS response.",response);
|
||||
for (let k in response.answers) {
|
||||
let answer = response.answers[k];
|
||||
@ -72,6 +95,11 @@ function sniffmDNSLocalPackets() {
|
||||
|
||||
db.local[answer.name][answer.type] = answer.data;
|
||||
}
|
||||
await insertToMariaDB({
|
||||
Type: answer.type,
|
||||
Name: answer.name,
|
||||
Data: answer.data
|
||||
});
|
||||
saveDbToFile(db,DB_FILENAME);
|
||||
}
|
||||
})
|
||||
@ -193,6 +221,24 @@ 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
|
||||
const DB_HOST = "10.10.8.1";
|
||||
const DB_NAME = "NETWORK";
|
||||
const DB_TABLE = "mDNS";
|
||||
const DB_USER = os.hostname();
|
||||
|
||||
|
||||
// Initialize MariaDB connection pool
|
||||
const dbPool = mysql.createPool({
|
||||
host: DB_HOST,
|
||||
user: DB_USER,
|
||||
password: getDbPassword(),
|
||||
database: DB_NAME,
|
||||
connectionLimit: 5
|
||||
});
|
||||
|
||||
|
||||
|
||||
sniffmDNSLocalPackets();
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user