Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

dns.go 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package network
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "net/url"
  7. "time"
  8. "github.com/ncruces/go-dns"
  9. )
  10. var dnsCacheOptions = []dns.CacheOption{
  11. dns.MaxCacheEntries(dns.DefaultMaxCacheEntries),
  12. dns.MaxCacheTTL(time.Hour),
  13. dns.NegativeCache(false),
  14. }
  15. func GetDNS(u *url.URL) (*net.Resolver, error) {
  16. if u == nil {
  17. return dns.NewCachingResolver(nil, dnsCacheOptions...), nil
  18. }
  19. switch u.Scheme {
  20. case "tls":
  21. return dns.NewDoTResolver(u.Host, dns.DoTCache(dnsCacheOptions...))
  22. case "https":
  23. if u.Path == "" {
  24. u.Path = "/dns-query"
  25. }
  26. return dns.NewDoHResolver(u.String(), dns.DoHCache(dnsCacheOptions...))
  27. case "udp", "":
  28. default:
  29. return nil, fmt.Errorf("unsupported DNS %v", u)
  30. }
  31. port := u.Port()
  32. if port == "" {
  33. port = "53"
  34. }
  35. hostport := net.JoinHostPort(u.Hostname(), port)
  36. dialer := &net.Dialer{}
  37. resolver := &net.Resolver{
  38. PreferGo: true,
  39. Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
  40. return dialer.DialContext(ctx, "udp", hostport)
  41. },
  42. }
  43. return dns.NewCachingResolver(resolver, dnsCacheOptions...), nil
  44. }