栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Custom DNS resolver for the default HTTP client in Go

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Custom DNS resolver for the default HTTP client in Go

这里填写标题
  • 1. Custom DNS resolver for the default HTTP client in Go

1. Custom DNS resolver for the default HTTP client in Go

The default HTTP client in Go uses the default DNS resolver available on the machine. There are some cases where you might want to use a different DNS resolver.

I want to quickly show how you can change the DNS resolver for the default HTTP client. We are using this same method for our custom DNS resolver in my company: Violetnorth

// Changing DNS resolver
package main

import (
  "context"
  "io/ioutil"
  "log"
  "net"
  "net/http"
  "time"
)

func main() {
  var (
    dnsResolverIP        = "8.8.8.8:53" // Google DNS resolver.
    dnsResolverProto     = "udp"        // Protocol to use for the DNS resolver
    dnsResolverTimeoutMs = 5000         // Timeout (ms) for the DNS resolver (optional)
  )

  dialer := &net.Dialer{
    Resolver: &net.Resolver{
      PreferGo: true,
      Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
        d := net.Dialer{
          Timeout: time.Duration(dnsResolverTimeoutMs) * time.Millisecond,
        }
        return d.DialContext(ctx, dnsResolverProto, dnsResolverIP)
      },
    },
  }

  dialContext := func(ctx context.Context, network, addr string) (net.Conn, error) {
    return dialer.DialContext(ctx, network, addr)
  }

  http.DefaultTransport.(*http.Transport).DialContext = dialContext
  httpClient := &http.Client{}

  // Testing the new HTTP client with the custom DNS resolver.
  resp, err := httpClient.Get("https://www.violetnorth.com")
  if err != nil {
    log.Fatalln(err)
  }
  defer resp.Body.Close()

  body, err := ioutil.ReadAll(resp.Body)
  if err != nil {
    log.Fatalln(err)
  }

  log.Println(string(body))
}

It only takes a few lines but you have to overwrite the Dial function in the Resolver which is inside the Dialer which is inside the Transport function.

It might seem weird but it actually makes a lot of sense, follow the functions from top to bottom, DefaultTransport -> DialContext -> Dialer -> Resolver -> Dial

We are changing the Dial function which dials the DNS server to resolve the request, so it should be inside the Resolver. But this whole thing is in the DialContext of the actual HTTP request.

You have to dial the DNS resolver first, resolve the IP, then dial that resolved IP to make the HTTP request.

Anyways that’s pretty much it. Changing the DNS resolver in the default HTTP client.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939964.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号