Skip to content

Commit

Permalink
Optionally allow specification of custom DNS resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
oxtoacart committed Oct 5, 2023
1 parent 08c7942 commit 106c0be
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
3 changes: 3 additions & 0 deletions http-proxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ var (
broflakeAddr = flag.String("broflake-addr", "", "Address at which to listen for broflake connections.")

track = flag.String("track", "", "The track this proxy is running on")

dnsServer = flag.String("dns-server", "172.16.0.53", "Optional DNS server to use for DNS lookups (in place of system resolver)")
)

const (
Expand Down Expand Up @@ -468,6 +470,7 @@ func main() {
BroflakeAddr: *broflakeAddr,
BroflakeCert: os.Getenv("BROFLAKE_CERT"),
BroflakeKey: os.Getenv("BROFLAKE_KEY"),
DNSServer: *dnsServer,
}
if *maxmindLicenseKey != "" {
log.Debug("Will use Maxmind for geolocating clients")
Expand Down
33 changes: 32 additions & 1 deletion http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
shadowsocks "github.com/getlantern/http-proxy-lantern/v2/shadowsocks"
"github.com/getlantern/http-proxy-lantern/v2/starbridge"
"github.com/getlantern/kcpwrapper"
"github.com/getlantern/netx"

"github.com/xtaci/smux"

Expand Down Expand Up @@ -183,6 +184,7 @@ type Proxy struct {
BroflakeCert string
BroflakeKey string

DNSServer string
throttleConfig throttle.Config
instrument instrument.Instrument
}
Expand Down Expand Up @@ -277,6 +279,35 @@ func (p *Proxy) ListenAndServe(ctx context.Context) error {
// Throttle connections when signaled
srv.AddListenerWrappers(lanternlisteners.NewBitrateListener, bwReporting.wrapper)

if p.DNSServer != "" {
log.Debugf("Will resolve DNS using %v", p.DNSServer)
host, port, err := net.SplitHostPort(p.DNSServer)
if err != nil {
log.Fatalf("invalid dns-server address %v: %v", p.DNSServer, err)
}
r := &net.Resolver{
PreferGo: true,
Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
log.Debug("Dialing custom resolver")
return netx.DialContext(ctx, host, port)
},
}
netx.OverrideResolveIPs(func(host string) ([]net.IP, error) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

addrs, err := r.LookupIPAddr(ctx, host)
if err != nil {
return nil, err
}
ips := make([]net.IP, 0, len(addrs))
for _, addr := range addrs {
ips = append(ips, addr.IP)
}
return ips, nil
})
}

allListeners := make([]net.Listener, 0)
listenerProtocols := make([]string, 0)
addListenerIfNecessary := func(proto, addr string, fn listenerBuilderFN) error {
Expand Down Expand Up @@ -613,7 +644,7 @@ func (p *Proxy) createFilterChain(bl *blacklist.Blacklist) (filters.Chain, proxy
_dialer := preferIPV4Dialer(timeoutToDialOriginSite)
dialer := func(ctx context.Context, network, addr string) (net.Conn, error) {
// resolve separately so that we can track the DNS resolution time
resolvedAddr, resolveErr := net.ResolveTCPAddr(network, addr)
resolvedAddr, resolveErr := netx.Resolve(network, addr)
if resolveErr != nil {
return nil, resolveErr
}
Expand Down
5 changes: 3 additions & 2 deletions throttle_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import (
"testing"
"time"

"github.com/getlantern/golog/testlog"
. "github.com/getlantern/waitforserver"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/getlantern/golog/testlog"
. "github.com/getlantern/waitforserver"

"github.com/getlantern/http-proxy-lantern/v2/common"
"github.com/getlantern/http-proxy-lantern/v2/internal/testutil"
"github.com/getlantern/http-proxy-lantern/v2/throttle"
Expand Down

0 comments on commit 106c0be

Please sign in to comment.