2021-06-19 13:54:16 -07:00
|
|
|
import argon2
|
2021-06-20 15:34:04 -07:00
|
|
|
import asyncio
|
2021-06-20 16:44:03 -07:00
|
|
|
import base64
|
|
|
|
import imgproc
|
2021-06-20 15:48:14 -07:00
|
|
|
import importlib
|
2021-06-20 16:44:03 -07:00
|
|
|
import os
|
2021-06-20 16:37:04 -07:00
|
|
|
import parse_config
|
2021-06-20 16:44:03 -07:00
|
|
|
import random
|
|
|
|
import shutil
|
2021-06-20 16:37:04 -07:00
|
|
|
import sys
|
2021-06-20 16:44:03 -07:00
|
|
|
import tempfile
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
import tornado.web, tornado.websocket, tornado.ioloop
|
2021-06-20 16:37:04 -07:00
|
|
|
|
|
|
|
try:
|
|
|
|
conf = sys.argv[1]
|
|
|
|
except IndexError:
|
|
|
|
conf = 'conf.txt'
|
2021-06-17 14:52:23 -07:00
|
|
|
|
2021-06-20 16:37:04 -07:00
|
|
|
with open(conf) as f:
|
|
|
|
config_data = parse_config.load(f)
|
2021-06-17 19:50:16 -07:00
|
|
|
|
2021-06-20 16:44:03 -07:00
|
|
|
imgproc.backend = importlib.import_module(f'backends.{config_data["backend"]}')
|
|
|
|
imgproc.backend.config = config_data
|
|
|
|
imgproc.config = config_data
|
2021-06-17 14:52:23 -07:00
|
|
|
|
2021-06-19 13:54:16 -07:00
|
|
|
ph = argon2.PasswordHasher()
|
|
|
|
|
2021-06-20 15:34:04 -07:00
|
|
|
client = None
|
2021-06-19 13:54:16 -07:00
|
|
|
|
2021-06-15 12:50:51 -07:00
|
|
|
|
2021-06-20 15:34:04 -07:00
|
|
|
class HCRAServer(tornado.websocket.WebSocketHandler):
|
|
|
|
def open(self):
|
|
|
|
global client
|
|
|
|
if client is not None:
|
|
|
|
self.write_message('err%*inuse%Server already in use')
|
|
|
|
self.close()
|
|
|
|
else:
|
|
|
|
client = self
|
|
|
|
self.items = {}
|
|
|
|
self.lock = threading.Lock()
|
|
|
|
self.good = True
|
|
|
|
try:
|
2021-06-20 16:44:03 -07:00
|
|
|
imgname = imgproc.get_full_img()
|
2021-06-20 15:34:04 -07:00
|
|
|
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.ack()
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
threading.Thread(target=self.run, args=(loop,)).start()
|
2021-06-17 14:33:40 -07:00
|
|
|
|
2021-06-20 15:34:04 -07:00
|
|
|
def send(self, item, name):
|
|
|
|
with self.lock:
|
|
|
|
self.items[name] = item
|
2021-06-15 12:50:51 -07:00
|
|
|
|
2021-06-20 15:34:04 -07:00
|
|
|
def ack(self):
|
|
|
|
with self.lock:
|
|
|
|
self.items['ack'] = 'ack'
|
|
|
|
|
|
|
|
async def cycle(self):
|
|
|
|
with self.lock:
|
|
|
|
while not self.good:
|
|
|
|
pass
|
|
|
|
for name, item in list(self.items.items()):
|
|
|
|
if not self.good:
|
|
|
|
break
|
|
|
|
self.write_message(item)
|
|
|
|
del self.items[name]
|
|
|
|
if item == 'ack':
|
|
|
|
self.good = False
|
|
|
|
|
|
|
|
def run(self, loop):
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
while True:
|
|
|
|
if client is not self:
|
|
|
|
break
|
|
|
|
loop.run_until_complete(self.cycle())
|
|
|
|
|
|
|
|
def on_close(self):
|
|
|
|
global client
|
|
|
|
if client is self:
|
|
|
|
self.good = None
|
|
|
|
client = None
|
|
|
|
|
|
|
|
def on_message(self, message):
|
|
|
|
action = message.split(' ', 1)[0]
|
|
|
|
if action == 'ack':
|
|
|
|
self.good = True
|
|
|
|
else:
|
|
|
|
_, password, x, y, w, is_long = message.split(' ')
|
|
|
|
try:
|
2021-06-20 16:37:04 -07:00
|
|
|
ph.verify(config_data['password_argon2'], password)
|
2021-06-20 15:34:04 -07:00
|
|
|
x, y, w, is_long = int(x), int(y), int(w), is_long == 'true'
|
2021-06-20 16:44:03 -07:00
|
|
|
imgproc.touch(x, y, w, is_long)
|
2021-06-20 15:34:04 -07:00
|
|
|
except argon2.exceptions.VerifyMismatchError:
|
|
|
|
self.send(f'err%*badpass%Incorrect password', 'BADPASS')
|
|
|
|
self.close()
|
|
|
|
|
|
|
|
def check_origin(self, origin):
|
|
|
|
return True
|
2021-06-15 14:21:59 -07:00
|
|
|
|
2021-06-17 14:33:40 -07:00
|
|
|
|
2021-06-20 15:39:21 -07:00
|
|
|
def cycle():
|
|
|
|
try:
|
2021-06-20 16:44:03 -07:00
|
|
|
changed = imgproc.get_split_imgs()
|
2021-06-20 15:39:21 -07:00
|
|
|
except Exception as e:
|
|
|
|
if client is not None:
|
|
|
|
client.send('err%noconn%Server failed to capture screenshot', 'ERR')
|
|
|
|
time.sleep(3)
|
|
|
|
return
|
|
|
|
|
|
|
|
for i in changed:
|
|
|
|
threading.Thread(target=do_img, args=(i,)).start()
|
|
|
|
|
|
|
|
if client is not None:
|
|
|
|
client.ack()
|
|
|
|
|
|
|
|
|
|
|
|
def do_img(imgname):
|
|
|
|
if client is not None:
|
|
|
|
client.send(img(imgname), imgname)
|
|
|
|
|
|
|
|
|
|
|
|
def img(imgname):
|
|
|
|
with open(f'pieces/{imgname}.jpg', 'rb') as f:
|
|
|
|
img = f.read()
|
|
|
|
|
|
|
|
response = f'pic%{imgname}%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}'
|
|
|
|
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
2021-06-15 12:50:51 -07:00
|
|
|
def do_cycles():
|
|
|
|
while True:
|
2021-06-20 15:34:04 -07:00
|
|
|
if client is not None:
|
2021-06-17 14:33:40 -07:00
|
|
|
cycle()
|
|
|
|
time.sleep(0.4)
|
|
|
|
else:
|
|
|
|
time.sleep(1)
|
2021-06-15 12:50:51 -07:00
|
|
|
|
2021-06-20 15:39:21 -07:00
|
|
|
|
2021-06-15 12:50:51 -07:00
|
|
|
tmp = tempfile.mkdtemp(prefix="HCRA-")
|
|
|
|
try:
|
|
|
|
shutil.copy('crops.json', tmp)
|
|
|
|
os.chdir(tmp)
|
|
|
|
os.mkdir('pieces')
|
2021-06-20 15:34:04 -07:00
|
|
|
|
2021-06-15 12:50:51 -07:00
|
|
|
threading.Thread(target=do_cycles).start()
|
2021-06-20 15:34:04 -07:00
|
|
|
|
|
|
|
application = tornado.web.Application([
|
|
|
|
(r"/", HCRAServer),
|
|
|
|
])
|
|
|
|
application.listen(1234)
|
|
|
|
tornado.ioloop.IOLoop.current().start()
|
2021-06-15 12:50:51 -07:00
|
|
|
finally:
|
|
|
|
os.chdir('/')
|
|
|
|
shutil.rmtree(tmp)
|