Skip to content

Commit

Permalink
[WIP] updater rafactoring, minor improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
vlabo committed Sep 17, 2024
1 parent 83ec18f commit 84a15b5
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 312 deletions.
56 changes: 23 additions & 33 deletions cmds/portmaster-core/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,12 @@ type windowsService struct {
func (ws *windowsService) Execute(args []string, changeRequests <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending}

startupComplete := make(chan struct{})
go func() {
for !ws.instance.Ready() {
time.Sleep(1 * time.Second)
}
startupComplete <- struct{}{}
}()
ws.instance.Start()
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}

service:
for {
select {
case <-startupComplete:
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
case <-ws.instance.Stopped():
changes <- svc.Status{State: svc.StopPending}
break service
Expand All @@ -59,13 +51,13 @@ service:
case svc.Stop, svc.Shutdown:
ws.instance.Shutdown()
default:
log.Errorf("unexpected control request: #%d\n", c)
log.Errorf("unexpected control request: #%d", c)
}
}
}

// wait until everything else is finished
finishWg.Wait()
// finishWg.Wait()

log.Shutdown()

Expand All @@ -88,12 +80,12 @@ func run(instance *service.Instance) error {
// select service run type
svcRun := svc.Run
if !isService {
log.Warningf("running interactively, switching to debug execution (no real service).\n")
log.Warningf("running interactively, switching to debug execution (no real service).")
svcRun = debug.Run
go registerSignalHandler(instance)
}

runWg.Add(2)
runWg.Add(1)

// run service client
go func() {
Expand All @@ -105,29 +97,27 @@ func run(instance *service.Instance) error {
} else {
log.Infof("shuting down service")
}
instance.Shutdown()
runWg.Done()
}()

finishWg.Add(1)
// finishWg.Add(1)
// run service
go func() {
// run slightly delayed
time.Sleep(250 * time.Millisecond)
instance.Start()

if err != nil {
fmt.Printf("instance start failed: %s\n", err)

// Print stack on start failure, if enabled.
if printStackOnExit {
printStackTo(os.Stdout, "PRINTING STACK ON START FAILURE")
}

}
runWg.Done()
finishWg.Done()
}()
// go func() {
// // run slightly delayed
// time.Sleep(250 * time.Millisecond)

// if err != nil {
// fmt.Printf("instance start failed: %s\n", err)

// // Print stack on start failure, if enabled.
// if printStackOnExit {
// printStackTo(os.Stdout, "PRINTING STACK ON START FAILURE")
// }

// }
// runWg.Done()
// finishWg.Done()
// }()

runWg.Wait()

Expand Down
2 changes: 2 additions & 0 deletions desktop/tauri/src-tauri/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ Update WIX installer template:
2. Replace the contents of `templates/main_original.wxs` with the repository version.
3. Replace the contents of `templates/main.wsx` and add the fallowing lines at the end of the file, inside the `Product` tag.
```xml
<!-- Service fragments -->
<CustomActionRef Id='InstallPortmasterService' />
<CustomActionRef Id='StopPortmasterService' />
<CustomActionRef Id='DeletePortmasterService' />
<!-- End Service fragments -->
```
4 changes: 3 additions & 1 deletion desktop/tauri/src-tauri/tauri.conf.json5
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@
},
"wix": {
"fragmentPaths": [
"templates/service.wxs"
"templates/service.wxs",
"templates/files.wxs"
],
"componentGroupRefs": ["BinaryAndIntelFiles"],
"template": "templates/main.wxs"
}
},
Expand Down
36 changes: 36 additions & 0 deletions desktop/tauri/src-tauri/templates/files.wxs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
<Fragment>
<DirectoryRef Id="INSTALLDIR">
<Directory Id="BinaryDir" Name="binary" />
<Directory Id="IntelDir" Name="intel" />
</DirectoryRef>
</Fragment>

<Fragment>
<Component Id="BinaryFiles" Directory="BinaryDir" Guid="850cdd31-424d-45f5-b8f0-95df950ebd0d">
<File Id="BinIndexJson" Source="..\..\..\..\binaries\bin-index.json" />
<File Id="PortmasterCoreExe" Source="..\..\..\..\binaries\portmaster-core.exe" />
<File Id="PortmasterKextSys" Source="..\..\..\..\binaries\portmaster-kext.sys" />
<File Id="PortmasterZip" Source="..\..\..\..\binaries\portmaster.zip" />
<File Id="AssetsZip" Source="..\..\..\..\binaries\assets.zip" />
</Component>

<Component Id="IntelFiles" Directory="IntelDir" Guid="0bb439f1-2075-45b0-95bf-78ed3dffeb69">
<File Id="IntelIndexJson" Source="..\..\..\..\binaries\intel-index.json" />
<File Id="BaseDsdl" Source="..\..\..\..\binaries\base.dsdl" />
<File Id="Geoipv4Mmdb" Source="..\..\..\..\binaries\geoipv4.mmdb" />
<File Id="Geoipv6Mmdb" Source="..\..\..\..\binaries\geoipv6.mmdb" />
<File Id="IndexDsd" Source="..\..\..\..\binaries\index.dsd" />
<File Id="IntermediateDsdl" Source="..\..\..\..\binaries\intermediate.dsdl" />
<File Id="UrgentDsdl" Source="..\..\..\..\binaries\urgent.dsdl" />
</Component>
</Fragment>

