github.com/noisysockets/noisysockets@v0.21.2-0.20240515114641-7f467e651c90/config/v1alpha2/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 v1alpha2 11 12 import ( 13 "fmt" 14 15 "github.com/noisysockets/noisysockets/config/types" 16 ) 17 18 const APIVersion = "noisysockets.github.com/v1alpha2" 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 // DNS is the DNS configuration for this peer. 34 DNS *DNSConfig `yaml:"dns,omitempty" mapstructure:"dns,omitempty"` 35 // Routes is the routing table to use for the network. 36 Routes []RouteConfig `yaml:"routes,omitempty" mapstructure:"routes,omitempty"` 37 // Peers is a list of known peers to which we can send and receive packets. 38 Peers []PeerConfig `yaml:"peers,omitempty" mapstructure:"peers,omitempty"` 39 } 40 41 // DNSConfig is the configuration for DNS resolution. 42 type DNSConfig struct { 43 // Domain is the optional default search domain to use for this network. 44 // If not specified, a default value of "my.nzzy.net." will be used. 45 Domain string `yaml:"domain,omitempty" mapstructure:"domain,omitempty"` 46 // Nameservers is a list of DNS servers to use for DNS resolution. 47 Nameservers []string `yaml:"nameservers,omitempty" mapstructure:"nameservers,omitempty"` 48 } 49 50 // RouteConfig is the configuration for a route in the routing table. 51 type RouteConfig struct { 52 // Destinations is a CIDR block for which this route should be used. 53 Destination string `yaml:"destination" mapstructure:"destination"` 54 // Via is the name (or public key) of the peer to use as the gateway for this route. 55 Via string `yaml:"via" mapstructure:"via"` 56 } 57 58 // PeerConfig is the configuration for a known wireguard peer. 59 type PeerConfig struct { 60 // Name is the optional hostname of the peer. 61 Name string `yaml:"name,omitempty" mapstructure:"name,omitempty"` 62 // PublicKey is the public key of the peer. 63 PublicKey string `yaml:"publicKey" mapstructure:"publicKey"` 64 // Endpoint is an optional endpoint to which the peer's packets should be sent. 65 // If not specified, the peers endpoint will be determined from received packets. 66 Endpoint string `yaml:"endpoint,omitempty" mapstructure:"endpoint,omitempty"` 67 // IPs is a list of IP addresses assigned to the peer, this is optional for gateways. 68 // Traffic with a source IP not in this list will be dropped. 69 IPs []string `yaml:"ips,omitempty" mapstructure:"ips,omitempty"` 70 } 71 72 func (c *Config) GetAPIVersion() string { 73 return APIVersion 74 } 75 76 func (c *Config) GetKind() string { 77 return "Config" 78 } 79 80 func (c *Config) PopulateTypeMeta() { 81 c.TypeMeta = types.TypeMeta{ 82 APIVersion: APIVersion, 83 Kind: "Config", 84 } 85 } 86 87 func GetConfigByKind(kind string) (types.Config, error) { 88 switch kind { 89 case "Config": 90 return &Config{}, nil 91 default: 92 return nil, fmt.Errorf("unsupported kind: %s", kind) 93 } 94 }