Skip to content

Commit

Permalink
Retrieve uid from gRPC peer data
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszWojciechO committed Feb 12, 2025
1 parent d6e13c6 commit 19616a2
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 156 deletions.
2 changes: 0 additions & 2 deletions cli/cli_set_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cli
import (
"context"
"fmt"
"os"

"github.com/NordSecurity/nordvpn-linux/daemon/pb"
filesharepb "github.com/NordSecurity/nordvpn-linux/fileshare/pb"
Expand All @@ -28,7 +27,6 @@ func (c *cmd) SetNotify(ctx *cli.Context) error {
}

daemonResp, err := c.client.SetNotify(context.Background(), &pb.SetNotifyRequest{
Uid: int64(os.Getuid()),
Notify: flag,
})
if err != nil {
Expand Down
215 changes: 103 additions & 112 deletions daemon/pb/set.pb.go

Large diffs are not rendered by default.

25 changes: 21 additions & 4 deletions daemon/rpc_set_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,27 @@ import (
"github.com/NordSecurity/nordvpn-linux/config"
"github.com/NordSecurity/nordvpn-linux/daemon/pb"
"github.com/NordSecurity/nordvpn-linux/internal"
"google.golang.org/grpc/peer"
)

func (r *RPC) SetNotify(ctx context.Context, in *pb.SetNotifyRequest) (*pb.Payload, error) {
isInNordVPNGroup, err := internal.IsInAllowedGroup(uint32(in.Uid))
peer, ok := peer.FromContext(ctx)
if !ok {
log.Println(internal.ErrorPrefix, "failed to retrieve gRPC peer information from the context")
return &pb.Payload{
Type: internal.CodeInternalError,
}, nil
}

cred, ok := peer.AuthInfo.(internal.UcredAuth)
if !ok {
log.Println(internal.ErrorPrefix, "failed to extract ucred out of gRPC peer info")
return &pb.Payload{
Type: internal.CodeInternalError,
}, nil
}

isInNordVPNGroup, err := internal.IsInAllowedGroup(cred.Uid)
if err != nil {
return &pb.Payload{
Type: internal.CodeInternalError,
Expand All @@ -28,7 +45,7 @@ func (r *RPC) SetNotify(ctx context.Context, in *pb.SetNotifyRequest) (*pb.Paylo
log.Println(internal.ErrorPrefix, err)
}

notifyStatus := !cfg.UsersData.NotifyOff[in.GetUid()]
notifyStatus := !cfg.UsersData.NotifyOff[int64(cred.Uid)]

if in.GetNotify() == notifyStatus {
getBool := func(label bool) string {
Expand All @@ -45,7 +62,7 @@ func (r *RPC) SetNotify(ctx context.Context, in *pb.SetNotifyRequest) (*pb.Paylo

if !in.GetNotify() {
if err := r.cm.SaveWith(func(c config.Config) config.Config {
c.UsersData.NotifyOff[in.GetUid()] = true
c.UsersData.NotifyOff[int64(cred.Uid)] = true
return c
}); err != nil {
log.Println(internal.ErrorPrefix, err)
Expand All @@ -55,7 +72,7 @@ func (r *RPC) SetNotify(ctx context.Context, in *pb.SetNotifyRequest) (*pb.Paylo
}
} else {
if err := r.cm.SaveWith(func(c config.Config) config.Config {
delete(c.UsersData.NotifyOff, in.GetUid())
delete(c.UsersData.NotifyOff, int64(cred.Uid))
return c
}); err != nil {
log.Println(internal.ErrorPrefix, err)
Expand Down
1 change: 0 additions & 1 deletion protobuf/daemon/set.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ message SetKillSwitchRequest {
}

message SetNotifyRequest {
int64 uid = 2;
bool notify = 3;
}

Expand Down
64 changes: 32 additions & 32 deletions test/qa/lib/protobuf/daemon/set_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions test/qa/lib/protobuf/daemon/set_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,10 @@ class SetKillSwitchRequest(_message.Message):
def __init__(self, kill_switch: bool = ...) -> None: ...

class SetNotifyRequest(_message.Message):
__slots__ = ("uid", "notify")
UID_FIELD_NUMBER: _ClassVar[int]
__slots__ = ("notify",)
NOTIFY_FIELD_NUMBER: _ClassVar[int]
uid: int
notify: bool
def __init__(self, uid: _Optional[int] = ..., notify: bool = ...) -> None: ...
def __init__(self, notify: bool = ...) -> None: ...

class SetTrayRequest(_message.Message):
__slots__ = ("uid", "tray")
Expand Down
1 change: 0 additions & 1 deletion tray/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ func (ti *Instance) setNotify(flag bool) bool {
flagText = "on"
}
resp, err := ti.client.SetNotify(context.Background(), &pb.SetNotifyRequest{
Uid: int64(os.Getuid()),
Notify: flag,
})
if err != nil {
Expand Down

0 comments on commit 19616a2

Please sign in to comment.