Skip to content

Commit

Permalink
feat: nameserver-policy support multiple keys
Browse files Browse the repository at this point in the history
e.g.,
  nameserver-policy: #   'www.baidu.com': '114.114.114.114'
    #   '+.internal.crop.com': '10.0.0.1'
    "geosite:cn,private,apple":
      - https://doh.pub/dns-query
      - https://dns.alidns.com/dns-query
    "www.baidu.com,+.google.cn":
      - 223.5.5.5
      - 1.1.1.1
  • Loading branch information
Larvan2 committed Mar 12, 2023
1 parent 074fee2 commit 0a6c848
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
29 changes: 27 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"container/list"
"errors"
"fmt"

"net"
"net/netip"
"net/url"
"os"
"regexp"
"runtime"
"strconv"
"strings"
Expand Down Expand Up @@ -985,8 +985,33 @@ func parsePureDNSServer(server string) string {
}
func parseNameServerPolicy(nsPolicy map[string]any, preferH3 bool) (map[string][]dns.NameServer, error) {
policy := map[string][]dns.NameServer{}
updatedPolicy := make(map[string]interface{})
re := regexp.MustCompile(`[a-zA-Z0-9\-]+\.[a-zA-Z]{2,}(\.[a-zA-Z]{2,})?`)

for k, v := range nsPolicy {
if strings.Contains(k, "geosite:") {
subkeys := strings.Split(k, ":")
subkeys = subkeys[1:]
subkeys = strings.Split(subkeys[0], ",")
//log.Infoln("subkeys:%+v", subkeys)
for _, subkey := range subkeys {
newKey := "geosite:" + subkey
//log.Infoln("newKey:%+v", newKey)
updatedPolicy[newKey] = v
}
} else if re.MatchString(k) {
subkeys := strings.Split(k, ",")
//log.Infoln("subkeys:%+v", subkeys)
for _, subkey := range subkeys {
updatedPolicy[subkey] = v
}
} else {
updatedPolicy[k] = v
}
}
//log.Infoln("updatedPolicy:%+v", updatedPolicy)

for domain, server := range nsPolicy {
for domain, server := range updatedPolicy {

servers, err := utils.ToStringSlice(server)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions docs/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,13 @@ dns:
# - '+.youtube.com'

# 配置查询域名使用的 DNS 服务器
nameserver-policy: # 'www.baidu.com': '114.114.114.114'
nameserver-policy:
# 'www.baidu.com': '114.114.114.114'
# '+.internal.crop.com': '10.0.0.1'
"geosite:cn":
"geosite:cn,private,apple":
- https://doh.pub/dns-query
- https://dns.alidns.com/dns-query
"www.baidu.com": [https://doh.pub/dns-query, https://dns.alidns.com/dns-query]
"www.baidu.com,+.google.cn": [223.5.5.5, https://dns.alidns.com/dns-query]

proxies: # socks5
- name: "socks"
Expand Down

0 comments on commit 0a6c848

Please sign in to comment.