proj/server: Update testers
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from typing import Optional, TypedDict, NotRequired
|
||||
from typing import Optional, TypedDict, NotRequired, BinaryIO
|
||||
from socketserver import UnixStreamServer, StreamRequestHandler, ThreadingMixIn
|
||||
import os
|
||||
import re
|
||||
@@ -30,7 +30,9 @@ class ThreadedUnixStreamServer(ThreadingMixIn, UnixStreamServer):
|
||||
pass
|
||||
|
||||
|
||||
class Handler(StreamRequestHandler):
|
||||
class Parser:
|
||||
rfile: BinaryIO
|
||||
wfile: Optional[BinaryIO]
|
||||
pid: Optional[int]
|
||||
tid: Optional[int]
|
||||
path: Optional[str]
|
||||
@@ -40,18 +42,19 @@ class Handler(StreamRequestHandler):
|
||||
rel_ret_addr: int
|
||||
dli_sym_name: Optional[str]
|
||||
|
||||
def __init__(self, rfile: BinaryIO, wfile: BinaryIO = None):
|
||||
self.rfile = rfile
|
||||
self.wfile = wfile
|
||||
self.stack = []
|
||||
self.pid = None
|
||||
self.path = None
|
||||
|
||||
def before(self) -> None: pass
|
||||
def after(self) -> None: pass
|
||||
def before_fallback(self, func_name: str, *args) -> str: pass
|
||||
def after_fallback(self, func_name: str, *args, **kwargs) -> None: pass
|
||||
|
||||
def handle(self):
|
||||
first = self.rfile.readline()
|
||||
meta = {a[0]: a[1] for a in [tuple(p.decode('utf-8').split(':', 1)) for p in first.split(b' ', 3)[3].strip().split(b';')]}
|
||||
self.pid = int(meta['PID']) if 'PID' in meta else None
|
||||
self.path = meta['PATH'] if 'PATH' in meta else None
|
||||
self.stack = []
|
||||
print(f'Process with PID {self.pid} connected ({self.path})')
|
||||
def parse(self):
|
||||
self.before()
|
||||
try:
|
||||
while True:
|
||||
@@ -115,25 +118,25 @@ class Handler(StreamRequestHandler):
|
||||
m = re.match(r'\s*"', argument)
|
||||
if m:
|
||||
idx = len(m.group(0)) - 1
|
||||
s, i = Handler.parse_str(argument[idx:])
|
||||
s, i = Parser.parse_str(argument[idx:])
|
||||
idx = i
|
||||
if idx < len(argument) and argument[idx] == ',':
|
||||
if idx < len(argument) and argument[idx] in ',;':
|
||||
idx += 1
|
||||
return s, idx
|
||||
m = re.match(r'\s*\[', argument)
|
||||
if m:
|
||||
idx = len(m.group(0))
|
||||
s, i = Handler.parse_args(argument[idx:])
|
||||
s, i = Parser.parse_args(argument[idx:])
|
||||
idx += i
|
||||
if idx < len(argument) and argument[idx] == ',':
|
||||
if idx < len(argument) and argument[idx] in ',;':
|
||||
idx += 1
|
||||
return s, idx
|
||||
m = re.match(r'\s*\{', argument)
|
||||
if m:
|
||||
idx = len(m.group(0))
|
||||
s, i = Handler.parse_args(argument[idx:], named=True)
|
||||
s, i = Parser.parse_args(argument[idx:], named=True)
|
||||
idx += i
|
||||
if idx < len(argument) and argument[idx] == ',':
|
||||
if idx < len(argument) and argument[idx] in ',;':
|
||||
idx += 1
|
||||
return s, idx
|
||||
m = re.match(r'^\s*(.*?)([,:\]}]|$)', argument)
|
||||
@@ -153,7 +156,7 @@ class Handler(StreamRequestHandler):
|
||||
return val, idx - 1
|
||||
if argument[idx] == '[':
|
||||
idx += 1
|
||||
l, i = Handler.parse_args(argument[idx:])
|
||||
l, i = Parser.parse_args(argument[idx:])
|
||||
idx += i
|
||||
if idx < len(argument) and argument[idx] == ',':
|
||||
idx += 1
|
||||
@@ -169,14 +172,14 @@ class Handler(StreamRequestHandler):
|
||||
flags = [f.strip() for f in flags[1:-1].split('|') if len(f.strip()) > 0]
|
||||
return (val, flags), idx
|
||||
elif argument[idx] == '"':
|
||||
s, i = Handler.parse_str(argument[idx:])
|
||||
s, i = Parser.parse_str(argument[idx:])
|
||||
idx += i
|
||||
if idx < len(argument) and argument[idx] == ',':
|
||||
idx += 1
|
||||
return (val, s), idx
|
||||
elif argument[idx] == '{':
|
||||
idx += 1
|
||||
l, i = Handler.parse_args(argument[idx:], named=True)
|
||||
l, i = Parser.parse_args(argument[idx:], named=True)
|
||||
idx += i
|
||||
if idx < len(argument) and argument[idx] == ',':
|
||||
idx += 1
|
||||
@@ -192,9 +195,7 @@ class Handler(StreamRequestHandler):
|
||||
return (val, value), idx
|
||||
|
||||
@staticmethod
|
||||
def parse_args(arguments: str, named: bool = False) -> tuple[tuple or dict, int]:
|
||||
# FIXME parse dicts in lists, dicts in dicts, ...
|
||||
print(named, arguments)
|
||||
def parse_args(arguments: str, named: bool = False, ret: bool = False) -> tuple[tuple or dict, int]:
|
||||
args = [] if not named else {}
|
||||
idx = 0
|
||||
name = None
|
||||
@@ -202,11 +203,15 @@ class Handler(StreamRequestHandler):
|
||||
if arguments[idx] == ']' and not named or arguments[idx] == '}' and named:
|
||||
idx += 1
|
||||
break
|
||||
elif ret and named:
|
||||
m = re.match(r'^\s*([^=]+)=', arguments[idx:])
|
||||
idx += len(m.group(0))
|
||||
name = m.group(1)
|
||||
elif named:
|
||||
m = re.match(r'^\s*([^:]+):', arguments[idx:])
|
||||
idx += len(m.group(0))
|
||||
name = m.group(1)
|
||||
val, i = Handler.parse_arg(arguments[idx:])
|
||||
val, i = Parser.parse_arg(arguments[idx:])
|
||||
if named:
|
||||
args[name] = val
|
||||
else:
|
||||
@@ -219,7 +224,7 @@ class Handler(StreamRequestHandler):
|
||||
self.pid, self.tid = int(pid), int(tid)
|
||||
if not data.startswith(b'return ') and not data == b'return':
|
||||
call = data.decode('utf-8')
|
||||
print(f'[{self.pid}][{self.tid}] {call}')
|
||||
#print(f'[{self.pid}][{self.tid}] {call}')
|
||||
func_name = call[:call.find('(')]
|
||||
ret = call[call.rfind(':'):]
|
||||
m = RET_ADDR_RE.match(ret)
|
||||
@@ -228,7 +233,7 @@ class Handler(StreamRequestHandler):
|
||||
self.dli_file_name = g_fname
|
||||
self.rel_ret_addr = int(g_ret, 0)
|
||||
self.dli_sym_name = g_sym
|
||||
args, _ = Handler.parse_args(call[call.find('(') + 1:call.rfind(':') - 1])
|
||||
args, _ = Parser.parse_args(call[call.find('(') + 1:call.rfind(':') - 1])
|
||||
self.stack.append((self.ret_addr, self.dli_file_name, self.rel_ret_addr, self.dli_sym_name, func_name, args))
|
||||
try:
|
||||
func = getattr(self, f'before_{func_name}')
|
||||
@@ -242,12 +247,21 @@ class Handler(StreamRequestHandler):
|
||||
command = func(*args) or self.before_fallback(func_name, *args) or 'ok'
|
||||
except NotImplementedError:
|
||||
command = self.before_fallback(func_name, *args) or 'ok'
|
||||
print(f'[{self.pid}][{self.tid}] -> {command}')
|
||||
self.wfile.write(command.encode('utf-8') + b'\n')
|
||||
if self.wfile:
|
||||
#print(f'[{self.pid}][{self.tid}] -> {command}')
|
||||
self.wfile.write(command.encode('utf-8') + b'\n')
|
||||
else:
|
||||
ret = data.decode('utf-8')
|
||||
ret_value, _ = Handler.parse_arg(ret[7:].split(';', 1)[0])
|
||||
# FIXME parse return values (errno, ...)
|
||||
ret = ret.split(';', 1)
|
||||
other_vals = ret[1].strip() if len(ret) > 1 else ''
|
||||
ret_value, _ = Parser.parse_arg(ret[0][7:])
|
||||
kwargs = {}
|
||||
if other_vals.startswith('errno '):
|
||||
ret = other_vals[6:].split(';', 1)
|
||||
kwargs['errno'] = ret[0].strip()
|
||||
other_vals = ret[1].strip() if len(ret) > 1 else ''
|
||||
if len(other_vals) > 0:
|
||||
kwargs, _ = Parser.parse_args(other_vals, named=True, ret=True)
|
||||
(self.ret_addr, self.dli_file_name, self.rel_ret_addr, self.dli_sym_name, func_name, args) = self.stack.pop()
|
||||
try:
|
||||
func = getattr(self, f'after_{func_name}')
|
||||
@@ -259,15 +273,15 @@ class Handler(StreamRequestHandler):
|
||||
if func is None:
|
||||
raise NotImplementedError()
|
||||
if ret_value is None:
|
||||
func(*args)
|
||||
func(*args, **kwargs)
|
||||
else:
|
||||
func(*args, ret_value)
|
||||
func(*args, ret_value, **kwargs)
|
||||
except NotImplementedError:
|
||||
if ret_value is None:
|
||||
self.after_fallback(func_name, *args)
|
||||
self.after_fallback(func_name, *args, **kwargs)
|
||||
else:
|
||||
self.after_fallback(func_name, *args, ret_value)
|
||||
print(f'[{self.pid}][{self.tid}] -> {ret}')
|
||||
self.after_fallback(func_name, *args, ret_value, **kwargs)
|
||||
#print(f'[{self.pid}][{self.tid}] -> {ret}')
|
||||
|
||||
def before_malloc(self, size: int) -> str:
|
||||
raise NotImplementedError()
|
||||
@@ -548,6 +562,17 @@ class Handler(StreamRequestHandler):
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class Handler(StreamRequestHandler, Parser):
|
||||
def handle(self):
|
||||
first = self.rfile.readline()
|
||||
meta = {a[0]: a[1] for a in [tuple(p.decode('utf-8').split(':', 1)) for p in first.split(b' ', 3)[3].strip().split(b';')]}
|
||||
self.pid = int(meta['PID']) if 'PID' in meta else None
|
||||
self.path = meta['PATH'] if 'PATH' in meta else None
|
||||
print(f'Process with PID {self.pid} connected ({self.path})')
|
||||
self.stack = []
|
||||
self.parse()
|
||||
|
||||
|
||||
def intercept(socket: str, handler: type[Handler]) -> None:
|
||||
try:
|
||||
with ThreadedUnixStreamServer(socket, handler) as server:
|
||||
|
||||
Reference in New Issue
Block a user