Update main.py
Browse files
main.py
CHANGED
|
@@ -105,11 +105,35 @@ def get_cached_1psidts_path(psid: str) -> str:
|
|
| 105 |
return os.path.join(GEMINI_COOKIE_PATH, f".cached_1psidts_{psid}.txt")
|
| 106 |
|
| 107 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
def load_cached_1psidts(psid: str) -> str:
|
| 109 |
"""Load a cached rotated 1PSIDTS value for the given 1PSID."""
|
| 110 |
cached_file_path = get_cached_1psidts_path(psid)
|
| 111 |
if not cached_file_path:
|
| 112 |
-
return ""
|
| 113 |
|
| 114 |
if os.path.exists(cached_file_path):
|
| 115 |
try:
|
|
@@ -119,7 +143,7 @@ def load_cached_1psidts(psid: str) -> str:
|
|
| 119 |
except Exception as e:
|
| 120 |
logger.warning(f"Error reading cache file {cached_file_path}: {e}")
|
| 121 |
|
| 122 |
-
return ""
|
| 123 |
|
| 124 |
|
| 125 |
def get_cookie_value(cookies, name: str) -> str:
|
|
@@ -159,7 +183,8 @@ TEMPORARY_CHAT = os.environ.get("TEMPORARY_CHAT", "false").lower() == "true"
|
|
| 159 |
AUTO_DELETE_CHAT = os.environ.get("AUTO_DELETE_CHAT", "true").lower() == "true" and not TEMPORARY_CHAT
|
| 160 |
PUBLIC_BASE_URL = os.environ.get("PUBLIC_BASE_URL", "").rstrip("/")
|
| 161 |
SECRET_FILE_PATH = os.path.join(os.path.dirname(__file__), "secrets", "proxy_secret")
|
| 162 |
-
|
|
|
|
| 163 |
SESSION_VALIDATION_PROMPT = "Reply with exactly OK."
|
| 164 |
AUTH_FAILURE_TEXT_PATTERNS = (
|
| 165 |
"are you signed in",
|
|
@@ -170,7 +195,7 @@ AUTH_FAILURE_TEXT_PATTERNS = (
|
|
| 170 |
)
|
| 171 |
DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0"
|
| 172 |
|
| 173 |
-
os.environ
|
| 174 |
|
| 175 |
|
| 176 |
async def background_delete_chat(client: GeminiClient, cid: str):
|
|
@@ -727,7 +752,12 @@ async def get_gemini_client():
|
|
| 727 |
logger.info("Initializing Gemini client using %s credentials", source)
|
| 728 |
|
| 729 |
tmp_client = GeminiClient(psid, psidts)
|
| 730 |
-
await tmp_client.init(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 731 |
await validate_gemini_client_session(tmp_client, source)
|
| 732 |
|
| 733 |
gemini_client = tmp_client
|
|
|
|
| 105 |
return os.path.join(GEMINI_COOKIE_PATH, f".cached_1psidts_{psid}.txt")
|
| 106 |
|
| 107 |
|
| 108 |
+
def get_cookie_cache_json_path(psid: str) -> str:
|
| 109 |
+
"""Return the gemini-webapi cache path for persisted cookies."""
|
| 110 |
+
if not psid:
|
| 111 |
+
return ""
|
| 112 |
+
return os.path.join(GEMINI_COOKIE_PATH, f".cached_cookies_{psid}.json")
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
def load_cached_cookie_value(psid: str, name: str) -> str:
|
| 116 |
+
"""Load a cookie value from gemini-webapi's persisted cookie cache."""
|
| 117 |
+
cached_file_path = get_cookie_cache_json_path(psid)
|
| 118 |
+
if not cached_file_path or not os.path.exists(cached_file_path):
|
| 119 |
+
return ""
|
| 120 |
+
|
| 121 |
+
try:
|
| 122 |
+
cookies = json.loads(Path(cached_file_path).read_text())
|
| 123 |
+
for cookie in cookies:
|
| 124 |
+
if cookie.get("name") == name:
|
| 125 |
+
return cookie.get("value", "")
|
| 126 |
+
except Exception as e:
|
| 127 |
+
logger.warning(f"Error reading cookie cache file {cached_file_path}: {e}")
|
| 128 |
+
|
| 129 |
+
return ""
|
| 130 |
+
|
| 131 |
+
|
| 132 |
def load_cached_1psidts(psid: str) -> str:
|
| 133 |
"""Load a cached rotated 1PSIDTS value for the given 1PSID."""
|
| 134 |
cached_file_path = get_cached_1psidts_path(psid)
|
| 135 |
if not cached_file_path:
|
| 136 |
+
return load_cached_cookie_value(psid, "__Secure-1PSIDTS")
|
| 137 |
|
| 138 |
if os.path.exists(cached_file_path):
|
| 139 |
try:
|
|
|
|
| 143 |
except Exception as e:
|
| 144 |
logger.warning(f"Error reading cache file {cached_file_path}: {e}")
|
| 145 |
|
| 146 |
+
return load_cached_cookie_value(psid, "__Secure-1PSIDTS")
|
| 147 |
|
| 148 |
|
| 149 |
def get_cookie_value(cookies, name: str) -> str:
|
|
|
|
| 183 |
AUTO_DELETE_CHAT = os.environ.get("AUTO_DELETE_CHAT", "true").lower() == "true" and not TEMPORARY_CHAT
|
| 184 |
PUBLIC_BASE_URL = os.environ.get("PUBLIC_BASE_URL", "").rstrip("/")
|
| 185 |
SECRET_FILE_PATH = os.path.join(os.path.dirname(__file__), "secrets", "proxy_secret")
|
| 186 |
+
DEFAULT_GEMINI_COOKIE_PATH = "/data/gemini_webapi" if os.path.isdir("/data") else os.path.join(os.path.dirname(__file__), "secrets")
|
| 187 |
+
GEMINI_COOKIE_PATH = os.environ.get("GEMINI_COOKIE_PATH", DEFAULT_GEMINI_COOKIE_PATH)
|
| 188 |
SESSION_VALIDATION_PROMPT = "Reply with exactly OK."
|
| 189 |
AUTH_FAILURE_TEXT_PATTERNS = (
|
| 190 |
"are you signed in",
|
|
|
|
| 195 |
)
|
| 196 |
DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0"
|
| 197 |
|
| 198 |
+
os.environ["GEMINI_COOKIE_PATH"] = GEMINI_COOKIE_PATH
|
| 199 |
|
| 200 |
|
| 201 |
async def background_delete_chat(client: GeminiClient, cid: str):
|
|
|
|
| 752 |
logger.info("Initializing Gemini client using %s credentials", source)
|
| 753 |
|
| 754 |
tmp_client = GeminiClient(psid, psidts)
|
| 755 |
+
await tmp_client.init(
|
| 756 |
+
timeout=300,
|
| 757 |
+
auto_refresh=True,
|
| 758 |
+
refresh_interval=600,
|
| 759 |
+
auto_close=False,
|
| 760 |
+
)
|
| 761 |
await validate_gemini_client_session(tmp_client, source)
|
| 762 |
|
| 763 |
gemini_client = tmp_client
|