diff --git a/conf/dnsresolver_other.go b/conf/dnsresolver_other.go new file mode 100644 index 000000000..ea1b0c1aa --- /dev/null +++ b/conf/dnsresolver_other.go @@ -0,0 +1,35 @@ +// +build !windows + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package conf + +import ( + "fmt" + "net" +) + +func resolveHostname(name string) (resolvedIPString string, err error) { + ips, err := net.LookupIP(name) + if err != nil { + return "", err + } + var ip net.IP + for _, iterip := range ips { + if ip4 := iterip.To4(); ip4 != nil { + ip = ip4 + break + } + if ip == nil { + ip = iterip + } + } + if ip == nil { + return "", fmt.Errorf("unable to resolve IP address of endpoint %q (%v)", name, ips) + } + + return ip.String(), nil +} diff --git a/conf/path.go b/conf/path.go new file mode 100644 index 000000000..89e7a9661 --- /dev/null +++ b/conf/path.go @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package conf + +var cachedConfigFileDir string +var cachedRootDir string +var disableAutoMigration bool + +// PresetRootDirectory causes RootDirectory() to not try any automatic deduction, and instead +// uses what's passed to it. This isn't used by wireguard-windows, but is useful for external +// consumers of our libraries who might want to do strange things. +func PresetRootDirectory(root string) { + cachedRootDir = root + disableAutoMigration = true +} diff --git a/conf/path_other.go b/conf/path_other.go new file mode 100644 index 000000000..3363fff90 --- /dev/null +++ b/conf/path_other.go @@ -0,0 +1,39 @@ +// +build !windows + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package conf + +import ( + "os" + "path/filepath" +) + +func tunnelConfigurationsDirectory() (string, error) { + if cachedConfigFileDir != "" { + return cachedConfigFileDir, nil + } + root, err := RootDirectory() + if err != nil { + return "", err + } + // on linux the configs are just in /etc/wireguard + cachedConfigFileDir = root + return cachedConfigFileDir, nil +} + +func RootDirectory() (string, error) { + if cachedRootDir != "" { + return cachedRootDir, nil + } + c := filepath.Join("/etc", "wireguard") + err := os.MkdirAll(c, os.ModeDir|0700) + if err != nil { + return "", err + } + cachedRootDir = c + return cachedRootDir, nil +} diff --git a/conf/path_windows.go b/conf/path_windows.go index a53968c5b..208442f61 100644 --- a/conf/path_windows.go +++ b/conf/path_windows.go @@ -12,10 +12,6 @@ import ( "golang.org/x/sys/windows" ) -var cachedConfigFileDir string -var cachedRootDir string -var disableAutoMigration bool - func tunnelConfigurationsDirectory() (string, error) { if cachedConfigFileDir != "" { return cachedConfigFileDir, nil @@ -34,14 +30,6 @@ func tunnelConfigurationsDirectory() (string, error) { return cachedConfigFileDir, nil } -// PresetRootDirectory causes RootDirectory() to not try any automatic deduction, and instead -// uses what's passed to it. This isn't used by wireguard-windows, but is useful for external -// consumers of our libraries who might want to do strange things. -func PresetRootDirectory(root string) { - cachedRootDir = root - disableAutoMigration = true -} - func RootDirectory() (string, error) { if cachedRootDir != "" { return cachedRootDir, nil diff --git a/conf/store.go b/conf/store.go index 21bd3a225..d1d2bf0f4 100644 --- a/conf/store.go +++ b/conf/store.go @@ -11,8 +11,6 @@ import ( "os" "path/filepath" "strings" - - "golang.zx2c4.com/wireguard/windows/conf/dpapi" ) const configFileSuffix = ".conf.dpapi" @@ -91,7 +89,7 @@ func MigrateUnencryptedConfigs() (int, []error) { continue } - bytes, err = dpapi.Encrypt(bytes, strings.TrimSuffix(name, configFileUnencryptedSuffix)) + bytes, err = platformEnvelope(bytes, strings.TrimSuffix(name, configFileUnencryptedSuffix)) if err != nil { errs[e] = err e++ @@ -142,7 +140,7 @@ func LoadFromPath(path string) (*Config, error) { return nil, err } if strings.HasSuffix(path, configFileSuffix) { - bytes, err = dpapi.Decrypt(bytes, name) + bytes, err = platformUnenvelope(bytes, name) if err != nil { return nil, err } @@ -181,7 +179,7 @@ func (config *Config) Save() error { } filename := filepath.Join(configFileDir, config.Name+configFileSuffix) bytes := []byte(config.ToWgQuick()) - bytes, err = dpapi.Encrypt(bytes, config.Name) + bytes, err = platformEnvelope(bytes, config.Name) if err != nil { return err } diff --git a/conf/store_other.go b/conf/store_other.go new file mode 100644 index 000000000..cfc02a974 --- /dev/null +++ b/conf/store_other.go @@ -0,0 +1,16 @@ +// +build !windows + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package conf + +func platformEnvelope(bytes []byte, name string) ([]byte, error) { + return bytes, nil +} + +func platformUnenvelope(bytes []byte, name string) ([]byte, error) { + return bytes, nil +} diff --git a/conf/store_windows.go b/conf/store_windows.go new file mode 100644 index 000000000..ab66402a1 --- /dev/null +++ b/conf/store_windows.go @@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package conf + +import ( + "golang.zx2c4.com/wireguard/windows/conf/dpapi" +) + +func platformEnvelope(bytes []byte, name string) ([]byte, error) { + return dpapi.Encrypt(bytes, name) +} + +func platformUnenvelope(bytes []byte, name string) ([]byte, error) { + return dpapi.Decrypt(bytes, name) +} diff --git a/conf/storewatcher.go b/conf/storewatcher.go deleted file mode 100644 index ffd20ee03..000000000 --- a/conf/storewatcher.go +++ /dev/null @@ -1,24 +0,0 @@ -/* SPDX-License-Identifier: MIT - * - * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. - */ - -package conf - -type StoreCallback struct { - cb func() -} - -var storeCallbacks = make(map[*StoreCallback]bool) - -func RegisterStoreChangeCallback(cb func()) *StoreCallback { - startWatchingConfigDir() - cb() - s := &StoreCallback{cb} - storeCallbacks[s] = true - return s -} - -func (cb *StoreCallback) Unregister() { - delete(storeCallbacks, cb) -} diff --git a/conf/storewatcher_windows.go b/conf/storewatcher_windows.go index 199562637..86c3aeebd 100644 --- a/conf/storewatcher_windows.go +++ b/conf/storewatcher_windows.go @@ -11,6 +11,24 @@ import ( "golang.org/x/sys/windows" ) +type StoreCallback struct { + cb func() +} + +var storeCallbacks = make(map[*StoreCallback]bool) + +func RegisterStoreChangeCallback(cb func()) *StoreCallback { + startWatchingConfigDir() + cb() + s := &StoreCallback{cb} + storeCallbacks[s] = true + return s +} + +func (cb *StoreCallback) Unregister() { + delete(storeCallbacks, cb) +} + const ( fncFILE_NAME uint32 = 0x00000001 fncDIR_NAME uint32 = 0x00000002 diff --git a/l18n/l18n.go b/l18n/l18n.go index 76c52ccff..f908f0e14 100644 --- a/l18n/l18n.go +++ b/l18n/l18n.go @@ -8,7 +8,6 @@ package l18n import ( "sync" - "golang.org/x/sys/windows" "golang.org/x/text/language" "golang.org/x/text/message" ) @@ -35,7 +34,7 @@ func prn() *message.Printer { func lang() (tag language.Tag) { tag = language.English confidence := language.No - languages, err := windows.GetUserPreferredUILanguages(windows.MUI_LANGUAGE_NAME) + languages, err := getUserLanguages() if err != nil { return } diff --git a/l18n/l18n_other.go b/l18n/l18n_other.go new file mode 100644 index 000000000..aa8ec17ac --- /dev/null +++ b/l18n/l18n_other.go @@ -0,0 +1,12 @@ +// +build !windows + +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package l18n + +func getUserLanguages() ([]string, error) { + return []string{}, nil +} diff --git a/l18n/l18n_windows.go b/l18n/l18n_windows.go new file mode 100644 index 000000000..31e18a142 --- /dev/null +++ b/l18n/l18n_windows.go @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: MIT + * + * Copyright (C) 2019 WireGuard LLC. All Rights Reserved. + */ + +package l18n + +import ( + "golang.org/x/sys/windows" +) + +func getUserLanguages() ([]string, error) { + return windows.GetUserPreferredUILanguages(windows.MUI_LANGUAGE_NAME) +}