-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_service.py
More file actions
188 lines (156 loc) · 5.28 KB
/
Copy pathworker_service.py
File metadata and controls
188 lines (156 loc) · 5.28 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
worker_service.py
OS level daemon — runs at California VPS
Watches for incoming hash transmissions
Verifies integrity
Rebuilds payload from fold
Executes instructions
Folds report — sends back as hash
No persistent connection needed
Receives → executes → reports → sleeps
"""
import json
import hashlib
import time
import os
import subprocess
from pathlib import Path
from data_to_fold import encode_to_6base
WATCH_DIR = Path("incoming")
DONE_DIR = Path("executed")
OUT_DIR = Path("output")
REPORT_DIR = Path("reports")
WATCH_DIR.mkdir(exist_ok=True)
DONE_DIR.mkdir(exist_ok=True)
OUT_DIR.mkdir(exist_ok=True)
REPORT_DIR.mkdir(exist_ok=True)
def verify_transmission(transmission, folded):
verify_obj = {
"uid": transmission["uid"],
"seed": transmission["seed"],
"input": folded,
"timestamp": transmission["timestamp"]
}
computed = hashlib.sha256(
json.dumps(verify_obj, sort_keys=True).encode()
).hexdigest()
return computed == transmission["hash_key"]
def execute_payload(payload):
"""
Execute instruction from payload
Supports: shell commands, state updates,
file writes, status checks
Extend this as needed
"""
result = {
"status": "unknown",
"output": "",
"timestamp": time.time()
}
action = payload.get("action", "status")
if action == "status":
result["status"] = "ok"
result["output"] = {
"machine": os.uname().nodename,
"uptime": os.popen("uptime -p").read().strip(),
"disk": os.popen("df -h /").read().strip(),
"memory": os.popen("free -h").read().strip()
}
elif action == "shell":
cmd = payload.get("command", "echo ok")
out = subprocess.run(
cmd, shell=True,
capture_output=True, text=True, timeout=30
)
result["status"] = "executed"
result["output"] = {
"stdout": out.stdout.strip(),
"stderr": out.stderr.strip(),
"code": out.returncode
}
elif action == "write_file":
path = Path(payload.get("path", "output/received.txt"))
content = payload.get("content", "")
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(content)
result["status"] = "written"
result["output"] = str(path)
elif action == "ping":
result["status"] = "alive"
result["output"] = "worker online — California VPS"
return result
def build_report(uid, seed, result, original_hash):
report_data = {
"worker": "california_vps",
"responding_to": original_hash[:16],
"uid": uid,
"result": result,
"timestamp": time.time()
}
folded = encode_to_6base(report_data)
report_hash_obj = {
"uid": uid,
"seed": seed,
"input": folded,
"timestamp": report_data["timestamp"]
}
report_hash = hashlib.sha256(
json.dumps(report_hash_obj, sort_keys=True).encode()
).hexdigest()
transmission = {
"uid": uid,
"hash_key": report_hash,
"seed": seed,
"timestamp": report_data["timestamp"]
}
ts = int(report_data["timestamp"])
with open(REPORT_DIR / f"report_{ts}.json", "w") as f:
json.dump({"transmission": transmission, "fold": folded}, f, indent=2)
print(f"[✔] Report folded — hash: {report_hash[:16]}...")
print(f"[✔] Report strand: {folded['strand_length']} bits")
print(f"[✔] Saved: reports/report_{ts}.json")
print(f"[✔] Send this hash back to Texas: {report_hash[:16]}...")
def process_transmission(path):
print(f"\n[+] Incoming: {path.name}")
with open(path) as f:
packet = json.load(f)
transmission = packet["transmission"]
folded = packet["fold"]
print(f"[+] UID: {transmission['uid']}")
print(f"[+] Hash: {transmission['hash_key'][:16]}...")
from machine_auth import verify_mac_for_fold
if not verify_mac_for_fold(transmission["hash_key"]):
print("[!] Fold self destructed — unauthorized machine")
path.rename(DONE_DIR / f"REJECTED_{path.name}")
return
if not verify_transmission(transmission, folded):
print("[!] Hash mismatch — rejected")
return
print("[✔] Integrity verified")
payload = folded.get("payload", {})
print(f"[+] Action: {payload.get('action', 'status')}")
result = execute_payload(payload)
print(f"[✔] Executed — status: {result['status']}")
build_report(
transmission["uid"],
transmission["seed"],
result,
transmission["hash_key"]
)
path.rename(DONE_DIR / path.name)
print(f"[✔] Moved to executed/")
def watch_loop():
print("== HASHKEY WORKER SERVICE ==")
print("== California VPS — OS Level ==")
print(f"[+] Watching: {WATCH_DIR.resolve()}")
print("[+] Waiting for transmissions...\n")
while True:
incoming = list(WATCH_DIR.glob("*.json"))
for path in incoming:
try:
process_transmission(path)
except Exception as e:
print(f"[!] Error processing {path.name}: {e}")
time.sleep(2)
if __name__ == "__main__":
watch_loop()