mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
fix(native): an error page in place of the server's keeps a way back to the address screen
A proxy's block page or a refused client certificate (Cloudflare's "Sorry, you have been blocked") left the app on a page without TREK's UI and no way to change the server. Any 4xx main-frame page that the client-certificate path does not handle now shows the sign-in bar with its own hint and "Change server", on iOS and Android. TestFlight feedback, 5.1.2.
This commit is contained in:
@@ -47,7 +47,13 @@ public class MainActivity extends BridgeActivity {
|
||||
// Set before any WebView callback can run: they are posted to this thread.
|
||||
if (onServer) {
|
||||
bridge.setWebViewClient(
|
||||
new TrekWebViewClient(bridge, this::showAddressScreen, this::showSignInBar, TrekServerStore.getSignInHosts(this))
|
||||
new TrekWebViewClient(
|
||||
bridge,
|
||||
this::showAddressScreen,
|
||||
this::showSignInBar,
|
||||
this::showErrorPageBar,
|
||||
TrekServerStore.getSignInHosts(this)
|
||||
)
|
||||
);
|
||||
}
|
||||
TrekSharedFiles.receive(this, getIntent());
|
||||
@@ -84,7 +90,10 @@ public class MainActivity extends BridgeActivity {
|
||||
private View signInBar;
|
||||
/** Whether the page is a proxy's sign-in page, and whether the keyboard is up over it. */
|
||||
private boolean onSignInPage;
|
||||
/** Whether the page is an error page in place of the server's (TrekWebViewClient.onReceivedHttpError). */
|
||||
private boolean onErrorPage;
|
||||
private boolean keyboardUp;
|
||||
private TextView signInHint;
|
||||
|
||||
/**
|
||||
* Over a proxy's sign-in page (Pangolin, authentik, ...): the server's UI is
|
||||
@@ -100,9 +109,22 @@ public class MainActivity extends BridgeActivity {
|
||||
});
|
||||
}
|
||||
|
||||
/** The same bar over an error page in place of the server's, with words for that. */
|
||||
private void showErrorPageBar(boolean show) {
|
||||
runOnUiThread(() -> {
|
||||
onErrorPage = show;
|
||||
if (show && signInBar == null) signInBar = buildSignInBar();
|
||||
updateSignInBar();
|
||||
});
|
||||
}
|
||||
|
||||
/** Out of the way while the keyboard is up: the form needs the room, and the bar is there again once it closes. */
|
||||
private void updateSignInBar() {
|
||||
if (signInBar != null) signInBar.setVisibility(onSignInPage && !keyboardUp ? View.VISIBLE : View.GONE);
|
||||
if (signInBar == null) return;
|
||||
if (signInHint != null) {
|
||||
signInHint.setText(onSignInPage ? TrekServerStore.signInHintLabel(this) : TrekServerStore.errorPageHintLabel(this));
|
||||
}
|
||||
signInBar.setVisibility((onSignInPage || onErrorPage) && !keyboardUp ? View.VISIBLE : View.GONE);
|
||||
}
|
||||
|
||||
private View buildSignInBar() {
|
||||
@@ -118,6 +140,7 @@ public class MainActivity extends BridgeActivity {
|
||||
bar.setElevation(6 * dp);
|
||||
|
||||
TextView hint = new TextView(this);
|
||||
signInHint = hint;
|
||||
hint.setText(TrekServerStore.signInHintLabel(this));
|
||||
hint.setTextColor(0xFFE5E7EB);
|
||||
hint.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12.5f);
|
||||
|
||||
@@ -26,6 +26,7 @@ final class TrekServerStore {
|
||||
private static final String KEY_SERVER_CHECK = "server_check";
|
||||
private static final String KEY_LABEL_SIGN_IN_HINT = "label_sign_in_hint";
|
||||
private static final String KEY_LABEL_CHANGE_SERVER = "label_change_server";
|
||||
private static final String KEY_LABEL_ERROR_PAGE_HINT = "label_error_page_hint";
|
||||
|
||||
/**
|
||||
* The server API this build needs (MIN_NATIVE_API in serverAddress.ts). A
|
||||
@@ -83,13 +84,21 @@ final class TrekServerStore {
|
||||
* as the address screen passed them (setServer's `labels`): the server's UI
|
||||
* is not there to say them. Null keeps the English fallback.
|
||||
*/
|
||||
static void setLabels(Context context, String signInHint, String changeServer) {
|
||||
static void setLabels(Context context, String signInHint, String changeServer, String errorPageHint) {
|
||||
SharedPreferences.Editor editor = prefs(context).edit();
|
||||
if (signInHint != null) editor.putString(KEY_LABEL_SIGN_IN_HINT, signInHint);
|
||||
if (changeServer != null) editor.putString(KEY_LABEL_CHANGE_SERVER, changeServer);
|
||||
if (errorPageHint != null) editor.putString(KEY_LABEL_ERROR_PAGE_HINT, errorPageHint);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
static String errorPageHintLabel(Context context) {
|
||||
return prefs(context).getString(
|
||||
KEY_LABEL_ERROR_PAGE_HINT,
|
||||
"This page is not from TREK: the server or a proxy in front of it refused the app. Check your proxy or certificate, or change the server."
|
||||
);
|
||||
}
|
||||
|
||||
static String signInHintLabel(Context context) {
|
||||
return prefs(context).getString(
|
||||
KEY_LABEL_SIGN_IN_HINT,
|
||||
|
||||
@@ -328,7 +328,12 @@ public class TrekShellPlugin extends Plugin {
|
||||
TrekServerStore.setServer(getContext(), url, signInHostsOf(call));
|
||||
JSObject labels = call.getObject("labels");
|
||||
if (labels != null) {
|
||||
TrekServerStore.setLabels(getContext(), labels.getString("signInHint"), labels.getString("changeServer"));
|
||||
TrekServerStore.setLabels(
|
||||
getContext(),
|
||||
labels.getString("signInHint"),
|
||||
labels.getString("changeServer"),
|
||||
labels.getString("errorPageHint")
|
||||
);
|
||||
}
|
||||
// A TREK Cloud purchase: the code that signs the buyer in, opened once after the first page.
|
||||
String welcome = call.getString("welcome");
|
||||
|
||||
@@ -27,14 +27,25 @@ class TrekWebViewClient extends BridgeWebViewClient {
|
||||
private final Runnable onUnreachable;
|
||||
/** Told whether the page now shown is a proxy's sign-in page, for the bar with the way out. */
|
||||
private final Consumer<Boolean> onSignInPage;
|
||||
/** Told whether the page now shown is an error page in place of the server's, for the same bar. */
|
||||
private final Consumer<Boolean> onErrorPage;
|
||||
/** The main-frame address that last came back as a 4xx page nobody else handled. */
|
||||
private String errorPageUrl;
|
||||
private final Set<String> signInHosts;
|
||||
/** A page of the server has shown since launch; from then on no sign-in host is learned (SignInRedirect). */
|
||||
private boolean serverShown;
|
||||
|
||||
TrekWebViewClient(Bridge bridge, Runnable onUnreachable, Consumer<Boolean> onSignInPage, Set<String> signInHosts) {
|
||||
TrekWebViewClient(
|
||||
Bridge bridge,
|
||||
Runnable onUnreachable,
|
||||
Consumer<Boolean> onSignInPage,
|
||||
Consumer<Boolean> onErrorPage,
|
||||
Set<String> signInHosts
|
||||
) {
|
||||
super(bridge);
|
||||
this.onUnreachable = onUnreachable;
|
||||
this.onSignInPage = onSignInPage;
|
||||
this.onErrorPage = onErrorPage;
|
||||
this.signInHosts = new HashSet<>(signInHosts);
|
||||
}
|
||||
|
||||
@@ -70,6 +81,11 @@ class TrekWebViewClient extends BridgeWebViewClient {
|
||||
public void doUpdateVisitedHistory(WebView view, String url, boolean isReload) {
|
||||
super.doUpdateVisitedHistory(view, url, isReload);
|
||||
onSignInPage.accept(url != null && isSignInHost(Uri.parse(url)));
|
||||
// The 4xx answer arrives before this commit or after it; either way the
|
||||
// flag is read once here, so the next page clears the bar again.
|
||||
boolean error = url != null && url.equals(errorPageUrl);
|
||||
errorPageUrl = null;
|
||||
onErrorPage.accept(error);
|
||||
}
|
||||
|
||||
/** After the server's first page, a pending welcome code opens the welcome page once (TrekServerStore.takeWelcomeUrl). */
|
||||
@@ -132,10 +148,12 @@ class TrekWebViewClient extends BridgeWebViewClient {
|
||||
|
||||
/**
|
||||
* Only a 5xx on the page itself means the server is down (a proxy's 502 or
|
||||
* 503 while TREK restarts). A 4xx page is left alone: the server answered,
|
||||
* and TREK serves its UI for every other path. The exception is a proxy
|
||||
* that asked this page for a client certificate and got none, as Cloudflare
|
||||
* does with a 403: the address screen then says so and offers to add one.
|
||||
* 503 while TREK restarts). A 4xx page is not TREK's, which serves its UI for
|
||||
* every path: a proxy that asked this page for a client certificate and got
|
||||
* none, as Cloudflare does with a 403, sends the app to the address screen,
|
||||
* which says so and offers to add one. Any other 4xx page (a proxy's block
|
||||
* page, a certificate it refused) stays up with the bar and its way out, so
|
||||
* the app is never stuck on a page without TREK's UI.
|
||||
*/
|
||||
@Override
|
||||
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
|
||||
@@ -144,6 +162,9 @@ class TrekWebViewClient extends BridgeWebViewClient {
|
||||
super.onReceivedHttpError(view, request, errorResponse);
|
||||
} else if (status >= 500 || (status >= 400 && wantedCertificate(request.getUrl()))) {
|
||||
onUnreachable.run();
|
||||
} else if (status >= 400) {
|
||||
errorPageUrl = request.getUrl().toString();
|
||||
onErrorPage.accept(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -68,6 +68,10 @@ class TrekBridgeViewController: CAPBridgeViewController {
|
||||
private var urlObservation: NSKeyValueObservation?
|
||||
private var insetObservation: NSKeyValueObservation?
|
||||
private var onSignInPage = false
|
||||
/// Whether the main frame shows an error page in place of the server's (a 4xx
|
||||
/// nobody else handled: a proxy's block page, a client certificate it refused).
|
||||
private var onErrorPage = false
|
||||
private var signInHint: UILabel?
|
||||
private var keyboardUp = false
|
||||
/// How far the keyboard reaches into the view while it is up.
|
||||
private var keyboardRoom: CGFloat = 0
|
||||
@@ -97,9 +101,20 @@ class TrekBridgeViewController: CAPBridgeViewController {
|
||||
updateSignInBar()
|
||||
}
|
||||
|
||||
private func errorPageChanged(_ error: Bool) {
|
||||
onErrorPage = error
|
||||
if error && signInBar == nil { signInBar = makeSignInBar() }
|
||||
updateSignInBar()
|
||||
}
|
||||
|
||||
/// Out of the way while the keyboard is up: the form needs the room.
|
||||
private func updateSignInBar() {
|
||||
signInBar?.isHidden = !(onSignInPage && !keyboardUp)
|
||||
let labels = TrekServerStore.labels
|
||||
signInHint?.text = onSignInPage
|
||||
? labels["signInHint"]
|
||||
?? "Passkeys and security keys do not work on this page in the app. Sign in with a password, or let the app through your proxy."
|
||||
: labels["errorPageHint"] ?? "This page is not from TREK: the server or a proxy in front of it refused the app. Check your proxy or certificate, or change the server."
|
||||
signInBar?.isHidden = !((onSignInPage || onErrorPage) && !keyboardUp)
|
||||
applyInset()
|
||||
}
|
||||
|
||||
@@ -108,7 +123,7 @@ class TrekBridgeViewController: CAPBridgeViewController {
|
||||
private func applyInset() {
|
||||
guard let scrollView = webView?.scrollView else { return }
|
||||
let wanted: CGFloat
|
||||
if onSignInPage {
|
||||
if onSignInPage || onErrorPage {
|
||||
if keyboardUp {
|
||||
wanted = keyboardRoom
|
||||
} else if let bar = signInBar {
|
||||
@@ -172,6 +187,7 @@ class TrekBridgeViewController: CAPBridgeViewController {
|
||||
hint.font = .preferredFont(forTextStyle: .footnote)
|
||||
hint.adjustsFontForContentSizeCategory = true
|
||||
hint.numberOfLines = 0
|
||||
signInHint = hint
|
||||
hint.translatesAutoresizingMaskIntoConstraints = false
|
||||
|
||||
let change = UIButton(type: .system)
|
||||
@@ -748,7 +764,9 @@ final class TrekNavigationProxy: NSObject, WKNavigationDelegate {
|
||||
/// A proxy that asked the page for a client certificate, got none and
|
||||
/// answered with its own refusal (Cloudflare's 403, nginx's 400) sends the
|
||||
/// app to the address screen, which says so and offers to add one, rather
|
||||
/// than leaving the proxy's error page up.
|
||||
/// than leaving the proxy's error page up. Any other 4xx page is not TREK's,
|
||||
/// which serves its UI for every path (a proxy's block page, a certificate it
|
||||
/// refused): it stays up with the bar and its way out to another server.
|
||||
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse,
|
||||
decisionHandler: @escaping (WKNavigationResponsePolicy) -> Void) {
|
||||
if navigationResponse.isForMainFrame, let http = navigationResponse.response as? HTTPURLResponse,
|
||||
@@ -758,6 +776,10 @@ final class TrekNavigationProxy: NSObject, WKNavigationDelegate {
|
||||
inner.webView?(webView, didFailProvisionalNavigation: nil, withError: URLError(.clientCertificateRequired))
|
||||
return
|
||||
}
|
||||
if navigationResponse.isForMainFrame, let http = navigationResponse.response as? HTTPURLResponse {
|
||||
let error = http.statusCode >= 400 && http.statusCode < 500
|
||||
DispatchQueue.main.async { [weak self] in self?.errorPageChanged(error) }
|
||||
}
|
||||
let selector = NSSelectorFromString("webView:decidePolicyForNavigationResponse:decisionHandler:")
|
||||
guard (inner as AnyObject).responds(to: selector) else {
|
||||
decisionHandler(.allow)
|
||||
|
||||
@@ -1281,7 +1281,11 @@ async function connected(url: string, signInHosts: string[] = [], welcome?: stri
|
||||
await new Promise((r) => setTimeout(r, 700));
|
||||
await TrekShell.setServer({
|
||||
url,
|
||||
labels: { signInHint: t('native.signInBar.hint'), changeServer: t('native.changeServer') },
|
||||
labels: {
|
||||
signInHint: t('native.signInBar.hint'),
|
||||
changeServer: t('native.changeServer'),
|
||||
errorPageHint: t('native.errorPageBar.hint'),
|
||||
},
|
||||
...(signInHosts.length ? { signInHosts } : {}),
|
||||
...(welcome ? { welcome } : {}),
|
||||
});
|
||||
|
||||
@@ -27,13 +27,14 @@ export interface TrekShellPlugin {
|
||||
* opens the server's welcome page with it, which signs the buyer in. A build
|
||||
* without this option ignores it and opens the server as usual.
|
||||
* `labels` are the words of the bar the app shows over a proxy's sign-in page,
|
||||
* in the user's language: the server's UI is not there to say them.
|
||||
* or over an error page in place of the server's (a proxy's block page), in the
|
||||
* user's language: the server's UI is not there to say them.
|
||||
*/
|
||||
setServer(options: {
|
||||
url: string;
|
||||
signInHosts?: string[];
|
||||
welcome?: string;
|
||||
labels?: { signInHint: string; changeServer: string };
|
||||
labels?: { signInHint: string; changeServer: string; errorPageHint?: string };
|
||||
}): Promise<void>;
|
||||
/** Forgets the server and returns to the address screen. */
|
||||
resetServer(): Promise<void>;
|
||||
|
||||
Reference in New Issue
Block a user