Skip to content

Commit

Permalink
refactor(domains): use publicsuffix to parse (#952)
Browse files Browse the repository at this point in the history
* refactor(domains): use publicsuffix to parse

* fix: incorrect sub domain when sub domain and root domain are the same
  • Loading branch information
WaterLemons2k authored Dec 23, 2023
1 parent e2605ff commit 1576b7b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 59 deletions.
105 changes: 47 additions & 58 deletions config/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import (
"strings"

"github.com/jeessy2/ddns-go/v5/util"
"golang.org/x/net/publicsuffix"
)

// 固定的主域名
var staticMainDomains = []string{"com.cn", "org.cn", "net.cn", "ac.cn", "eu.org", "asso.eu.org", "edu.eu.org", "int.eu.org", "net.eu.org", "cn.eu.org", "pu.ua"}

// Domains Ipv4/Ipv6 domains
type Domains struct {
Ipv4Addr string
Expand All @@ -23,7 +21,9 @@ type Domains struct {

// Domain 域名实体
type Domain struct {
DomainName string
// DomainName 根域名
DomainName string
// SubDomain 子域名
SubDomain string
CustomParams string
UpdateStatus updateStatusType // 更新状态
Expand Down Expand Up @@ -107,66 +107,55 @@ func (domains *Domains) GetNewIp(dnsConf *DnsConfig) {
func checkParseDomains(domainArr []string) (domains []*Domain) {
for _, domainStr := range domainArr {
domainStr = strings.TrimSpace(domainStr)
if domainStr != "" {
domain := &Domain{}

dp := strings.Split(domainStr, ":")
dplen := len(dp)
if dplen == 1 { // 自动识别域名
sp := strings.Split(domainStr, ".")
length := len(sp)
if length <= 1 {
log.Println(domainStr, "域名不正确")
continue
}
// 处理域名
domain.DomainName = sp[length-2] + "." + sp[length-1]
// 如包含在org.cn等顶级域名下,后三个才为用户主域名
for _, staticMainDomain := range staticMainDomains {
// 移除 domain.DomainName 的查询字符串以便与 staticMainDomain 进行比较。
// 查询字符串是 URL ? 后面的部分。
// 查询字符串的存在会导致顶级域名无法与 staticMainDomain 精确匹配,从而被误认为二级域名。
// 示例:"com.cn?param=value" 将被替换为 "com.cn"。
// https://github.com/jeessy2/ddns-go/issues/714
if staticMainDomain == strings.Split(domain.DomainName, "?")[0] {
domain.DomainName = sp[length-3] + "." + domain.DomainName
break
}
}

domainLen := len(domainStr) - len(domain.DomainName)
if domainLen > 0 {
domain.SubDomain = domainStr[:domainLen-1]
} else {
domain.SubDomain = domainStr[:domainLen]
}

} else if dplen == 2 { // 主机记录:域名 格式
sp := strings.Split(dp[1], ".")
length := len(sp)
if length <= 1 {
log.Println(domainStr, "域名不正确")
continue
}
domain.DomainName = dp[1]
domain.SubDomain = dp[0]
} else {
if domainStr == "" {
continue
}

domain := &Domain{}

// qp(queryParts) 从域名中提取自定义参数,如 baidu.com?q=1 => [baidu.com, q=1]
qp := strings.Split(domainStr, "?")
domainStr = qp[0]

// dp(domainParts) 将域名(qp[0])分割为子域名与根域名,如 www:example.cn.eu.org => [www, example.cn.eu.org]
dp := strings.Split(domainStr, ":")

switch len(dp) {
case 1: // 不使用冒号分割,自动识别域名
domainName, err := publicsuffix.EffectiveTLDPlusOne(domainStr)
if err != nil {
log.Println(domainStr, "域名不正确:", err)
continue
}
domain.DomainName = domainName

domainLen := len(domainStr) - len(domainName) - 1
if domainLen > 0 {
domain.SubDomain = domainStr[:domainLen]
}
case 2: // 使用冒号分隔,为 子域名:根域名 格式
sp := strings.Split(dp[1], ".")
if len(sp) <= 1 {
log.Println(domainStr, "域名不正确")
continue
}
domain.DomainName = dp[1]
domain.SubDomain = dp[0]
default:
log.Println(domainStr, "域名不正确")
continue
}

// 参数条件
if strings.Contains(domain.DomainName, "?") {
u, err := url.Parse("http://" + domain.DomainName)
if err != nil {
log.Println(domainStr, "域名解析失败")
continue
}
domain.DomainName = u.Host
domain.CustomParams = u.Query().Encode()
// 参数条件
if len(qp) == 2 {
u, err := url.Parse("http://baidu.com?" + qp[1])
if err != nil {
log.Println(domainStr, "域名解析失败")
continue
}
domains = append(domains, domain)
domain.CustomParams = u.Query().Encode()
}
domains = append(domains, domain)
}
return
}
Expand Down
3 changes: 2 additions & 1 deletion config/domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (

// TestParseDomainArr 测试 parseDomainArr
func TestParseDomainArr(t *testing.T) {
domains := []string{"mydomain.com", "test.mydomain.com", "test2.test.mydomain.com", "mydomain.com.cn",
domains := []string{"mydomain.com", "test.mydomain.com", "test2.test.mydomain.com", "mydomain.com.mydomain.com", "mydomain.com.cn",
"test.mydomain.com.cn", "test:mydomain.com.cn",
"test.mydomain.com?Line=oversea&RecordId=123", "test.mydomain.com.cn?Line=oversea&RecordId=123",
"test2:test.mydomain.com?Line=oversea&RecordId=123"}
result := []Domain{
{DomainName: "mydomain.com", SubDomain: ""},
{DomainName: "mydomain.com", SubDomain: "test"},
{DomainName: "mydomain.com", SubDomain: "test2.test"},
{DomainName: "mydomain.com", SubDomain: "mydomain.com"},
{DomainName: "mydomain.com.cn", SubDomain: ""},
{DomainName: "mydomain.com.cn", SubDomain: "test"},
{DomainName: "mydomain.com.cn", SubDomain: "test"},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.20
require (
github.com/kardianos/service v1.2.2
github.com/wagslane/go-password-validator v0.3.0
golang.org/x/net v0.19.0
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/kardianos/service v1.2.2 h1:ZvePhAHfvo0A7Mftk/tEzqEZ7Q4lgnR8sGz4xu1YX
github.com/kardianos/service v1.2.2/go.mod h1:CIMRFEJVL+0DS1a3Nx06NaMn4Dz63Ng6O7dl0qH0zVM=
github.com/wagslane/go-password-validator v0.3.0 h1:vfxOPzGHkz5S146HDpavl0cw1DSVP061Ry2PX0/ON6I=
github.com/wagslane/go-password-validator v0.3.0/go.mod h1:TI1XJ6T5fRdRnHqHt14pvy1tNVnrwe7m3/f1f2fDphQ=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/sys v0.0.0-20201015000850-e3ed0017c211/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand Down

0 comments on commit 1576b7b

Please sign in to comment.