From a06a9f873b4f78ccccb93cf94c85d54b69df8e2e Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Mon, 30 Oct 2023 14:49:11 +0100 Subject: [PATCH 1/3] switched implementation to multi-account mode * instead of starting signal-cli in json-rpc mode with the '-u', we start signal-cli without the '-u' parameter (also known as multi-account mode). This makes it possible to register a number in json-rpc mode. --- src/api/api.go | 22 +++-- src/client/client.go | 127 ++++++++++++++++------------- src/client/jsonrpc2.go | 9 ++- src/scripts/jsonrpc2-helper.go | 141 +++++++++------------------------ src/utils/config.go | 2 + 5 files changed, 137 insertions(+), 164 deletions(-) diff --git a/src/api/api.go b/src/api/api.go index 2d8194f7..4ef5a31f 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -384,7 +384,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 @@ -404,12 +404,24 @@ func (a *Api) handleSignalReceive(ws *websocket.Conn, number string, stop chan s if err == nil { if data != "" { - err = ws.WriteMessage(websocket.TextMessage, []byte(data)) + type Response struct { + Account string `json:"account"` + } + var response Response + err = json.Unmarshal([]byte(data), &response) if err != nil { - if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { - log.Error("Couldn't write message: " + err.Error()) + log.Error("Couldn't parse message ", data, ":", err.Error()) + continue + } + + if response.Account == number { + err = ws.WriteMessage(websocket.TextMessage, []byte(data)) //TODO split up data in different channels + if err != nil { + if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { + log.Error("Couldn't write message: " + err.Error()) + } + return } - return } } } else { diff --git a/src/client/client.go b/src/client/client.go index 72e629a2..96ac3bb9 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -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 @@ -526,20 +526,41 @@ func (s *SignalClient) About() About { } func (s *SignalClient) RegisterNumber(number string, useVoice bool, captcha string) error { + var err error if s.signalCliMode == JsonRpc { - return errors.New(endpointNotSupportedInJsonRpcMode) - } - command := []string{"--config", s.signalCliConfig, "-a", number, "register"} + type Request struct { + UseVoice bool `json:"voice,omitempty"` + Captcha string `json:"captcha,omitempty"` + Account string `json:"account,omitempty"` + } + request := Request{Account: number} - if useVoice { - command = append(command, "--voice") - } + if useVoice { + request.UseVoice = useVoice + } - if captcha != "" { - command = append(command, []string{"--captcha", captcha}...) - } + if captcha != "" { + request.Captcha = captcha + } - _, err := s.cliClient.Execute(true, command, "") + jsonRpc2Client, err := s.getJsonRpc2Client() + if err != nil { + return err + } + _, err = jsonRpc2Client.getRaw("register", nil, request) + } else { + command := []string{"--config", s.signalCliConfig, "-a", number, "register"} + + if useVoice { + command = append(command, "--voice") + } + + if captcha != "" { + command = append(command, []string{"--captcha", captcha}...) + } + + _, err = s.cliClient.Execute(true, command, "") + } return err } @@ -588,8 +609,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 +713,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 +750,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 +838,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 +901,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 +937,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 +1020,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 +1160,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 +1183,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 +1245,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 +1273,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 +1291,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 +1309,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}, "") } @@ -1352,11 +1373,11 @@ func (s *SignalClient) UpdateGroup(number string, groupId string, base64Avatar * request.Description = groupDescription - 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 { @@ -1413,11 +1434,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 } @@ -1463,11 +1484,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 { @@ -1506,11 +1527,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 { @@ -1540,7 +1561,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 } @@ -1585,11 +1606,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, "") @@ -1612,11 +1633,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 { @@ -1637,11 +1658,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, "") diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 64ccca3a..e614a8f6 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -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")) diff --git a/src/scripts/jsonrpc2-helper.go b/src/scripts/jsonrpc2-helper.go index c9c10085..3669ddb5 100644 --- a/src/scripts/jsonrpc2-helper.go +++ b/src/scripts/jsonrpc2-helper.go @@ -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,81 +38,47 @@ 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" + + jsonRpc2ClientConfig.AddEntry(utils.MULTI_ACCOUNT_NUMBER, utils.JsonRpc2ClientConfigEntry{TcpPort: tcpPort, FifoPathname: fifoPathname}) - items, err := ioutil.ReadDir(signalCliConfigDataDir) + os.Remove(fifoPathname) //remove any existing named pipe + + _, err := exec.Command("mkfifo", fifoPathname).Output() 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!") + log.Fatal("Couldn't create fifo with name ", fifoPathname, ": ", err.Error()) } - 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 - - os.Remove(fifoPathname) //remove any existing named pipe - - _, err = exec.Command("mkfifo", fifoPathname).Output() - if err != nil { - log.Fatal("Couldn't create fifo with name ", fifoPathname, ": ", err.Error()) - } - - uid := utils.GetEnv("SIGNAL_CLI_UID", "1000") - gid := utils.GetEnv("SIGNAL_CLI_GID", "1000") - _, err = exec.Command("chown", uid+":"+gid, fifoPathname).Output() - if err != nil { - log.Fatal("Couldn't change permissions of fifo with name ", fifoPathname, ": ", err.Error()) - } - - supervisorctlProgramName := "signal-cli-json-rpc-" + strconv.FormatInt(ctr, 10) - 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") - - //write supervisorctl config - supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-" + strconv.FormatInt(ctr, 10) + ".conf" - supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName, - tcpPort, fifoPathname, number, signalCliConfigDir, fifoPathname, supervisorctlProgramName, supervisorctlProgramName) - err = ioutil.WriteFile(supervisorctlConfigFilename, []byte(supervisorctlConfig), 0644) - if err != nil { - log.Fatal("Couldn't write ", supervisorctlConfigFilename, ": ", err.Error()) - } - } + uid := utils.GetEnv("SIGNAL_CLI_UID", "1000") + gid := utils.GetEnv("SIGNAL_CLI_GID", "1000") + _, err = exec.Command("chown", uid+":"+gid, fifoPathname).Output() + if err != nil { + log.Fatal("Couldn't change permissions of fifo with name ", fifoPathname, ": ", err.Error()) + } + + 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("Updated jsonrpc2.yml") + + //write supervisorctl config + supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf" + + + supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, 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 diff --git a/src/utils/config.go b/src/utils/config.go index c6f7667d..feb525bd 100644 --- a/src/utils/config.go +++ b/src/utils/config.go @@ -6,6 +6,8 @@ import ( "io/ioutil" ) +const MULTI_ACCOUNT_NUMBER string = "" + type JsonRpc2ClientConfigEntry struct { TcpPort int64 `yaml:"tcp_port"` FifoPathname string `yaml:"fifo_pathname"` From 512458b202772bbc18888e8cc76ca81328e6a42c Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Mon, 30 Oct 2023 19:56:01 +0100 Subject: [PATCH 2/3] json-rpc mode: fixed bug in register endpoint + added verify endpoint --- src/client/client.go | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/client/client.go b/src/client/client.go index 96ac3bb9..605126bf 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -526,7 +526,6 @@ func (s *SignalClient) About() About { } func (s *SignalClient) RegisterNumber(number string, useVoice bool, captcha string) error { - var err error if s.signalCliMode == JsonRpc { type Request struct { UseVoice bool `json:"voice,omitempty"` @@ -548,6 +547,7 @@ func (s *SignalClient) RegisterNumber(number string, useVoice bool, captcha stri return err } _, err = jsonRpc2Client.getRaw("register", nil, request) + return err } else { command := []string{"--config", s.signalCliConfig, "-a", number, "register"} @@ -559,9 +559,9 @@ func (s *SignalClient) RegisterNumber(number string, useVoice bool, captcha stri command = append(command, []string{"--captcha", captcha}...) } - _, err = s.cliClient.Execute(true, command, "") + _, err := s.cliClient.Execute(true, command, "") + return err } - return err } func (s *SignalClient) UnregisterNumber(number string, deleteAccount bool, deleteLocalData bool) error { @@ -591,17 +591,33 @@ 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} - cmd := []string{"--config", s.signalCliConfig, "-a", number, "verify", token} - if pin != "" { - cmd = append(cmd, "--pin") - cmd = append(cmd, pin) - } + if pin != "" { + request.Pin = pin + } - _, err := s.cliClient.Execute(true, cmd, "") - return err + 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") + cmd = append(cmd, pin) + } + + _, err := s.cliClient.Execute(true, cmd, "") + return err + } } func (s *SignalClient) SendV1(number string, message string, recipients []string, base64Attachments []string, isGroup bool) (*SendResponse, error) { From 6feb7078f17b07e4f87706eaa09ff3924767590e Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Mon, 30 Oct 2023 20:00:16 +0100 Subject: [PATCH 3/3] improved misleading comment --- src/api/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/api/api.go b/src/api/api.go index 4ef5a31f..f25766ef 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -415,7 +415,7 @@ func (a *Api) handleSignalReceive(ws *websocket.Conn, number string, stop chan s } if response.Account == number { - err = ws.WriteMessage(websocket.TextMessage, []byte(data)) //TODO split up data in different channels + err = ws.WriteMessage(websocket.TextMessage, []byte(data)) if err != nil { if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { log.Error("Couldn't write message: " + err.Error())