Skip to content

Commit

Permalink
refactor(netcore): separate resolver and dialer code (#5)
Browse files Browse the repository at this point in the history
No functional change, just more clear separation of concerns.
  • Loading branch information
bassosimone authored Nov 19, 2024
1 parent 975d24a commit a6c80f7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 45 deletions.
45 changes: 0 additions & 45 deletions netcore/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,51 +54,6 @@ func (nx *Network) sequentialDial(
return nil, errors.Join(errv...)
}

// maybeLookupEndpoint resolves the domain name inside an endpoint into
// a list of TCP/UDP endpoints. If the domain name is already an IP
// address, we short circuit the lookup.
func (nx *Network) maybeLookupEndpoint(ctx context.Context, endpoint string) ([]string, error) {
domain, port, err := net.SplitHostPort(endpoint)
if err != nil {
return nil, err
}

addrs, err := nx.maybeLookupHost(ctx, domain)
if err != nil {
return nil, err
}

var endpoints []string
for _, addr := range addrs {
endpoints = append(endpoints, net.JoinHostPort(addr, port))
}
return endpoints, nil
}

// maybeLookupHost resolves a domain name to IP addresses unless the domain
// is already an IP address, in which case we short circuit the lookup.
func (nx *Network) maybeLookupHost(ctx context.Context, domain string) ([]string, error) {
// handle the case where domain is already an IP address
if net.ParseIP(domain) != nil {
return []string{domain}, nil
}

// TODO(bassosimone): we should probably ensure we nonetheless
// include the lookup event inside the logs.
return nx.doLookupHost(ctx, domain)
}

func (nx *Network) doLookupHost(ctx context.Context, domain string) ([]string, error) {
// if there is a custom LookupHostFunc, use it
if nx.LookupHostFunc != nil {
return nx.LookupHostFunc(ctx, domain)
}

// otherwise fallback to the system resolver
reso := &net.Resolver{}
return reso.LookupHost(ctx, domain)
}

func (nx *Network) dialLog(ctx context.Context, network, address string) (net.Conn, error) {
// TODO(bassosimone): emit structured logs
return nx.dialNet(ctx, network, address)
Expand Down
4 changes: 4 additions & 0 deletions netcore/network.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Definition of Network.
//

package netcore

Expand Down
60 changes: 60 additions & 0 deletions netcore/resolver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Adapted from: https://github.com/ooni/probe-cli/blob/v3.20.1/internal/netxlite/dialer.go
//
// Internal code for DNS lookups.
//

package netcore

import (
"context"
"net"
)

// maybeLookupEndpoint resolves the domain name inside an endpoint into
// a list of TCP/UDP endpoints. If the domain name is already an IP
// address, we short circuit the lookup.
func (nx *Network) maybeLookupEndpoint(ctx context.Context, endpoint string) ([]string, error) {
domain, port, err := net.SplitHostPort(endpoint)
if err != nil {
return nil, err
}

addrs, err := nx.maybeLookupHost(ctx, domain)
if err != nil {
return nil, err
}

var endpoints []string
for _, addr := range addrs {
endpoints = append(endpoints, net.JoinHostPort(addr, port))
}
return endpoints, nil
}

// maybeLookupHost resolves a domain name to IP addresses unless the domain
// is already an IP address, in which case we short circuit the lookup.
func (nx *Network) maybeLookupHost(ctx context.Context, domain string) ([]string, error) {
// handle the case where domain is already an IP address
if net.ParseIP(domain) != nil {
return []string{domain}, nil
}

// TODO(bassosimone): we should probably ensure we nonetheless
// include the lookup event inside the logs.
return nx.doLookupHost(ctx, domain)
}

// doLookupHost performs the DNS lookup.
func (nx *Network) doLookupHost(ctx context.Context, domain string) ([]string, error) {
// if there is a custom LookupHostFunc, use it
if nx.LookupHostFunc != nil {
return nx.LookupHostFunc(ctx, domain)
}

// otherwise fallback to the system resolver
reso := &net.Resolver{}
return reso.LookupHost(ctx, domain)
}
4 changes: 4 additions & 0 deletions netcore/tlsconfig.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// TLS config code.
//

package netcore

Expand Down
4 changes: 4 additions & 0 deletions netcore/tlsdialer.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
// TLS dialing code
//

package netcore

Expand Down

0 comments on commit a6c80f7

Please sign in to comment.