mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-25 14:34:22 +00:00
changes signal-json-rpc run script
* switched to signal-cli daemon mode - this way we don't need to pipe signal-cli's output to netcat. * removed fifo pathname from the json-rpc config (not needed anymore)
This commit is contained in:
parent
1543997e02
commit
bb8c0efcac
@ -1,67 +1,10 @@
|
||||
#!/command/with-contenv sh
|
||||
# File: /etc/s6-overlay/s6-rc.d/signal-json-rpc/run
|
||||
|
||||
PIPE=/tmp/sigsocket1
|
||||
PORT=6001
|
||||
|
||||
if [ "$MODE" != "json-rpc" ]; then
|
||||
echo "Running as mode: $MODE - skipping json-rpc setup"
|
||||
sleep infinity # do nothing, but keep service running
|
||||
exit 0
|
||||
fi
|
||||
|
||||
## general parameters for signal-cli
|
||||
# set SIGNAL_CLI_CONFIG_DIR if not set
|
||||
if [ -z "$SIGNAL_CLI_CONFIG_DIR" ]; then
|
||||
SIGNAL_CLI_CONFIG_DIR="/home/.local/share/signal-cli/"
|
||||
fi
|
||||
|
||||
jsonRpcConfig="${SIGNAL_CLI_CONFIG_DIR%/}/jsonrpc2.yml"
|
||||
|
||||
# here file to write config
|
||||
cat > "$jsonRpcConfig" <<EOL
|
||||
config:
|
||||
<multi-account>:
|
||||
tcp_port: $PORT
|
||||
fifo_pathname: $PIPE
|
||||
EOL
|
||||
|
||||
# Trust Identities
|
||||
trustNewIdentitiesEnv="${JSON_RPC_TRUST_NEW_IDENTITIES:-}"
|
||||
trustNewIdentities=""
|
||||
|
||||
if [ "$trustNewIdentitiesEnv" = "on-first-use" ]; then
|
||||
trustNewIdentities=" --trust-new-identities on-first-use"
|
||||
elif [ "$trustNewIdentitiesEnv" = "always" ]; then
|
||||
trustNewIdentities=" --trust-new-identities always"
|
||||
elif [ "$trustNewIdentitiesEnv" = "never" ]; then
|
||||
trustNewIdentities=" --trust-new-identities never"
|
||||
elif [ -n "$trustNewIdentitiesEnv" ]; then
|
||||
# This mirrors your log.Fatal check
|
||||
echo "Invalid JSON_RPC_TRUST_NEW_IDENTITIES environment variable set!" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
## parameters for jsonrpc mode
|
||||
# Attachments
|
||||
ignoreAttachments="${JSON_RPC_IGNORE_ATTACHMENTS:-}"
|
||||
signalCliIgnoreAttachments=""
|
||||
if [ "$ignoreAttachments" = "true" ]; then
|
||||
signalCliIgnoreAttachments=" --ignore-attachments"
|
||||
fi
|
||||
|
||||
# Stories
|
||||
ignoreStories="${JSON_RPC_IGNORE_STORIES:-}"
|
||||
signalCliIgnoreStories=""
|
||||
if [ "$ignoreStories" = "true" ]; then
|
||||
signalCliIgnoreStories=" --ignore-stories"
|
||||
fi
|
||||
|
||||
# Load the pipe
|
||||
[ -p "$PIPE" ] || mkfifo "$PIPE"
|
||||
|
||||
# Ensure permissions are correct for non-root
|
||||
chmod 600 "$PIPE"
|
||||
|
||||
# Launch the circular communication
|
||||
exec sh -c "nc -l -p "${PORT}" < $PIPE | signal-cli --output=json --config "${SIGNAL_CLI_CONFIG_DIR}" "${trustNewIdentities}" jsonRpc ${signalCliIgnoreAttachments} ${signalCliIgnoreStories} > $PIPE"
|
||||
exec jsonrpc2-helper
|
||||
|
||||
@ -1,33 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/bbernhard/signal-cli-rest-api/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const supervisorctlConfigTemplate = `
|
||||
[program:%s]
|
||||
process_name=%s
|
||||
command=signal-cli --output=json --config %s%s daemon %s%s --tcp 127.0.0.1:%d
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startretries=10
|
||||
user=signal-api
|
||||
directory=/usr/bin/
|
||||
redirect_stderr=true
|
||||
stdout_logfile=/var/log/%s/out.log
|
||||
stderr_logfile=/var/log/%s/err.log
|
||||
stdout_logfile_maxbytes=50MB
|
||||
stdout_logfile_backups=10
|
||||
numprocs=1
|
||||
`
|
||||
|
||||
func main() {
|
||||
signalCliConfigDir := "/home/.local/share/signal-cli/"
|
||||
signalCliConfigDirEnv := utils.GetEnv("SIGNAL_CLI_CONFIG_DIR", "")
|
||||
@ -41,72 +23,47 @@ func main() {
|
||||
jsonRpc2ClientConfig := utils.NewJsonRpc2ClientConfig()
|
||||
|
||||
var tcpPort int64 = 6001
|
||||
fifoPathname := "/tmp/sigsocket1"
|
||||
jsonRpc2ClientConfig.AddEntry(utils.MULTI_ACCOUNT_NUMBER, utils.JsonRpc2ClientConfigEntry{TcpPort: tcpPort})
|
||||
|
||||
jsonRpc2ClientConfig.AddEntry(utils.MULTI_ACCOUNT_NUMBER, utils.JsonRpc2ClientConfigEntry{TcpPort: tcpPort, FifoPathname: fifoPathname})
|
||||
args := []string{"--output=json", "--config", signalCliConfigDir}
|
||||
|
||||
os.Remove(fifoPathname) //remove any existing named pipe
|
||||
|
||||
_, err := exec.Command("mkfifo", fifoPathname).Output()
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't create fifo with name ", fifoPathname, ": ", err.Error())
|
||||
}
|
||||
|
||||
uid := utils.GetEnv("SIGNAL_CLI_UID", "1000")
|
||||
gid := utils.GetEnv("SIGNAL_CLI_GID", "1000")
|
||||
_, err = exec.Command("chown", uid+":"+gid, fifoPathname).Output()
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't change permissions of fifo with name ", fifoPathname, ": ", err.Error())
|
||||
}
|
||||
|
||||
signalCliIgnoreAttachments := ""
|
||||
ignoreAttachments := utils.GetEnv("JSON_RPC_IGNORE_ATTACHMENTS", "")
|
||||
if ignoreAttachments == "true" {
|
||||
signalCliIgnoreAttachments = " --ignore-attachments"
|
||||
}
|
||||
|
||||
signalCliIgnoreStories := ""
|
||||
ignoreStories := utils.GetEnv("JSON_RPC_IGNORE_STORIES", "")
|
||||
if ignoreStories == "true" {
|
||||
signalCliIgnoreStories = " --ignore-stories"
|
||||
}
|
||||
|
||||
supervisorctlProgramName := "signal-cli-json-rpc-1"
|
||||
supervisorctlLogFolder := "/var/log/" + supervisorctlProgramName
|
||||
_, err = exec.Command("mkdir", "-p", supervisorctlLogFolder).Output()
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't create log folder ", supervisorctlLogFolder, ": ", err.Error())
|
||||
}
|
||||
|
||||
trustNewIdentities := ""
|
||||
trustNewIdentitiesEnv := utils.GetEnv("JSON_RPC_TRUST_NEW_IDENTITIES", "")
|
||||
if trustNewIdentitiesEnv == "on-first-use" {
|
||||
trustNewIdentities = " --trust-new-identities on-first-use"
|
||||
args = append(args, []string{"--trust-new-identities", "on-first-use"}...)
|
||||
} else if trustNewIdentitiesEnv == "always" {
|
||||
trustNewIdentities = " --trust-new-identities always"
|
||||
args = append(args, []string{"--trust-new-identities", "always"}...)
|
||||
} else if trustNewIdentitiesEnv == "never" {
|
||||
trustNewIdentities = " --trust-new-identities never"
|
||||
args = append(args, []string{"--trust-new-identities", "never"}...)
|
||||
} else if trustNewIdentitiesEnv != "" {
|
||||
log.Fatal("Invalid JSON_RPC_TRUST_NEW_IDENTITIES environment variable set!")
|
||||
}
|
||||
|
||||
log.Info("Updated jsonrpc2.yml")
|
||||
args = append(args, "daemon")
|
||||
|
||||
//write supervisorctl config
|
||||
supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf"
|
||||
|
||||
supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName,
|
||||
signalCliConfigDir, trustNewIdentities, signalCliIgnoreAttachments, signalCliIgnoreStories, tcpPort,
|
||||
supervisorctlProgramName, supervisorctlProgramName)
|
||||
|
||||
err = ioutil.WriteFile(supervisorctlConfigFilename, []byte(supervisorctlConfig), 0644)
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't write ", supervisorctlConfigFilename, ": ", err.Error())
|
||||
ignoreAttachments := utils.GetEnv("JSON_RPC_IGNORE_ATTACHMENTS", "")
|
||||
if ignoreAttachments == "true" {
|
||||
args = append(args, " --ignore-attachments")
|
||||
}
|
||||
|
||||
ignoreStories := utils.GetEnv("JSON_RPC_IGNORE_STORIES", "")
|
||||
if ignoreStories == "true" {
|
||||
args = append(args, " --ignore-stories")
|
||||
}
|
||||
|
||||
args = append(args, []string{"--tcp", "127.0.0.1:" + strconv.FormatInt(tcpPort, 10)}...)
|
||||
|
||||
// write jsonrpc.yml config file
|
||||
err = jsonRpc2ClientConfig.Persist(signalCliConfigDir + "jsonrpc2.yml")
|
||||
err := jsonRpc2ClientConfig.Persist(signalCliConfigDir + "jsonrpc2.yml")
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't persist jsonrpc2.yaml: ", err.Error())
|
||||
}
|
||||
|
||||
log.Info("Updated jsonrpc2.yml")
|
||||
|
||||
env := os.Environ()
|
||||
|
||||
err = syscall.Exec("/usr/bin/signal-cli", args, env)
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't start signal-cli in json-rpc mode: ", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,15 +2,15 @@ package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gopkg.in/yaml.v2"
|
||||
"io/ioutil"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
const MULTI_ACCOUNT_NUMBER string = "<multi-account>"
|
||||
|
||||
type JsonRpc2ClientConfigEntry struct {
|
||||
TcpPort int64 `yaml:"tcp_port"`
|
||||
FifoPathname string `yaml:"fifo_pathname"`
|
||||
TcpPort int64 `yaml:"tcp_port"`
|
||||
}
|
||||
|
||||
type JsonRpc2ClientConfigEntries struct {
|
||||
@ -47,14 +47,6 @@ func (c *JsonRpc2ClientConfig) GetTcpPortForNumber(number string) (int64, error)
|
||||
return 0, errors.New("Number " + number + " not found in local map")
|
||||
}
|
||||
|
||||
func (c *JsonRpc2ClientConfig) GetFifoPathnameForNumber(number string) (string, error) {
|
||||
if val, ok := c.config.Entries[number]; ok {
|
||||
return val.FifoPathname, nil
|
||||
}
|
||||
|
||||
return "", errors.New("Number " + number + " not found in local map")
|
||||
}
|
||||
|
||||
func (c *JsonRpc2ClientConfig) GetTcpPortsForNumbers() map[string]int64 {
|
||||
mapping := make(map[string]int64)
|
||||
for number, val := range c.config.Entries {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user