Skip to content

Commit 7f1de15

Browse files
committed
Improve fileshare monitoring job
1 parent 9449530 commit 7f1de15

File tree

6 files changed

+83
-25
lines changed

6 files changed

+83
-25
lines changed

daemon/jobs_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func (failingLoginChecker) IsMFAEnabled() (bool, error) { return false, nil }
3636
func (failingLoginChecker) IsVPNExpired() (bool, error) {
3737
return true, errors.New("IsVPNExpired error")
3838
}
39+
3940
func (failingLoginChecker) GetDedicatedIPServices() ([]auth.DedicatedIPService, error) {
4041
return nil, fmt.Errorf("Not implemented")
4142
}
@@ -128,6 +129,10 @@ func (n *meshNetworker) AllowFileshare(address meshnet.UniqueAddress) error {
128129
return nil
129130
}
130131

132+
func (n *meshNetworker) PermitFileshare() error {
133+
return nil
134+
}
135+
131136
func (n *meshNetworker) AllowIncoming(address meshnet.UniqueAddress, lanAllowed bool) error {
132137
n.allowedIncoming = append(n.allowedIncoming, address)
133138
return nil
@@ -138,6 +143,10 @@ func (n *meshNetworker) BlockIncoming(address meshnet.UniqueAddress) error {
138143
return nil
139144
}
140145

146+
func (n *meshNetworker) ForbidFileshare() error {
147+
return nil
148+
}
149+
141150
func (n *meshNetworker) BlockFileshare(address meshnet.UniqueAddress) error {
142151
n.blockedFileshare = append(n.blockedFileshare, address)
143152
return nil

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ github.com/NordSecurity/gopenvpn v0.0.0-20230117114932-2252c52984b4 h1:2ozEjYEw4
1212
github.com/NordSecurity/gopenvpn v0.0.0-20230117114932-2252c52984b4/go.mod h1:tguhorMSnkMcQExNIHWBX6TRhFeGYlERzbeAWZ4j9Uw=
1313
github.com/NordSecurity/libdrop-go/v8 v8.0.0-20241017064027-670787595588 h1:L/nAbQXJGCOFqw1eTTRYEBKmiuaQQeS7b863+0Ifevw=
1414
github.com/NordSecurity/libdrop-go/v8 v8.0.0-20241017064027-670787595588/go.mod h1:SRYI0D0K6hSMBskvcB2/t/5ktSTNLPGbOvLaQ5p/sAE=
15-
github.com/NordSecurity/libtelio-go/v5 v5.1.4 h1:o2JbYad8sdRsljFAMZRVmkXGQ7kTVbS32P6TTDOTuL0=
16-
github.com/NordSecurity/libtelio-go/v5 v5.1.4/go.mod h1:mnoTGgXOu8dBQgPxG8MBju4d9C+ljKIT2p8OX5GFom4=
1715
github.com/NordSecurity/libtelio-go/v5 v5.1.5 h1:lwP7m2GJ+GkO1EDaRqm5ymDT/CtjIBC/1bN2CL55mnY=
1816
github.com/NordSecurity/libtelio-go/v5 v5.1.5/go.mod h1:mnoTGgXOu8dBQgPxG8MBju4d9C+ljKIT2p8OX5GFom4=
1917
github.com/NordSecurity/systray v0.0.0-20240327004800-3e3b59c1b83d h1:oUEFXgFRa9Svcjr+O1stzR3vEXZ5OfQxLUcDjqFcOuo=

meshnet/jobs.go

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func (s *Server) StartJobs() {
1515
}
1616

1717
if _, err := s.scheduler.NewJob(
18-
gocron.DurationJob(5*time.Second),
18+
gocron.DurationJob(500*time.Millisecond),
1919
gocron.NewTask(JobMonitorFileshareProcess(s)),
2020
gocron.WithName("job monitor fileshare process")); err != nil {
2121
log.Println(internal.WarningPrefix, "job monitor fileshare process schedule error:", err)
@@ -39,32 +39,16 @@ func JobRefreshMeshnet(s *Server) func() error {
3939
}
4040

4141
func JobMonitorFileshareProcess(s *Server) func() error {
42-
oldState := false
4342
return func() error {
4443
if !s.isMeshOn() {
4544
return nil
4645
}
47-
newState := internal.IsProcessRunning(internal.FileshareBinaryPath)
48-
if newState == oldState {
49-
// only state change triggers the modifications
50-
return nil
51-
}
52-
53-
log.Println(internal.InfoPrefix, "fileshare change to running", newState)
54-
peers, err := s.listPeers()
55-
if err != nil {
56-
return err
57-
}
5846

59-
isFileshareUp := newState
60-
for _, peer := range peers {
61-
if !isFileshareUp {
62-
s.netw.BlockFileshare(UniqueAddress{UID: peer.PublicKey, Address: peer.Address})
63-
} else {
64-
s.netw.AllowFileshare(UniqueAddress{UID: peer.PublicKey, Address: peer.Address})
65-
}
47+
if internal.IsProcessRunning(internal.FileshareBinaryPath) {
48+
s.netw.PermitFileshare()
49+
} else {
50+
s.netw.ForbidFileshare()
6651
}
67-
oldState = newState
6852

6953
return nil
7054
}

meshnet/networker.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,12 @@ type Networker interface {
2828
BlockIncoming(UniqueAddress) error
2929
// AllowFileshare creates a rule enabling fileshare port for the given address
3030
AllowFileshare(UniqueAddress) error
31+
// PermitFileshare creates a rules enabling fileshare port for all available peers and sets fileshare as permitted
32+
PermitFileshare() error
3133
// BlockFileshare removes a rule enabling fileshare port for the given address if it exists
3234
BlockFileshare(UniqueAddress) error
35+
// ForbidFileshare removes a rules enabling fileshare port for all available peers and sets fileshare as forbidden
36+
ForbidFileshare() error
3337
// ResetRouting is used when there are routing setting changes,
3438
// except when routing is denied - then BlockRouting must be used. changedPeer is the peer whose routing settings
3539
// changed, peers is the map of all the machine peers(including the changed peer).

meshnet/server_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type meshRenewChecker struct {
4444
func (m meshRenewChecker) IsLoggedIn() bool {
4545
return !m.IsNotLoggedIn
4646
}
47+
4748
func (m meshRenewChecker) IsMFAEnabled() (bool, error) {
4849
return false, nil
4950
}
@@ -92,6 +93,10 @@ func (n *workingNetworker) AllowFileshare(address UniqueAddress) error {
9293
return nil
9394
}
9495

96+
func (n *workingNetworker) PermitFileshare() error {
97+
return nil
98+
}
99+
95100
func (n *workingNetworker) AllowIncoming(address UniqueAddress, lanAllowed bool) error {
96101
n.allowedIncoming = append(n.allowedIncoming, allowedIncoming{
97102
address: address,
@@ -111,6 +116,10 @@ func (n *workingNetworker) BlockFileshare(address UniqueAddress) error {
111116
return nil
112117
}
113118

119+
func (n *workingNetworker) ForbidFileshare() error {
120+
return nil
121+
}
122+
114123
func (n *workingNetworker) ResetRouting(changedPeer mesh.MachinePeer, peer mesh.MachinePeers) error {
115124
n.resetPeers = append(n.resetPeers, changedPeer.PublicKey)
116125

networker/networker.go

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,8 @@ type Combined struct {
168168
enableLocalTraffic bool
169169
// list with the existing OS interfaces when VPN was connected.
170170
// This is used at network changes to know when a new interface was inserted
171-
interfaces mapset.Set[string]
171+
interfaces mapset.Set[string]
172+
isFilesharePermitted bool
172173
}
173174

174175
// NewCombined returns a ready made version of
@@ -1509,6 +1510,11 @@ func (netw *Combined) AllowFileshare(uniqueAddress meshnet.UniqueAddress) error
15091510
}
15101511

15111512
func (netw *Combined) allowFileshare(publicKey string, address netip.Addr) error {
1513+
if !netw.isFilesharePermitted {
1514+
log.Println(internal.WarningPrefix, "fileshare is not permitted, can't add allow rules")
1515+
return nil
1516+
}
1517+
15121518
ruleName := publicKey + "-allow-fileshare-rule-" + address.String()
15131519
rules := []firewall.Rule{{
15141520
Name: ruleName,
@@ -1537,6 +1543,27 @@ func (netw *Combined) allowFileshare(publicKey string, address netip.Addr) error
15371543
return nil
15381544
}
15391545

1546+
func (netw *Combined) PermitFileshare() error {
1547+
netw.mu.Lock()
1548+
defer netw.mu.Unlock()
1549+
if netw.isFilesharePermitted {
1550+
return nil
1551+
}
1552+
netw.isFilesharePermitted = true
1553+
return netw.allowFileshareAll()
1554+
}
1555+
1556+
func (netw *Combined) allowFileshareAll() error {
1557+
var allErrors []error
1558+
for _, peer := range netw.cfg.Peers {
1559+
if peer.DoIAllowFileshare {
1560+
err := netw.allowFileshare(peer.PublicKey, peer.Address)
1561+
allErrors = append(allErrors, err)
1562+
}
1563+
}
1564+
return errors.Join(allErrors...)
1565+
}
1566+
15401567
func (netw *Combined) undenyDNS() error {
15411568
ruleName := "deny-private-dns"
15421569

@@ -1608,7 +1635,15 @@ func (netw *Combined) blockIncoming(uniqueAddress meshnet.UniqueAddress) error {
16081635
func (netw *Combined) BlockFileshare(uniqueAddress meshnet.UniqueAddress) error {
16091636
netw.mu.Lock()
16101637
defer netw.mu.Unlock()
1611-
ruleName := uniqueAddress.UID + "-allow-fileshare-rule-" + uniqueAddress.Address.String()
1638+
return netw.blockFileshare(uniqueAddress.UID, uniqueAddress.Address)
1639+
}
1640+
1641+
func (netw *Combined) blockFileshare(publicKey string, address netip.Addr) error {
1642+
if !netw.isFilesharePermitted {
1643+
log.Println(internal.WarningPrefix, "fileshare is already forbidden")
1644+
return nil
1645+
}
1646+
ruleName := publicKey + "-allow-fileshare-rule-" + address.String()
16121647
return netw.removeRule(ruleName)
16131648
}
16141649

@@ -1627,6 +1662,25 @@ func (netw *Combined) removeRule(ruleName string) error {
16271662
return nil
16281663
}
16291664

1665+
func (netw *Combined) ForbidFileshare() error {
1666+
netw.mu.Lock()
1667+
defer netw.mu.Unlock()
1668+
if !netw.isFilesharePermitted {
1669+
return nil
1670+
}
1671+
defer func() { netw.isFilesharePermitted = false }()
1672+
return netw.blockFileshareAll()
1673+
}
1674+
1675+
func (netw *Combined) blockFileshareAll() error {
1676+
var allErrors []error
1677+
for _, peer := range netw.cfg.Peers {
1678+
err := netw.blockFileshare(peer.PublicKey, peer.Address)
1679+
allErrors = append(allErrors, err)
1680+
}
1681+
return errors.Join(allErrors...)
1682+
}
1683+
16301684
func getHostsFromConfig(peers mesh.MachinePeers) dns.Hosts {
16311685
hosts := make(dns.Hosts, 0, len(peers))
16321686
for _, peer := range peers {

0 commit comments

Comments
 (0)