CI / changes (push) Successful in 6s
CI / openapi (push) Successful in 1m2s
CI / go (push) Successful in 27s
CI / docker-web (push) Successful in 1m27s
CI / docker-bird (push) Has been skipped
CI / bird2 (push) Successful in 16s
CI / docker-go (push) Successful in 8m5s
- Added `DohResolverPolicy` schema to OpenAPI documentation, defining policies for domain resolution. - Updated module handling to support multiple DoH profiles via `doh_profile_ids` and introduced `doh_resolver_policy` in the API. - Refactored related functions to accommodate the new DoH profile structure, ensuring backward compatibility with existing `doh_profile_id`. - Enhanced UI components to allow selection and management of DoH profiles and policies in the web interface. - Updated database interactions to handle new fields and ensure proper data normalization.
111 lines
2.6 KiB
Go
111 lines
2.6 KiB
Go
package store
|
|
|
|
import "strings"
|
|
|
|
const (
|
|
DohPolicyPrimaryOnly = "primary_only"
|
|
DohPolicyFailover = "failover"
|
|
DohPolicyUnion = "union"
|
|
)
|
|
|
|
// NormalizeDohResolverPolicy returns a supported resolver policy name.
|
|
func NormalizeDohResolverPolicy(policy string) string {
|
|
switch strings.TrimSpace(policy) {
|
|
case DohPolicyFailover, DohPolicyUnion:
|
|
return strings.TrimSpace(policy)
|
|
default:
|
|
return DohPolicyPrimaryOnly
|
|
}
|
|
}
|
|
|
|
// NormalizeDohProfileIDList deduplicates profile ids preserving order.
|
|
func NormalizeDohProfileIDList(ids []string) []string {
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
seen := make(map[string]struct{}, len(ids))
|
|
out := make([]string, 0, len(ids))
|
|
for _, id := range ids {
|
|
id = strings.TrimSpace(id)
|
|
if id == "" {
|
|
continue
|
|
}
|
|
if _, ok := seen[id]; ok {
|
|
continue
|
|
}
|
|
seen[id] = struct{}{}
|
|
out = append(out, id)
|
|
}
|
|
if len(out) == 0 {
|
|
return nil
|
|
}
|
|
return out
|
|
}
|
|
|
|
// EffectiveDohProfileIDs returns ordered DoH profile ids for a module.
|
|
func (m *Module) EffectiveDohProfileIDs() []string {
|
|
if m == nil {
|
|
return nil
|
|
}
|
|
if ids := NormalizeDohProfileIDList(m.DohProfileIDs); len(ids) > 0 {
|
|
return ids
|
|
}
|
|
if m.DohProfileID != nil {
|
|
if id := strings.TrimSpace(*m.DohProfileID); id != "" {
|
|
return []string{id}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SyncLegacyDohProfileID keeps deprecated doh_profile_id aligned with the first profile.
|
|
func (m *Module) SyncLegacyDohProfileID() {
|
|
if m == nil {
|
|
return
|
|
}
|
|
ids := NormalizeDohProfileIDList(m.DohProfileIDs)
|
|
if len(ids) == 0 {
|
|
m.DohProfileID = nil
|
|
return
|
|
}
|
|
first := ids[0]
|
|
m.DohProfileID = &first
|
|
}
|
|
|
|
// NormalizeModuleDoh fills doh_profile_ids, policy and legacy id from module input.
|
|
func NormalizeModuleDoh(m *Module) {
|
|
if m == nil {
|
|
return
|
|
}
|
|
ids := NormalizeDohProfileIDList(m.DohProfileIDs)
|
|
if len(ids) == 0 && m.DohProfileID != nil {
|
|
if id := strings.TrimSpace(*m.DohProfileID); id != "" {
|
|
ids = []string{id}
|
|
}
|
|
}
|
|
m.DohProfileIDs = ids
|
|
m.DohResolverPolicy = NormalizeDohResolverPolicy(m.DohResolverPolicy)
|
|
m.SyncLegacyDohProfileID()
|
|
}
|
|
|
|
// ApplyModuleDohPatch merges DoH-related fields from patch into mod.
|
|
func ApplyModuleDohPatch(mod *Module, patch *ModulePatch) {
|
|
if mod == nil || patch == nil {
|
|
return
|
|
}
|
|
if patch.DohProfileIDs != nil {
|
|
mod.DohProfileIDs = NormalizeDohProfileIDList(*patch.DohProfileIDs)
|
|
} else if patch.DohProfileID != nil {
|
|
v := strings.TrimSpace(*patch.DohProfileID)
|
|
if v == "" {
|
|
mod.DohProfileIDs = nil
|
|
} else {
|
|
mod.DohProfileIDs = []string{v}
|
|
}
|
|
}
|
|
if patch.DohResolverPolicy != nil {
|
|
mod.DohResolverPolicy = NormalizeDohResolverPolicy(*patch.DohResolverPolicy)
|
|
}
|
|
mod.SyncLegacyDohProfileID()
|
|
}
|