Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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 みたいなのを破壊せずコピーしてくる便利な方法ないのかな

wantedly
「シゴトでココロオドル」ためのビジネスSNS「Wantedly」の開発・運営をしています。
https://wantedlyinc.com/ja/presentations
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away