@@ -4,10 +4,6 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"log"
7
- "os"
8
- "path/filepath"
9
- "strconv"
10
- "strings"
11
7
"sync"
12
8
13
9
"github.com/NordSecurity/nordvpn-linux/config"
@@ -24,17 +20,30 @@ type FilesharePortAccessController struct {
24
20
netw Networker
25
21
reg mesh.Registry
26
22
filesharePID PID
27
- processChecker processChecker
23
+ processChecker ProcessChecker
28
24
mu sync.Mutex
29
25
}
30
26
31
- func NewPortAccessController (cm config.Manager , netw Networker , reg mesh.Registry ) FilesharePortAccessController {
27
+ func NewPortAccessController (
28
+ cm config.Manager ,
29
+ netw Networker ,
30
+ reg mesh.Registry ,
31
+ pc ProcessChecker ,
32
+ ) FilesharePortAccessController {
33
+ filesharePID := PID (0 )
34
+ // NOTE:if the fileshare is already running, set the initial PID.
35
+ // This can happen only when the daemon was restarted, but nordfileshare
36
+ // process was not - for example there was a panic in daemon.
37
+ PID := pc .GiveProcessPID (internal .FileshareBinaryPath )
38
+ if PID != nil {
39
+ filesharePID = * PID
40
+ }
32
41
return FilesharePortAccessController {
33
42
cm : cm ,
34
43
netw : netw ,
35
44
reg : reg ,
36
- filesharePID : 0 ,
37
- processChecker : defaultProcChecker {} ,
45
+ filesharePID : filesharePID ,
46
+ processChecker : pc ,
38
47
}
39
48
}
40
49
@@ -44,9 +53,21 @@ func (eventHandler *FilesharePortAccessController) OnProcessStarted(ev ProcEvent
44
53
// process next events anymore until the PID gets reset in [EventHandler.OnProcessStopped]
45
54
return
46
55
}
47
- if ! eventHandler .processChecker .isFileshareProcess (ev .PID ) {
56
+
57
+ // NOTE: at this point, we can ignore older processes. It's because
58
+ // we checked above that the [eventHandler.filesharePID] is not set
59
+ // which means that nordfileshare process was not running at the time
60
+ // of creation of [FilesharePortAccessController] - constructor checks
61
+ // if nordfilshare is already running - so we know that nordfileshare
62
+ // PID will be higher than the daemon PID.
63
+ if ev .PID < eventHandler .processChecker .CurrentPID () {
64
+ return
65
+ }
66
+
67
+ if ! eventHandler .processChecker .IsFileshareProcess (ev .PID ) {
48
68
return
49
69
}
70
+
50
71
log .Println (internal .InfoPrefix , "updating fileshare process pid to:" , ev .PID )
51
72
eventHandler .filesharePID = ev .PID
52
73
go eventHandler .allowFileshare ()
@@ -121,43 +142,9 @@ func (eventHandler *FilesharePortAccessController) blockFileshare() error {
121
142
return nil
122
143
}
123
144
124
- // processChecker allows checking if process with specified [PID]
125
- // is a fileshare process.
126
- type processChecker interface {
127
- isFileshareProcess (PID ) bool
128
- }
129
-
130
- // defaultProcChecker allows checking if process specified by [PID]
131
- // is a fileshare process by reading its path via `/proc/<pid>/cmdline`.
132
- type defaultProcChecker struct {}
133
-
134
- func (defaultProcChecker ) isFileshareProcess (pid PID ) bool {
135
- // ignore older processes, fileshare is always
136
- // younger than the daemon so it has higher PID
137
- if pid < PID (os .Getpid ()) {
138
- return false
139
- }
140
-
141
- procPath , err := readProcPath (pid )
142
- if err != nil {
143
- log .Println (internal .ErrorPrefix , "failed to read process path from /proc" , err )
144
- return false
145
- }
146
-
147
- return procPath == internal .FileshareBinaryPath
148
- }
149
-
150
- func readProcPath (pid PID ) (string , error ) {
151
- pidStr := strconv .FormatUint (uint64 (pid ), 10 )
152
- cmdlinePath := filepath .Join ("/proc" , pidStr , "cmdline" )
153
-
154
- cmdline , err := os .ReadFile (cmdlinePath )
155
- if err != nil {
156
- return "" , err
157
- }
158
- args := strings .Split (string (cmdline ), "\x00 " )
159
- if len (args ) == 0 {
160
- return "" , ErrIncorrectCmdlineContent
161
- }
162
- return args [0 ], nil
145
+ // ProcessChecker represents process-related utilities
146
+ type ProcessChecker interface {
147
+ IsFileshareProcess (PID ) bool
148
+ GiveProcessPID (string ) * PID
149
+ CurrentPID () PID
163
150
}
0 commit comments