Require capabilities on startup

pull/92/head
lawl 3 years ago
parent e7072b2bf9
commit 730dc35d94

@ -58,10 +58,11 @@ func makeBinarySetcapped() error {
return nil
}
func pkexecSetcapSelf() {
func pkexecSetcapSelf() error {
self, err := os.Executable()
if err != nil {
log.Fatalf("Couldn't find path to own binary\n")
return err
}
cmd := exec.Command("pkexec", self, "-setcap")
@ -69,5 +70,8 @@ func pkexecSetcapSelf() {
err = cmd.Run()
if err != nil {
log.Printf("Couldn't setcap self as root: %v\n", err)
return err
}
return nil
}

@ -200,6 +200,8 @@ func main() {
go paConnectionWatchdog(&ctx)
ctx.haveCapabilities = hasCapSysResource(getCurrentCaps())
wnd := nucular.NewMasterWindowSize(0, appName, image.Point{600, 400}, func(w *nucular.Window) {
updatefn(&ctx, w)
})

55
ui.go

@ -8,7 +8,9 @@ import (
"image/draw"
"image/png"
"log"
"os"
"os/exec"
"syscall"
"time"
"github.com/aarzilli/nucular"
@ -31,6 +33,8 @@ type ntcontext struct {
masterWindow *nucular.MasterWindow
update updateui
reloadRequired bool
haveCapabilities bool
errorMsg string
}
var green = color.RGBA{34, 187, 69, 255}
@ -41,6 +45,18 @@ var patreonImg *image.RGBA
func updatefn(ctx *ntcontext, w *nucular.Window) {
//TODO: this is disgusting
if ctx.errorMsg != "" {
errorScreen(ctx, w)
return
}
if !ctx.haveCapabilities {
capabilitiesScreen(ctx, w)
return
}
if !ctx.paClient.Connected() {
connectScreen(ctx, w)
return
@ -373,6 +389,45 @@ func connectScreen(ctx *ntcontext, w *nucular.Window) {
w.Label("Connecting to pulseaudio...", "CB")
}
func capabilitiesScreen(ctx *ntcontext, w *nucular.Window) {
w.Row(15).Dynamic(1)
w.Label("NoiseTorch currently does not have the capabilities to function properly.", "CB")
w.Row(15).Dynamic(1)
w.Label("We require CAP_SYS_RESOURCE. If that doesn't mean anything to you, don't worry. I'll fix it for you.", "CB")
w.Row(40).Dynamic(1)
w.Row(25).Dynamic(1)
if w.ButtonText("Grant capability (requires root)") {
err := pkexecSetcapSelf()
if err != nil {
ctx.errorMsg = err.Error()
return
}
self, err := os.Executable()
if err != nil {
ctx.errorMsg = err.Error()
return
}
err = syscall.Exec(self, []string{""}, os.Environ())
if err != nil {
ctx.errorMsg = err.Error()
return
}
}
}
func errorScreen(ctx *ntcontext, w *nucular.Window) {
w.Row(15).Dynamic(1)
w.Label("Error", "CB")
w.Row(15).Dynamic(1)
w.Label(ctx.errorMsg, "CB")
w.Row(40).Dynamic(1)
w.Row(25).Dynamic(1)
if w.ButtonText("OK") {
ctx.errorMsg = ""
return
}
}
func resetUI(ctx *ntcontext) {
ctx.loadingScreen = false

Loading…
Cancel
Save