mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user