mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-29 19:43:57 +01:00
4fec709bb1
* compile client under freebsd (#1620) Compile netbird client under freebsd and now support netstack and userspace modes. Refactoring linux specific code to share same code with FreeBSD, move to *_unix.go files. Not implemented yet: Kernel mode not supported DNS probably does not work yet Routing also probably does not work yet SSH support did not tested yet Lack of test environment for freebsd (dedicated VM for github runners under FreeBSD required) Lack of tests for freebsd specific code info reporting need to review and also implement, for example OS reported as GENERIC instead of FreeBSD (lack of FreeBSD icon in management interface) Lack of proper client setup under FreeBSD Lack of FreeBSD port/package * Add DNS routes (#1943) Given domains are resolved periodically and resolved IPs are replaced with the new ones. Unless the flag keep_route is set to true, then only new ones are added. This option is helpful if there are long-running connections that might still point to old IP addresses from changed DNS records. * Add process posture check (#1693) Introduces a process posture check to validate the existence and active status of specific binaries on peer systems. The check ensures that files are present at specified paths, and that corresponding processes are running. This check supports Linux, Windows, and macOS systems. Co-authored-by: Evgenii <mail@skillcoder.com> Co-authored-by: Pascal Fischer <pascal@netbird.io> Co-authored-by: Zoltan Papp <zoltan.pmail@gmail.com> Co-authored-by: Viktor Liu <17948409+lixmal@users.noreply.github.com> Co-authored-by: Bethuel Mmbaga <bethuelmbaga12@gmail.com>
210 lines
5.1 KiB
Go
210 lines
5.1 KiB
Go
//go:build !android
|
|
|
|
package systemops
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"strings"
|
|
"syscall"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"github.com/vishvananda/netlink"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/routemanager/vars"
|
|
)
|
|
|
|
var expectedVPNint = "wgtest0"
|
|
var expectedLoopbackInt = "lo"
|
|
var expectedExternalInt = "dummyext0"
|
|
var expectedInternalInt = "dummyint0"
|
|
|
|
func init() {
|
|
testCases = append(testCases, []testCase{
|
|
{
|
|
name: "To more specific route without custom dialer via physical interface",
|
|
destination: "10.10.0.2:53",
|
|
expectedInterface: expectedInternalInt,
|
|
dialer: &net.Dialer{},
|
|
expectedPacket: createPacketExpectation("192.168.1.1", 12345, "10.10.0.2", 53),
|
|
},
|
|
{
|
|
name: "To more specific route (local) without custom dialer via physical interface",
|
|
destination: "127.0.10.1:53",
|
|
expectedInterface: expectedLoopbackInt,
|
|
dialer: &net.Dialer{},
|
|
expectedPacket: createPacketExpectation("127.0.0.1", 12345, "127.0.10.1", 53),
|
|
},
|
|
}...)
|
|
}
|
|
|
|
func TestEntryExists(t *testing.T) {
|
|
tempDir := t.TempDir()
|
|
tempFilePath := fmt.Sprintf("%s/rt_tables", tempDir)
|
|
|
|
content := []string{
|
|
"1000 reserved",
|
|
fmt.Sprintf("%d %s", NetbirdVPNTableID, NetbirdVPNTableName),
|
|
"9999 other_table",
|
|
}
|
|
require.NoError(t, os.WriteFile(tempFilePath, []byte(strings.Join(content, "\n")), 0644))
|
|
|
|
file, err := os.Open(tempFilePath)
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
assert.NoError(t, file.Close())
|
|
}()
|
|
|
|
tests := []struct {
|
|
name string
|
|
id int
|
|
shouldExist bool
|
|
err error
|
|
}{
|
|
{
|
|
name: "ExistsWithNetbirdPrefix",
|
|
id: 7120,
|
|
shouldExist: true,
|
|
err: nil,
|
|
},
|
|
{
|
|
name: "ExistsWithDifferentName",
|
|
id: 1000,
|
|
shouldExist: true,
|
|
err: ErrTableIDExists,
|
|
},
|
|
{
|
|
name: "DoesNotExist",
|
|
id: 1234,
|
|
shouldExist: false,
|
|
err: nil,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
exists, err := entryExists(file, tc.id)
|
|
if tc.err != nil {
|
|
assert.ErrorIs(t, err, tc.err)
|
|
} else {
|
|
assert.NoError(t, err)
|
|
}
|
|
assert.Equal(t, tc.shouldExist, exists)
|
|
})
|
|
}
|
|
}
|
|
|
|
func createAndSetupDummyInterface(t *testing.T, interfaceName, ipAddressCIDR string) string {
|
|
t.Helper()
|
|
|
|
dummy := &netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Name: interfaceName}}
|
|
err := netlink.LinkDel(dummy)
|
|
if err != nil && !errors.Is(err, syscall.EINVAL) {
|
|
t.Logf("Failed to delete dummy interface: %v", err)
|
|
}
|
|
|
|
err = netlink.LinkAdd(dummy)
|
|
require.NoError(t, err)
|
|
|
|
err = netlink.LinkSetUp(dummy)
|
|
require.NoError(t, err)
|
|
|
|
if ipAddressCIDR != "" {
|
|
addr, err := netlink.ParseAddr(ipAddressCIDR)
|
|
require.NoError(t, err)
|
|
err = netlink.AddrAdd(dummy, addr)
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
t.Cleanup(func() {
|
|
err := netlink.LinkDel(dummy)
|
|
assert.NoError(t, err)
|
|
})
|
|
|
|
return dummy.Name
|
|
}
|
|
|
|
func addDummyRoute(t *testing.T, dstCIDR string, gw net.IP, intf string) {
|
|
t.Helper()
|
|
|
|
_, dstIPNet, err := net.ParseCIDR(dstCIDR)
|
|
require.NoError(t, err)
|
|
|
|
// Handle existing routes with metric 0
|
|
var originalNexthop net.IP
|
|
var originalLinkIndex int
|
|
if dstIPNet.String() == "0.0.0.0/0" {
|
|
var err error
|
|
originalNexthop, originalLinkIndex, err = fetchOriginalGateway(netlink.FAMILY_V4)
|
|
if err != nil && !errors.Is(err, vars.ErrRouteNotFound) {
|
|
t.Logf("Failed to fetch original gateway: %v", err)
|
|
}
|
|
|
|
if originalNexthop != nil {
|
|
err = netlink.RouteDel(&netlink.Route{Dst: dstIPNet, Priority: 0})
|
|
switch {
|
|
case err != nil && !errors.Is(err, syscall.ESRCH):
|
|
t.Logf("Failed to delete route: %v", err)
|
|
case err == nil:
|
|
t.Cleanup(func() {
|
|
err := netlink.RouteAdd(&netlink.Route{Dst: dstIPNet, Gw: originalNexthop, LinkIndex: originalLinkIndex, Priority: 0})
|
|
if err != nil && !errors.Is(err, syscall.EEXIST) {
|
|
t.Fatalf("Failed to add route: %v", err)
|
|
}
|
|
})
|
|
default:
|
|
t.Logf("Failed to delete route: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
link, err := netlink.LinkByName(intf)
|
|
require.NoError(t, err)
|
|
linkIndex := link.Attrs().Index
|
|
|
|
route := &netlink.Route{
|
|
Dst: dstIPNet,
|
|
Gw: gw,
|
|
LinkIndex: linkIndex,
|
|
}
|
|
err = netlink.RouteDel(route)
|
|
if err != nil && !errors.Is(err, syscall.ESRCH) {
|
|
t.Logf("Failed to delete route: %v", err)
|
|
}
|
|
|
|
err = netlink.RouteAdd(route)
|
|
if err != nil && !errors.Is(err, syscall.EEXIST) {
|
|
t.Fatalf("Failed to add route: %v", err)
|
|
}
|
|
require.NoError(t, err)
|
|
}
|
|
|
|
func fetchOriginalGateway(family int) (net.IP, int, error) {
|
|
routes, err := netlink.RouteList(nil, family)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
for _, route := range routes {
|
|
if route.Dst == nil && route.Priority == 0 {
|
|
return route.Gw, route.LinkIndex, nil
|
|
}
|
|
}
|
|
|
|
return nil, 0, vars.ErrRouteNotFound
|
|
}
|
|
|
|
func setupDummyInterfacesAndRoutes(t *testing.T) {
|
|
t.Helper()
|
|
|
|
defaultDummy := createAndSetupDummyInterface(t, "dummyext0", "192.168.0.1/24")
|
|
addDummyRoute(t, "0.0.0.0/0", net.IPv4(192, 168, 0, 1), defaultDummy)
|
|
|
|
otherDummy := createAndSetupDummyInterface(t, "dummyint0", "192.168.1.1/24")
|
|
addDummyRoute(t, "10.0.0.0/8", net.IPv4(192, 168, 1, 1), otherDummy)
|
|
}
|