forked from unimatrix/web
Add receipt-bound native kit delivery and Paper metadata verification
This commit is contained in:
parent
eccff352da
commit
99b4c89ee9
3 changed files with 909 additions and 1 deletions
79
network/server-control/install-kit-equipment.py
Normal file
79
network/server-control/install-kit-equipment.py
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
"""Install only the reviewed kit bridge modules; refuse unrelated source changes."""
|
||||
import ast
|
||||
import hashlib
|
||||
import os
|
||||
from pathlib import Path
|
||||
import py_compile
|
||||
import sys
|
||||
|
||||
BASELINES = {
|
||||
'player_kits.py': 'fa76420423941189ba08b94382274e40762121e30b008ec0d08e35aa1a00fda3',
|
||||
'kit_commerce.py': '29395a483fdb3f6d94b36fa57587e3a1391d6e4ed8a44159cbb488dd47d960e6',
|
||||
}
|
||||
FILES = ['player_kits.py', 'kit_commerce.py', 'kit_equipment.py', 'kit-equipment-contract.json']
|
||||
|
||||
|
||||
def kit_body_limit(source):
|
||||
tree = ast.parse(source)
|
||||
candidates = [node for node in tree.body if isinstance(node, ast.FunctionDef)
|
||||
and any(isinstance(value, ast.Constant) and value.value == '/kits/player/definition' for value in ast.walk(node))]
|
||||
if len(candidates) != 1:
|
||||
raise RuntimeError('Native office kit handler changed')
|
||||
node = candidates[0]
|
||||
lines = source.splitlines(keepends=True)
|
||||
section = ''.join(lines[node.lineno - 1:node.end_lineno])
|
||||
old, new = 'if not 0 < length <= 8192:', 'if not 0 < length <= 32768:'
|
||||
if section.count(new) == 1 and old not in section:
|
||||
return source
|
||||
if section.count(old) != 1:
|
||||
raise RuntimeError('Native office body limit changed')
|
||||
lines[node.lineno - 1:node.end_lineno] = [section.replace(old, new)]
|
||||
return ''.join(lines)
|
||||
|
||||
|
||||
def install(apply=False):
|
||||
source = Path(__file__).resolve().parent
|
||||
target = Path('/opt/dunerealms/server-control')
|
||||
plans = {}
|
||||
for name in FILES:
|
||||
content = (source / name).read_bytes()
|
||||
destination = target / name
|
||||
before = destination.read_bytes() if destination.exists() else None
|
||||
if before == content:
|
||||
continue
|
||||
if before is not None and hashlib.sha256(before).hexdigest() != BASELINES.get(name):
|
||||
raise RuntimeError('Concurrent native change detected in ' + name)
|
||||
plans[destination] = content
|
||||
agent = target / 'agent.py'
|
||||
before = agent.read_text()
|
||||
after = kit_body_limit(before)
|
||||
if after != before:
|
||||
plans[agent] = after.encode()
|
||||
for name in ['player_kits.py', 'kit_commerce.py', 'kit_equipment.py']:
|
||||
py_compile.compile(str(source / name), doraise=True)
|
||||
if not apply:
|
||||
print('Plan: install', len(plans), 'exact kit module/body-limit changes; no files changed')
|
||||
return
|
||||
originals = {path: path.read_bytes() if path.exists() else None for path in plans}
|
||||
changed = []
|
||||
try:
|
||||
for path, content in plans.items():
|
||||
temporary = path.with_name(path.name + '.kit-v3-new')
|
||||
temporary.write_bytes(content)
|
||||
temporary.chmod(path.stat().st_mode & 0o777 if path.exists() else 0o644)
|
||||
if path.suffix == '.py':
|
||||
py_compile.compile(str(temporary), doraise=True)
|
||||
os.replace(temporary, path)
|
||||
changed.append(path)
|
||||
except Exception:
|
||||
for path in reversed(changed):
|
||||
if originals[path] is None:
|
||||
path.unlink()
|
||||
else:
|
||||
path.write_bytes(originals[path])
|
||||
raise
|
||||
print('Installed reviewed kit metadata modules; restart the private control service to activate')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
install('--apply' in sys.argv)
|
||||
9
network/server-control/kit-delivery-schema.sql
Normal file
9
network/server-control/kit-delivery-schema.sql
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
CREATE TABLE IF NOT EXISTS dune_kit_deliveries (
|
||||
id UUID PRIMARY KEY,
|
||||
buyer_uuid UUID NOT NULL,
|
||||
snapshot_json TEXT NOT NULL CHECK (octet_length(snapshot_json) <= 65536),
|
||||
snapshot_hash CHAR(64) NOT NULL CHECK (snapshot_hash ~ '^[a-f0-9]{64}$'),
|
||||
created_at BIGINT NOT NULL CHECK (created_at > 0),
|
||||
delivered_at BIGINT NULL CHECK (delivered_at IS NULL OR delivered_at > 0)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS dune_kit_deliveries_pending ON dune_kit_deliveries(buyer_uuid,created_at,id) WHERE delivered_at IS NULL;
|
||||
|
|
@ -150,4 +150,3 @@ def save(connect, payload):
|
|||
con.rollback()
|
||||
raise
|
||||
return {'ok': True, 'uuid': data['uuid'], 'office': data['office'], 'term': data['term'], 'version': data['version'] + 1}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff