github.com/Asutorufa/yuhaiin@v0.3.6-0.20240502055049-7984da7023a0/pkg/net/latency/udp.go (about) 1 package latency 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/Asutorufa/yuhaiin/pkg/net/dns" 8 "github.com/Asutorufa/yuhaiin/pkg/net/netapi" 9 pdns "github.com/Asutorufa/yuhaiin/pkg/protos/config/dns" 10 ) 11 12 var timeout = 5 * time.Second 13 14 func DNS(p netapi.Proxy, host, target string) (time.Duration, error) { 15 d, err := dns.New(dns.Config{ 16 Type: pdns.Type_udp, 17 Host: host, 18 Dialer: p, 19 }) 20 if err != nil { 21 return 0, err 22 } 23 defer d.Close() 24 25 start := time.Now() 26 27 ctx, cancel := context.WithTimeout(context.TODO(), timeout) 28 defer cancel() 29 30 _, err = d.LookupIP(ctx, target) 31 if err != nil { 32 return 0, err 33 } 34 35 return time.Since(start), nil 36 } 37 38 func DNSOverQuic(p netapi.Proxy, host, target string) (time.Duration, error) { 39 d, err := dns.New( 40 dns.Config{ 41 Type: pdns.Type_doq, 42 Host: host, 43 Dialer: p, 44 }, 45 ) 46 if err != nil { 47 return 0, err 48 } 49 defer d.Close() 50 51 start := time.Now() 52 53 ctx, cancel := context.WithTimeout(context.TODO(), timeout) 54 defer cancel() 55 56 _, err = d.LookupIP(ctx, target) 57 if err != nil { 58 return 0, err 59 } 60 61 return time.Since(start), nil 62 }