Skip to content

Commit

Permalink
Enable fileshare notifications without meshnet restart
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszWojciechO committed Apr 24, 2023
1 parent 9319a81 commit 5b95aea
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 71 deletions.
44 changes: 40 additions & 4 deletions cli/cli_set_notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"

"github.com/NordSecurity/nordvpn-linux/daemon/pb"
filesharepb "github.com/NordSecurity/nordvpn-linux/fileshare/pb"
"github.com/NordSecurity/nordvpn-linux/internal"
"github.com/NordSecurity/nordvpn-linux/nstrings"

Expand All @@ -26,21 +27,56 @@ func (c *cmd) SetNotify(ctx *cli.Context) error {
return formatError(argsParseError(ctx))
}

resp, err := c.client.SetNotify(context.Background(), &pb.SetNotifyRequest{
daemonResp, err := c.client.SetNotify(context.Background(), &pb.SetNotifyRequest{
Uid: int64(os.Getuid()),
Notify: flag,
})
if err != nil {
return formatError(err)
}

switch resp.Type {
printMessage := func() {}
defer func() {
printMessage()
}()

messageNothingToSet := func() {
color.Yellow(fmt.Sprintf(SetNotifyNothingToSet, nstrings.GetBoolLabel(flag)))
}
messageSuccess := func() {
color.Green(fmt.Sprintf(SetNotifySuccess, nstrings.GetBoolLabel(flag)))
}

switch daemonResp.Type {
case internal.CodeConfigError:
return formatError(ErrConfig)
case internal.CodeNothingToDo:
color.Yellow(fmt.Sprintf(SetNotifyNothingToSet, nstrings.GetBoolLabel(flag)))
printMessage = messageNothingToSet
case internal.CodeSuccess:
color.Green(fmt.Sprintf(SetNotifySuccess, nstrings.GetBoolLabel(flag)))
printMessage = messageSuccess
}

if c.IsFileshareDaemonReachable(ctx) != nil {
return nil
}

fileshareDaemonResp, err := c.fileshareClient.SetNotifications(context.Background(),
&filesharepb.SetNotificationsRequest{Enable: flag})

if err != nil {
return formatError(err)
}

// We configure notifications for main and fileshare daemon
// if both notifications were configured successfully, report success to the user
// if main daemon was already configured but fileshare daemon was not yet configured and vice versa, report success
// if both daemons were already configured, report already configured
switch fileshareDaemonResp.Status {
case filesharepb.SetNotificationsStatus_NOTHING_TO_DO:
return nil
case filesharepb.SetNotificationsStatus_SET_SUCCESS:
printMessage = messageSuccess
}

return nil
}
30 changes: 30 additions & 0 deletions fileshare/event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,36 @@ func NewEventManager(storage Storage, meshClient meshpb.MeshnetClient, notificat
}
}

func (em *EventManager) AreNotificationsEnabled() bool {
return em.notificationManager != nil
}

func (em *EventManager) EnableNotifications() error {
em.mutex.Lock()
defer em.mutex.Unlock()

if em.notificationManager != nil {
return nil
}

notificationManager, err := NewNotificationManager()
if err != nil {
return err
}
em.notificationManager = notificationManager

return nil
}

func (em *EventManager) DisableNotifications() {
em.mutex.Lock()
defer em.mutex.Unlock()
if err := em.notificationManager.notifier.Close(); err != nil {
log.Println(err)
}
em.notificationManager = nil
}

func (em *EventManager) isFileshareFromPeerAllowed(peerIP string) bool {
resp, err := em.meshClient.GetPeers(context.Background(), &meshpb.Empty{})
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions fileshare/event_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (mn *MockNotifier) SendNotification(summary string, body string, actions []
return notificationID, nil
}

func (mn *MockNotifier) Close() error {
return nil
}

type MockFileOpener struct {
openedFiles []string
}
Expand Down
11 changes: 11 additions & 0 deletions fileshare/notification_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Action struct {
// Notifier is responsible for sending notifications to the user
type Notifier interface {
SendNotification(summary string, body string, actions []Action) (uint32, error)
Close() error
}

// DbusNotifier wraps github.com/esiqveland/notify notifier implementation
Expand Down Expand Up @@ -46,6 +47,10 @@ func (n DbusNotifier) SendNotification(summary string, body string, actions []Ac
return n.notifier.SendNotification(notification)
}

func (n DbusNotifier) Close() error {
return n.notifier.Close()
}

func newDbusNotifier(notificationManager *NotificationManager) (*DbusNotifier, error) {
dbusConn, err := dbus.SessionBusPrivate()
defer func() {
Expand Down Expand Up @@ -137,6 +142,12 @@ func NewNotificationManager() (*NotificationManager, error) {
return &notificationManager, nil
}

func (nm *NotificationManager) Disable() {
if err := nm.notifier.Close(); err != nil {
log.Println("Failed to close notifier: ", err)
}
}

func (nm *NotificationManager) openFile(actionID uint32) {
nm.downloadedFilesMutex.Lock()
defer nm.downloadedFilesMutex.Unlock()
Expand Down
Loading

0 comments on commit 5b95aea

Please sign in to comment.