-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving the udp2dtls and dtls2udp usage.
- Loading branch information
Showing
6 changed files
with
110 additions
and
87 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
function help(){ | ||
console.log('\nnode-dtls-proxy\n\nusage:\n $ node bin/dtls2udp <DTLS_LISTEN_PORT> <UDP_LISTEN_PORT> <DTLS_ENDPOINT> <DTLS_ENDPOINT_PORT> <UDP_ENDPOINT> <UDP_ENDPOINT_PORT> \n\nexample:\n $ node bin/dtls2udp 5684 5685 localhost 5686 localhost 5683\n') | ||
} | ||
|
||
require('dotenv').config() | ||
var dtls_endpoint = process.env.DTLS_ENDPOINT | ||
var dtls_endpointPort = process.env.DTLS_ENDPOINT_PORT | ||
var endpoint = process.env.UDP_ENDPOINT | ||
var endpointPort = process.env.UDP_ENDPOINT_PORT | ||
var dtls_listen_port = process.env.DTLS_LISTEN_PORT | ||
var udp_listen_port = process.env.UDP_LISTEN_PORT | ||
|
||
if (typeof(udp_listen_port) != "undefined" || typeof(dtls_listen_port) != "undefined" || typeof(dtls_endpoint) != "undefined" || typeof(dtls_endpointPort) != "undefined" || typeof(endpoint) != "undefined" || typeof(endpointPort) != "undefined") | ||
{} | ||
else { | ||
dtls_listen_port = process.argv[2] | ||
udp_listen_port = process.argv[3] | ||
dtls_endpoint = process.argv[4] | ||
dtls_endpointPort = process.argv[5] | ||
endpoint = process.argv[6] | ||
endpointPort = process.argv[7] | ||
} | ||
|
||
if (typeof(udp_listen_port) == "undefined" || typeof(dtls_listen_port) == "undefined" || typeof(dtls_endpoint) == "undefined" || typeof(dtls_endpointPort) == "undefined" || typeof(endpoint) == "undefined" || typeof(endpointPort) == "undefined"){ | ||
help() | ||
process.exit() | ||
} | ||
|
||
console.log("[+] Starting UDP2DTLS Proxy") | ||
|
||
const index = require('../') | ||
, dtls = index.createDTLSServer("cert.crt","cert.key",dtls_listen_port,"udp4") | ||
, dtls_client = require('node-dtls') | ||
, dgram = require('dgram') | ||
|
||
|
||
|
||
udp = dgram.createSocket("udp4") | ||
udp.bind(udp_listen_port) | ||
console.log("UDP Server listening on port: ", udp_listen_port) | ||
|
||
var client = dtls_client.connect( dtls_endpointPort, dtls_endpoint, 'udp4') | ||
udp.on('message', function (msg, rinfo) { | ||
console.log("Forwarding UDP response over DTLS from:",[rinfo.address,rinfo.port].join(":"), "to DTLS endpoint:", [dtls_endpoint,dtls_endpointPort].join(":")); | ||
client.send(msg); | ||
//console.log(msg.toString()) | ||
}); | ||
|
||
|
||
|
||
dtls.on( 'secureConnection', function( socket ) { | ||
console.log("Got a DTLS Connection from:",[socket.rinfo.address,socket.rinfo.port].join(":")) | ||
socket.on( 'message', function( message ) { | ||
console.log("Forwarding DTLS message from:", [socket.rinfo.address,socket.rinfo.port].join(":"), "to UDP endpoint:", [endpoint,endpointPort].join(":")); | ||
udp.send(message, 0, message.length, endpointPort, endpoint) | ||
}); | ||
}); | ||
|
||
|
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
function help(){ | ||
console.log('\nnode-dtls-proxy\n\nusage:\n $ node bin/udp2dtls <DTLS_LISTEN_PORT> <UDP_LISTEN_PORT> <DTLS_ENDPOINT> <DTLS_ENDPOINT_PORT> \n\nexample:\n $ node bin/udp2dtls 5686 5687 localhost 5684\n') | ||
} | ||
|
||
require('dotenv').config() | ||
var dtls_endpoint = process.env.DTLS_ENDPOINT | ||
var dtls_endpointPort = process.env.DTLS_ENDPOINT_PORT | ||
var dtls_listen_port = process.env.DTLS_LISTEN_PORT | ||
var udp_listen_port = process.env.UDP_LISTEN_PORT | ||
|
||
if (typeof(udp_listen_port) != "undefined" || typeof(dtls_listen_port) != "undefined" || typeof(dtls_endpoint) != "undefined" || typeof(dtls_endpointPort) != "undefined") | ||
{} | ||
else { | ||
dtls_listen_port = process.argv[2] | ||
udp_listen_port = process.argv[3] | ||
dtls_endpoint = process.argv[4] | ||
dtls_endpointPort = process.argv[5] | ||
} | ||
|
||
if (typeof(udp_listen_port) == "undefined" || typeof(dtls_listen_port) == "undefined" || typeof(dtls_endpoint) == "undefined" || typeof(dtls_endpointPort) == "undefined"){ | ||
help() | ||
process.exit() | ||
} | ||
|
||
console.log("[+] Starting UDP2DTLS Proxy"); | ||
|
||
const index = require('../') | ||
, udp = index.createUDPServer(udp_listen_port,"udp4") | ||
, dtls = require('node-dtls') | ||
, dtls_server = index.createDTLSServer("cert.crt","cert.key",dtls_listen_port,"udp4") | ||
|
||
dtls_server.on( 'secureConnection', function( socket ){ | ||
console.log("Got a DTLS Connection from:", [socket.rinfo.address, socket.rinfo.port].join(":")); | ||
socket.on('message', function(message){ | ||
console.log("Forwarding DTLS message from:", [socket.rinfo.address,socket.rinfo.port].join(":"), "to UDP client:", [udp_endpoint,udp_endpointPort].join(":")); | ||
udp.send(message, 0, message.length, udp_endpointPort, udp_endpoint); | ||
}); | ||
}); | ||
|
||
|
||
var client = dtls.connect( dtls_endpointPort, dtls_endpoint, 'udp4') | ||
udp.on('message', function (message, rinfo) { | ||
udp_endpoint = rinfo.address | ||
udp_endpointPort = rinfo.port | ||
console.log("Forwarding UDP message from:", [rinfo.address,rinfo.port].join(":"), "to DTLS server:", [udp_endpoint, udp_endpointPort].join(":")); | ||
client.send(message) | ||
}); | ||
|
This file was deleted.
Oops, something went wrong.
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