-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathrift_server.py
More file actions
298 lines (250 loc) · 10.6 KB
/
Copy pathrift_server.py
File metadata and controls
298 lines (250 loc) · 10.6 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# copied from https://github.com/ynsrc/python-simple-rest-api/blob/main/server.py
import json
import argparse
import hmac
import signal
import threading
import os
import shutil
import ssl
from collections import namedtuple
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
from librift.utils import get_logger
from rift_engine import RiftEngine
from librift.rift_cfg import RiftConfig
from libsrv.flirtjob import JobRegistry, JobStatus
from libsrv.flirtworker import FlirtWorker
from libsrv.server_storage import ServerStorage
logger = get_logger()
FileResponse = namedtuple("FileResponse", ["path", "download_name"])
class ApiRequestHandler(BaseHTTPRequestHandler):
def __init__(self, request, client_address, ref_req, api_ref):
self.api = api_ref
super().__init__(request, client_address, ref_req)
def send_json_response(self, status_code, data, message=None):
self.send_response(status_code, message)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(json.dumps(data, indent=4).encode())
def call_api(self, method, path, args):
if path in self.api.routing[method]:
try:
result = self.api.routing[method][path](args)
if isinstance(result, FileResponse):
self.send_file(result)
else:
self.send_json_response(200, result)
except Exception as e:
self.send_json_response(500, {"error": e.args}, "Server Error")
else:
self.send_json_response(404, {"error": "not found"}, "Not Found")
def do_GET(self):
self._handle("GET")
def do_POST(self):
self._handle("POST")
def _handle(self, method):
parsed_url = urlparse(self.path)
path = parsed_url.path
if path not in self.api.auth_exempt_paths and not self._is_authorized():
self.send_json_response(401, {"error": "unauthorized"})
return
if method == "GET":
args = parse_qs(parsed_url.query)
for k in args.keys():
if len(args[k]) == 1:
args[k] = args[k][0]
self.call_api("GET", path, args)
else:
if self.headers.get("content-type") != "application/json":
self.send_json_response(400, {"error": "posted data must be in json format"})
return
data_len = int(self.headers.get("content-length"))
data = self.rfile.read(data_len).decode()
self.call_api("POST", path, json.loads(data))
def _is_authorized(self):
"""Perform authentication check if server mode is set to remote."""
if not self.api.require_auth:
return True
provided = self.headers.get("X-RIFT-API-KEY")
if provided is None:
return False
return hmac.compare_digest(provided, self.api.api_key or "")
def send_file(self, file_response):
"""Send FLIRT signature file"""
file_size = os.path.getsize(file_response.path)
self.send_response(200)
self.send_header("Content-Type", "application/octet-stream")
self.send_header("Content-Disposition", f'attachment; filename="{file_response.download_name}"')
self.send_header("Content-Length", str(file_size))
self.end_headers()
with open(file_response.path, "rb") as f:
shutil.copyfileobj(f, self.wfile)
class RIFT_API():
def __init__(self):
"""Initialization routine."""
self.routing = { "GET": { }, "POST": { }}
self.rift_api = None
self.logger = None
self.output_folder = None
self.storage = None
self.job_registry = JobRegistry(max_jobs=100)
self.worker = None
self.api_key = None
self.require_auth = False
self.auth_exempt_paths = {"/health"}
self.server_mode = None
def get(self, path):
def wrapper(fn):
self.routing["GET"][path] = fn
return wrapper
def post(self, path):
def wrapper(fn):
self.routing["POST"][path] = fn
return wrapper
def __call__(self, request, client_address, ref_request):
api_handler = ApiRequestHandler(request, client_address, ref_request, api_ref=self)
return api_handler
def start_worker(self):
"""Initialize and start the background worker."""
self.worker = FlirtWorker(
job_registry=self.job_registry,
rift_api=self.rift_api,
output_folder=self.output_folder,
logger=self.logger,
storage=self.storage,
is_remote = self.rift_api.cfg.server_mode == "remote"
)
self.worker.start()
def stop_worker(self):
"""Stop the background worker gracefully."""
if self.worker:
self.worker.stop()
api = RIFT_API()
@api.post("/flirt")
def submit_flirt_job(json_data):
"""Submit FLIRT generation job. Returns job_id immediately."""
logger.info("FLIRT job submission received")
logger.debug(json_data)
required_fields = ["commithash", "arch", "filetype", "crates", "target_triple"]
missing = [f for f in required_fields if f not in json_data]
if missing:
return {"error": f"Missing required fields: {missing}", "status": "error"}
if api.output_folder:
json_data["output_folder"] = api.output_folder
job = api.job_registry.create_job(json_data)
api.worker.submit(job.job_id)
return {
"job_id": job.job_id,
"status": job.status.value,
"message": "Job submitted successfully. Use GET /job?id=<job_id> to check status."
}
@api.get("/download")
def download_flirt(json_data):
"""Download a specific FLIRT signature"""
logger.info("Download request received")
required_fields = ["filename", "mode"]
missing = [f for f in required_fields if f not in json_data]
if missing:
return {"error": f"Missing required fields: {missing}", "status": "error"}
mode = json_data["mode"]
filename = json_data["filename"]
path = api.storage.get_flirt_path(filename) if api.storage else None
if not path:
return {"error": f"File not found: {filename}", "status": "error"}
if mode == "local":
return {"filename": filename, "path": path, "status": "ok"}
elif mode == "remote":
return FileResponse(path=path, download_name=filename)
else:
return {"error": f"Invalid mode: {mode}", "status": "error"}
@api.get("/job")
def get_job_status(args):
"""Get status of a specific job."""
job_id = args.get("id")
if not job_id:
return {"error": "Missing required parameter: id"}
job = api.job_registry.get_job(job_id)
if not job:
return {"error": f"Job not found: {job_id}"}
return job.to_dict()
@api.get("/jobs")
def list_jobs(args):
"""List all jobs, optionally filtered by status."""
status_filter = args.get("status")
if status_filter:
try:
status = JobStatus(status_filter)
return {"jobs": api.job_registry.list_jobs(status=status)}
except ValueError:
return {"error": f"Invalid status: {status_filter}"}
return {"jobs": api.job_registry.list_jobs()}
@api.get("/health")
def health_check(args):
"""Health check endpoint."""
pending = len(api.job_registry.list_jobs(status=JobStatus.PENDING))
running = len(api.job_registry.list_jobs(status=JobStatus.RUNNING))
return {
"status": "healthy",
"server_mode": api.server_mode,
"pending_jobs": pending,
"running_jobs": running,
"worker_alive": api.worker.is_alive() if api.worker else False
}
def main(args):
"""Main, loop entry."""
global logger
logger = get_logger(args.log, verbose=args.verbose)
rift_cfg = RiftConfig(logger, args.cfg)
if not rift_cfg.flirt_available:
logger.error("PCF.exe or sigmake.exe not found. Both files are necessary for rift_server to run")
return
if rift_cfg.server_mode == "remote":
missing = []
if not rift_cfg.api_key or rift_cfg.api_key == "NOT_SET":
missing.append("ApiKey")
if not rift_cfg.tls_cert or rift_cfg.tls_cert == "NOT_SET" or not os.path.isfile(rift_cfg.tls_cert):
missing.append("TlsCert")
if not rift_cfg.tls_key or rift_cfg.tls_key == "NOT_SET" or not os.path.isfile(rift_cfg.tls_key):
missing.append("TlsKey")
if missing:
logger.error(f"server_mode=remote requires a valid {', '.join(missing)} in the config. Refusing to start.")
return
# if remote, we want to set the api storage here. This is the folder we store the flirt signature on the server device
# However, if we are in local mode, we do not need this server storage! We store the files, whatever the user configures through the mask
rift_api = RiftEngine(logger, args.cfg, rift_cfg.server_storage)
api.rift_api = rift_api
api.logger = logger
api.storage = ServerStorage(logger, rift_cfg.server_storage)
api.api_key = rift_cfg.api_key
api.server_mode = rift_api.cfg.server_mode
api.require_auth = api.server_mode == "remote"
# Start background worker
api.start_worker()
httpd = HTTPServer((rift_api.cfg.api_ip, int(rift_api.cfg.api_port, 10)), api)
if api.server_mode == "remote":
try:
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain(certfile=rift_cfg.tls_cert, keyfile=rift_cfg.tls_key)
httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True)
except Exception:
logger.exception("Failed initializing TLS socket for remote mode")
api.stop_worker()
httpd.server_close()
return
def shutdown_handler(signum, frame):
logger.info("Shutdown signal received, stopping server...")
api.stop_worker()
threading.Thread(target=httpd.shutdown).start()
signal.signal(signal.SIGINT, shutdown_handler)
signal.signal(signal.SIGTERM, shutdown_handler)
logger.info(f"Starting RIFT_Server at {rift_api.cfg.api_ip}:{int(rift_api.cfg.api_port, 10)}")
httpd.serve_forever()
logger.info("Server stopped.")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--log", help="Log file output")
parser.add_argument("--verbose", default=False, action="store_true", help="Enable verbose logging")
parser.add_argument("--cfg", help="Path to rift_config.cfg", default="./rift_config.cfg")
args = parser.parse_args()
main(args)