Fix trade recovery, local web isolation and backtest risk accounting

This commit is contained in:
Igol committed 2026-10-22 21:49:20 +03:00
1 parent 5c6b0dc54a
commit 6aa76064dc
29 files changed
+2662 -1693

No files matched your search

+12 -5
View File
@@ -24,6 +24,7 @@ from dataclasses import fields
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from pathlib import Path
from request_security import trusted_request
import keystore
import lead_window as lw
import live
@@ -111,6 +112,10 @@ def scanner_add(symbol: str, connections: list[dict]) -> tuple[dict | None, str]
DATA_DIR = ROOT / "web_sim_data"
SETTINGS_FILE = DATA_DIR / "settings.json"
PAGE = ROOT / "web_sim.html"
STATIC_ASSETS = {
"/static/app.js": (ROOT / "web" / "app.js", "text/javascript"),
"/static/app.css": (ROOT / "web" / "app.css", "text/css"),
}
PORT = 8765
CONNECTION_FIELDS = set(lw.CONNECTION_KEYS)
COMMON_FIELDS = [f.name for f in fields(sp.PaperSettings)
@@ -640,9 +645,15 @@ class Handler(BaseHTTPRequestHandler):
self._send(code, json.dumps(data, ensure_ascii=False).encode("utf-8"))
def do_GET(self) -> None:
if not trusted_request(self):
self._json({"error": "недоверенный адрес страницы"}, 403)
return
path, _, query = self.path.partition("?")
if path in ("/", "/index.html"):
self._send(200, PAGE.read_bytes(), "text/html")
elif path in STATIC_ASSETS:
asset, kind = STATIC_ASSETS[path]
self._send(200, asset.read_bytes(), kind)
elif path == "/api/state":
self._json(SIM.state())
elif path == "/api/arb":
@@ -804,11 +815,7 @@ LOCAL_HOSTS = {"127.0.0.1", "localhost", "[::1]"}
def local_request(handler) -> bool:
"""A command from this simulator's own page: the Host is the local address (no DNS rebinding) and the request
carries the X-Sim header — another web site open in the browser cannot add it without a CORS preflight, which
this server never allows, so it cannot press buttons here (real trading, keys)."""
host = (handler.headers.get("Host") or "").rsplit(":", 1)[0].lower()
return host in LOCAL_HOSTS and handler.headers.get("X-Sim") == "1"
return trusted_request(handler, command=True)
def main() -> None: