chore: Create cloudflare dns provider interface and separate it from api

This commit is contained in:
NaiJi 2023-05-25 00:15:58 -03:00
parent 509b2ac7c7
commit 7832094609
3 changed files with 144 additions and 20 deletions

View file

@ -19,10 +19,6 @@ class CloudflareApi extends DnsProviderApi {
final String? customToken; final String? customToken;
@override
RegExp getApiTokenValidation() =>
RegExp(r'\s+|[!$%^&*()@+|~=`{}\[\]:<>?,.\/]');
@override @override
BaseOptions get options { BaseOptions get options {
final BaseOptions options = BaseOptions(baseUrl: rootAddress); final BaseOptions options = BaseOptions(baseUrl: rootAddress);
@ -45,7 +41,6 @@ class CloudflareApi extends DnsProviderApi {
@override @override
String rootAddress = 'https://api.cloudflare.com/client/v4'; String rootAddress = 'https://api.cloudflare.com/client/v4';
@override
Future<GenericResult<bool>> isApiTokenValid(final String token) async { Future<GenericResult<bool>> isApiTokenValid(final String token) async {
bool isValid = false; bool isValid = false;
Response? response; Response? response;
@ -92,27 +87,32 @@ class CloudflareApi extends DnsProviderApi {
); );
} }
@override Future<GenericResult<List<dynamic>>> getZones(final String domain) async {
Future<String?> getZoneId(final String domain) async { List zones = [];
String? zoneId;
late final Response? response;
final Dio client = await getClient(); final Dio client = await getClient();
try { try {
final Response response = await client.get( response = await client.get(
'/zones', '/zones',
queryParameters: {'name': domain}, queryParameters: {'name': domain},
); );
zoneId = response.data['result'][0]['id']; zones = response.data['result'];
} catch (e) { } catch (e) {
print(e); print(e);
GenericResult(
success: false,
data: zones,
code: response?.statusCode,
message: response?.statusMessage,
);
} finally { } finally {
close(client); close(client);
} }
return zoneId; return GenericResult(success: true, data: zones);
} }
@override
Future<GenericResult<void>> removeSimilarRecords({ Future<GenericResult<void>> removeSimilarRecords({
required final ServerDomain domain, required final ServerDomain domain,
final String? ip4, final String? ip4,
@ -151,7 +151,6 @@ class CloudflareApi extends DnsProviderApi {
return GenericResult(success: true, data: null); return GenericResult(success: true, data: null);
} }
@override
Future<List<DnsRecord>> getDnsRecords({ Future<List<DnsRecord>> getDnsRecords({
required final ServerDomain domain, required final ServerDomain domain,
}) async { }) async {
@ -189,7 +188,6 @@ class CloudflareApi extends DnsProviderApi {
return allRecords; return allRecords;
} }
@override
Future<GenericResult<void>> createMultipleDnsRecords({ Future<GenericResult<void>> createMultipleDnsRecords({
required final ServerDomain domain, required final ServerDomain domain,
final String? ip4, final String? ip4,
@ -228,7 +226,6 @@ class CloudflareApi extends DnsProviderApi {
return GenericResult(success: true, data: null); return GenericResult(success: true, data: null);
} }
@override
Future<void> setDnsRecord( Future<void> setDnsRecord(
final DnsRecord record, final DnsRecord record,
final ServerDomain domain, final ServerDomain domain,
@ -249,7 +246,6 @@ class CloudflareApi extends DnsProviderApi {
} }
} }
@override
Future<List<String>> domainList() async { Future<List<String>> domainList() async {
final String url = '$rootAddress/zones'; final String url = '$rootAddress/zones';
List<String> domains = []; List<String> domains = [];
@ -272,7 +268,6 @@ class CloudflareApi extends DnsProviderApi {
return domains; return domains;
} }
@override
Future<GenericResult<List<DesiredDnsRecord>>> validateDnsRecords( Future<GenericResult<List<DesiredDnsRecord>>> validateDnsRecords(
final ServerDomain domain, final ServerDomain domain,
final String ip4, final String ip4,
@ -333,7 +328,6 @@ class CloudflareApi extends DnsProviderApi {
); );
} }
@override
List<DesiredDnsRecord> getDesiredDnsRecords( List<DesiredDnsRecord> getDesiredDnsRecords(
final String? domainName, final String? domainName,
final String? ip4, final String? ip4,
@ -414,4 +408,51 @@ class CloudflareApi extends DnsProviderApi {
), ),
]; ];
} }
List<DnsRecord> getProjectDnsRecords(
final String? domainName,
final String? ip4,
) {
final DnsRecord domainA =
DnsRecord(type: 'A', name: domainName, content: ip4);
final DnsRecord mx = DnsRecord(type: 'MX', name: '@', content: domainName);
final DnsRecord apiA = DnsRecord(type: 'A', name: 'api', content: ip4);
final DnsRecord cloudA = DnsRecord(type: 'A', name: 'cloud', content: ip4);
final DnsRecord gitA = DnsRecord(type: 'A', name: 'git', content: ip4);
final DnsRecord meetA = DnsRecord(type: 'A', name: 'meet', content: ip4);
final DnsRecord passwordA =
DnsRecord(type: 'A', name: 'password', content: ip4);
final DnsRecord socialA =
DnsRecord(type: 'A', name: 'social', content: ip4);
final DnsRecord vpn = DnsRecord(type: 'A', name: 'vpn', content: ip4);
final DnsRecord txt1 = DnsRecord(
type: 'TXT',
name: '_dmarc',
content: 'v=DMARC1; p=none',
ttl: 18000,
);
final DnsRecord txt2 = DnsRecord(
type: 'TXT',
name: domainName,
content: 'v=spf1 a mx ip4:$ip4 -all',
ttl: 18000,
);
return <DnsRecord>[
domainA,
apiA,
cloudA,
gitA,
meetA,
passwordA,
socialA,
mx,
txt1,
txt2,
vpn
];
}
} }

View file

@ -1,4 +1,4 @@
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare.dart'; import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare_api.dart';
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider.dart'; import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider.dart';
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_api_settings.dart'; import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_api_settings.dart';
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_factory.dart'; import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_factory.dart';

View file

@ -1,3 +1,86 @@
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare_api.dart';
import 'package:selfprivacy/logic/providers/dns_providers/dns_provider.dart'; import 'package:selfprivacy/logic/providers/dns_providers/dns_provider.dart';
class CloudflareDnsProvider extends DnsProvider {} class ApiAdapter {
ApiAdapter({final bool isWithToken = true})
: _api = CloudflareApi(
isWithToken: isWithToken,
);
CloudflareApi api({final bool getInitialized = true}) => getInitialized
? _api
: CloudflareApi(
isWithToken: false,
);
final CloudflareApi _api;
}
class CloudflareDnsProvider extends DnsProvider {
CloudflareDnsProvider() : _adapter = ApiAdapter();
CloudflareDnsProvider.load(
final bool isAuthotized,
) : _adapter = ApiAdapter(
isWithToken: isAuthotized,
);
ApiAdapter _adapter;
@override
Future<GenericResult<bool>> tryInitApiByToken(final String token) async {
final api = _adapter.api(getInitialized: false);
final result = await api.isApiTokenValid(token);
if (!result.data || !result.success) {
return result;
}
_adapter = ApiAdapter(isWithToken: true);
return result;
}
@override
Future<GenericResult<String?>> getZoneId(final String domain) async {
String? id;
final result = await _adapter.api().getZones(domain);
if (result.data.isEmpty || !result.success) {
return GenericResult(
success: result.success,
data: id,
code: result.code,
message: result.message,
);
}
id = result.data[0]['id'];
return GenericResult(success: true, data: id);
}
@override
Future<GenericResult<void>> removeDomainRecords({
required final ServerDomain domain,
final String? ip4,
}) async {}
Future<GenericResult<List<DnsRecord>>> getDnsRecords({
required final ServerDomain domain,
});
Future<GenericResult<void>> createDomainRecords({
required final ServerDomain domain,
final String? ip4,
});
Future<GenericResult<void>> setDnsRecord(
final DnsRecord record,
final ServerDomain domain,
);
Future<GenericResult<List<String>>> domainList();
Future<GenericResult<List<DesiredDnsRecord>>> validateDnsRecords(
final ServerDomain domain,
final String ip4,
final String dkimPublicKey,
);
List<DesiredDnsRecord> getDesiredDnsRecords(
final String? domainName,
final String? ip4,
final String? dkimPublicKey,
);
}