Skip to content

Commit

Permalink
add custom dns addr flag (#399)
Browse files Browse the repository at this point in the history
Fixes #354

Signed-off-by: titanventura <aswath7862001@gmail.com>
  • Loading branch information
titanventura authored Jul 17, 2024
1 parent a77feac commit 743a4e0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type arguments struct {
MaxConnections int `short:"c" long:"max-connections" value-name:"<count>" default:"512" description:"Maximum number of HTTP connections"`
MaxConnectionsPerHost int `long:"max-connections-per-host" value-name:"<count>" default:"512" description:"Maximum number of HTTP connections per host"`
MaxResponseBodySize int `long:"max-response-body-size" value-name:"<size>" default:"10000000" description:"Maximum response body size to read"`
CustomDnsAddr string `long:"custom-dns-addr" description:"Custom DNS server to be used for requests"`
RawExcludedPatterns []string `short:"e" long:"exclude" value-name:"<pattern>..." description:"Exclude URLs matched with given regular expressions"`
RawIncludedPatterns []string `short:"i" long:"include" value-name:"<pattern>..." description:"Include URLs matched with given regular expressions"`
FollowRobotsTxt bool `long:"follow-robots-txt" description:"Follow robots.txt when scraping pages"`
Expand Down
1 change: 1 addition & 0 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (c *command) runWithError(ss []string) (bool, error) {
SkipTLSVerification: args.SkipTLSVerification,
Timeout: time.Duration(args.Timeout) * time.Second,
Header: args.Header,
CustomDnsAddr: args.CustomDnsAddr,
},
),
args.RateLimit,
Expand Down
18 changes: 18 additions & 0 deletions fasthttp_http_client_factory.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"crypto/tls"
"net"

Expand All @@ -24,6 +25,23 @@ func (*fasthttpHttpClientFactory) Create(o httpClientOptions) httpClient {
d = fasthttpproxy.FasthttpHTTPDialerTimeout(o.Proxy, tcpTimeout)
}

if o.CustomDnsAddr != "" {
d = func(addr string) (net.Conn, error) {
dialer := fasthttp.TCPDialer{
Concurrency: 1000,
Resolver: &net.Resolver{
PreferGo: true,
StrictErrors: false,
Dial: func(ctx context.Context, network string, address string) (net.Conn, error) {
internalDialer := net.Dialer{}
return internalDialer.DialContext(ctx, "udp", o.CustomDnsAddr)
},
},
}
return dialer.DialTimeout(addr, tcpTimeout)
}
}

return newFasthttpHttpClient(
&fasthttp.Client{
MaxConnsPerHost: o.MaxConnectionsPerHost,
Expand Down
4 changes: 4 additions & 0 deletions fasthttp_http_client_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ func TestFasthttpHttpClientFactoryCreate(t *testing.T) {
func TestFasthttpHttpClientFactoryCreateWithProxy(t *testing.T) {
newFasthttpHttpClientFactory().Create(httpClientOptions{Proxy: "foo"})
}

func TestFasthttpHttpClientFactoryCreateWithCustomDnsResolver(t *testing.T) {
newFasthttpHttpClientFactory().Create(httpClientOptions{CustomDnsAddr: "1.1.1.1:53"})
}
1 change: 1 addition & 0 deletions http_client_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type httpClientOptions struct {
SkipTLSVerification bool
Timeout time.Duration
Header http.Header
CustomDnsAddr string
}

type httpClientFactory interface {
Expand Down

0 comments on commit 743a4e0

Please sign in to comment.