Permalink
Browse files

Reading/writing from/to tunnel on server-side websocket server.

  • Loading branch information...
spoletto committed Apr 8, 2012
1 parent 4c7d004 commit 60195d14a8480d7b0826c8711c35e674166a8d5d
Showing with 16 additions and 1 deletion.
  1. +16 −1 server/ws-server.js
View
@@ -6,13 +6,28 @@
var ws = require("websocket-server");
var server = ws.createServer();
var fd = fs.openSync('/dev/tun0', 'r+');
// Add a listener for new websocket connections.
server.addListener("connection", function(connection) {
console.log("Websocket connection established.")
connection.addListener("message", function(msg) {
console.log("recevied message " + msg);
// Write received websocket messages into the tunnel.
fs.write(fd, msg, 0, msg.length, null, function(err, written) {
if (error !== null) {
console.log('Write error: ' + error);
}
});
});
});
// Continuously read from the tunnel and broadcast IP packets to all connected websockets.
var buffer = new Buffer(500);
function network_traffic_to_websocket() {
fs.read(fd, buffer, 0, 500, 0, function(err, num) {
server.broadcast(buffer.toString(0, num));
network_traffic_to_websocket();
});
}
server.listen(8080);

0 comments on commit 60195d1

Please sign in to comment.