Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions gemini_web2api.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"log_requests": True,
"cookie_file": None,
"proxy": None,
"api_keys": [],
}

CONFIG = dict(DEFAULT_CONFIG)
Expand Down Expand Up @@ -427,6 +428,14 @@ def send_json(self, data, status=200):
self.end_headers()
self.wfile.write(body)

def _authorized(self):
keys = CONFIG.get("api_keys") or []
if not keys:
return True
auth = self.headers.get("Authorization", "")
key = auth[7:] if auth.startswith("Bearer ") else self.headers.get("x-api-key", "")
return key in keys

def do_OPTIONS(self):
self.send_response(204)
self.send_header("Access-Control-Allow-Origin", "*")
Expand All @@ -436,6 +445,9 @@ def do_OPTIONS(self):

def do_GET(self):
try:
if self.path.startswith("/v1/") and not self._authorized():
self.send_json({"error": {"message": "invalid api key"}}, 401)
return
if self.path == "/v1/models":
self.send_json({"object": "list", "data": [
{"id": n, "object": "model", "created": 1700000000,
Expand All @@ -456,6 +468,9 @@ def do_GET(self):

def do_POST(self):
try:
if self.path.startswith("/v1/") and not self._authorized():
self.send_json({"error": {"message": "invalid api key"}}, 401)
return
length = int(self.headers.get("Content-Length", 0))
body = self.rfile.read(length) if length else b""
if self.path == "/v1/chat/completions":
Expand Down