conf: bring over changes from wireguard-windows#10

https://github.com/WireGuard/wireguard-windows/pull/10 don't appear to
be of interest, and now need big rebasing.

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2021-04-21 07:05:50 -04:00
parent 77158f3dde
commit d5812860fc
No known key found for this signature in database
GPG Key ID: 524F155275DF0C3E
6 changed files with 128 additions and 4 deletions

35
conf/dnsresolver_other.go Normal file
View File

@ -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
}

18
conf/path.go Normal file
View File

@ -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
}

39
conf/path_other.go Normal file
View File

@ -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
}

View File

@ -10,8 +10,6 @@ import (
"os"
"path/filepath"
"strings"
"golang.zx2c4.com/wireguard/windows/conf/dpapi"
)
const configFileSuffix = ".conf.dpapi"
@ -71,7 +69,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
}
@ -110,7 +108,7 @@ func (config *Config) Save(overwrite bool) 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
}

16
conf/store_other.go Normal file
View File

@ -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
}

18
conf/store_windows.go Normal file
View File

@ -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)
}