mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-22 02:06:39 +00:00
Wails UI
This commit is contained in:
39
client/uiwails/process/process.go
Normal file
39
client/uiwails/process/process.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package process
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/process"
|
||||
)
|
||||
|
||||
// IsAnotherProcessRunning returns the PID and true if another instance of the
|
||||
// same binary is already running for the current OS user.
|
||||
func IsAnotherProcessRunning() (int32, bool, error) {
|
||||
processes, err := process.Processes()
|
||||
if err != nil {
|
||||
return 0, false, err
|
||||
}
|
||||
|
||||
pid := os.Getpid()
|
||||
processName := strings.ToLower(filepath.Base(os.Args[0]))
|
||||
|
||||
for _, p := range processes {
|
||||
if int(p.Pid) == pid {
|
||||
continue
|
||||
}
|
||||
|
||||
runningProcessPath, err := p.Exe()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
runningProcessName := strings.ToLower(filepath.Base(runningProcessPath))
|
||||
if runningProcessName == processName && isProcessOwnedByCurrentUser(p) {
|
||||
return p.Pid, true, nil
|
||||
}
|
||||
}
|
||||
|
||||
return 0, false, nil
|
||||
}
|
||||
25
client/uiwails/process/process_nonwindows.go
Normal file
25
client/uiwails/process/process_nonwindows.go
Normal file
@@ -0,0 +1,25 @@
|
||||
//go:build !windows
|
||||
|
||||
package process
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/process"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func isProcessOwnedByCurrentUser(p *process.Process) bool {
|
||||
currentUserID := os.Getuid()
|
||||
uids, err := p.Uids()
|
||||
if err != nil {
|
||||
log.Errorf("get process uids: %v", err)
|
||||
return false
|
||||
}
|
||||
for _, id := range uids {
|
||||
if int(id) == currentUserID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
24
client/uiwails/process/process_windows.go
Normal file
24
client/uiwails/process/process_windows.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package process
|
||||
|
||||
import (
|
||||
"os/user"
|
||||
|
||||
"github.com/shirou/gopsutil/v3/process"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func isProcessOwnedByCurrentUser(p *process.Process) bool {
|
||||
processUsername, err := p.Username()
|
||||
if err != nil {
|
||||
log.Errorf("get process username error: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
currUser, err := user.Current()
|
||||
if err != nil {
|
||||
log.Errorf("get current user error: %v", err)
|
||||
return false
|
||||
}
|
||||
|
||||
return processUsername == currUser.Username
|
||||
}
|
||||
Reference in New Issue
Block a user