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

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"github.com/sirupsen/logrus"
     8  	"gopkg.in/yaml.v3"
     9  )
    10  
    11  type OSSpecificConfig struct {
    12  	Network Network `json:"network,omitempty" yaml:"network,omitempty"`
    13  }
    14  
    15  func GetDefaultOSSpecificConfig() OSSpecificConfig {
    16  	return OSSpecificConfig{
    17  		Network: Network{
    18  			GlobalDNSSearchConfigStrategy: defaultGlobalDNSSearchConfigStrategy,
    19  		},
    20  	}
    21  }
    22  
    23  // Merge merges this instance with the non-zero values of the given argument. The argument values take priority.
    24  func (c *OSSpecificConfig) Merge(o *OSSpecificConfig) {
    25  	c.Network.merge(&o.Network)
    26  }
    27  
    28  type GSCStrategy string
    29  
    30  const (
    31  	// GSCAuto configure DNS search first attempting GSCPowershell and if that fails, GSCRegistry.
    32  	GSCAuto = "auto"
    33  
    34  	// GSCRegistry configure DNS search by setting the registry value System\CurrentControlSet\Services\Tcpip\Parameters\SearchList.
    35  	GSCRegistry = "registry"
    36  
    37  	// GSCPowershell configure DNS search using the powershell Set-DnsClientGlobalSetting command.
    38  	GSCPowershell = "powershell"
    39  
    40  	defaultGlobalDNSSearchConfigStrategy = GSCAuto
    41  
    42  	// defaultVirtualIPSubnet is an IP that, on windows, is built from 16 class C subnets which were chosen randomly,
    43  	// hoping that they don't collide with another subnet.
    44  	defaultVirtualIPSubnet = "211.55.48.0/20"
    45  )
    46  
    47  type Network struct {
    48  	GlobalDNSSearchConfigStrategy GSCStrategy `json:"globalDNSSearchConfigStrategy,omitempty" yaml:"globalDNSSearchConfigStrategy,omitempty"`
    49  }
    50  
    51  func (n *Network) merge(o *Network) {
    52  	if o.GlobalDNSSearchConfigStrategy != defaultGlobalDNSSearchConfigStrategy {
    53  		n.GlobalDNSSearchConfigStrategy = o.GlobalDNSSearchConfigStrategy
    54  	}
    55  }
    56  
    57  func (n Network) IsZero() bool {
    58  	return n.GlobalDNSSearchConfigStrategy == defaultGlobalDNSSearchConfigStrategy
    59  }
    60  
    61  func (n *Network) UnmarshalYAML(node *yaml.Node) (err error) {
    62  	if node.Kind != yaml.MappingNode {
    63  		return errors.New(WithLoc("network must be an object", node))
    64  	}
    65  	ms := node.Content
    66  	top := len(ms)
    67  	for i := 0; i < top; i += 2 {
    68  		kv, err := StringKey(ms[i])
    69  		if err != nil {
    70  			return err
    71  		}
    72  		v := ms[i+1]
    73  		switch kv {
    74  		case "globalDNSSearchConfigStrategy":
    75  			switch v.Value {
    76  			case GSCAuto, GSCRegistry, GSCPowershell:
    77  				n.GlobalDNSSearchConfigStrategy = GSCStrategy(v.Value)
    78  			default:
    79  				logrus.Warn(WithLoc(fmt.Sprintf("invalid globalDNSSearchConfigStrategy %q. Valid values are %q, %q or %q",
    80  					v.Value, GSCAuto, GSCRegistry, GSCPowershell), ms[i+1]))
    81  			}
    82  		default:
    83  			logrus.Warn(WithLoc(fmt.Sprintf("unknown key %q", kv), ms[i]))
    84  		}
    85  	}
    86  	if n.GlobalDNSSearchConfigStrategy == "" {
    87  		n.GlobalDNSSearchConfigStrategy = defaultGlobalDNSSearchConfigStrategy
    88  	}
    89  	return nil
    90  }