data_upstream_server.rs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2025 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. use crate::{smartdns, DnsServerType};
  19. #[derive(Debug, Clone)]
  20. pub struct UpstreamServerInfo {
  21. pub host: String,
  22. pub ip: String,
  23. pub port: u16,
  24. pub server_type: DnsServerType,
  25. pub total_query_count: u64,
  26. pub total_query_success: u64,
  27. pub total_query_recv_count: u64,
  28. pub query_success_rate: f64,
  29. pub avg_time: f64,
  30. pub status: String,
  31. pub security: String,
  32. }
  33. impl UpstreamServerInfo {
  34. pub fn get_all() -> Result<Vec<UpstreamServerInfo>, Box<dyn std::error::Error>> {
  35. let mut servers = Vec::new();
  36. smartdns::DnsUpstreamServer::get_server_list()?
  37. .iter()
  38. .for_each(|server| {
  39. let stats = server.get_server_stats();
  40. let status = if stats.get_query_total() == 0 {
  41. "Unknown"
  42. } else if server.is_server_alive() {
  43. "Normal"
  44. } else {
  45. "Abnormal"
  46. };
  47. let security_status = server.get_server_security_status();
  48. servers.push(UpstreamServerInfo {
  49. host: server.get_host(),
  50. ip: server.get_ip(),
  51. port: server.get_port(),
  52. server_type: server.get_type(),
  53. total_query_count: stats.get_query_total(),
  54. total_query_recv_count: stats.get_query_recv(),
  55. total_query_success: stats.get_query_success(),
  56. query_success_rate: stats.get_success_rate(),
  57. avg_time: stats.get_query_avg_time(),
  58. status: status.to_string(),
  59. security: security_status.to_string(),
  60. });
  61. });
  62. Ok(servers)
  63. }
  64. }