field.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. use serde_yaml::{Mapping, Value};
  2. use std::collections::HashSet;
  3. pub const HANDLE_FIELDS: [&str; 9] = [
  4. "mode",
  5. "port",
  6. "socks-port",
  7. "mixed-port",
  8. "allow-lan",
  9. "log-level",
  10. "ipv6",
  11. "secret",
  12. "external-controller",
  13. ];
  14. pub const DEFAULT_FIELDS: [&str; 5] = [
  15. "proxies",
  16. "proxy-groups",
  17. "proxy-providers",
  18. "rules",
  19. "rule-providers",
  20. ];
  21. pub const OTHERS_FIELDS: [&str; 31] = [
  22. "dns",
  23. "tun",
  24. "ebpf",
  25. "hosts",
  26. "script",
  27. "profile",
  28. "payload",
  29. "tunnels",
  30. "auto-redir",
  31. "experimental",
  32. "interface-name",
  33. "routing-mark",
  34. "redir-port",
  35. "tproxy-port",
  36. "iptables",
  37. "external-ui",
  38. "bind-address",
  39. "authentication",
  40. "tls", // meta
  41. "sniffer", // meta
  42. "geox-url", // meta
  43. "listeners", // meta
  44. "sub-rules", // meta
  45. "geodata-mode", // meta
  46. "unified-delay", // meta
  47. "tcp-concurrent", // meta
  48. "enable-process", // meta
  49. "find-process-mode", // meta
  50. "skip-auth-prefixes", // meta
  51. "external-controller-tls", // meta
  52. "global-client-fingerprint", // meta
  53. ];
  54. pub fn use_clash_fields() -> Vec<String> {
  55. DEFAULT_FIELDS
  56. .into_iter()
  57. .chain(HANDLE_FIELDS)
  58. .chain(OTHERS_FIELDS)
  59. .map(|s| s.to_string())
  60. .collect()
  61. }
  62. pub fn use_valid_fields(mut valid: Vec<String>) -> Vec<String> {
  63. let others = Vec::from(OTHERS_FIELDS);
  64. valid.iter_mut().for_each(|s| s.make_ascii_lowercase());
  65. valid
  66. .into_iter()
  67. .filter(|s| others.contains(&s.as_str()))
  68. .chain(DEFAULT_FIELDS.iter().map(|s| s.to_string()))
  69. .collect()
  70. }
  71. pub fn use_filter(config: Mapping, filter: &Vec<String>, enable: bool) -> Mapping {
  72. if !enable {
  73. return config;
  74. }
  75. let mut ret = Mapping::new();
  76. for (key, value) in config.into_iter() {
  77. if let Some(key) = key.as_str() {
  78. if filter.contains(&key.to_string()) {
  79. ret.insert(Value::from(key), value);
  80. }
  81. }
  82. }
  83. ret
  84. }
  85. pub fn use_lowercase(config: Mapping) -> Mapping {
  86. let mut ret = Mapping::new();
  87. for (key, value) in config.into_iter() {
  88. if let Some(key_str) = key.as_str() {
  89. let mut key_str = String::from(key_str);
  90. key_str.make_ascii_lowercase();
  91. ret.insert(Value::from(key_str), value);
  92. }
  93. }
  94. ret
  95. }
  96. pub fn use_sort(config: Mapping, enable_filter: bool) -> Mapping {
  97. let mut ret = Mapping::new();
  98. HANDLE_FIELDS
  99. .into_iter()
  100. .chain(OTHERS_FIELDS)
  101. .chain(DEFAULT_FIELDS)
  102. .for_each(|key| {
  103. let key = Value::from(key);
  104. if let Some(value) = config.get(&key) {
  105. ret.insert(key, value.clone());
  106. }
  107. });
  108. if !enable_filter {
  109. let supported_keys: HashSet<&str> = HANDLE_FIELDS
  110. .into_iter()
  111. .chain(OTHERS_FIELDS)
  112. .chain(DEFAULT_FIELDS)
  113. .collect();
  114. let config_keys: HashSet<&str> = config
  115. .keys()
  116. .filter_map(|e| e.as_str())
  117. .into_iter()
  118. .collect();
  119. config_keys.difference(&supported_keys).for_each(|&key| {
  120. let key = Value::from(key);
  121. if let Some(value) = config.get(&key) {
  122. ret.insert(key, value.clone());
  123. }
  124. });
  125. }
  126. ret
  127. }
  128. pub fn use_keys(config: &Mapping) -> Vec<String> {
  129. config
  130. .iter()
  131. .filter_map(|(key, _)| key.as_str())
  132. .map(|s| {
  133. let mut s = s.to_string();
  134. s.make_ascii_lowercase();
  135. s
  136. })
  137. .collect()
  138. }