Compare commits

...

3 Commits

Author SHA1 Message Date
Bernhard B
af34a0881c fixed small bug in json-rpc reconnect mechanism
* after we successfully reconnected, wait for new data
2026-03-05 20:50:42 +01:00
Bernhard B
c7cb9ab13e improved json-rpc reconnection logic
* when the connection the signal-cli daemon is lost in json-rpc mode,
  the open connection will be closed and a new connection attempt will
  be made. If after 15 connection attempts we are unable to connect to
  the signal-cli daemon, we give up and abort.
2026-03-05 20:43:22 +01:00
Bernhard B
af18c7aea8 switched to signal-cli daemon mode
* this gets rid off the ugly netcat workaround and should improve
  the general stability of the connection to the signal-cli binary
  in json-rpc mode.
2026-03-05 20:41:17 +01:00
4 changed files with 34 additions and 17 deletions

View File

@ -410,7 +410,7 @@ func (s *SignalClient) GetSignalCliMode() SignalCliMode {
return s.signalCliMode return s.signalCliMode
} }
func (s *SignalClient) Init() error { func (s *SignalClient) Init(maxRetries int) error {
s.signalCliApiConfig = utils.NewSignalCliApiConfig() s.signalCliApiConfig = utils.NewSignalCliApiConfig()
err := s.signalCliApiConfig.Load(s.signalCliApiConfigPath) err := s.signalCliApiConfig.Load(s.signalCliApiConfigPath)
if err != nil { if err != nil {
@ -427,7 +427,7 @@ func (s *SignalClient) Init() error {
tcpPortsNumberMapping := s.jsonRpc2ClientConfig.GetTcpPortsForNumbers() tcpPortsNumberMapping := s.jsonRpc2ClientConfig.GetTcpPortsForNumbers()
for number, tcpPort := range tcpPortsNumberMapping { for number, tcpPort := range tcpPortsNumberMapping {
s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number) 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 { if err != nil {
return err return err
} }

View File

@ -2,14 +2,14 @@ package client
import ( import (
"bufio" "bufio"
"bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"net" "net"
"net/http"
"strconv"
"sync" "sync"
"time" "time"
"net/http"
"bytes"
"strconv"
"github.com/bbernhard/signal-cli-rest-api/utils" "github.com/bbernhard/signal-cli-rest-api/utils"
uuid "github.com/gofrs/uuid" uuid "github.com/gofrs/uuid"
@ -60,11 +60,11 @@ type JsonRpc2Client struct {
conn net.Conn conn net.Conn
receivedResponsesById map[string]chan JsonRpc2MessageResponse receivedResponsesById map[string]chan JsonRpc2MessageResponse
receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage
lastTimeErrorMessageSent time.Time
signalCliApiConfig *utils.SignalCliApiConfig signalCliApiConfig *utils.SignalCliApiConfig
number string number string
receivedMessagesMutex sync.Mutex receivedMessagesMutex sync.Mutex
receivedResponsesMutex sync.Mutex receivedResponsesMutex sync.Mutex
address string
} }
func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client { 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 var err error
r.conn, err = net.Dial("tcp", address) r.address = address
if err != nil { 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 return err
} }
@ -207,11 +221,14 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) {
for { for {
str, err := connbuf.ReadString('\n') str, err := connbuf.ReadString('\n')
if err != nil { if err != nil {
elapsed := time.Since(r.lastTimeErrorMessageSent) log.Error("Lost connection to signal-cli...attempting to reconnect (", err.Error(), ")")
if (elapsed) > time.Duration(5*time.Minute) { //avoid spamming the log file and only log the message at max every 5 minutes r.conn.Close()
log.Error("Couldn't read data for number ", number, ": ", err.Error(), ". Is the number properly registered?") err = r.Dial(r.address, 15)
r.lastTimeErrorMessageSent = time.Now() 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 continue
} }
log.Debug("json-rpc received data: ", str) log.Debug("json-rpc received data: ", str)
@ -248,7 +265,7 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) {
} }
} }
} else { } else {
log.Error("Received unparsable message: ", str) log.Warn("Received unparsable message: ", str)
} }
} }
} }

View File

@ -163,7 +163,7 @@ func main() {
jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.yml" jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.yml"
signalCliApiConfigPath := *signalCliConfig + "/api-config.yml" signalCliApiConfigPath := *signalCliConfig + "/api-config.yml"
signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath, signalCliApiConfigPath, webhookUrl) signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath, signalCliApiConfigPath, webhookUrl)
err = signalClient.Init() err = signalClient.Init(15)
if err != nil { if err != nil {
log.Fatal("Couldn't init Signal Client: ", err.Error()) log.Fatal("Couldn't init Signal Client: ", err.Error())
} }

View File

@ -14,7 +14,7 @@ import (
const supervisorctlConfigTemplate = ` const supervisorctlConfigTemplate = `
[program:%s] [program:%s]
process_name=%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 autostart=true
autorestart=true autorestart=true
startretries=10 startretries=10
@ -96,7 +96,7 @@ func main() {
supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf" supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf"
supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName, supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName,
tcpPort, fifoPathname, signalCliConfigDir, trustNewIdentities, signalCliIgnoreAttachments, signalCliIgnoreStories, fifoPathname, signalCliConfigDir, trustNewIdentities, signalCliIgnoreAttachments, signalCliIgnoreStories, tcpPort,
supervisorctlProgramName, supervisorctlProgramName) supervisorctlProgramName, supervisorctlProgramName)
err = ioutil.WriteFile(supervisorctlConfigFilename, []byte(supervisorctlConfig), 0644) err = ioutil.WriteFile(supervisorctlConfigFilename, []byte(supervisorctlConfig), 0644)