Skip to content

Commit

Permalink
Remove unused meshnet functionality
Browse files Browse the repository at this point in the history
Signed-off-by: Savolro <me@savolro.com>
  • Loading branch information
Savolro committed Feb 26, 2025
1 parent 7eea6b9 commit 0ce7b9d
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 496 deletions.
31 changes: 2 additions & 29 deletions daemon/jobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,7 @@ func (invitationsAPI) Accept(string, uuid.UUID, uuid.UUID, bool, bool, bool, boo
func (invitationsAPI) Revoke(string, uuid.UUID, uuid.UUID) error { return nil }
func (invitationsAPI) Reject(string, uuid.UUID, uuid.UUID) error { return nil }

type meshNetworker struct {
allowedIncoming []meshnet.UniqueAddress
blockedIncoming []meshnet.UniqueAddress
allowedFileshare []meshnet.UniqueAddress
blockedFileshare []meshnet.UniqueAddress
}
type meshNetworker struct{}

func (meshNetworker) Start(
context.Context,
Expand All @@ -124,37 +119,15 @@ func (*meshNetworker) Stop() error { retur
func (*meshNetworker) SetMesh(mesh.MachineMap, netip.Addr, string) error { return nil }
func (*meshNetworker) UnSetMesh() error { return nil }

func (n *meshNetworker) AllowFileshare(address meshnet.UniqueAddress) error {
n.allowedFileshare = append(n.allowedFileshare, address)
return nil
}

func (n *meshNetworker) PermitFileshare() error {
return nil
}

func (n *meshNetworker) AllowIncoming(address meshnet.UniqueAddress, lanAllowed bool) error {
n.allowedIncoming = append(n.allowedIncoming, address)
return nil
}

func (n *meshNetworker) BlockIncoming(address meshnet.UniqueAddress) error {
n.blockedIncoming = append(n.blockedIncoming, address)
return nil
}

func (n *meshNetworker) ForbidFileshare() error {
return nil
}

func (n *meshNetworker) BlockFileshare(address meshnet.UniqueAddress) error {
n.blockedFileshare = append(n.blockedFileshare, address)
return nil
}

func (*meshNetworker) ResetRouting(mesh.MachinePeer, mesh.MachinePeers) error { return nil }
func (*meshNetworker) BlockRouting(meshnet.UniqueAddress) error { return nil }
func (*meshNetworker) Refresh(mesh.MachineMap) error { return nil }
func (*meshNetworker) Refresh(mesh.MachineMap) error { return nil }
func (*meshNetworker) StatusMap() (map[string]string, error) {
return map[string]string{}, nil
}
Expand Down
10 changes: 9 additions & 1 deletion meshnet/mapper/caching.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
"github.com/google/uuid"
)

// CachingMapper is an implementation of mesh.CachingMapper that wraps inner Mapper and caching
// logic from CachedValue.
type CachingMapper struct {
mmap *CachedValue[retrievalKey, *mesh.MachineMap]
}

// NewCachingMapper returns a new instance of CachingMapper filled newly created CachedValue and
// inner.Map function as GetFn for CachedValue.
func NewCachingMapper(inner mesh.Mapper, cacheTTL time.Duration) *CachingMapper {
mapFn := func(key retrievalKey) (*mesh.MachineMap, error) {
return inner.Map(key.token, key.id)
Expand All @@ -21,6 +25,7 @@ func NewCachingMapper(inner mesh.Mapper, cacheTTL time.Duration) *CachingMapper
}
}

// Map uses CachedValue.Get function, where GetFn is inner.Map function.
func (r *CachingMapper) Map(
token string,
self uuid.UUID,
Expand All @@ -29,6 +34,8 @@ func (r *CachingMapper) Map(
return r.mmap.Get(retrievalKey{token: token, id: self}, forceUpdate)
}

// retrievalKey is a structure acting as a key to CachedValue which composes of fields that are
// arguments to mesh.Mapper.Map function.
type retrievalKey struct {
token string
id uuid.UUID
Expand All @@ -44,9 +51,10 @@ type CachedValue[K comparable, V any] struct {
mu sync.Mutex
}

// GetFn is a function that returns data and error in case it failed
// GetFn is a function that returns data and error in case it failed.
type GetFn[K, V any] func(K) (V, error)

// NewCachedValue creates a new instance of CachedValue filled with the given parameters.
func NewCachedValue[K comparable, V any](
validity time.Duration,
getFn GetFn[K, V],
Expand Down
163 changes: 163 additions & 0 deletions meshnet/mapper/caching_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package mapper

import (
"io"
"testing"
"time"

"github.com/NordSecurity/nordvpn-linux/core/mesh"
"github.com/NordSecurity/nordvpn-linux/test/category"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

type mapper struct {
value *mesh.MachineMap
err error
}

func (m *mapper) Map(_ string, _ uuid.UUID) (*mesh.MachineMap, error) {
return m.value, m.err
}

// TestCachingMapper_Map simply checks whether inner mapper is used in the implementation without
// checking the CachedValue internals.
func TestCachingMapper_Map(t *testing.T) {
for _, tt := range []struct {
name string
err error
inner mesh.Mapper
mmap *mesh.MachineMap
}{
{
name: "no error",
err: nil,
inner: &mapper{value: &mesh.MachineMap{}},
mmap: &mesh.MachineMap{},
},
{
name: "error",
err: io.EOF,
inner: &mapper{err: io.EOF},
},
} {
t.Run(tt.name, func(t *testing.T) {
mapper := NewCachingMapper(tt.inner, time.Second)
mmap, err := mapper.Map("any", uuid.New(), false)
assert.ErrorIs(t, tt.err, err)
assert.EqualValues(t, tt.mmap, mmap)
assert.Equal(t, tt.err == nil, mapper.mmap.cachedDate != time.Time{})
assert.Equal(t, tt.err == nil, mapper.mmap.value != nil)
})
}
}

func TestCachedValue_Get(t *testing.T) {
category.Set(t, category.Unit)
fnRetOne := func(_ int) (int, error) {
return 1, nil
}
for _, tt := range []struct {
name string
key int
forceUpdate bool
err error
expValue int
updated bool
cv *CachedValue[int, int]
}{
{
name: "return func res",
expValue: 1,
key: 1,
updated: true,
cv: NewCachedValue(time.Second, func(key int) (int, error) {
return key, nil
}),
},
{
name: "return cached value on nil GetFn",
expValue: 0,
key: 1,
updated: false,
cv: &CachedValue[int, int]{},
},
{
name: "get while cache still valid",
expValue: 2,
key: 2,
updated: false,
cv: func() *CachedValue[int, int] {
cv := NewCachedValue(time.Second, fnRetOne)
cv.cachedDate = time.Now()
cv.value = 2
cv.key = 2
return cv
}(),
},
{
name: "forceUpdate causes get fn call",
expValue: 1,
key: 2,
forceUpdate: true,
updated: true,
cv: func() *CachedValue[int, int] {
cv := NewCachedValue(time.Second, fnRetOne)
cv.cachedDate = time.Now()
cv.value = 2
cv.key = 2
return cv
}(),
},
{
name: "outdated value causes get fn call",
expValue: 1,
key: 2,
updated: true,
cv: func() *CachedValue[int, int] {
cv := NewCachedValue(time.Second, fnRetOne)
cv.value = 2
cv.key = 2
return cv
}(),
},
{
name: "new key value causes get fn call",
expValue: 1,
key: 3,
updated: true,
cv: func() *CachedValue[int, int] {
cv := NewCachedValue(time.Second, fnRetOne)
cv.cachedDate = time.Now()
cv.value = 2
cv.key = 2
return cv
}(),
},
{
name: "failing function does not update",
expValue: 0,
key: 2,
err: io.EOF,
forceUpdate: true,
updated: false,
cv: func() *CachedValue[int, int] {
cv := NewCachedValue(time.Second, func(_ int) (int, error) {
return 0, io.EOF
})
cv.cachedDate = time.Now()
cv.value = 2
cv.key = 2
return cv
}(),
},
} {
t.Run(tt.name, func(t *testing.T) {
cachedBefore := tt.cv.cachedDate
val, err := tt.cv.Get(tt.key, tt.forceUpdate)
assert.Equal(t, tt.expValue, val)
assert.ErrorIs(t, tt.err, err)
assert.Equal(t, tt.updated, cachedBefore.Before(tt.cv.cachedDate))
})
}
}
14 changes: 0 additions & 14 deletions meshnet/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,10 @@ type Networker interface {
Refresh(mesh.MachineMap) error // Remove
// UnSetMesh unsets the meshnet configuration
UnSetMesh() error
// AllowIncoming creates an allowing fw rule for the given
// address
AllowIncoming(address UniqueAddress, lanAllowed bool) error
// BlockIncoming creates a blocking fw rule for the given
// address
BlockIncoming(UniqueAddress) error
// AllowFileshare creates a rule enabling fileshare port for the given address
AllowFileshare(UniqueAddress) error
// PermitFileshare creates a rules enabling fileshare port for all available peers and sets fileshare as permitted
PermitFileshare() error
// BlockFileshare removes a rule enabling fileshare port for the given address if it exists
BlockFileshare(UniqueAddress) error
// ForbidFileshare removes a rules enabling fileshare port for all available peers and sets fileshare as forbidden
ForbidFileshare() error
// ResetRouting is used when there are routing setting changes,
// except when routing is denied - then BlockRouting must be used. changedPeer is the peer whose routing settings
// changed, peers is the map of all the machine peers(including the changed peer).
ResetRouting(changedPeer mesh.MachinePeer, peers mesh.MachinePeers) error
StatusMap() (map[string]string, error)
LastServerName() string
Start(
Expand Down
46 changes: 2 additions & 44 deletions meshnet/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,7 @@ type registrationChecker struct {
func (r registrationChecker) IsRegistrationInfoCorrect() bool { return r.registrationErr == nil }
func (r registrationChecker) Register() error { return r.registrationErr }

type allowedIncoming struct {
address UniqueAddress
lanAllowed bool
}

type workingNetworker struct {
allowedIncoming []allowedIncoming
blockedIncoming []UniqueAddress
allowedFileshare []UniqueAddress
blockedFileshare []UniqueAddress
resetPeers []string
}
type workingNetworker struct{}

func (workingNetworker) Start(
context.Context,
Expand All @@ -89,46 +78,15 @@ func (*workingNetworker) Stop() error { re
func (*workingNetworker) SetMesh(mesh.MachineMap, netip.Addr, string) error { return nil }
func (*workingNetworker) UnSetMesh() error { return nil }

func (n *workingNetworker) AllowFileshare(address UniqueAddress) error {
n.allowedFileshare = append(n.allowedFileshare, address)
return nil
}

func (n *workingNetworker) PermitFileshare() error {
return nil
}

func (n *workingNetworker) AllowIncoming(address UniqueAddress, lanAllowed bool) error {
n.allowedIncoming = append(n.allowedIncoming, allowedIncoming{
address: address,
lanAllowed: lanAllowed,
})

return nil
}

func (n *workingNetworker) BlockIncoming(address UniqueAddress) error {
n.blockedIncoming = append(n.blockedIncoming, address)
return nil
}

func (n *workingNetworker) BlockFileshare(address UniqueAddress) error {
n.blockedFileshare = append(n.blockedFileshare, address)
return nil
}

func (n *workingNetworker) ForbidFileshare() error {
return nil
}

func (n *workingNetworker) ResetRouting(changedPeer mesh.MachinePeer, peer mesh.MachinePeers) error {
n.resetPeers = append(n.resetPeers, changedPeer.PublicKey)

return nil
}

func (*workingNetworker) BlockRouting(UniqueAddress) error { return nil }
func (*workingNetworker) Refresh(mesh.MachineMap) error { return nil }
func (*workingNetworker) Refresh(mesh.MachineMap) error { return nil }
func (*workingNetworker) StatusMap() (map[string]string, error) {
return map[string]string{}, nil
}
Expand Down
21 changes: 0 additions & 21 deletions networker/networker.go
Original file line number Diff line number Diff line change
Expand Up @@ -1465,13 +1465,6 @@ func (netw *Combined) StatusMap() (map[string]string, error) {
return netw.mesh.StatusMap()
}

// AllowIncoming traffic from the uniqueAddress.
func (netw *Combined) AllowIncoming(uniqueAddress meshnet.UniqueAddress, lanAllowed bool) error {
netw.mu.Lock()
defer netw.mu.Unlock()
return netw.allowIncoming(uniqueAddress.UID, uniqueAddress.Address, lanAllowed)
}

func (netw *Combined) allowIncoming(publicKey string, address netip.Addr, lanAllowed bool) error {
rules := []firewall.Rule{}

Expand Down Expand Up @@ -1632,14 +1625,6 @@ func (netw *Combined) denyDNS() error {
return nil
}

// Unblock address.
func (netw *Combined) BlockIncoming(uniqueAddress meshnet.UniqueAddress) error {
netw.mu.Lock()
defer netw.mu.Unlock()

return netw.blockIncoming(uniqueAddress)
}

func (netw *Combined) blockIncoming(uniqueAddress meshnet.UniqueAddress) error {
lanRuleName := uniqueAddress.UID + blockLanRule + uniqueAddress.Address.String()
if slices.Index(netw.rules, lanRuleName) != -1 {
Expand All @@ -1652,12 +1637,6 @@ func (netw *Combined) blockIncoming(uniqueAddress meshnet.UniqueAddress) error {
return netw.removeRule(ruleName)
}

func (netw *Combined) BlockFileshare(uniqueAddress meshnet.UniqueAddress) error {
netw.mu.Lock()
defer netw.mu.Unlock()
return netw.blockFileshare(uniqueAddress.UID, uniqueAddress.Address)
}

func (netw *Combined) blockFileshare(publicKey string, address netip.Addr) error {
if !netw.isFilesharePermitted {
log.Println(internal.WarningPrefix, "fileshare is already forbidden")
Expand Down
Loading

0 comments on commit 0ce7b9d

Please sign in to comment.