mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-27 14:54:14 +00:00
Compare commits
9 Commits
1d928bfe5a
...
a5a4fa4ac4
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a5a4fa4ac4 | ||
|
|
ddc5aa55df | ||
|
|
00bbfca878 | ||
|
|
884582a71e | ||
|
|
e3d503c746 | ||
|
|
af34a0881c | ||
|
|
c7cb9ab13e | ||
|
|
af18c7aea8 | ||
|
|
a6b4392fd7 |
@ -1,6 +1,6 @@
|
||||
ARG SIGNAL_CLI_VERSION=0.14.0
|
||||
ARG LIBSIGNAL_CLIENT_VERSION=0.87.4
|
||||
ARG SIGNAL_CLI_NATIVE_PACKAGE_VERSION=0.14.0+morph027+3
|
||||
ARG SIGNAL_CLI_NATIVE_PACKAGE_VERSION=0.14.0+morph027+5
|
||||
|
||||
ARG SWAG_VERSION=1.16.4
|
||||
ARG GRAALVM_VERSION=25.0.2
|
||||
@ -172,7 +172,7 @@ ENV SIGNAL_CLI_REST_API_PLUGIN_SHARED_OBJ_DIR=/usr/bin/
|
||||
|
||||
RUN dpkg-reconfigure debconf --frontend=noninteractive \
|
||||
&& apt-get update \
|
||||
&& apt-get install -y --no-install-recommends util-linux supervisor netcat-openbsd openjdk-25-jre curl locales \
|
||||
&& apt-get install -y --no-install-recommends util-linux supervisor openjdk-25-jre curl locales \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
COPY --from=buildcontainer /tmp/signal-cli-rest-api-src/signal-cli-rest-api /usr/bin/signal-cli-rest-api
|
||||
|
||||
@ -4,6 +4,8 @@ This can be done by putting the docker container into debug mode with the follow
|
||||
|
||||
```curl -X POST -H "Content-Type: application/json" -d '{"logging": {"level": "debug"}}' 'http://127.0.0.1:8080/v1/configuration'```
|
||||
|
||||
Alternatively, you can set the `LOG_LEVEL` environment variable.
|
||||
|
||||
Once the docker container is in debug mode, execute the REST API command you want to debug.
|
||||
|
||||
e.g Let's assume we are experiencing some problems with sending messages. So, let's send a Signal message with
|
||||
|
||||
@ -4,40 +4,57 @@ set -x
|
||||
set -e
|
||||
|
||||
[ -z "${SIGNAL_CLI_CONFIG_DIR}" ] && echo "SIGNAL_CLI_CONFIG_DIR environmental variable needs to be set! Aborting!" && exit 1;
|
||||
[ -z "${SIGNAL_CLI_UID}" ] && echo "SIGNAL_CLI_UID environmental variable needs to be set! Aborting!" && exit 1;
|
||||
[ -z "${SIGNAL_CLI_GID}" ] && echo "SIGNAL_CLI_GID environmental variable needs to be set! Aborting!" && exit 1;
|
||||
|
||||
usermod -u ${SIGNAL_CLI_UID} signal-api
|
||||
groupmod -o -g ${SIGNAL_CLI_GID} signal-api
|
||||
|
||||
# Fix permissions to ensure backward compatibility if SIGNAL_CLI_CHOWN_ON_STARTUP is not set to "false"
|
||||
if [ "$SIGNAL_CLI_CHOWN_ON_STARTUP" != "false" ]; then
|
||||
echo "Changing ownership of ${SIGNAL_CLI_CONFIG_DIR} to ${SIGNAL_CLI_UID}:${SIGNAL_CLI_GID}"
|
||||
chown ${SIGNAL_CLI_UID}:${SIGNAL_CLI_GID} -R ${SIGNAL_CLI_CONFIG_DIR}
|
||||
else
|
||||
echo "Skipping chown on startup since SIGNAL_CLI_CHOWN_ON_STARTUP is set to 'false'"
|
||||
# Check if we are already running as the target user and group
|
||||
RUNNING_AS_TARGET_USER=false
|
||||
if [ "$(id -u)" -eq "${SIGNAL_CLI_UID}" ] && [ "$(id -g)" -eq "${SIGNAL_CLI_GID}" ]; then
|
||||
RUNNING_AS_TARGET_USER=true
|
||||
fi
|
||||
|
||||
# Show warning on docker exec
|
||||
cat <<EOF >> /root/.bashrc
|
||||
if [ "$RUNNING_AS_TARGET_USER" = "true" ]; then
|
||||
echo "Already running as UID ${SIGNAL_CLI_UID} and GID ${SIGNAL_CLI_GID}. Skipping privileged operations."
|
||||
else
|
||||
echo "Adjusting user and group IDs to ${SIGNAL_CLI_UID}:${SIGNAL_CLI_GID}"
|
||||
usermod -u "${SIGNAL_CLI_UID}" signal-api
|
||||
groupmod -o -g "${SIGNAL_CLI_GID}" signal-api
|
||||
|
||||
# Fix permissions to ensure backward compatibility if SIGNAL_CLI_CHOWN_ON_STARTUP is not set to "false"
|
||||
if [ "$SIGNAL_CLI_CHOWN_ON_STARTUP" != "false" ]; then
|
||||
echo "Changing ownership of ${SIGNAL_CLI_CONFIG_DIR} to ${SIGNAL_CLI_UID}:${SIGNAL_CLI_GID}"
|
||||
chown "${SIGNAL_CLI_UID}":"${SIGNAL_CLI_GID}" -R "${SIGNAL_CLI_CONFIG_DIR}"
|
||||
else
|
||||
echo "Skipping chown on startup since SIGNAL_CLI_CHOWN_ON_STARTUP is set to 'false'"
|
||||
fi
|
||||
|
||||
# Show warning on docker exec
|
||||
cat <<EOF >> /root/.bashrc
|
||||
echo "WARNING: signal-cli-rest-api runs as signal-api (not as root!)"
|
||||
echo "Run 'su signal-api' before using signal-cli!"
|
||||
echo "If you want to use signal-cli directly, don't forget to specify the config directory. e.g: \"signal-cli --config ${SIGNAL_CLI_CONFIG_DIR}\""
|
||||
EOF
|
||||
|
||||
cap_prefix="-cap_"
|
||||
caps="$cap_prefix$(seq -s ",$cap_prefix" 0 $(cat /proc/sys/kernel/cap_last_cap))"
|
||||
fi
|
||||
|
||||
# TODO: check mode
|
||||
if [ "$MODE" = "json-rpc" ]
|
||||
then
|
||||
/usr/bin/jsonrpc2-helper
|
||||
if [ -n "$JAVA_OPTS" ] ; then
|
||||
echo "export JAVA_OPTS='$JAVA_OPTS'" >> /etc/default/supervisor
|
||||
fi
|
||||
service supervisor start
|
||||
supervisorctl start all
|
||||
if [ "$MODE" = "json-rpc" ]; then
|
||||
/usr/bin/jsonrpc2-helper
|
||||
if [ -n "$JAVA_OPTS" ]; then
|
||||
echo "export JAVA_OPTS='$JAVA_OPTS'" >> /etc/default/supervisor
|
||||
fi
|
||||
service supervisor start
|
||||
supervisorctl start all
|
||||
fi
|
||||
|
||||
export HOST_IP=$(hostname -I | awk '{print $1}')
|
||||
|
||||
# Start API as signal-api user
|
||||
exec setpriv --reuid=${SIGNAL_CLI_UID} --regid=${SIGNAL_CLI_GID} --init-groups --inh-caps=$caps signal-cli-rest-api -signal-cli-config=${SIGNAL_CLI_CONFIG_DIR}
|
||||
if [ "$RUNNING_AS_TARGET_USER" = "true" ]; then
|
||||
# Already running as the target user, start directly
|
||||
exec signal-cli-rest-api -signal-cli-config="${SIGNAL_CLI_CONFIG_DIR}"
|
||||
else
|
||||
# Use setpriv to switch to target user
|
||||
cap_prefix="-cap_"
|
||||
caps="$cap_prefix$(seq -s ",$cap_prefix" 0 $(cat /proc/sys/kernel/cap_last_cap))"
|
||||
exec setpriv --reuid="${SIGNAL_CLI_UID}" --regid="${SIGNAL_CLI_GID}" --init-groups --inh-caps="$caps" signal-cli-rest-api -signal-cli-config="${SIGNAL_CLI_CONFIG_DIR}"
|
||||
fi
|
||||
|
||||
@ -410,7 +410,7 @@ func (s *SignalClient) GetSignalCliMode() SignalCliMode {
|
||||
return s.signalCliMode
|
||||
}
|
||||
|
||||
func (s *SignalClient) Init() error {
|
||||
func (s *SignalClient) Init(maxRetries int) error {
|
||||
s.signalCliApiConfig = utils.NewSignalCliApiConfig()
|
||||
err := s.signalCliApiConfig.Load(s.signalCliApiConfigPath)
|
||||
if err != nil {
|
||||
@ -427,7 +427,7 @@ func (s *SignalClient) Init() error {
|
||||
tcpPortsNumberMapping := s.jsonRpc2ClientConfig.GetTcpPortsForNumbers()
|
||||
for number, tcpPort := range tcpPortsNumberMapping {
|
||||
s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number)
|
||||
err := s.jsonRpc2Clients[number].Dial("127.0.0.1:" + strconv.FormatInt(tcpPort, 10))
|
||||
err := s.jsonRpc2Clients[number].Dial("127.0.0.1:"+strconv.FormatInt(tcpPort, 10), maxRetries)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -2,14 +2,14 @@ package client
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
"net/http"
|
||||
"bytes"
|
||||
"strconv"
|
||||
|
||||
"github.com/bbernhard/signal-cli-rest-api/utils"
|
||||
uuid "github.com/gofrs/uuid"
|
||||
@ -60,11 +60,11 @@ type JsonRpc2Client struct {
|
||||
conn net.Conn
|
||||
receivedResponsesById map[string]chan JsonRpc2MessageResponse
|
||||
receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage
|
||||
lastTimeErrorMessageSent time.Time
|
||||
signalCliApiConfig *utils.SignalCliApiConfig
|
||||
number string
|
||||
receivedMessagesMutex sync.Mutex
|
||||
receivedResponsesMutex sync.Mutex
|
||||
address string
|
||||
}
|
||||
|
||||
func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client {
|
||||
@ -76,10 +76,24 @@ func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number stri
|
||||
}
|
||||
}
|
||||
|
||||
func (r *JsonRpc2Client) Dial(address string) error {
|
||||
func (r *JsonRpc2Client) Dial(address string, maxRetries int) error {
|
||||
var err error
|
||||
r.conn, err = net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
r.address = address
|
||||
connected := false
|
||||
for i := 0; i < maxRetries; i++ {
|
||||
r.conn, err = net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
log.Info("Waiting for signal-cli to start up in daemon mode...")
|
||||
time.Sleep(2 * time.Second)
|
||||
continue
|
||||
}
|
||||
|
||||
connected = true
|
||||
log.Info("Successfully connected to signal-cli in daemon mode")
|
||||
break
|
||||
}
|
||||
|
||||
if !connected {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -207,11 +221,14 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) {
|
||||
for {
|
||||
str, err := connbuf.ReadString('\n')
|
||||
if err != nil {
|
||||
elapsed := time.Since(r.lastTimeErrorMessageSent)
|
||||
if (elapsed) > time.Duration(5*time.Minute) { //avoid spamming the log file and only log the message at max every 5 minutes
|
||||
log.Error("Couldn't read data for number ", number, ": ", err.Error(), ". Is the number properly registered?")
|
||||
r.lastTimeErrorMessageSent = time.Now()
|
||||
log.Error("Lost connection to signal-cli...attempting to reconnect (", err.Error(), ")")
|
||||
r.conn.Close()
|
||||
err = r.Dial(r.address, 15)
|
||||
if err != nil {
|
||||
log.Fatal("Unable to reconnect to signal-cli: ", err.Error(), "...aborting")
|
||||
}
|
||||
connbuf = bufio.NewReader(r.conn)
|
||||
log.Info("Successfully reconnected to signal-cli")
|
||||
continue
|
||||
}
|
||||
log.Debug("json-rpc received data: ", str)
|
||||
@ -248,7 +265,7 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.Error("Received unparsable message: ", str)
|
||||
log.Warn("Received unparsable message: ", str)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -163,7 +163,7 @@ func main() {
|
||||
jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.yml"
|
||||
signalCliApiConfigPath := *signalCliConfig + "/api-config.yml"
|
||||
signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath, signalCliApiConfigPath, webhookUrl)
|
||||
err = signalClient.Init()
|
||||
err = signalClient.Init(15)
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't init Signal Client: ", err.Error())
|
||||
}
|
||||
|
||||
@ -14,7 +14,7 @@ import (
|
||||
const supervisorctlConfigTemplate = `
|
||||
[program:%s]
|
||||
process_name=%s
|
||||
command=bash -c "nc -l -p %d <%s | signal-cli --output=json --config %s%s jsonRpc%s%s >%s"
|
||||
command=signal-cli --output=json --config %s%s daemon %s%s --tcp 127.0.0.1:%d
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startretries=10
|
||||
@ -96,7 +96,7 @@ func main() {
|
||||
supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf"
|
||||
|
||||
supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName,
|
||||
tcpPort, fifoPathname, signalCliConfigDir, trustNewIdentities, signalCliIgnoreAttachments, signalCliIgnoreStories, fifoPathname,
|
||||
signalCliConfigDir, trustNewIdentities, signalCliIgnoreAttachments, signalCliIgnoreStories, tcpPort,
|
||||
supervisorctlProgramName, supervisorctlProgramName)
|
||||
|
||||
err = ioutil.WriteFile(supervisorctlConfigFilename, []byte(supervisorctlConfig), 0644)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user