|
| 1 | +use std::collections::HashSet; |
| 2 | +use std::net::IpAddr; |
| 3 | + |
| 4 | +use serde::{Deserialize, Serialize}; |
| 5 | +use uuid::Uuid; |
| 6 | + |
| 7 | +use crate::dns::config::{DnsBindConfig, DnsUpstreamConfig}; |
| 8 | +use crate::dns::redirect::DNSRedirectRuntimeRule; |
| 9 | +use crate::dns::redirect::DnsRedirectAnswerMode; |
| 10 | +use crate::dns::rule::{DNSRuntimeRule, DomainConfig, FilterResult}; |
| 11 | +use crate::dns::upstream::DnsUpstreamMode; |
| 12 | +use crate::flow::mark::FlowMark; |
| 13 | +use crate::geo::GeoFileCacheKey; |
| 14 | + |
| 15 | +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] |
| 16 | +#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))] |
| 17 | +pub struct CacheRuntimeConfig { |
| 18 | + pub cache_capacity: u32, |
| 19 | + pub cache_ttl: u32, |
| 20 | + pub negative_cache_ttl: u32, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)] |
| 24 | +#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))] |
| 25 | +pub struct DohRuntimeConfig { |
| 26 | + pub listen_port: u16, |
| 27 | + pub http_endpoint: String, |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] |
| 31 | +#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))] |
| 32 | +pub struct RuntimeUpstreamTarget { |
| 33 | + pub mode: DnsUpstreamMode, |
| 34 | + #[cfg_attr(feature = "openapi", schema(value_type = Vec<String>))] |
| 35 | + pub ips: Vec<IpAddr>, |
| 36 | + #[cfg_attr(feature = "openapi", schema(required = true, nullable = true))] |
| 37 | + pub port: Option<u16>, |
| 38 | + pub enable_ip_validation: bool, |
| 39 | +} |
| 40 | + |
| 41 | +impl From<&DnsUpstreamConfig> for RuntimeUpstreamTarget { |
| 42 | + fn from(value: &DnsUpstreamConfig) -> Self { |
| 43 | + Self { |
| 44 | + mode: value.mode.clone(), |
| 45 | + ips: value.ips.clone(), |
| 46 | + port: value.port, |
| 47 | + enable_ip_validation: value.enable_ip_validation.unwrap_or(false), |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl From<RuntimeUpstreamTarget> for DnsUpstreamConfig { |
| 53 | + fn from(value: RuntimeUpstreamTarget) -> Self { |
| 54 | + Self { |
| 55 | + id: Uuid::nil(), |
| 56 | + remark: String::new(), |
| 57 | + mode: value.mode, |
| 58 | + ips: value.ips, |
| 59 | + port: value.port, |
| 60 | + enable_ip_validation: Some(value.enable_ip_validation), |
| 61 | + update_at: 0.0, |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] |
| 67 | +#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))] |
| 68 | +pub struct RuntimeDnsRule { |
| 69 | + pub rule_id: Uuid, |
| 70 | + pub order: u32, |
| 71 | + pub filter: FilterResult, |
| 72 | + pub upstream: RuntimeUpstreamTarget, |
| 73 | + pub bind_config: DnsBindConfig, |
| 74 | + pub mark: FlowMark, |
| 75 | + pub sources: Vec<DomainConfig>, |
| 76 | +} |
| 77 | + |
| 78 | +impl From<DNSRuntimeRule> for RuntimeDnsRule { |
| 79 | + fn from(value: DNSRuntimeRule) -> Self { |
| 80 | + Self { |
| 81 | + rule_id: value.id, |
| 82 | + order: value.index, |
| 83 | + filter: value.filter, |
| 84 | + upstream: RuntimeUpstreamTarget::from(&value.resolve_mode), |
| 85 | + bind_config: value.bind_config, |
| 86 | + mark: value.mark, |
| 87 | + sources: value.source, |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl From<RuntimeDnsRule> for DNSRuntimeRule { |
| 93 | + fn from(value: RuntimeDnsRule) -> Self { |
| 94 | + Self { |
| 95 | + id: value.rule_id, |
| 96 | + name: String::new(), |
| 97 | + index: value.order, |
| 98 | + enable: true, |
| 99 | + filter: value.filter, |
| 100 | + resolve_mode: value.upstream.into(), |
| 101 | + bind_config: value.bind_config, |
| 102 | + mark: value.mark, |
| 103 | + source: value.sources, |
| 104 | + flow_id: 0, |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] |
| 110 | +#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))] |
| 111 | +pub struct RuntimeRedirectRule { |
| 112 | + #[cfg_attr(feature = "openapi", schema(nullable = false))] |
| 113 | + pub redirect_id: Option<Uuid>, |
| 114 | + #[cfg_attr(feature = "openapi", schema(nullable = false))] |
| 115 | + pub dynamic_source_id: Option<String>, |
| 116 | + pub order: u32, |
| 117 | + pub answer_mode: DnsRedirectAnswerMode, |
| 118 | + pub match_rules: Vec<DomainConfig>, |
| 119 | + #[cfg_attr(feature = "openapi", schema(value_type = Vec<String>))] |
| 120 | + pub result_ips: Vec<IpAddr>, |
| 121 | + pub ttl_secs: u32, |
| 122 | +} |
| 123 | + |
| 124 | +impl From<DNSRedirectRuntimeRule> for RuntimeRedirectRule { |
| 125 | + fn from(value: DNSRedirectRuntimeRule) -> Self { |
| 126 | + Self { |
| 127 | + redirect_id: value.redirect_id, |
| 128 | + dynamic_source_id: value.dynamic_redirect_source, |
| 129 | + order: 0, |
| 130 | + answer_mode: value.answer_mode, |
| 131 | + match_rules: value.match_rules, |
| 132 | + result_ips: value.result_info, |
| 133 | + ttl_secs: value.ttl_secs, |
| 134 | + } |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +impl From<RuntimeRedirectRule> for DNSRedirectRuntimeRule { |
| 139 | + fn from(value: RuntimeRedirectRule) -> Self { |
| 140 | + Self { |
| 141 | + redirect_id: value.redirect_id, |
| 142 | + dynamic_redirect_source: value.dynamic_source_id, |
| 143 | + answer_mode: value.answer_mode, |
| 144 | + match_rules: value.match_rules, |
| 145 | + result_info: value.result_ips, |
| 146 | + ttl_secs: value.ttl_secs, |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)] |
| 152 | +#[cfg_attr(feature = "openapi", derive(utoipa::ToSchema))] |
| 153 | +pub struct FlowDnsDesiredState { |
| 154 | + pub flow_id: u32, |
| 155 | + pub dns_rules: Vec<RuntimeDnsRule>, |
| 156 | + pub redirect_rules: Vec<RuntimeRedirectRule>, |
| 157 | + pub cache_runtime: CacheRuntimeConfig, |
| 158 | + #[cfg_attr(feature = "openapi", schema(nullable = false))] |
| 159 | + pub doh_runtime: Option<DohRuntimeConfig>, |
| 160 | +} |
| 161 | + |
| 162 | +#[derive(Debug, Clone, Default, PartialEq, Eq)] |
| 163 | +pub struct FlowDnsDependencies { |
| 164 | + pub geo_keys: HashSet<GeoFileCacheKey>, |
| 165 | + pub upstream_ids: HashSet<Uuid>, |
| 166 | + pub dynamic_redirect_sources: HashSet<String>, |
| 167 | +} |
0 commit comments