Created
May 4, 2018 04:08
-
-
Save max-mapper/d5b4dc9b9af64704d0548afcc8ef1d22 to your computer and use it in GitHub Desktop.
proxy multicast from one interface to another (node.js)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// proxies 239.255.42.42:5004 from eth0 239.1.2.3:6666 wlan0 | |
var dgram = require('dgram') | |
var os = require('os') | |
var socket = dgram.createSocket({ type: 'udp4', reuseAddr: true }) | |
socket.bind(5004, function () { | |
socket.addMembership('239.255.42.42', getIpForInterface('eth0')) | |
socket.setMulticastTTL(255) | |
}) | |
var proxy = dgram.createSocket({type: 'udp4', reuseAddr: true}) | |
proxy.bind(6666, '192.168.1.35', function () { | |
proxy.addMembership('239.1.2.3', '192.168.1.35') | |
}) | |
socket.on('message', function (m) { | |
if (!m.length) return | |
proxy.send(m, 0, m.length, '6666', '239.1.2.3', function (err) { | |
if (err) throw err | |
}) | |
}) | |
function getIpForInterface (ifname) { | |
return os.networkInterfaces()[ifname].reduce(function (i, j) { | |
if (i.family === 'IPv4') return i | |
return j | |
}).address | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment