D901 #2

Merged
bmartin5692 merged 34 commits from D901 into dev 2019-02-22 05:37:44 +01:00
2 changed files with 36 additions and 17 deletions
Showing only changes of commit f2ddf01465 - Show all commits

View file

@ -56,7 +56,7 @@ class ConfServer():
app.add_routes([ app.add_routes([
web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/login', self.handle_login), web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/login', self.handle_login),
web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/checkLogin', self.handle_login), # web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/checkLogin', self.handle_checkLogin),
web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/logout', self.handle_logout), web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/logout', self.handle_logout),
web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/getAuthCode', self.handle_getAuthCode), web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/getAuthCode', self.handle_getAuthCode),
web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/checkAgreement', self.handle_checkAgreement), web.get('/{apiversion}/private/{country}/{language}/{devid}/{apptype}/{appversion}/{devtype}/{aid}/user/checkAgreement', self.handle_checkAgreement),
@ -101,6 +101,25 @@ class ConfServer():
return web.json_response(body) return web.json_response(body)
async def handle_checkLogin(self, request):
# The app seems to remember it's last uid and accessToken
# If these don't match, it fails
countrycode = request.match_info.get('country', "us")
body = {
"code": "0000",
"data": {
"accessToken": "tempaccesstoken", #Random chars 32 length
"country": countrycode,
"email": "null@null.com",
"uid": "fuid_{}".format(''.join(random.sample(string.ascii_letters,6))), #Date(14)_RandomChars(32)
"username": "fusername_{}".format(''.join(random.sample(string.ascii_letters,6))) #Random chars 8
},
"msg": "操作成功",
"time": bumper.get_milli_time(time.time())
}
return web.json_response(body)
async def handle_logout(self, request): async def handle_logout(self, request):
body = {"code": "0000","data": None,"msg": "操作成功", "time": bumper.get_milli_time(time.time())} body = {"code": "0000","data": None,"msg": "操作成功", "time": bumper.get_milli_time(time.time())}
#TODO - when logging out close out any other connections MQTT/XMPP #TODO - when logging out close out any other connections MQTT/XMPP

View file

