-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
324 lines (267 loc) · 10.3 KB
/
server.py
File metadata and controls
324 lines (267 loc) · 10.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#!/usr/bin/env python3
"""WebDAV Config Manager - config.yaml visualization tool"""
import os
import json
import shutil
import re
import secrets
import time
import hashlib
import hmac
import subprocess
from flask import Flask, request, jsonify, send_from_directory, send_file
import yaml
app = Flask(__name__, static_folder="static", static_url_path="")
# ─── Settings ───
SETTINGS_PATH = os.environ.get("SETTINGS_PATH", os.path.join(os.path.dirname(__file__), "settings.json"))
def load_settings():
default = {
"language": "en",
"web_port": 3080,
"config_path": "/app/webdav/config.yaml",
"service_name": "webdav",
"file_root": "/app/webdav/data",
"secret_key": ""
}
if os.path.exists(SETTINGS_PATH):
try:
with open(SETTINGS_PATH, "r", encoding="utf-8") as f:
s = json.load(f)
default.update(s)
except Exception:
pass
return default
def save_settings(settings):
with open(SETTINGS_PATH, "w", encoding="utf-8") as f:
json.dump(settings, f, indent=2, ensure_ascii=False)
settings = load_settings()
CONFIG_PATH = os.environ.get("CONFIG_PATH", settings.get("config_path", "/app/webdav/config.yaml"))
APP_PORT = int(os.environ.get("PORT", settings.get("web_port", 3080)))
SECRET_KEY = os.environ.get("SECRET_KEY", settings.get("secret_key") or secrets.token_hex(32))
app.secret_key = SECRET_KEY
SERVICE_NAME = settings.get("service_name", "webdav")
FILE_ROOT = os.environ.get("FILE_ROOT", settings.get("file_root", "/app/webdav/data"))
LANG = settings.get("language", "en")
# ─── YAML Tools ───
def load_config():
if not os.path.exists(CONFIG_PATH):
return None
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
return yaml.safe_load(f)
def save_config(config):
if os.path.exists(CONFIG_PATH):
shutil.copy2(CONFIG_PATH, CONFIG_PATH + ".bak")
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
yaml.dump(config, f, default_flow_style=False, allow_unicode=True,
sort_keys=False, width=120)
# ─── File Management ───
SIGN_EXPIRE = 3600
def safe_path(user_path):
"""Prevent path traversal attacks"""
target = os.path.normpath(os.path.join(FILE_ROOT, user_path.lstrip('/')))
if not target.startswith(os.path.normpath(FILE_ROOT)):
return None
return target
def make_sign(path, expire=None):
"""Generate file download signature"""
if expire is None:
expire = int(time.time()) + SIGN_EXPIRE
msg = f"{path}:{expire}"
sig = hmac.new(
SECRET_KEY.encode(), msg.encode(), hashlib.sha256
).hexdigest()
return f"/api/signed-download?path={path}&expire={expire}&sig={sig}"
# ─── Routes: Pages ───
@app.route("/")
def index():
return send_from_directory(".", "index.html")
# ─── Routes: Settings ───
@app.route("/api/settings", methods=["GET"])
def api_get_settings():
return jsonify(settings)
@app.route("/api/settings", methods=["POST"])
def api_save_settings():
global settings, CONFIG_PATH, SERVICE_NAME, FILE_ROOT, LANG
try:
data = request.get_json()
if not data or not isinstance(data, dict):
return jsonify({"error": "Invalid settings data"}), 400
settings.update(data)
save_settings(settings)
CONFIG_PATH = settings.get("config_path", CONFIG_PATH)
SERVICE_NAME = settings.get("service_name", SERVICE_NAME)
FILE_ROOT = settings.get("file_root", FILE_ROOT)
LANG = settings.get("language", LANG)
return jsonify({"success": True, "settings": settings})
except Exception as e:
return jsonify({"error": str(e)}), 500
# ─── Routes: Service ───
@app.route("/api/service/status")
def get_service_status():
try:
result = subprocess.run(
["systemctl", "status", f"{SERVICE_NAME}.service"],
capture_output=True, text=True, timeout=2
)
active_line = ""
for line in result.stdout.splitlines():
if "Active:" in line:
active_line = line
break
state = "unknown"
uptime_desc = ""
if "active (running)" in active_line:
state = "active"
match = re.search(r';\s*([^;]+)$', active_line)
if match:
uptime_desc = match.group(1).strip()
elif "inactive" in active_line:
state = "inactive"
return jsonify({
"state": state,
"uptime": uptime_desc
})
except Exception:
return jsonify({"state": "error", "uptime": "n/a"}), 200
# ─── Routes: Config ───
@app.route("/api/config")
def api_get_config():
try:
if not os.path.exists(CONFIG_PATH):
return jsonify({"error": "Config file not found", "path": CONFIG_PATH}), 404
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
raw = f.read()
config = yaml.safe_load(raw)
return jsonify({"config": config, "path": CONFIG_PATH, "raw": raw})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/config", methods=["POST"])
def api_save_config():
try:
config = request.get_json()
if not config or not isinstance(config, dict):
return jsonify({"error": "Invalid config data"}), 400
save_config(config)
return jsonify({"success": True, "message": "Config saved successfully"})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/raw")
def api_get_raw():
try:
with open(CONFIG_PATH, "r", encoding="utf-8") as f:
return f.read(), 200, {"Content-Type": "text/plain; charset=utf-8"}
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/raw", methods=["POST"])
def api_save_raw():
try:
data = request.get_json()
content = data.get("content", "")
yaml.safe_load(content)
if os.path.exists(CONFIG_PATH):
shutil.copy2(CONFIG_PATH, CONFIG_PATH + ".bak")
with open(CONFIG_PATH, "w", encoding="utf-8") as f:
f.write(content)
return jsonify({"success": True})
except yaml.YAMLError as e:
return jsonify({"error": f"Invalid YAML: {e}"}), 400
except Exception as e:
return jsonify({"error": str(e)}), 500
# ─── Routes: Files ───
@app.route("/api/files/sign", methods=["POST"])
def api_sign_files():
data = request.get_json()
paths = data.get("paths", [])
expire = int(time.time()) + SIGN_EXPIRE
result = {p: make_sign(p, expire) for p in paths}
return jsonify(result)
@app.route("/api/signed-download")
def api_signed_download():
path = request.args.get("path", "")
expire = request.args.get("expire", "")
sig = request.args.get("sig", "")
if not path or not expire or not sig:
return jsonify({"error": "Missing parameters"}), 400
try:
if int(expire) < time.time():
return jsonify({"error": "Link expired"}), 403
except ValueError:
return jsonify({"error": "Invalid expire"}), 400
msg = f"{path}:{expire}"
expected_sig = hmac.new(
SECRET_KEY.encode(), msg.encode(), hashlib.sha256
).hexdigest()
if not hmac.compare_digest(sig, expected_sig):
return jsonify({"error": "Invalid signature"}), 403
target = safe_path(path)
if not target or not os.path.isfile(target):
return jsonify({"error": "File not found"}), 404
filename = os.path.basename(target)
resp = send_file(target, mimetype="application/octet-stream")
resp.headers["Content-Disposition"] = 'attachment; filename="{}"'.format(
filename.replace("\\", "\\\\").replace('"', '\\"')
)
return resp
@app.route("/api/files")
def api_list_files():
try:
rel_path = request.args.get("path", "/")
target = safe_path(rel_path)
if not target:
return jsonify({"error": "Invalid path"}), 400
if not os.path.exists(target):
return jsonify({"error": "Path not found"}), 404
if not os.path.isdir(target):
return jsonify({"error": "Not a directory"}), 400
items = []
names = os.listdir(target)
for name in names[:5000]:
full = os.path.join(target, name)
try:
stat = os.stat(full)
items.append({
"name": name,
"is_dir": os.path.isdir(full),
"size": stat.st_size,
"mod_time": int(stat.st_mtime),
})
except (OSError, PermissionError):
pass
truncated = len(names) > 5000
return jsonify({"items": items, "path": rel_path, "truncated": truncated})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route("/api/files/download")
def api_download_file():
try:
rel_path = request.args.get("path", "")
target = safe_path(rel_path)
if not target or not os.path.isfile(target):
return jsonify({"error": "File not found"}), 404
return send_file(target, as_attachment=True, download_name=os.path.basename(target))
except Exception as e:
return jsonify({"error": str(e)}), 500
# ─── Routes: Restart ───
@app.route("/api/restart", methods=["POST"])
def api_restart():
try:
result = subprocess.run(
["systemctl", "restart", SERVICE_NAME],
capture_output=True, text=True, timeout=30
)
if result.returncode == 0:
return jsonify({"success": True, "message": f"{SERVICE_NAME} service restarted"})
else:
return jsonify({"success": False, "error": result.stderr.strip() or "Restart failed"}), 500
except subprocess.TimeoutExpired:
return jsonify({"success": False, "error": "Restart timed out"}), 500
except Exception as e:
return jsonify({"success": False, "error": str(e)}), 500
# ─── Main ───
if __name__ == "__main__":
print(f"WebDAV Config Manager running at http://localhost:{APP_PORT}")
print(f"Managing config: {CONFIG_PATH}")
print(f"Service name: {SERVICE_NAME}")
print(f"File root: {FILE_ROOT}")
print(f"Language: {LANG}")
app.run(host="0.0.0.0", port=APP_PORT, debug=False, threaded=True)