|
|
@@ -28,6 +28,10 @@ class SysProxyResponse(BaseModel):
|
|
|
class SubUrlPost(BaseModel):
|
|
|
sub_url: str
|
|
|
|
|
|
+class ProxyPoolResponse(BaseModel):
|
|
|
+ proxies: List[str]
|
|
|
+ cached: bool
|
|
|
+
|
|
|
from fastapi.requests import Request
|
|
|
|
|
|
@router.get("/sys")
|
|
|
@@ -81,7 +85,7 @@ async def get_all_proxy_response(use_cache: bool = True) -> List[ProxyResponse]:
|
|
|
ret = []
|
|
|
tasks = []
|
|
|
for port,porxy_model in sub_mgr.sub.proxies.items():
|
|
|
- tasks.append(get_proxy_response(port, use_cache))
|
|
|
+ tasks.append(get_proxy_response(port))
|
|
|
ret = await asyncio.gather(*tasks)
|
|
|
return ret
|
|
|
|
|
|
@@ -100,6 +104,24 @@ async def ping_proxies() -> Dict[str, int]:
|
|
|
logger.info(f"use cache: {cache_key}")
|
|
|
return cache[cache_key]
|
|
|
|
|
|
+@router.get("/proxies-pool")
|
|
|
+async def get_proxies_pool(force_refresh: bool = False):
|
|
|
+ global cache
|
|
|
+ cache_key = "proxy_pool"
|
|
|
+
|
|
|
+ if not force_refresh and cache_key in cache:
|
|
|
+ return ProxyPoolResponse(proxies=cache[cache_key], cached=True)
|
|
|
+
|
|
|
+ proxies = []
|
|
|
+ all_proxies = await get_all_proxy_response()
|
|
|
+ for p in all_proxies:
|
|
|
+ if p.reachable: # 健康检查
|
|
|
+ proxies.append(f"127.0.0.1:{p.port}")
|
|
|
+
|
|
|
+ # 更新缓存并返回
|
|
|
+ cache[cache_key] = proxies
|
|
|
+ return ProxyPoolResponse(proxies=proxies, cached=False)
|
|
|
+
|
|
|
@router.get("/proxies/{port}")
|
|
|
@router.get("/proxies")
|
|
|
async def get_proxies(port: int = None):
|
|
|
@@ -142,6 +164,10 @@ async def delete_proxy(port: int):
|
|
|
if port in sub_mgr.sub.proxies:
|
|
|
del sub_mgr.sub.proxies[port]
|
|
|
sub_mgr.save_config()
|
|
|
+
|
|
|
+ # 清除代理池缓存
|
|
|
+ if 'proxy_pool' in cache:
|
|
|
+ del cache['proxy_pool']
|
|
|
|
|
|
return await get_all_proxy_response()
|
|
|
except Exception as e:
|
|
|
@@ -161,28 +187,26 @@ async def create_proxy(request:ProxyPost):
|
|
|
porxy_port = request.port
|
|
|
proxy_mgr = sub_mgr.get_proxy_manager(porxy_port)
|
|
|
if proxy_mgr and proxy_mgr.running:
|
|
|
- # return {'err': 0, "msg": f"已开启,跳过 {porxy_port} ", "data": await get_proxy_response(porxy_port)}
|
|
|
return ProxyPostResponse(err=0, msg=f"已开启,跳过 {porxy_port} ", data=await get_proxy_response(porxy_port))
|
|
|
porxy_port_is_using = await port_is_using(porxy_port)
|
|
|
controler_port = request.port + 1
|
|
|
if porxy_port_is_using:
|
|
|
- # return ProxyPostResponse(err=1, msg=f"porxy_port={porxy_port} 端口已被占用")
|
|
|
raise HTTPException(status_code=400, detail=ProxyPostResponse(err=1, msg=f"porxy_port={porxy_port} 端口已被占用"))
|
|
|
if await port_is_using(controler_port):
|
|
|
- # return {"err": 1, "msg": f"controler_port={controler_port} 端口已被占用"}
|
|
|
- # return ProxyPostResponse(err=1, msg=f"controler_port={controler_port} 端口已被占用")
|
|
|
raise HTTPException(status_code=400, detail=ProxyPostResponse(err=1, msg=f"controler_port={controler_port} 端口已被占用"))
|
|
|
else:
|
|
|
- # return ProxyPostResponse(err=1, msg="port 或 auto 必须有一个")
|
|
|
raise HTTPException(status_code=400, detail=ProxyPostResponse(err=1, msg="port 或 auto 必须有一个"))
|
|
|
await sub_mgr.create_custom_config(porxy_port, controler_port)
|
|
|
await sub_mgr.start_proxy(porxy_port)
|
|
|
await auto_select_proxy(porxy_port)
|
|
|
- # return {"err": 0, "msg": "ok", "data": await get_proxy_response(porxy_port)}
|
|
|
res = ProxyPostResponse(err=0, msg="ok", data=await get_proxy_response(porxy_port))
|
|
|
logger.info(f"{res}")
|
|
|
+
|
|
|
+ # 清除代理池缓存
|
|
|
+ if 'proxy_pool' in cache:
|
|
|
+ del cache['proxy_pool']
|
|
|
+
|
|
|
return res
|
|
|
- # return ProxyPostResponse(err=1, msg="proxy_lock error", data=sub_mgr.sub)
|
|
|
return HTTPException(status_code=500, detail=ProxyPostResponse(err=1, msg="proxy_lock error", data=sub_mgr.sub))
|
|
|
|
|
|
@router.post("/proxies/{port}/stop")
|
|
|
@@ -192,7 +216,13 @@ async def stop_proxy(port: int):
|
|
|
if not proxy_mgr:
|
|
|
raise HTTPException(status_code=404, detail=f"Proxy with port {port} not found")
|
|
|
await sub_mgr.stop_proxy(port)
|
|
|
+
|
|
|
+ # 清除代理池缓存
|
|
|
+ if 'proxy_pool' in cache:
|
|
|
+ del cache['proxy_pool']
|
|
|
+
|
|
|
return await get_proxy_response(port)
|
|
|
+
|
|
|
@router.get("/subs")
|
|
|
async def get_subscriptions():
|
|
|
global sub_mgr
|
|
|
@@ -207,7 +237,6 @@ async def create_subscription(request: SubUrlPost):
|
|
|
except Exception as e:
|
|
|
return {"err": 1, "msg": str(e)}
|
|
|
|
|
|
-
|
|
|
class StartupRequest(BaseModel):
|
|
|
auto_start: bool
|
|
|
|
|
|
@@ -215,6 +244,11 @@ class StartupRequest(BaseModel):
|
|
|
async def startup(request: StartupRequest):
|
|
|
global sub_mgr,config
|
|
|
sub_mgr.save_startup(request.auto_start)
|
|
|
+
|
|
|
+ # 清除代理池缓存
|
|
|
+ if 'proxy_pool' in cache:
|
|
|
+ del cache['proxy_pool']
|
|
|
+
|
|
|
return {"err": 0, "msg": "ok", "data": config}
|
|
|
|
|
|
def main():
|
|
|
@@ -226,6 +260,5 @@ def main():
|
|
|
else:
|
|
|
print("代理未启用")
|
|
|
|
|
|
-
|
|
|
if __name__ == "__main__":
|
|
|
main()
|