comandy/dns.go

146 lines
2.9 KiB
Go
Raw Permalink Normal View History

2024-04-15 17:10:06 +09:00
package main
import (
"context"
"crypto/tls"
"net"
"sync"
"github.com/fumiama/terasu"
)
type dnsstat struct {
A string
E bool
}
type dnsservers struct {
sync.RWMutex
m map[string][]*dnsstat
}
// hasrecord no lock, use under lock
func hasrecord(lst []*dnsstat, a string) bool {
for _, addr := range lst {
if addr.A == a {
return true
}
}
return false
}
func (ds *dnsservers) add(m map[string][]string) {
ds.Lock()
defer ds.Unlock()
addList := map[string][]*dnsstat{}
for host, addrs := range m {
for _, addr := range addrs {
if !hasrecord(ds.m[host], addr) && !hasrecord(addList[host], addr) {
addList[host] = append(addList[host], &dnsstat{addr, true})
}
}
}
for host, addrs := range addList {
ds.m[host] = append(ds.m[host], addrs...)
}
}
func (ds *dnsservers) dial(ctx context.Context) (tlsConn *tls.Conn, err error) {
ds.RLock()
defer ds.RUnlock()
2024-04-15 23:58:32 +09:00
if dialer.Timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, dialer.Timeout)
defer cancel()
}
if !dialer.Deadline.IsZero() {
var cancel context.CancelFunc
ctx, cancel = context.WithDeadline(ctx, dialer.Deadline)
defer cancel()
}
2024-04-15 17:10:06 +09:00
var conn net.Conn
for host, addrs := range ds.m {
for _, addr := range addrs {
if !addr.E {
continue
}
2024-04-15 23:58:32 +09:00
conn, err = dialer.DialContext(ctx, "tcp", addr.A)
2024-04-15 17:10:06 +09:00
if err != nil {
2024-04-15 23:58:32 +09:00
addr.E = false // no need to acquire write lock
2024-04-15 17:10:06 +09:00
continue
}
2024-04-16 15:27:06 +09:00
tlsConn = tls.Client(conn, &tls.Config{ServerName: host})
err = terasu.Use(tlsConn).HandshakeContext(ctx)
2024-04-15 17:10:06 +09:00
if err == nil {
return
}
2024-04-15 23:58:32 +09:00
_ = tlsConn.Close()
2024-04-15 17:10:06 +09:00
addr.E = false // no need to acquire write lock
}
}
return
}
var dotv6servers = dnsservers{
m: map[string][]*dnsstat{
"dot.sb": {
{"[2a09::]:853", true},
{"[2a11::]:853", true},
},
"dns.google": {
{"[2001:4860:4860::8888]:853", true},
{"[2001:4860:4860::8844]:853", true},
},
"cloudflare-dns.com": {
{"[2606:4700:4700::1111]:853", true},
{"[2606:4700:4700::1001]:853", true},
},
"dns.umbrella.com": {
{"[2620:0:ccc::2]:853", true},
{"[2620:0:ccd::2]:853", true},
},
"dns10.quad9.net": {
{"[2620:fe::10]:853", true},
{"[2620:fe::fe:10]:853", true},
},
},
}
var dotv4servers = dnsservers{
m: map[string][]*dnsstat{
"dot.sb": {
{"185.222.222.222:853", true},
{"45.11.45.11:853", true},
},
"dns.google": {
{"8.8.8.8:853", true},
{"8.8.4.4:853", true},
},
"cloudflare-dns.com": {
{"1.1.1.1:853", true},
{"1.0.0.1:853", true},
},
"dns.umbrella.com": {
{"208.67.222.222:853", true},
{"208.67.220.220:853", true},
},
"dns10.quad9.net": {
{"9.9.9.10:853", true},
{"149.112.112.10:853", true},
},
},
}
var resolver = &net.Resolver{
PreferGo: true,
2024-04-15 23:58:32 +09:00
Dial: func(ctx context.Context, _, _ string) (net.Conn, error) {
2024-04-15 17:10:06 +09:00
if canUseIPv6.Get() {
return dotv6servers.dial(ctx)
}
return dotv4servers.dial(ctx)
},
}