返回提交历史
Modified
g4f/api/__init__.py
+14
-36
XFEstudio/gpt4free
Show logs
064f4678
代码差异
1 个文件
+14
-36
@@ -562,7 +562,7 @@ class Api:
562
562
"""Return ``True`` when *path* must present a G4F API key."""
563
563
return (
564
564
path.startswith("/v1/")
565
or (path.startswith("/api/") and path != "/api/logs")
565
or (path.startswith("/api/"))
566
566
or (path.startswith("/pa/") and not demo)
567
567
or (demo and path in ["/backend-api/v2/upload_cookies"])
568
568
)
@@ -572,9 +572,7 @@ class Api:
572
572
user = None
573
573
if (
574
574
request.method != "OPTIONS"
575
and AppConfig.g4f_api_key is not None
576
or AppConfig.demo
577
):
575
and AppConfig.g4f_api_key is not None ):
578
576
try:
579
577
user_g4f_api_key = await self.get_g4f_api_key(request)
580
578
except HTTPException:
@@ -583,11 +581,6 @@ class Api:
583
581
)
584
582
if user_g4f_api_key:
585
583
user_g4f_api_key = user_g4f_api_key.split()
586
country = request.headers.get("Cf-Ipcountry", "")
587
if AppConfig.demo and user is None:
588
ip = request.headers.get("X-Forwarded-For", "")[:4].strip(":.")
589
user = request.headers.get("x-user", ip)
590
user = f"{country}:{user}" if country else user
591
584
if (
592
585
AppConfig.g4f_api_key is None
593
586
or not user_g4f_api_key
@@ -640,34 +633,19 @@ class Api:
640
633
)
641
634
elif user is None:
642
635
user = "admin"
643
path = request.url.path
644
if _requires_api_key(path, AppConfig.demo):
645
if request.method != "OPTIONS" and not path.endswith("/models"):
646
if not user_g4f_api_key:
647
return ErrorResponse.from_message(
648
"G4F API key required", HTTP_401_UNAUTHORIZED
649
)
650
if AppConfig.g4f_api_key is None and user is None:
651
return ErrorResponse.from_message(
652
"Invalid G4F API key", HTTP_403_FORBIDDEN
653
)
654
elif (
655
not path.startswith("/images/")
656
and not path.startswith("/media/")
657
):
658
if user_g4f_api_key:
636
if user_g4f_api_key:
637
if user is None:
638
return ErrorResponse.from_message(
639
"Invalid G4F API key", HTTP_403_FORBIDDEN
640
)
641
try:
642
new_user = await self.get_username(request)
659
643
if user is None:
660
return ErrorResponse.from_message(
661
"Invalid G4F API key", HTTP_403_FORBIDDEN
662
)
663
try:
664
new_user = await self.get_username(request)
665
if user is None:
666
user = new_user
667
except HTTPException as e:
668
return ErrorResponse.from_message(
669
e.detail, e.status_code, e.headers
670
)
644
user = new_user
645
except HTTPException as e:
646
return ErrorResponse.from_message(
647
e.detail, e.status_code, e.headers
648
)
671
649
request = update_headers(request, user)
672
650
response = await call_next(request)
673
651
return response