Skip to content

Commit 407fa87

Browse files
committed
fix: 修复漏洞
1 parent 1ba50b5 commit 407fa87

File tree

3 files changed

+26
-27
lines changed

3 files changed

+26
-27
lines changed

core/cluster.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ async def fetchFileList(self) -> None:
102102
io.BytesIO(await response.read())
103103
)
104104
decompressed_data = io.BytesIO(decompressor.read())
105-
for _ in range(self.read_long(decompressed_data)):
105+
for _ in range(self.readLong(decompressed_data)):
106106
self.filelist.files.append(
107107
FileInfo(
108108
self.readString(decompressed_data),
@@ -130,8 +130,7 @@ async def getConfiguration(self) -> None:
130130
self.configuration = AgentConfiguration(
131131
**(await response.json())["sync"]
132132
)
133-
# self.semaphore = asyncio.Semaphore(self.configuration.concurrency)
134-
self.semaphore = asyncio.Semaphore(64)
133+
self.semaphore = asyncio.Semaphore(self.configuration.concurrency)
135134
logger.tdebug("configuration.debug.get", sync=self.configuration)
136135

137136
async def getMissingFiles(self) -> FileList:
@@ -223,7 +222,6 @@ async def downloadFile(
223222
pbar.update(len(content))
224223
return
225224
except Exception as e:
226-
logger.debug(_)
227225
logger.terror(
228226
"cluster.error.download_file.retry",
229227
file=file.hash,
@@ -237,8 +235,9 @@ async def downloadFile(
237235
async def setupExpress(self, https: bool) -> None:
238236
logger.tinfo("cluster.info.router.creating")
239237
app = web.Application
240-
Router(https, app)
241-
238+
router = Router(https, app)
239+
router.init()
240+
242241

243242
async def init(self) -> None:
244243
await asyncio.gather(*(storage.init() for storage in self.storages))
@@ -258,4 +257,4 @@ def readLong(self, stream: io.BytesIO):
258257
return (n >> 1) ^ -(n & 1)
259258

260259
def readString(self, stream: io.BytesIO):
261-
return stream.read(self.read_long(stream)).decode()
260+
return stream.read(self.readLong(stream)).decode()

core/router.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@ async def wrapper(request):
2525
return wrapper
2626
return decorator
2727

28-
@route("/auth")
29-
async def _():
30-
pass
28+
def init(self) -> None:
29+
@self.route("/auth")
30+
async def _():
31+
pass
3132

32-
@route("/download/{hash}")
33-
async def _(self, request: web.Request, storages: List[Storage]) -> web.Response | web.StreamResponse:
34-
def check_sign(hash: str, secret: str, query: dict) -> bool:
35-
if not (s := query.get('s')) or not (e := query.get('e')): return False
36-
sign = base64.urlsafe_b64encode(hashlib.sha1(f"{secret}{hash}{e}".encode('utf-8')).digest()).decode('utf-8').rstrip('=')
37-
return sign == s and time.time() < int(e, 36)
38-
39-
hash = request.match_info.get('hash').lower()
40-
valid = check_sign(hash, self.secret, request.query)
41-
if not valid:
42-
return web.Response(text="invalid sign", status=403)
43-
response = web.StreamResponse(status=200)
44-
response.headers['x-bmclapi-hash'] = hash
45-
storage = random.randint(0, len(storages) - 1)
46-
data = storages[storage].express(hash, request, response)
47-
return response
33+
@self.route("/download/{hash}")
34+
async def _(self, request: web.Request, storages: List[Storage]) -> web.Response | web.StreamResponse:
35+
def check_sign(hash: str, secret: str, query: dict) -> bool:
36+
if not (s := query.get('s')) or not (e := query.get('e')): return False
37+
sign = base64.urlsafe_b64encode(hashlib.sha1(f"{secret}{hash}{e}".encode('utf-8')).digest()).decode('utf-8').rstrip('=')
38+
return sign == s and time.time() < int(e, 36)
39+
40+
hash = request.match_info.get('hash').lower()
41+
valid = check_sign(hash, self.secret, request.query)
42+
if not valid:
43+
return web.Response(text="invalid sign", status=403)
44+
response = web.StreamResponse(status=200)
45+
response.headers['x-bmclapi-hash'] = hash
46+
data = await random.choice(storages).express(hash, request, response)
47+
return response
4848

4949

core/storages/local.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def writeFile(
3939
try:
4040
async with aiofiles.open(file_path, "wb") as f:
4141
await f.write(content.getbuffer())
42-
42+
await asyncio.sleep(0.1)
4343
if os.path.getsize(file_path) == file.size:
4444
return True
4545
else:

0 commit comments

Comments
 (0)