Merge pull request #1167 from safing/fix/android-update-system

Fix android update system
This commit is contained in:
Daniel Hovie
2023-04-13 15:47:02 +02:00
committed by GitHub
5 changed files with 54 additions and 32 deletions

View File

@@ -2,6 +2,7 @@ package customlists
import ( import (
"context" "context"
"fmt"
"net" "net"
"os" "os"
"regexp" "regexp"
@@ -49,36 +50,16 @@ func prep() error {
return err return err
} }
return nil
}
func start() error {
// Register to hook to update after config change.
if err := module.RegisterEventHook(
configModuleName,
configChangeEvent,
"update custom filter list",
func(ctx context.Context, obj interface{}) error {
checkAndUpdateFilterList()
return nil
},
); err != nil {
return err
}
// Create parser task and enqueue for execution. "checkAndUpdateFilterList" will schedule the next execution.
parserTask = module.NewTask("intel/customlists:file-update-check", func(context.Context, *modules.Task) error {
checkAndUpdateFilterList()
return nil
}).Schedule(time.Now().Add(20 * time.Second))
// Register api endpoint for updating the filter list. // Register api endpoint for updating the filter list.
if err := api.RegisterEndpoint(api.Endpoint{ if err := api.RegisterEndpoint(api.Endpoint{
Path: "customlists/update", Path: "customlists/update",
Write: api.PermitUser, Write: api.PermitUser,
BelongsTo: module, BelongsTo: module,
ActionFunc: func(ar *api.Request) (msg string, err error) { ActionFunc: func(ar *api.Request) (msg string, err error) {
checkAndUpdateFilterList() errCheck := checkAndUpdateFilterList()
if errCheck != nil {
return "", errCheck
}
return "Custom filter list loaded successfully.", nil return "Custom filter list loaded successfully.", nil
}, },
Name: "Update custom filter list", Name: "Update custom filter list",
@@ -90,14 +71,36 @@ func start() error {
return nil return nil
} }
func checkAndUpdateFilterList() { func start() error {
// Register to hook to update after config change.
if err := module.RegisterEventHook(
configModuleName,
configChangeEvent,
"update custom filter list",
func(ctx context.Context, obj interface{}) error {
return checkAndUpdateFilterList()
},
); err != nil {
return err
}
// Create parser task and enqueue for execution. "checkAndUpdateFilterList" will schedule the next execution.
parserTask = module.NewTask("intel/customlists:file-update-check", func(context.Context, *modules.Task) error {
_ = checkAndUpdateFilterList()
return nil
}).Schedule(time.Now().Add(20 * time.Second))
return nil
}
func checkAndUpdateFilterList() error {
filterListLock.Lock() filterListLock.Lock()
defer filterListLock.Unlock() defer filterListLock.Unlock()
// Get path and ignore if empty // Get path and return error if empty
filePath := getFilePath() filePath := getFilePath()
if filePath == "" { if filePath == "" {
return return fmt.Errorf("custom filter list setting is empty")
} }
// Schedule next update check // Schedule next update check
@@ -113,11 +116,12 @@ func checkAndUpdateFilterList() {
if filterListFilePath != filePath || !filterListFileModifiedTime.Equal(modifiedTime) { if filterListFilePath != filePath || !filterListFileModifiedTime.Equal(modifiedTime) {
err := parseFile(filePath) err := parseFile(filePath)
if err != nil { if err != nil {
return return err
} }
filterListFileModifiedTime = modifiedTime filterListFileModifiedTime = modifiedTime
filterListFilePath = filePath filterListFilePath = filePath
} }
return nil
} }
// LookupIP checks if the IP address is in a custom filter list. // LookupIP checks if the IP address is in a custom filter list.

View File

@@ -10,9 +10,13 @@ func osGetInterfaceAddrs() ([]net.Addr, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
var netList []net.Addr var netList []net.Addr
for _, addr := range list { for _, addr := range list {
netList = append(netList, addr.ToIPNet()) ipNetAddr, err := addr.ToIPNet()
if err == nil {
netList = append(netList, ipNetAddr)
}
} }
return netList, nil return netList, nil

View File

@@ -59,3 +59,14 @@ func GetVersion(identifier string) (*updater.ResourceVersion, error) {
return rv, nil return rv, nil
} }
// GetVersionWithFullID returns the selected generic version of the given full identifier.
// The returned resource version may not be modified.
func GetVersionWithFullID(identifier string) (*updater.ResourceVersion, error) {
rv, err := registry.GetVersion(identifier)
if err != nil {
return nil, err
}
return rv, nil
}

View File

@@ -58,6 +58,9 @@ var (
// more context to requests made by the registry when // more context to requests made by the registry when
// fetching resources from the update server. // fetching resources from the update server.
UserAgent = "Core" UserAgent = "Core"
// Explicitly disables automatic software updates. Used in android.
DisableSoftwareAutoUpdate = false
) )
const ( const (
@@ -145,7 +148,7 @@ func start() error {
registry, registry,
initialReleaseChannel, initialReleaseChannel,
true, true,
enableSoftwareUpdates(), enableSoftwareUpdates() && !DisableSoftwareAutoUpdate,
enableIntelUpdates(), enableIntelUpdates(),
) )
if warning != nil { if warning != nil {
@@ -247,7 +250,7 @@ func checkForUpdates(ctx context.Context) (err error) {
} }
defer func() { defer func() {
// Resolve any error and and send succes notification. // Resolve any error and and send success notification.
if err == nil { if err == nil {
log.Infof("updates: successfully checked for updates") log.Infof("updates: successfully checked for updates")
notifyUpdateSuccess(forcedUpdate) notifyUpdateSuccess(forcedUpdate)

View File

@@ -115,7 +115,7 @@ func notifyUpdateCheckFailed(forced bool, err error) {
// Not failed often enough for notification. // Not failed often enough for notification.
return return
case lastSuccess == nil: case lastSuccess == nil:
// No recorded successful udpate. // No recorded successful update.
case time.Now().Add(-failedUpdateNotifyDurationThreshold).Before(*lastSuccess): case time.Now().Add(-failedUpdateNotifyDurationThreshold).Before(*lastSuccess):
// Failed too recently for notification. // Failed too recently for notification.
return return