fortify: clean up config loading
Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
parent
d2575b6708
commit
61b473a06f
88
config.go
88
config.go
|
@ -1,9 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"flag"
|
"flag"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.ophivana.moe/cat/fortify/dbus"
|
||||||
"git.ophivana.moe/cat/fortify/internal"
|
"git.ophivana.moe/cat/fortify/internal"
|
||||||
|
"git.ophivana.moe/cat/fortify/internal/app"
|
||||||
|
"git.ophivana.moe/cat/fortify/internal/state"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -11,15 +16,12 @@ var (
|
||||||
|
|
||||||
dbusConfigSession string
|
dbusConfigSession string
|
||||||
dbusConfigSystem string
|
dbusConfigSystem string
|
||||||
dbusVerbose bool
|
|
||||||
dbusID string
|
dbusID string
|
||||||
mpris bool
|
mpris bool
|
||||||
|
dbusVerbose bool
|
||||||
|
|
||||||
userName string
|
userName string
|
||||||
mustWayland bool
|
enablements [state.EnableLength]bool
|
||||||
mustX bool
|
|
||||||
mustDBus bool
|
|
||||||
mustPulse bool
|
|
||||||
|
|
||||||
launchMethodText string
|
launchMethodText string
|
||||||
)
|
)
|
||||||
|
@ -30,15 +32,15 @@ func init() {
|
||||||
|
|
||||||
flag.StringVar(&dbusConfigSession, "dbus-config", "builtin", "Path to D-Bus proxy config file, or \"builtin\" for defaults")
|
flag.StringVar(&dbusConfigSession, "dbus-config", "builtin", "Path to D-Bus proxy config file, or \"builtin\" for defaults")
|
||||||
flag.StringVar(&dbusConfigSystem, "dbus-system", "nil", "Path to system D-Bus proxy config file, or \"nil\" to disable")
|
flag.StringVar(&dbusConfigSystem, "dbus-system", "nil", "Path to system D-Bus proxy config file, or \"nil\" to disable")
|
||||||
flag.BoolVar(&dbusVerbose, "dbus-log", false, "Enable logging in the D-Bus proxy")
|
|
||||||
flag.StringVar(&dbusID, "dbus-id", "", "D-Bus ID of application, leave empty to disable own paths, has no effect if custom config is available")
|
flag.StringVar(&dbusID, "dbus-id", "", "D-Bus ID of application, leave empty to disable own paths, has no effect if custom config is available")
|
||||||
flag.BoolVar(&mpris, "mpris", false, "Allow owning MPRIS D-Bus path, has no effect if custom config is available")
|
flag.BoolVar(&mpris, "mpris", false, "Allow owning MPRIS D-Bus path, has no effect if custom config is available")
|
||||||
|
flag.BoolVar(&dbusVerbose, "dbus-log", false, "Force logging in the D-Bus proxy")
|
||||||
|
|
||||||
flag.StringVar(&userName, "u", "chronos", "Passwd name of user to run as")
|
flag.StringVar(&userName, "u", "chronos", "Passwd name of user to run as")
|
||||||
flag.BoolVar(&mustWayland, "wayland", false, "Share Wayland socket")
|
flag.BoolVar(&enablements[state.EnableWayland], "wayland", false, "Share Wayland socket")
|
||||||
flag.BoolVar(&mustX, "X", false, "Share X11 socket and allow connection")
|
flag.BoolVar(&enablements[state.EnableX], "X", false, "Share X11 socket and allow connection")
|
||||||
flag.BoolVar(&mustDBus, "dbus", false, "Proxy D-Bus connection")
|
flag.BoolVar(&enablements[state.EnableDBus], "dbus", false, "Proxy D-Bus connection")
|
||||||
flag.BoolVar(&mustPulse, "pulse", false, "Share PulseAudio socket and cookie")
|
flag.BoolVar(&enablements[state.EnablePulse], "pulse", false, "Share PulseAudio socket and cookie")
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -49,3 +51,69 @@ func init() {
|
||||||
|
|
||||||
flag.StringVar(&launchMethodText, "method", "sudo", methodHelpString)
|
flag.StringVar(&launchMethodText, "method", "sudo", methodHelpString)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func loadConfig() *app.Config {
|
||||||
|
if confPath == "nil" {
|
||||||
|
// config from flags
|
||||||
|
return configFromFlags()
|
||||||
|
} else {
|
||||||
|
// config from file
|
||||||
|
c := new(app.Config)
|
||||||
|
if f, err := os.Open(confPath); err != nil {
|
||||||
|
fatalf("cannot access config file '%s': %s\n", confPath, err)
|
||||||
|
panic("unreachable")
|
||||||
|
} else if err = json.NewDecoder(f).Decode(&c); err != nil {
|
||||||
|
fatalf("cannot parse config file '%s': %s\n", confPath, err)
|
||||||
|
panic("unreachable")
|
||||||
|
} else {
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func configFromFlags() (config *app.Config) {
|
||||||
|
// initialise config from flags
|
||||||
|
config = &app.Config{
|
||||||
|
ID: dbusID,
|
||||||
|
User: userName,
|
||||||
|
Command: flag.Args(),
|
||||||
|
Method: launchMethodText,
|
||||||
|
}
|
||||||
|
|
||||||
|
// enablements from flags
|
||||||
|
for i := state.Enablement(0); i < state.EnableLength; i++ {
|
||||||
|
if enablements[i] {
|
||||||
|
config.Confinement.Enablements.Set(i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse D-Bus config file from flags if applicable
|
||||||
|
if enablements[state.EnableDBus] {
|
||||||
|
if dbusConfigSession == "builtin" {
|
||||||
|
config.Confinement.SessionBus = dbus.NewConfig(dbusID, true, mpris)
|
||||||
|
} else {
|
||||||
|
if c, err := dbus.NewConfigFromFile(dbusConfigSession); err != nil {
|
||||||
|
fatalf("cannot load session bus proxy config from %q: %s\n", dbusConfigSession, err)
|
||||||
|
} else {
|
||||||
|
config.Confinement.SessionBus = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// system bus proxy is optional
|
||||||
|
if dbusConfigSystem != "nil" {
|
||||||
|
if c, err := dbus.NewConfigFromFile(dbusConfigSystem); err != nil {
|
||||||
|
fatalf("cannot load system bus proxy config from %q: %s\n", dbusConfigSystem, err)
|
||||||
|
} else {
|
||||||
|
config.Confinement.SystemBus = c
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// override log from configuration
|
||||||
|
if dbusVerbose {
|
||||||
|
config.Confinement.SessionBus.Log = true
|
||||||
|
config.Confinement.SystemBus.Log = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
79
main.go
79
main.go
|
@ -1,17 +1,14 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.ophivana.moe/cat/fortify/dbus"
|
|
||||||
"git.ophivana.moe/cat/fortify/internal"
|
"git.ophivana.moe/cat/fortify/internal"
|
||||||
"git.ophivana.moe/cat/fortify/internal/app"
|
"git.ophivana.moe/cat/fortify/internal/app"
|
||||||
"git.ophivana.moe/cat/fortify/internal/shim"
|
"git.ophivana.moe/cat/fortify/internal/shim"
|
||||||
"git.ophivana.moe/cat/fortify/internal/state"
|
|
||||||
"git.ophivana.moe/cat/fortify/internal/verbose"
|
"git.ophivana.moe/cat/fortify/internal/verbose"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -43,27 +40,10 @@ func main() {
|
||||||
// state query command early exit
|
// state query command early exit
|
||||||
tryState()
|
tryState()
|
||||||
|
|
||||||
// prepare config
|
|
||||||
var config *app.Config
|
|
||||||
|
|
||||||
if confPath == "nil" {
|
|
||||||
// config from flags
|
|
||||||
config = configFromFlags()
|
|
||||||
} else {
|
|
||||||
// config from file
|
|
||||||
if f, err := os.Open(confPath); err != nil {
|
|
||||||
fatalf("cannot access config file '%s': %s\n", confPath, err)
|
|
||||||
} else {
|
|
||||||
if err = json.NewDecoder(f).Decode(&config); err != nil {
|
|
||||||
fatalf("cannot parse config file '%s': %s\n", confPath, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// invoke app
|
// invoke app
|
||||||
r := 1
|
r := 1
|
||||||
a := app.New()
|
a := app.New()
|
||||||
if err := a.Seal(config); err != nil {
|
if err := a.Seal(loadConfig()); err != nil {
|
||||||
logBaseError(err, "fortify: cannot seal app:")
|
logBaseError(err, "fortify: cannot seal app:")
|
||||||
} else if err = a.Start(); err != nil {
|
} else if err = a.Start(); err != nil {
|
||||||
logBaseError(err, "fortify: cannot start app:")
|
logBaseError(err, "fortify: cannot start app:")
|
||||||
|
@ -121,63 +101,6 @@ func logBaseError(err error, message string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func configFromFlags() (config *app.Config) {
|
|
||||||
// initialise config from flags
|
|
||||||
config = &app.Config{
|
|
||||||
ID: dbusID,
|
|
||||||
User: userName,
|
|
||||||
Command: flag.Args(),
|
|
||||||
Method: launchMethodText,
|
|
||||||
}
|
|
||||||
|
|
||||||
// enablements from flags
|
|
||||||
if mustWayland {
|
|
||||||
config.Confinement.Enablements.Set(state.EnableWayland)
|
|
||||||
}
|
|
||||||
if mustX {
|
|
||||||
config.Confinement.Enablements.Set(state.EnableX)
|
|
||||||
}
|
|
||||||
if mustDBus {
|
|
||||||
config.Confinement.Enablements.Set(state.EnableDBus)
|
|
||||||
}
|
|
||||||
if mustPulse {
|
|
||||||
config.Confinement.Enablements.Set(state.EnablePulse)
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse D-Bus config file from flags if applicable
|
|
||||||
if mustDBus {
|
|
||||||
if dbusConfigSession == "builtin" {
|
|
||||||
config.Confinement.SessionBus = dbus.NewConfig(dbusID, true, mpris)
|
|
||||||
} else {
|
|
||||||
if f, err := os.Open(dbusConfigSession); err != nil {
|
|
||||||
fatalf("cannot access session bus proxy config file '%s': %s\n", dbusConfigSession, err)
|
|
||||||
} else {
|
|
||||||
if err = json.NewDecoder(f).Decode(&config.Confinement.SessionBus); err != nil {
|
|
||||||
fatalf("cannot parse session bus proxy config file '%s': %s\n", dbusConfigSession, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// system bus proxy is optional
|
|
||||||
if dbusConfigSystem != "nil" {
|
|
||||||
if f, err := os.Open(dbusConfigSystem); err != nil {
|
|
||||||
fatalf("cannot access system bus proxy config file '%s': %s\n", dbusConfigSystem, err)
|
|
||||||
} else {
|
|
||||||
if err = json.NewDecoder(f).Decode(&config.Confinement.SystemBus); err != nil {
|
|
||||||
fatalf("cannot parse system bus proxy config file '%s': %s\n", dbusConfigSystem, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if dbusVerbose {
|
|
||||||
config.Confinement.SessionBus.Log = true
|
|
||||||
config.Confinement.SystemBus.Log = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func fatalf(format string, a ...any) {
|
func fatalf(format string, a ...any) {
|
||||||
fmt.Printf("fortify: "+format, a...)
|
fmt.Printf("fortify: "+format, a...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -37,8 +37,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func ChangeHosts(mode, family C.uint8_t, address string) error {
|
func ChangeHosts(mode, family C.uint8_t, address string) error {
|
||||||
var c *C.xcb_connection_t
|
c := C.xcb_connect(nil, nil)
|
||||||
c = C.xcb_connect(nil, nil)
|
|
||||||
defer C.xcb_disconnect(c)
|
defer C.xcb_disconnect(c)
|
||||||
|
|
||||||
if err := xcbHandleConnectionError(c); err != nil {
|
if err := xcbHandleConnectionError(c); err != nil {
|
||||||
|
|
Loading…
Reference in New Issue