You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
meshcore-client/lib/helpers/share_link_forms.dart

63 lines
2.5 KiB

/// The two forms of a channel or contact share link (#1280).
///
/// - `meshcore://channel/add?<fields>` and `meshcore://contact/add?<fields>`:
/// what the stock app reads, so QR codes keep using it.
/// - `https://offband.app/add/channel#<fields>` and `/add/contact#<fields>`:
/// what Share and Copy Link emit. The OS opens it in Offband, which the
/// stock app's claim on `meshcore://` gets in the way of.
///
/// The fields are the same in both. In the https form they sit after `#`,
/// which browsers never send to the server, so a channel secret stays out
/// of the server's logs.
library;
/// The hosts whose `/add/...` links are Offband share links. The app is
/// verified for both (#1309), so these open it instead of the browser.
const shareLinkHosts = {'offband.app', 'dev.offband.app'};
/// The host Share and Copy Link emit (#1311).
const shareLinkHost = 'offband.app';
final _meshcoreForm = RegExp(r'^meshcore://(channel|contact)/add\?(.+)$');
/// The `meshcore://` twin of an `https://offband.app/add/channel#...` or
/// `/add/contact#...` link, or null if [raw] is not one (#1309).
String? meshcoreFormOf(String raw) {
final uri = Uri.tryParse(raw.trim());
if (uri == null || !isShareAddress(uri)) return null;
final path = _pathOf(uri);
if (path.length != 2) return null;
if (path[1] != 'channel' && path[1] != 'contact') return null;
if (uri.query.isNotEmpty || uri.fragment.isEmpty) return null;
return 'meshcore://${path[1]}/add?${uri.fragment}';
}
/// The path's segments, lowercased: a link typed by hand as `/Add/Channel`
/// is the same link.
List<String> _pathOf(Uri uri) => [
for (final s in uri.pathSegments)
if (s.isNotEmpty) s.toLowerCase(),
];
/// The https twin of a `meshcore://channel/add?...` or `contact/add?...`
/// link, on [shareLinkHost] (#1311). The fields are carried as they are,
/// already encoded.
String httpsFormOf(String meshcoreLink) {
final match = _meshcoreForm.firstMatch(meshcoreLink);
if (match == null) {
throw ArgumentError.value(meshcoreLink, 'meshcoreLink', 'not a share link');
}
return 'https://$shareLinkHost/add/${match[1]}#${match[2]}';
}
/// [raw] in the `meshcore://` form, whichever form it arrived in, so one
/// parser reads both.
String toMeshcoreForm(String raw) => meshcoreFormOf(raw) ?? raw.trim();
/// True for any `https://offband.app/add/...` address, well formed or not.
bool isShareAddress(Uri uri) =>
uri.scheme.toLowerCase() == 'https' &&
!uri.hasPort &&
shareLinkHosts.contains(uri.host.toLowerCase()) &&
_pathOf(uri).firstOrNull == 'add';

Powered by TurnKey Linux.