Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

l18n,conf: isolate GOOS=windows code #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions conf/dnsresolver_other.go
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions conf/path.go
Original file line number Diff line number Diff line change
@@ -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 changes: 39 additions & 0 deletions conf/path_other.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 0 additions & 12 deletions conf/path_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions conf/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"os"
"path/filepath"
"strings"

"golang.zx2c4.com/wireguard/windows/conf/dpapi"
)

const configFileSuffix = ".conf.dpapi"
Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions conf/store_other.go
Original file line number Diff line number Diff line change
@@ -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 changes: 18 additions & 0 deletions conf/store_windows.go
Original file line number Diff line number Diff line change
@@ -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)
}
24 changes: 0 additions & 24 deletions conf/storewatcher.go

This file was deleted.

18 changes: 18 additions & 0 deletions conf/storewatcher_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions l18n/l18n.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ package l18n
import (
"sync"

"golang.org/x/sys/windows"
"golang.org/x/text/language"
"golang.org/x/text/message"
)
Expand All @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions l18n/l18n_other.go
Original file line number Diff line number Diff line change
@@ -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
}
14 changes: 14 additions & 0 deletions l18n/l18n_windows.go
Original file line number Diff line number Diff line change
@@ -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)
}