app.dart 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import 'config_imports.dart';
  2. class AppConfig {
  3. final BasicConfig basicConfig;
  4. final DNSConfig? dnsConfig;
  5. final TunConfig? tunConfig;
  6. final List<ProxyConfig>? proxies;
  7. final List<ProxyGroup>? proxyGroups;
  8. final List<Rule> rules;
  9. AppConfig({
  10. required this.basicConfig,
  11. this.dnsConfig,
  12. this.tunConfig,
  13. this.proxies,
  14. this.proxyGroups,
  15. required this.rules,
  16. });
  17. factory AppConfig.fromMap(Map<String, dynamic> map) {
  18. return AppConfig(
  19. basicConfig: BasicConfig.fromMap(map['basic']),
  20. dnsConfig: map['dns'] != null ? DNSConfig.fromMap(map['dns']) : null,
  21. tunConfig: map['tun'] != null ? TunConfig.fromMap(map['tun']) : null,
  22. proxies: map['proxies'] != null ? List<ProxyConfig>.from(map['proxies'].map((x) => ProxyConfig.fromMap(x))) : null,
  23. proxyGroups: map['proxy-groups'] != null ? List<ProxyGroup>.from(map['proxy-groups'].map((x) => ProxyGroup.fromMap(x))) : null,
  24. rules: List<Rule>.from(map['rules'].map((x) => Rule.fromMap(x))),
  25. );
  26. }
  27. Map<String, dynamic> toJson() {
  28. return {
  29. 'basic': basicConfig.toJson(),
  30. if (dnsConfig != null) 'dns': dnsConfig!.toJson(),
  31. if (tunConfig != null) 'tun': tunConfig!.toJson(),
  32. if (proxies != null) 'proxies': proxies!.map((proxy) => proxy.toJson()).toList(),
  33. if (proxyGroups != null) 'proxy-groups': proxyGroups!.map((group) => group.toJson()).toList(),
  34. 'rules': rules.map((rule) => rule.toJson()).toList(),
  35. };
  36. }
  37. String toYaml() {
  38. var yaml = basicConfig.toYaml();
  39. if (dnsConfig != null) {
  40. yaml += '\n${dnsConfig!.toYaml()}';
  41. }
  42. if (tunConfig != null) {
  43. yaml += '\n${tunConfig!.toYaml()}';
  44. }
  45. // 总是包含 'proxies:' 键,即使列表为空
  46. var proxiesYaml = proxies != null && proxies!.isNotEmpty
  47. ? proxies!.map((proxy) => proxy.toYaml()).join('\n')
  48. : '';
  49. yaml += '\nproxies:\n$proxiesYaml';
  50. // 总是包含 'proxy-groups:' 键,即使列表为空
  51. var proxyGroupsYaml = proxyGroups != null && proxyGroups!.isNotEmpty
  52. ? proxyGroups!.map((group) => group.toYaml()).join('\n')
  53. : '';
  54. yaml += '\nproxy-groups:\n$proxyGroupsYaml';
  55. var rulesYaml = rules.map((rule) => rule.toYaml()).join('\n');
  56. yaml += '\nrules:\n$rulesYaml';
  57. return yaml;
  58. }
  59. }