2022-10-19 22:47:34 +02:00
|
|
|
// ignore_for_file: use_build_context_synchronously
|
|
|
|
|
2022-10-19 18:34:11 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-10-19 22:47:34 +02:00
|
|
|
import 'package:provider/provider.dart';
|
2022-10-19 18:34:11 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
|
|
|
|
|
|
|
import 'package:adguard_home_manager/widgets/custom_switch_list_tile.dart';
|
|
|
|
|
2023-05-24 20:40:45 +02:00
|
|
|
import 'package:adguard_home_manager/providers/dns_provider.dart';
|
2022-10-19 22:47:34 +02:00
|
|
|
import 'package:adguard_home_manager/classes/process_modal.dart';
|
|
|
|
import 'package:adguard_home_manager/functions/snackbar.dart';
|
|
|
|
import 'package:adguard_home_manager/providers/app_config_provider.dart';
|
2022-10-19 20:26:40 +02:00
|
|
|
|
2022-10-19 18:34:11 +02:00
|
|
|
class PrivateReverseDnsServersScreen extends StatefulWidget {
|
2023-05-24 20:40:45 +02:00
|
|
|
const PrivateReverseDnsServersScreen({Key? key}) : super(key: key);
|
2022-10-19 18:34:11 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<PrivateReverseDnsServersScreen> createState() => _PrivateReverseDnsServersScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PrivateReverseDnsServersScreenState extends State<PrivateReverseDnsServersScreen> {
|
2022-10-19 20:26:40 +02:00
|
|
|
List<String> defaultReverseResolvers = [];
|
2022-10-19 18:34:11 +02:00
|
|
|
bool editReverseResolvers = false;
|
2023-10-21 19:02:52 +02:00
|
|
|
List<Map<String, dynamic>> reverseResolversControllers = [];
|
2022-10-19 18:34:11 +02:00
|
|
|
bool usePrivateReverseDnsResolvers = false;
|
|
|
|
bool enableReverseResolve = false;
|
|
|
|
|
2022-10-19 21:57:48 +02:00
|
|
|
bool validValues = false;
|
|
|
|
|
|
|
|
void validateAddress(Map<String, dynamic> item ,String value) {
|
|
|
|
RegExp ipAddress = RegExp(r'(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6}|:)|(?::(?:(?::[a-fA-F\d]{1,4}){0,5}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,7}|:)))(?:%[0-9a-zA-Z]{1,})?$)');
|
|
|
|
RegExp domain = RegExp(r'^((http|https|tls|udp|tcp|quic|sdns):\/\/)?([a-z0-9|-]+\.)*[a-z0-9|-]+\.[a-z]+$');
|
|
|
|
if (ipAddress.hasMatch(value) == true || domain.hasMatch(value) == true) {
|
|
|
|
setState(() => item['error'] = null);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => item['error'] = AppLocalizations.of(context)!.invalidIpDomain);
|
|
|
|
}
|
|
|
|
|
|
|
|
checkDataValid();
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkDataValid() {
|
|
|
|
if (
|
|
|
|
(
|
|
|
|
editReverseResolvers == true &&
|
|
|
|
reverseResolversControllers.every((element) => element['controller'].text != '') &&
|
|
|
|
reverseResolversControllers.every((element) => element['error'] == null)
|
|
|
|
) == true
|
|
|
|
) {
|
|
|
|
setState(() => validValues = true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
setState(() => validValues = false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 20:26:40 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
2023-05-24 20:40:45 +02:00
|
|
|
final dnsProvider = Provider.of<DnsProvider>(context, listen: false);
|
|
|
|
|
|
|
|
for (var item in dnsProvider.dnsInfo!.defaultLocalPtrUpstreams) {
|
2022-10-19 20:26:40 +02:00
|
|
|
defaultReverseResolvers.add(item);
|
|
|
|
}
|
2023-10-21 19:02:52 +02:00
|
|
|
if (dnsProvider.dnsInfo!.localPtrUpstreams.isEmpty) {
|
|
|
|
reverseResolversControllers.add({
|
|
|
|
'controller': TextEditingController(),
|
|
|
|
'error': null
|
|
|
|
});
|
|
|
|
}
|
2023-05-24 20:40:45 +02:00
|
|
|
for (var item in dnsProvider.dnsInfo!.localPtrUpstreams) {
|
2022-10-19 20:26:40 +02:00
|
|
|
final controller = TextEditingController();
|
|
|
|
controller.text = item;
|
2023-10-21 19:02:52 +02:00
|
|
|
reverseResolversControllers.add({
|
2022-10-19 21:57:48 +02:00
|
|
|
'controller': controller,
|
|
|
|
'error': null
|
2023-10-21 19:02:52 +02:00
|
|
|
});
|
2022-10-19 20:26:40 +02:00
|
|
|
}
|
2023-05-24 20:40:45 +02:00
|
|
|
if (dnsProvider.dnsInfo!.localPtrUpstreams.isNotEmpty) {
|
2022-10-19 20:26:40 +02:00
|
|
|
editReverseResolvers = true;
|
|
|
|
}
|
2023-05-24 20:40:45 +02:00
|
|
|
usePrivateReverseDnsResolvers = dnsProvider.dnsInfo!.usePrivatePtrResolvers;
|
2023-10-08 14:22:03 +02:00
|
|
|
enableReverseResolve = dnsProvider.dnsInfo!.resolveClients ?? false;
|
2022-10-19 21:57:48 +02:00
|
|
|
validValues = true;
|
2022-10-19 20:26:40 +02:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
2022-10-19 18:34:11 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2023-05-24 20:40:45 +02:00
|
|
|
final dnsProvider = Provider.of<DnsProvider>(context);
|
2022-10-19 22:47:34 +02:00
|
|
|
final appConfigProvider = Provider.of<AppConfigProvider>(context);
|
|
|
|
|
|
|
|
void saveData() async {
|
|
|
|
ProcessModal processModal = ProcessModal(context: context);
|
|
|
|
processModal.open(AppLocalizations.of(context)!.savingConfig);
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
final result = await dnsProvider.savePrivateReverseServersConfig(
|
|
|
|
editReverseResolvers == true
|
|
|
|
? {
|
|
|
|
"local_ptr_upstreams": List<String>.from(reverseResolversControllers.map((e) => e['controller'].text)),
|
|
|
|
"use_private_ptr_resolvers": usePrivateReverseDnsResolvers,
|
|
|
|
"resolve_clients": enableReverseResolve
|
|
|
|
} : {
|
|
|
|
"use_private_ptr_resolvers": usePrivateReverseDnsResolvers,
|
|
|
|
"resolve_clients": enableReverseResolve
|
|
|
|
}
|
|
|
|
);
|
2022-10-19 22:47:34 +02:00
|
|
|
|
|
|
|
processModal.close();
|
|
|
|
|
2023-05-25 15:06:21 +02:00
|
|
|
if (result['success'] == true) {
|
2022-10-19 22:47:34 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.dnsConfigSaved,
|
|
|
|
color: Colors.green
|
|
|
|
);
|
|
|
|
}
|
2023-05-25 15:06:21 +02:00
|
|
|
else if (result['success'] == false && result['error'] == 400) {
|
2022-10-19 22:47:34 +02:00
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.someValueNotValid,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
showSnacbkar(
|
|
|
|
appConfigProvider: appConfigProvider,
|
|
|
|
label: AppLocalizations.of(context)!.dnsConfigNotSaved,
|
|
|
|
color: Colors.red
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 18:34:11 +02:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: Text(AppLocalizations.of(context)!.privateReverseDnsServers),
|
2022-10-19 21:57:48 +02:00
|
|
|
actions: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: validValues == true
|
2022-10-19 22:47:34 +02:00
|
|
|
? () => saveData()
|
2022-10-19 21:57:48 +02:00
|
|
|
: null,
|
|
|
|
icon: const Icon(Icons.save_rounded),
|
|
|
|
tooltip: AppLocalizations.of(context)!.save,
|
|
|
|
),
|
|
|
|
const SizedBox(width: 10)
|
|
|
|
],
|
2022-10-19 18:34:11 +02:00
|
|
|
),
|
|
|
|
body: ListView(
|
|
|
|
padding: const EdgeInsets.only(top: 10),
|
|
|
|
children: [
|
|
|
|
Card(
|
|
|
|
margin: const EdgeInsets.only(
|
2022-11-05 03:56:04 +01:00
|
|
|
left: 16, right: 16, bottom: 10
|
2022-10-19 18:34:11 +02:00
|
|
|
),
|
|
|
|
child: Padding(
|
2022-11-05 03:56:04 +01:00
|
|
|
padding: const EdgeInsets.all(16),
|
2022-10-19 18:34:11 +02:00
|
|
|
child: Row(
|
|
|
|
children: [
|
2022-11-05 01:09:09 +01:00
|
|
|
Icon(
|
|
|
|
Icons.info_rounded,
|
2022-11-05 02:35:35 +01:00
|
|
|
color: Theme.of(context).listTileTheme.iconColor,
|
2022-11-05 01:09:09 +01:00
|
|
|
),
|
2022-11-05 03:56:04 +01:00
|
|
|
const SizedBox(width: 16),
|
2022-11-05 01:09:09 +01:00
|
|
|
Flexible(
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.privateReverseDnsServersDescription,
|
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurface
|
|
|
|
),
|
|
|
|
)
|
2022-10-19 18:34:11 +02:00
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (editReverseResolvers == false) ...[
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(20),
|
|
|
|
child: Text(
|
|
|
|
"${AppLocalizations.of(context)!.reverseDnsDefault}:\n\n${defaultReverseResolvers.map((item) => item).join(', ').toString().replaceAll(RegExp(r'\(|\)'), '')}",
|
|
|
|
textAlign: TextAlign.center,
|
2023-01-29 21:52:37 +01:00
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
2022-10-19 18:34:11 +02:00
|
|
|
fontSize: 16
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 10, bottom: 20),
|
|
|
|
child: Row(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: [
|
|
|
|
ElevatedButton.icon(
|
2022-10-19 21:57:48 +02:00
|
|
|
onPressed: () {
|
|
|
|
setState(() => editReverseResolvers = true);
|
|
|
|
checkDataValid();
|
|
|
|
},
|
2022-10-19 18:34:11 +02:00
|
|
|
icon: const Icon(Icons.edit),
|
|
|
|
label: Text(AppLocalizations.of(context)!.edit)
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
if (editReverseResolvers == true) ...[
|
|
|
|
const SizedBox(height: 20),
|
|
|
|
...reverseResolversControllers.map((c) => Padding(
|
2023-10-21 19:02:52 +02:00
|
|
|
padding: const EdgeInsets.only(
|
2022-11-05 03:56:04 +01:00
|
|
|
left: 16, right: 6, bottom: 20
|
2022-10-19 18:34:11 +02:00
|
|
|
),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
2023-05-01 02:50:42 +02:00
|
|
|
Expanded(
|
2022-10-19 18:34:11 +02:00
|
|
|
child: TextFormField(
|
2022-10-19 21:57:48 +02:00
|
|
|
controller: c['controller'],
|
|
|
|
onChanged: (value) => validateAddress(c, value),
|
2022-10-19 18:34:11 +02:00
|
|
|
decoration: InputDecoration(
|
|
|
|
prefixIcon: const Icon(Icons.dns_rounded),
|
|
|
|
border: const OutlineInputBorder(
|
|
|
|
borderRadius: BorderRadius.all(
|
|
|
|
Radius.circular(10)
|
|
|
|
)
|
|
|
|
),
|
2022-10-19 21:57:48 +02:00
|
|
|
errorText: c['error'],
|
2022-10-19 18:34:11 +02:00
|
|
|
labelText: AppLocalizations.of(context)!.serverAddress,
|
|
|
|
)
|
|
|
|
),
|
|
|
|
),
|
2023-05-01 02:50:42 +02:00
|
|
|
const SizedBox(width: 8),
|
2022-10-19 18:34:11 +02:00
|
|
|
IconButton(
|
2022-10-19 21:57:48 +02:00
|
|
|
onPressed: () {
|
|
|
|
setState(() => reverseResolversControllers = reverseResolversControllers.where((con) => con != c).toList());
|
|
|
|
checkDataValid();
|
|
|
|
},
|
2022-10-19 18:34:11 +02:00
|
|
|
icon: const Icon(Icons.remove_circle_outline)
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)),
|
|
|
|
if (reverseResolversControllers.isEmpty) Padding(
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
left: 20, right: 20, bottom: 20
|
|
|
|
),
|
|
|
|
child: Center(
|
|
|
|
child: Text(
|
|
|
|
AppLocalizations.of(context)!.noServerAddressesAdded,
|
|
|
|
textAlign: TextAlign.center,
|
2023-01-29 21:52:37 +01:00
|
|
|
style: TextStyle(
|
|
|
|
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
2022-10-19 18:34:11 +02:00
|
|
|
fontSize: 16
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(bottom: 20),
|
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
ElevatedButton.icon(
|
2022-10-19 21:57:48 +02:00
|
|
|
onPressed: () {
|
|
|
|
setState(() => reverseResolversControllers.add({
|
|
|
|
'controller': TextEditingController(),
|
|
|
|
'error': null
|
|
|
|
}));
|
|
|
|
checkDataValid();
|
|
|
|
},
|
2022-10-19 18:34:11 +02:00
|
|
|
icon: const Icon(Icons.add),
|
|
|
|
label: Text(AppLocalizations.of(context)!.addItem)
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
CustomSwitchListTile(
|
|
|
|
value: usePrivateReverseDnsResolvers,
|
|
|
|
onChanged: (value) => setState(() => usePrivateReverseDnsResolvers = value),
|
|
|
|
title: AppLocalizations.of(context)!.usePrivateReverseDnsResolvers,
|
|
|
|
subtitle: AppLocalizations.of(context)!.usePrivateReverseDnsResolversDescription
|
|
|
|
),
|
|
|
|
CustomSwitchListTile(
|
|
|
|
value: enableReverseResolve,
|
|
|
|
onChanged: (value) => setState(() => enableReverseResolve = value),
|
|
|
|
title: AppLocalizations.of(context)!.enableReverseResolving,
|
|
|
|
subtitle: AppLocalizations.of(context)!.enableReverseResolvingDescription
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|