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

Update dependencies: Wireguard, go-ini and logrus #3

Merged
merged 3 commits into from
Dec 10, 2021
Merged
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
28 changes: 18 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
module github.com/place1/wg-embed

go 1.13
go 1.17

require (
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.6.0
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/vishvananda/netlink v1.1.0
golang.zx2c4.com/wireguard v0.0.0-20211129173154-2dd424e2d808
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20211207222515-92d582e4e166
gopkg.in/ini.v1 v1.66.2
)

require (
github.com/google/go-cmp v0.5.6 // indirect
github.com/josharian/native v0.0.0-20200817173448-b6b71def0850 // indirect
github.com/mdlayher/genetlink v1.0.0 // indirect
github.com/mdlayher/netlink v1.4.1 // indirect
github.com/mdlayher/socket v0.0.0-20211102153432-57e3fa563ecb // indirect
github.com/vishvananda/netns v0.0.0-20200520041808-52d707b772fe // indirect
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899 // indirect
golang.org/x/net v0.0.0-20200707034311-ab3426394381 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
golang.org/x/text v0.3.3 // indirect
golang.zx2c4.com/wireguard v0.0.20200320
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20200609130330-bd2cb7843e1b
gopkg.in/ini.v1 v1.57.0
golang.org/x/crypto v0.0.0-20211117183948-ae814b36b871 // indirect
golang.org/x/net v0.0.0-20211123203042-d83791d6bcd9 // indirect
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
golang.zx2c4.com/go118/netip v0.0.0-20211111135330-a4a02eeacf9d // indirect
golang.zx2c4.com/wintun v0.0.0-20211104114900-415007cec224 // indirect
)
123 changes: 84 additions & 39 deletions go.sum

Large diffs are not rendered by default.

46 changes: 9 additions & 37 deletions pkg/wgembed/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

type ConfigFile struct {
Interface IfaceConfig
Peers []PeerConfig
wgconfig *wgtypes.Config
Peers []PeerConfig `ini:"Peer,nonunique"`
wgconfig *wgtypes.Config `ini:"-"`
}

type IfaceConfig struct {
Expand Down Expand Up @@ -56,43 +56,15 @@ func ReadConfig(path string) (*ConfigFile, error) {
}

func (c *ConfigFile) parse(config []byte) error {
// this is harder than it needs to be because of this bug
// https://github.com/go-ini/ini/issues/190

sections := []string{}
for _, s := range strings.Split(string(config), "[") {
if s != "" {
sections = append(sections, "["+s)
}
opt := ini.LoadOptions{AllowNonUniqueSections: true}
f, err := ini.LoadSources(opt, config)
if err != nil {
return errors.Wrap(err, "failed to read wireguard config file")
}

for _, s := range sections {
if s == "" {
continue
}

f, err := ini.Load([]byte(s))
if err != nil {
return errors.Wrap(err, "failed to read wireguard config file")
}
section := f.Sections()[1]

switch section.Name() {
case ini.DEFAULT_SECTION:
// nothing to do here (case so that we don't warn for it)
case "Interface":
if err := section.MapTo(&c.Interface); err != nil {
return errors.Wrap(err, "failed to parse Interface config")
}
case "Peer":
peer := PeerConfig{}
if err := section.MapTo(&peer); err != nil {
return errors.Wrap(err, "failed to parse Interface config")
}
c.Peers = append(c.Peers, peer)
default:
logrus.Warnf("skipping unknown config section: %s", section.Name())
}
err = f.MapTo(c)
if err != nil {
return errors.Wrap(err, "failed to map wireguard config file")
}

return nil
Expand Down
5 changes: 4 additions & 1 deletion pkg/wgembed/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ func TestReadConfig(t *testing.T) {
Address: []string{"10.44.0.1/24", "fd48:4c4:7aa9::1/64"},
DNS: []string{},
},
Peers: []PeerConfig{{PublicKey: "gysKSkCS/VeAyHIAVtf8B/sbQnEd5FYogtj7kO4d4zY=", AllowedIPs: []string{"10.44.0.2/32", "fd48:4c4:7aa9::2/128"}}},
Peers: []PeerConfig{
{PublicKey: "gysKSkCS/VeAyHIAVtf8B/sbQnEd5FYogtj7kO4d4zY=", AllowedIPs: []string{"10.44.0.2/32", "fd48:4c4:7aa9::2/128"}},
{PublicKey: "bcEwHLic4PW9O3qECsbUWuD4PeU3NfRl5Cmiz+e/p3o=", AllowedIPs: []string{"10.44.0.3/32", "fd48:4c4:7aa9::3/128"}},
},
wgconfig: nil,
},
false,
Expand Down
12 changes: 8 additions & 4 deletions pkg/wgembed/iface.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

/* SPDX-License-Identifier: MIT
Expand All @@ -10,9 +11,11 @@
package wgembed

import (
"fmt"
"net"

"github.com/pkg/errors"
"golang.zx2c4.com/wireguard/conn"
"golang.zx2c4.com/wireguard/device"
"golang.zx2c4.com/wireguard/ipc"
"golang.zx2c4.com/wireguard/tun"
Expand Down Expand Up @@ -52,7 +55,7 @@ func New(interfaceName string) (WireGuardInterface, error) {
}
wg.client = client

tun, err := tun.CreateTUN(wg.name, device.DefaultMTU)
tunDevice, err := tun.CreateTUN(wg.name, device.DefaultMTU)
if err != nil {
return nil, errors.Wrap(err, "failed to create TUN device")
}
Expand All @@ -63,7 +66,8 @@ func New(interfaceName string) (WireGuardInterface, error) {
return nil, errors.Wrap(err, "UAPI listen error")
}

wg.device = device.NewDevice(tun, device.NewLogger(device.LogLevelError, wg.name))
logger := device.NewLogger(device.LogLevelError, fmt.Sprintf("(%s) ", interfaceName))
wg.device = device.NewDevice(tunDevice, conn.NewDefaultBind(), logger)

errs := make(chan error)

Expand All @@ -75,12 +79,12 @@ func New(interfaceName string) (WireGuardInterface, error) {

go func() {
for {
conn, err := uapi.Accept()
connection, err := uapi.Accept()
if err != nil {
errs <- err
return
}
go wg.device.IpcHandle(conn)
go wg.device.IpcHandle(connection)
}
}()

Expand Down
1 change: 1 addition & 0 deletions pkg/wgembed/iface_darwin.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build darwin
// +build darwin

package wgembed
Expand Down
1 change: 1 addition & 0 deletions pkg/wgembed/iface_linux.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build linux
// +build linux

package wgembed
Expand Down
1 change: 1 addition & 0 deletions pkg/wgembed/iface_windows.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build windows
// +build windows

package wgembed
Expand Down
4 changes: 4 additions & 0 deletions pkg/wgembed/testdata/dualstack.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ Address = 10.44.0.1/24, fd48:4c4:7aa9::1/64
[Peer]
PublicKey = gysKSkCS/VeAyHIAVtf8B/sbQnEd5FYogtj7kO4d4zY=
AllowedIPs = 10.44.0.2/32, fd48:4c4:7aa9::2/128

[Peer]
PublicKey = bcEwHLic4PW9O3qECsbUWuD4PeU3NfRl5Cmiz+e/p3o=
AllowedIPs = 10.44.0.3/32, fd48:4c4:7aa9::3/128