1
0
mirror of https://github.com/fumiama/terasu.git synced 2026-06-05 01:00:23 +08:00

feat(dns): add lookup host fallback

This commit is contained in:
源文雨
2024-04-21 01:11:09 +09:00
parent ecec5a9e4d
commit c8c0088d72
2 changed files with 14 additions and 14 deletions

View File

@@ -21,8 +21,8 @@ var DefaultDialer = net.Dialer{
}
type dnsstat struct {
A string
E bool
a string
e bool
}
type DNSList struct {
@@ -32,14 +32,14 @@ type DNSList struct {
}
type DNSConfig struct {
Servers map[string][]string // Servers map[dot.com]ip:ports
Fallbacks map[string][]string // Fallbacks map[domain]ips
Servers map[string][]string `yaml:"Servers"` // Servers map[dot.com]ip:ports
Fallbacks map[string][]string `yaml:"Fallbacks"` // Fallbacks map[domain]ips
}
// hasrecord no lock, use under lock
func hasrecord(lst []*dnsstat, a string) bool {
for _, addr := range lst {
if addr.A == a {
if addr.a == a {
return true
}
}
@@ -117,12 +117,12 @@ func (ds *DNSList) DialContext(ctx context.Context, dialer *net.Dialer, firstFra
var conn net.Conn
for host, addrs := range ds.m {
for _, addr := range addrs {
if !addr.E {
if !addr.e {
continue
}
conn, err = dialer.DialContext(ctx, "tcp", addr.A)
conn, err = dialer.DialContext(ctx, "tcp", addr.a)
if err != nil {
addr.E = false // no need to acquire write lock
addr.e = false // no need to acquire write lock
continue
}
tlsConn = tls.Client(conn, &tls.Config{ServerName: host})
@@ -131,7 +131,7 @@ func (ds *DNSList) DialContext(ctx context.Context, dialer *net.Dialer, firstFra
return
}
_ = tlsConn.Close()
addr.E = false // no need to acquire write lock
addr.e = false // no need to acquire write lock
}
}
return

View File

@@ -79,11 +79,11 @@ func (ds *DNSList) test() {
defer ds.RUnlock()
for host, addrs := range ds.m {
for _, addr := range addrs {
if !addr.E {
if !addr.e {
continue
}
fmt.Println("dial:", host, addr.A)
conn, err := net.Dial("tcp", addr.A)
fmt.Println("dial:", host, addr.a)
conn, err := net.Dial("tcp", addr.a)
if err != nil {
continue
}
@@ -91,10 +91,10 @@ func (ds *DNSList) test() {
err = terasu.Use(tlsConn).Handshake(4)
_ = tlsConn.Close()
if err == nil {
fmt.Println("succ:", host, addr.A)
fmt.Println("succ:", host, addr.a)
continue
}
fmt.Println("fail:", host, addr.A)
fmt.Println("fail:", host, addr.a)
}
}
}