mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 03:26:07 +00:00
07448e662c
A proxy in front of TREK (Cloudflare, nginx, Caddy, Traefik, HAProxy) can let in only devices that show a client certificate. The apps could not, so such a server was unusable from them. - Address screen: the server check runs natively and notices when the server asks for a certificate. Instead of "no server answered" it says so and offers to add a .p12/.pfx file; a wrong password asks again, an expired, not yet valid or keyless file is named as such. - The certificate is shown only to the host and port it was added for. iOS keeps it in the keychain (this device only), Android seals the key with an Android Keystore key. The file's password is never stored. - The web view answers the server's certificate request on both platforms; WebSocket, service worker and API requests go through with it. The offline copy, watch requests and the check use it too. - When the server stops letting the app in (certificate removed, expired or no longer trusted) while the page still comes from the service worker or the offline copy, a native check sends the app to the address screen, which offers to add another. A server that only asks and lets everyone in is left alone. - Cloudflare-style 403 and nginx-style 400 refusals without a certificate lead to the same screen instead of staying up as an error page. - Settings, Account: a Client certificate card (only with one stored) shows subject, issuer, server and expiry, with Replace and Remove. - Wiki: Native-Apps "Client certificates (mTLS)" with Cloudflare, nginx, Caddy and Traefik setup and how to make a certificate for the app. - The app icon on the unreachable screen is black on the light surface.
70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
import { hasNativeFeature } from './capabilities';
|
|
import { reportNativeFailure } from './reportFailure';
|
|
import {
|
|
TrekShell,
|
|
type ClientCertificateImport,
|
|
type ClientCertificateInfo,
|
|
type ClientCertificateLabels,
|
|
} from './trekShell';
|
|
|
|
/**
|
|
* The client certificate the app shows to a server behind mutual TLS (a proxy
|
|
* such as Cloudflare, nginx, Caddy or Traefik that lets in only devices with
|
|
* one). The address screen adds it when the server asks; the settings show,
|
|
* replace and remove it. The binary keeps it (TrekClientIdentity on iOS and
|
|
* Android) and shows it to the one host and port it was added for.
|
|
*/
|
|
|
|
export function clientCertificateSupported(): boolean {
|
|
return hasNativeFeature('clientCertificate');
|
|
}
|
|
|
|
/** The stored certificate, or null when there is none or the binary cannot hold one. */
|
|
export async function storedClientCertificate(): Promise<ClientCertificateInfo | null> {
|
|
if (!clientCertificateSupported()) return null;
|
|
try {
|
|
const { certificate } = await TrekShell.clientCertificate();
|
|
return certificate ?? null;
|
|
} catch (err) {
|
|
reportNativeFailure(err);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** Host and port a certificate for this server address is kept for; null for a plain http server. */
|
|
export function certificateTarget(serverUrl: string): { host: string; port: number } | null {
|
|
let url: URL;
|
|
try {
|
|
url = new URL(serverUrl);
|
|
} catch {
|
|
return null;
|
|
}
|
|
if (url.protocol !== 'https:' || !url.hostname) return null;
|
|
return { host: url.hostname.toLowerCase(), port: url.port ? Number(url.port) : 443 };
|
|
}
|
|
|
|
/** Picks a certificate file for the server the app shows and keeps it in place of the stored one. */
|
|
export async function replaceClientCertificate(
|
|
serverUrl: string,
|
|
labels: ClientCertificateLabels
|
|
): Promise<ClientCertificateImport> {
|
|
const target = certificateTarget(serverUrl);
|
|
if (!target) return { result: 'failed', failure: 'format' };
|
|
try {
|
|
return await TrekShell.importClientCertificate({ ...target, labels });
|
|
} catch (err) {
|
|
reportNativeFailure(err);
|
|
return { result: 'failed', failure: 'store' };
|
|
}
|
|
}
|
|
|
|
export async function removeClientCertificate(): Promise<boolean> {
|
|
try {
|
|
await TrekShell.removeClientCertificate();
|
|
return true;
|
|
} catch (err) {
|
|
reportNativeFailure(err);
|
|
return false;
|
|
}
|
|
}
|