@ -49,7 +49,7 @@ class XMPPServer():
self.exit_flag = True self.exit_flag = True
logging.info('XMPPServer: shutting down...') logging.info('XMPPServer: shutting down...')
except Exception as e: except Exception as e:
logging.exception("Exception: {}".format(e)) logging.exception("XMPPServer Exception: {}".format(e))
class Client(threading.Thread): class Client(threading.Thread):
@ -75,23 +75,23 @@ class Client(threading.Thread):
def send(self, command): def send(self, command):
try: try:
logging.debug('to {}: {}'.format(self.address, command)) logging.debug('XMPPServer to {}: {}'.format(self.address, command))
self.connection.send(command.encode()) self.connection.send(command.encode())
except OSError as e: except OSError as e:
logging.error('XMPPServer: {}'.format(e)) logging.error('XMPPServer: {}'.format(e))
except Exception as e: except Exception as e:
logging.exception("Exception: {}".format(e)) logging.exception("XMPPServer Exception: {}".format(e))
def disconnect(self): def disconnect(self):
try: try:
logging.info('{} with resource {} disconnecting'.format(self.address, self.clientresource)) logging.info('XMPPServer: {} with resource {} disconnecting'.format(self.address, self.clientresource))
self.connection.close() self.connection.close()
self._set_state('DISCONNECT') self._set_state('DISCONNECT')
except Exception as e: except Exception as e:
logging.exception("Exception: {}".format(e)) logging.exception("XMPPServer Exception: {}".format(e))
def _tag_strip_uri(self, tag): def _tag_strip_uri(self, tag):
try: try:
@ -99,25 +99,25 @@ class Client(threading.Thread):
uri, ignore, tag = tag[1:].partition('}') uri, ignore, tag = tag[1:].partition('}')
return tag return tag
except Exception as e: except Exception as e:
logging.exception("Exception: {}".format(e)) logging.exception("XMPPServer Exception: {}".format(e))
def _set_state(self, state): def _set_state(self, state):
try: try:
new_state = getattr(Client, state) new_state = getattr(Client, state)
if self.state > new_state: if self.state > new_state:
raise Exception('{} illegal state change {}->{}'.format(self.address, self.state, new_state)) raise Exception('XMPPServer: {} illegal state change {}->{}'.format(self.address, self.state, new_state))
logging.debug('{} state: {}'.format(self.address, state)) logging.debug('XMPPServer: {} state: {}'.format(self.address, state))
self.state = new_state self.state = new_state
if new_state == '5': if new_state == '5':
self.join() self.join()
except Exception as e: except Exception as e:
logging.exception("Exception: {}".format(e)) logging.exception("XMPPServer: Exception: {}".format(e))
def _handle_ctl(self, xml, data): def _handle_ctl(self, xml, data):
try: try:
ctl = xml[0][0] ctl = xml[0][0]
if ctl.get('admin') and self.type == self.BOT: if ctl.get('admin') and self.type == self.BOT:
logging.debug('admin username received from bot: {}'.format(ctl.get('admin'))) logging.debug('XMPPServer: admin username received from bot: {}'.format(ctl.get('admin')))
XMPPServer.client_id = ctl.get('admin') XMPPServer.client_id = ctl.get('admin')
return return
# forward # forward
@ -131,7 +131,7 @@ class Client(threading.Thread):
data = data.encode() data = data.encode()
client.send(data.decode('utf-8')) client.send(data.decode('utf-8'))
except Exception as e: except Exception as e:
logging.exception("Exception: {}".format(e)) logging.exception("XMPPServer Exception: {}".format(e))
def _handle_ping(self, xml, data): def _handle_ping(self, xml, data):
@ -150,11 +150,11 @@ class Client(threading.Thread):
if client.address != self.address and client.state == client.READY: if client.address != self.address and client.state == client.READY:
client.send(data.decode('utf-8')) client.send(data.decode('utf-8'))
except Exception as e: except Exception as e:
logging.exception("Exception: {}".format(e)) logging.exception("XMPPServer Exception: {}".format(e))
def run(self): def run(self):
try: try:
logging.info('client connected: {}'.format(self.address)) logging.info('XMPPServer: client connected: {}'.format(self.address))
self._set_state('CONNECT') self._set_state('CONNECT')
data = "" data = ""
while True: while True:
@ -162,7 +162,7 @@ class Client(threading.Thread):
if not self.connection._closed: if not self.connection._closed:
data = self.connection.recv(4096) data = self.connection.recv(4096)
if data: if data:
logging.debug('from {}: {}'.format(self.address, data.decode('utf-8'))) logging.debug('XMPPServer: from {}: {}'.format(self.address, data.decode('utf-8')))
try: try:
if self.state == self.CONNECT: if self.state == self.CONNECT:
if data.decode('utf-8').find('jabber:client') > -1: if data.decode('utf-8').find('jabber:client') > -1:
@ -204,12 +204,12 @@ class Client(threading.Thread):
if len(xml) and xml[0].tag == 'status': if len(xml) and xml[0].tag == 'status':
# bot announcing arrival # bot announcing arrival
self.type = self.BOT self.type = self.BOT
logging.debug('{} type set to BOT (based on presence tag)'.format(self.address)) logging.debug('XMPPServer: {} type set to BOT (based on presence tag)'.format(self.address))
# send a command from an unknown user - the response will contain the correct admin username # send a command from an unknown user - the response will contain the correct admin username
self.send('<iq type="set" id="{}" from="{}" to="{}"><query xmlns="com:ctl"><ctl td="GetCleanState" /></query></iq>'.format(uuid.uuid4(), 'unknown@ecouser.net', XMPPServer.bot_id)) self.send('<iq type="set" id="{}" from="{}" to="{}"><query xmlns="com:ctl"><ctl td="GetCleanState" /></query></iq>'.format(uuid.uuid4(), 'unknown@ecouser.net', XMPPServer.bot_id))
elif xml.get('type') == 'available': elif xml.get('type') == 'available':
self.type = self.CONTROLLER self.type = self.CONTROLLER
logging.debug('{} type set to CONTROLLER (based on presence tag)'.format(self.address)) logging.debug('XMPPServer: {} type set to CONTROLLER (based on presence tag)'.format(self.address))
except ET.ParseError as e: except ET.ParseError as e:
logging.debug('error: {}'.format(e)) logging.debug('error: {}'.format(e))
except Exception as e: except Exception as e: