2021-07-19 15:02:11 +02:00
|
|
|
package iface
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-13 17:00:01 +02:00
|
|
|
"net"
|
2024-05-22 12:32:01 +02:00
|
|
|
"net/netip"
|
2024-08-27 19:21:14 +02:00
|
|
|
"strings"
|
2023-04-13 17:00:01 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2024-08-27 19:21:14 +02:00
|
|
|
"github.com/google/uuid"
|
2023-12-20 23:02:42 +01:00
|
|
|
"github.com/pion/transport/v3/stdnet"
|
2021-07-19 15:02:11 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2022-06-04 19:41:01 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-07-19 15:02:11 +02:00
|
|
|
"golang.zx2c4.com/wireguard/wgctrl"
|
|
|
|
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
|
2024-10-02 18:24:22 +02:00
|
|
|
|
|
|
|
"github.com/netbirdio/netbird/client/iface/device"
|
2021-07-19 15:02:11 +02:00
|
|
|
)
|
|
|
|
|
2023-11-01 17:11:16 +01:00
|
|
|
// keep darwin compatibility
|
2021-07-19 15:02:11 +02:00
|
|
|
const (
|
2022-01-25 09:40:28 +01:00
|
|
|
WgIntNumber = 2000
|
2022-01-17 14:01:58 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
key string
|
|
|
|
peerPubKey string
|
2021-07-19 15:02:11 +02:00
|
|
|
)
|
|
|
|
|
2021-08-29 17:48:31 +02:00
|
|
|
func init() {
|
|
|
|
log.SetLevel(log.DebugLevel)
|
2022-01-17 14:01:58 +01:00
|
|
|
privateKey, _ := wgtypes.GeneratePrivateKey()
|
|
|
|
key = privateKey.String()
|
|
|
|
peerPrivateKey, _ := wgtypes.GeneratePrivateKey()
|
|
|
|
peerPubKey = peerPrivateKey.PublicKey().String()
|
2021-08-29 17:48:31 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 19:41:01 +02:00
|
|
|
func TestWGIface_UpdateAddr(t *testing.T) {
|
|
|
|
ifaceName := fmt.Sprintf("utun%d", WgIntNumber+4)
|
|
|
|
addr := "100.64.0.1/8"
|
2024-01-03 16:06:20 +01:00
|
|
|
wgPort := 33100
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-10-22 20:53:14 +02:00
|
|
|
opts := WGIFaceOpts{
|
|
|
|
IFaceName: ifaceName,
|
|
|
|
Address: addr,
|
|
|
|
WGPort: wgPort,
|
|
|
|
WGPrivKey: key,
|
|
|
|
MTU: DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, err := NewWGIFace(opts)
|
2022-06-04 19:41:01 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = iface.Create()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = iface.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
2022-06-04 19:41:01 +02:00
|
|
|
}()
|
2024-01-03 16:06:20 +01:00
|
|
|
|
|
|
|
_, err = iface.Up()
|
2022-06-04 19:41:01 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := getIfaceAddrs(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
assert.Equal(t, addr, addrs[0].String())
|
|
|
|
|
|
|
|
//update WireGuard address
|
|
|
|
addr = "100.64.0.2/8"
|
|
|
|
err = iface.UpdateAddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err = getIfaceAddrs(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2024-05-22 12:32:01 +02:00
|
|
|
var found bool
|
|
|
|
for _, a := range addrs {
|
|
|
|
prefix, err := netip.ParsePrefix(a.String())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if prefix.Addr().Is4() {
|
|
|
|
found = true
|
|
|
|
assert.Equal(t, addr, prefix.String())
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 19:41:01 +02:00
|
|
|
|
2024-05-22 12:32:01 +02:00
|
|
|
if !found {
|
|
|
|
t.Fatal("v4 address not found")
|
|
|
|
}
|
2022-06-04 19:41:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func getIfaceAddrs(ifaceName string) ([]net.Addr, error) {
|
|
|
|
ief, err := net.InterfaceByName(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
addrs, err := ief.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return addrs, nil
|
|
|
|
}
|
|
|
|
|
2021-07-19 15:02:11 +02:00
|
|
|
func Test_CreateInterface(t *testing.T) {
|
2022-01-25 09:40:28 +01:00
|
|
|
ifaceName := fmt.Sprintf("utun%d", WgIntNumber+1)
|
|
|
|
wgIP := "10.99.99.1/32"
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-10-22 20:53:14 +02:00
|
|
|
opts := WGIFaceOpts{
|
|
|
|
IFaceName: ifaceName,
|
|
|
|
Address: wgIP,
|
|
|
|
WGPort: 33100,
|
|
|
|
WGPrivKey: key,
|
|
|
|
MTU: DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, err := NewWGIFace(opts)
|
2022-01-17 14:01:58 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = iface.Create()
|
2021-07-19 15:02:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-29 17:48:31 +02:00
|
|
|
defer func() {
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface.Close()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2021-07-19 15:02:11 +02:00
|
|
|
wg, err := wgctrl.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-29 17:48:31 +02:00
|
|
|
defer func() {
|
|
|
|
err = wg.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|
2022-01-17 14:01:58 +01:00
|
|
|
|
|
|
|
func Test_Close(t *testing.T) {
|
2022-01-25 09:40:28 +01:00
|
|
|
ifaceName := fmt.Sprintf("utun%d", WgIntNumber+2)
|
|
|
|
wgIP := "10.99.99.2/32"
|
2024-01-03 16:06:20 +01:00
|
|
|
wgPort := 33100
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
2024-10-22 20:53:14 +02:00
|
|
|
opts := WGIFaceOpts{
|
|
|
|
IFaceName: ifaceName,
|
|
|
|
Address: wgIP,
|
|
|
|
WGPort: wgPort,
|
|
|
|
WGPrivKey: key,
|
|
|
|
MTU: DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, err := NewWGIFace(opts)
|
2022-01-17 14:01:58 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = iface.Create()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
wg, err := wgctrl.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = wg.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
err = iface.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-09 14:44:46 +02:00
|
|
|
func TestRecreation(t *testing.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
t.Run(fmt.Sprintf("down-%d", i), func(t *testing.T) {
|
|
|
|
ifaceName := fmt.Sprintf("utun%d", WgIntNumber+2)
|
|
|
|
wgIP := "10.99.99.2/32"
|
|
|
|
wgPort := 33100
|
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-10-22 20:53:14 +02:00
|
|
|
opts := WGIFaceOpts{
|
|
|
|
IFaceName: ifaceName,
|
|
|
|
Address: wgIP,
|
|
|
|
WGPort: wgPort,
|
|
|
|
WGPrivKey: key,
|
|
|
|
MTU: DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, err := NewWGIFace(opts)
|
2024-09-09 14:44:46 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err = net.InterfaceByName(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
t.Logf("interface %s not found: err: %s", ifaceName, err)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Logf("interface %s found", ifaceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = iface.Create()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
wg, err := wgctrl.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
err = wg.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
_, err = iface.Up()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
_, err = net.InterfaceByName(ifaceName)
|
|
|
|
if err == nil {
|
|
|
|
t.Logf("interface %s found", ifaceName)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
t.Logf("interface %s not found: err: %s", ifaceName, err)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
err = iface.Close()
|
|
|
|
t.Logf("down time: %s", time.Since(start))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 15:02:11 +02:00
|
|
|
func Test_ConfigureInterface(t *testing.T) {
|
2022-01-25 09:40:28 +01:00
|
|
|
ifaceName := fmt.Sprintf("utun%d", WgIntNumber+3)
|
|
|
|
wgIP := "10.99.99.5/30"
|
2024-01-03 16:06:20 +01:00
|
|
|
wgPort := 33100
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-10-22 20:53:14 +02:00
|
|
|
opts := WGIFaceOpts{
|
|
|
|
IFaceName: ifaceName,
|
|
|
|
Address: wgIP,
|
|
|
|
WGPort: wgPort,
|
|
|
|
WGPrivKey: key,
|
|
|
|
MTU: DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
iface, err := NewWGIFace(opts)
|
2022-01-17 14:01:58 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = iface.Create()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface.Close()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
_, err = iface.Up()
|
2021-07-19 15:02:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg, err := wgctrl.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-29 17:48:31 +02:00
|
|
|
defer func() {
|
|
|
|
err = wg.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2021-07-19 15:02:11 +02:00
|
|
|
|
|
|
|
wgDevice, err := wg.Device(ifaceName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if wgDevice.PrivateKey.String() != key {
|
|
|
|
t.Fatalf("Private keys don't match after configure: %s != %s", key, wgDevice.PrivateKey.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_UpdatePeer(t *testing.T) {
|
2022-01-25 09:40:28 +01:00
|
|
|
ifaceName := fmt.Sprintf("utun%d", WgIntNumber+4)
|
|
|
|
wgIP := "10.99.99.9/30"
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
2024-10-22 20:53:14 +02:00
|
|
|
opts := WGIFaceOpts{
|
|
|
|
IFaceName: ifaceName,
|
|
|
|
Address: wgIP,
|
|
|
|
WGPort: 33100,
|
|
|
|
WGPrivKey: key,
|
|
|
|
MTU: DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, err := NewWGIFace(opts)
|
2022-01-17 14:01:58 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = iface.Create()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface.Close()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2024-01-03 16:06:20 +01:00
|
|
|
|
|
|
|
_, err = iface.Up()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-07-19 15:02:11 +02:00
|
|
|
keepAlive := 15 * time.Second
|
2022-01-25 09:40:28 +01:00
|
|
|
allowedIP := "10.99.99.10/32"
|
2022-01-17 14:01:58 +01:00
|
|
|
endpoint, err := net.ResolveUDPAddr("udp", "127.0.0.1:9900")
|
2021-07-19 15:02:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface.UpdatePeer(peerPubKey, allowedIP, keepAlive, endpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
peer, err := getPeer(ifaceName, peerPubKey)
|
2021-07-19 15:02:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if peer.PersistentKeepaliveInterval != keepAlive {
|
|
|
|
t.Fatal("configured peer with mismatched keepalive interval value")
|
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
if peer.Endpoint.String() != endpoint.String() {
|
2021-07-19 15:02:11 +02:00
|
|
|
t.Fatal("configured peer with mismatched endpoint")
|
|
|
|
}
|
|
|
|
|
|
|
|
var foundAllowedIP bool
|
|
|
|
for _, aip := range peer.AllowedIPs {
|
|
|
|
if aip.String() == allowedIP {
|
|
|
|
foundAllowedIP = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundAllowedIP {
|
|
|
|
t.Fatal("configured peer with mismatched Allowed IPs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
func Test_RemovePeer(t *testing.T) {
|
2022-01-25 09:40:28 +01:00
|
|
|
ifaceName := fmt.Sprintf("utun%d", WgIntNumber+4)
|
|
|
|
wgIP := "10.99.99.13/30"
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
2024-10-22 20:53:14 +02:00
|
|
|
opts := WGIFaceOpts{
|
|
|
|
IFaceName: ifaceName,
|
|
|
|
Address: wgIP,
|
|
|
|
WGPort: 33100,
|
|
|
|
WGPrivKey: key,
|
|
|
|
MTU: DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
|
|
|
|
iface, err := NewWGIFace(opts)
|
2022-01-17 14:01:58 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = iface.Create()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface.Close()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2024-01-03 16:06:20 +01:00
|
|
|
|
|
|
|
_, err = iface.Up()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
keepAlive := 15 * time.Second
|
2022-01-25 09:40:28 +01:00
|
|
|
allowedIP := "10.99.99.14/32"
|
2021-08-29 17:48:31 +02:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface.UpdatePeer(peerPubKey, allowedIP, keepAlive, nil, nil)
|
2021-07-19 15:02:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface.RemovePeer(peerPubKey)
|
2021-07-19 15:02:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
|
|
|
_, err = getPeer(ifaceName, peerPubKey)
|
2022-01-17 14:01:58 +01:00
|
|
|
if err.Error() != "peer not found" {
|
|
|
|
t.Fatal(err)
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
func Test_ConnectPeers(t *testing.T) {
|
2022-01-25 09:40:28 +01:00
|
|
|
peer1ifaceName := fmt.Sprintf("utun%d", WgIntNumber+400)
|
|
|
|
peer1wgIP := "10.99.99.17/30"
|
2022-01-17 14:01:58 +01:00
|
|
|
peer1Key, _ := wgtypes.GeneratePrivateKey()
|
2024-01-03 16:06:20 +01:00
|
|
|
peer1wgPort := 33100
|
2022-01-17 14:01:58 +01:00
|
|
|
|
2023-02-13 18:34:56 +01:00
|
|
|
peer2ifaceName := "utun500"
|
2022-01-25 09:40:28 +01:00
|
|
|
peer2wgIP := "10.99.99.18/30"
|
2022-01-17 14:01:58 +01:00
|
|
|
peer2Key, _ := wgtypes.GeneratePrivateKey()
|
2024-01-03 16:06:20 +01:00
|
|
|
peer2wgPort := 33200
|
2022-01-17 14:01:58 +01:00
|
|
|
|
|
|
|
keepAlive := 1 * time.Second
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err := stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
2024-08-27 19:21:14 +02:00
|
|
|
guid := fmt.Sprintf("{%s}", uuid.New().String())
|
2024-10-02 18:24:22 +02:00
|
|
|
device.CustomWindowsGUIDString = strings.ToLower(guid)
|
2024-08-27 19:21:14 +02:00
|
|
|
|
2024-10-22 20:53:14 +02:00
|
|
|
optsPeer1 := WGIFaceOpts{
|
|
|
|
IFaceName: peer1ifaceName,
|
|
|
|
Address: peer1wgIP,
|
|
|
|
WGPort: peer1wgPort,
|
|
|
|
WGPrivKey: peer1Key.String(),
|
|
|
|
MTU: DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
iface1, err := NewWGIFace(optsPeer1)
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface1.Create()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
|
|
|
_, err = iface1.Up()
|
2022-01-25 09:40:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
2024-10-22 20:53:14 +02:00
|
|
|
localIP, err := getLocalIP()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
peer1endpoint, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", localIP, peer1wgPort))
|
2022-01-25 09:40:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:21:14 +02:00
|
|
|
guid = fmt.Sprintf("{%s}", uuid.New().String())
|
2024-10-02 18:24:22 +02:00
|
|
|
device.CustomWindowsGUIDString = strings.ToLower(guid)
|
2024-08-27 19:21:14 +02:00
|
|
|
|
2023-04-13 17:00:01 +02:00
|
|
|
newNet, err = stdnet.NewNet()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-10-22 20:53:14 +02:00
|
|
|
|
|
|
|
optsPeer2 := WGIFaceOpts{
|
|
|
|
IFaceName: peer2ifaceName,
|
|
|
|
Address: peer2wgIP,
|
|
|
|
WGPort: peer2wgPort,
|
|
|
|
WGPrivKey: peer2Key.String(),
|
|
|
|
MTU: DefaultMTU,
|
|
|
|
TransportNet: newNet,
|
|
|
|
}
|
|
|
|
|
|
|
|
iface2, err := NewWGIFace(optsPeer2)
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface2.Create()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
|
|
|
_, err = iface2.Up()
|
2022-01-25 09:40:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
2024-10-22 20:53:14 +02:00
|
|
|
peer2endpoint, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", localIP, peer2wgPort))
|
2022-01-25 09:40:28 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-08-29 17:48:31 +02:00
|
|
|
defer func() {
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface1.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
err = iface2.Close()
|
2021-08-29 17:48:31 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2022-01-25 09:40:28 +01:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
err = iface1.UpdatePeer(peer2Key.PublicKey().String(), peer2wgIP, keepAlive, peer2endpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
err = iface2.UpdatePeer(peer1Key.PublicKey().String(), peer1wgIP, keepAlive, peer1endpoint, nil)
|
2021-07-19 15:02:11 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2022-10-26 14:48:40 +02:00
|
|
|
// todo: investigate why in some tests execution we need 30s
|
|
|
|
timeout := 30 * time.Second
|
2022-01-17 14:01:58 +01:00
|
|
|
timeoutChannel := time.After(timeout)
|
2024-01-03 16:06:20 +01:00
|
|
|
|
2022-01-17 14:01:58 +01:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeoutChannel:
|
|
|
|
t.Fatalf("waiting for peer handshake timeout after %s", timeout.String())
|
|
|
|
default:
|
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
|
|
|
|
peer, gpErr := getPeer(peer1ifaceName, peer2Key.PublicKey().String())
|
2022-01-17 14:01:58 +01:00
|
|
|
if gpErr != nil {
|
|
|
|
t.Fatal(gpErr)
|
|
|
|
}
|
|
|
|
if !peer.LastHandshakeTime.IsZero() {
|
|
|
|
t.Log("peers successfully handshake")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 15:02:11 +02:00
|
|
|
}
|
2023-02-13 18:34:56 +01:00
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
func getPeer(ifaceName, peerPubKey string) (wgtypes.Peer, error) {
|
2023-02-13 18:34:56 +01:00
|
|
|
wg, err := wgctrl.New()
|
|
|
|
if err != nil {
|
2024-01-03 16:06:20 +01:00
|
|
|
return wgtypes.Peer{}, err
|
2023-02-13 18:34:56 +01:00
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
defer func() {
|
|
|
|
err = wg.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("got error while closing wgctl: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-02-13 18:34:56 +01:00
|
|
|
|
2024-01-03 16:06:20 +01:00
|
|
|
wgDevice, err := wg.Device(ifaceName)
|
2023-02-13 18:34:56 +01:00
|
|
|
if err != nil {
|
2024-01-03 16:06:20 +01:00
|
|
|
return wgtypes.Peer{}, err
|
2023-02-13 18:34:56 +01:00
|
|
|
}
|
2024-01-03 16:06:20 +01:00
|
|
|
for _, peer := range wgDevice.Peers {
|
|
|
|
if peer.PublicKey.String() == peerPubKey {
|
|
|
|
return peer, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wgtypes.Peer{}, fmt.Errorf("peer not found")
|
2023-02-13 18:34:56 +01:00
|
|
|
}
|
2024-10-22 20:53:14 +02:00
|
|
|
|
|
|
|
func getLocalIP() (string, error) {
|
|
|
|
// Get all interfaces
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ipNet, ok := addr.(*net.IPNet)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if ipNet.IP.IsLoopback() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ipNet.IP.To4() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return ipNet.IP.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", fmt.Errorf("no local IP found")
|
|
|
|
}
|