Skip to content

Commit

Permalink
Adding Pulseaudio loading and unloading + automating DISPLAY value co…
Browse files Browse the repository at this point in the history
…nfiguration based on host env variable
  • Loading branch information
FlUxIuS committed Jul 11, 2024
1 parent 9643839 commit 75abf38
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 13 deletions.
38 changes: 37 additions & 1 deletion go/rfswift/cli/rfcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ var runCmd = &cobra.Command{
} else {
rfutils.XHostEnable() // force xhost to add local connections ALCs, TODO: to optimize later
}
rfdock.DockerSetXDisplay(XDisplay)
rfdock.DockerSetShell(ExecCmd)
rfdock.DockerAddBiding(ExtraBind)
rfdock.DockerSetImage(DImage)
Expand Down Expand Up @@ -207,6 +208,35 @@ var DeleteCmd = &cobra.Command{
},
}

var HostCmd = &cobra.Command{
Use: "host",
Short: "Host configuration",
Long: `Configures the host for container operations`,
}

var HostPulseAudioCmd = &cobra.Command{
Use: "audio",
Short: "Pulseaudio server",
Long: `Manage pulseaudio server`,
}

var HostPulseAudioEnableCmd = &cobra.Command{
Use: "enable",
Short: "Enable connection",
Long: `Allow connections to a specific port and interface. Warning: command to be executed as user!`,
Run: func(cmd *cobra.Command, args []string) {
rfutils.SetPulseCTL(PulseServer)
},
}

var HostPulseAudioUnloadCmd = &cobra.Command{
Use: "unload",
Short: "Unload TCP module from Pulseaudio server",
Run: func(cmd *cobra.Command, args []string) {
rfutils.UnloadPulseCTL()
},
}

