mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-02-04 12:40:07 +00:00
refactor: simplify server & service manipulation (#993)
* refactor: simplify server & service manipulation * update * fix * update for nat, ddns & notification * chore * update cron * update dependencies * use of function iterators * update default dns servers
This commit is contained in:
@@ -19,11 +19,6 @@ var (
|
||||
customDNSServers []string
|
||||
)
|
||||
|
||||
type IP struct {
|
||||
Ipv4Addr string
|
||||
Ipv6Addr string
|
||||
}
|
||||
|
||||
type Provider struct {
|
||||
ctx context.Context
|
||||
ipAddr string
|
||||
@@ -32,7 +27,7 @@ type Provider struct {
|
||||
zone string
|
||||
|
||||
DDNSProfile *model.DDNSProfile
|
||||
IPAddrs *IP
|
||||
IPAddrs *model.IP
|
||||
Setter libdns.RecordSetter
|
||||
}
|
||||
|
||||
@@ -71,7 +66,7 @@ func (provider *Provider) updateDomain(domain string) error {
|
||||
// 当IPv4和IPv6同时成功才算作成功
|
||||
if *provider.DDNSProfile.EnableIPv4 {
|
||||
provider.recordType = getRecordString(true)
|
||||
provider.ipAddr = provider.IPAddrs.Ipv4Addr
|
||||
provider.ipAddr = provider.IPAddrs.IPv4Addr
|
||||
if err = provider.addDomainRecord(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -79,7 +74,7 @@ func (provider *Provider) updateDomain(domain string) error {
|
||||
|
||||
if *provider.DDNSProfile.EnableIPv6 {
|
||||
provider.recordType = getRecordString(false)
|
||||
provider.ipAddr = provider.IPAddrs.Ipv6Addr
|
||||
provider.ipAddr = provider.IPAddrs.IPv6Addr
|
||||
if err = provider.addDomainRecord(); err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -114,11 +109,11 @@ func splitDomainSOA(domain string) (prefix string, zone string, err error) {
|
||||
|
||||
var r *dns.Msg
|
||||
for _, idx := range indexes {
|
||||
m := new(dns.Msg)
|
||||
var m dns.Msg
|
||||
m.SetQuestion(domain[idx:], dns.TypeSOA)
|
||||
|
||||
for _, server := range servers {
|
||||
r, _, err = c.Exchange(m, server)
|
||||
r, _, err = c.Exchange(&m, server)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import (
|
||||
"maps"
|
||||
"math/big"
|
||||
"net/netip"
|
||||
"os"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
@@ -21,7 +20,7 @@ import (
|
||||
var (
|
||||
Json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
DNSServers = []string{"1.1.1.1:53", "223.5.5.5:53"}
|
||||
DNSServers = []string{"8.8.8.8:53", "8.8.4.4:53", "1.1.1.1:53", "1.0.0.1:53"}
|
||||
)
|
||||
|
||||
var ipv4Re = regexp.MustCompile(`(\d*\.).*(\.\d*)`)
|
||||
@@ -71,35 +70,6 @@ func GetIPFromHeader(headerValue string) (string, error) {
|
||||
return ip.String(), nil
|
||||
}
|
||||
|
||||
// SplitIPAddr 传入/分割的v4v6混合地址,返回v4和v6地址与有效地址
|
||||
func SplitIPAddr(v4v6Bundle string) (string, string, string) {
|
||||
ipList := strings.Split(v4v6Bundle, "/")
|
||||
ipv4 := ""
|
||||
ipv6 := ""
|
||||
validIP := ""
|
||||
if len(ipList) > 1 {
|
||||
// 双栈
|
||||
ipv4 = ipList[0]
|
||||
ipv6 = ipList[1]
|
||||
validIP = ipv4
|
||||
} else if len(ipList) == 1 {
|
||||
// 仅ipv4|ipv6
|
||||
if strings.Contains(ipList[0], ":") {
|
||||
ipv6 = ipList[0]
|
||||
validIP = ipv6
|
||||
} else {
|
||||
ipv4 = ipList[0]
|
||||
validIP = ipv4
|
||||
}
|
||||
}
|
||||
return ipv4, ipv6, validIP
|
||||
}
|
||||
|
||||
func IsFileExists(path string) bool {
|
||||
_, err := os.Stat(path)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func GenerateRandomString(n int) (string, error) {
|
||||
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
||||
lettersLength := big.NewInt(int64(len(letters)))
|
||||
@@ -131,13 +101,6 @@ func IfOr[T any](a bool, x, y T) T {
|
||||
return y
|
||||
}
|
||||
|
||||
func IfOrFn[T any](a bool, x, y func() T) T {
|
||||
if a {
|
||||
return x()
|
||||
}
|
||||
return y()
|
||||
}
|
||||
|
||||
func Itoa[T constraints.Integer](i T) string {
|
||||
switch any(i).(type) {
|
||||
case int, int8, int16, int32, int64:
|
||||
|
||||
Reference in New Issue
Block a user