def onConnect(self, request):
		print "peer " + str(request.peer)
		print "peer_host " + request.peer.host
		print "peerstr " + request.peerstr
		print "headers " + str(request.headers)
		print "host " + request.host
		print "path " + request.path
		print "params " + str(request.params)
		print "version " + str(request.version)
		print "origin " + request.origin
		print "protocols " + str(request.protocols)

		#TODO: For development purposes only. Fix this so it doesn't work for null (localhost) as well.
		if(request.peer.host != "127.0.0.1" or (request.origin != "null" and request.origin != "http://codebender.cc")):
			self.connection_invalid = True
			raise HttpException(httpstatus.HTTP_STATUS_CODE_UNAUTHORIZED[0], "You are not authorized for this!")

		global serial_port
		global serial_device
		global serial_baudrate

		if(serial_port != None):
			self.connection_invalid = True
			raise HttpException(httpstatus.HTTP_STATUS_CODE_UNAUTHORIZED[0], "You cannot open 2 serial ports!")

		if(serial_device == None or serial_baudrate == None):
			self.connection_invalid = True
			raise HttpException(httpstatus.HTTP_STATUS_CODE_UNAUTHORIZED[0], "You must set serial device first!")
Beispiel #2
0
    def onConnect(self, req):
        """ Method is called by the Autobahn engine when a request to establish
            a connection has been received.

            @param req:     Connection Request object.
            @type  req:     autobahn.websocket.ConnectionRequest

            @return:        Deferred which fires callback with None or errback
                            with autobahn.websocket.HttpException
        """
        params = req.params

        try:
            userID = params['userID']
            robotID = params['robotID']
            password = params['password']
        except KeyError as e:
            raise HttpException(httpstatus.HTTP_STATUS_CODE_BAD_REQUEST[0],
                                'Request is missing parameter: {0}'.format(e))

        for name, param in [('userID', userID), ('robotID', robotID),
                            ('password', password)]:
            if len(param) != 1:
                raise HttpException(
                    httpstatus.HTTP_STATUS_CODE_BAD_REQUEST[0],
                    "Parameter '{0}' has to be unique in "
                    'request.'.format(name))

        d = self._realm.login(userID[0], robotID[0], password[0])
        d.addCallback(self._authenticate_success)
        d.addErrback(self._authenticate_failed)
        return d
Beispiel #3
0
    def onConnect(self, connectionRequest):
        ## connectionRequest has all the information from the initial
        ## WebSocket opening handshake ..
        print connectionRequest.peer
        print connectionRequest.peerstr
        print connectionRequest.headers
        print connectionRequest.host
        print connectionRequest.path
        print connectionRequest.params
        print connectionRequest.version
        print connectionRequest.origin
        print connectionRequest.protocols
        print connectionRequest.extensions

        ## We map to services based on path component of the URL the
        ## WebSocket client requested. This is just an example. We could
        ## use other information from connectionRequest, such has HTTP headers,
        ## WebSocket subprotocol, WebSocket origin etc etc
        ##
        if self.SERVICEMAP.has_key(connectionRequest.path):
            self.service = self.SERVICEMAP[connectionRequest.path](self)
        else:
            err = "No service under %s" % connectionRequest.path
            print err
            raise HttpException(404, err)
Beispiel #4
0
    def onConnect(self, connectionRequest):
        def user(headers):
            if self.debug:
                log.msg("RPIServerProtocol.onConnect - User connected")
            return UserClient(self)

        def rpi(headers):
            # check user agent
            if 'user-agent' in headers:
                if headers['user-agent'] == settings.RPI_USER_AGENT:
                    if self.debug:
                        log.msg("RPIServerProtocol.onConnect - RPI connected")
                    return RPIClient(self)
            raise HttpException(httpstatus.HTTP_STATUS_CODE_FORBIDDEN[0],
                                httpstatus.HTTP_STATUS_CODE_FORBIDDEN[1])

        paths = {
            '/': user,
            '/rpi/': rpi,
            '/rpi': rpi,
        }

        if connectionRequest.path not in paths:
            raise HttpException(httpstatus.HTTP_STATUS_CODE_NOT_FOUND[0],
                                httpstatus.HTTP_STATUS_CODE_NOT_FOUND[1])

        self.client = paths[connectionRequest.path](connectionRequest.headers)
