github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/config/v1alpha1/types.go (about)

     1  // SPDX-License-Identifier: MPL-2.0
     2  /*
     3   * Copyright (C) 2024 The Noisy Sockets Authors.
     4   *
     5   * This Source Code Form is subject to the terms of the Mozilla Public
     6   * License, v. 2.0. If a copy of the MPL was not distributed with this
     7   * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     8   */
     9  
    10  package v1alpha1
    11  
    12  import (
    13  	"fmt"
    14  
    15  	"github.com/noisysockets/noisysockets/config/types"
    16  )
    17  
    18  const APIVersion = "noisysockets.github.com/v1alpha1"
    19  
    20  // Config is the configuration for a NoisySockets network.
    21  // It is analogous to the configuration for a WireGuard interface.
    22  type Config struct {
    23  	types.TypeMeta `yaml:",inline" mapstructure:",squash"`
    24  	// Name is the optional hostname of this peer.
    25  	Name string `yaml:"name,omitempty" mapstructure:"name,omitempty"`
    26  	// ListenPort is an optional port on which to listen for incoming packets.
    27  	// If not specified, one will be chosen randomly.
    28  	ListenPort uint16 `yaml:"listenPort,omitempty" mapstructure:"listenPort,omitempty"`
    29  	// PrivateKey is the private key for this peer.
    30  	PrivateKey string `yaml:"privateKey" mapstructure:"privateKey"`
    31  	// IPs is a list of IP addresses assigned to this peer.
    32  	IPs []string `yaml:"ips,omitempty" mapstructure:"ips,omitempty"`
    33  	// DNSServers is an optional list of DNS servers to use for host resolution.
    34  	DNSServers []string `yaml:"dnsServers,omitempty" mapstructure:"dnsServers,omitempty"`
    35  	// Peers is a list of known peers to which we can send and receive packets.
    36  	Peers []PeerConfig `yaml:"peers,omitempty" mapstructure:"peers,omitempty"`
    37  }
    38  
    39  // PeerConfig is the configuration for a known wireguard peer.
    40  type PeerConfig struct {
    41  	// Name is the optional hostname of the peer.
    42  	Name string `yaml:"name,omitempty" mapstructure:"name,omitempty"`
    43  	// PublicKey is the public key of the peer.
    44  	PublicKey string `yaml:"publicKey" mapstructure:"publicKey"`
    45  	// Endpoint is an optional endpoint to which the peer's packets should be sent.
    46  	// If not specified, the peers endpoint will be determined from received packets.
    47  	Endpoint string `yaml:"endpoint,omitempty" mapstructure:"endpoint,omitempty"`
    48  	// IPs is a list of IP addresses assigned to the peer, this is optional for gateways.
    49  	// Traffic with a source IP not in this list will be dropped.
    50  	IPs []string `yaml:"ips,omitempty" mapstructure:"ips,omitempty"`
    51  	// DefaultGateway indicates this peer should be used as the default gateway for traffic.
    52  	DefaultGateway bool `yaml:"defaultGateway,omitempty" mapstructure:"defaultGateway,omitempty"`
    53  	// GatewayForCIDRs is a list of subnets for which this peer should be used as the gateway.
    54  	GatewayForCIDRs []string `yaml:"gatewayForCIDRs,omitempty" mapstructure:"gatewayForCIDRs,omitempty"`
    55  }
    56  
    57  func (c *Config) GetAPIVersion() string {
    58  	return APIVersion
    59  }
    60  
    61  func (c *Config) GetKind() string {
    62  	return "Config"
    63  }
    64  
    65  func (c *Config) PopulateTypeMeta() {
    66  	c.TypeMeta = types.TypeMeta{
    67  		APIVersion: APIVersion,
    68  		Kind:       "Config",
    69  	}
    70  }
    71  
    72  func GetConfigByKind(kind string) (types.Config, error) {
    73  	switch kind {
    74  	case "Config":
    75  		return &Config{}, nil
    76  	default:
    77  		return nil, fmt.Errorf("unsupported kind: %s", kind)
    78  	}
    79  }