From c1c7138e37de910589280fa89ee1fa8d2fdf0212 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20I=C3=B1iguez=20Goia?= Date: Thu, 10 Oct 2024 00:24:29 +0200 Subject: [PATCH] refactored tasks notifications moved to their own file. --- daemon/ui/notifications.go | 91 --------------------------- daemon/ui/notifications_tasks.go | 103 +++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 91 deletions(-) create mode 100644 daemon/ui/notifications_tasks.go diff --git a/daemon/ui/notifications.go b/daemon/ui/notifications.go index 13af0f8a..64d95350 100644 --- a/daemon/ui/notifications.go +++ b/daemon/ui/notifications.go @@ -60,97 +60,6 @@ func (c *Client) getClientConfig() *protocol.ClientConfig { } } -func (c *Client) monitorSockets(config interface{}, stream protocol.UI_NotificationsClient, notification *protocol.Notification) { - sockMonTask, err := socketsmonitor.New(config, true) - if err != nil { - c.sendNotificationReply(stream, notification.Id, "", err) - return - } - ctxSock, err := TaskMgr.AddTask(socketsmonitor.Name, sockMonTask) - if err != nil { - c.sendNotificationReply(stream, notification.Id, "", err) - return - } - go func(ctx context.Context) { - for { - select { - case <-ctx.Done(): - goto Exit - case err := <-sockMonTask.Errors(): - c.sendNotificationReply(stream, notification.Id, "", err) - case temp := <-sockMonTask.Results(): - data, ok := temp.(string) - if !ok { - goto Exit - } - c.sendNotificationReply(stream, notification.Id, data, nil) - } - } - Exit: - // task should have already been removed via TASK_STOP - }(ctxSock) -} - -func (c *Client) monitorNode(node, interval string, stream protocol.UI_NotificationsClient, notification *protocol.Notification) { - taskName, nodeMonTask := nodemonitor.New(node, interval, true) - ctxNode, err := TaskMgr.AddTask(taskName, nodeMonTask) - if err != nil { - c.sendNotificationReply(stream, notification.Id, "", err) - return - } - go func(ctx context.Context) { - for { - select { - case <-ctx.Done(): - goto Exit - case err := <-nodeMonTask.Errors(): - c.sendNotificationReply(stream, notification.Id, "", err) - case temp := <-nodeMonTask.Results(): - data, ok := temp.(string) - if !ok { - goto Exit - } - c.sendNotificationReply(stream, notification.Id, data, nil) - } - } - Exit: - TaskMgr.RemoveTask(taskName) - }(ctxNode) -} - -func (c *Client) monitorProcessDetails(pid int, interval string, stream protocol.UI_NotificationsClient, notification *protocol.Notification) { - if !core.Exists(fmt.Sprint("/proc/", pid)) { - c.sendNotificationReply(stream, notification.Id, "", fmt.Errorf("The process is no longer running")) - return - } - - taskName, pidMonTask := pidmonitor.New(pid, interval, true) - ctx, err := TaskMgr.AddTask(taskName, pidMonTask) - if err != nil { - c.sendNotificationReply(stream, notification.Id, "", err) - return - } - go func(ctx context.Context) { - for { - select { - case <-ctx.Done(): - goto Exit - case err := <-pidMonTask.Errors(): - c.sendNotificationReply(stream, notification.Id, "", err) - case temp := <-pidMonTask.Results(): - data, ok := temp.(string) - if !ok { - goto Exit - } - c.sendNotificationReply(stream, notification.Id, data, nil) - } - } - Exit: - TaskMgr.RemoveTask(taskName) - }(ctx) - -} - func (c *Client) handleActionChangeConfig(stream protocol.UI_NotificationsClient, notification *protocol.Notification) { log.Info("[notification] Reloading configuration") // Parse received configuration first, to get the new proc monitor method. diff --git a/daemon/ui/notifications_tasks.go b/daemon/ui/notifications_tasks.go new file mode 100644 index 00000000..e5b3504c --- /dev/null +++ b/daemon/ui/notifications_tasks.go @@ -0,0 +1,103 @@ +package ui + +import ( + "fmt" + + "github.com/evilsocket/opensnitch/daemon/core" + "github.com/evilsocket/opensnitch/daemon/tasks/nodemonitor" + "github.com/evilsocket/opensnitch/daemon/tasks/pidmonitor" + "github.com/evilsocket/opensnitch/daemon/tasks/socketsmonitor" + "github.com/evilsocket/opensnitch/daemon/ui/protocol" + "golang.org/x/net/context" +) + +func (c *Client) monitorSockets(config interface{}, stream protocol.UI_NotificationsClient, notification *protocol.Notification) { + sockMonTask, err := socketsmonitor.New(config, true) + if err != nil { + c.sendNotificationReply(stream, notification.Id, "", err) + return + } + ctxSock, err := TaskMgr.AddTask(socketsmonitor.Name, sockMonTask) + if err != nil { + c.sendNotificationReply(stream, notification.Id, "", err) + return + } + go func(ctx context.Context) { + for { + select { + case <-ctx.Done(): + goto Exit + case err := <-sockMonTask.Errors(): + c.sendNotificationReply(stream, notification.Id, "", err) + case temp := <-sockMonTask.Results(): + data, ok := temp.(string) + if !ok { + goto Exit + } + c.sendNotificationReply(stream, notification.Id, data, nil) + } + } + Exit: + // task should have already been removed via TASK_STOP + }(ctxSock) +} + +func (c *Client) monitorNode(node, interval string, stream protocol.UI_NotificationsClient, notification *protocol.Notification) { + taskName, nodeMonTask := nodemonitor.New(node, interval, true) + ctxNode, err := TaskMgr.AddTask(taskName, nodeMonTask) + if err != nil { + c.sendNotificationReply(stream, notification.Id, "", err) + return + } + go func(ctx context.Context) { + for { + select { + case <-ctx.Done(): + goto Exit + case err := <-nodeMonTask.Errors(): + c.sendNotificationReply(stream, notification.Id, "", err) + case temp := <-nodeMonTask.Results(): + data, ok := temp.(string) + if !ok { + goto Exit + } + c.sendNotificationReply(stream, notification.Id, data, nil) + } + } + Exit: + TaskMgr.RemoveTask(taskName) + }(ctxNode) +} + +func (c *Client) monitorProcessDetails(pid int, interval string, stream protocol.UI_NotificationsClient, notification *protocol.Notification) { + if !core.Exists(fmt.Sprint("/proc/", pid)) { + c.sendNotificationReply(stream, notification.Id, "", fmt.Errorf("The process is no longer running")) + return + } + + taskName, pidMonTask := pidmonitor.New(pid, interval, true) + ctx, err := TaskMgr.AddTask(taskName, pidMonTask) + if err != nil { + c.sendNotificationReply(stream, notification.Id, "", err) + return + } + go func(ctx context.Context) { + for { + select { + case <-ctx.Done(): + goto Exit + case err := <-pidMonTask.Errors(): + c.sendNotificationReply(stream, notification.Id, "", err) + case temp := <-pidMonTask.Results(): + data, ok := temp.(string) + if !ok { + goto Exit + } + c.sendNotificationReply(stream, notification.Id, data, nil) + } + } + Exit: + TaskMgr.RemoveTask(taskName) + }(ctx) + +}