Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add SetResolver method support custom dns server #768

Open
wants to merge 4 commits into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions client.go
Expand Up @@ -7,6 +7,7 @@ package resty
import (
"bytes"
"compress/gzip"
"context"
"crypto/tls"
"crypto/x509"
"encoding/json"
Expand All @@ -15,6 +16,7 @@ import (
"fmt"
"io"
"math"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -821,6 +823,31 @@ func (c *Client) SetTLSClientConfig(config *tls.Config) *Client {
return c
}

// SetResolver method set Custom DNS for resolver
func (c *Client) SetResolver(dns string) *Client {
transport, err := c.transport()
if err != nil {
c.log.Errorf("%v", err)
return c
}

transport.DialContext = (&net.Dialer{
Timeout: c.httpClient.Timeout,
KeepAlive: 30 * time.Second,
Resolver: &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
d := net.Dialer{
Timeout: time.Duration(5000) * time.Millisecond,
}
return d.DialContext(ctx, "udp", fmt.Sprintf("%s:53", dns))
},
},
}).DialContext

return c
}

// SetProxy method sets the Proxy URL and Port for Resty client.
//
// client.SetProxy("http://proxyserver:8888")
Expand Down