nexus3rd commited on
Commit
e6e948c
·
verified ·
1 Parent(s): d6107ac

Upload 4 files

Browse files
Files changed (4) hide show
  1. .dockerignore +7 -0
  2. Dockerfile +25 -11
  3. app.py +114 -0
  4. requirements.txt +3 -36
.dockerignore ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ __pycache__
2
+ *.pyc
3
+ *.pyo
4
+ *.pyd
5
+ .git
6
+ .gitignore
7
+ .env
Dockerfile CHANGED
@@ -1,11 +1,25 @@
1
- FROM arakurumi/bot:mltb
2
-
3
- WORKDIR /app
4
-
5
- RUN chmod -R 777 /app
6
-
7
- COPY . .
8
-
9
- RUN pip install --break-system-packages --no-cache-dir --requirement requirements.txt
10
-
11
- CMD ["bash", "start.sh", "python main.py"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.12-slim
2
+
3
+ ENV PYTHONDONTWRITEBYTECODE=1
4
+ ENV PYTHONUNBUFFERED=1
5
+ ENV PORT=8080
6
+ ENV HOST=0.0.0.0
7
+ ENV RCLONE_CONFIG=/app/rclone.conf
8
+
9
+ WORKDIR /app
10
+
11
+ RUN apt-get update && apt-get install -y --no-install-recommends \
12
+ curl ca-certificates unzip \
13
+ && rm -rf /var/lib/apt/lists/*
14
+
15
+ RUN curl -fsSL https://rclone.org/install.sh | bash
16
+
17
+ COPY requirements.txt .
18
+ RUN pip install --no-cache-dir -r requirements.txt
19
+
20
+ COPY app.py .
21
+ COPY rclone.conf .
22
+
23
+ EXPOSE 8080
24
+
25
+ CMD ["sh", "-c", "uvicorn app:app --host 0.0.0.0 --port ${PORT}"]
app.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ import json
4
+ import asyncio
5
+ from pathlib import Path
6
+ from fastapi import FastAPI, HTTPException, Header
7
+
8
+ RCLONE_CONFIG = os.getenv("RCLONE_CONFIG", "https://gist.githubusercontent.com/genstava789/e7fa4e34e126bfc257b21db4f8c0f3ba/raw/rclone.conf")
9
+ HOST = os.getenv("HOST", "0.0.0.0")
10
+ PORT = int(os.getenv("PORT", "8080"))
11
+ API_TOKEN = os.getenv("API_TOKEN", "admin")
12
+
13
+ app = FastAPI(title="rclone index backend")
14
+
15
+
16
+ def check_auth(token: str | None):
17
+ if API_TOKEN and token != API_TOKEN:
18
+ raise HTTPException(status_code=401, detail="unauthorized")
19
+
20
+
21
+ def parse_remotes(conf_path: str) -> list[str]:
22
+ path = Path(conf_path)
23
+ if not path.exists():
24
+ return []
25
+
26
+ text = path.read_text(encoding="utf-8", errors="ignore")
27
+ out = []
28
+
29
+ for line in text.splitlines():
30
+ line = line.strip()
31
+ if line.startswith("[") and line.endswith("]"):
32
+ name = line[1:-1].strip()
33
+ if name:
34
+ out.append(name if name.endswith(":") else name + ":")
35
+
36
+ return out
37
+
38
+
39
+ def safe_remote(fs: str) -> bool:
40
+ return bool(re.fullmatch(r"[A-Za-z0-9_.\-]+:", fs))
41
+
42
+
43
+ def safe_dir(remote: str) -> bool:
44
+ if ".." in remote or "\x00" in remote:
45
+ return False
46
+ return True
47
+
48
+
49
+ async def run_rclone(*args: str):
50
+ cmd = ["rclone", *args, "--config", RCLONE_CONFIG, "--use-json-log=false"]
51
+ proc = await asyncio.create_subprocess_exec(
52
+ *cmd,
53
+ stdout=asyncio.subprocess.PIPE,
54
+ stderr=asyncio.subprocess.PIPE
55
+ )
56
+ stdout, stderr = await proc.communicate()
57
+
58
+ out = stdout.decode("utf-8", errors="ignore").strip()
59
+ err = stderr.decode("utf-8", errors="ignore").strip()
60
+
61
+ if proc.returncode != 0:
62
+ raise HTTPException(status_code=500, detail=err or "rclone error")
63
+
64
+ return out
65
+
66
+
67
+ @app.get("/health")
68
+ async def health():
69
+ return {"ok": True}
70
+
71
+
72
+ @app.get("/api/remotes")
73
+ async def api_remotes(x_api_token: str | None = Header(default=None)):
74
+ check_auth(x_api_token)
75
+ return {"ok": True, "remotes": parse_remotes(RCLONE_CONFIG)}
76
+
77
+
78
+ @app.get("/api/list")
79
+ async def api_list(
80
+ fs: str,
81
+ dir: str = "",
82
+ x_api_token: str | None = Header(default=None)
83
+ ):
84
+ check_auth(x_api_token)
85
+
86
+ if not safe_remote(fs):
87
+ raise HTTPException(status_code=400, detail="invalid fs")
88
+
89
+ if not safe_dir(dir):
90
+ raise HTTPException(status_code=400, detail="invalid dir")
91
+
92
+ target = fs + dir
93
+ out = await run_rclone("lsjson", target)
94
+
95
+ try:
96
+ data = json.loads(out)
97
+ except Exception:
98
+ raise HTTPException(status_code=500, detail="invalid rclone response")
99
+
100
+ items = []
101
+ for item in data:
102
+ items.append({
103
+ "name": item.get("Name", ""),
104
+ "path": item.get("Path", ""),
105
+ "size": item.get("Size", 0),
106
+ "isDir": item.get("IsDir", False)
107
+ })
108
+
109
+ return {
110
+ "ok": True,
111
+ "fs": fs,
112
+ "dir": dir,
113
+ "list": items
114
+ }
requirements.txt CHANGED
@@ -1,36 +1,3 @@
1
- aiofiles
2
- aioshutil
3
- anytree
4
- apscheduler
5
- aria2p
6
- beautifulsoup4
7
- cloudscraper
8
- feedparser
9
- flask
10
- gevent
11
- google-api-python-client
12
- google-auth-httplib2
13
- google-auth-oauthlib
14
- gunicorn
15
- httpx
16
- lxml
17
- motor
18
- natsort
19
- pillow
20
- psutil
21
- pycryptodome
22
- pycurl
23
- pymongo
24
- pyrofork
25
- python-dotenv
26
- python-magic
27
- qbittorrent-api
28
- requests
29
- speedtest-cli
30
- telegraph
31
- tenacity
32
- TgCrypto-pyrofork
33
- urllib3
34
- uvloop
35
- xattr
36
- yt-dlp
 
1
+ fastapi
2
+ uvicorn
3
+ python-dotenv