opensnitch/daemon/tasks/downloader/utils.go
Gustavo Iñiguez Goia 149e0f61b8
tasks: added downloader task
Added a new task to schedule files downloads.

Whenever the task is loaded, the files will be downloaded. From that on,
files will be downloaded according to the duration specified in the
"interval" field.
The format of the interval field is time.Duration:
https://pkg.go.dev/time#Duration

configuration example:

/etc/opensnitchd/tasks/tasks.json:
{
    "tasks":[
        {
            "enabled": false,
            "name": "downloader",
            "configfile": "/etc/opensnitchd/tasks/downloader/downloader.json"
        }
    ]
}

/etc/opensnitchd/downloader/downloader.json:
{
  "name": "downloader",
  "parent": "downloader",
  "description": "",
  "data": {
      "interval": "6h",
      "timeout": "5s",
      "urls": [
          {
              "name": "adaway",
              "enabled": true,
              "remote": "https://adaway.org/hosts.txt",
              "localfile": "/tmp/bl3/ads-adaway-hosts.txt"
          }
      ],
      "notify": {
          "enabled": true
      }
}
2025-09-12 00:24:59 +02:00

61 lines
1.7 KiB
Go

// 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.
package downloader
import (
"os"
"path/filepath"
"time"
"github.com/evilsocket/opensnitch/daemon/core"
"github.com/evilsocket/opensnitch/daemon/log"
)
func (pm *Downloader) parseInterval() (time.Duration, error) {
if pm.Config.Interval == "" {
pm.Config.Interval = DefaultInterval
}
return time.ParseDuration(pm.Config.Interval)
}
func (pm *Downloader) parseTimeout() (time.Duration, error) {
if pm.Config.Timeout == "" {
pm.Config.Timeout = DefaultTimeout
}
return time.ParseDuration(pm.Config.Timeout)
}
// the urls have this format:
// {
// "name": "name-of-the-url-list",
// "enabled": true,
// "remote": "https://adaway.org/hosts.txt",
// "localfile": "/tmp/blacklist/ads-adaway-hosts.txt"
// }
//
// XXX: we may need to have a interval option per list.
// The lists are not updated on the same date, and they may fail or stop working.
// so a Interval field per list could be used to update them at different intervals,
// or disable them (interval == 0) if they fail after n errors.
func (pm *Downloader) loadUrls() error {
for _, url := range pm.Config.Urls {
localdir := filepath.Dir(url.LocalFile)
if !core.Exists(localdir) {
err := os.MkdirAll(localdir, 0700)
if err != nil {
log.Warning("[Downloader] url %s localdir create error %s: %s", url.Name, localdir, err)
continue
}
log.Debug("[Downloader] localdir created %s", localdir)
} else {
log.Debug("[Downloader] localdir exists %s", localdir)
}
pm.Urls[url.Remote] = url.LocalFile
}
return nil
}