2021-08-15 16:56:26 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2023-12-27 20:56:04 +01:00
|
|
|
"context"
|
2021-08-18 13:35:42 +02:00
|
|
|
"fmt"
|
2022-11-23 08:42:12 +01:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
|
2022-06-23 17:04:53 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2022-05-12 11:17:24 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2023-03-02 13:28:14 +01:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/ssh"
|
|
|
|
"github.com/netbirdio/netbird/iface"
|
2023-12-27 20:56:04 +01:00
|
|
|
mgm "github.com/netbirdio/netbird/management/client"
|
2023-03-02 13:28:14 +01:00
|
|
|
"github.com/netbirdio/netbird/util"
|
2021-08-15 16:56:26 +02:00
|
|
|
)
|
|
|
|
|
2023-02-07 11:40:05 +01:00
|
|
|
const (
|
2023-12-27 20:56:04 +01:00
|
|
|
// managementLegacyPortString is the port that was used before by the Management gRPC server.
|
2023-02-07 11:40:05 +01:00
|
|
|
// It is used for backward compatibility now.
|
|
|
|
// NB: hardcoded from github.com/netbirdio/netbird/management/cmd to avoid import
|
2023-12-27 20:56:04 +01:00
|
|
|
managementLegacyPortString = "33073"
|
2023-02-07 11:40:05 +01:00
|
|
|
// DefaultManagementURL points to the NetBird's cloud management endpoint
|
2023-12-27 20:56:04 +01:00
|
|
|
DefaultManagementURL = "https://api.netbird.io:443"
|
|
|
|
// oldDefaultManagementURL points to the NetBird's old cloud management endpoint
|
|
|
|
oldDefaultManagementURL = "https://api.wiretrustee.com:443"
|
2023-02-07 11:40:05 +01:00
|
|
|
// DefaultAdminURL points to NetBird's cloud management console
|
|
|
|
DefaultAdminURL = "https://app.netbird.io:443"
|
|
|
|
)
|
2023-01-17 19:16:50 +01:00
|
|
|
|
2024-03-28 09:56:41 +01:00
|
|
|
var defaultInterfaceBlacklist = []string{
|
|
|
|
iface.WgInterfaceDefault, "wt", "utun", "tun0", "zt", "ZeroTier", "wg", "ts",
|
|
|
|
"Tailscale", "tailscale", "docker", "veth", "br-", "lo",
|
|
|
|
}
|
2023-01-17 19:16:50 +01:00
|
|
|
|
2023-01-08 12:57:28 +01:00
|
|
|
// ConfigInput carries configuration changes to the client
|
|
|
|
type ConfigInput struct {
|
2024-02-21 17:23:17 +01:00
|
|
|
ManagementURL string
|
|
|
|
AdminURL string
|
|
|
|
ConfigPath string
|
|
|
|
PreSharedKey *string
|
|
|
|
ServerSSHAllowed *bool
|
|
|
|
NATExternalIPs []string
|
|
|
|
CustomDNSAddress []byte
|
|
|
|
RosenpassEnabled *bool
|
|
|
|
RosenpassPermissive *bool
|
|
|
|
InterfaceName *string
|
|
|
|
WireguardPort *int
|
|
|
|
DisableAutoConnect *bool
|
2024-03-28 09:56:41 +01:00
|
|
|
ExtraIFaceBlackList []string
|
2023-01-08 12:57:28 +01:00
|
|
|
}
|
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
// Config Configuration type
|
|
|
|
type Config struct {
|
|
|
|
// Wireguard private key of local peer
|
2022-11-23 11:03:29 +01:00
|
|
|
PrivateKey string
|
|
|
|
PreSharedKey string
|
|
|
|
ManagementURL *url.URL
|
|
|
|
AdminURL *url.URL
|
|
|
|
WgIface string
|
|
|
|
WgPort int
|
|
|
|
IFaceBlackList []string
|
|
|
|
DisableIPv6Discovery bool
|
2024-01-08 12:25:35 +01:00
|
|
|
RosenpassEnabled bool
|
2024-02-21 17:23:17 +01:00
|
|
|
RosenpassPermissive bool
|
2024-02-20 11:13:27 +01:00
|
|
|
ServerSSHAllowed *bool
|
2022-06-23 17:04:53 +02:00
|
|
|
// SSHKey is a private SSH key in a PEM format
|
2024-02-21 17:23:17 +01:00
|
|
|
SSHKey string
|
2022-11-23 08:42:12 +01:00
|
|
|
|
2024-01-15 15:53:23 +01:00
|
|
|
// ExternalIP mappings, if different from the host interface IP
|
2022-11-23 08:42:12 +01:00
|
|
|
//
|
|
|
|
// External IP must not be behind a CGNAT and port-forwarding for incoming UDP packets from WgPort on ExternalIP
|
|
|
|
// to WgPort on host interface IP must be present. This can take form of single port-forwarding rule, 1:1 DNAT
|
|
|
|
// mapping ExternalIP to host interface IP, or a NAT DMZ to host interface IP.
|
|
|
|
//
|
|
|
|
// A single mapping will take the form of: external[/internal]
|
|
|
|
// external (required): either the external IP address or "stun" to use STUN to determine the external IP address
|
|
|
|
// internal (optional): either the internal/interface IP address or an interface name
|
|
|
|
//
|
|
|
|
// examples:
|
|
|
|
// "12.34.56.78" => all interfaces IPs will be mapped to external IP of 12.34.56.78
|
|
|
|
// "12.34.56.78/eth0" => IPv4 assigned to interface eth0 will be mapped to external IP of 12.34.56.78
|
|
|
|
// "12.34.56.78/10.1.2.3" => interface IP 10.1.2.3 will be mapped to external IP of 12.34.56.78
|
|
|
|
|
2024-02-21 17:23:17 +01:00
|
|
|
NATExternalIPs []string
|
2023-01-17 19:16:50 +01:00
|
|
|
// CustomDNSAddress sets the DNS resolver listening address in format ip:port
|
2024-02-21 17:23:17 +01:00
|
|
|
CustomDNSAddress string
|
2024-02-20 10:10:05 +01:00
|
|
|
|
|
|
|
// DisableAutoConnect determines whether the client should not start with the service
|
|
|
|
// it's set to false by default due to backwards compatibility
|
|
|
|
DisableAutoConnect bool
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
// ReadConfig read config file and return with Config. If it is not exists create a new with default values
|
|
|
|
func ReadConfig(configPath string) (*Config, error) {
|
|
|
|
if configFileIsExists(configPath) {
|
|
|
|
config := &Config{}
|
|
|
|
if _, err := util.ReadJson(configPath, config); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-20 11:13:27 +01:00
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg, err := createNewConfig(ConfigInput{ConfigPath: configPath})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = WriteOutConfig(configPath, cfg)
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
// UpdateConfig update existing configuration according to input configuration and return with the configuration
|
|
|
|
func UpdateConfig(input ConfigInput) (*Config, error) {
|
|
|
|
if !configFileIsExists(input.ConfigPath) {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "config file doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
return update(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateOrCreateConfig reads existing config or generates a new one
|
|
|
|
func UpdateOrCreateConfig(input ConfigInput) (*Config, error) {
|
|
|
|
if !configFileIsExists(input.ConfigPath) {
|
|
|
|
log.Infof("generating new config %s", input.ConfigPath)
|
2023-03-17 10:37:27 +01:00
|
|
|
cfg, err := createNewConfig(input)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = WriteOutConfig(input.ConfigPath, cfg)
|
|
|
|
return cfg, err
|
2023-03-02 13:28:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if isPreSharedKeyHidden(input.PreSharedKey) {
|
|
|
|
input.PreSharedKey = nil
|
|
|
|
}
|
|
|
|
return update(input)
|
|
|
|
}
|
|
|
|
|
2023-03-17 10:37:27 +01:00
|
|
|
// CreateInMemoryConfig generate a new config but do not write out it to the store
|
|
|
|
func CreateInMemoryConfig(input ConfigInput) (*Config, error) {
|
|
|
|
return createNewConfig(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteOutConfig write put the prepared config to the given path
|
|
|
|
func WriteOutConfig(path string, config *Config) error {
|
|
|
|
return util.WriteJson(path, config)
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
// createNewConfig creates a new config generating a new Wireguard key and saving to file
|
2023-01-08 12:57:28 +01:00
|
|
|
func createNewConfig(input ConfigInput) (*Config, error) {
|
2021-08-15 16:56:26 +02:00
|
|
|
wgKey := generateKey()
|
2022-06-23 17:04:53 +02:00
|
|
|
pem, err := ssh.GeneratePrivateKey(ssh.ED25519)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-01-15 15:53:23 +01:00
|
|
|
|
2022-09-02 19:33:35 +02:00
|
|
|
config := &Config{
|
2022-11-23 11:03:29 +01:00
|
|
|
SSHKey: string(pem),
|
|
|
|
PrivateKey: wgKey,
|
|
|
|
IFaceBlackList: []string{},
|
|
|
|
DisableIPv6Discovery: false,
|
2023-01-17 19:16:50 +01:00
|
|
|
NATExternalIPs: input.NATExternalIPs,
|
|
|
|
CustomDNSAddress: string(input.CustomDNSAddress),
|
2024-02-20 11:13:27 +01:00
|
|
|
ServerSSHAllowed: util.False(),
|
2024-02-20 10:10:05 +01:00
|
|
|
DisableAutoConnect: false,
|
2022-09-02 19:33:35 +02:00
|
|
|
}
|
2023-02-07 11:40:05 +01:00
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
defaultManagementURL, err := parseURL("Management URL", DefaultManagementURL)
|
2023-02-07 11:40:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.ManagementURL = defaultManagementURL
|
2023-01-08 12:57:28 +01:00
|
|
|
if input.ManagementURL != "" {
|
2023-03-02 13:28:14 +01:00
|
|
|
URL, err := parseURL("Management URL", input.ManagementURL)
|
2021-08-18 13:35:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.ManagementURL = URL
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2024-01-15 15:53:23 +01:00
|
|
|
config.WgPort = iface.DefaultWgPort
|
|
|
|
if input.WireguardPort != nil {
|
|
|
|
config.WgPort = *input.WireguardPort
|
|
|
|
}
|
|
|
|
|
|
|
|
config.WgIface = iface.WgInterfaceDefault
|
|
|
|
if input.InterfaceName != nil {
|
|
|
|
config.WgIface = *input.InterfaceName
|
|
|
|
}
|
|
|
|
|
2023-01-08 12:57:28 +01:00
|
|
|
if input.PreSharedKey != nil {
|
|
|
|
config.PreSharedKey = *input.PreSharedKey
|
2021-11-21 17:47:19 +01:00
|
|
|
}
|
|
|
|
|
2024-01-08 12:25:35 +01:00
|
|
|
if input.RosenpassEnabled != nil {
|
|
|
|
config.RosenpassEnabled = *input.RosenpassEnabled
|
|
|
|
}
|
|
|
|
|
2024-02-21 17:23:17 +01:00
|
|
|
if input.RosenpassPermissive != nil {
|
|
|
|
config.RosenpassPermissive = *input.RosenpassPermissive
|
|
|
|
}
|
|
|
|
|
2024-02-20 11:13:27 +01:00
|
|
|
if input.ServerSSHAllowed != nil {
|
|
|
|
config.ServerSSHAllowed = input.ServerSSHAllowed
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
defaultAdminURL, err := parseURL("Admin URL", DefaultAdminURL)
|
2023-02-07 11:40:05 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.AdminURL = defaultAdminURL
|
2023-01-08 12:57:28 +01:00
|
|
|
if input.AdminURL != "" {
|
2023-03-02 13:28:14 +01:00
|
|
|
newURL, err := parseURL("Admin Panel URL", input.AdminURL)
|
2022-07-12 15:02:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.AdminURL = newURL
|
|
|
|
}
|
|
|
|
|
2024-03-28 09:56:41 +01:00
|
|
|
// nolint:gocritic
|
|
|
|
config.IFaceBlackList = append(defaultInterfaceBlacklist, input.ExtraIFaceBlackList...)
|
2021-08-15 16:56:26 +02:00
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
func update(input ConfigInput) (*Config, error) {
|
2021-08-18 13:35:42 +02:00
|
|
|
config := &Config{}
|
2022-05-12 11:17:24 +02:00
|
|
|
|
2023-01-08 12:57:28 +01:00
|
|
|
if _, err := util.ReadJson(input.ConfigPath, config); err != nil {
|
2021-08-18 13:35:42 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
refresh := false
|
|
|
|
|
2023-01-08 12:57:28 +01:00
|
|
|
if input.ManagementURL != "" && config.ManagementURL.String() != input.ManagementURL {
|
2022-04-15 17:30:12 +02:00
|
|
|
log.Infof("new Management URL provided, updated to %s (old value %s)",
|
2023-01-08 12:57:28 +01:00
|
|
|
input.ManagementURL, config.ManagementURL)
|
2023-03-02 13:28:14 +01:00
|
|
|
newURL, err := parseURL("Management URL", input.ManagementURL)
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-15 17:30:12 +02:00
|
|
|
config.ManagementURL = newURL
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
2023-01-08 12:57:28 +01:00
|
|
|
if input.AdminURL != "" && (config.AdminURL == nil || config.AdminURL.String() != input.AdminURL) {
|
2022-04-15 17:30:12 +02:00
|
|
|
log.Infof("new Admin Panel URL provided, updated to %s (old value %s)",
|
2023-01-08 12:57:28 +01:00
|
|
|
input.AdminURL, config.AdminURL)
|
2023-03-02 13:28:14 +01:00
|
|
|
newURL, err := parseURL("Admin Panel URL", input.AdminURL)
|
2022-03-13 15:17:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-15 17:30:12 +02:00
|
|
|
config.AdminURL = newURL
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
2023-01-08 12:57:28 +01:00
|
|
|
if input.PreSharedKey != nil && config.PreSharedKey != *input.PreSharedKey {
|
2023-12-14 11:48:12 +01:00
|
|
|
log.Infof("new pre-shared key provided, replacing old key")
|
|
|
|
config.PreSharedKey = *input.PreSharedKey
|
|
|
|
refresh = true
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
2023-01-17 19:16:50 +01:00
|
|
|
|
2022-06-23 17:04:53 +02:00
|
|
|
if config.SSHKey == "" {
|
|
|
|
pem, err := ssh.GeneratePrivateKey(ssh.ED25519)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.SSHKey = string(pem)
|
|
|
|
refresh = true
|
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
|
2022-09-02 19:33:35 +02:00
|
|
|
if config.WgPort == 0 {
|
|
|
|
config.WgPort = iface.DefaultWgPort
|
|
|
|
refresh = true
|
|
|
|
}
|
2024-01-15 15:53:23 +01:00
|
|
|
|
|
|
|
if input.WireguardPort != nil {
|
|
|
|
config.WgPort = *input.WireguardPort
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.InterfaceName != nil {
|
|
|
|
config.WgIface = *input.InterfaceName
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
2023-01-17 19:16:50 +01:00
|
|
|
if input.NATExternalIPs != nil && len(config.NATExternalIPs) != len(input.NATExternalIPs) {
|
|
|
|
config.NATExternalIPs = input.NATExternalIPs
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.CustomDNSAddress != nil {
|
|
|
|
config.CustomDNSAddress = string(input.CustomDNSAddress)
|
|
|
|
refresh = true
|
|
|
|
}
|
2022-09-02 19:33:35 +02:00
|
|
|
|
2024-01-08 12:25:35 +01:00
|
|
|
if input.RosenpassEnabled != nil {
|
|
|
|
config.RosenpassEnabled = *input.RosenpassEnabled
|
2024-02-20 11:13:27 +01:00
|
|
|
refresh = true
|
2024-02-20 10:10:05 +01:00
|
|
|
}
|
|
|
|
|
2024-02-21 17:23:17 +01:00
|
|
|
if input.RosenpassPermissive != nil {
|
|
|
|
config.RosenpassPermissive = *input.RosenpassPermissive
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
2024-02-20 10:10:05 +01:00
|
|
|
if input.DisableAutoConnect != nil {
|
|
|
|
config.DisableAutoConnect = *input.DisableAutoConnect
|
2024-01-08 12:25:35 +01:00
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
2024-02-20 11:13:27 +01:00
|
|
|
if input.ServerSSHAllowed != nil {
|
|
|
|
config.ServerSSHAllowed = input.ServerSSHAllowed
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ServerSSHAllowed == nil {
|
|
|
|
config.ServerSSHAllowed = util.True()
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
|
2024-03-28 09:56:41 +01:00
|
|
|
if len(input.ExtraIFaceBlackList) > 0 {
|
|
|
|
for _, iFace := range util.SliceDiff(input.ExtraIFaceBlackList, config.IFaceBlackList) {
|
|
|
|
config.IFaceBlackList = append(config.IFaceBlackList, iFace)
|
|
|
|
refresh = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 17:30:12 +02:00
|
|
|
if refresh {
|
|
|
|
// since we have new management URL, we need to update config file
|
2023-01-08 12:57:28 +01:00
|
|
|
if err := util.WriteJson(input.ConfigPath, config); err != nil {
|
2022-04-15 17:30:12 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
2021-08-18 13:35:42 +02:00
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
// parseURL parses and validates a service URL
|
|
|
|
func parseURL(serviceName, serviceURL string) (*url.URL, error) {
|
|
|
|
parsedMgmtURL, err := url.ParseRequestURI(serviceURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed parsing %s URL %s: [%s]", serviceName, serviceURL, err.Error())
|
|
|
|
return nil, err
|
2021-08-18 13:35:42 +02:00
|
|
|
}
|
2023-02-23 09:48:43 +01:00
|
|
|
|
2023-03-02 13:28:14 +01:00
|
|
|
if parsedMgmtURL.Scheme != "https" && parsedMgmtURL.Scheme != "http" {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"invalid %s URL provided %s. Supported format [http|https]://[host]:[port]",
|
|
|
|
serviceName, serviceURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
if parsedMgmtURL.Port() == "" {
|
|
|
|
switch parsedMgmtURL.Scheme {
|
|
|
|
case "https":
|
2023-11-27 16:40:02 +01:00
|
|
|
parsedMgmtURL.Host += ":443"
|
2023-03-02 13:28:14 +01:00
|
|
|
case "http":
|
2023-11-27 16:40:02 +01:00
|
|
|
parsedMgmtURL.Host += ":80"
|
2023-03-02 13:28:14 +01:00
|
|
|
default:
|
|
|
|
log.Infof("unable to determine a default port for schema %s in URL %s", parsedMgmtURL.Scheme, serviceURL)
|
|
|
|
}
|
2023-02-23 09:48:43 +01:00
|
|
|
}
|
2023-03-02 13:28:14 +01:00
|
|
|
|
|
|
|
return parsedMgmtURL, err
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// generateKey generates a new Wireguard private key
|
|
|
|
func generateKey() string {
|
2022-01-16 17:10:36 +01:00
|
|
|
key, err := wgtypes.GeneratePrivateKey()
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return key.String()
|
|
|
|
}
|
2022-05-12 11:17:24 +02:00
|
|
|
|
2023-02-23 09:48:43 +01:00
|
|
|
// don't overwrite pre-shared key if we receive asterisks from UI
|
|
|
|
func isPreSharedKeyHidden(preSharedKey *string) bool {
|
|
|
|
if preSharedKey != nil && *preSharedKey == "**********" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2023-03-02 13:28:14 +01:00
|
|
|
|
|
|
|
func configFileIsExists(path string) bool {
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
return !os.IsNotExist(err)
|
|
|
|
}
|
2023-12-27 20:56:04 +01:00
|
|
|
|
|
|
|
// UpdateOldManagementURL checks whether client can switch to the new Management URL with port 443 and the management domain.
|
|
|
|
// If it can switch, then it updates the config and returns a new one. Otherwise, it returns the provided config.
|
|
|
|
// The check is performed only for the NetBird's managed version.
|
|
|
|
func UpdateOldManagementURL(ctx context.Context, config *Config, configPath string) (*Config, error) {
|
|
|
|
defaultManagementURL, err := parseURL("Management URL", DefaultManagementURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedOldDefaultManagementURL, err := parseURL("Management URL", oldDefaultManagementURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ManagementURL.Hostname() != defaultManagementURL.Hostname() &&
|
|
|
|
config.ManagementURL.Hostname() != parsedOldDefaultManagementURL.Hostname() {
|
|
|
|
// only do the check for the NetBird's managed version
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var mgmTlsEnabled bool
|
|
|
|
if config.ManagementURL.Scheme == "https" {
|
|
|
|
mgmTlsEnabled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !mgmTlsEnabled {
|
|
|
|
// only do the check for HTTPs scheme (the hosted version of the Management service is always HTTPs)
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.ManagementURL.Port() != managementLegacyPortString &&
|
|
|
|
config.ManagementURL.Hostname() == defaultManagementURL.Hostname() {
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newURL, err := parseURL("Management URL", fmt.Sprintf("%s://%s:%d",
|
|
|
|
config.ManagementURL.Scheme, defaultManagementURL.Hostname(), 443))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// here we check whether we could switch from the legacy 33073 port to the new 443
|
|
|
|
log.Infof("attempting to switch from the legacy Management URL %s to the new one %s",
|
|
|
|
config.ManagementURL.String(), newURL.String())
|
|
|
|
key, err := wgtypes.ParseKey(config.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
log.Infof("couldn't switch to the new Management %s", newURL.String())
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := mgm.NewClient(ctx, newURL.Host, key, mgmTlsEnabled)
|
|
|
|
if err != nil {
|
|
|
|
log.Infof("couldn't switch to the new Management %s", newURL.String())
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = client.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Warnf("failed to close the Management service client %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// gRPC check
|
|
|
|
_, err = client.GetServerPublicKey()
|
|
|
|
if err != nil {
|
|
|
|
log.Infof("couldn't switch to the new Management %s", newURL.String())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// everything is alright => update the config
|
|
|
|
newConfig, err := UpdateConfig(ConfigInput{
|
|
|
|
ManagementURL: newURL.String(),
|
|
|
|
ConfigPath: configPath,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.Infof("couldn't switch to the new Management %s", newURL.String())
|
|
|
|
return config, fmt.Errorf("failed updating config file: %v", err)
|
|
|
|
}
|
|
|
|
log.Infof("successfully switched to the new Management URL: %s", newURL.String())
|
|
|
|
|
|
|
|
return newConfig, nil
|
|
|
|
}
|