mirror of
https://github.com/fumiama/terasu.git
synced 2026-06-21 04:40:25 +08:00
feat(dns): add lookup host fallback
This commit is contained in:
18
dns/dns.go
18
dns/dns.go
@@ -21,8 +21,8 @@ var DefaultDialer = net.Dialer{
|
|||||||
}
|
}
|
||||||
|
|
||||||
type dnsstat struct {
|
type dnsstat struct {
|
||||||
A string
|
a string
|
||||||
E bool
|
e bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type DNSList struct {
|
type DNSList struct {
|
||||||
@@ -32,14 +32,14 @@ type DNSList struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DNSConfig struct {
|
type DNSConfig struct {
|
||||||
Servers map[string][]string // Servers map[dot.com]ip:ports
|
Servers map[string][]string `yaml:"Servers"` // Servers map[dot.com]ip:ports
|
||||||
Fallbacks map[string][]string // Fallbacks map[domain]ips
|
Fallbacks map[string][]string `yaml:"Fallbacks"` // Fallbacks map[domain]ips
|
||||||
}
|
}
|
||||||
|
|
||||||
// hasrecord no lock, use under lock
|
// hasrecord no lock, use under lock
|
||||||
func hasrecord(lst []*dnsstat, a string) bool {
|
func hasrecord(lst []*dnsstat, a string) bool {
|
||||||
for _, addr := range lst {
|
for _, addr := range lst {
|
||||||
if addr.A == a {
|
if addr.a == a {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -117,12 +117,12 @@ func (ds *DNSList) DialContext(ctx context.Context, dialer *net.Dialer, firstFra
|
|||||||
var conn net.Conn
|
var conn net.Conn
|
||||||
for host, addrs := range ds.m {
|
for host, addrs := range ds.m {
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
if !addr.E {
|
if !addr.e {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
conn, err = dialer.DialContext(ctx, "tcp", addr.A)
|
conn, err = dialer.DialContext(ctx, "tcp", addr.a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
addr.E = false // no need to acquire write lock
|
addr.e = false // no need to acquire write lock
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tlsConn = tls.Client(conn, &tls.Config{ServerName: host})
|
tlsConn = tls.Client(conn, &tls.Config{ServerName: host})
|
||||||
@@ -131,7 +131,7 @@ func (ds *DNSList) DialContext(ctx context.Context, dialer *net.Dialer, firstFra
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
_ = tlsConn.Close()
|
_ = tlsConn.Close()
|
||||||
addr.E = false // no need to acquire write lock
|
addr.e = false // no need to acquire write lock
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -79,11 +79,11 @@ func (ds *DNSList) test() {
|
|||||||
defer ds.RUnlock()
|
defer ds.RUnlock()
|
||||||
for host, addrs := range ds.m {
|
for host, addrs := range ds.m {
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
if !addr.E {
|
if !addr.e {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Println("dial:", host, addr.A)
|
fmt.Println("dial:", host, addr.a)
|
||||||
conn, err := net.Dial("tcp", addr.A)
|
conn, err := net.Dial("tcp", addr.a)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -91,10 +91,10 @@ func (ds *DNSList) test() {
|
|||||||
err = terasu.Use(tlsConn).Handshake(4)
|
err = terasu.Use(tlsConn).Handshake(4)
|
||||||
_ = tlsConn.Close()
|
_ = tlsConn.Close()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
fmt.Println("succ:", host, addr.A)
|
fmt.Println("succ:", host, addr.a)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
fmt.Println("fail:", host, addr.A)
|
fmt.Println("fail:", host, addr.a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user