Skip to content

Commit

Permalink
Improving the udp2dtls and dtls2udp usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4n3dw0lf committed Dec 17, 2017
1 parent 5e6c763 commit 9e8989f
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 87 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ $ openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout cert.key -out cert

- On a terminal
```
$ node bin/dtls2udp.js <Udp2Dtls Server>
$ node bin/dtls2udp 5684 5685 <UDP2DTLS SERVER IP> 5686 localhost 5683
```

- On another terminal
Expand All @@ -54,7 +54,7 @@ $ node examples/server/udp_server.js

- On a terminal
```
$ node bin/udp2dtls.js <Dtls2Udp Server>
$ node bin/udp2dtls 5686 5687 <DTLS2UDP SERVER IP> 5684
```

- On another terminal
Expand Down
59 changes: 59 additions & 0 deletions bin/dtls2udp
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)
});
});


44 changes: 0 additions & 44 deletions bin/dtls2udp.js

This file was deleted.

48 changes: 48 additions & 0 deletions bin/udp2dtls
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)
});

38 changes: 0 additions & 38 deletions bin/udp2dtls.js

This file was deleted.

4 changes: 1 addition & 3 deletions examples/client/udp_client.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
var dgram = require('dgram');

var udp2dtls_port = 5684;
var udp2dtls_port = 5687;
var udp2dtls_host = 'localhost';

var message = new Buffer('m4n3dw0lf');

var client = dgram.createSocket('udp4');

client.bind(5688);

client.on('message', function(msg, rinfo){
console.log(msg.toString());
client.close();
Expand Down

0 comments on commit 9e8989f

Please sign in to comment.