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
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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,12 @@ 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
|
//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 +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
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user