clash_config_generator.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // 代理类
  2. class Proxy {
  3. final String name;
  4. final String type;
  5. final String server;
  6. final int port;
  7. final String uuid;
  8. final int udp;
  9. final String? flow;
  10. final String? servername;
  11. final bool? tls;
  12. Proxy({
  13. required this.name,
  14. required this.type,
  15. required this.server,
  16. required this.port,
  17. required this.uuid,
  18. required this.udp,
  19. this.flow,
  20. this.servername,
  21. this.tls,
  22. });
  23. String toYamlString() {
  24. var lines = [
  25. 'name: $name',
  26. 'type: $type',
  27. 'server: $server',
  28. 'port: $port',
  29. 'uuid: $uuid',
  30. 'udp: $udp',
  31. ];
  32. if (flow != null) lines.add('flow: $flow');
  33. if (servername != null) lines.add('servername: $servername');
  34. if (tls != null) lines.add('tls: $tls');
  35. return lines.map((line) => ' - $line').join('\n');
  36. }
  37. }
  38. // 代理组类
  39. class ProxyGroup {
  40. final String name;
  41. final String type;
  42. final List<String> proxies;
  43. ProxyGroup({
  44. required this.name,
  45. required this.type,
  46. required this.proxies,
  47. });
  48. String toYamlString() {
  49. return ' - name: $name\n type: $type\n proxies: [${proxies.join(', ')}]';
  50. }
  51. }
  52. // 主配置类
  53. class YamlConfig {
  54. final int mixedPort;
  55. final bool allowLan;
  56. final String bindAddress;
  57. final String mode;
  58. final String logLevel;
  59. final String externalController;
  60. final List<Proxy> proxies;
  61. final List<ProxyGroup> proxyGroups;
  62. final List<String> rules;
  63. YamlConfig({
  64. required this.mixedPort,
  65. required this.allowLan,
  66. required this.bindAddress,
  67. required this.mode,
  68. required this.logLevel,
  69. required this.externalController,
  70. required this.proxies,
  71. required this.proxyGroups,
  72. required this.rules,
  73. });
  74. String toYamlString() {
  75. return '''mixed-port: $mixedPort
  76. allow-lan: $allowLan
  77. bind-address: '$bindAddress'
  78. mode: $mode
  79. log-level: $logLevel
  80. external-controller: '$externalController'
  81. proxies:
  82. ${proxies.map((proxy) => proxy.toYamlString()).join('\n')}
  83. proxy-groups:
  84. ${proxyGroups.map((group) => group.toYamlString()).join('\n')}
  85. rules:
  86. - ${rules.join('\n- ')}''';
  87. }
  88. }
  89. // void main() {
  90. // // 为示例,这里仅添加了一些代理和代理组。
  91. // var exampleProxies = [
  92. // Proxy(
  93. // name: '香港22-vless',
  94. // type: 'vless',
  95. // server: 'sz.ip2000.top',
  96. // port: 27399,
  97. // uuid: '459b4a80-bd61-4ecd-a26b-e9c1809d9e45',
  98. // udp: 1,
  99. // flow: 'xtls-rprx-vision',
  100. // servername: 'www.amazon.com',
  101. // tls: true,
  102. // ),
  103. // // ... 可以根据需要添加更多代理
  104. // ];
  105. //
  106. // var exampleProxyGroup = ProxyGroup(
  107. // name: 'proxy',
  108. // type: 'select',
  109. // proxies: ['香港22-vless', '香港原生61D'],
  110. // );
  111. //
  112. // var config = YamlConfig(
  113. // mixedPort: 7890,
  114. // allowLan: true,
  115. // bindAddress: '*',
  116. // mode: 'rule',
  117. // logLevel: 'info',
  118. // externalController: '127.0.0.1:9090',
  119. // proxies: exampleProxies,
  120. // proxyGroups: [exampleProxyGroup],
  121. // rules: ['MATCH,用户中心'],
  122. // );
  123. //
  124. // // var fileName = 'config_output.yaml';
  125. // // File(fileName).writeAsStringSync(config.toYamlString());
  126. // // print('YAML config saved to $fileName');
  127. // }