You've already forked fm-dx-webserver
mirror of
https://github.com/KubaPro010/fm-dx-webserver.git
synced 2026-02-26 22:13:53 +01:00
TX ID, UI fixes
This commit is contained in:
145
index.js
145
index.js
@@ -10,7 +10,6 @@ const http = require('http');
|
||||
const https = require('https');
|
||||
const app = express();
|
||||
const httpServer = http.createServer(app);
|
||||
const ejs = require('ejs');
|
||||
|
||||
// Websocket handling
|
||||
const WebSocket = require('ws');
|
||||
@@ -43,13 +42,13 @@ let serverConfig = {
|
||||
xdrd: {
|
||||
xdrdIp: "127.0.0.1",
|
||||
xdrdPort: "7373",
|
||||
xdrdPassword: "password"
|
||||
xdrdPassword: ""
|
||||
},
|
||||
identification: {
|
||||
tunerName: "",
|
||||
tunerDesc: "",
|
||||
lat: "",
|
||||
lon: ""
|
||||
lat: "0",
|
||||
lon: "0"
|
||||
},
|
||||
password: {
|
||||
tunePass: "",
|
||||
@@ -102,78 +101,80 @@ function authenticateWithXdrd(client, salt, password) {
|
||||
}
|
||||
|
||||
// xdrd connection
|
||||
client.connect(serverConfig.xdrd.xdrdPort, serverConfig.xdrd.xdrdIp, () => {
|
||||
logInfo('Connection to xdrd established successfully.');
|
||||
|
||||
const authFlags = {
|
||||
authMsg: false,
|
||||
firstClient: false,
|
||||
receivedPassword: false
|
||||
};
|
||||
|
||||
const authDataHandler = (data) => {
|
||||
const receivedData = data.toString();
|
||||
const lines = receivedData.split('\n');
|
||||
|
||||
for (const line of lines) {
|
||||
|
||||
if (!authFlags.receivedPassword) {
|
||||
authFlags.receivedSalt = line.trim();
|
||||
authenticateWithXdrd(client, authFlags.receivedSalt, serverConfig.xdrd.xdrdPassword);
|
||||
authFlags.receivedPassword = true;
|
||||
} else {
|
||||
if (line.startsWith('a')) {
|
||||
authFlags.authMsg = true;
|
||||
logWarn('Authentication with xdrd failed. Is your password set correctly?');
|
||||
} else if (line.startsWith('o1,')) {
|
||||
authFlags.firstClient = true;
|
||||
} else if (line.startsWith('T') && line.length <= 7) {
|
||||
const freq = line.slice(1) / 1000;
|
||||
dataHandler.dataToSend.freq = freq.toFixed(3);
|
||||
} else if (line.startsWith('OK')) {
|
||||
authFlags.authMsg = true;
|
||||
logInfo('Authentication with xdrd successful.');
|
||||
}
|
||||
|
||||
if (authFlags.authMsg && authFlags.firstClient) {
|
||||
client.write('T87500\n');
|
||||
client.write('A0\n');
|
||||
client.write('G11\n');
|
||||
client.off('data', authDataHandler);
|
||||
return;
|
||||
if (serverConfig.xdrd.xdrdPassword.length > 1) {
|
||||
client.connect(serverConfig.xdrd.xdrdPort, serverConfig.xdrd.xdrdIp, () => {
|
||||
logInfo('Connection to xdrd established successfully.');
|
||||
|
||||
const authFlags = {
|
||||
authMsg: false,
|
||||
firstClient: false,
|
||||
receivedPassword: false
|
||||
};
|
||||
|
||||
const authDataHandler = (data) => {
|
||||
const receivedData = data.toString();
|
||||
const lines = receivedData.split('\n');
|
||||
|
||||
for (const line of lines) {
|
||||
|
||||
if (!authFlags.receivedPassword) {
|
||||
authFlags.receivedSalt = line.trim();
|
||||
authenticateWithXdrd(client, authFlags.receivedSalt, serverConfig.xdrd.xdrdPassword);
|
||||
authFlags.receivedPassword = true;
|
||||
} else {
|
||||
if (line.startsWith('a')) {
|
||||
authFlags.authMsg = true;
|
||||
logWarn('Authentication with xdrd failed. Is your password set correctly?');
|
||||
} else if (line.startsWith('o1,')) {
|
||||
authFlags.firstClient = true;
|
||||
} else if (line.startsWith('T') && line.length <= 7) {
|
||||
const freq = line.slice(1) / 1000;
|
||||
dataHandler.dataToSend.freq = freq.toFixed(3);
|
||||
} else if (line.startsWith('OK')) {
|
||||
authFlags.authMsg = true;
|
||||
logInfo('Authentication with xdrd successful.');
|
||||
}
|
||||
|
||||
if (authFlags.authMsg && authFlags.firstClient) {
|
||||
client.write('T87500\n');
|
||||
client.write('A0\n');
|
||||
client.write('G11\n');
|
||||
client.off('data', authDataHandler);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
client.on('data', (data) => {
|
||||
var receivedData = incompleteDataBuffer + data.toString();
|
||||
const isIncomplete = (receivedData.slice(-1) != '\n');
|
||||
|
||||
if (isIncomplete) {
|
||||
const position = receivedData.lastIndexOf('\n');
|
||||
if (position < 0) {
|
||||
incompleteDataBuffer = receivedData;
|
||||
receivedData = '';
|
||||
} else {
|
||||
incompleteDataBuffer = receivedData.slice(position + 1);
|
||||
receivedData = receivedData.slice(0, position + 1);
|
||||
}
|
||||
} else {
|
||||
incompleteDataBuffer = '';
|
||||
}
|
||||
|
||||
if (receivedData.length) {
|
||||
wss.clients.forEach((client) => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
dataHandler.handleData(client, receivedData);
|
||||
};
|
||||
|
||||
client.on('data', (data) => {
|
||||
var receivedData = incompleteDataBuffer + data.toString();
|
||||
const isIncomplete = (receivedData.slice(-1) != '\n');
|
||||
|
||||
if (isIncomplete) {
|
||||
const position = receivedData.lastIndexOf('\n');
|
||||
if (position < 0) {
|
||||
incompleteDataBuffer = receivedData;
|
||||
receivedData = '';
|
||||
} else {
|
||||
incompleteDataBuffer = receivedData.slice(position + 1);
|
||||
receivedData = receivedData.slice(0, position + 1);
|
||||
}
|
||||
});
|
||||
}
|
||||
} else {
|
||||
incompleteDataBuffer = '';
|
||||
}
|
||||
|
||||
if (receivedData.length) {
|
||||
wss.clients.forEach((client) => {
|
||||
if (client.readyState === WebSocket.OPEN) {
|
||||
dataHandler.handleData(client, receivedData);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
client.on('data', authDataHandler);
|
||||
});
|
||||
|
||||
client.on('data', authDataHandler);
|
||||
});
|
||||
}
|
||||
|
||||
client.on('close', () => {
|
||||
logWarn('Disconnected from xdrd.');
|
||||
|
||||
Reference in New Issue
Block a user