feat(app): Schloss-Symbol fuer die Verbindung + abgelaufenes Zertifikat erlaubbar
Neben dem Zahnrad zeigt ein Schloss, wie sicher die Verbindung zu ARIA ist: Schloss = verschluesselt und in Ordnung, Schloss mit ! = Zertifikat-Problem (abgelaufen oder abgelehnt), durchgestrichen = unverschluesselt (TLS-Fallback oder so eingestellt). Antippen erklaert es in einfachen Worten; einmal pro App-Start kommt ein Hinweis, wenn das Zertifikat abgelaufen ist. Einstellungen → Verbindung: "Abgelaufenes Zertifikat akzeptieren". Toleriert GENAU das Ablaufdatum: die Kette wird zum Zeitpunkt kurz vor dem Ablauf gegen die System-CAs geprueft, der Hostname weiter von OkHttp. Selbstsignierte oder fremde Zertifikate kommen auch mit Schalter nicht durch (gegen das echte, abgelaufene RVS-Zertifikat und badssl.com getestet). Gilt fuer WebSocket und fetch (z.B. App-Update vom RVS). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,9 @@
|
||||
*/
|
||||
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { NativeModules } from 'react-native';
|
||||
|
||||
const TlsTrust: any = NativeModules.TlsTrust;
|
||||
|
||||
// --- Typen ---
|
||||
|
||||
@@ -27,6 +30,29 @@ export interface ConnectionConfig {
|
||||
useTLS: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wie sicher ist die Verbindung? Fuer das Schloss-Symbol in der Tab-Leiste.
|
||||
* secure — verschluesselt, Zertifikat in Ordnung
|
||||
* expired — verschluesselt, aber das Zertifikat ist abgelaufen (per Schalter erlaubt)
|
||||
* cert_error— keine Verbindung, weil das Zertifikat abgelehnt wurde
|
||||
* fallback — verbunden OHNE Verschluesselung, weil TLS fehlschlug
|
||||
* plain — verbunden ohne Verschluesselung, so konfiguriert
|
||||
* unknown — (noch) nichts Genaues
|
||||
*/
|
||||
export type SecurityKind = 'secure' | 'expired' | 'cert_error' | 'fallback' | 'plain' | 'unknown';
|
||||
|
||||
export interface SecurityState {
|
||||
kind: SecurityKind;
|
||||
/** Pruefergebnis der App: ok | expired_accepted | expired | invalid | unknown */
|
||||
certStatus: string;
|
||||
/** Ablaufdatum des Server-Zertifikats (ms), 0 = unbekannt */
|
||||
notAfter: number;
|
||||
/** Bei fallback/cert_error: warum TLS nicht ging */
|
||||
reason: string;
|
||||
}
|
||||
|
||||
type SecurityCallback = (state: SecurityState) => void;
|
||||
|
||||
type MessageCallback = (message: RVSMessage) => void;
|
||||
type StateCallback = (state: ConnectionState) => void;
|
||||
|
||||
@@ -64,6 +90,10 @@ class RVSConnection {
|
||||
private logListeners: LogCallback[] = [];
|
||||
private connectionLog: ConnectionLogEntry[] = [];
|
||||
private usingTLSFallback: boolean = false;
|
||||
private securityListeners: SecurityCallback[] = [];
|
||||
private security: SecurityState = { kind: 'unknown', certStatus: 'unknown', notAfter: 0, reason: '' };
|
||||
/** Letzter Zertifikats-Fehler beim wss://-Versuch (bleibt bis wss:// wieder klappt) */
|
||||
private certFailure: { status: string; notAfter: number; detail: string } | null = null;
|
||||
|
||||
// --- Konfiguration ---
|
||||
|
||||
@@ -216,6 +246,7 @@ class RVSConnection {
|
||||
const tlsInfo = this.usingTLSFallback ? ' (TLS-Fallback: ws://)' : '';
|
||||
this.log('info', `Verbunden${tlsInfo}`);
|
||||
this.setState('connected');
|
||||
this.updateSecurityOnOpen(useTLS);
|
||||
this.reconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
||||
this.startHeartbeat();
|
||||
};
|
||||
@@ -239,6 +270,10 @@ class RVSConnection {
|
||||
this.clearTimers();
|
||||
this.ws = null;
|
||||
this.setState('disconnected');
|
||||
this.setSecurity(this.certFailure
|
||||
? { kind: 'cert_error', certStatus: this.certFailure.status, notAfter: this.certFailure.notAfter,
|
||||
reason: this.certFailure.detail }
|
||||
: { kind: 'unknown', certStatus: 'unknown', notAfter: 0, reason: '' });
|
||||
|
||||
// Sticky-Fallback-Reset: beim naechsten Reconnect wieder primary
|
||||
// (wss://) versuchen statt fuer immer auf ws:// zu kleben. War
|
||||
@@ -258,6 +293,7 @@ class RVSConnection {
|
||||
this.ws.onerror = (error) => {
|
||||
const errorMsg = (error as any)?.message || 'Unbekannter Fehler';
|
||||
this.log('error', `WebSocket-Fehler: ${errorMsg}`);
|
||||
if (useTLS) this.checkCertFailure(errorMsg);
|
||||
|
||||
// TLS-Fallback: Wenn wss:// fehlschlaegt, auf ws:// wechseln
|
||||
if (this.config?.useTLS && !this.usingTLSFallback) {
|
||||
@@ -287,6 +323,83 @@ class RVSConnection {
|
||||
}
|
||||
}
|
||||
|
||||
// --- Verbindungs-Sicherheit (Schloss-Symbol) ---
|
||||
|
||||
getSecurity(): SecurityState {
|
||||
return this.security;
|
||||
}
|
||||
|
||||
onSecurityChange(callback: SecurityCallback): () => void {
|
||||
this.securityListeners.push(callback);
|
||||
return () => {
|
||||
this.securityListeners = this.securityListeners.filter(cb => cb !== callback);
|
||||
};
|
||||
}
|
||||
|
||||
private setSecurity(next: SecurityState): void {
|
||||
const cur = this.security;
|
||||
if (cur.kind === next.kind && cur.certStatus === next.certStatus
|
||||
&& cur.notAfter === next.notAfter && cur.reason === next.reason) return;
|
||||
this.security = next;
|
||||
this.securityListeners.forEach(cb => cb(next));
|
||||
}
|
||||
|
||||
private async nativeCertStatus(): Promise<{ status: string; notAfter: number; detail: string }> {
|
||||
try {
|
||||
const r = await TlsTrust?.getStatus();
|
||||
return { status: r?.status || 'unknown', notAfter: Number(r?.notAfter) || 0, detail: r?.detail || '' };
|
||||
} catch (_) {
|
||||
return { status: 'unknown', notAfter: 0, detail: '' };
|
||||
}
|
||||
}
|
||||
|
||||
private async updateSecurityOnOpen(viaTLS: boolean): Promise<void> {
|
||||
if (viaTLS) {
|
||||
this.certFailure = null;
|
||||
const c = await this.nativeCertStatus();
|
||||
if (c.status === 'expired_accepted') {
|
||||
this.log('warn', `Zertifikat abgelaufen (${new Date(c.notAfter).toLocaleDateString()}) — per Einstellung akzeptiert`);
|
||||
}
|
||||
this.setSecurity({ kind: c.status === 'expired_accepted' ? 'expired' : 'secure',
|
||||
certStatus: c.status, notAfter: c.notAfter, reason: '' });
|
||||
} else if (this.config?.useTLS) {
|
||||
// wss:// ging nicht, ws:// schon → unverschluesselt per Fallback
|
||||
this.setSecurity({ kind: 'fallback', certStatus: this.certFailure?.status || 'unknown',
|
||||
notAfter: this.certFailure?.notAfter || 0, reason: this.certFailure?.detail || '' });
|
||||
} else {
|
||||
this.setSecurity({ kind: 'plain', certStatus: 'unknown', notAfter: 0, reason: '' });
|
||||
}
|
||||
}
|
||||
|
||||
/** Lag der wss://-Fehler am Zertifikat? Dann merken (fuer Schloss + Hinweis). */
|
||||
private async checkCertFailure(errorMsg: string): Promise<void> {
|
||||
if (!/cert|trust anchor|chain validation|handshake|ssl/i.test(errorMsg)) return;
|
||||
const c = await this.nativeCertStatus();
|
||||
if (c.status !== 'expired' && c.status !== 'invalid') return;
|
||||
this.certFailure = { status: c.status, notAfter: c.notAfter, detail: c.detail || errorMsg };
|
||||
this.log('error', c.status === 'expired'
|
||||
? `Server-Zertifikat abgelaufen seit ${new Date(c.notAfter).toLocaleString()}`
|
||||
: 'Server-Zertifikat ungueltig');
|
||||
if (this.state !== 'connected') {
|
||||
this.setSecurity({ kind: 'cert_error', certStatus: c.status, notAfter: c.notAfter,
|
||||
reason: this.certFailure.detail });
|
||||
} else if (this.security.kind === 'fallback') {
|
||||
// ws://-Fallback war schneller verbunden als diese Pruefung — Grund nachtragen
|
||||
this.setSecurity({ ...this.security, certStatus: c.status, notAfter: c.notAfter,
|
||||
reason: this.certFailure.detail });
|
||||
}
|
||||
}
|
||||
|
||||
/** Schalter "Abgelaufenes Zertifikat akzeptieren" */
|
||||
async getAllowExpiredCert(): Promise<boolean> {
|
||||
try { return !!(await TlsTrust?.getStatus())?.allowExpired; } catch (_) { return false; }
|
||||
}
|
||||
|
||||
async setAllowExpiredCert(value: boolean): Promise<void> {
|
||||
try { await TlsTrust?.setAllowExpired(value); } catch (_) {}
|
||||
this.log('info', `Abgelaufene Zertifikate ${value ? 'werden akzeptiert' : 'werden abgelehnt'}`);
|
||||
}
|
||||
|
||||
/** Reconnect mit exponentiellem Backoff planen */
|
||||
private scheduleReconnect(): void {
|
||||
this.log('info', `Reconnect in ${this.reconnectDelay / 1000}s...`);
|
||||
|
||||
Reference in New Issue
Block a user