Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link device in JSON-RPC mode #445

Merged
merged 2 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion src/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -1053,7 +1054,59 @@ func (s *SignalClient) DeleteGroup(number string, groupId string) error {

func (s *SignalClient) GetQrCodeLink(deviceName string, qrCodeVersion int) ([]byte, error) {
if s.signalCliMode == JsonRpc {
return []byte{}, errors.New(endpointNotSupportedInJsonRpcMode)
jsonRpc2Client, err := s.getJsonRpc2Client()
if err != nil {
return []byte{}, err
}

type StartRequest struct{}
type Response struct {
DeviceLinkUri string `json:"deviceLinkUri"`
}

result, err := jsonRpc2Client.getRaw("startLink", nil, &StartRequest{})
if err != nil {
return []byte{}, errors.New("Couldn't create QR code: " + err.Error())
}

var resp Response
err = json.Unmarshal([]byte(result), &resp)
if err != nil {
return []byte{}, errors.New("Couldn't create QR code: " + err.Error())
}

q, err := qrcode.NewWithForcedVersion(string(resp.DeviceLinkUri), qrCodeVersion, qrcode.Highest)
if err != nil {
return []byte{}, errors.New("Couldn't create QR code: " + err.Error())
}

var png []byte
png, err = q.PNG(256)
if err != nil {
return []byte{}, errors.New("Couldn't create QR code: " + err.Error())
}

go (func() {
type FinishRequest struct {
DeviceLinkUri string `json:"deviceLinkUri"`
DeviceName string `json:"deviceName"`
}

req := FinishRequest{
DeviceLinkUri: resp.DeviceLinkUri,
DeviceName: deviceName,
}

result, err := jsonRpc2Client.getRaw("finishLink", nil, &req)
if err != nil {
log.Debug("Error linking device: ", err.Error())
return
}
log.Debug("Linking device result: ", result)
s.signalCliApiConfig.Load(s.signalCliApiConfigPath)
})()

return png, nil
}
command := []string{"--config", s.signalCliConfig, "link", "-n", deviceName}

Expand Down
25 changes: 13 additions & 12 deletions src/client/jsonrpc2.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,18 @@ type JsonRpc2ReceivedMessage struct {

type JsonRpc2Client struct {
conn net.Conn
receivedMessageResponses chan JsonRpc2MessageResponse
receivedResponsesById map[string]chan JsonRpc2MessageResponse
receivedMessages chan JsonRpc2ReceivedMessage
lastTimeErrorMessageSent time.Time
signalCliApiConfig *utils.SignalCliApiConfig
number string
number string
}

func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client {
return &JsonRpc2Client{
signalCliApiConfig: signalCliApiConfig,
number: number,
signalCliApiConfig: signalCliApiConfig,
number: number,
receivedResponsesById: make(map[string]chan JsonRpc2MessageResponse),
}
}

Expand All @@ -53,7 +54,6 @@ func (r *JsonRpc2Client) Dial(address string) error {
return err
}

r.receivedMessageResponses = make(chan JsonRpc2MessageResponse)
r.receivedMessages = make(chan JsonRpc2ReceivedMessage)

return nil
Expand Down Expand Up @@ -113,13 +113,12 @@ func (r *JsonRpc2Client) getRaw(command string, account *string, args interface{
return "", err
}

responseChan := make(chan JsonRpc2MessageResponse)
r.receivedResponsesById[u.String()] = responseChan

var resp JsonRpc2MessageResponse
for {
resp = <-r.receivedMessageResponses
if resp.Id == u.String() {
break
}
}
resp = <-responseChan
delete(r.receivedResponsesById, u.String())

if resp.Err.Code != 0 {
return "", errors.New(resp.Err.Message)
Expand Down Expand Up @@ -157,7 +156,9 @@ func (r *JsonRpc2Client) ReceiveData(number string) {
err = json.Unmarshal([]byte(str), &resp2)
if err == nil {
if resp2.Id != "" {
r.receivedMessageResponses <- resp2
if responseChan, ok := r.receivedResponsesById[resp2.Id]; ok {
responseChan <- resp2
}
}
} else {
log.Error("Received unparsable message: ", str)
Expand Down
Loading