Repository navigation
Expand file tree
/
Copy pathsync_version.py
More file actions
117 lines (105 loc) · 4.3 KB
/
Copy pathsync_version.py
File metadata and controls
117 lines (105 loc) · 4.3 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
#!/usr/bin/env python3
"""
Sync latest ZySentinel release version from GitHub to sec.zysens.com/version.json
"""
import urllib.request
import json
import os
import tempfile
import datetime
TARGET_FILE = "/www/wwwroot/sec.zysens.com/version.json"
REPO = "omysurya/zysentinel"
URL = f"https://api.github.com/repos/{REPO}/releases/latest"
def versi_lokal():
"""Versi plugin dari info.json (sumber tunggal, ditanam versi.py); tanpa literal versi di skrip ini."""
for p in (os.path.join(os.path.dirname(os.path.abspath(__file__)), "info.json"),
"/www/server/panel/plugin/sentinel/info.json"):
try:
with open(p, "r", encoding="utf-8") as f:
v = str(json.load(f).get("versions", "")).strip().lstrip("v")
if v:
return v
except Exception:
pass
return "0.0.0"
def sync_version():
ver = versi_lokal()
tag = f"v{ver}"
name = f"ZySentinel {tag}"
published_at = ""
html_url = f"https://github.com/{REPO}/releases/latest"
zip_name = f"zysentinel-{tag}.zip"
zip_url = f"https://github.com/{REPO}/releases/download/{tag}/{zip_name}"
tarball_url = f"https://api.github.com/repos/{REPO}/tarball/{tag}"
headers = {
"User-Agent": "ZySentinel-Web-Sync/1.0",
"Accept": "application/vnd.github.v3+json"
}
try:
req = urllib.request.Request(URL, headers=headers)
with urllib.request.urlopen(req, timeout=8) as resp:
data = json.loads(resp.read().decode("utf-8"))
tag = data.get("tag_name", tag).strip()
if not tag.startswith("v") and tag[0].isdigit():
ver = tag
tag = f"v{tag}"
else:
ver = tag.lstrip("v")
name = data.get("name", f"ZySentinel {tag}")
published_at = data.get("published_at", "")
html_url = data.get("html_url", f"https://github.com/{REPO}/releases/tag/{tag}")
tarball_url = data.get("tarball_url", f"https://api.github.com/repos/{REPO}/tarball/{tag}")
# Find matching zip asset
zip_name = f"zysentinel-{tag}.zip"
zip_url = f"https://github.com/{REPO}/releases/download/{tag}/{zip_name}"
for asset in data.get("assets", []):
a_name = asset.get("name", "")
if a_name.startswith("zysentinel-") and a_name.endswith(".zip"):
zip_name = a_name
zip_url = asset.get("browser_download_url", zip_url)
break
except Exception as e:
# Fallback to local plugin info.json if GitHub API is unreachable
info_path = "/www/server/panel/plugin/sentinel/info.json"
if os.path.exists(info_path):
try:
with open(info_path, "r", encoding="utf-8") as f:
local_info = json.load(f)
ver = local_info.get("versions", ver)
tag = f"v{ver}" if not ver.startswith("v") else ver
name = f"ZySentinel {tag}"
zip_name = f"zysentinel-{tag}.zip"
zip_url = f"https://github.com/{REPO}/releases/download/{tag}/{zip_name}"
except Exception:
pass
out_data = {
"tag_name": tag,
"version": ver,
"name": name,
"published_at": published_at,
"html_url": html_url,
"zip_url": zip_url,
"zip_name": zip_name,
"tarball_url": tarball_url,
"synced_at": datetime.datetime.now(datetime.timezone(datetime.timedelta(hours=7))).isoformat()
}
os.makedirs(os.path.dirname(TARGET_FILE), exist_ok=True)
dir_name = os.path.dirname(TARGET_FILE)
with tempfile.NamedTemporaryFile("w", dir=dir_name, delete=False, encoding="utf-8") as tf:
json.dump(out_data, tf, indent=2)
temp_name = tf.name
os.replace(temp_name, TARGET_FILE)
try:
import shutil
os.chmod(TARGET_FILE, 0o644)
# chown nobody:nogroup if possible
import pwd, grp
uid = pwd.getpwnam("nobody").pw_uid
gid = grp.getgrnam("nogroup").gr_gid
os.chown(TARGET_FILE, uid, gid)
except Exception:
pass
return out_data
if __name__ == "__main__":
result = sync_version()
print(f"Synced version {result['tag_name']} ({result['version']}) successfully.")