Skip to content

Commit

Permalink
Add random default for client ID
Browse files Browse the repository at this point in the history
  • Loading branch information
albertnis committed Apr 26, 2023
1 parent f2e3985 commit b4752b1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 35 deletions.
66 changes: 33 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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
Expand All @@ -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
```
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"crypto/rand"
"encoding/csv"
"flag"
"fmt"
Expand All @@ -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()
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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 = ' '
Expand Down

0 comments on commit b4752b1

Please sign in to comment.