Beispiel #5
0
 def rpi(headers):
     # check user agent
     if 'user-agent' in headers:
         if headers['user-agent'] == settings.RPI_USER_AGENT:
             if self.debug:
                 log.msg("RPIServerProtocol.onConnect - RPI connected")
             return RPIClient(self)
     raise HttpException(httpstatus.HTTP_STATUS_CODE_FORBIDDEN[0],
                         httpstatus.HTTP_STATUS_CODE_FORBIDDEN[1])
Beispiel #6
0
    def onConnect(self, connectionRequest):
        print connectionRequest
        print "Client connecting .."
        print "URL", connectionRequest.path
        print "Origin", connectionRequest.origin
        print "Protocols", connectionRequest.protocols

        if 'foo' in connectionRequest.protocols:
            ## accept WS, we will speak the 'foo' subprotocol
            return 'foo'
        else:
            raise HttpException(HTTP_STATUS_CODE_BAD_REQUEST[0],
                                "This server only speaks the 'foo' protocol")
Beispiel #7
0
    def onConnect(self, connectionRequest):
        headers = {}
        for subprotocol in connectionRequest.protocols:
            version, serializerId = _parse_subprotocol(subprotocol)
            if version == 2 and serializerId in self.factory._serializers.keys(
            ):
                self._serializer = self.factory._serializers[serializerId]
                return subprotocol, headers

        raise HttpException(
            HTTP_STATUS_CODE_BAD_REQUEST[0],
            "This server only speaks WebSocket subprotocols %s" %
            ', '.join(self.factory.protocols))
	def onConnect(self, request):
		print "peer " + str(request.peer)
		print "peer_host " + request.peer.host
		print "peerstr " + request.peerstr
		print "headers " + str(request.headers)
		print "host " + request.host
		print "path " + request.path
		print "params " + str(request.params)
		print "version " + str(request.version)
		print "origin " + request.origin
		print "protocols " + str(request.protocols)

		#TODO: For development purposes only. Fix this so it doesn't work for null (localhost) as well.
		if(request.peer.host != "127.0.0.1" or (request.origin != "null" and request.origin != "http://codebender.cc")):
			raise HttpException(httpstatus.HTTP_STATUS_CODE_UNAUTHORIZED[0], "You are not authorized for this!")
Beispiel #9
0
    def _authenticate_failed(self, e):
        """ Method is called by deferred when the connection could not been
            authenticated while being in 'onConnect'.
        """
        if e.check(InvalidRequest):
            code = httpstatus.HTTP_STATUS_CODE_BAD_REQUEST[0]
            msg = e.getErrorMessage()
        elif e.check(UnauthorizedLogin):
            code = httpstatus.HTTP_STATUS_CODE_UNAUTHORIZED[0]
            msg = httpstatus.HTTP_STATUS_CODE_UNAUTHORIZED[1]
        else:
            e.printTraceback()
            code = httpstatus.HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR[0]
            msg = httpstatus.HTTP_STATUS_CODE_INTERNAL_SERVER_ERROR[1]

        return Failure(HttpException(code, msg))
Beispiel #10
0
    def onConnect(self, connectionRequest):
        """Connection to WebSocket Protocol"""

        def user(headers):
            if self.debug:
                log.msg("MSP430ServerProtocol.onConnect - User connected")
            return UserClient(self)

        # MSP430 won't connect via WebSocket so only option is the client
        paths = {
            '/':user,
        }

        if connectionRequest.path not in paths:
            raise HttpException(httpstatus.HTTP_STATUS_CODE_NOT_FOUND[0],
                                httpstatus.HTTP_STATUS_CODE_NOT_FOUND[1])

        self.client = paths[connectionRequest.path](connectionRequest.headers)
 def onConnect(self, connectionRequest):
    if 'wsperf' in connectionRequest.protocols:
       return 'wsperf'
    else:
       raise HttpException(httpstatus.HTTP_STATUS_CODE_BAD_REQUEST[0],
                           "You need to speak wsperf subprotocol with this server!")