enum ServerService { http, tcp, mqtt, webRtc, droneMqtt, geoServer, pathPlanning, ai, update, stationWeb; bool get isCore => this == http || this == tcp || this == mqtt; Uri defaultAddress({required bool production, required String commonHost}) { return switch (this) { http => Uri( scheme: 'http', host: commonHost, port: production ? 8081 : 59003, ), tcp => Uri( scheme: 'tcp', host: commonHost, port: production ? 9001 : 59004, ), mqtt => Uri( scheme: 'mqtt', host: commonHost, port: production ? 1883 : 59007, ), webRtc => Uri( scheme: 'http', host: commonHost, port: 1985, path: '/rtc/v1/play/', ), droneMqtt => Uri.parse('ws://1.95.137.212:8083/mqtt'), geoServer => Uri.parse('http://1.95.137.212:7900/geoserver'), pathPlanning => Uri.parse('https://servicepathplan.satabot.com/api/path'), ai => Uri.parse('http://1.95.137.212:30009'), update => Uri.parse('http://8.159.134.0:8012/api/update/check'), stationWeb => Uri.parse('https://pvs.satabot.com/'), }; } /// A port uses the shared host. An explicit address can use another host. Uri resolve( String input, { required bool production, required String commonHost, }) { final fallback = defaultAddress( production: production, commonHost: commonHost, ); if (input.isEmpty) return fallback; final port = int.tryParse(input); if (port != null) { if (port < 1 || port > 65535) throw const FormatException('Invalid port'); return fallback.replace(host: commonHost, port: port); } final url = input.contains('://') ? input : '${fallback.scheme}://$input'; final address = Uri.tryParse(url); final authority = url .substring(url.indexOf('://') + 3) .split(RegExp(r'[/#?]')) .first; if (address == null || !ServerConfiguration.isHost(address.host) || address.userInfo.isNotEmpty || address.hasFragment || address.hasQuery || RegExp(r':0+$').hasMatch(authority)) { throw const FormatException('Invalid server address'); } final schemes = switch (this) { tcp => const ['tcp'], mqtt => const ['mqtt'], droneMqtt => const ['ws', 'wss'], _ => const ['http', 'https'], }; if (!schemes.contains(address.scheme) || ((this == tcp || this == mqtt) && address.path.isNotEmpty)) { throw const FormatException('Invalid protocol or path'); } final effectivePort = address.hasPort || address.port != 0 ? address.port : switch (address.scheme) { 'ws' => 80, 'wss' => 443, _ => fallback.port, }; if (effectivePort < 1 || effectivePort > 65535) { throw const FormatException('Invalid port'); } return address.replace( port: effectivePort, path: address.path.isEmpty || address.path == '/' ? fallback.path : address.path, ); } } class ServerConfiguration { static const defaultHost = '1.95.137.212'; final String sharedHost; final Map overrides; final Map _addresses; ServerConfiguration._(this.sharedHost, this.overrides, this._addresses); factory ServerConfiguration({ required bool production, String sharedHost = '', Map overrides = const {}, }) { final host = sharedHost.trim(); if (host.isNotEmpty && !isHost(host)) { throw const FormatException('Invalid shared host'); } final inputs = { for (final entry in overrides.entries) if (entry.value.trim().isNotEmpty) entry.key: entry.value.trim(), }; final effectiveHost = host.isEmpty ? defaultHost : host; return ServerConfiguration._(host, Map.unmodifiable(inputs), { for (final service in ServerService.values) service: service.resolve( inputs[service] ?? '', production: production, commonHost: effectiveHost, ), }); } factory ServerConfiguration.fromJson( Map json, { required bool production, }) => ServerConfiguration( production: production, sharedHost: json['host'] as String? ?? '', overrides: { for (final service in ServerService.values) if (json[service.name] case final String input) service: input, }, ); Uri address(ServerService service) => _addresses[service]!; Map toJson() => { if (sharedHost.isNotEmpty) 'host': sharedHost, for (final entry in overrides.entries) entry.key.name: entry.value, }; static bool isHost(String input) { if (RegExp( r'^[a-zA-Z0-9](?:[a-zA-Z0-9.-]*[a-zA-Z0-9.])?$', ).hasMatch(input)) { return !input.contains('..'); } try { final host = input.startsWith('[') && input.endsWith(']') ? input.substring(1, input.length - 1) : input; return host.contains(':') && Uri.parseIPv6Address(host).length == 16; } on FormatException { return false; } } }