From b4752b139708c4300812f0a67c69156eec7b5de6 Mon Sep 17 00:00:00 2001 From: Albert Nisbet Date: Wed, 26 Apr 2023 12:04:17 +1200 Subject: [PATCH] Add random default for client ID --- README.md | 66 +++++++++++++++++++++++++++---------------------------- main.go | 19 ++++++++++++++-- 2 files changed, 50 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 3ea1dae..3b09832 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ mqcontrol --help -h string Address and port of MQTT broker (default "127.0.0.1:1883") -i string - ID to use for this client + ID to use for this client (default "mqcontrol-{randomString}") -t string Topic to subscribe to (default "computer/command") -u string @@ -46,31 +46,31 @@ mqcontrol --help ### Examples -* Make a topic to hibernate your PC +- Make a topic to hibernate your PC - ```bash - mqcontrol -i mqc -c "systemctl hibernate" -t desktop/command/hibernate - ``` + ```bash + mqcontrol -c "systemctl hibernate" -t desktop/command/hibernate + ``` -* Dim laptop screen when the lights are turned off +- Dim laptop screen when the lights are turned off - ```bash - mqcontrol -i mqc -c "brightnessctl 50%-" -t lights/bedroom/turnoff - ``` + ```bash + mqcontrol -c "brightnessctl 50%-" -t lights/bedroom/turnoff + ``` -* Close gzdoom when the office door is opened +- Close gzdoom when the office door is opened - ```bash - mqcontrol -i mqc -c "killall gzdoom" -t work/office/door/open - ``` + ```bash + mqcontrol -c "killall gzdoom" -t work/office/door/open + ``` ### Notes - The command argument does not include any shell processing. If you're having problems getting commands to run or want them to run in a shell, specify the shell in the command. For example: - ```bash - mqcontrol -i mqc -c "/bin/sh -c \"echo message received\"" -t desktop/command/hibernate - ``` + ```bash + mqcontrol -c "/bin/sh -c \"echo message received\"" -t desktop/command/hibernate + ``` - An error in the executed command will cause the entire program to terminate. Stderr and an exit code from the executed command will be available. @@ -80,27 +80,27 @@ Get then run with Go: ```bash go get github.com/albertnis/mqcontrol -go run github.com/albertnis/mqcontrol -i mqc -c "echo Message received" +go run github.com/albertnis/mqcontrol -c "echo Message received" ``` Run with Go in cloned repo: ```bash -go run main.go -i mqc -c "echo Message received" +go run main.go -c "echo Message received" ``` With Docker (BuildKit): ```bash DOCKER_BUILDKIT=1 docker build -t mqcontrol . -docker run -it --rm --network=host mqcontrol -i mqc -c "echo Message received" +docker run -it --rm --network=host mqcontrol -c "echo Message received" ``` With docker-compose (BuildKit): ```bash COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build -docker-compose run mqcontrol -i mqc -c "echo Message received" +docker-compose run mqcontrol -c "echo Message received" ``` ## Run it at startup @@ -118,21 +118,21 @@ docker-compose run mqcontrol -i mqc -c "echo Message received" 1. Create a systemd unit file as below, customise the `ExecStart` line, then save it at `/usr/lib/systemd/system/mqcontrol.service`: - ```service - [Unit] - Description=mqcontrol remote control + ```service + [Unit] + Description=mqcontrol remote control - [Service] - Type=simple - ExecStart=/home/user/go/bin/mqcontrol -i mqclinux -c "systemctl hibernate" -h 192.168.1.110:1883 + [Service] + Type=simple + ExecStart=/home/user/go/bin/mqcontrol -i mqclinux -c "systemctl hibernate" -h 192.168.1.110:1883 - [Install] - WantedBy=multi-user.target - ``` + [Install] + WantedBy=multi-user.target + ``` 1. Start and enable the `mqcontrol` service - ```sh - systemctl start mqcontrol - systemctl enable mqcontrol - ``` + ```sh + systemctl start mqcontrol + systemctl enable mqcontrol + ``` diff --git a/main.go b/main.go index c3c6a80..af436f1 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "crypto/rand" "encoding/csv" "flag" "fmt" @@ -24,6 +25,16 @@ type config struct { Password string } +func randomString(n int) string { + const alphanum = "ABCDEFGHJKMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz" + var bytes = make([]byte, n) + rand.Read(bytes) + for i, b := range bytes { + bytes[i] = alphanum[b%byte(len(alphanum))] + } + return string(bytes) +} + func subscribe(client MQTT.Client, topic string, msg chan []byte) { handler := MQTT.MessageHandler(func(client MQTT.Client, message MQTT.Message) { payload := message.Payload() @@ -74,7 +85,7 @@ func main() { commandPtr := flag.String("c", "", "Command to run when any message received on topic") topicPtr := flag.String("t", "computer/command", "Topic to subscribe to") brokerPtr := flag.String("h", "127.0.0.1:1883", "Address and port of MQTT broker") - clientIDPtr := flag.String("i", "", "ID to use for this client") + clientIDPtr := flag.String("i", "mqcontrol-{randomString}", "ID to use for this client") userPtr := flag.String("u", "", "Username for MQTT connection") passwordPtr := flag.String("p", "", "Password for MQTT connection") @@ -102,11 +113,15 @@ func main() { os.Exit(1) } if conf.ClientID == "" { - fmt.Println("No client ID argument provided") + fmt.Println("Empty client ID argument provided") fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0]) flag.PrintDefaults() os.Exit(1) } + if conf.ClientID == "mqcontrol-{randomString}" { + conf.ClientID = "mqcontrol-" + randomString(8) + fmt.Println("No client ID argument provided. Using randomly-generated client ID " + conf.ClientID) + } r := csv.NewReader(strings.NewReader(conf.Command)) r.Comma = ' '