""" Implements various cloud servers simultaneously. Currently implements two servers: 1) DeviceHTTPServer: The IoT device uploads data to the cloud via this HTTP server. 2) WebPortalHTTPServer: A server for a user to access using a web browser to see data from the IoT device. All data is received from IoT devices and appended to a perpetually growing list. When a user browses to the web portal, they are presented with all of the data received so far. """ """ # GDB code to pause on ctrl+c # http://stackoverflow.com/questions/10239760/interrupt-pause-running-python-program-in-pdb def debug_signal_handler(signal, frame): import pdb pdb.set_trace() import signal signal.signal(signal.SIGINT, debug_signal_handler) """ import OpenSSL import sys from twisted.internet import endpoints, reactor, ssl from twisted.python import log from twisted.python.modules import getModule from twisted.web import resource, server PORT_DEVICE_UPLOAD = 5555 PORT_WEB_PORTAL = 5556 # All temperature readings received from IoT device connections. temperature_readings = [] class DeviceHTTPServer(resource.Resource): """ HTTP Server for IoT device to upload data. Stores response bodies in global list variable temperature_readings. """ isLeaf = True def render_POST(self, request): print "Received something" #for header in request.responseHeaders.getAllRawHeaders(): # request.responseHeaders.removeHeader(header[0]) reading = request.content.read() print "Received:", reading temperature_readings.append(reading) return "" # Empty response class WebPortalHTTPServer(resource.Resource): """ HTTP Server for users to browse. Currently prints out all the data received so far from devices. """ isLeaf = True def render_GET(self, request): # Print all of the readings so far. return ("" + ''.join([str(t) for t in temperature_readings]) + "") log.startLogging(sys.stdout) # DeviceSite - The site the device connects to deviceSite = server.Site(DeviceHTTPServer()) #endpoint = endpoints.TCP4ServerEndpoint(reactor, PORT_DEVICE_UPLOAD) #endpoint = endpoints.serverFromString(reactor, # "ssl:port=%d" # ":privateKey=certs/middlebox.key" # ":certKey=certs/middlebox.crt" # ":extraCertChain=certs/ca_send_chain.crt" # ":sslmethod=TLSv1_2_METHOD" # % (PORT_DEVICE_UPLOAD,)) #endpoint.listen(deviceSite) def load_cert(file_name): filepath = getModule(__name__).filePath data = filepath.sibling('certs').child(file_name).getContent() return OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, data) def load_key(file_name): filepath = getModule(__name__).filePath data = filepath.sibling('certs').child(file_name).getContent() return OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, data) client_cert = load_cert('client_for_server.crt') server_cert = load_cert('server.crt') server_key = load_key('server.key') chain_filenames = ['ca-int1.crt', 'ca-int2.crt'] chain_certs = [load_cert(filename) for filename in chain_filenames] reactor.listenSSL(PORT_DEVICE_UPLOAD, deviceSite, ssl.CertificateOptions(privateKey=server_key, certificate=server_cert, extraCertChain=chain_certs, method=OpenSSL.SSL.TLSv1_2_METHOD, # verify=True, # requireCertificate=True, # caCerts=[client_cert] ) ) # PortalSite - The web site for browsers portalSite = server.Site(WebPortalHTTPServer()) endpoint = endpoints.TCP4ServerEndpoint(reactor, PORT_WEB_PORTAL) endpoint.listen(portalSite) reactor.run()