mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-25 14:34:22 +00:00
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.
This commit is contained in:
parent
ed3626fa77
commit
af18c7aea8
@ -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,12 @@ 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
|
||||
//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 +77,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
|
||||
}
|
||||
|
||||
|
||||
@ -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