Skip to content

Commit

Permalink
feature: override for MFD addresses
Browse files Browse the repository at this point in the history
Add env variable MFD_ADDRESS_SCRIPT to allow overriding the
default MFD advertisement addresses with a shell script that
outputs a comma delimited list of addresses to advertise.
  • Loading branch information
tkurki committed Jul 23, 2023
1 parent 9611319 commit 0c2eb7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ To enable debugging without going through the Admin UI, see the file `~/.signalk
- `ALLOW_NEW_USER_REGISTRATION` (default is true).
- `ADMINUSER` force a account for admin user (username:password format).
- `PRESERIALCOMMAND` command to run before opening a serial port.
- `MFD_ADDRESS_SCRIPT` command to run to provide a comma separate list of server addresses to advertise to (Navico) MFDs.
- `SIGNALK_NODE_SETTINGS` override the path to the settings file.
- `SIGNALK_NODE_CONFIG_DIR` override the path to find server configuration. Includes all run-time changing content: configuration files, plugins, plugin configuration files, webapps, and so forth.

Expand Down
26 changes: 19 additions & 7 deletions src/interfaces/mfd_webapp.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import dgram from 'dgram'
import { promisify } from 'util'
import { exec } from 'child_process'
import { networkInterfaces } from 'os'
import { createDebug } from '../debug'
import { getExternalPort } from '../ports'
const PUBLISH_PORT = 2053
const MULTICAST_GROUP_IP = '239.2.1.1'
const debug = createDebug('signalk-server:interfaces:mfd_webapps')
const execP = promisify(exec)

// For debugging you can use
// tcpdump -i en0 -A -v net 239.2.1.1
Expand Down Expand Up @@ -67,18 +70,27 @@ const send = (
socket.bind(PUBLISH_PORT, fromAddress)
}

const getPublishToNavico = (protocol: string, port: number) => () => {
const getPublishToNavico = (protocol: string, port: number) => async () => {
let addresses: string[] = []
// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const [name, infos] of Object.entries(networkInterfaces())) {
for (const addressInfo of infos || []) {
if (addressInfo.family === 'IPv4') {
send(
getPublishMessage(protocol, addressInfo.address, port),
addressInfo.address,
MULTICAST_GROUP_IP,
PUBLISH_PORT
)
addresses.push(addressInfo.address)
}
}
}
if (process.env.MFD_ADDRESS_SCRIPT) {
addresses = (await execP(process.env.MFD_ADDRESS_SCRIPT)).stdout
.trim()
.split(',')
}
addresses.forEach((address) =>
send(
getPublishMessage(protocol, address, port),
address,
MULTICAST_GROUP_IP,
PUBLISH_PORT
)
)
}

0 comments on commit 0c2eb7b

Please sign in to comment.