Last active
December 14, 2015 22:37
-
-
Save dale3h/db9cd6829f4dee02e41f to your computer and use it in GitHub Desktop.
Simple NodeJS UDP forwarding server
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
/** | |
* Simple NodeJS UDP forwarding server | |
* | |
* @author dale3h | |
* | |
* This script was built to run on a Raspberry Pi that is connected to two networks (wired + wireless). | |
* By default it listens on port 443, but only serves HTTP (not HTTPS) | |
* | |
* @usage | |
* http://192.168.1.102:443/send-udp?host=192.168.1.113&port=12345&device=wlan0&command=PACKET_CONTENTS | |
* | |
* This would be a Raspberry Pi running at 192.168.1.102 (on eth0), forwarding a UDP packet with the | |
* contents "PACKET_CONTENTS" to host 192.168.1.113, port 12345, using the wlan0 interface. | |
* | |
* @dependency httpdispatcher | |
*/ | |
var http = require( 'http' ); | |
var dispatcher = require( 'httpdispatcher' ); | |
var util = require( 'util' ); | |
var exec = require( 'child_process' ).exec; | |
const LISTEN_PORT = 443; | |
const SEND_TRIES = 3; | |
function handleRequest( request, response ) { | |
try { | |
//console.log( request.url ); | |
dispatcher.dispatch( request, response ); | |
} catch( err ) { | |
console.log( err ); | |
} | |
} | |
function sendUDP( request, response ) { | |
var host = request.params.host || false; | |
var port = request.params.port || false; | |
var device = request.params.device || false; | |
var command = request.params.command || false; | |
var tries = request.params.tries || SEND_TRIES; | |
if ( ! host || ! port || ! command ) { | |
response.writeHead( 400, { 'Content-Type': 'text/plain' } ); | |
response.end( '400 Bad Request' ); | |
return; | |
} | |
sendUDPHelper( host, port, device, command, tries ); | |
response.writeHead( 200, { 'Content-Type': 'text/plain' } ); | |
response.end( command ); | |
} | |
function sendUDPHelper( host, port, device, command, tries ) { | |
var child; | |
var bindto = ''; | |
if ( device ) { | |
bindto = ',so-bindtodevice=' + device; | |
} | |
var cmd = 'echo \"' + command + '\" | sudo socat - UDP-DATAGRAM:' + host + ':' + port + bindto; | |
child = exec( cmd, function( error, stdout, stderr ) { | |
var header = '[' + getDateTime() + '] [' + (device ? device + ':' : '' ) + host + ':' + port + '] '; | |
if ( error !== null ) { | |
console.log( header + 'ERROR: ' + error ); | |
return; | |
} | |
console.log( header + command ); | |
} ); | |
if ( --tries > 0 ) { | |
sendUDPHelper( host, port, device, command, tries ); | |
} | |
} | |
function getDateTime() { | |
var date = new Date(); | |
var hour = date.getHours(); | |
hour = ( hour < 10 ? "0" : "" ) + hour; | |
var min = date.getMinutes(); | |
min = ( min < 10 ? "0" : "" ) + min; | |
var sec = date.getSeconds(); | |
sec = ( sec < 10 ? "0" : "" ) + sec; | |
var year = date.getFullYear(); | |
var month = date.getMonth() + 1; | |
month = ( month < 10 ? "0" : "" ) + month; | |
var day = date.getDate(); | |
day = ( day < 10 ? "0" : "" ) + day; | |
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec; | |
} | |
dispatcher.onGet( '/send-udp', sendUDP ); | |
dispatcher.onPost( '/send-udp', sendUDP ); | |
var server = http.createServer( handleRequest ); | |
server.listen( LISTEN_PORT, function() { | |
console.log( '[' + getDateTime() + '] Server listening on: http://localhost:%s', LISTEN_PORT ); | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment