4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

http.Client の DNS resolver を差し替える

Last updated at Posted at 2018-11-18

http.Client の名前解決をローカルに適当に立てた DNS sever に向けたいときに.

:warning: production ready かどうかは知らない

dnsAddr := ":5300"

dialerFunc := func(ctx context.Context, network, address string) (net.Conn, error) {
	d := net.Dialer{}
	return d.DialContext(ctx, "udp", dnsAddr)
}

resolver := &net.Resolver{PreferGo: true, Dial: dialerFunc}
dialer := net.Dialer{Resolver: resolver}

transport := &http.Transport{
	Dial:        dialer.Dial,
	DialContext: dialer.DialContext,
}

httpClient := &http.Client{Transport: transport}

http.DefaultTransport みたいなのを破壊せずコピーしてくる便利な方法ないのかな

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?