ui: Add websocket notifications.

This commit is contained in:
tecnovert
2022-07-31 19:33:01 +02:00
parent 6cc54d9c61
commit 1601a57aed
21 changed files with 830 additions and 215 deletions

View File

@@ -80,6 +80,7 @@ if not len(logger.handlers):
logger.addHandler(logging.StreamHandler(sys.stdout))
UI_HTML_PORT = int(os.getenv('UI_HTML_PORT', 12700))
UI_WS_PORT = int(os.getenv('UI_WS_PORT', 11700))
COINS_RPCBIND_IP = os.getenv('COINS_RPCBIND_IP', '127.0.0.1')
PART_ZMQ_PORT = int(os.getenv('PART_ZMQ_PORT', 20792))
@@ -748,7 +749,8 @@ def printHelp():
logger.info('--nocores Don\'t download and extract any coin clients.')
logger.info('--usecontainers Expect each core to run in a unique container.')
logger.info('--portoffset=n Raise all ports by n.')
logger.info('--htmlhost= Interface to host on, default:127.0.0.1.')
logger.info('--htmlhost= Interface to host html server on, default:127.0.0.1.')
logger.info('--wshost= Interface to host websocket server on, disable by setting to "none", default:127.0.0.1.')
logger.info('--xmrrestoreheight=n Block height to restore Monero wallet from, default:{}.'.format(DEFAULT_XMR_RESTORE_HEIGHT))
logger.info('--noextractover Prevent extracting cores if files exist. Speeds up tests')
logger.info('--usetorproxy Use TOR proxy during setup. Note that some download links may be inaccessible over TOR.')
@@ -853,6 +855,7 @@ def main():
add_coin = ''
disable_coin = ''
htmlhost = '127.0.0.1'
wshost = '127.0.0.1'
xmr_restore_height = DEFAULT_XMR_RESTORE_HEIGHT
prepare_bin_only = False
no_cores = False
@@ -955,6 +958,9 @@ def main():
if name == 'htmlhost':
htmlhost = s[1].strip('"')
continue
if name == 'wshost':
wshost = s[1].strip('"')
continue
if name == 'xmrrestoreheight':
xmr_restore_height = int(s[1])
continue
@@ -1200,6 +1206,10 @@ def main():
'check_expired_seconds': 60
}
if wshost != 'none':
settings['wshost'] = wshost
settings['wsport'] = UI_WS_PORT + port_offset
if use_tor_proxy:
tor_control_password = generate_salt(24)
addTorSettings(settings, tor_control_password)

View File

@@ -19,6 +19,7 @@ import basicswap.config as cfg
from basicswap import __version__
from basicswap.basicswap import BasicSwap
from basicswap.http_server import HttpThread
from basicswap.contrib.websocket_server import WebsocketServer
logger = logging.getLogger()
@@ -93,6 +94,25 @@ def startXmrWalletDaemon(node_dir, bin_dir, wallet_bin, opts=[]):
return subprocess.Popen(args, stdin=subprocess.PIPE, stdout=wallet_stdout, stderr=wallet_stderr, cwd=data_dir)
def ws_new_client(client, server):
if swap_client:
swap_client.log.debug(f'ws_new_client {client["id"]}')
def ws_client_left(client, server):
if client is None:
return
if swap_client:
swap_client.log.debug(f'ws_client_left {client["id"]}')
def ws_message_received(client, server, message):
if len(message) > 200:
message = message[:200] + '..'
if swap_client:
swap_client.log.debug(f'ws_message_received {client["id"]} {message}')
def runClient(fp, data_dir, chain):
global swap_client
settings_path = os.path.join(data_dir, cfg.CONFIG_FILENAME)
@@ -158,24 +178,45 @@ def runClient(fp, data_dir, chain):
swap_client.start()
if 'htmlhost' in settings:
swap_client.log.info('Starting server at http://%s:%d.' % (settings['htmlhost'], settings['htmlport']))
swap_client.log.info('Starting http server at http://%s:%d.' % (settings['htmlhost'], settings['htmlport']))
allow_cors = settings['allowcors'] if 'allowcors' in settings else cfg.DEFAULT_ALLOW_CORS
tS1 = HttpThread(fp, settings['htmlhost'], settings['htmlport'], allow_cors, swap_client)
threads.append(tS1)
tS1.start()
thread_http = HttpThread(fp, settings['htmlhost'], settings['htmlport'], allow_cors, swap_client)
threads.append(thread_http)
thread_http.start()
if 'wshost' in settings:
ws_url = 'ws://{}:{}'.format(settings['wshost'], settings['wsport'])
swap_client.log.info(f'Starting ws server at {ws_url}.')
swap_client.ws_server = WebsocketServer(host=settings['wshost'], port=settings['wsport'])
swap_client.ws_server.set_fn_new_client(ws_new_client)
swap_client.ws_server.set_fn_client_left(ws_client_left)
swap_client.ws_server.set_fn_message_received(ws_message_received)
swap_client.ws_server.run_forever(threaded=True)
logger.info('Exit with Ctrl + c.')
while swap_client.is_running:
time.sleep(0.5)
swap_client.update()
except Exception as ex:
traceback.print_exc()
if swap_client.ws_server:
try:
swap_client.log.info('Stopping websocket server.')
swap_client.ws_server.shutdown_gracefully()
except Exception as ex:
traceback.print_exc()
swap_client.finalise()
swap_client.log.info('Stopping HTTP threads.')
for t in threads:
t.stop()
t.join()
try:
t.stop()
t.join()
except Exception as ex:
traceback.print_exc()
closed_pids = []
for d in daemons: