github.com/decred/dcrlnd@v0.7.6/watchtower/conf.go (about)

     1  package watchtower
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  )
     7  
     8  // Conf specifies the watchtower options that can be configured from the command
     9  // line or configuration file.
    10  type Conf struct {
    11  	// RawListeners configures the watchtower's listening ports/interfaces.
    12  	RawListeners []string `long:"listen" description:"Add interfaces/ports to listen for peer connections"`
    13  
    14  	// RawExternalIPs configures the watchtower's external ports/interfaces.
    15  	RawExternalIPs []string `long:"externalip" description:"Add interfaces/ports where the watchtower can accept peer connections"`
    16  
    17  	// ReadTimeout specifies the duration the tower will wait when trying to
    18  	// read a message from a client before hanging up.
    19  	ReadTimeout time.Duration `long:"readtimeout" description:"Duration the watchtower server will wait for messages to be received before hanging up on clients"`
    20  
    21  	// WriteTimeout specifies the duration the tower will wait when trying
    22  	// to write a message from a client before hanging up.
    23  	WriteTimeout time.Duration `long:"writetimeout" description:"Duration the watchtower server will wait for messages to be written before hanging up on client connections"`
    24  }
    25  
    26  // Apply completes the passed Config struct by applying any parsed Conf options.
    27  // If the corresponding values parsed by Conf are already set in the Config,
    28  // those fields will be not be modified.
    29  func (c *Conf) Apply(cfg *Config,
    30  	normalizer AddressNormalizer) (*Config, error) {
    31  
    32  	// Set the Config's listening addresses if they are empty.
    33  	if cfg.ListenAddrs == nil {
    34  		// Without a network, we will be unable to resolve the listening
    35  		// addresses.
    36  		if cfg.Net == nil {
    37  			return nil, ErrNoNetwork
    38  		}
    39  
    40  		// If no addresses are specified by the Config, we will resort
    41  		// to the default peer port.
    42  		if len(c.RawListeners) == 0 {
    43  			addr := DefaultListenAddr
    44  			c.RawListeners = append(c.RawListeners, addr)
    45  		}
    46  
    47  		// Normalize the raw listening addresses so that they can be
    48  		// used by the brontide listener.
    49  		var err error
    50  		cfg.ListenAddrs, err = normalizer(
    51  			c.RawListeners, strconv.Itoa(DefaultPeerPort),
    52  			cfg.Net.ResolveTCPAddr,
    53  		)
    54  		if err != nil {
    55  			return nil, err
    56  		}
    57  	}
    58  
    59  	// Set the Config's external ips if they are empty.
    60  	if cfg.ExternalIPs == nil {
    61  		// Without a network, we will be unable to resolve the external
    62  		// IP addresses.
    63  		if cfg.Net == nil {
    64  			return nil, ErrNoNetwork
    65  		}
    66  
    67  		var err error
    68  		cfg.ExternalIPs, err = normalizer(
    69  			c.RawExternalIPs, strconv.Itoa(DefaultPeerPort),
    70  			cfg.Net.ResolveTCPAddr,
    71  		)
    72  		if err != nil {
    73  			return nil, err
    74  		}
    75  	}
    76  
    77  	// If the Config has no read timeout, we will use the parsed Conf
    78  	// value.
    79  	if cfg.ReadTimeout == 0 && c.ReadTimeout != 0 {
    80  		cfg.ReadTimeout = c.ReadTimeout
    81  	}
    82  
    83  	// If the Config has no write timeout, we will use the parsed Conf
    84  	// value.
    85  	if cfg.WriteTimeout == 0 && c.WriteTimeout != 0 {
    86  		cfg.WriteTimeout = c.WriteTimeout
    87  	}
    88  
    89  	return cfg, nil
    90  }