fix: DrHider — CSV in ZIP, double-encode fix, multipart parser
Deploy contracts-flask / validate (push) Successful in 0s
Deploy contracts-flask / validate (push) Successful in 0s
This commit is contained in:
+47
-11
@@ -217,28 +217,64 @@ class Handler(BaseHTTPRequestHandler):
|
||||
def _handle_drhider(self):
|
||||
"""Обфускация: multipart files → LLM-NER → ZIP."""
|
||||
from services.drhider import obfuscate_files
|
||||
from services.llm_client import HttpxLLMClient
|
||||
from services.upload import parse_multipart
|
||||
|
||||
length = int(self.headers.get("Content-Length", 0))
|
||||
raw = self.rfile.read(length)
|
||||
ctype = self.headers.get("Content-Type", "")
|
||||
|
||||
files = parse_multipart(raw, ctype)
|
||||
# Парсим multipart вручную
|
||||
import cgi, re
|
||||
_, params = cgi.parse_header(ctype)
|
||||
boundary = params.get("boundary", "").encode()
|
||||
if not boundary:
|
||||
self._json({"ok": False, "error": "No multipart boundary"}, 400)
|
||||
return
|
||||
|
||||
files = []
|
||||
# Разбиваем по boundary
|
||||
parts = raw.split(b"--" + boundary)
|
||||
for part in parts:
|
||||
if b"Content-Disposition" not in part:
|
||||
continue
|
||||
try:
|
||||
header_end = part.index(b"\r\n\r\n")
|
||||
except ValueError:
|
||||
continue
|
||||
headers_raw = part[:header_end].decode("latin-1", errors="replace")
|
||||
content = part[header_end + 4:]
|
||||
if content.endswith(b"\r\n"):
|
||||
content = content[:-2]
|
||||
# Извлечь filename
|
||||
m = re.search(r'filename="([^"]*)"', headers_raw)
|
||||
if not m:
|
||||
continue
|
||||
fname = m.group(1)
|
||||
if not fname:
|
||||
continue
|
||||
files.append((fname, content, ""))
|
||||
|
||||
if not files:
|
||||
self._json({"ok": False, "error": "Нет файлов"}, 400)
|
||||
return
|
||||
|
||||
file_list = [(f.filename, f.content, f.content_type) for f in files]
|
||||
# LLM-клиент (совместимый с drhider.LLMClient протоколом: complete(prompt)->str)
|
||||
class _VMLLM:
|
||||
def complete(self, prompt):
|
||||
import httpx
|
||||
r = httpx.post(
|
||||
os.environ.get("LLM_URL", "https://api.aillm.ru/v1/chat/completions"),
|
||||
json={"model": os.environ.get("LLM_MODEL", "gpt-oss-120b"),
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"max_tokens": 8000, "temperature": 0.1},
|
||||
headers={"Authorization": f"Bearer {os.environ.get('LLM_KEY', '')}",
|
||||
"Content-Type": "application/json"},
|
||||
timeout=120
|
||||
)
|
||||
r.raise_for_status()
|
||||
return r.json()["choices"][0]["message"]["content"]
|
||||
|
||||
try:
|
||||
llm = HttpxLLMClient(
|
||||
os.environ.get("LLM_API_URL", "https://api.aillm.ru/v1/chat/completions"),
|
||||
os.environ.get("LLM_API_KEY", ""),
|
||||
os.environ.get("LLM_MODEL", "gpt-oss-120b")
|
||||
)
|
||||
zip_data, _csv = obfuscate_files(file_list, llm_client=llm)
|
||||
|
||||
zip_data, _csv = obfuscate_files(files, llm_client=_VMLLM())
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/zip")
|
||||
self.send_header("Content-Disposition", 'attachment; filename="drhider_output.zip"')
|
||||
|
||||
Reference in New Issue
Block a user