55from core .storages import getStorages
66from core .classes import FileInfo , FileList , AgentConfiguration
77from core .router import Router
8+ from core .client import WebSocketClient
89from core .i18n import locale
910from aiohttp import web
1011from tqdm import tqdm
1415import asyncio
1516import hmac
1617import hashlib
17- import socketio
18+ import ssl
1819import humanize
1920import io
2021
@@ -82,6 +83,9 @@ def __init__(self) -> None:
8283 self .storages = getStorages ()
8384 self .configuration = None
8485 self .semaphore = None
86+ self .socket = None
87+ self .router = None
88+ self .server = None
8589 self .failed_filelist = FileList (files = [])
8690
8791 async def fetchFileList (self ) -> None :
@@ -214,7 +218,9 @@ async def downloadFile(
214218 content = await response .read ()
215219 results = await asyncio .gather (
216220 * (
217- storage .writeFile (file , io .BytesIO (content ), delay , retry )
221+ storage .writeFile (
222+ file , io .BytesIO (content ), delay , retry
223+ )
218224 for storage in self .storages
219225 )
220226 )
@@ -226,18 +232,37 @@ async def downloadFile(
226232 "cluster.error.download_file.retry" ,
227233 file = file .hash ,
228234 e = e ,
229- retry = delay
235+ retry = delay ,
230236 )
231237 await asyncio .sleep (delay )
232238 logger .terror ("cluster.error.download_file.failed" , file = file .hash )
233239 self .failed_filelist .files .append (file )
234240
235- async def setupExpress (self , https : bool ) -> None :
241+ async def setupExpress (self , https : bool , port : int ) -> None :
236242 logger .tinfo ("cluster.info.router.creating" )
237- app = web .Application
238- router = Router (https , app )
239- router .init ()
243+ app = web .Application ()
244+ try :
245+ self .router = Router (app , self .storages )
246+ self .router .init ()
247+ ssl_context = None
248+ # if https:
249+ # ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
250+ # ssl_context.load_cert_chain(
251+ # certfile=Config.get("advanced.paths.cert"),
252+ # keyfile=Config.get("advanced.paths.key")
253+ # )
254+ self .server = web .AppRunner (app )
255+ logger .tsuccess ("cluster.success.router.created" )
256+ await self .server .setup ()
257+ site = web .TCPSite (self .server , "0.0.0.0" , port , ssl_context = ssl_context )
258+ await site .start ()
259+ logger .tsuccess ("cluster.success.site.start" )
260+ except Exception as e :
261+ logger .terror ("cluster.error.router.exception" , e = e )
240262
263+ async def connect (self ) -> None :
264+ self .socket = WebSocketClient (self .token .token )
265+ await self .socket .connect ()
241266
242267 async def init (self ) -> None :
243268 await asyncio .gather (* (storage .init () for storage in self .storages ))
0 commit comments