diff --git a/htdocs/vistax.mjs b/htdocs/vistax.mjs index 6816fc8..edb2734 100644 --- a/htdocs/vistax.mjs +++ b/htdocs/vistax.mjs @@ -159,7 +159,11 @@ Client.fromCookie = async function(secret, host='vista.northport.med.va.gov', po console.log('Using saved secret and connection', secret); var cid = cookie.get('cid'); var client = Client.fromID(cid, secret); - if((await vista.call(cid, 'XWB_IM_HERE')).result == '1') return client; + if((await vista.call(cid, 'XWB_IM_HERE')).result == '1') { + var server = await client.serverinfo(); + if((host == server.result.host) && (port == server.result.port)) return client; + else console.log('Rejecting previous connection to different server', server); + } cookie.reset('cid'); return await Client.fromCookie(secret, host, port); } diff --git a/main.py b/main.py index 0ff560c..ade1321 100644 --- a/main.py +++ b/main.py @@ -66,7 +66,7 @@ def application(): def cb_serverinfo(cid): try: client = clients[cid] - return jsonify({ 'result': client._obj._server._asdict() if client._obj._server else None, 'error': None, 'id': request.json.get('id') }) + return jsonify({ 'result': client._obj._server, 'error': None, 'id': request.json.get('id') }) except Exception as ex: logger.exception(request.url) return jsonify({ 'result': None, 'error': { 'type': ex.__class__.__name__, 'args': ex.args }, 'id': request.json.get('id') }) @@ -87,13 +87,13 @@ def application(): client = clients[cid] if 'avcode' in params: user = client.authenticate(params['avcode']) - client._cache_persistent(persistent=util.Store(f'cache.{client._server.volume.lower()}.{client._server.uci.lower()}.{user[0]}.db', journal_mode='WAL').memo) + client._cache_persistent(persistent=util.Store(f'cache.{client._server["volume"].lower()}.{client._server["uci"].lower()}.{user[0]}.db', journal_mode='WAL').memo) return jsonify({ 'result': user, 'error': None, 'id': request.json.get('id') }) else: from auth import XUIAMSSOi_MySsoTokenVBA if token := XUIAMSSOi_MySsoTokenVBA(): user = client.authenticate(token) - client._cache_persistent(persistent=util.Store(f'cache.{client._server.volume.lower()}.{client._server.uci.lower()}.{user[0]}.db', journal_mode='WAL').memo) + client._cache_persistent(persistent=util.Store(f'cache.{client._server["volume"].lower()}.{client._server["uci"].lower()}.{user[0]}.db', journal_mode='WAL').memo) return jsonify({ 'result': user, 'error': None, 'id': request.json.get('id') }) else: return jsonify({ 'result': None, 'error': { 'type': 'Unauthorized', 'args': [] }, 'id': request.json.get('id') }) diff --git a/rpc.py b/rpc.py index f662440..8ff9ca7 100644 --- a/rpc.py +++ b/rpc.py @@ -99,7 +99,8 @@ class ClientSync(object): self.sock.connect((host, port)) self.recv_rpc_msg = recv_rpc_msg(self.sock) self.lock = threading.Lock() - self._server = self._user = None + self._server = { 'host': host, 'port': port } + self._user = None self.context = 'XUS SIGNON' if TCPConnect and (res := self.TCPConnect(self.sock.getsockname()[0], '0', socket.gethostname())) != 'accept': raise RPCExcInvalidResult('TCPConnect', self.sock.getsockname()[0], '0', socket.gethostname(), res) @@ -129,7 +130,7 @@ class ClientSync(object): self.sock = self.recv_rpc_msg = None return res def authenticate(self, identity: str, *, context=('XUS SIGNON',)): - self._server = RecordServerInfo(*self('XUS SIGNON SETUP', '', '1', context=context)) + self._server.update(RecordServerInfo(*self('XUS SIGNON SETUP', '', '1', context=context))._asdict()) res = self('XUS AV CODE', XWBHash_encrypt(identity)) if res[0] == '0' or res[2] != '0': raise RPCExcAuth(res[3], res) @@ -170,7 +171,8 @@ class ClientAsync(object): self.reader, self.writer = await asyncio.open_connection(host, port) self.arecv_rpc_msg = arecv_rpc_msg(self.reader) self.lock = asyncio.Lock() - self._server = self._user = None + self._server = { 'host': host, 'port': port, 'info': None } + self._user = None self.context = 'XUS SIGNON' if TCPConnect and (res := await self.TCPConnect(self.writer.get_extra_info('sockname')[0], '0', socket.gethostname())) != 'accept': raise RPCExcInvalidResult('TCPConnect', self.writer.get_extra_info('sockname')[0], '0', socket.gethostname(), res) @@ -205,7 +207,7 @@ class ClientAsync(object): self.reader = self.writer = None return res async def authenticate(self, identity: str, *, context=('XUS SIGNON',)): - self._server = RecordServerInfo(*await self('XUS SIGNON SETUP', '', '1', context=context)) + self._server.update(RecordServerInfo(*await self('XUS SIGNON SETUP', '', '1', context=context))._asdict()) res = await self('XUS AV CODE', XWBHash_encrypt(identity)) if res[0] == '0' or res[2] != '0': raise RPCExcAuth(res[3], res)