Repository navigation
fix(cwasync): send HTTPS through KOReader's HTTP proxy when one is set - #1663
Open
luis-alvarez wants to merge 1 commit into
Open
luis-alvarez wants to merge 1 commit into
luis-alvarez wants to merge 1 commit into
Conversation
Spore sends https:// requests through luasec's ssl.https.request, which returns "proxy not supported" whenever KOReader's HTTP proxy (socket.http.PROXY) is set, so login and sync fail with "Unknown server error" before any connection is made. This is the case on Kindles without kernel TUN, where Tailscale runs in userspace mode and its local HTTP proxy is the only route to the server. Add a Spore middleware that, only when a proxy is set and the URL is https://, sends the request through socket.http.request the way the OPDS browser does: luasocket hands the absolute URL to the proxy, which makes the TLS connection. Without a proxy, nothing changes. Fixes crocodilestick#1332
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.
Fixes #1332.
Context
I read on a jailbroken Kindle with KOReader and reach my CWA server privately over Tailscale; the server isn't exposed to the internet. The Kindle's kernel has no TUN device, so Tailscale can only run in userspace mode, and KOReader gets onto the tailnet through Tailscale's local HTTP proxy. With that setup, OPDS browsing and downloads from CWA work, but CWA Progress Sync can't log in. #1332 reports the same thing on a Kindle PW3.
Problem
On a Kindle without kernel TUN, Tailscale runs in userspace mode (e.g. via koreader-tailscale or tailscale_kual), and KOReader reaches the tailnet only through tailscaled's local HTTP proxy, set as KOReader's proxy (Settings → Network → Proxy). OPDS works through it, but CWA Progress Sync fails to log in with "Unknown server error", and nothing reaches the server.
The cause is in the HTTP stack, not the server:
http.PROXY(NetworkMgr:setHTTPProxy).CWASyncClientuses lua-Spore, which sendshttps://URLs through luasec'sssl.https.request. That returnsnil, "proxy not supported"wheneverhttp.PROXYis set (luasecsrc/https.lua), before any socket is opened. With no response body, the plugin falls back to "Unknown server error".socket.http.requestwith thehttps://URL. Under a proxy, luasocket sends the absolute URL to the proxy, which makes the TLS connection. That's why OPDS works on the same device.The reporter of #1332 got around it by replacing KOReader's
common/ssl/https.luawith a patched luasec, which changes every HTTPS request in KOReader. This PR fixes it inside the plugin instead.Change
CWASyncClient.luagets a small Spore middleware,ProxiedHTTPS, enabled forauthorize,update_progressandget_progress. It acts only when a proxy is set and the URL ishttps://: it finalizes the request and sends it withsocket.http.request, then returns the response table, which short-circuits Spore's own request.Format.JSONstill decodes the body. In every other case it returnsniland Spore's normal path runs unchanged, so setups without a proxy are unaffected.Security is the same as OPDS's through a proxy: the request is plain HTTP only between KOReader and the local proxy, and the proxy makes the TLS connection to the server.
Testing
On a Kindle with KOReader, koreader-tailscale in userspace mode with its HTTP proxy set in KOReader, and CWA v4.0.8 reached at its
https://tailnet name:/kosyncrequest reached the server (checked with a capture between Tailscale Serve and CWA)Not tested: a setup without a proxy, where the middleware returns
niland the code path is unchanged.