Skip to content

Commit

Permalink
[WIP] working download and replace.
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Aug 30, 2024
1 parent abca9f0 commit eed4b09
Show file tree
Hide file tree
Showing 18 changed files with 168 additions and 276 deletions.
2 changes: 1 addition & 1 deletion service/broadcasts/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,5 @@ func New(instance instance) (*Broadcasts, error) {
}

type instance interface {
Updates() *updates.Updates
IntelUpdates() *updates.Updates
}
2 changes: 1 addition & 1 deletion service/broadcasts/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type BroadcastNotification struct {

func broadcastNotify(ctx *mgr.WorkerCtx) error {
// Get broadcast notifications file, load it from disk and parse it.
broadcastsResource, err := module.instance.Updates().GetFile(broadcastsResourcePath)
broadcastsResource, err := module.instance.IntelUpdates().GetFile(broadcastsResourcePath)
if err != nil {
return fmt.Errorf("failed to get broadcast notifications update: %w", err)
}
Expand Down
77 changes: 53 additions & 24 deletions service/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/safing/portmaster/service/sync"
"github.com/safing/portmaster/service/ui"
"github.com/safing/portmaster/service/updates"
"github.com/safing/portmaster/service/updates/registry"
"github.com/safing/portmaster/spn/access"
"github.com/safing/portmaster/spn/cabin"
"github.com/safing/portmaster/spn/captain"
Expand All @@ -46,6 +47,23 @@ import (
"github.com/safing/portmaster/spn/terminal"
)

var binaryUpdateIndex = registry.UpdateIndex{
Directory: "/usr/lib/portmaster",
DownloadDirectory: "/var/lib/portmaster/new_bin",
Ignore: []string{"databases", "intel", "config.json"},
IndexURLs: []string{"http://localhost:8000/test-binary.json"},
IndexFile: "bin-index.json",
AutoApply: false,
}

var intelUpdateIndex = registry.UpdateIndex{
Directory: "/var/lib/portmaster/intel",
DownloadDirectory: "/var/lib/portmaster/new_intel",
IndexURLs: []string{"http://localhost:8000/test-intel.json"},
IndexFile: "intel-index.json",
AutoApply: true,
}

// Instance is an instance of a Portmaster service.
type Instance struct {
ctx context.Context
Expand All @@ -63,25 +81,26 @@ type Instance struct {
rng *rng.Rng
base *base.Base

core *core.Core
updates *updates.Updates
geoip *geoip.GeoIP
netenv *netenv.NetEnv
ui *ui.UI
profile *profile.ProfileModule
network *network.Network
netquery *netquery.NetQuery
firewall *firewall.Firewall
filterLists *filterlists.FilterLists
interception *interception.Interception
customlist *customlists.CustomList
status *status.Status
broadcasts *broadcasts.Broadcasts
compat *compat.Compat
nameserver *nameserver.NameServer
process *process.ProcessModule
resolver *resolver.ResolverModule
sync *sync.Sync
core *core.Core
binaryUpdates *updates.Updates
intelUpdates *updates.Updates
geoip *geoip.GeoIP
netenv *netenv.NetEnv
ui *ui.UI
profile *profile.ProfileModule
network *network.Network
netquery *netquery.NetQuery
firewall *firewall.Firewall
filterLists *filterlists.FilterLists
interception *interception.Interception
customlist *customlists.CustomList
status *status.Status
broadcasts *broadcasts.Broadcasts
compat *compat.Compat
nameserver *nameserver.NameServer
process *process.ProcessModule
resolver *resolver.ResolverModule
sync *sync.Sync

access *access.Access

Expand Down Expand Up @@ -147,7 +166,11 @@ func New(svcCfg *ServiceConfig) (*Instance, error) { //nolint:maintidx
if err != nil {
return instance, fmt.Errorf("create core module: %w", err)
}
instance.updates, err = updates.New(instance)
instance.binaryUpdates, err = updates.New(instance, "Binary Updater", binaryUpdateIndex)
if err != nil {
return instance, fmt.Errorf("create updates module: %w", err)
}
instance.intelUpdates, err = updates.New(instance, "Intel Updater", intelUpdateIndex)
if err != nil {
return instance, fmt.Errorf("create updates module: %w", err)
}
Expand Down Expand Up @@ -274,7 +297,8 @@ func New(svcCfg *ServiceConfig) (*Instance, error) { //nolint:maintidx
instance.rng,

instance.core,
instance.updates,
instance.binaryUpdates,
instance.intelUpdates,
instance.geoip,
instance.netenv,

Expand Down Expand Up @@ -372,9 +396,14 @@ func (i *Instance) Base() *base.Base {
return i.base
}

// Updates returns the updates module.
func (i *Instance) Updates() *updates.Updates {
return i.updates
// BinaryUpdates returns the updates module.
func (i *Instance) BinaryUpdates() *updates.Updates {
return i.binaryUpdates
}

// IntelUpdates returns the updates module.
func (i *Instance) IntelUpdates() *updates.Updates {
return i.intelUpdates
}

// GeoIP returns the geoip module.
Expand Down
2 changes: 1 addition & 1 deletion service/intel/filterlists/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func updateListIndex() error {
case listIndexUpdate == nil:
// This is the first time this function is run, get updater file for index.
var err error
listIndexUpdate, err = module.instance.Updates().GetFile(listIndexFilePath)
listIndexUpdate, err = module.instance.IntelUpdates().GetFile(listIndexFilePath)
if err != nil {
return err
}
Expand Down
5 changes: 3 additions & 2 deletions service/intel/filterlists/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ func init() {
}

func prep() error {
module.instance.Updates().EventResourcesUpdated.AddCallback("Check for blocklist updates",
module.instance.IntelUpdates().EventResourcesUpdated.AddCallback("Check for blocklist updates",
func(wc *mgr.WorkerCtx, s struct{}) (bool, error) {
if ignoreUpdateEvents.IsSet() {
return false, nil
}
log.Debugf("performing filter list upadte")

return false, tryListUpdate(wc.Ctx())
})
Expand Down Expand Up @@ -141,6 +142,6 @@ func New(instance instance) (*FilterLists, error) {
}

type instance interface {
Updates() *updates.Updates
IntelUpdates() *updates.Updates
NetEnv() *netenv.NetEnv
}
2 changes: 1 addition & 1 deletion service/intel/geoip/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func getGeoIPDB(resource string) (*geoIPDB, error) {
}

func open(resource string) (*registry.File, error) {
f, err := module.instance.Updates().GetFile(resource)
f, err := module.instance.IntelUpdates().GetFile(resource)
if err != nil {
return nil, fmt.Errorf("getting file: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions service/intel/geoip/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (g *GeoIP) Manager() *mgr.Manager {
}

func (g *GeoIP) Start() error {
module.instance.Updates().EventResourcesUpdated.AddCallback(
module.instance.IntelUpdates().EventResourcesUpdated.AddCallback(
"Check for GeoIP database updates",
func(_ *mgr.WorkerCtx, _ struct{}) (bool, error) {
worker.triggerUpdate()
Expand Down Expand Up @@ -66,5 +66,5 @@ func New(instance instance) (*GeoIP, error) {
}

type instance interface {
Updates() *updates.Updates
IntelUpdates() *updates.Updates
}
2 changes: 1 addition & 1 deletion service/netenv/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,5 @@ func New(instance instance) (*NetEnv, error) {
}

type instance interface {
Updates() *updates.Updates
IntelUpdates() *updates.Updates
}
2 changes: 1 addition & 1 deletion service/netenv/online-status.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ func updateOnlineStatus(status OnlineStatus, portalURL *url.URL, comment string)

// Trigger update check when coming (semi) online.
if Online() {
module.instance.Updates().EventResourcesUpdated.Submit(struct{}{})
module.instance.IntelUpdates().EventResourcesUpdated.Submit(struct{}{})
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion service/ui/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,5 @@ func New(instance instance) (*UI, error) {

type instance interface {
API() *api.API
Updates() *updates.Updates
BinaryUpdates() *updates.Updates
}
2 changes: 1 addition & 1 deletion service/ui/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (bs *archiveServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

// get file from update system
zipFile, err := module.instance.Updates().GetFile(fmt.Sprintf("%s.zip", moduleName))
zipFile, err := module.instance.BinaryUpdates().GetFile(fmt.Sprintf("%s.zip", moduleName))
if err != nil {
if errors.Is(err, registry.ErrNotFound) {
log.Tracef("ui: requested module %s does not exist", moduleName)
Expand Down
115 changes: 34 additions & 81 deletions service/updates/module.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package updates

import (
"errors"
"flag"
"fmt"
"sync/atomic"
"time"

"github.com/safing/portmaster/base/api"
"github.com/safing/portmaster/base/config"
Expand All @@ -14,20 +12,19 @@ import (
"github.com/safing/portmaster/service/updates/registry"
)

var applyUpdates bool
var autoUpdate bool

func init() {
flag.BoolVar(&applyUpdates, "update", false, "apply downloaded updates")
flag.BoolVar(&autoUpdate, "auto-update", false, "auto apply downloaded updates")
}

// Updates provides access to released artifacts.
type Updates struct {
m *mgr.Manager
states *mgr.StateMgr

updateBinaryWorkerMgr *mgr.WorkerMgr
updateIntelWorkerMgr *mgr.WorkerMgr
restartWorkerMgr *mgr.WorkerMgr
updateCheckWorkerMgr *mgr.WorkerMgr
upgraderWorkerMgr *mgr.WorkerMgr

EventResourcesUpdated *mgr.EventMgr[struct{}]
EventVersionsUpdated *mgr.EventMgr[struct{}]
Expand All @@ -37,15 +34,9 @@ type Updates struct {
instance instance
}

var shimLoaded atomic.Bool

// New returns a new UI module.
func New(instance instance) (*Updates, error) {
if !shimLoaded.CompareAndSwap(false, true) {
return nil, errors.New("only one instance allowed")
}

m := mgr.New("Updates")
// New returns a new Updates module.
func New(instance instance, name string, index registry.UpdateIndex) (*Updates, error) {
m := mgr.New(name)
module := &Updates{
m: m,
states: m.NewStateMgr(),
Expand All @@ -57,63 +48,47 @@ func New(instance instance) (*Updates, error) {
}

// Events
module.updateBinaryWorkerMgr = m.NewWorkerMgr("binary updater", module.checkForBinaryUpdates, nil)
module.updateIntelWorkerMgr = m.NewWorkerMgr("intel updater", module.checkForIntelUpdates, nil)
module.restartWorkerMgr = m.NewWorkerMgr("automatic restart", automaticRestart, nil)

binIndex := registry.UpdateIndex{
Directory: "/usr/lib/portmaster",
DownloadDirectory: "/var/lib/portmaster/new_bin",
Ignore: []string{"databases", "intel", "config.json"},
IndexURLs: []string{"http://localhost:8000/test-binary.json"},
IndexFile: "bin-index.json",
AutoApply: false,
}
module.updateCheckWorkerMgr = m.NewWorkerMgr("update checker", module.checkForUpdates, nil)
module.updateCheckWorkerMgr.Repeat(30 * time.Second)
module.upgraderWorkerMgr = m.NewWorkerMgr("upgrader", func(w *mgr.WorkerCtx) error {
err := module.registry.ApplyUpdates()
if err != nil {
// TODO(vladimir): Send notification to UI
log.Errorf("updates: failed to apply updates: %s", err)
} else {
module.instance.Restart()
}
return nil
}, nil)

intelIndex := registry.UpdateIndex{
Directory: "/var/lib/portmaster/intel",
DownloadDirectory: "/var/lib/portmaster/new_intel",
IndexURLs: []string{"http://localhost:8000/test-intel.json"},
IndexFile: "intel-index.json",
AutoApply: true,
}
module.registry = registry.New(binIndex, intelIndex)
module.registry = registry.New(index)
_ = module.registry.Initialize()

return module, nil
}

func (u *Updates) checkForBinaryUpdates(_ *mgr.WorkerCtx) error {
hasUpdates, err := u.registry.CheckForBinaryUpdates()
func (u *Updates) checkForUpdates(_ *mgr.WorkerCtx) error {
hasUpdates, err := u.registry.CheckForUpdates()
if err != nil {
log.Errorf("updates: failed to check for binary updates: %s", err)
log.Errorf("updates: failed to check for updates: %s", err)
}
if hasUpdates {
log.Infof("updates: there is updates available in the binary bundle")
err = u.registry.DownloadBinaryUpdates()
log.Infof("updates: there is updates available")
err = u.registry.DownloadUpdates()
if err != nil {
log.Errorf("updates: failed to download bundle: %s", err)
} else if autoUpdate {
u.ApplyUpdates()
}
} else {
log.Infof("updates: no new binary updates")
log.Infof("updates: no new updates")
u.EventResourcesUpdated.Submit(struct{}{})
}
return nil
}

func (u *Updates) checkForIntelUpdates(_ *mgr.WorkerCtx) error {
hasUpdates, err := u.registry.CheckForIntelUpdates()
if err != nil {
log.Errorf("updates: failed to check for intel updates: %s", err)
}
if hasUpdates {
log.Infof("updates: there is updates available in the intel bundle")
err = u.registry.DownloadIntelUpdates()
if err != nil {
log.Errorf("updates: failed to download bundle: %s", err)
}
} else {
log.Infof("updates: no new intel data updates")
}
return nil
func (u *Updates) ApplyUpdates() {
u.upgraderWorkerMgr.Go()
}

// States returns the state manager.
Expand All @@ -128,29 +103,7 @@ func (u *Updates) Manager() *mgr.Manager {

// Start starts the module.
func (u *Updates) Start() error {
// initConfig()

if applyUpdates {
err := u.registry.ApplyBinaryUpdates()
if err != nil {
log.Errorf("updates: failed to apply binary updates: %s", err)
}
err = u.registry.ApplyIntelUpdates()
if err != nil {
log.Errorf("updates: failed to apply intel updates: %s", err)
}
u.instance.Restart()
return nil
}

err := u.registry.Initialize()
if err != nil {
// TODO(vladimir): Find a better way to handle this error. The service will stop if parsing of the bundle files fails.
return fmt.Errorf("failed to initialize registry: %w", err)
}

u.updateBinaryWorkerMgr.Go()
u.updateIntelWorkerMgr.Go()
u.updateCheckWorkerMgr.Go()
return nil
}

Expand Down
Loading

0 comments on commit eed4b09

Please sign in to comment.