ad6cdab418
The web proxy binds the wildcard and drops connections that are not loopback or Tailscale at both ends. A LAN listener is opt-in (web.lan) and refuses sign-in and signed-in pages over plain HTTP unless web.lan_plain_login is set. Existing installs keep their bind address with a warning. Without the LAN the mDNS advert carries only the tailnet and public routes (lan=0).
265 lines
8.2 KiB
Dart
265 lines
8.2 KiB
Dart
/// The DNS-SD service type the server advertises (internal/discovery).
|
|
const pipelinesServiceType = '_pipelines._tcp';
|
|
|
|
/// How one route reaches a discovered machine.
|
|
enum DiscoveredRoute {
|
|
/// An address on the network the browsing host shares with the machine.
|
|
lan,
|
|
|
|
/// The machine's tailnet IP; the same port, encrypted by Tailscale.
|
|
tailscale,
|
|
|
|
/// An HTTPS origin fronting the same server, usually its tailnet DNS name.
|
|
public,
|
|
}
|
|
|
|
/// One way to dial a discovered machine.
|
|
class DiscoveredAddress {
|
|
const DiscoveredAddress({required this.route, required this.endpoint});
|
|
|
|
final DiscoveredRoute route;
|
|
final Uri endpoint;
|
|
|
|
/// What the choice list calls this route.
|
|
String get label => switch (route) {
|
|
DiscoveredRoute.lan => 'Local network',
|
|
DiscoveredRoute.tailscale => 'Tailscale IP',
|
|
DiscoveredRoute.public =>
|
|
endpoint.scheme == 'wss' ? 'Tailscale name (HTTPS)' : 'Public name',
|
|
};
|
|
|
|
static DiscoveredRoute? routeNamed(String? kind) => switch (kind) {
|
|
'lan' => DiscoveredRoute.lan,
|
|
'tailscale' => DiscoveredRoute.tailscale,
|
|
'public' => DiscoveredRoute.public,
|
|
_ => null,
|
|
};
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is DiscoveredAddress &&
|
|
other.route == route &&
|
|
other.endpoint == endpoint;
|
|
|
|
@override
|
|
int get hashCode => Object.hash(route, endpoint);
|
|
|
|
@override
|
|
String toString() => '$label: $endpoint';
|
|
}
|
|
|
|
/// A Pipelines server found on the local network.
|
|
///
|
|
/// [endpoint] is the first route; [addresses] lists every advertised route to
|
|
/// this same machine so the operator can choose which ones to save.
|
|
class DiscoveredServer {
|
|
const DiscoveredServer({
|
|
required this.name,
|
|
required this.host,
|
|
required this.port,
|
|
this.secure = false,
|
|
this.path = '/connect',
|
|
this.passwordRequired = true,
|
|
this.version,
|
|
List<DiscoveredAddress>? addresses,
|
|
}) : _routes = addresses;
|
|
|
|
/// Builds a server from a resolved DNS-SD record: the service [instance]
|
|
/// name, the resolved [addresses] (and SRV [hostname] when no address is
|
|
/// usable), the SRV [port] and the TXT [attributes]. Returns null when there
|
|
/// is nothing to dial.
|
|
static DiscoveredServer? fromRecord({
|
|
required String instance,
|
|
required List<String> addresses,
|
|
String? hostname,
|
|
required int port,
|
|
Map<String, String> attributes = const {},
|
|
}) {
|
|
final host = preferredHost(addresses) ?? _bare(hostname);
|
|
if (host == null || port <= 0 || port > 65535) return null;
|
|
final path = attributes['path'] ?? '';
|
|
final name = attributes['name'] ?? '';
|
|
final secure = attributes['tls'] == '1';
|
|
final cleanPath = path.startsWith('/') ? path : '/connect';
|
|
final routes = <DiscoveredAddress>[
|
|
DiscoveredAddress(
|
|
route: DiscoveredRoute.lan,
|
|
endpoint: Uri(
|
|
scheme: secure ? 'wss' : 'ws',
|
|
host: host,
|
|
port: port,
|
|
path: cleanPath,
|
|
),
|
|
),
|
|
];
|
|
for (final value in (attributes['ts'] ?? '').split(',')) {
|
|
final ip = value.trim();
|
|
if (!isTailscaleIPv4(ip) || routes.length > 5) continue;
|
|
routes.add(
|
|
DiscoveredAddress(
|
|
route: DiscoveredRoute.tailscale,
|
|
endpoint: Uri(
|
|
scheme: secure ? 'wss' : 'ws',
|
|
host: ip,
|
|
port: port,
|
|
path: cleanPath,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
final public = publicEndpoint(attributes['public'], path: cleanPath);
|
|
if (public != null) {
|
|
routes.add(
|
|
DiscoveredAddress(route: DiscoveredRoute.public, endpoint: public),
|
|
);
|
|
}
|
|
// lan=0: the web proxy refuses LAN connections, so the address record is
|
|
// not a LAN route; the first tailnet or public route is the endpoint.
|
|
if (attributes['lan'] == '0') routes.removeAt(0);
|
|
if (routes.isEmpty) return null;
|
|
final first = routes.first.endpoint;
|
|
return DiscoveredServer(
|
|
name: name.isEmpty ? instance : name,
|
|
host: first.host,
|
|
port: first.port,
|
|
secure: first.scheme == 'wss',
|
|
path: cleanPath,
|
|
passwordRequired: attributes['auth'] != 'none',
|
|
version: attributes['v'],
|
|
addresses: routes,
|
|
);
|
|
}
|
|
|
|
final String name;
|
|
final String host;
|
|
final int port;
|
|
final bool secure;
|
|
final String path;
|
|
final bool passwordRequired;
|
|
final String? version;
|
|
final List<DiscoveredAddress>? _routes;
|
|
|
|
/// The websocket endpoint the connect screen dials.
|
|
Uri get endpoint =>
|
|
Uri(scheme: secure ? 'wss' : 'ws', host: host, port: port, path: path);
|
|
|
|
/// Every advertised route to this machine, the primary first.
|
|
List<DiscoveredAddress> get addresses =>
|
|
_routes ??
|
|
[DiscoveredAddress(route: DiscoveredRoute.lan, endpoint: endpoint)];
|
|
|
|
/// The routes' endpoints, in order.
|
|
List<Uri> get endpoints => [for (final a in addresses) a.endpoint];
|
|
|
|
/// "host:port" as shown under the name.
|
|
String get address => endpoint.authority;
|
|
|
|
/// The same machine with only the [chosen] routes, the first one primary.
|
|
DiscoveredServer choosing(List<DiscoveredAddress> chosen) {
|
|
if (chosen.isEmpty) return this;
|
|
final first = chosen.first.endpoint;
|
|
return DiscoveredServer(
|
|
name: name,
|
|
host: first.host,
|
|
port: first.hasPort ? first.port : (first.scheme == 'wss' ? 443 : 80),
|
|
secure: first.scheme == 'wss',
|
|
path: first.path,
|
|
passwordRequired: passwordRequired,
|
|
version: version,
|
|
addresses: List.unmodifiable(chosen),
|
|
);
|
|
}
|
|
|
|
/// Picks the address a phone can dial: IPv4 first, then a routable IPv6
|
|
/// address. Link-local IPv6 needs a zone the URL cannot carry.
|
|
static String? preferredHost(List<String> addresses) {
|
|
final values = [for (final value in addresses) value.trim()];
|
|
for (final value in values) {
|
|
final bytes = _ipv4(value);
|
|
if (bytes != null && bytes[0] != 127) return value;
|
|
}
|
|
for (final value in values) {
|
|
final bytes = _ipv6(value);
|
|
if (bytes == null) continue;
|
|
final loopback =
|
|
bytes.sublist(0, 15).every((byte) => byte == 0) && bytes[15] == 1;
|
|
final linkLocal = bytes[0] == 0xfe && (bytes[1] & 0xc0) == 0x80;
|
|
if (!loopback && !linkLocal) return value;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// Tailscale assigns IPv4 addresses from 100.64.0.0/10.
|
|
static bool isTailscaleIPv4(String value) {
|
|
final bytes = _ipv4(value);
|
|
return bytes != null && bytes[0] == 100 && (bytes[1] & 0xc0) == 0x40;
|
|
}
|
|
|
|
/// The ws(s) endpoint for an advertised HTTP(S) origin, or null when the
|
|
/// value is not a bare origin.
|
|
static Uri? publicEndpoint(String? raw, {String path = '/connect'}) {
|
|
final value = raw?.trim() ?? '';
|
|
if (value.isEmpty || value.length > 200) return null;
|
|
final uri = Uri.tryParse(value);
|
|
if (uri == null ||
|
|
(uri.scheme != 'https' && uri.scheme != 'http') ||
|
|
uri.host.isEmpty ||
|
|
uri.userInfo.isNotEmpty ||
|
|
(uri.path.isNotEmpty && uri.path != '/') ||
|
|
uri.hasQuery ||
|
|
uri.hasFragment) {
|
|
return null;
|
|
}
|
|
return Uri(
|
|
scheme: uri.scheme == 'https' ? 'wss' : 'ws',
|
|
host: uri.host,
|
|
port: uri.hasPort ? uri.port : null,
|
|
path: path,
|
|
);
|
|
}
|
|
|
|
static List<int>? _ipv4(String value) {
|
|
if (!RegExp(r'^\d{1,3}(\.\d{1,3}){3}$').hasMatch(value)) return null;
|
|
try {
|
|
return Uri.parseIPv4Address(value);
|
|
} on FormatException {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static List<int>? _ipv6(String value) {
|
|
if (!value.contains(':') || value.contains('%')) return null;
|
|
try {
|
|
return Uri.parseIPv6Address(value);
|
|
} on FormatException {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
static String? _bare(String? hostname) {
|
|
final value = hostname?.trim().replaceFirst(RegExp(r'\.$'), '') ?? '';
|
|
return value.isEmpty ? null : value;
|
|
}
|
|
|
|
@override
|
|
bool operator ==(Object other) =>
|
|
other is DiscoveredServer &&
|
|
other.name == name &&
|
|
other.endpoint == endpoint &&
|
|
other.passwordRequired == passwordRequired &&
|
|
other.version == version &&
|
|
_sameRoutes(other.addresses);
|
|
|
|
bool _sameRoutes(List<DiscoveredAddress> other) {
|
|
if (other.length != addresses.length) return false;
|
|
for (var i = 0; i < other.length; i++) {
|
|
if (other[i] != addresses[i]) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@override
|
|
int get hashCode =>
|
|
Object.hash(name, endpoint, passwordRequired, version, addresses.length);
|
|
}
|