class WPServerProtocol(WPProtocol): def __init__(self): WPProtocol.__init__(self) self._logined = False self.forwarder_manager = WPServerForwarderManager(self) def dataReceived(self, data): if not self.key: self.key = struct.unpack('!B', data[0])[0] if self.key == 0: self.closeLinkWithMsg('key cannot be zero') return data = data[1:] assert self.key if data: WPProtocol.dataReceived(self, data) def command_received(self, cmd, args): if not self._logined: if cmd != COMMAND.LOGIN: self.closeLinkWithMsg('first login please') return if len(args) != 3: self.closeLinkWithMsg('invalid login command') return version, username, password = args if version != '0.1': self.closeLinkWithMsg('invalid version') return if not user_manager.login(username, password): self.closeLinkWithMsg('invalid user name or password') return self.send_ok_response(COMMAND.LOGIN) self._logined = True elif cmd == COMMAND.CONNECT: if len(args) != 3: self.closeLinkWithMsg( 'CONNECT command needs 3 args, received %d' % len(args)) return id, host, port = args if not id.isdigit() or not port.isdigit(): self.closeLinkWithMsg( 'CONNECT command id and port must be integer, received %s %s' % id, port) return id = int(id) port = int(port) self.forwarder_manager.create_forwarder(id, (host, port)) else: WPProtocol.command_received(self, cmd, args) def response_received(self, cmd, response): if not self._logined: self.closeLinkWithMsg('response before login command received') return
class WPServerProtocol(WPProtocol): def __init__(self): WPProtocol.__init__(self) self._logined = False self.forwarder_manager = WPServerForwarderManager(self) def dataReceived(self, data): if not self.key: self.key = struct.unpack('!B', data[0])[0] if self.key == 0: self.closeLinkWithMsg('key cannot be zero') return data = data[1:] assert self.key if data: WPProtocol.dataReceived(self, data) def command_received(self, cmd, args): if not self._logined: if cmd != COMMAND.LOGIN: self.closeLinkWithMsg('first login please') return if len(args) != 3: self.closeLinkWithMsg('invalid login command') return version, username, password = args if version != '0.1': self.closeLinkWithMsg('invalid version') return if not user_manager.login(username, password): self.closeLinkWithMsg('invalid user name or password') return self.send_ok_response(COMMAND.LOGIN) self._logined = True elif cmd == COMMAND.CONNECT: if len(args) != 3: self.closeLinkWithMsg('CONNECT command needs 3 args, received %d'%len(args)) return id, host, port = args if not id.isdigit() or not port.isdigit(): self.closeLinkWithMsg('CONNECT command id and port must be integer, received %s %s'%id, port) return id = int(id) port = int(port) self.forwarder_manager.create_forwarder(id, (host, port)) else: WPProtocol.command_received(self, cmd, args) def response_received(self, cmd, response): if not self._logined: self.closeLinkWithMsg('response before login command received') return
def __init__(self): WPProtocol.__init__(self) self._logined = False self.forwarder_manager = WPServerForwarderManager(self)