Now it's possible to load tasks from disk.
There's a central file which contains the defined tasks:
/etc/opensnitchd/tasks/tasks.json
with this format:
{
"tasks": [
{
"enabled": false,
"name": "downloader",
"configfile":
"/etc/opensnitchd/tasks/downloader/downloader.json",
}
]
}
The field "name" defines the type of task to load, defined in each task.
It's static.
Whenever the tasks.json is modified, if the tasks are enabled, the task
configuration file defined in the "configfile" field will be loaded.
Configuration format of each task:
{
"name": "downloader",
"parent": "downloader",
"data": {}
}
The field "name" is dynamic, unique, and identifies the instance of the
task. If there're two tasks configured with the same name, only one will
be loaded.
The "parent" field is used to know what's the base task. If it's not
specified, we'll try to use the "name" as the base task.
This allows you to run multiple instances of the same task with
different configurations: a downloader to update malware blocklists,
another to update IP blocklists, etc.
The "data" field depends of each task. It's generic, and each task is
responsible for defining it. For example for the Downloader task:
"data": {
"interval": "1h",
"timeout": "5s",
"urls": [
{
"name": "adaway",
"enabled": true,
"remote": "https://adaway.o/hosts.txt",
"localfile": "/tmp/blocklist/ads-adaway-hosts.txt"
}
]
}
The tasks configuration file are also monitored for changes, and
whenever a change is detected the task is reloaded.
----
Notifications (WIP)
It's not yet fully defined and it'll change, but this is the state as
of today.
For real-time tasks, like pid-monitor, sockets monitor and node monitor,
the GUI initiates the task with a unique ID (a timestamp), to know who
is sending what, and display the data properly on the GUI.
For permanent tasks we use a unique ID defined for each task.
Right now, if we don't have the notifications channel opened and the
notification ID corresponds with one of the tasks, we'll post an alert
to the GUI, to display a desktop notification.
Each task can define if send notifications or not. For example:
{
"name": "downloader",
"data": {
(...)
"notify": {
"enabled": true
}
}
}
then in each task, act accordingly to what is defined in the
configuration.
114 lines
2.6 KiB
Go
114 lines
2.6 KiB
Go
package config
|
|
|
|
// Copyright 2025 The OpenSnitch Authors. All rights reserved.
|
|
// Use of this source code is governed by the GPLv3
|
|
// license that can be found in the LICENSE file.
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"sync"
|
|
|
|
"github.com/evilsocket/opensnitch/daemon/log"
|
|
"github.com/fsnotify/fsnotify"
|
|
)
|
|
|
|
var (
|
|
DefaultCfgFile = "/etc/opensnitchd/tasks/tasks.json"
|
|
)
|
|
|
|
// TaskData represents the configuration of a task.
|
|
// For example:
|
|
// {
|
|
// "name": "sockets-monitor",
|
|
// "data": {"protocol": 1, "state": "all", array: [...], ...}
|
|
// }
|
|
// The data field must be a JSON object.
|
|
// Each task can unmarshal the object to its own JSON object.
|
|
type TaskData struct {
|
|
// Parent holds the name of the parent task
|
|
Parent string
|
|
|
|
// Name holds the name of this particular task.
|
|
// It must be unique if you want to run multiple instances of the same task.
|
|
Name string
|
|
Data map[string]interface{}
|
|
}
|
|
|
|
// TaskConfig holds the information of each task. The name, the configuration
|
|
// file and if its enabled or not.
|
|
// The name of the task must be the one defined in each task: task.Name
|
|
type TaskConfig struct {
|
|
Name string
|
|
ConfigFile string
|
|
Enabled bool
|
|
}
|
|
|
|
// TaskList holds the list of existing tasks.
|
|
//
|
|
// {
|
|
// "list": [
|
|
// {
|
|
// "name": "node-monitor",
|
|
// "enabled": true,
|
|
// "file": "/etc/opensnitchd/tasks/node-monitor/node-monitor.json",
|
|
// },
|
|
// ...
|
|
// ]
|
|
// }
|
|
//
|
|
type TasksList struct {
|
|
Tasks []TaskConfig
|
|
}
|
|
|
|
type Loader struct {
|
|
watcher *fsnotify.Watcher
|
|
Tasks []TaskConfig
|
|
CfgFile string
|
|
stopLiveReload chan struct{}
|
|
TaskChanged chan string
|
|
liveReloadRunning bool
|
|
|
|
sync.RWMutex
|
|
}
|
|
|
|
// NewTasksLoader returns a new configuration loader object.
|
|
// It'll monitor the configuration files for changes.
|
|
func NewTasksLoader() (*Loader, error) {
|
|
watcher, err := fsnotify.NewWatcher()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &Loader{
|
|
liveReloadRunning: false,
|
|
watcher: watcher,
|
|
stopLiveReload: make(chan struct{}),
|
|
TaskChanged: make(chan string),
|
|
}, nil
|
|
}
|
|
|
|
func (l *Loader) Load(path string) ([]TaskConfig, error) {
|
|
if path == "" {
|
|
path = DefaultCfgFile
|
|
}
|
|
log.Debug("[tasks] Loader.Load() config file: %s", path)
|
|
|
|
raw, err := ioutil.ReadFile(path)
|
|
if err != nil || len(raw) == 0 {
|
|
return nil, fmt.Errorf("error reading tasks list file %s: %s", path, err)
|
|
}
|
|
var tasks TasksList
|
|
err = json.Unmarshal(raw, &tasks)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error unmarshalling config file %s: %s", path, err)
|
|
}
|
|
l.Tasks = tasks.Tasks
|
|
l.CfgFile = path
|
|
|
|
if !l.isLiveReloadRunning() {
|
|
go l.liveReloadWorker()
|
|
}
|
|
|
|
return l.Tasks, nil
|
|
}
|