[client] Use static requested GUID when creating Windows interface (#2479)

RequestedGUID is the GUID of the created network adapter, which then influences NLA generation deterministically.

With this change, NetBird should not generate multiple interfaces in every restart on Windows.
This commit is contained in:
Maycon Santos 2024-08-27 19:21:14 +02:00 committed by GitHub
parent 63a75d72fc
commit 7efaf7eadb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 1 deletions

View File

@ -13,6 +13,7 @@ import (
"testing"
"time"
"github.com/google/uuid"
"github.com/pion/transport/v3/stdnet"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
@ -845,6 +846,8 @@ func TestEngine_MultiplePeers(t *testing.T) {
engine.dnsServer = &dns.MockServer{}
mu.Lock()
defer mu.Unlock()
guid := fmt.Sprintf("{%s}", uuid.New().String())
iface.CustomWindowsGUIDString = strings.ToLower(guid)
err = engine.Start()
if err != nil {
t.Errorf("unable to start engine for peer %d with error %v", j, err)

View File

@ -4,9 +4,11 @@ import (
"fmt"
"net"
"net/netip"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/pion/transport/v3/stdnet"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
@ -345,6 +347,9 @@ func Test_ConnectPeers(t *testing.T) {
t.Fatal(err)
}
guid := fmt.Sprintf("{%s}", uuid.New().String())
CustomWindowsGUIDString = strings.ToLower(guid)
iface1, err := NewWGIFace(peer1ifaceName, peer1wgIP, peer1wgPort, peer1Key.String(), DefaultMTU, newNet, nil, nil)
if err != nil {
t.Fatal(err)
@ -364,6 +369,9 @@ func Test_ConnectPeers(t *testing.T) {
t.Fatal(err)
}
guid = fmt.Sprintf("{%s}", uuid.New().String())
CustomWindowsGUIDString = strings.ToLower(guid)
newNet, err = stdnet.NewNet()
if err != nil {
t.Fatal(err)

View File

@ -7,6 +7,9 @@ import (
"github.com/netbirdio/netbird/iface/bind"
)
// CustomWindowsGUIDString is a custom GUID string for the interface
var CustomWindowsGUIDString string
type wgTunDevice interface {
Create() (wgConfigurer, error)
Up() (*bind.UniversalUDPMuxDefault, error)

View File

@ -14,6 +14,8 @@ import (
"github.com/netbirdio/netbird/iface/bind"
)
const defaultWindowsGUIDSTring = "{f2f29e61-d91f-4d76-8151-119b20c4bdeb}"
type tunDevice struct {
name string
address WGAddress
@ -40,9 +42,22 @@ func newTunDevice(name string, address WGAddress, port int, key string, mtu int,
}
}
func getGUID() (windows.GUID, error) {
guidString := defaultWindowsGUIDSTring
if CustomWindowsGUIDString != "" {
guidString = CustomWindowsGUIDString
}
return windows.GUIDFromString(guidString)
}
func (t *tunDevice) Create() (wgConfigurer, error) {
guid, err := getGUID()
if err != nil {
log.Errorf("failed to get GUID: %s", err)
return nil, err
}
log.Info("create tun interface")
tunDevice, err := tun.CreateTUN(t.name, t.mtu)
tunDevice, err := tun.CreateTUNWithRequestedGUID(t.name, &guid, t.mtu)
if err != nil {
return nil, err
}