feat: multi-account load balancing, mandatory Bearer auth, Linux/systemd deployment guide#22
Open
GoNNoTDa wants to merge 1 commit into
Open
feat: multi-account load balancing, mandatory Bearer auth, Linux/systemd deployment guide#22GoNNoTDa wants to merge 1 commit into
GoNNoTDa wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds two major features and a comprehensive deployment guide:
api_keysto be configured and rejects all requests without a valid tokenChanges
New file:
gemini_web2api/accounts.pyIntroduces two classes:
Account— wraps a single Google session (cookie file,auth_user, per-account proxy, XSRF token). Cookie files are reloaded automatically on change via mtime caching, so you can rotate cookies without restarting the server.AccountPool— thread-safe pool with round-robin selection. Each call togenerate()orgenerate_stream()picks the next account in sequence.Modified:
gemini_web2api/gemini.pygenerate()andgenerate_stream()now callPOOL.next()to pick an account per request, instead of readingCONFIGglobals directly. Each account carries its own headers, cookie, SAPISIDHASH, proxy, and URL prefix.Modified:
gemini_web2api/server.py_authorized()now returnsFalse(deny) whenapi_keysis empty, instead ofTrue(allow). An unconfigured server no longer accepts all traffic._require_auth()helper — sends a401with a descriptive error and returnsTrueso handlers canreturnimmediately.GET /v1/models,GET /v1beta/models,GET /, and allPOSTroutes.GET /) now reports account count and names.Modified:
gemini_web2api/config.pyhostchanged from0.0.0.0to127.0.0.1(localhost-only by default).accounts: []key with inline documentation.api_keysdocumented as required.Modified:
gemini_web2api/__main__.pyPOOL.load_from_config(CONFIG)on startup.api_keysis empty.--hostCLI argument.Modified:
gemini_web2api.py(monolithic file)All of the above changes mirrored into the standalone single-file version, so users who run
python gemini_web2api.pydirectly get the same behaviour.Modified:
config.example.jsonUpdated to show the multi-account
accountslist format, with comments explaining each field. Legacy flat fields documented as a fallback for single-account setups.Modified:
README.mdRewritten to document all new features and add a full Linux server deployment guide. See details below.
README additions
auth_user, and step-by-step cookie export instructions.geminisystem userscpor git clone600permissions on cookie files/etc/systemd/system/gemini-web2api.servicewith security hardening directives (NoNewPrivileges,ProtectSystem=strict,PrivateTmp)systemctl enable+start+ status verificationjournalctlproxy_buffering offneeded for SSE streaming.start,stop,restart,enable,disable, log commands).Backwards compatibility
cookie_file/auth_user/proxyconfig continue to work without any changes — the pool falls back to a single account built from those fields.api_keyslist (the previous default) will now refuse all requests and refuse to start. This is intentional: an accidentally open server on0.0.0.0is a security risk. To restore the old open behaviour, the operator must explicitly set"host": "127.0.0.1"and accept that only local processes can reach it — or add API keys.Testing
Tested with:
auth_userindicescurlwith and withoutAuthorization: Bearerheader (401 verified)gemini-3.5-flash) and non-streaming (gemini-3.5-flash-thinking) requestspython -m gemini_web2api(package) andpython gemini_web2api.py(monolithic)