'use strict'; const dgram = require('dgram'); var ifaces = [ {udp6:"::%eth1", udp4:"192.168.1.233"}, {udp6:"::%wlan1", udp4:"192.168.192.11"} ]; var socks = [ {port:'36000', dir:"in", prot:"udp6", iface:ifaces[0], mcast:"ff02::1:5"}, {port:'36001', dir:"in", prot:"udp6", iface:ifaces[1], mcast:"ff02::1:6"}, {port:'34000', dir:"in", prot:"udp4", iface:ifaces[0], mcast:"233.252.2.15"}, {port:'34001', dir:"in", prot:"udp4", iface:ifaces[1], mcast:"233.252.2.16"}, {port:'44000', dir:"out", prot:"udp4", iface:ifaces[0]}, {port:'44001', dir:"out", prot:"udp4", iface:ifaces[1]}, {port:'46000', dir:"out", prot:"udp6", iface:ifaces[0]}, {port:'46001', dir:"out", prot:"udp6", iface:ifaces[1]} ]; for (var i in socks) { socks[i].sock = dgram.createSocket(socks[i].prot, {reuseAddr:true}); } var sin = socks.filter(x => x.dir == 'in'); for (let j of sin) { j.sock.on('message', (data, source) => { console.log(j.port,' -> ', data.toString()); }); j.sock.bind(j.port, () => { j.sock.addMembership(j.mcast, j.iface[j.prot]); }) } var sout = socks.filter(x => x.dir == 'out'); let trySend = function(o,i) { let buf = Buffer.from(`${o.prot} ${o.iface.udp6} ${i.mcast}`); o.sock.send(buf, 0, buf.length, i.port, i.mcast); } for (let o of sout) { o.sock.bind(o.port, () => { o.sock.setMulticastInterface(o.iface[o.prot]); for (let i of sin) { if (i.prot == o.prot) setInterval(()=>trySend(o, i),10000); } }); }