Skip to content
Browse files

Added SSL to Python/node servers

  • Loading branch information...
1 parent a648141 commit a207999a48190ed80e630759c4b658e314148c2a @jawj committed
Showing with 27 additions and 11 deletions.
  1. +21 −9 server/forwarding.coffee
  2. +6 −2 server/websocket-kinect.py
View
30 server/forwarding.coffee
@@ -1,23 +1,35 @@
#!/usr/bin/env coffee
-sendingAddress = process.argv[2] # first argument -- e.g. '127.0.0.1'
-port = parseInt(process.argv[3]) # second argument -- e.g. 10000
+sendingAddress = process.argv[2] # first argument -- e.g. '127.0.0.1'
+port = parseInt(process.argv[3]) # second argument -- e.g. 10000
+https = process.argv[4] is 'https' # third arg -- 'https' or not
WebSocketServer = require('websocket').server
-http = require('http')
+fs = require('fs')
-hServer = http.createServer (request, response) ->
- response.writeHead(404)
+html = fs.readFileSync('index.html')
+httpCallback = (request, response) ->
+ response.write(html)
response.end()
-hServer.listen(port)
-wsServer = new WebSocketServer(httpServer: hServer, autoAcceptConnections: false)
+httpServer = if https
+ key = fs.readFileSync('server.key')
+ cert = fs.readFileSync('server.crt')
+ https = require('https')
+ https.createServer {key, cert}, httpCallback
+else
+ http = require('http')
+ http.createServer httpCallback
+
+httpServer.listen(port)
+
+wsServer = new WebSocketServer(httpServer: httpServer, autoAcceptConnections: false)
wsServer.on 'request', (request) ->
connection = request.accept(null, request.origin)
console.log "clients: #{wsServer.connections.length}"
if connection.remoteAddress is sendingAddress
connection.on 'message', (message) ->
for c in wsServer.connections
- continue if c is connection # don't send back to the sender
- continue unless c.socket.bufferSize is 0 # minimal buffering for slow connections
+ continue if c is connection # don't send back to the sender
+ continue unless c.socket.bufferSize is 0 or https # minimal buffering for slow connections (doesn't work with SSL)
c.sendBytes(message.binaryData)
View
8 server/websocket-kinect.py
@@ -1,16 +1,19 @@
#!/usr/bin/env python
import sys, signal, numpy, freenect, pylzma
-from twisted.internet import reactor, threads
+from twisted.internet import reactor, threads, ssl
+from twisted.web.client import WebClientContextFactory
from autobahn.websocket import WebSocketServerFactory, WebSocketServerProtocol, listenWS, WebSocketClientFactory, WebSocketClientProtocol, connectWS
class SendClientProtocol(WebSocketClientProtocol):
def onOpen(self):
+ print 'connection opened'
self.factory.register(self)
def connectionLost(self, reason):
+ print 'connection lost'
WebSocketClientProtocol.connectionLost(self, reason)
self.factory.unregister(self)
@@ -169,6 +172,7 @@ def signalHandler(signum, frame):
listenWS(factory)
else:
factory = SendClientFactory(url)
- connectWS(factory)
+ contextFactory = ssl.ClientContextFactory()
+ connectWS(factory, contextFactory)
reactor.run()

0 comments on commit a207999

Please sign in to comment.
Something went wrong with that request. Please try again.