2021-06-15 12:50:51 -07:00
|
|
|
import os
|
|
|
|
import base64
|
|
|
|
import threading
|
|
|
|
import logging
|
|
|
|
import random
|
|
|
|
import time
|
|
|
|
import shutil
|
|
|
|
import tempfile
|
|
|
|
from websocket_server import WebsocketServer
|
2021-06-17 19:50:16 -07:00
|
|
|
import imgproc as hcapi
|
2021-06-19 13:54:16 -07:00
|
|
|
import argon2
|
2021-06-17 14:52:23 -07:00
|
|
|
|
|
|
|
# Select backend - backends.port8080 uses HamClock's port 8080 service;
|
|
|
|
# backends.x11 uses an X11 server (typically Xvfb) (make sure DISPLAY is set
|
|
|
|
# correctly!)
|
|
|
|
|
2021-06-17 19:50:16 -07:00
|
|
|
#import backends.port8080 as backend
|
|
|
|
import backends.x11 as backend
|
|
|
|
|
|
|
|
hcapi.backend = backend
|
2021-06-17 14:52:23 -07:00
|
|
|
|
|
|
|
|
2021-06-19 13:54:16 -07:00
|
|
|
ph = argon2.PasswordHasher()
|
|
|
|
|
|
|
|
|
2021-06-15 12:50:51 -07:00
|
|
|
def cycle():
|
|
|
|
try:
|
2021-06-17 19:50:16 -07:00
|
|
|
changed = hcapi.get_split_imgs()
|
2021-06-15 14:21:59 -07:00
|
|
|
except Exception as e:
|
|
|
|
for client in clients.values():
|
2021-06-15 14:24:32 -07:00
|
|
|
client.send('err%noconn%Server failed to capture screenshot', 'ERR')
|
2021-06-15 12:50:51 -07:00
|
|
|
time.sleep(3)
|
|
|
|
return
|
|
|
|
|
|
|
|
for i in changed:
|
2021-06-15 14:21:59 -07:00
|
|
|
threading.Thread(target=do_img, args=(i,)).start()
|
|
|
|
|
|
|
|
for client in clients.values():
|
|
|
|
client.ack()
|
|
|
|
|
|
|
|
|
|
|
|
class Client:
|
|
|
|
def __init__(self, client, server):
|
|
|
|
self.client = client
|
|
|
|
self.server = server
|
2021-06-15 14:24:32 -07:00
|
|
|
self.items = {}
|
2021-06-17 14:33:40 -07:00
|
|
|
self.lock = threading.Lock()
|
2021-06-15 14:21:59 -07:00
|
|
|
self.good = True
|
|
|
|
|
|
|
|
def send(self, item, name):
|
2021-06-17 14:33:40 -07:00
|
|
|
with self.lock:
|
|
|
|
self.items[name] = item
|
2021-06-15 14:21:59 -07:00
|
|
|
|
|
|
|
def ack(self):
|
2021-06-17 14:33:40 -07:00
|
|
|
with self.lock:
|
|
|
|
try:
|
|
|
|
self.server.send_message(self.client, 'ack')
|
|
|
|
self.good = False
|
|
|
|
except BrokenPipeError:
|
|
|
|
pass
|
2021-06-15 12:50:51 -07:00
|
|
|
|
2021-06-15 14:21:59 -07:00
|
|
|
def cycle(self):
|
2021-06-17 14:33:40 -07:00
|
|
|
with self.lock:
|
|
|
|
try:
|
|
|
|
while not self.good:
|
|
|
|
pass
|
|
|
|
for name, item in list(self.items.items()):
|
|
|
|
if not self.good:
|
|
|
|
break
|
|
|
|
self.server.send_message(self.client, item)
|
|
|
|
del self.items[name]
|
|
|
|
except BrokenPipeError:
|
|
|
|
pass
|
2021-06-15 14:21:59 -07:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
while True:
|
|
|
|
self.cycle()
|
|
|
|
clients = {}
|
2021-06-15 12:50:51 -07:00
|
|
|
|
|
|
|
def do_img(imgname):
|
2021-06-15 14:21:59 -07:00
|
|
|
for client in clients.values():
|
2021-06-15 14:24:32 -07:00
|
|
|
client.send(img(imgname), imgname)
|
2021-06-15 12:50:51 -07:00
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def new_client(client, server):
|
2021-06-17 14:33:40 -07:00
|
|
|
if clients:
|
|
|
|
server.send_message(client, 'err%*inuse%Server already in use')
|
|
|
|
client['handler'].send_text("", opcode=0x8)
|
|
|
|
return
|
|
|
|
|
|
|
|
clients[client['id']] = Client(client, server)
|
2021-06-15 12:50:51 -07:00
|
|
|
try:
|
|
|
|
imgname = hcapi.get_full_img()
|
2021-06-19 13:17:02 -07:00
|
|
|
except Exceptidon as e:
|
2021-06-15 12:50:51 -07:00
|
|
|
server.send_message(client, 'err%noconn%Server failed to capture screenshot')
|
|
|
|
return
|
|
|
|
|
2021-06-15 14:21:59 -07:00
|
|
|
clients[client['id']] = Client(client, server)
|
|
|
|
|
2021-06-15 12:50:51 -07:00
|
|
|
with open(imgname, 'rb') as f:
|
|
|
|
img = f.read()
|
|
|
|
os.unlink(imgname)
|
|
|
|
server.send_message(client, f'pic%0x0%data:imgage/jpeg;base64,{base64.b64encode(img).decode("utf-8")}')
|
2021-06-15 14:21:59 -07:00
|
|
|
clients[client['id']] = Client(client, server)
|
|
|
|
clients[client['id']].ack()
|
|
|
|
threading.Thread(target=clients[client['id']].run).start()
|
2021-06-15 12:50:51 -07:00
|
|
|
|
|
|
|
def do_touch(client, server, message):
|
2021-06-15 14:21:59 -07:00
|
|
|
action = message.split(' ', 1)[0]
|
|
|
|
if action == 'ack':
|
|
|
|
clients[client['id']].good = True
|
2021-06-15 12:50:51 -07:00
|
|
|
else:
|
2021-06-17 14:33:40 -07:00
|
|
|
_, password, x, y, w, is_long = message.split(' ')
|
2021-06-19 13:54:16 -07:00
|
|
|
try:
|
|
|
|
ph.verify('$argon2id$v=19$m=102400,t=2,p=8$NExqSUh+0wzBznBG9jM6ww$MkaPLZ6WPAegb8BI+IL7Bg', password)
|
2021-06-17 14:52:23 -07:00
|
|
|
x, y, w, is_long = int(x), int(y), int(w), is_long == 'true'
|
2021-06-17 14:33:40 -07:00
|
|
|
hcapi.touch(x, y, w, is_long)
|
2021-06-19 13:54:16 -07:00
|
|
|
except argon2.exceptions.VerifyMismatchError:
|
2021-06-15 14:24:32 -07:00
|
|
|
clients[client['id']].send(f'badpass', 'BADPASS')
|
2021-06-19 13:54:16 -07:00
|
|
|
client['handler'].send_text("", opcode=0x8)
|
2021-06-15 12:50:51 -07:00
|
|
|
|
2021-06-17 14:33:40 -07:00
|
|
|
def client_left(client, server):
|
|
|
|
clients[client['id']].good = False
|
|
|
|
del clients[client['id']]
|
|
|
|
|
2021-06-15 12:50:51 -07:00
|
|
|
def do_cycles():
|
|
|
|
while True:
|
2021-06-17 14:33:40 -07:00
|
|
|
if clients:
|
|
|
|
cycle()
|
|
|
|
time.sleep(0.4)
|
|
|
|
else:
|
|
|
|
time.sleep(1)
|
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')
|
|
|
|
server = WebsocketServer(1234, host='0.0.0.0', loglevel=logging.INFO)
|
|
|
|
threading.Thread(target=do_cycles).start()
|
|
|
|
server.set_fn_new_client(new_client)
|
|
|
|
server.set_fn_message_received(do_touch)
|
2021-06-17 14:33:40 -07:00
|
|
|
server.set_fn_client_left(client_left)
|
2021-06-15 12:50:51 -07:00
|
|
|
server.run_forever()
|
|
|
|
finally:
|
|
|
|
os.chdir('/')
|
|
|
|
shutil.rmtree(tmp)
|