2021-08-15 16:56:26 +02:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2021-08-18 13:35:42 +02:00
|
|
|
"fmt"
|
2021-08-15 16:56:26 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/wiretrustee/wiretrustee/iface"
|
|
|
|
"github.com/wiretrustee/wiretrustee/util"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2021-08-18 13:35:42 +02:00
|
|
|
"net/url"
|
2021-08-15 16:56:26 +02:00
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
var managementURLDefault *url.URL
|
|
|
|
|
|
|
|
func ManagementURLDefault() *url.URL {
|
|
|
|
return managementURLDefault
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
managementURL, err := parseManagementURL("https://api.wiretrustee.com:33073")
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
managementURLDefault = managementURL
|
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
|
|
|
|
// Config Configuration type
|
|
|
|
type Config struct {
|
|
|
|
// Wireguard private key of local peer
|
|
|
|
PrivateKey string
|
2021-11-21 17:47:19 +01:00
|
|
|
PreSharedKey string
|
2021-08-18 13:35:42 +02:00
|
|
|
ManagementURL *url.URL
|
2021-08-15 16:56:26 +02:00
|
|
|
WgIface string
|
|
|
|
IFaceBlackList []string
|
|
|
|
}
|
|
|
|
|
|
|
|
//createNewConfig creates a new config generating a new Wireguard key and saving to file
|
2021-11-21 17:47:19 +01:00
|
|
|
func createNewConfig(managementURL string, configPath string, preSharedKey string) (*Config, error) {
|
2021-08-15 16:56:26 +02:00
|
|
|
wgKey := generateKey()
|
|
|
|
config := &Config{PrivateKey: wgKey, WgIface: iface.WgInterfaceDefault, IFaceBlackList: []string{}}
|
|
|
|
if managementURL != "" {
|
2021-08-18 13:35:42 +02:00
|
|
|
URL, err := parseManagementURL(managementURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
config.ManagementURL = URL
|
2021-08-15 16:56:26 +02:00
|
|
|
} else {
|
2021-08-18 13:35:42 +02:00
|
|
|
config.ManagementURL = managementURLDefault
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2021-11-21 17:47:19 +01:00
|
|
|
if preSharedKey != "" {
|
|
|
|
config.PreSharedKey = preSharedKey
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
config.IFaceBlackList = []string{iface.WgInterfaceDefault, "tun0"}
|
|
|
|
|
2021-08-15 16:56:26 +02:00
|
|
|
err := util.WriteJson(configPath, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
func parseManagementURL(managementURL string) (*url.URL, error) {
|
2021-08-15 16:56:26 +02:00
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
parsedMgmtURL, err := url.ParseRequestURI(managementURL)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("failed parsing management URL %s: [%s]", managementURL, err.Error())
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !(parsedMgmtURL.Scheme == "https" || parsedMgmtURL.Scheme == "http") {
|
|
|
|
return nil, fmt.Errorf("invalid Management Service URL provided %s. Supported format [http|https]://[host]:[port]", managementURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedMgmtURL, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadConfig reads existing config. In case provided managementURL is not empty overrides the read property
|
|
|
|
func ReadConfig(managementURL string, configPath string) (*Config, error) {
|
|
|
|
config := &Config{}
|
|
|
|
_, err := util.ReadJson(configPath, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-13 15:17:18 +01:00
|
|
|
if managementURL != "" && config.ManagementURL.String() != managementURL {
|
2021-08-18 13:35:42 +02:00
|
|
|
URL, err := parseManagementURL(managementURL)
|
2021-08-15 16:56:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-18 13:35:42 +02:00
|
|
|
config.ManagementURL = URL
|
2022-03-13 15:17:18 +01:00
|
|
|
// since we have new management URL, we need to update config file
|
|
|
|
err = util.WriteJson(configPath, config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
log.Infof("new Management URL provided, updated to %s (old value %s)", managementURL, config.ManagementURL)
|
2021-08-15 16:56:26 +02:00
|
|
|
}
|
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
return config, err
|
|
|
|
}
|
2021-08-15 16:56:26 +02:00
|
|
|
|
2021-08-18 13:35:42 +02:00
|
|
|
// GetConfig reads existing config or generates a new one
|
2021-11-21 17:47:19 +01:00
|
|
|
func GetConfig(managementURL string, configPath string, preSharedKey string) (*Config, error) {
|
2021-08-18 13:35:42 +02:00
|
|
|
|
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
log.Infof("generating new config %s", configPath)
|
2021-11-21 17:47:19 +01:00
|
|
|
return createNewConfig(managementURL, configPath, preSharedKey)
|
2021-08-18 13:35:42 +02:00
|
|
|
} else {
|
|
|
|
return ReadConfig(managementURL, configPath)
|
|
|
|
}
|
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()
|
|
|
|
}
|