<Fragment>
<ComponentGroup Id="BinaryAndIntelFiles">
<ComponentRef Id="BinaryFiles" />
<ComponentRef Id="IntelFiles" />
</ComponentGroup>
</Fragment>
</Wix>
10 changes: 6 additions & 4 deletions desktop/tauri/src-tauri/templates/main.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,16 @@
</InstallExecuteSequence>
{{/if}}

<CustomActionRef Id='InstallPortmasterService' />
<CustomActionRef Id='StopPortmasterService' />
<CustomActionRef Id='DeletePortmasterService' />

<InstallExecuteSequence>
<Custom Action="LaunchApplication" After="InstallFinalize">AUTOLAUNCHAPP AND NOT Installed</Custom>
</InstallExecuteSequence>

<SetProperty Id="ARPINSTALLLOCATION" Value="[INSTALLDIR]" After="CostFinalize"/>

<!-- Service fragments -->
<CustomActionRef Id='InstallPortmasterService' />
<CustomActionRef Id='StopPortmasterService' />
<CustomActionRef Id='DeletePortmasterService' />
<!-- End Service fragments -->
</Product>
</Wix>
2 changes: 1 addition & 1 deletion desktop/tauri/src-tauri/templates/service.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Fragment>
<CustomAction Id="InstallPortmasterService"
Directory="INSTALLDIR"
ExeCommand="&quot;[INSTALLDIR]portmaster-start.exe&quot; install core-service --data=&quot;[INSTALLDIR]data&quot;"
ExeCommand="sc.exe create PortmasterCore binPath= &quot;[INSTALLDIR]binary\portmaster-core.exe --data [INSTALLDIR]data&quot;"
Execute="commit"
Return="check"
Impersonate="no"
Expand Down
50 changes: 37 additions & 13 deletions service/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package service
import (
"context"
"fmt"
"os"
"path/filepath"
go_runtime "runtime"
"sync/atomic"
"time"
Expand Down Expand Up @@ -35,7 +37,6 @@ 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 Down Expand Up @@ -103,31 +104,54 @@ type Instance struct {
CommandLineOperation func() error
}

func getCurrentBinaryFolder() (string, error) {
// Get the path of the currently running executable
exePath, err := os.Executable()
if err != nil {
return "", fmt.Errorf("failed to get executable path: %w", err)
}

// Get the absolute path
absPath, err := filepath.Abs(exePath)
if err != nil {
return "", fmt.Errorf("failed to get absolute path: %w", err)
}

// Get the directory of the executable
installDir := filepath.Dir(absPath)

return installDir, nil
}

// New returns a new Portmaster service instance.
func New(svcCfg *ServiceConfig) (*Instance, error) { //nolint:maintidx
var binaryUpdateIndex registry.UpdateIndex
var intelUpdateIndex registry.UpdateIndex
var binaryUpdateIndex updates.UpdateIndex
var intelUpdateIndex updates.UpdateIndex
if go_runtime.GOOS == "windows" {
binaryUpdateIndex = registry.UpdateIndex{
Directory: "C:/Program Files/Portmaster/binary",
DownloadDirectory: "C:/Program Files/Portmaster/new_binary",
PurgeDirectory: "C:/Program Files/Portmaster/old_binary",
binaryFolder, err := getCurrentBinaryFolder()
if err != nil {
return nil, err
}
binaryUpdateIndex = updates.UpdateIndex{
Directory: binaryFolder, // Default: C:/Program Files/Portmaster/binary
DownloadDirectory: os.ExpandEnv("%ProgramData%/Portmaster/new_binary"),
PurgeDirectory: os.ExpandEnv("%ProgramData%/Portmaster/old_binary"),
Ignore: []string{"databases", "intel", "config.json"},
IndexURLs: []string{"http://192.168.88.11:8000/test-binary.json"},
IndexFile: "bin-index.json",
AutoApply: false,
}

intelUpdateIndex = registry.UpdateIndex{
Directory: "C:/Program Files/Portmaster/intel",
DownloadDirectory: "C:/Program Files/Portmaster/new_intel",
PurgeDirectory: "C:/Program Files/Portmaster/old_intel",
intelUpdateIndex = updates.UpdateIndex{
Directory: os.ExpandEnv("%ProgramData%/Portmaster/intel"),
DownloadDirectory: os.ExpandEnv("%ProgramData%/Portmaster/new_intel"),
PurgeDirectory: os.ExpandEnv("%ProgramData%/Portmaster/old_intel"),
IndexURLs: []string{"http://192.168.88.11:8000/test-intel.json"},
IndexFile: "intel-index.json",
AutoApply: true,
}
} else if go_runtime.GOOS == "linux" {
binaryUpdateIndex = registry.UpdateIndex{
binaryUpdateIndex = updates.UpdateIndex{
Directory: "/usr/lib/portmaster",
DownloadDirectory: "/var/lib/portmaster/new_bin",
PurgeDirectory: "/var/lib/portmaster/old_bin",
Expand All @@ -137,7 +161,7 @@ func New(svcCfg *ServiceConfig) (*Instance, error) { //nolint:maintidx
AutoApply: false,
}

intelUpdateIndex = registry.UpdateIndex{
intelUpdateIndex = updates.UpdateIndex{
Directory: "/var/lib/portmaster/intel",
DownloadDirectory: "/var/lib/portmaster/new_intel",
PurgeDirectory: "/var/lib/portmaster/intel_bin",
Expand Down
10 changes: 5 additions & 5 deletions service/intel/filterlists/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/safing/portmaster/base/database"
"github.com/safing/portmaster/base/database/record"
"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/service/updates/registry"
"github.com/safing/portmaster/service/updates"
)

const (
Expand All @@ -39,9 +39,9 @@ var (
filterListLock sync.RWMutex

// Updater files for tracking upgrades.
baseFile *registry.File
intermediateFile *registry.File
urgentFile *registry.File
baseFile *updates.File
intermediateFile *updates.File
urgentFile *updates.File

filterListsLoaded chan struct{}
)
Expand Down Expand Up @@ -77,7 +77,7 @@ func isLoaded() bool {

// processListFile opens the latest version of file and decodes it's DSDL
// content. It calls processEntry for each decoded filterlists entry.
func processListFile(ctx context.Context, filter *scopedBloom, file *registry.File) error {
func processListFile(ctx context.Context, filter *scopedBloom, file *updates.File) error {
f, err := os.Open(file.Path())
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions service/intel/filterlists/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/safing/portmaster/base/database"
"github.com/safing/portmaster/base/database/record"
"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/service/updates/registry"
"github.com/safing/portmaster/service/updates"
"github.com/safing/structures/dsd"
)

Expand Down Expand Up @@ -162,7 +162,7 @@ func getListIndexFromCache() (*ListIndexFile, error) {

var (
// listIndexUpdate must only be used by updateListIndex.
listIndexUpdate *registry.File
listIndexUpdate *updates.File
listIndexUpdateLock sync.Mutex
)

Expand Down
10 changes: 5 additions & 5 deletions service/intel/filterlists/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/safing/portmaster/base/database/query"
"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/service/updates/registry"
"github.com/safing/portmaster/service/updates"
)

var updateInProgress = abool.New()
Expand Down Expand Up @@ -174,8 +174,8 @@ func removeAllObsoleteFilterEntries(wc *mgr.WorkerCtx) error {
// getUpgradableFiles returns a slice of filterlists files
// that should be updated. The files MUST be updated and
// processed in the returned order!
func getUpgradableFiles() ([]*registry.File, error) {
var updateOrder []*registry.File
func getUpgradableFiles() ([]*updates.File, error) {
var updateOrder []*updates.File

// cacheDBInUse := isLoaded()

Expand Down Expand Up @@ -218,7 +218,7 @@ func getUpgradableFiles() ([]*registry.File, error) {
return resolveUpdateOrder(updateOrder)
}

func resolveUpdateOrder(updateOrder []*registry.File) ([]*registry.File, error) {
func resolveUpdateOrder(updateOrder []*updates.File) ([]*updates.File, error) {
// sort the update order by ascending version
sort.Sort(byAscVersion(updateOrder))
log.Tracef("intel/filterlists: order of updates: %v", updateOrder)
Expand Down Expand Up @@ -258,7 +258,7 @@ func resolveUpdateOrder(updateOrder []*registry.File) ([]*registry.File, error)
return updateOrder[startAtIdx:], nil
}

type byAscVersion []*registry.File
type byAscVersion []*updates.File

func (fs byAscVersion) Len() int { return len(fs) }

Expand Down
6 changes: 3 additions & 3 deletions service/intel/geoip/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/safing/portmaster/base/log"
"github.com/safing/portmaster/service/mgr"
"github.com/safing/portmaster/service/updates/registry"
"github.com/safing/portmaster/service/updates"
)

var worker *updateWorker
Expand All @@ -27,7 +27,7 @@ const (

type geoIPDB struct {
*maxminddb.Reader
file *registry.File
file *updates.File
}

// updateBroadcaster stores a geoIPDB and provides synchronized
Expand Down Expand Up @@ -197,7 +197,7 @@ func getGeoIPDB(resource string) (*geoIPDB, error) {
}, nil
}

func open(resource string) (*registry.File, error) {
func open(resource string) (*updates.File, error) {
f, err := module.instance.IntelUpdates().GetFile(resource)
if err != nil {
return nil, fmt.Errorf("getting file: %w", err)
Expand Down
Loading

0 comments on commit 84a15b5

Please sign in to comment.