github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/dnsproxy/lookup_linux.go (about)

     1  package dnsproxy
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"context"
     7  	"fmt"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/telepresenceio/telepresence/v2/pkg/iputil"
    12  	"github.com/telepresenceio/telepresence/v2/pkg/proc"
    13  )
    14  
    15  func externalLookup(ctx context.Context, host string, timeout time.Duration) iputil.IPs {
    16  	cmd := proc.CommandContext(ctx, "nslookup", fmt.Sprintf("-timeout=%.2g", timeout.Seconds()), host)
    17  	cmd.DisableLogging = true
    18  	out, err := cmd.Output()
    19  	if err != nil {
    20  		return nil
    21  	}
    22  
    23  	// Look for the adjacent lines
    24  	//   Name: <host> [possibly extended with search path]
    25  	//   Address: <ip>
    26  	var ips iputil.IPs
    27  	sc := bufio.NewScanner(bytes.NewReader(out))
    28  	for sc.Scan() {
    29  		s := sc.Text()
    30  		if a := strings.TrimPrefix(s, "Name:\t"); a != s && strings.HasPrefix(a, host) && sc.Scan() {
    31  			s = sc.Text()
    32  			if a := strings.TrimPrefix(s, "Address:"); a != s {
    33  				if ip := iputil.Parse(strings.TrimSpace(a)); ip != nil {
    34  					ips = append(ips, ip)
    35  				}
    36  			}
    37  		}
    38  	}
    39  	return ips
    40  }