-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (122 loc) · 4.64 KB
/
Copy pathmain.py
File metadata and controls
151 lines (122 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
"""
main.py
--------
Entry point for Horton.
On first run (no .env file), launches an interactive setup screen that
lets the user choose their LLM provider and paste their API key.
The key is written to .env and never logged.
On subsequent runs, loads the existing .env and launches the game directly.
Usage:
python main.py # launch game
python main.py --setup # re-run provider setup
python main.py --scenario PATH # use a custom scenario file
"""
from __future__ import annotations
import argparse
import os
import sys
from pathlib import Path
def _load_env() -> None:
"""Load .env if it exists."""
env_path = Path(__file__).parent / ".env"
if env_path.exists():
from dotenv import load_dotenv
load_dotenv(env_path)
def _run_setup() -> None:
"""
Interactive provider setup — runs before the main TUI.
Uses plain terminal I/O (not Textual) so it works before dependencies
are fully initialized.
"""
import getpass
CYAN = "\033[96m"
MAGENTA = "\033[95m"
GREEN = "\033[92m"
DIM = "\033[2m"
RESET = "\033[0m"
BOLD = "\033[1m"
print()
print(f"{MAGENTA}{BOLD}╔══════════════════════════════════════════════╗{RESET}")
print(f"{MAGENTA}{BOLD}║ HORTON — FIRST RUN SETUP ║{RESET}")
print(f"{MAGENTA}{BOLD}╚══════════════════════════════════════════════╝{RESET}")
print()
print(f"{CYAN}Choose your LLM provider:{RESET}")
print(f" {BOLD}[1]{RESET} Anthropic Claude (claude-sonnet-4-6 + haiku-4-5)")
print(f" {BOLD}[2]{RESET} Google Gemini (gemini-1.5-pro + flash) {GREEN}← free tier available{RESET}")
print()
while True:
choice = input(f"{MAGENTA}>{RESET} ").strip()
if choice in ("1", "2"):
break
print(" Enter 1 or 2")
if choice == "1":
provider = "anthropic"
key_name = "ANTHROPIC_API_KEY"
key_hint = "sk-ant-..."
docs_hint = "https://console.anthropic.com"
else:
provider = "gemini"
key_name = "GOOGLE_API_KEY"
key_hint = "AIza..."
docs_hint = "https://aistudio.google.com — free tier, no credit card"
print()
print(f"{CYAN}Paste your {key_name}:{RESET}")
print(f"{DIM}(input hidden) Get one at: {docs_hint}{RESET}")
print()
api_key = getpass.getpass(f"{MAGENTA}>{RESET} ").strip()
if not api_key:
print(f"\n{MAGENTA}No key entered. Exiting.{RESET}")
sys.exit(1)
# Write .env
env_path = Path(__file__).parent / ".env"
with open(env_path, "w") as f:
f.write(f"LLM_PROVIDER={provider}\n")
f.write(f"{key_name}={api_key}\n")
print()
print(f"{GREEN}✓ Config saved to .env{RESET}")
print(f"{DIM} To change provider later, run: python main.py --setup{RESET}")
print()
# Set in current process env so we don't need to reload
os.environ["LLM_PROVIDER"] = provider
os.environ[key_name] = api_key
def _validate_env() -> bool:
"""Check that required env vars are set for the chosen provider."""
provider = os.environ.get("LLM_PROVIDER", "").lower()
if provider == "anthropic":
return bool(os.environ.get("ANTHROPIC_API_KEY"))
elif provider == "gemini":
return bool(os.environ.get("GOOGLE_API_KEY"))
return False
def main() -> None:
parser = argparse.ArgumentParser(description="Horton — Social Engineering CTF")
parser.add_argument("--setup", action="store_true", help="Re-run provider setup")
parser.add_argument(
"--scenario",
default=str(Path(__file__).parent / "scenarios" / "github_pat_heist.json"),
help="Path to scenario JSON file",
)
args = parser.parse_args()
# Load existing env
_load_env()
# Run setup if requested or if no valid config found
env_path = Path(__file__).parent / ".env"
if args.setup or not env_path.exists() or not _validate_env():
_run_setup()
# Validate once more after setup
if not _validate_env():
print("\n[ERROR] No valid API key configured. Run: python main.py --setup")
sys.exit(1)
# Launch game
from engine.game import GameEngine
from ui.app import HortonApp
try:
engine = GameEngine.from_scenario_file(args.scenario)
app = HortonApp(engine=engine)
app.run()
except KeyboardInterrupt:
pass
except Exception as e:
print(f"\n[ERROR] {e}")
sys.exit(1)
if __name__ == "__main__":
main()