From a690fb5089ec74e497831714f428c6e2404a11f5 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Mon, 5 Jun 2023 09:46:42 -0300 Subject: [PATCH 1/3] fix: Force JSON content-type for REST API requests --- lib/logic/api_maps/rest_maps/backblaze.dart | 6 +++++- .../rest_maps/dns_providers/cloudflare/cloudflare.dart | 6 +++++- lib/logic/api_maps/rest_maps/dns_providers/desec/desec.dart | 6 +++++- .../server_providers/digital_ocean/digital_ocean.dart | 6 +++++- .../rest_maps/server_providers/hetzner/hetzner.dart | 6 +++++- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/lib/logic/api_maps/rest_maps/backblaze.dart b/lib/logic/api_maps/rest_maps/backblaze.dart index 8ea94803..59292775 100644 --- a/lib/logic/api_maps/rest_maps/backblaze.dart +++ b/lib/logic/api_maps/rest_maps/backblaze.dart @@ -30,7 +30,11 @@ class BackblazeApi extends ApiMap { @override BaseOptions get options { - final BaseOptions options = BaseOptions(baseUrl: rootAddress); + final BaseOptions options = BaseOptions( + baseUrl: rootAddress, + contentType: Headers.jsonContentType, + responseType: ResponseType.json, + ); if (isWithToken) { final BackblazeCredential? backblazeCredential = getIt().backblazeCredential; diff --git a/lib/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare.dart b/lib/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare.dart index eb22e4d8..f594029f 100644 --- a/lib/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare.dart +++ b/lib/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare.dart @@ -26,7 +26,11 @@ class CloudflareApi extends DnsProviderApi { @override BaseOptions get options { - final BaseOptions options = BaseOptions(baseUrl: rootAddress); + final BaseOptions options = BaseOptions( + baseUrl: rootAddress, + contentType: Headers.jsonContentType, + responseType: ResponseType.json, + ); if (isWithToken) { final String? token = getIt().dnsProviderKey; assert(token != null); diff --git a/lib/logic/api_maps/rest_maps/dns_providers/desec/desec.dart b/lib/logic/api_maps/rest_maps/dns_providers/desec/desec.dart index c1a8f43b..e8192cb9 100644 --- a/lib/logic/api_maps/rest_maps/dns_providers/desec/desec.dart +++ b/lib/logic/api_maps/rest_maps/dns_providers/desec/desec.dart @@ -26,7 +26,11 @@ class DesecApi extends DnsProviderApi { @override BaseOptions get options { - final BaseOptions options = BaseOptions(baseUrl: rootAddress); + final BaseOptions options = BaseOptions( + baseUrl: rootAddress, + contentType: Headers.jsonContentType, + responseType: ResponseType.json, + ); if (isWithToken) { final String? token = getIt().dnsProviderKey; assert(token != null); diff --git a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean.dart b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean.dart index 21861dd8..81b7a9fb 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/digital_ocean/digital_ocean.dart @@ -36,7 +36,11 @@ class DigitalOceanApi extends ServerProviderApi with VolumeProviderApi { @override BaseOptions get options { - final BaseOptions options = BaseOptions(baseUrl: rootAddress); + final BaseOptions options = BaseOptions( + baseUrl: rootAddress, + contentType: Headers.jsonContentType, + responseType: ResponseType.json, + ); if (isWithToken) { final String? token = getIt().serverProviderKey; assert(token != null); diff --git a/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart b/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart index c2228030..372722fa 100644 --- a/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart +++ b/lib/logic/api_maps/rest_maps/server_providers/hetzner/hetzner.dart @@ -37,7 +37,11 @@ class HetznerApi extends ServerProviderApi with VolumeProviderApi { @override BaseOptions get options { - final BaseOptions options = BaseOptions(baseUrl: rootAddress); + final BaseOptions options = BaseOptions( + baseUrl: rootAddress, + contentType: Headers.jsonContentType, + responseType: ResponseType.json, + ); if (isWithToken) { final String? token = getIt().serverProviderKey; assert(token != null); From 29cbf702e5c22b899e4860626f0291ac5bcf89fe Mon Sep 17 00:00:00 2001 From: NaiJi Date: Mon, 5 Jun 2023 11:18:41 -0300 Subject: [PATCH 2/3] fix: Remove unneded DNS check depending on CLOUDFLARE --- .../server_installation_repository.dart | 53 +++++-------------- 1 file changed, 13 insertions(+), 40 deletions(-) diff --git a/lib/logic/cubit/server_installation/server_installation_repository.dart b/lib/logic/cubit/server_installation/server_installation_repository.dart index bee112f9..b3b80b3f 100644 --- a/lib/logic/cubit/server_installation/server_installation_repository.dart +++ b/lib/logic/cubit/server_installation/server_installation_repository.dart @@ -23,7 +23,6 @@ import 'package:selfprivacy/logic/models/hive/server_domain.dart'; import 'package:selfprivacy/logic/models/hive/user.dart'; import 'package:selfprivacy/logic/models/json/device_token.dart'; import 'package:selfprivacy/logic/models/json/dns_records.dart'; -import 'package:selfprivacy/logic/models/message.dart'; import 'package:selfprivacy/logic/models/server_basic_info.dart'; import 'package:selfprivacy/logic/models/server_type.dart'; import 'package:selfprivacy/ui/helpers/modals.dart'; @@ -208,46 +207,20 @@ class ServerInstallationRepository { final String? ip4, final Map skippedMatches, ) async { - final List addresses = [ - '$domainName', - 'api.$domainName', - 'cloud.$domainName', - 'meet.$domainName', - 'password.$domainName' - ]; - final Map matches = {}; - - for (final String address in addresses) { - if (skippedMatches[address] ?? false) { - matches[address] = true; - continue; - } - final List? lookupRecordRes = await DnsUtils.lookupRecord( - address, - RRecordType.A, - provider: DnsApiProvider.CLOUDFLARE, - ); - getIt.get().addMessage( - Message( - text: - 'DnsLookup: address: $address, $RRecordType, provider: CLOUDFLARE, ip4: $ip4', - ), - ); - getIt.get().addMessage( - Message( - text: - 'DnsLookup: ${lookupRecordRes == null ? 'empty' : (lookupRecordRes[0].data != ip4 ? 'wrong ip4' : 'right ip4')}', - ), - ); - if (lookupRecordRes == null || - lookupRecordRes.isEmpty || - lookupRecordRes[0].data != ip4) { - matches[address] = false; - } else { - matches[address] = true; - } - } + await InternetAddress.lookup(domainName!).then( + (final records) { + for (final record in records) { + if (skippedMatches[record.host] ?? false) { + matches[record.host] = true; + continue; + } + if (record.address == ip4!) { + matches[record.host] = true; + } + } + }, + ); return matches; } From 53c56e6bd443d712616df060d824176833c600ec Mon Sep 17 00:00:00 2001 From: NaiJi Date: Mon, 5 Jun 2023 12:14:54 -0300 Subject: [PATCH 3/3] feat: Remove basic_utils dependency Replace it with InternetAddress from dart:io --- .../server_installation_repository.dart | 16 +++++++++------- pubspec.lock | 8 -------- pubspec.yaml | 1 - 3 files changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/logic/cubit/server_installation/server_installation_repository.dart b/lib/logic/cubit/server_installation/server_installation_repository.dart index b3b80b3f..c138a340 100644 --- a/lib/logic/cubit/server_installation/server_installation_repository.dart +++ b/lib/logic/cubit/server_installation/server_installation_repository.dart @@ -1,6 +1,5 @@ import 'dart:io'; -import 'package:basic_utils/basic_utils.dart'; import 'package:device_info_plus/device_info_plus.dart'; import 'package:dio/dio.dart'; import 'package:easy_localization/easy_localization.dart'; @@ -451,15 +450,18 @@ class ServerInstallationRepository { } Future getServerIpFromDomain(final ServerDomain serverDomain) async { - final List? lookup = await DnsUtils.lookupRecord( - serverDomain.domainName, - RRecordType.A, - provider: DnsApiProvider.CLOUDFLARE, + String? domain; + await InternetAddress.lookup(serverDomain.domainName).then( + (final records) { + for (final record in records) { + domain = record.address; + } + }, ); - if (lookup == null || lookup.isEmpty) { + if (domain == null || domain!.isEmpty) { throw IpNotFoundException('No IP found for domain $serverDomain'); } - return lookup[0].data; + return domain!; } Future getDeviceName() async { diff --git a/pubspec.lock b/pubspec.lock index bd81200b..d812a890 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -73,14 +73,6 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.0" - basic_utils: - dependency: "direct main" - description: - name: basic_utils - sha256: "8815477fcf58499e42326bd858e391442425fa57db9a45e48e15224c62049262" - url: "https://pub.dev" - source: hosted - version: "5.5.4" bloc: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b0554aba..4e5e35d7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,7 +11,6 @@ dependencies: animations: ^2.0.7 auto_route: ^7.3.2 auto_size_text: ^3.0.0 - basic_utils: ^5.5.4 crypt: ^4.3.0 cubit_form: ^2.0.1 device_info_plus: ^9.0.2