diff --git a/webdav_simulator.amd64 b/webdav_simulator.amd64 index 333f19f..9beb2b7 100644 Binary files a/webdav_simulator.amd64 and b/webdav_simulator.amd64 differ diff --git a/webdav_simulator.exe b/webdav_simulator.exe index 707968d..3822c71 100644 Binary files a/webdav_simulator.exe and b/webdav_simulator.exe differ diff --git a/webdav_simulator.py b/webdav_simulator.py index 695525f..afcc30c 100644 --- a/webdav_simulator.py +++ b/webdav_simulator.py @@ -31,6 +31,8 @@ import json import traceback import time +from functools import lru_cache + def convert_path(webdav_path: str) -> str: return f"/{webdav_path.replace('/dav','',1)}" @@ -97,6 +99,28 @@ class VirtualFSProvider(DAVProvider): self.root_node = root_node self.alist_config = alist_config self.sort_reverse = sort_reverse + self.serverlist=list() + if self.alist_config["config"] == "": + if self.alist_config["api_url"] != "": + api_url = self.alist_config["api_url"] + if api_url.endswith("/"): + api_url = api_url[:-1] + self.serverlist.append({"api_url":api_url,"token":self.alist_config["token"],"prefix":self.alist_config["prefix"]}) + else: + # 解析配置 + config_list = self.alist_config["config"].split("#") + for config in config_list: + config = config.split(",") + config[0] = config[0].strip() + if config[0] != "": + configlen=len(config) + for i in range(configlen,3): + config.append("") + if len(config) >= 3: + api_url = config[0] + if api_url.endswith("/"): + api_url = api_url[:-1] + self.serverlist.append({"api_url":api_url,"token":config[1],"prefix":config[2]}) def get_resource_inst(self, path, environ): if path == '/': @@ -110,6 +134,53 @@ class VirtualFSProvider(DAVProvider): return None return VirtualResource(path, current, environ, self.alist_config, sort_reverse=self.sort_reverse) + @lru_cache(maxsize=1024, typed=True) + def get_redirect_url(self, path): + for server in self.serverlist: + print("server:",server) + pathsegs=path.split('/') + pathlist=set() + pathlist.add(path) + if "我的115分享" in pathsegs[1]: + pathlist.add(path.replace("🏷️我的115分享", "🏷️ 我的115分享")) + pathlist.add(path.replace("🏷️ 我的115分享", "🏷️我的115分享")) + for tmppath in pathlist: + try: + headers = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36", + } + if server["token"] != "": + headers["Authorization"] = server["token"] + alist_path = os.path.join(server['prefix'],tmppath) + params = {"path": alist_path} + # print("params:",params) + resp = requests.get( + server["api_url"]+"/api/fs/get", + headers=headers, + params=params, + stream=True + ) + resp.raise_for_status() + body = resp.content.decode('utf-8') + print(f"query alist_api:{server['api_url']}, headers:{headers}, params:{params}, respcode:{resp.status_code}, respbody:{body[:4096]}") + # 解析 JSON 数据 + data_dict = json.loads(body) + + # 获取 raw_url + raw_url = data_dict['data']['raw_url'] + if raw_url: + # print(f"raw_url: {raw_url}") + # send_redirect(environ, start_response, raw_url) + # return [b"Redirecting..."] + return raw_url + except: + traceback.print_exc() + time.sleep(1) + pass + time.sleep(1) + return "" + + def custom_request_handler(self, environ, start_response, default_handler): """Optionally implement custom request handling. @@ -122,70 +193,11 @@ class VirtualFSProvider(DAVProvider): requestmethod = environ["REQUEST_METHOD"] path = environ["PATH_INFO"] if requestmethod == "GET" and not path.endswith('/') and len(path.split('/'))>2: - serverlist=list() - if self.alist_config["config"] == "": - if self.alist_config["api_url"] != "": - api_url = self.alist_config["api_url"] - if api_url.endswith("/"): - api_url = api_url[:-1] - serverlist.append({"api_url":api_url,"token":self.alist_config["token"],"prefix":self.alist_config["prefix"]}) - else: - # 解析配置 - config_list = self.alist_config["config"].split("#") - for config in config_list: - config = config.split(",") - config[0] = config[0].strip() - if config[0] != "": - configlen=len(config) - for i in range(configlen,3): - config.append("") - if len(config) >= 3: - api_url = config[0] - if api_url.endswith("/"): - api_url = api_url[:-1] - serverlist.append({"api_url":api_url,"token":config[1],"prefix":config[2]}) - - for server in serverlist: - print("server:",server) - pathsegs=path.split('/') - pathlist=set() - pathlist.add(path) - if "我的115分享" in pathsegs[1]: - pathlist.add(path.replace("🏷️我的115分享", "🏷️ 我的115分享")) - pathlist.add(path.replace("🏷️ 我的115分享", "🏷️我的115分享")) - for tmppath in pathlist: - try: - headers = { - "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36", - } - if server["token"] != "": - headers["Authorization"] = server["token"] - alist_path = os.path.join(server['prefix'],tmppath) - params = {"path": alist_path} - # print("params:",params) - resp = requests.get( - server["api_url"]+"/api/fs/get", - headers=headers, - params=params, - stream=True - ) - resp.raise_for_status() - body = resp.content.decode('utf-8') - print(f"query alist_api:{server['api_url']}, headers:{headers}, params:{params}, respcode:{resp.status_code}, respbody:{body[:4096]}") - # 解析 JSON 数据 - data_dict = json.loads(body) - - # 获取 raw_url - raw_url = data_dict['data']['raw_url'] - if raw_url: - print(f"raw_url: {raw_url}") - send_redirect(environ, start_response, raw_url) - return [b"Redirecting..."] - except: - traceback.print_exc() - time.sleep(3) - pass - time.sleep(3) + redirect_url = self.get_redirect_url(path) + if redirect_url!="": + print(f"redirect_urL:{redirect_url}") + send_redirect(environ, start_response, redirect_url) + return [b"Redirecting..."] return default_handler(environ, start_response)