1
0
mirror of https://github.com/KubaPro010/fm-dx-webserver.git synced 2026-02-27 06:23:53 +01:00
This commit is contained in:
NoobishSVK
2024-09-11 23:49:09 +02:00
2 changed files with 37 additions and 6 deletions

View File

@@ -63,6 +63,21 @@ app.use(bodyParser.json());
connectToXdrd(); connectToXdrd();
connectToSerial(); connectToSerial();
// Check for working IPv6
function checkIPv6Support(callback) {
const server = net.createServer();
server.listen(0, '::1', () => {
server.close(() => callback(true));
}).on('error', (error) => {
if (error.code === 'EADDRNOTAVAIL') {
callback(false);
} else {
callback(false);
}
});
}
// Serial Connection // Serial Connection
function connectToSerial() { function connectToSerial() {
if (serverConfig.xdrd.wirelessConnection === false) { if (serverConfig.xdrd.wirelessConnection === false) {
@@ -490,13 +505,29 @@ httpServer.on('upgrade', (request, socket, head) => {
app.use(express.static(path.join(__dirname, '../web'))); // Serve the entire web folder to the user app.use(express.static(path.join(__dirname, '../web'))); // Serve the entire web folder to the user
httpServer.listen(serverConfig.webserver.webserverPort, serverConfig.webserver.webserverIp, () => { // Determine ip stack support and start server accordingly
let currentAddress = serverConfig.webserver.webserverIp; checkIPv6Support((isIPv6Supported) => {
currentAddress == '0.0.0.0' ? currentAddress = 'localhost' : currentAddress = serverConfig.webserver.webserverIp; const ipv4Address = serverConfig.webserver.webserverIp === '0.0.0.0' ? 'localhost' : serverConfig.webserver.webserverIp;
if(configExists()) { const ipv6Address = '::'; // This will bind to all available IPv6 interfaces
logInfo(`Web server has started on address \x1b[34mhttp://${currentAddress}:${serverConfig.webserver.webserverPort}\x1b[0m.`);
if (isIPv6Supported) {
// Start server on both IPv4 and IPv6 addresses
httpServer.listen(serverConfig.webserver.webserverPort, ipv4Address, () => {
logInfo(`Web server has started on address \x1b[34mhttp://${ipv4Address}:${serverConfig.webserver.webserverPort}\x1b[0m.`);
});
httpServer.listen(serverConfig.webserver.webserverPort, ipv6Address, () => {
logInfo(`Web server has started on address \x1b[34mhttp://[${ipv6Address}]:${serverConfig.webserver.webserverPort}\x1b[0m.`);
});
} else { } else {
logInfo(`Open your browser and proceed to \x1b[34mhttp://${currentAddress}:${serverConfig.webserver.webserverPort}\x1b[0m to continue with setup.`); // Start server only on IPv4 address
httpServer.listen(serverConfig.webserver.webserverPort, ipv4Address, () => {
if (configExists()) {
logInfo(`Web server has started on address \x1b[34mhttp://${ipv4Address}:${serverConfig.webserver.webserverPort}\x1b[0m.`);
} else {
logInfo(`Open your browser and proceed to \x1b[34mhttp://${ipv4Address}:${serverConfig.webserver.webserverPort}\x1b[0m to continue with setup.`);
}
});
} }
}); });

Binary file not shown.