mirror of
https://github.com/aljazceru/signal-cli-rest-api.git
synced 2025-12-19 15:44:28 +01:00
Merge pull request #434 from bbernhard/multimaster_mode
Multimaster mode
This commit is contained in:
@@ -385,7 +385,7 @@ func (a *Api) SendV2(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (a *Api) handleSignalReceive(ws *websocket.Conn, number string, stop chan struct{}) {
|
||||
receiveChannel, err := a.signalClient.GetReceiveChannel(number)
|
||||
receiveChannel, err := a.signalClient.GetReceiveChannel()
|
||||
if err != nil {
|
||||
log.Error("Couldn't get receive channel: ", err.Error())
|
||||
return
|
||||
@@ -405,6 +405,17 @@ func (a *Api) handleSignalReceive(ws *websocket.Conn, number string, stop chan s
|
||||
|
||||
if err == nil {
|
||||
if data != "" {
|
||||
type Response struct {
|
||||
Account string `json:"account"`
|
||||
}
|
||||
var response Response
|
||||
err = json.Unmarshal([]byte(data), &response)
|
||||
if err != nil {
|
||||
log.Error("Couldn't parse message ", data, ":", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
if response.Account == number {
|
||||
err = ws.WriteMessage(websocket.TextMessage, []byte(data))
|
||||
if err != nil {
|
||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
|
||||
@@ -413,6 +424,7 @@ func (a *Api) handleSignalReceive(ws *websocket.Conn, number string, stop chan s
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
errorMsg := Error{Msg: err.Error()}
|
||||
errorMsgBytes, err := json.Marshal(errorMsg)
|
||||
|
||||
@@ -377,7 +377,7 @@ func (s *SignalClient) send(number string, message string,
|
||||
}
|
||||
|
||||
if s.signalCliMode == JsonRpc {
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -431,7 +431,7 @@ func (s *SignalClient) send(number string, message string,
|
||||
request.TextStyles = signalCliTextFormatStrings
|
||||
}
|
||||
|
||||
rawData, err := jsonRpc2Client.getRaw("send", request)
|
||||
rawData, err := jsonRpc2Client.getRaw("send", &number, request)
|
||||
if err != nil {
|
||||
cleanupAttachmentEntries(attachmentEntries)
|
||||
return nil, err
|
||||
@@ -527,8 +527,28 @@ func (s *SignalClient) About() About {
|
||||
|
||||
func (s *SignalClient) RegisterNumber(number string, useVoice bool, captcha string) error {
|
||||
if s.signalCliMode == JsonRpc {
|
||||
return errors.New(endpointNotSupportedInJsonRpcMode)
|
||||
type Request struct {
|
||||
UseVoice bool `json:"voice,omitempty"`
|
||||
Captcha string `json:"captcha,omitempty"`
|
||||
Account string `json:"account,omitempty"`
|
||||
}
|
||||
request := Request{Account: number}
|
||||
|
||||
if useVoice {
|
||||
request.UseVoice = useVoice
|
||||
}
|
||||
|
||||
if captcha != "" {
|
||||
request.Captcha = captcha
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("register", nil, request)
|
||||
return err
|
||||
} else {
|
||||
command := []string{"--config", s.signalCliConfig, "-a", number, "register"}
|
||||
|
||||
if useVoice {
|
||||
@@ -541,6 +561,7 @@ func (s *SignalClient) RegisterNumber(number string, useVoice bool, captcha stri
|
||||
|
||||
_, err := s.cliClient.Execute(true, command, "")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignalClient) UnregisterNumber(number string, deleteAccount bool, deleteLocalData bool) error {
|
||||
@@ -570,9 +591,24 @@ func (s *SignalClient) UnregisterNumber(number string, deleteAccount bool, delet
|
||||
|
||||
func (s *SignalClient) VerifyRegisteredNumber(number string, token string, pin string) error {
|
||||
if s.signalCliMode == JsonRpc {
|
||||
return errors.New(endpointNotSupportedInJsonRpcMode)
|
||||
type Request struct {
|
||||
VerificationCode string `json:"verificationCode,omitempty"`
|
||||
Account string `json:"account,omitempty"`
|
||||
Pin string `json:"pin,omitempty"`
|
||||
}
|
||||
request := Request{Account: number, VerificationCode: token}
|
||||
|
||||
if pin != "" {
|
||||
request.Pin = pin
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("verify", nil, request)
|
||||
return err
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "verify", token}
|
||||
if pin != "" {
|
||||
cmd = append(cmd, "--pin")
|
||||
@@ -581,6 +617,7 @@ func (s *SignalClient) VerifyRegisteredNumber(number string, token string, pin s
|
||||
|
||||
_, err := s.cliClient.Execute(true, cmd, "")
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignalClient) SendV1(number string, message string, recipients []string, base64Attachments []string, isGroup bool) (*SendResponse, error) {
|
||||
@@ -588,8 +625,8 @@ func (s *SignalClient) SendV1(number string, message string, recipients []string
|
||||
return timestamp, err
|
||||
}
|
||||
|
||||
func (s *SignalClient) getJsonRpc2Client(number string) (*JsonRpc2Client, error) {
|
||||
if val, ok := s.jsonRpc2Clients[number]; ok {
|
||||
func (s *SignalClient) getJsonRpc2Client() (*JsonRpc2Client, error) {
|
||||
if val, ok := s.jsonRpc2Clients[utils.MULTI_ACCOUNT_NUMBER]; ok {
|
||||
return val, nil
|
||||
}
|
||||
return nil, errors.New("Number not registered with JSON-RPC")
|
||||
@@ -692,8 +729,8 @@ func (s *SignalClient) Receive(number string, timeout int64, ignoreAttachments b
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignalClient) GetReceiveChannel(number string) (chan JsonRpc2ReceivedMessage, error) {
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
func (s *SignalClient) GetReceiveChannel() (chan JsonRpc2ReceivedMessage, error) {
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -729,11 +766,11 @@ func (s *SignalClient) CreateGroup(number string, name string, members []string,
|
||||
request.AddMembersPermissions = addMembersPermission.String()
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rawData, err := jsonRpc2Client.getRaw("updateGroup", request)
|
||||
rawData, err := jsonRpc2Client.getRaw("updateGroup", &number, request)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@@ -817,11 +854,11 @@ func (s *SignalClient) updateGroupMembers(number string, groupId string, members
|
||||
request.RemoveMembers = append(request.RemoveMembers, members...)
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("updateGroup", request)
|
||||
_, err = jsonRpc2Client.getRaw("updateGroup", &number, request)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "updateGroup", "-g", internalGroupId}
|
||||
|
||||
@@ -880,11 +917,11 @@ func (s *SignalClient) updateGroupAdmins(number string, groupId string, admins [
|
||||
request.RemoveAdmins = append(request.RemoveAdmins, admins...)
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("updateGroup", request)
|
||||
_, err = jsonRpc2Client.getRaw("updateGroup", &number, request)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "updateGroup", "-g", internalGroupId}
|
||||
|
||||
@@ -916,11 +953,11 @@ func (s *SignalClient) GetGroups(number string) ([]GroupEntry, error) {
|
||||
var rawData string
|
||||
|
||||
if s.signalCliMode == JsonRpc {
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return groupEntries, err
|
||||
}
|
||||
rawData, err = jsonRpc2Client.getRaw("listGroups", nil)
|
||||
rawData, err = jsonRpc2Client.getRaw("listGroups", &number, nil)
|
||||
if err != nil {
|
||||
return groupEntries, err
|
||||
}
|
||||
@@ -999,11 +1036,11 @@ func (s *SignalClient) DeleteGroup(number string, groupId string) error {
|
||||
}
|
||||
request := Request{GroupId: groupId}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("quitGroup", request)
|
||||
_, err = jsonRpc2Client.getRaw("quitGroup", &number, request)
|
||||
return err
|
||||
} else {
|
||||
ret, err := s.cliClient.Execute(true, []string{"--config", s.signalCliConfig, "-a", number, "quitGroup", "-g", string(groupId)}, "")
|
||||
@@ -1139,11 +1176,11 @@ func (s *SignalClient) UpdateProfile(number string, profileName string, base64Av
|
||||
request.Avatar = avatarTmpPath
|
||||
request.RemoveAvatar = false
|
||||
}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("updateProfile", request)
|
||||
_, err = jsonRpc2Client.getRaw("updateProfile", &number, request)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "updateProfile", "--given-name", profileName}
|
||||
if base64Avatar == "" {
|
||||
@@ -1162,11 +1199,11 @@ func (s *SignalClient) UpdateProfile(number string, profileName string, base64Av
|
||||
func (s *SignalClient) ListIdentities(number string) (*[]IdentityEntry, error) {
|
||||
identityEntries := []IdentityEntry{}
|
||||
if s.signalCliMode == JsonRpc {
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rawData, err := jsonRpc2Client.getRaw("listIdentities", nil)
|
||||
rawData, err := jsonRpc2Client.getRaw("listIdentities", &number, nil)
|
||||
signalCliIdentityEntries := []SignalCliIdentityEntry{}
|
||||
err = json.Unmarshal([]byte(rawData), &signalCliIdentityEntries)
|
||||
if err != nil {
|
||||
@@ -1224,11 +1261,11 @@ func (s *SignalClient) TrustIdentity(number string, numberToTrust string, verifi
|
||||
request.TrustAllKnownKeys = *trustAllKnownKeys
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("trust", request)
|
||||
_, err = jsonRpc2Client.getRaw("trust", &number, request)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "trust", numberToTrust}
|
||||
|
||||
@@ -1252,11 +1289,11 @@ func (s *SignalClient) BlockGroup(number string, groupId string) error {
|
||||
GroupId string `json:"groupId"`
|
||||
}
|
||||
request := Request{GroupId: groupId}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("block", request)
|
||||
_, err = jsonRpc2Client.getRaw("block", &number, request)
|
||||
} else {
|
||||
_, err = s.cliClient.Execute(true, []string{"--config", s.signalCliConfig, "-a", number, "block", "-g", groupId}, "")
|
||||
}
|
||||
@@ -1270,11 +1307,11 @@ func (s *SignalClient) JoinGroup(number string, groupId string) error {
|
||||
GroupId string `json:"groupId"`
|
||||
}
|
||||
request := Request{GroupId: groupId}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("updateGroup", request)
|
||||
_, err = jsonRpc2Client.getRaw("updateGroup", &number, request)
|
||||
} else {
|
||||
_, err = s.cliClient.Execute(true, []string{"--config", s.signalCliConfig, "-a", number, "updateGroup", "-g", groupId}, "")
|
||||
}
|
||||
@@ -1288,11 +1325,11 @@ func (s *SignalClient) QuitGroup(number string, groupId string) error {
|
||||
GroupId string `json:"groupId"`
|
||||
}
|
||||
request := Request{GroupId: groupId}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("quitGroup", request)
|
||||
_, err = jsonRpc2Client.getRaw("quitGroup", &number, request)
|
||||
} else {
|
||||
_, err = s.cliClient.Execute(true, []string{"--config", s.signalCliConfig, "-a", number, "quitGroup", "-g", groupId}, "")
|
||||
}
|
||||
@@ -1354,11 +1391,11 @@ func (s *SignalClient) UpdateGroup(number string, groupId string, base64Avatar *
|
||||
request.Name = groupName
|
||||
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("updateGroup", request)
|
||||
_, err = jsonRpc2Client.getRaw("updateGroup", &number, request)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "updateGroup", "-g", groupId}
|
||||
if base64Avatar != nil {
|
||||
@@ -1420,11 +1457,11 @@ func (s *SignalClient) SendReaction(number string, recipient string, emoji strin
|
||||
if remove {
|
||||
request.Remove = remove
|
||||
}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("sendReaction", request)
|
||||
_, err = jsonRpc2Client.getRaw("sendReaction", &number, request)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -1470,11 +1507,11 @@ func (s *SignalClient) SendStartTyping(number string, recipient string) error {
|
||||
request.GroupId = recp
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("sendTyping", request)
|
||||
_, err = jsonRpc2Client.getRaw("sendTyping", &number, request)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "sendTyping"}
|
||||
if !isGroup {
|
||||
@@ -1513,11 +1550,11 @@ func (s *SignalClient) SendStopTyping(number string, recipient string) error {
|
||||
request.GroupId = recp
|
||||
}
|
||||
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("sendTyping", request)
|
||||
_, err = jsonRpc2Client.getRaw("sendTyping", &number, request)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "sendTyping", "--stop"}
|
||||
if !isGroup {
|
||||
@@ -1547,7 +1584,7 @@ func (s *SignalClient) SearchForNumbers(number string, numbers []string) ([]Sear
|
||||
return searchResultEntries, errors.New("No JsonRpc2Client registered!")
|
||||
}
|
||||
for _, jsonRpc2Client := range jsonRpc2Clients {
|
||||
rawData, err = jsonRpc2Client.getRaw("getUserStatus", request)
|
||||
rawData, err = jsonRpc2Client.getRaw("getUserStatus", &number, request)
|
||||
if err == nil { //getUserStatus doesn't need an account to work, so try all the registered acounts and stop until we succeed
|
||||
break
|
||||
}
|
||||
@@ -1592,11 +1629,11 @@ func (s *SignalClient) SearchForNumbers(number string, numbers []string) ([]Sear
|
||||
func (s* SignalClient) SendContacts(number string) error {
|
||||
var err error
|
||||
if s.signalCliMode == JsonRpc {
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("sendContacts", nil)
|
||||
_, err = jsonRpc2Client.getRaw("sendContacts", &number, nil)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "sendContacts"}
|
||||
_, err = s.cliClient.Execute(true, cmd, "")
|
||||
@@ -1619,11 +1656,11 @@ func (s *SignalClient) UpdateContact(number string, recipient string, name *stri
|
||||
if expirationInSeconds != nil {
|
||||
request.Expiration = *expirationInSeconds
|
||||
}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("updateContact", request)
|
||||
_, err = jsonRpc2Client.getRaw("updateContact", &number, request)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "updateContact", recipient}
|
||||
if name != nil {
|
||||
@@ -1644,11 +1681,11 @@ func (s *SignalClient) AddDevice(number string, uri string) error {
|
||||
Uri string `json:"uri"`
|
||||
}
|
||||
request := Request{Uri: uri}
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client(number)
|
||||
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = jsonRpc2Client.getRaw("addDevice", request)
|
||||
_, err = jsonRpc2Client.getRaw("addDevice", &number, request)
|
||||
} else {
|
||||
cmd := []string{"--config", s.signalCliConfig, "-a", number, "addDevice", "--uri", uri}
|
||||
_, err = s.cliClient.Execute(true, cmd, "")
|
||||
|
||||
@@ -59,7 +59,7 @@ func (r *JsonRpc2Client) Dial(address string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *JsonRpc2Client) getRaw(command string, args interface{}) (string, error) {
|
||||
func (r *JsonRpc2Client) getRaw(command string, account *string, args interface{}) (string, error) {
|
||||
type Request struct {
|
||||
JsonRpc string `json:"jsonrpc"`
|
||||
Method string `json:"method"`
|
||||
@@ -99,6 +99,13 @@ func (r *JsonRpc2Client) getRaw(command string, args interface{}) (string, error
|
||||
}
|
||||
}
|
||||
|
||||
if account != nil {
|
||||
fullCommandBytes, err = sjson.SetBytes(fullCommandBytes, "params.account", account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
|
||||
log.Debug("full command: ", string(fullCommandBytes))
|
||||
|
||||
_, err = r.conn.Write([]byte(string(fullCommandBytes) + "\n"))
|
||||
|
||||
@@ -1,16 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/bbernhard/signal-cli-rest-api/utils"
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -18,7 +14,7 @@ const supervisorctlConfigTemplate = `
|
||||
[program:%s]
|
||||
environment=JAVA_HOME=/opt/java/openjdk
|
||||
process_name=%s
|
||||
command=bash -c "nc -l -p %d <%s | signal-cli --output=json -u %s --config %s jsonRpc >%s"
|
||||
command=bash -c "nc -l -p %d <%s | signal-cli --output=json --config %s jsonRpc >%s"
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startretries=10
|
||||
@@ -32,37 +28,6 @@ stdout_logfile_backups=10
|
||||
numprocs=1
|
||||
`
|
||||
|
||||
func isSignalCliLinkedNumberConfigFile(filename string) (bool, error) {
|
||||
fileExtension := filepath.Ext(filename)
|
||||
if fileExtension != "" {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
mimetype, err := mimetype.DetectFile(filename)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if mimetype.String() == "application/json" {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func getUsernameFromLinkedNumberConfigFile(filename string) (string, error) {
|
||||
type LinkedNumberConfigFile struct {
|
||||
Username string `json:"username"`
|
||||
}
|
||||
bytes, err := ioutil.ReadFile(filename)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var linkedNumberConfigFile LinkedNumberConfigFile
|
||||
err = json.Unmarshal(bytes, &linkedNumberConfigFile)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return linkedNumberConfigFile.Username, nil
|
||||
}
|
||||
func main() {
|
||||
signalCliConfigDir := "/home/.local/share/signal-cli/"
|
||||
signalCliConfigDirEnv := utils.GetEnv("SIGNAL_CLI_CONFIG_DIR", "")
|
||||
@@ -73,52 +38,16 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
signalCliConfigDataDir := signalCliConfigDir + "data"
|
||||
|
||||
jsonRpc2ClientConfig := utils.NewJsonRpc2ClientConfig()
|
||||
|
||||
var tcpBasePort int64 = 6000
|
||||
fifoBasePathName := "/tmp/sigsocket"
|
||||
var ctr int64 = 0
|
||||
var tcpPort int64 = 6001
|
||||
fifoPathname := "/tmp/sigsocket1"
|
||||
|
||||
items, err := ioutil.ReadDir(signalCliConfigDataDir)
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't read contents of ", signalCliConfigDataDir, ". Is your phone number properly registered? Please be aware that registering a phone number only works in normal/native mode and is currently not supported in json-rpc mode!")
|
||||
}
|
||||
for _, item := range items {
|
||||
if item.IsDir() {
|
||||
continue
|
||||
}
|
||||
filename := filepath.Base(item.Name())
|
||||
isSignalCliLinkedNumberConfigFile, err := isSignalCliLinkedNumberConfigFile(signalCliConfigDataDir + "/" + filename)
|
||||
if err != nil {
|
||||
log.Error("Couldn't determine whether file ", filename, " is a signal-cli config file: ", err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
if strings.HasPrefix(filename, "+") || isSignalCliLinkedNumberConfigFile {
|
||||
var number string = ""
|
||||
if utils.IsPhoneNumber(filename) {
|
||||
number = filename
|
||||
} else if isSignalCliLinkedNumberConfigFile {
|
||||
number, err = getUsernameFromLinkedNumberConfigFile(signalCliConfigDataDir + "/" + filename)
|
||||
if err != nil {
|
||||
log.Debug("Skipping ", filename, " as it is not a valid signal-cli config file: ", err.Error())
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
log.Error("Skipping ", filename, " as it is not a valid phone number!")
|
||||
continue
|
||||
}
|
||||
|
||||
fifoPathname := fifoBasePathName + strconv.FormatInt(ctr, 10)
|
||||
tcpPort := tcpBasePort + ctr
|
||||
jsonRpc2ClientConfig.AddEntry(number, utils.JsonRpc2ClientConfigEntry{TcpPort: tcpPort, FifoPathname: fifoPathname})
|
||||
ctr += 1
|
||||
jsonRpc2ClientConfig.AddEntry(utils.MULTI_ACCOUNT_NUMBER, utils.JsonRpc2ClientConfigEntry{TcpPort: tcpPort, FifoPathname: fifoPathname})
|
||||
|
||||
os.Remove(fifoPathname) //remove any existing named pipe
|
||||
|
||||
_, err = exec.Command("mkfifo", fifoPathname).Output()
|
||||
_, err := exec.Command("mkfifo", fifoPathname).Output()
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't create fifo with name ", fifoPathname, ": ", err.Error())
|
||||
}
|
||||
@@ -130,25 +59,27 @@ func main() {
|
||||
log.Fatal("Couldn't change permissions of fifo with name ", fifoPathname, ": ", err.Error())
|
||||
}
|
||||
|
||||
supervisorctlProgramName := "signal-cli-json-rpc-" + strconv.FormatInt(ctr, 10)
|
||||
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())
|
||||
}
|
||||
|
||||
log.Info("Found number ", number, " and added it to jsonrpc2.yml")
|
||||
log.Info("Updated jsonrpc2.yml")
|
||||
|
||||
//write supervisorctl config
|
||||
supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-" + strconv.FormatInt(ctr, 10) + ".conf"
|
||||
supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf"
|
||||
|
||||
|
||||
supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName,
|
||||
tcpPort, fifoPathname, number, signalCliConfigDir, fifoPathname, supervisorctlProgramName, supervisorctlProgramName)
|
||||
tcpPort, fifoPathname, signalCliConfigDir, fifoPathname, supervisorctlProgramName, supervisorctlProgramName)
|
||||
|
||||
|
||||
err = ioutil.WriteFile(supervisorctlConfigFilename, []byte(supervisorctlConfig), 0644)
|
||||
if err != nil {
|
||||
log.Fatal("Couldn't write ", supervisorctlConfigFilename, ": ", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// write jsonrpc.yml config file
|
||||
err = jsonRpc2ClientConfig.Persist(signalCliConfigDir + "jsonrpc2.yml")
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
const MULTI_ACCOUNT_NUMBER string = "<multi-account>"
|
||||
|
||||
type JsonRpc2ClientConfigEntry struct {
|
||||
TcpPort int64 `yaml:"tcp_port"`
|
||||
FifoPathname string `yaml:"fifo_pathname"`
|
||||
|
||||
Reference in New Issue
Block a user