adding mac support mainly
This commit is contained in:
@@ -16,16 +16,31 @@ import (
|
||||
"os/exec"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
socketPath = "/run/streamdeck-go/helper.sock"
|
||||
whitelistPath = "/etc/streamdeck-go/privileged.yaml"
|
||||
socketMode = 0660
|
||||
)
|
||||
const socketMode = 0660
|
||||
|
||||
// socketPath returns the Unix socket path for the helper daemon.
|
||||
// /run is standard on Linux; /var/run is used on macOS.
|
||||
func socketPath() string {
|
||||
if runtime.GOOS == "darwin" {
|
||||
return "/var/run/streamdeck-go/helper.sock"
|
||||
}
|
||||
return "/run/streamdeck-go/helper.sock"
|
||||
}
|
||||
|
||||
// whitelistPath returns the path to the root-owned command whitelist.
|
||||
// /etc is standard on Linux; /usr/local/etc is the convention on macOS.
|
||||
func whitelistPath() string {
|
||||
if runtime.GOOS == "darwin" {
|
||||
return "/usr/local/etc/streamdeck-go/privileged.yaml"
|
||||
}
|
||||
return "/etc/streamdeck-go/privileged.yaml"
|
||||
}
|
||||
|
||||
type whitelist struct {
|
||||
Commands map[string]string `yaml:"commands"`
|
||||
@@ -46,25 +61,28 @@ func main() {
|
||||
log.Fatal("streamdeck-helper must run as root (install as a system service)")
|
||||
}
|
||||
|
||||
wl, err := loadWhitelist(whitelistPath)
|
||||
if err != nil {
|
||||
log.Fatalf("load whitelist %q: %v", whitelistPath, err)
|
||||
}
|
||||
log.Printf("loaded %d whitelisted commands from %s", len(wl.Commands), whitelistPath)
|
||||
sock := socketPath()
|
||||
wlist := whitelistPath()
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(socketPath), 0755); err != nil {
|
||||
wl, err := loadWhitelist(wlist)
|
||||
if err != nil {
|
||||
log.Fatalf("load whitelist %q: %v", wlist, err)
|
||||
}
|
||||
log.Printf("loaded %d whitelisted commands from %s", len(wl.Commands), wlist)
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(sock), 0755); err != nil {
|
||||
log.Fatalf("create socket dir: %v", err)
|
||||
}
|
||||
// Remove stale socket from a previous run.
|
||||
_ = os.Remove(socketPath)
|
||||
_ = os.Remove(sock)
|
||||
|
||||
ln, err := net.Listen("unix", socketPath)
|
||||
ln, err := net.Listen("unix", sock)
|
||||
if err != nil {
|
||||
log.Fatalf("listen on %s: %v", socketPath, err)
|
||||
log.Fatalf("listen on %s: %v", sock, err)
|
||||
}
|
||||
defer ln.Close()
|
||||
|
||||
if err := os.Chmod(socketPath, socketMode); err != nil {
|
||||
if err := os.Chmod(sock, socketMode); err != nil {
|
||||
log.Fatalf("chmod socket: %v", err)
|
||||
}
|
||||
// Chown the socket to root:streamdeck so group members can connect.
|
||||
@@ -73,11 +91,11 @@ func main() {
|
||||
log.Fatalf("group 'streamdeck' not found — run 'make install-helper' first: %v", err)
|
||||
} else if gid, err := strconv.Atoi(grp.Gid); err != nil {
|
||||
log.Fatalf("invalid gid %q: %v", grp.Gid, err)
|
||||
} else if err := os.Lchown(socketPath, 0, gid); err != nil {
|
||||
} else if err := os.Lchown(sock, 0, gid); err != nil {
|
||||
log.Fatalf("chown socket: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("listening on %s (group: streamdeck)", socketPath)
|
||||
log.Printf("listening on %s (group: streamdeck)", sock)
|
||||
|
||||
for {
|
||||
conn, err := ln.Accept()
|
||||
@@ -111,7 +129,7 @@ func handle(conn net.Conn, wl *whitelist) {
|
||||
shell, ok := wl.Commands[req.Command]
|
||||
if !ok {
|
||||
log.Printf("REJECTED unknown command %q", req.Command)
|
||||
send(conn, response{Error: fmt.Sprintf("unknown command %q — add it to %s", req.Command, whitelistPath)})
|
||||
send(conn, response{Error: fmt.Sprintf("unknown command %q — add it to %s", req.Command, whitelistPath())})
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user