mirror of
https://github.com/safing/portmaster
synced 2025-09-04 11:39:29 +00:00
Improve core module to take care of dir structures and db init
This commit is contained in:
parent
82026cd7d9
commit
7a6189143c
5 changed files with 169 additions and 10 deletions
25
core/config.go
Normal file
25
core/config.go
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/safing/portbase/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
devMode config.BoolOption
|
||||||
|
)
|
||||||
|
|
||||||
|
func registerConfig() error {
|
||||||
|
err := config.Register(&config.Option{
|
||||||
|
Name: "Development Mode",
|
||||||
|
Key: "core/devMode",
|
||||||
|
Description: "In Development Mode security restrictions are lifted/softened to enable easier access to Portmaster for debugging and testing purposes. This is potentially very insecure, only activate if you know what you are doing.",
|
||||||
|
ExpertiseLevel: config.ExpertiseLevelDeveloper,
|
||||||
|
OptType: config.OptTypeBool,
|
||||||
|
DefaultValue: true,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -2,21 +2,12 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/safing/portbase/database"
|
"github.com/safing/portbase/database"
|
||||||
"github.com/safing/portbase/modules"
|
|
||||||
"github.com/safing/portbase/notifications"
|
|
||||||
|
|
||||||
// module dependencies
|
// module dependencies
|
||||||
_ "github.com/safing/portbase/database/dbmodule"
|
|
||||||
_ "github.com/safing/portbase/database/storage/bbolt"
|
_ "github.com/safing/portbase/database/storage/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func registerDatabases() error {
|
||||||
modules.Register("core", nil, start, nil, "database")
|
|
||||||
|
|
||||||
notifications.SetPersistenceBasePath("core:notifications")
|
|
||||||
}
|
|
||||||
|
|
||||||
func start() error {
|
|
||||||
_, err := database.Register(&database.Database{
|
_, err := database.Register(&database.Database{
|
||||||
Name: "core",
|
Name: "core",
|
||||||
Description: "Holds core data, such as settings and profiles",
|
Description: "Holds core data, such as settings and profiles",
|
||||||
|
|
58
core/db.go
Normal file
58
core/db.go
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/database"
|
||||||
|
"github.com/safing/portbase/log"
|
||||||
|
|
||||||
|
"github.com/safing/portmaster/core/structure"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
maintenanceWg sync.WaitGroup
|
||||||
|
maintenanceShortTickDuration = 10 * time.Minute
|
||||||
|
maintenanceLongTickDuration = 1 * time.Hour
|
||||||
|
)
|
||||||
|
|
||||||
|
func startDB() error {
|
||||||
|
err := database.Initialize(dataDir, structure.Root())
|
||||||
|
if err == nil {
|
||||||
|
maintenanceWg.Add(1)
|
||||||
|
go maintenanceWorker()
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func stopDB() error {
|
||||||
|
maintenanceWg.Wait()
|
||||||
|
return database.Shutdown()
|
||||||
|
}
|
||||||
|
|
||||||
|
func maintenanceWorker() {
|
||||||
|
ticker := time.NewTicker(maintenanceShortTickDuration)
|
||||||
|
longTicker := time.NewTicker(maintenanceLongTickDuration)
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
err := database.Maintain()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("database: maintenance error: %s", err)
|
||||||
|
}
|
||||||
|
case <-longTicker.C:
|
||||||
|
err := database.MaintainRecordStates()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("database: record states maintenance error: %s", err)
|
||||||
|
}
|
||||||
|
err = database.MaintainThorough()
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("database: thorough maintenance error: %s", err)
|
||||||
|
}
|
||||||
|
case <-shuttingDown:
|
||||||
|
maintenanceWg.Done()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
58
core/main.go
Normal file
58
core/main.go
Normal file
|
@ -0,0 +1,58 @@
|
||||||
|
package core
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/modules"
|
||||||
|
"github.com/safing/portbase/notifications"
|
||||||
|
|
||||||
|
"github.com/safing/portmaster/core/structure"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
dataDir string
|
||||||
|
databaseDir string
|
||||||
|
|
||||||
|
shuttingDown = make(chan struct{})
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
flag.StringVar(&dataDir, "data", "", "set data directory")
|
||||||
|
flag.StringVar(&databaseDir, "db", "", "alias to --data (deprecated)")
|
||||||
|
|
||||||
|
modules.Register("core", prep, start, stop)
|
||||||
|
|
||||||
|
notifications.SetPersistenceBasePath("core:notifications")
|
||||||
|
}
|
||||||
|
|
||||||
|
func prep() error {
|
||||||
|
// backwards compatibility
|
||||||
|
if dataDir == "" {
|
||||||
|
dataDir = databaseDir
|
||||||
|
}
|
||||||
|
|
||||||
|
// check data dir
|
||||||
|
if dataDir == "" {
|
||||||
|
return errors.New("please set the data directory using --data=/path/to/data/dir")
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize structure
|
||||||
|
return structure.Initialize(dataDir, 0755)
|
||||||
|
}
|
||||||
|
|
||||||
|
func start() error {
|
||||||
|
// init DB
|
||||||
|
err := startDB()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// register DBs
|
||||||
|
return registerDatabases()
|
||||||
|
}
|
||||||
|
|
||||||
|
func stop() error {
|
||||||
|
close(shuttingDown)
|
||||||
|
return stopDB()
|
||||||
|
}
|
27
core/structure/dirs.go
Normal file
27
core/structure/dirs.go
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
package structure
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/safing/portbase/utils"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
root *utils.DirStructure
|
||||||
|
)
|
||||||
|
|
||||||
|
// Initialize initializes the data root directory
|
||||||
|
func Initialize(rootDir string, perm os.FileMode) error {
|
||||||
|
root = utils.NewDirStructure(rootDir, perm)
|
||||||
|
return root.Ensure()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Root returns the data root directory.
|
||||||
|
func Root() *utils.DirStructure {
|
||||||
|
return root
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewRootDir calls ChildDir() on the data root directory.
|
||||||
|
func NewRootDir(dirName string, perm os.FileMode) (childDir *utils.DirStructure) {
|
||||||
|
return root.ChildDir(dirName, perm)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue