mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
## Summary Communities joined via an invite link never connect: the app dials `ws://` on port 80 instead of `wss://` on 443 and sits on "Reconnecting…" indefinitely. `RelayConfig.baseUrl` is documented as an HTTP origin, but the two onboarding flows disagree on what they persist: - **Device pairing** validates and stores `https://` — `pairing_provider.dart:657` throws on anything else. - **Invite join** stores the relay URL straight off the invite link, and `deep_link.dart:165` always emits `ws://` or `wss://`. `wsUrl` only special-cased `https://`, so a `wss://` base fell through to the plaintext branch: ```dart final scheme = uri.scheme == 'https' ? 'wss' : 'ws'; // 'wss' is not 'https' ``` The claim request itself succeeds, because `_claimUrlFromRelay` (`invite_join_provider.dart:242`) maps `wss → https` explicitly. Only the socket path is missing that conversion — which is why the community appears, correctly named, and then never loads. The same `baseUrl` also feeds `/query` (`relay_session.dart:136`), media upload (`media_upload.dart:765`), Blossom auth (`media_auth.dart:128`) and `relayClientProvider` (`relay_provider.dart:113`), so those requests were malformed too. Where port 80 *does* answer, it is additionally a silent TLS downgrade after `validateInviteRelayUri` insisted on `wss://`. This folds the websocket schemes back to their HTTP equivalents in `baseUrl` itself, so every consumer is correct by construction rather than needing a second getter remembered at each call site, and communities **already persisted** with `wss://` are repaired on read without a migration. `community_icon_provider.dart:46` already performs this same conversion locally. One subtlety worth flagging for review: the normalization is derived in the getter rather than applied in the constructor, so the constructor stays `const`. The compile-time fallback at `relay_provider.dart:77` relies on const canonicalization for a stable identity across rebuilds, and Riverpod's `defaultUpdateShouldNotify` is `previous != next` (`element.dart:361`), which falls back to identity for this class. A `factory` constructor here yields a fresh instance per rebuild, which tears down and resubscribes every listener — `channels_provider_test.dart` catches it as an unexpected unsubscribe during reconnect. ### Related issue Fixes #2662. ### Testing `flutter test` — **705 passed, 1 skipped, 0 failed** `flutter analyze` — No issues found `dart format --set-exit-if-changed .` — 249 files, 0 changed Run against the Hermit-pinned SDK (Flutter 3.41.7 / Dart 3.11.5), matching CI. 10 new unit tests in `mobile/test/shared/relay/relay_config_test.dart` covering both onboarding schemes, `http`/`https` passthrough, non-default ports, and agreement between the invite and pairing paths for the same relay. Verified end-to-end against a self-hosted relay behind `tailscale serve`, which terminates TLS on 443 and leaves port 80 closed. Relay logs show the invite claim succeeding over HTTPS at the moment of joining, while no WebSocket connection ever arrives — no `WebSocket connection established`, no NIP-42 auth, no `kind:0` profile, no push registration — across the relay's entire history, even though the member row is present and correct. Port-80 refusals are not logged by `tailscaled`'s netstack, which is why the retries leave no trace server-side. Reproduced on both iOS and Android. --------- Signed-off-by: Krishna C <github@kumb.uk>
127 lines
4.6 KiB
Dart
127 lines
4.6 KiB
Dart
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
import '../community/community_provider.dart';
|
|
import 'relay_client.dart';
|
|
|
|
/// Relay connection configuration.
|
|
///
|
|
/// In the pure-nostr world the only secrets the app cares about are:
|
|
/// - `baseUrl` — where the relay lives (used for WS + media upload)
|
|
/// - `nsec` — the user's signing key (drives NIP-42 AUTH and event sigs)
|
|
class RelayConfig {
|
|
const RelayConfig({required String baseUrl, this.nsec}) : _baseUrl = baseUrl;
|
|
|
|
/// Relay origin exactly as the active community stored it.
|
|
final String _baseUrl;
|
|
|
|
/// Nostr secret key (bech32 nsec) for signing events and NIP-42 AUTH.
|
|
final String? nsec;
|
|
|
|
/// The origin as persisted, before scheme canonicalization.
|
|
///
|
|
/// Exists solely so identity-scoped storage keys written before [baseUrl]
|
|
/// was canonicalized stay reachable — see [readMigratedPref]. Never use it
|
|
/// for network I/O; [baseUrl] and [wsUrl] are the addresses to connect to.
|
|
String get storedOrigin => _baseUrl;
|
|
|
|
/// Relay origin as an HTTP(S) URL.
|
|
///
|
|
/// Communities are persisted with whichever scheme their onboarding flow
|
|
/// used: device pairing stores `https://` (it rejects anything else), while
|
|
/// an invite join stores the `wss://` relay URL carried by the invite link.
|
|
/// Every consumer treats this as an HTTP origin — [wsUrl], the `/query`
|
|
/// endpoint, media upload and Blossom auth — so a `wss://` base silently
|
|
/// degrades all of them. Folding the websocket schemes back here keeps both
|
|
/// onboarding paths equivalent, including for already-persisted communities.
|
|
///
|
|
/// Derived rather than normalized in the constructor so that the constructor
|
|
/// stays `const`: the compile-time fallback below relies on canonicalization
|
|
/// to keep its identity stable across rebuilds, and Riverpod's default
|
|
/// `updateShouldNotify` is `previous != next`, which falls back to identity
|
|
/// here. A fresh instance per rebuild would resubscribe every listener.
|
|
String get baseUrl {
|
|
final uri = Uri.tryParse(_baseUrl);
|
|
if (uri == null) return _baseUrl;
|
|
final scheme = switch (uri.scheme) {
|
|
'wss' => 'https',
|
|
'ws' => 'http',
|
|
_ => null,
|
|
};
|
|
return scheme == null ? _baseUrl : uri.replace(scheme: scheme).toString();
|
|
}
|
|
|
|
/// Derive the websocket URL from the HTTP base URL.
|
|
String get wsUrl {
|
|
final uri = Uri.parse(baseUrl);
|
|
final scheme = uri.scheme == 'https' ? 'wss' : 'ws';
|
|
return uri.replace(scheme: scheme).toString();
|
|
}
|
|
}
|
|
|
|
/// Compile-time environment config via --dart-define.
|
|
///
|
|
/// Run with:
|
|
/// flutter run --dart-define=BUZZ_RELAY_URL=http://localhost:3000
|
|
///
|
|
/// Or create a `.env.json` and use --dart-define-from-file=.env.json
|
|
class Env {
|
|
static const relayUrl = String.fromEnvironment(
|
|
'BUZZ_RELAY_URL',
|
|
defaultValue: 'http://localhost:3000',
|
|
);
|
|
}
|
|
|
|
class RelayConfigNotifier extends Notifier<RelayConfig> {
|
|
@override
|
|
RelayConfig build() {
|
|
// Watch the active community so that when it changes (community switch),
|
|
// the config rebuilds, triggering the full provider cascade.
|
|
final activeAsync = ref.watch(activeCommunityProvider);
|
|
final active = activeAsync.value;
|
|
if (active != null) {
|
|
return RelayConfig(baseUrl: active.relayUrl, nsec: active.nsec);
|
|
}
|
|
|
|
// Fallback to compile-time env config (dev mode).
|
|
return const RelayConfig(baseUrl: Env.relayUrl);
|
|
}
|
|
|
|
void update({required String baseUrl, String? nsec}) {
|
|
state = RelayConfig(baseUrl: baseUrl, nsec: nsec);
|
|
}
|
|
}
|
|
|
|
final relayConfigProvider = NotifierProvider<RelayConfigNotifier, RelayConfig>(
|
|
RelayConfigNotifier.new,
|
|
);
|
|
|
|
/// Derive the hex pubkey from a bech32 nsec, or null on any failure.
|
|
String? pubkeyFromNsec(String? nsec) {
|
|
if (nsec == null || nsec.isEmpty) return null;
|
|
try {
|
|
final privkeyHex = nostr.Nip19.decode(payload: nsec).data;
|
|
if (privkeyHex.isEmpty) return null;
|
|
return nostr.Keys(privkeyHex).public;
|
|
} catch (_) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// The current user's hex pubkey, derived from the active community nsec.
|
|
final myPubkeyProvider = Provider<String?>((ref) {
|
|
final config = ref.watch(relayConfigProvider);
|
|
return pubkeyFromNsec(config.nsec);
|
|
});
|
|
|
|
/// Provides a [RelayClient] that reacts to config changes.
|
|
///
|
|
/// Only used for the media upload HTTP endpoint now — all data flow goes
|
|
/// through the relay WebSocket session.
|
|
final relayClientProvider = Provider<RelayClient>((ref) {
|
|
final config = ref.watch(relayConfigProvider);
|
|
final client = RelayClient(baseUrl: config.baseUrl);
|
|
ref.onDispose(client.dispose);
|
|
return client;
|
|
});
|