This is a fix for #17. Eventually I need to rewrite it again because
this isn't very well-written.
This commit is contained in:
Samuel Sloniker 2021-06-22 14:00:37 -07:00
parent 549cfb10f9
commit 94fa2bfa35

View File

@ -6,6 +6,8 @@ import importlib
import os import os
import parse_config import parse_config
import random import random
import requests
import secrets
import shutil import shutil
import sys import sys
import tempfile import tempfile
@ -30,30 +32,24 @@ ph = argon2.PasswordHasher()
client = None client = None
class HCRAServer(tornado.websocket.WebSocketHandler): token = secrets.token_urlsafe(64)
def open(self):
class DisconnectError(BaseException):
pass
class Client:
def __init__(self, conn):
global client global client
if client is not None: if client is not None:
self.write_message('err%*inuse%Server already in use') raise DisconnectError('err%*inuse%Server already in use')
self.close()
else:
client = self
self.items = {}
self.lock = threading.Lock()
self.good = True
try:
imgname = imgproc.get_full_img()
except Exception as e:
self.write_message('err%noconn%Server failed to capture screenshot')
return
with open(imgname, 'rb') as f: client = self
img = f.read() self.conn = conn
os.unlink(imgname) self.items = {}
self.write_message(f'pic%0x0%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}') self.lock = threading.Lock()
self.ack() self.good = True
loop = asyncio.new_event_loop()
threading.Thread(target=self.run, args=(loop,)).start()
def send(self, item, name): def send(self, item, name):
with self.lock: with self.lock:
@ -61,37 +57,53 @@ class HCRAServer(tornado.websocket.WebSocketHandler):
def ack(self): def ack(self):
with self.lock: with self.lock:
self.items['ack'] = 'ack' self.good = False
async def cycle(self): def get_item_to_send(self):
with self.lock: with self.lock:
while not self.good: if self.items:
pass name, content = list(self.items.items())[0]
for name, item in list(self.items.items()):
if not self.good:
break
self.write_message(item)
del self.items[name] del self.items[name]
if item == 'ack': return name, content
self.good = False elif not self.good:
return 'ack', 'ack'
else:
return None, None
def run(self, loop):
asyncio.set_event_loop(loop)
while True: class HCRAServer(tornado.websocket.WebSocketHandler):
if client is not self: def open(self):
break try:
loop.run_until_complete(self.cycle()) self.client = Client(self)
except DisconnectError as e:
self.write_message(str(e))
return
try:
imgname = imgproc.get_full_img()
except Exception as e:
self.write_message('err%noconn%Server failed to capture screenshot')
return
with open(imgname, 'rb') as f:
img = f.read()
os.unlink(imgname)
self.write_message(f'pic%0x0%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}')
self.is_open = True
self.client.ack()
def on_close(self): def on_close(self):
global client global client
if client is self: if client is self.client:
self.good = None self.client.good = None
client = None client = None
self.is_open = False
def on_message(self, message): def on_message(self, message):
action = message.split(' ', 1)[0] action = message.split(' ', 1)[0]
if action == 'ack': if action == 'ack':
self.good = True self.client.good = True
else: else:
_, password, x, y, w, is_long = message.split(' ') _, password, x, y, w, is_long = message.split(' ')
try: try:
@ -99,13 +111,12 @@ class HCRAServer(tornado.websocket.WebSocketHandler):
x, y, w, is_long = int(x), int(y), int(w), is_long == 'true' x, y, w, is_long = int(x), int(y), int(w), is_long == 'true'
imgproc.touch(x, y, w, is_long) imgproc.touch(x, y, w, is_long)
except argon2.exceptions.VerifyMismatchError: except argon2.exceptions.VerifyMismatchError:
self.send(f'err%*badpass%Incorrect password', 'BADPASS') self.client.send(f'err%*badpass%Incorrect password', 'BADPASS')
self.close() self.close()
def check_origin(self, origin): def check_origin(self, origin):
return True return True
def cycle(): def cycle():
try: try:
changed = imgproc.get_split_imgs() changed = imgproc.get_split_imgs()
@ -115,8 +126,11 @@ def cycle():
time.sleep(3) time.sleep(3)
return return
threads = []
for i in changed: for i in changed:
threading.Thread(target=do_img, args=(i,)).start() thread = threading.Thread(target=do_img, args=(i,))
threads.append(thread)
thread.start()
if client is not None: if client is not None:
client.ack() client.ack()
@ -145,6 +159,22 @@ def do_cycles():
time.sleep(1) time.sleep(1)
class Cycler(tornado.web.RequestHandler):
def get(self):
if client is not None:
name, item = client.get_item_to_send()
if item is not None:
client.conn.write_message(item)
self.write('OK')
def get_token():
while True:
requests.get('http://localhost:1234/' + token)
tmp = tempfile.mkdtemp(prefix="HCRA-") tmp = tempfile.mkdtemp(prefix="HCRA-")
try: try:
shutil.copy('crops.json', tmp) shutil.copy('crops.json', tmp)
@ -155,8 +185,10 @@ try:
application = tornado.web.Application([ application = tornado.web.Application([
(r"/", HCRAServer), (r"/", HCRAServer),
('/' + token, Cycler),
]) ])
application.listen(1234) application.listen(1234)
threading.Thread(target=get_token).start()
tornado.ioloop.IOLoop.current().start() tornado.ioloop.IOLoop.current().start()
finally: finally:
os.chdir('/') os.chdir('/')