2026-02-10 09:53:46 +01:00
|
|
|
import { networkInterfaces } from 'os'
|
2026-06-20 15:42:57 +02:00
|
|
|
import { readFileSync, existsSync } from 'fs'
|
|
|
|
|
import { execSync } from 'child_process'
|
|
|
|
|
|
|
|
|
|
function isWSL() {
|
|
|
|
|
try {
|
|
|
|
|
return existsSync('/proc/sys/kernel/osrelease') &&
|
|
|
|
|
readFileSync('/proc/sys/kernel/osrelease', 'utf8').toLowerCase().includes('microsoft')
|
|
|
|
|
} catch { return false }
|
|
|
|
|
}
|
2026-02-10 09:53:46 +01:00
|
|
|
|
2026-06-21 00:36:02 +02:00
|
|
|
// Un IPv4 è "pubblicabile" in LAN se non è loopback, link-local o bridge Docker.
|
|
|
|
|
function isLanIPv4(address) {
|
|
|
|
|
return !!address
|
|
|
|
|
&& !address.startsWith('127.')
|
|
|
|
|
&& !address.startsWith('169.254.')
|
|
|
|
|
&& !address.startsWith('172.')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Estrae gli IP LAN da un oggetto in stile os.networkInterfaces().
|
|
|
|
|
// Esportata per poter essere testata in isolamento, senza dipendere dalla piattaforma.
|
|
|
|
|
export function collectIPs(nets) {
|
|
|
|
|
const out = []
|
|
|
|
|
for (const name of Object.keys(nets || {})) {
|
|
|
|
|
for (const net of nets[name]) {
|
|
|
|
|
if (net.family === 'IPv4' && !net.internal && isLanIPv4(net.address)) {
|
|
|
|
|
out.push(net.address)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Restituisce gli IP di rete della macchina.
|
|
|
|
|
// Passando `nets` (oggetto in stile os.networkInterfaces) si forza il percorso
|
|
|
|
|
// deterministico, scavalcando il rilevamento WSL/PowerShell: utile nei test.
|
|
|
|
|
export function getNetworkIPs(nets) {
|
|
|
|
|
if (nets) return collectIPs(nets)
|
|
|
|
|
|
2026-06-20 15:42:57 +02:00
|
|
|
if (isWSL()) {
|
|
|
|
|
try {
|
|
|
|
|
const out = execSync(
|
|
|
|
|
'powershell.exe -NoProfile -Command "Get-NetIPAddress -AddressFamily IPv4 | Select-Object -ExpandProperty IPAddress"',
|
|
|
|
|
{ timeout: 3000 }
|
|
|
|
|
)
|
|
|
|
|
return out.toString().trim().split('\n')
|
|
|
|
|
.map(s => s.trim())
|
2026-06-21 00:36:02 +02:00
|
|
|
.filter(isLanIPv4)
|
2026-06-20 15:42:57 +02:00
|
|
|
} catch { return [] }
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-21 00:36:02 +02:00
|
|
|
return collectIPs(networkInterfaces())
|
2026-02-10 09:53:46 +01:00
|
|
|
}
|
|
|
|
|
|
2026-06-21 00:36:02 +02:00
|
|
|
// `networkIPs` è iniettabile per rendere la stampa testabile in modo deterministico.
|
|
|
|
|
export function printServerInfo(port = 3000, networkIPs = getNetworkIPs()) {
|
2026-02-10 09:53:46 +01:00
|
|
|
console.log(`\nSegnapunti Server`)
|
2026-05-12 12:22:43 +02:00
|
|
|
console.log(` Display: http://127.0.0.1:${port}/display`)
|
|
|
|
|
console.log(` Controller: http://127.0.0.1:${port}/controller`)
|
2026-02-10 09:53:46 +01:00
|
|
|
|
|
|
|
|
if (networkIPs.length > 0) {
|
2026-06-20 15:42:57 +02:00
|
|
|
console.log(`\n Da dispositivi remoti:`)
|
2026-02-10 09:53:46 +01:00
|
|
|
networkIPs.forEach(ip => {
|
2026-06-20 15:42:57 +02:00
|
|
|
console.log(` Display: http://${ip}:${port}/display`)
|
|
|
|
|
console.log(` Controller: http://${ip}:${port}/controller`)
|
2026-02-10 09:53:46 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log()
|
|
|
|
|
}
|