-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restart norduser/tray when user switches from GUI interface to text only
- Loading branch information
1 parent
21fc29e
commit 04d36c8
Showing
15 changed files
with
1,117 additions
and
595 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package norduser | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
) | ||
|
||
func findVariable(name string, environment string) string { | ||
variables := strings.Split(environment, "\000") | ||
for _, variable := range variables { | ||
split := strings.Split(variable, "=") | ||
if len(split) < 1 { | ||
continue | ||
} | ||
|
||
if split[0] != name { | ||
continue | ||
} | ||
|
||
if len(split) != 2 { | ||
return "" | ||
} | ||
|
||
return split[1] | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func findEnvVariableForPID(pid uint32, name string) (string, error) { | ||
path := fmt.Sprintf("/proc/%d/environ", pid) | ||
environment, err := os.ReadFile(path) | ||
if err != nil { | ||
return "", fmt.Errorf("reading file: %w", err) | ||
} | ||
|
||
return findVariable(name, string(environment)), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package norduser | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/NordSecurity/nordvpn-linux/test/category" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_findVariable(t *testing.T) { | ||
category.Set(t, category.Unit) | ||
|
||
const variable1 string = "VAR_1" | ||
const value1 string = "val_1" | ||
|
||
const variable2 string = "VAR_2" | ||
const value2 string = "val_2" | ||
|
||
const emptyVariable string = "VAR_3" | ||
|
||
environment := fmt.Sprintf("%s=%s\000%s=%s\000%s=", | ||
variable1, value1, | ||
variable2, value2, | ||
emptyVariable) | ||
|
||
tests := []struct { | ||
name string | ||
variableName string | ||
expectedValue string | ||
}{ | ||
{ | ||
name: "normal variable", | ||
variableName: variable1, | ||
expectedValue: value1, | ||
}, | ||
{ | ||
name: "empty variable", | ||
variableName: emptyVariable, | ||
expectedValue: "", | ||
}, | ||
{ | ||
name: "variable doesnt exist", | ||
variableName: "NO_VARIABLE", | ||
expectedValue: "", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
variableValue := findVariable(test.variableName, environment) | ||
assert.Equal(t, test.expectedValue, variableValue) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package norduser | ||
|
||
import ( | ||
"fmt" | ||
"os/user" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/NordSecurity/nordvpn-linux/internal" | ||
) | ||
|
||
func findGroupEntry(groups string, groupName string) string { | ||
r, _ := regexp.Compile(fmt.Sprintf("^%s:", groupName)) | ||
|
||
for _, groupEntry := range strings.Split(groups, "\n") { | ||
if r.MatchString(groupEntry) { | ||
return groupEntry | ||
} | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func getGroupEntry(groupName string) (string, error) { | ||
file, err := internal.FileRead(groupFilePath) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to read group file: %w", err) | ||
} | ||
|
||
groupEntry := findGroupEntry(string(file), groupName) | ||
if groupEntry == "" { | ||
return "", fmt.Errorf("group entry not found: %w", err) | ||
} | ||
|
||
return groupEntry, nil | ||
} | ||
|
||
func getGroupMembers(groupEntry string) []string { | ||
groupEntry = strings.TrimSuffix(groupEntry, "\n") | ||
sepIndex := strings.LastIndex(groupEntry, ":") | ||
groupMembers := groupEntry[sepIndex+1:] | ||
|
||
if groupMembers == "" { | ||
return []string{} | ||
} | ||
|
||
return strings.Split(groupMembers, ",") | ||
} | ||
|
||
func getNordVPNGroupMembers() ([]string, error) { | ||
groupEntry, err := getGroupEntry(internal.NordvpnGroup) | ||
if err != nil { | ||
return nil, fmt.Errorf("getting group entry: %w", err) | ||
} | ||
|
||
return getGroupMembers(groupEntry), nil | ||
} | ||
|
||
type userIDs struct { | ||
uid uint32 | ||
gid uint32 | ||
home string | ||
} | ||
|
||
type userIDGetter interface { | ||
getUserID(username string) (userIDs, error) | ||
} | ||
|
||
type osGetter struct{} | ||
|
||
func (osGetter) getUserID(username string) (userIDs, error) { | ||
user, err := user.Lookup(username) | ||
if err != nil { | ||
return userIDs{}, fmt.Errorf("looking up user: %w", err) | ||
} | ||
|
||
uid, err := strconv.Atoi(user.Uid) | ||
if err != nil { | ||
return userIDs{}, fmt.Errorf("converting uid string to int: %w", err) | ||
} | ||
|
||
gid, err := strconv.Atoi(user.Gid) | ||
if err != nil { | ||
return userIDs{}, fmt.Errorf("converting uid string to int: %w", err) | ||
} | ||
|
||
return userIDs{ | ||
uid: uint32(uid), | ||
gid: uint32(gid), | ||
home: user.HomeDir, | ||
}, nil | ||
} |
Oops, something went wrong.