95 lines
3.6 KiB
Python
95 lines
3.6 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""ar_sync.py — синк ключей api-route (sol 6.1) с ПК на прод iishko.
|
||
panel_bank.jsonl -> проба sol -> ar_keys.jsonl -> scp -> ar_inject.py --apply -> restart new-api.
|
||
Молчит, если новых живых ключей нет.
|
||
"""
|
||
import base64, json, os, subprocess, sys
|
||
import requests
|
||
import urllib3
|
||
|
||
import os as _os
|
||
PROD_HOST = _os.environ.get("PROD_HOST", "root@127.0.0.1")
|
||
FARM_HOST = _os.environ.get("FARM_HOST", "root@127.0.0.1")
|
||
PX_USER = _os.environ.get("PX_USER", "")
|
||
PX_PASS = _os.environ.get("PX_PASS", "")
|
||
PX_HOST1 = _os.environ.get("PX_HOST1", "127.0.0.1")
|
||
PX_HOST2 = _os.environ.get("PX_HOST2", "127.0.0.1")
|
||
PX_HOST3 = _os.environ.get("PX_HOST3", "127.0.0.1")
|
||
PX_HOST4 = _os.environ.get("PX_HOST4", "127.0.0.1")
|
||
MAIL_LOCAL = _os.environ.get("MAIL_LOCAL", "user")
|
||
PROBE_EMAIL = _os.environ.get("PROBE_EMAIL", "probe@example.com")
|
||
PROBE_EMAIL2 = _os.environ.get("PROBE_EMAIL2", "probe2@example.com")
|
||
PROBE_EMAIL3 = _os.environ.get("PROBE_EMAIL3", "probe3@example.com")
|
||
|
||
|
||
urllib3.disable_warnings()
|
||
TMP = r"C:\Users\User\tmp"
|
||
BANK = TMP + r"\panel_bank.jsonl"
|
||
OUT = TMP + r"\ar_keys.jsonl"
|
||
LOG = TMP + r"\ar_sync.log"
|
||
BASE = "https://www.api-route.com"
|
||
HOST = PROD_HOST
|
||
DIRECT = "-o ConnectTimeout=20 -o StrictHostKeyChecking=no"
|
||
|
||
|
||
def log(s):
|
||
print(s, flush=True)
|
||
open(LOG, "a", encoding="utf-8").write(s + "\n")
|
||
|
||
|
||
def load_keys():
|
||
if not os.path.exists(OUT):
|
||
return set()
|
||
out = set()
|
||
for l in open(OUT, encoding="utf-8"):
|
||
l = l.strip()
|
||
if l.startswith("{"):
|
||
try:
|
||
out.add(json.loads(l)["key"])
|
||
except Exception:
|
||
pass
|
||
return out
|
||
|
||
|
||
def main():
|
||
have = load_keys()
|
||
recs = []
|
||
if os.path.exists(BANK):
|
||
for l in open(BANK, encoding="utf-8", errors="replace"):
|
||
l = l.strip()
|
||
if l.startswith("{"):
|
||
try:
|
||
recs.append(json.loads(l))
|
||
except Exception:
|
||
pass
|
||
cand = [r for r in recs if r.get("base") == BASE and r.get("key") and r["key"] not in have]
|
||
new = []
|
||
for r in cand:
|
||
try:
|
||
resp = requests.post(BASE + "/v1/chat/completions", timeout=45, verify=False,
|
||
headers={"Authorization": "Bearer " + r["key"],
|
||
"Content-Type": "application/json"},
|
||
json={"model": "gpt-6.1-sol",
|
||
"messages": [{"role": "user", "content": "say OK"}]})
|
||
if resp.status_code == 200 and "choices" in resp.text:
|
||
new.append(r["key"])
|
||
except Exception:
|
||
pass
|
||
if not new:
|
||
return
|
||
with open(OUT, "a", encoding="utf-8") as f:
|
||
for k in new:
|
||
f.write(json.dumps({"key": k}) + "\n")
|
||
p = subprocess.run('scp %s %s %s:/root/ar_keys.jsonl' % (DIRECT, OUT, HOST),
|
||
shell=True, capture_output=True, text=True, timeout=240)
|
||
if p.returncode != 0:
|
||
return log("scp FAIL %s" % (p.stderr or p.stdout)[-100:])
|
||
b = base64.b64encode(b"cd /root && python3 ar_inject_pg.py --apply && systemctl restart new-api && sleep 5 && systemctl is-active new-api").decode()
|
||
cmd = 'ssh %s %s "echo %s | base64 -d | bash"' % (DIRECT, HOST, b)
|
||
p2 = subprocess.run(cmd, shell=True, capture_output=True, text=True, timeout=280)
|
||
out = (p2.stdout or "") + (p2.stderr or "")
|
||
log("sync +%d total=%d | %s" % (len(new), len(have) + len(new), out.strip().replace("\n", " ")[-200:]))
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main() |