func init() {
rootCmd.AddCommand(runCmd)
rootCmd.AddCommand(lastCmd)
Expand All @@ -218,6 +248,7 @@ func init() {
rootCmd.AddCommand(removeCmd)
rootCmd.AddCommand(ImagesCmd)
rootCmd.AddCommand(DeleteCmd)
rootCmd.AddCommand(HostCmd)

// Adding special commands for Windows
os := runtime.GOOS
Expand All @@ -230,6 +261,11 @@ func init() {
winusbdetachCmd.Flags().StringVarP(&UsbDevice, "busid", "i", "", "busid")
}

HostCmd.AddCommand(HostPulseAudioCmd)
HostPulseAudioCmd.AddCommand(HostPulseAudioEnableCmd)
HostPulseAudioCmd.AddCommand(HostPulseAudioUnloadCmd)
HostPulseAudioEnableCmd.Flags().StringVarP(&PulseServer, "pulseserver", "s", "tcp:127.0.0.1:34567", "pulse server address (by default: 'tcp:127.0.0.1:34567')")

DeleteCmd.Flags().StringVarP(&ContID, "image", "i", "", "image ID or tag")
removeCmd.Flags().StringVarP(&ContID, "container", "c", "", "container to remove")
installCmd.Flags().StringVarP(&ExecCmd, "install", "i", "", "function for installation")
Expand All @@ -249,7 +285,7 @@ func init() {
execCmd.Flags().StringVarP(&SInstall, "install", "i", "", "install from function script (e.g: 'sdrpp_soft_install')")
//execCmd.MarkFlagRequired("command")
runCmd.Flags().StringVarP(&ExtraHost, "extrahosts", "x", "", "set extra hosts (default: 'pluto.local:192.168.1.2', and separate them with commas)")
runCmd.Flags().StringVarP(&XDisplay, "display", "d", "", "set X Display (by default: 'DISPLAY=:0', and separate them with commas)")
runCmd.Flags().StringVarP(&XDisplay, "display", "d", rfutils.GetDisplayEnv(), "set X Display (duplicates hosts's env by default)")
runCmd.Flags().StringVarP(&ExecCmd, "command", "e", "", "command to exec (by default: '/bin/bash')")
runCmd.Flags().StringVarP(&ExtraBind, "bind", "b", "", "extra bindings (separate them with commas)")
runCmd.Flags().StringVarP(&DImage, "image", "i", "", "image (default: 'myrfswift:latest')")
Expand Down
1 change: 1 addition & 0 deletions go/rfswift/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lawl/pulseaudio v0.0.0-20220626105240-976bed5e247c // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go/rfswift/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLf
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/lawl/pulseaudio v0.0.0-20220626105240-976bed5e247c h1:j96x2SroUFS59ENzG7vI5n/hJ79qDYv8/MJ7pumgJCk=
github.com/lawl/pulseaudio v0.0.0-20220626105240-976bed5e247c/go.mod h1:9h36x4KH7r2V8DOCKoPMt87IXZ++X90y8D5nnuwq290=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
Expand Down
2 changes: 1 addition & 1 deletion go/rfswift/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
cli "penthertz/rfswift/cli"
)

var version = "0.4.1"
var version = "0.4.2"

var ascii_art = `
Expand Down
79 changes: 68 additions & 11 deletions go/rfswift/rfutils/hostcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"fmt"
"os/exec"
"strings"

"github.com/lawl/pulseaudio"
)

// USBDevice represents a USB device information
Expand Down Expand Up @@ -245,18 +247,73 @@ func AutoUnbindDetachUSB_Windows() {

func SetPulseCTL(address string) error {
/*
* SetPulseCTL Set pulse server IP and TCP port ACLs
* in(1): ip string
* in(2): port string
* out: error
*/

* Use PACTL in command line to accept connection in TCP with defined port
*/
parts := strings.Split(address, ":")
portstr := "port=" + parts[2]
ipstr := "auth-ip-acl=" + parts[1]
cmd := exec.Command("pactl", "load-module", "module-native-protocol-tcp", portstr, ipstr)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to load ACLs %s: %w", address, err)
if len(parts) != 3 {
return fmt.Errorf("invalid address format, expected format 'protocol:ip:port'")
}
port := parts[2]
ip := parts[1]

// Connect to PulseAudio
client, err := pulseaudio.NewClient()
if err != nil {
return fmt.Errorf("failed to connect to PulseAudio: %w", err)
}
defer client.Close()

// Construct the module arguments string
moduleArgs := fmt.Sprintf("port=%s auth-ip-acl=%s", port, ip)

// Load module-native-protocol-tcp with the specified IP and port
moduleIndex, err := client.LoadModule("module-native-protocol-tcp", moduleArgs)
if err != nil {
return fmt.Errorf("failed to load module-native-protocol-tcp: %w", err)
}

fmt.Printf("[+] Successfully loaded module-native-protocol-tcp with index %d\n", moduleIndex)
return nil
}

func UnloadPulseCTL() error {
/*
* Unload pulseaudio TCP module
*/
cmd := exec.Command("pactl", "list", "modules")
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to list PulseAudio modules: %w\nOutput: %s", err, string(output))
}

// Parse the output to find the module-native-protocol-tcp index
lines := strings.Split(string(output), "\n")
var moduleIndex string
for i, line := range lines {
if strings.Contains(line, "Name: module-native-protocol-tcp") {
// Find the "Index:" line above the module name
for j := i; j >= 0; j-- {
if strings.Contains(lines[j], "Module #") {
moduleIndex = strings.TrimSpace(strings.TrimPrefix(lines[j], "Module #"))
break
}
}
break
}
}

if moduleIndex == "" {
return fmt.Errorf("module-native-protocol-tcp not found")
}

// Execute pactl unload-module to unload the module
unloadCmd := exec.Command("pactl", "unload-module", moduleIndex)
unloadOutput, err := unloadCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("failed to unload module-native-protocol-tcp: %w\nOutput: %s", err, string(unloadOutput))
}
fmt.Printf("Command output: %s\n", string(unloadOutput))

fmt.Printf("[+] Successfully unloaded module-native-protocol-tcp with index %s\n", moduleIndex)
return nil
}
21 changes: 21 additions & 0 deletions go/rfswift/rfutils/rfutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package rfutils

import (
"fmt"
"os"
"os/exec"
)

Expand All @@ -31,3 +32,23 @@ func XHostEnable() {
s := "xhost local:root"
HostCmdExec(s)
}

func displayEnv() (string, error) {
display := os.Getenv("DISPLAY")
if display == "" {
return "", fmt.Errorf("DISPLAY environment variable is not set")
}
return display, nil
}

func GetDisplayEnv() (string) {
var dispenv string
display, err := displayEnv()
if err != nil {
fmt.Println("Error (using default 'DISPLAY=:0 value'):", err)
dispenv = "DISPLAY=:0"
} else {
dispenv = "DISPLAY="+display
}
return dispenv
}

0 comments on commit 75abf38

Please sign in to comment.