From ad0c2310325154b59b8bb5523e29a132a82326f8 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 8 Apr 2026 16:16:55 -0400 Subject: [PATCH 01/18] feat(jsonrpc2): add manual receive mode to prevent message loss MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In the default 'on-start' receive mode, signal-cli's daemon auto-pulls inbound messages and pushes them to its JSON-RPC clients the moment they arrive from the Signal servers. If no websocket subscriber is currently attached to /v1/receive/{number} — for example during a brief subscriber redeploy — the receiver loop in signal-cli-rest-api drops the message via a non-blocking channel send (see the 'no receiver' debug log in ReceiveData). signal-cli has already acknowledged the message to Signal, so it is never redelivered. Add support for signal-cli's --receive-mode=manual, opt-in via the new JSON_RPC_RECEIVE_MODE=manual environment variable. In manual mode signal-cli does NOT auto-receive; signal-cli-rest-api now issues subscribeReceive when a websocket subscriber attaches and unsubscribeReceive when the last subscriber detaches. While no subscriber is attached, signal-cli does not pull messages from the Signal servers, so they remain buffered server-side under Signal's normal retention rules and are delivered to the next subscriber that attaches. Implementation: * jsonrpc2-helper.go: thread JSON_RPC_RECEIVE_MODE through to the daemon launch command line. * jsonrpc2.go: add per-account subscription tracking with refcounting; subscribeReceive/unsubscribeReceive helpers; an unwrap step in ReceiveData so manual-mode notifications ({subscription,result}) and auto-mode notifications (envelope directly) reach downstream consumers in the same shape. * client.go, api.go: thread the account number through GetReceiveChannel so the JsonRpc2Client can attach the right subscription. The change is fully backward compatible: when JSON_RPC_RECEIVE_MODE is unset or set to 'on-start', signal-cli runs in auto mode and the new subscribeReceive code path is bypassed (account is empty, no RPC issued). Closes #255 (the same root cause: no subscriber → 'no receiver' drop). --- src/api/api.go | 2 +- src/client/client.go | 4 +- src/client/jsonrpc2.go | 140 ++++++++++++++++++++++++++++++--- src/scripts/jsonrpc2-helper.go | 20 ++++- 4 files changed, 150 insertions(+), 16 deletions(-) diff --git a/src/api/api.go b/src/api/api.go index a0304fc..ce447eb 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -572,7 +572,7 @@ func (a *Api) SendV2(c *gin.Context) { } func (a *Api) handleSignalReceive(ws *websocket.Conn, number string, stop chan struct{}) { - receiveChannel, channelUuid, err := a.signalClient.GetReceiveChannel() + receiveChannel, channelUuid, err := a.signalClient.GetReceiveChannel(number) if err != nil { log.Error("Couldn't get receive channel: ", err.Error()) return diff --git a/src/client/client.go b/src/client/client.go index d02eb36..5ba9fa5 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -1059,12 +1059,12 @@ func (s *SignalClient) Receive(number string, timeout int64, ignoreAttachments b } } -func (s *SignalClient) GetReceiveChannel() (chan JsonRpc2ReceivedMessage, string, error) { +func (s *SignalClient) GetReceiveChannel(number string) (chan JsonRpc2ReceivedMessage, string, error) { jsonRpc2Client, err := s.getJsonRpc2Client() if err != nil { return nil, "", err } - return jsonRpc2Client.GetReceiveChannel() + return jsonRpc2Client.GetReceiveChannel(number) } func (s *SignalClient) RemoveReceiveChannel(channelUuid string) { diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index ef00633..5d0d62f 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -5,6 +5,7 @@ import ( "bytes" "encoding/json" "errors" + "fmt" "net" "net/http" "strconv" @@ -56,15 +57,26 @@ func (r *RateLimitErrorType) Error() string { return r.Err.Error() } +// receiveSubscription tracks the state of a manual-mode signal-cli +// subscribeReceive call: the subscription id assigned by signal-cli and +// a refcount of websocket subscribers attached to that account. +type receiveSubscription struct { + id int64 + refcount int +} + type JsonRpc2Client struct { - conn net.Conn - receivedResponsesById map[string]chan JsonRpc2MessageResponse - receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage - signalCliApiConfig *utils.SignalCliApiConfig - number string - receivedMessagesMutex sync.Mutex - receivedResponsesMutex sync.Mutex - address string + conn net.Conn + receivedResponsesById map[string]chan JsonRpc2MessageResponse + receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage + receiveSubscriptions map[string]*receiveSubscription // account -> sub state + channelAccountByUuid map[string]string // channelUuid -> account + signalCliApiConfig *utils.SignalCliApiConfig + number string + receivedMessagesMutex sync.Mutex + receivedResponsesMutex sync.Mutex + receiveSubscriptionsMutex sync.Mutex + address string } func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client { @@ -73,6 +85,8 @@ func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number stri number: number, receivedResponsesById: make(map[string]chan JsonRpc2MessageResponse), receivedMessagesChannels: make(map[string]chan JsonRpc2ReceivedMessage), + receiveSubscriptions: make(map[string]*receiveSubscription), + channelAccountByUuid: make(map[string]string), } } @@ -236,6 +250,19 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) { var resp1 JsonRpc2ReceivedMessage json.Unmarshal([]byte(str), &resp1) if resp1.Method == "receive" { + // In manual receive-mode signal-cli wraps the envelope in + // {"subscription":N,"result":{...}}; in auto mode it sends + // the envelope directly. Unwrap so the broadcast format is + // the same in both modes and downstream consumers (e.g. the + // websocket handler) don't have to know which mode is in use. + var manualWrapper struct { + Subscription int64 `json:"subscription"` + Result json.RawMessage `json:"result"` + } + if err := json.Unmarshal(resp1.Params, &manualWrapper); err == nil && len(manualWrapper.Result) > 0 { + resp1.Params = manualWrapper.Result + } + r.receivedMessagesMutex.Lock() for _, c := range r.receivedMessagesChannels { select { @@ -244,7 +271,6 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) { default: log.Debug("Couldn't send message to golang channel, as there's no receiver") } - continue } r.receivedMessagesMutex.Unlock() @@ -270,16 +296,102 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) { } } -func (r *JsonRpc2Client) GetReceiveChannel() (chan JsonRpc2ReceivedMessage, string, error) { - c := make(chan JsonRpc2ReceivedMessage) +// subscribeReceive starts receiving messages for an account by calling +// signal-cli's subscribeReceive JSON-RPC method. Only relevant when +// signal-cli was launched with --receive-mode=manual; in auto mode the +// daemon pushes notifications without an explicit subscribe call. +// Returns the subscription id assigned by signal-cli. +func (r *JsonRpc2Client) subscribeReceive(account string) (int64, error) { + type subscribeReceiveArgs struct{} + resultStr, err := r.getRaw("subscribeReceive", &account, subscribeReceiveArgs{}) + if err != nil { + return 0, err + } + var subscriptionId int64 + if err := json.Unmarshal([]byte(resultStr), &subscriptionId); err != nil { + return 0, fmt.Errorf("subscribeReceive: couldn't parse subscription id from %q: %w", resultStr, err) + } + return subscriptionId, nil +} + +// unsubscribeReceive cancels a manual-mode subscription previously +// returned by subscribeReceive. +func (r *JsonRpc2Client) unsubscribeReceive(account string, subscriptionId int64) error { + type unsubscribeReceiveArgs struct { + Subscription int64 `json:"subscription"` + } + _, err := r.getRaw("unsubscribeReceive", &account, unsubscribeReceiveArgs{Subscription: subscriptionId}) + return err +} + +// acquireReceiveSubscription ensures an active manual-mode subscription +// exists for the given account, refcounting concurrent websocket +// subscribers. The first caller for an account triggers a real +// subscribeReceive RPC; subsequent callers just bump the refcount. +func (r *JsonRpc2Client) acquireReceiveSubscription(account string) error { + r.receiveSubscriptionsMutex.Lock() + defer r.receiveSubscriptionsMutex.Unlock() + + if sub, ok := r.receiveSubscriptions[account]; ok { + sub.refcount++ + return nil + } + id, err := r.subscribeReceive(account) + if err != nil { + return err + } + r.receiveSubscriptions[account] = &receiveSubscription{id: id, refcount: 1} + log.Infof("Subscribed to receive notifications for account %s (subscription=%d)", account, id) + return nil +} + +// releaseReceiveSubscription decrements the per-account refcount and +// cancels the subscription with signal-cli if it drops to zero. +func (r *JsonRpc2Client) releaseReceiveSubscription(account string) { + r.receiveSubscriptionsMutex.Lock() + defer r.receiveSubscriptionsMutex.Unlock() + + sub, ok := r.receiveSubscriptions[account] + if !ok { + return + } + sub.refcount-- + if sub.refcount > 0 { + return + } + if err := r.unsubscribeReceive(account, sub.id); err != nil { + log.Warnf("unsubscribeReceive failed for account %s (subscription=%d): %s", account, sub.id, err.Error()) + } else { + log.Infof("Unsubscribed from receive notifications for account %s (subscription=%d)", account, sub.id) + } + delete(r.receiveSubscriptions, account) +} + +// GetReceiveChannel returns a channel that will receive messages for the +// given account. If signal-cli is in manual receive-mode, it also acquires +// a subscription so that signal-cli starts forwarding messages for the +// account; in auto mode, account is unused (notifications flow regardless). +// +// account may be empty when the caller does not need a subscription +// (e.g. legacy callers in auto mode); in that case no subscribeReceive +// RPC is issued. +func (r *JsonRpc2Client) GetReceiveChannel(account string) (chan JsonRpc2ReceivedMessage, string, error) { + c := make(chan JsonRpc2ReceivedMessage, 64) channelUuid, err := uuid.NewV4() if err != nil { return c, "", err } + if account != "" { + if err := r.acquireReceiveSubscription(account); err != nil { + return c, "", fmt.Errorf("subscribeReceive failed for account %s: %w", account, err) + } + } + r.receivedMessagesMutex.Lock() r.receivedMessagesChannels[channelUuid.String()] = c + r.channelAccountByUuid[channelUuid.String()] = account r.receivedMessagesMutex.Unlock() return c, channelUuid.String(), nil @@ -288,5 +400,11 @@ func (r *JsonRpc2Client) GetReceiveChannel() (chan JsonRpc2ReceivedMessage, stri func (r *JsonRpc2Client) RemoveReceiveChannel(channelUuid string) { r.receivedMessagesMutex.Lock() delete(r.receivedMessagesChannels, channelUuid) + account := r.channelAccountByUuid[channelUuid] + delete(r.channelAccountByUuid, channelUuid) r.receivedMessagesMutex.Unlock() + + if account != "" { + r.releaseReceiveSubscription(account) + } } diff --git a/src/scripts/jsonrpc2-helper.go b/src/scripts/jsonrpc2-helper.go index 96a4c34..2c8a75b 100644 --- a/src/scripts/jsonrpc2-helper.go +++ b/src/scripts/jsonrpc2-helper.go @@ -13,7 +13,7 @@ import ( const supervisorctlConfigTemplate = ` [program:%s] process_name=%s -command=%s --output=json --config %s%s daemon %s%s%s%s --tcp 127.0.0.1:%d +command=%s --output=json --config %s%s daemon%s %s%s%s%s --tcp 127.0.0.1:%d autostart=true autorestart=true startretries=10 @@ -75,6 +75,22 @@ func main() { signalCliIgnoreStickers = " --ignore-stickers" } + // Receive mode: by default signal-cli auto-receives messages on the + // daemon and pushes them as JSON-RPC notifications, regardless of + // whether any websocket subscriber is connected. In a deploy where + // the subscribing client briefly disconnects, those notifications + // have nowhere to go and are silently dropped. With manual receive + // mode, signal-cli only fetches messages when explicitly requested + // via subscribeReceive — see jsonrpc2.go — so messages stay buffered + // on the Signal servers until a subscriber re-attaches. + signalCliReceiveMode := "" + receiveMode := utils.GetEnv("JSON_RPC_RECEIVE_MODE", "") + if receiveMode == "manual" { + signalCliReceiveMode = " --receive-mode=manual" + } else if receiveMode != "" && receiveMode != "on-start" { + log.Fatal("Invalid JSON_RPC_RECEIVE_MODE environment variable set! Must be 'manual' or 'on-start'.") + } + supervisorctlProgramName := "signal-cli-json-rpc-1" supervisorctlLogFolder := "/var/log/" + supervisorctlProgramName _, err := exec.Command("mkdir", "-p", supervisorctlLogFolder).Output() @@ -100,7 +116,7 @@ func main() { supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf" supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName, signalCliBinary, - signalCliConfigDir, trustNewIdentities, signalCliIgnoreAttachments, signalCliIgnoreStories, + signalCliConfigDir, trustNewIdentities, signalCliReceiveMode, signalCliIgnoreAttachments, signalCliIgnoreStories, signalCliIgnoreAvatars, signalCliIgnoreStickers, tcpPort, supervisorctlProgramName, supervisorctlProgramName) From e98c03e702efaed305be0d1a5379ea1b3a8d2f2f Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Wed, 8 Apr 2026 16:59:19 -0400 Subject: [PATCH 02/18] docs: document JSON_RPC_RECEIVE_MODE in README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 316bb21..9c73d36 100644 --- a/README.md +++ b/README.md @@ -160,3 +160,4 @@ There are a bunch of environmental variables that can be set inside the docker c * `JSON_RPC_IGNORE_AVATARS`: When set to `true`, avatars are not automatically downloaded in json-rpc mode (default: `false`) * `JSON_RPC_IGNORE_STICKERS`: When set to `true`, sticker packs are not automatically downloaded in json-rpc mode (default: `false`) * `JSON_RPC_TRUST_NEW_IDENTITIES`: Choose how to trust new identities in json-rpc mode. Supported values: `on-first-use`, `always`, `never`. (default: `on-first-use`) +* `JSON_RPC_RECEIVE_MODE`: Controls when signal-cli pulls inbound messages from the Signal servers in json-rpc mode. Supported values: `on-start` (default), `manual`. In the default `on-start` mode, signal-cli auto-receives messages and pushes them as JSON-RPC notifications regardless of whether any websocket subscriber is currently attached to `/v1/receive/{number}` — messages arriving while no subscriber is attached (e.g. during a brief subscriber redeploy) are silently dropped. In `manual` mode signal-cli only fetches messages while at least one websocket subscriber is attached; while no subscriber is attached, messages remain queued server-side under Signal's normal retention rules and are delivered on the next subscriber re-attach. Recommended for any deployment where the websocket consumer is restarted or scaled. From 453fcfad902b632f3d1b7c2b99040c4d59bd4463 Mon Sep 17 00:00:00 2001 From: Mykola Balakin Date: Wed, 12 Aug 2026 00:03:27 +0300 Subject: [PATCH 03/18] Delete excess check (review fixes) --- src/client/jsonrpc2.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 5d0d62f..b3bfb43 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -383,10 +383,8 @@ func (r *JsonRpc2Client) GetReceiveChannel(account string) (chan JsonRpc2Receive return c, "", err } - if account != "" { - if err := r.acquireReceiveSubscription(account); err != nil { - return c, "", fmt.Errorf("subscribeReceive failed for account %s: %w", account, err) - } + if err := r.acquireReceiveSubscription(account); err != nil { + return c, "", fmt.Errorf("subscribeReceive failed for account %s: %w", account, err) } r.receivedMessagesMutex.Lock() From 14998109e30413dac30b96163c3361688a8cc112 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Fri, 25 Sep 2026 22:35:05 +0200 Subject: [PATCH 04/18] updated swagger docs --- src/docs/docs.go | 110 +++++++++++++++++++++++++++++++++++++++++- src/docs/swagger.json | 110 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 218 insertions(+), 2 deletions(-) diff --git a/src/docs/docs.go b/src/docs/docs.go index 7eccd61..d65d108 100644 --- a/src/docs/docs.go +++ b/src/docs/docs.go @@ -119,6 +119,14 @@ const docTemplate = `{ "$ref": "#/definitions/data.GroupPermissions" } }, + "required": [ + "description", + "expiration_time", + "group_link", + "members", + "name", + "permissions" + ], "type": "object" }, "api.CreateGroupResponse": { @@ -159,6 +167,7 @@ const docTemplate = `{ } }, "required": [ + "allow_multiple_selections", "answers", "question", "recipient" @@ -184,6 +193,9 @@ const docTemplate = `{ "type": "boolean" } }, + "required": [ + "ignore_registered" + ], "type": "object" }, "api.DeviceLinkUriResponse": { @@ -232,6 +244,7 @@ const docTemplate = `{ } }, "required": [ + "duration", "target_author", "timestamp" ], @@ -286,6 +299,10 @@ const docTemplate = `{ "type": "boolean" } }, + "required": [ + "captcha", + "use_voice" + ], "type": "object" }, "api.RemoteDeleteRequest": { @@ -330,6 +347,7 @@ const docTemplate = `{ } }, "required": [ + "reaction", "recipient", "target_author", "timestamp" @@ -368,6 +386,7 @@ const docTemplate = `{ }, "required": [ "account", + "challenge_tokens", "error" ], "type": "object" @@ -395,6 +414,8 @@ const docTemplate = `{ } }, "required": [ + "base64_attachment", + "is_group", "message", "number", "recipients" @@ -471,9 +492,21 @@ const docTemplate = `{ } }, "required": [ + "base64_attachments", + "edit_timestamp", + "link_preview", + "mentions", "message", + "notify_self", "number", - "recipients" + "quote_author", + "quote_mentions", + "quote_message", + "quote_timestamp", + "recipients", + "sticker", + "text_mode", + "view_once" ], "type": "object" }, @@ -533,6 +566,10 @@ const docTemplate = `{ "type": "string" } }, + "required": [ + "trust_all_known_keys", + "verified_safety_number" + ], "type": "object" }, "api.TrustModeRequest": { @@ -594,6 +631,10 @@ const docTemplate = `{ "type": "boolean" } }, + "required": [ + "delete_account", + "delete_local_data" + ], "type": "object" }, "api.UpdateAccountSettingsRequest": { @@ -605,6 +646,10 @@ const docTemplate = `{ "type": "boolean" } }, + "required": [ + "discoverable_by_number", + "share_number" + ], "type": "object" }, "api.UpdateContactRequest": { @@ -620,6 +665,8 @@ const docTemplate = `{ } }, "required": [ + "expiration_in_seconds", + "name", "recipient" ], "type": "object" @@ -650,6 +697,14 @@ const docTemplate = `{ "$ref": "#/definitions/data.GroupPermissions" } }, + "required": [ + "base64_avatar", + "description", + "expiration_time", + "group_link", + "name", + "permissions" + ], "type": "object" }, "api.UpdateProfileRequest": { @@ -665,6 +720,8 @@ const docTemplate = `{ } }, "required": [ + "about", + "base64_avatar", "name" ], "type": "object" @@ -675,6 +732,9 @@ const docTemplate = `{ "type": "string" } }, + "required": [ + "pin" + ], "type": "object" }, "api.VoteRequest": { @@ -1112,6 +1172,12 @@ const docTemplate = `{ "type": "string" } }, + "required": [ + "number", + "reason", + "username", + "uuid" + ], "type": "object" }, "data.SendMessageErrors": { @@ -1123,6 +1189,9 @@ const docTemplate = `{ "type": "array" } }, + "required": [ + "recipients" + ], "type": "object" }, "data.SendMessageResponse": { @@ -1135,6 +1204,7 @@ const docTemplate = `{ } }, "required": [ + "errors", "timestamp" ], "type": "object" @@ -1176,6 +1246,9 @@ const docTemplate = `{ "id": { "type": "string" }, + "isVoiceNote": { + "type": "boolean" + }, "size": { "type": "integer" }, @@ -1186,6 +1259,9 @@ const docTemplate = `{ "type": "integer" } }, + "required": [ + "isVoiceNote" + ], "type": "object" }, "receive.AttachmentData": { @@ -1452,12 +1528,24 @@ const docTemplate = `{ "expiresInSeconds": { "type": "integer" }, + "groupCallUpdate": { + "$ref": "#/definitions/receive.GroupCallUpdate" + }, "groupInfo": { "$ref": "#/definitions/receive.GroupInfo" }, + "hasProfileKey": { + "type": "boolean" + }, + "isEndSession": { + "type": "boolean" + }, "isExpirationUpdate": { "type": "boolean" }, + "isProfileKeyUpdate": { + "type": "boolean" + }, "mentions": { "items": { "$ref": "#/definitions/receive.Mention" @@ -1549,6 +1637,14 @@ const docTemplate = `{ }, "type": "object" }, + "receive.GroupCallUpdate": { + "properties": { + "eraId": { + "type": "string" + } + }, + "type": "object" + }, "receive.GroupInfo": { "properties": { "groupId": { @@ -2139,12 +2235,24 @@ const docTemplate = `{ "expiresInSeconds": { "type": "integer" }, + "groupCallUpdate": { + "$ref": "#/definitions/receive.GroupCallUpdate" + }, "groupInfo": { "$ref": "#/definitions/receive.GroupInfo" }, + "hasProfileKey": { + "type": "boolean" + }, + "isEndSession": { + "type": "boolean" + }, "isExpirationUpdate": { "type": "boolean" }, + "isProfileKeyUpdate": { + "type": "boolean" + }, "mentions": { "items": { "$ref": "#/definitions/receive.Mention" diff --git a/src/docs/swagger.json b/src/docs/swagger.json index 5eee8ec..074c9f3 100644 --- a/src/docs/swagger.json +++ b/src/docs/swagger.json @@ -114,6 +114,14 @@ "$ref": "#/definitions/data.GroupPermissions" } }, + "required": [ + "description", + "expiration_time", + "group_link", + "members", + "name", + "permissions" + ], "type": "object" }, "api.CreateGroupResponse": { @@ -154,6 +162,7 @@ } }, "required": [ + "allow_multiple_selections", "answers", "question", "recipient" @@ -179,6 +188,9 @@ "type": "boolean" } }, + "required": [ + "ignore_registered" + ], "type": "object" }, "api.DeviceLinkUriResponse": { @@ -227,6 +239,7 @@ } }, "required": [ + "duration", "target_author", "timestamp" ], @@ -281,6 +294,10 @@ "type": "boolean" } }, + "required": [ + "captcha", + "use_voice" + ], "type": "object" }, "api.RemoteDeleteRequest": { @@ -325,6 +342,7 @@ } }, "required": [ + "reaction", "recipient", "target_author", "timestamp" @@ -363,6 +381,7 @@ }, "required": [ "account", + "challenge_tokens", "error" ], "type": "object" @@ -390,6 +409,8 @@ } }, "required": [ + "base64_attachment", + "is_group", "message", "number", "recipients" @@ -466,9 +487,21 @@ } }, "required": [ + "base64_attachments", + "edit_timestamp", + "link_preview", + "mentions", "message", + "notify_self", "number", - "recipients" + "quote_author", + "quote_mentions", + "quote_message", + "quote_timestamp", + "recipients", + "sticker", + "text_mode", + "view_once" ], "type": "object" }, @@ -528,6 +561,10 @@ "type": "string" } }, + "required": [ + "trust_all_known_keys", + "verified_safety_number" + ], "type": "object" }, "api.TrustModeRequest": { @@ -589,6 +626,10 @@ "type": "boolean" } }, + "required": [ + "delete_account", + "delete_local_data" + ], "type": "object" }, "api.UpdateAccountSettingsRequest": { @@ -600,6 +641,10 @@ "type": "boolean" } }, + "required": [ + "discoverable_by_number", + "share_number" + ], "type": "object" }, "api.UpdateContactRequest": { @@ -615,6 +660,8 @@ } }, "required": [ + "expiration_in_seconds", + "name", "recipient" ], "type": "object" @@ -645,6 +692,14 @@ "$ref": "#/definitions/data.GroupPermissions" } }, + "required": [ + "base64_avatar", + "description", + "expiration_time", + "group_link", + "name", + "permissions" + ], "type": "object" }, "api.UpdateProfileRequest": { @@ -660,6 +715,8 @@ } }, "required": [ + "about", + "base64_avatar", "name" ], "type": "object" @@ -670,6 +727,9 @@ "type": "string" } }, + "required": [ + "pin" + ], "type": "object" }, "api.VoteRequest": { @@ -1107,6 +1167,12 @@ "type": "string" } }, + "required": [ + "number", + "reason", + "username", + "uuid" + ], "type": "object" }, "data.SendMessageErrors": { @@ -1118,6 +1184,9 @@ "type": "array" } }, + "required": [ + "recipients" + ], "type": "object" }, "data.SendMessageResponse": { @@ -1130,6 +1199,7 @@ } }, "required": [ + "errors", "timestamp" ], "type": "object" @@ -1171,6 +1241,9 @@ "id": { "type": "string" }, + "isVoiceNote": { + "type": "boolean" + }, "size": { "type": "integer" }, @@ -1181,6 +1254,9 @@ "type": "integer" } }, + "required": [ + "isVoiceNote" + ], "type": "object" }, "receive.AttachmentData": { @@ -1447,12 +1523,24 @@ "expiresInSeconds": { "type": "integer" }, + "groupCallUpdate": { + "$ref": "#/definitions/receive.GroupCallUpdate" + }, "groupInfo": { "$ref": "#/definitions/receive.GroupInfo" }, + "hasProfileKey": { + "type": "boolean" + }, + "isEndSession": { + "type": "boolean" + }, "isExpirationUpdate": { "type": "boolean" }, + "isProfileKeyUpdate": { + "type": "boolean" + }, "mentions": { "items": { "$ref": "#/definitions/receive.Mention" @@ -1544,6 +1632,14 @@ }, "type": "object" }, + "receive.GroupCallUpdate": { + "properties": { + "eraId": { + "type": "string" + } + }, + "type": "object" + }, "receive.GroupInfo": { "properties": { "groupId": { @@ -2134,12 +2230,24 @@ "expiresInSeconds": { "type": "integer" }, + "groupCallUpdate": { + "$ref": "#/definitions/receive.GroupCallUpdate" + }, "groupInfo": { "$ref": "#/definitions/receive.GroupInfo" }, + "hasProfileKey": { + "type": "boolean" + }, + "isEndSession": { + "type": "boolean" + }, "isExpirationUpdate": { "type": "boolean" }, + "isProfileKeyUpdate": { + "type": "boolean" + }, "mentions": { "items": { "$ref": "#/definitions/receive.Mention" From 74253a3a41c2cade40fd4a53cf3fb7a3fdf305dc Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Fri, 25 Sep 2026 22:44:28 +0200 Subject: [PATCH 05/18] updated swagger docs --- src/docs/docs.go | 72 +------------------------------------------ src/docs/swagger.json | 72 +------------------------------------------ 2 files changed, 2 insertions(+), 142 deletions(-) diff --git a/src/docs/docs.go b/src/docs/docs.go index d65d108..cda355d 100644 --- a/src/docs/docs.go +++ b/src/docs/docs.go @@ -119,14 +119,6 @@ const docTemplate = `{ "$ref": "#/definitions/data.GroupPermissions" } }, - "required": [ - "description", - "expiration_time", - "group_link", - "members", - "name", - "permissions" - ], "type": "object" }, "api.CreateGroupResponse": { @@ -167,7 +159,6 @@ const docTemplate = `{ } }, "required": [ - "allow_multiple_selections", "answers", "question", "recipient" @@ -193,9 +184,6 @@ const docTemplate = `{ "type": "boolean" } }, - "required": [ - "ignore_registered" - ], "type": "object" }, "api.DeviceLinkUriResponse": { @@ -244,7 +232,6 @@ const docTemplate = `{ } }, "required": [ - "duration", "target_author", "timestamp" ], @@ -299,10 +286,6 @@ const docTemplate = `{ "type": "boolean" } }, - "required": [ - "captcha", - "use_voice" - ], "type": "object" }, "api.RemoteDeleteRequest": { @@ -347,7 +330,6 @@ const docTemplate = `{ } }, "required": [ - "reaction", "recipient", "target_author", "timestamp" @@ -386,7 +368,6 @@ const docTemplate = `{ }, "required": [ "account", - "challenge_tokens", "error" ], "type": "object" @@ -414,8 +395,6 @@ const docTemplate = `{ } }, "required": [ - "base64_attachment", - "is_group", "message", "number", "recipients" @@ -492,21 +471,9 @@ const docTemplate = `{ } }, "required": [ - "base64_attachments", - "edit_timestamp", - "link_preview", - "mentions", "message", - "notify_self", "number", - "quote_author", - "quote_mentions", - "quote_message", - "quote_timestamp", - "recipients", - "sticker", - "text_mode", - "view_once" + "recipients" ], "type": "object" }, @@ -566,10 +533,6 @@ const docTemplate = `{ "type": "string" } }, - "required": [ - "trust_all_known_keys", - "verified_safety_number" - ], "type": "object" }, "api.TrustModeRequest": { @@ -631,10 +594,6 @@ const docTemplate = `{ "type": "boolean" } }, - "required": [ - "delete_account", - "delete_local_data" - ], "type": "object" }, "api.UpdateAccountSettingsRequest": { @@ -646,10 +605,6 @@ const docTemplate = `{ "type": "boolean" } }, - "required": [ - "discoverable_by_number", - "share_number" - ], "type": "object" }, "api.UpdateContactRequest": { @@ -665,8 +620,6 @@ const docTemplate = `{ } }, "required": [ - "expiration_in_seconds", - "name", "recipient" ], "type": "object" @@ -697,14 +650,6 @@ const docTemplate = `{ "$ref": "#/definitions/data.GroupPermissions" } }, - "required": [ - "base64_avatar", - "description", - "expiration_time", - "group_link", - "name", - "permissions" - ], "type": "object" }, "api.UpdateProfileRequest": { @@ -720,8 +665,6 @@ const docTemplate = `{ } }, "required": [ - "about", - "base64_avatar", "name" ], "type": "object" @@ -732,9 +675,6 @@ const docTemplate = `{ "type": "string" } }, - "required": [ - "pin" - ], "type": "object" }, "api.VoteRequest": { @@ -1172,12 +1112,6 @@ const docTemplate = `{ "type": "string" } }, - "required": [ - "number", - "reason", - "username", - "uuid" - ], "type": "object" }, "data.SendMessageErrors": { @@ -1189,9 +1123,6 @@ const docTemplate = `{ "type": "array" } }, - "required": [ - "recipients" - ], "type": "object" }, "data.SendMessageResponse": { @@ -1204,7 +1135,6 @@ const docTemplate = `{ } }, "required": [ - "errors", "timestamp" ], "type": "object" diff --git a/src/docs/swagger.json b/src/docs/swagger.json index 074c9f3..27a3542 100644 --- a/src/docs/swagger.json +++ b/src/docs/swagger.json @@ -114,14 +114,6 @@ "$ref": "#/definitions/data.GroupPermissions" } }, - "required": [ - "description", - "expiration_time", - "group_link", - "members", - "name", - "permissions" - ], "type": "object" }, "api.CreateGroupResponse": { @@ -162,7 +154,6 @@ } }, "required": [ - "allow_multiple_selections", "answers", "question", "recipient" @@ -188,9 +179,6 @@ "type": "boolean" } }, - "required": [ - "ignore_registered" - ], "type": "object" }, "api.DeviceLinkUriResponse": { @@ -239,7 +227,6 @@ } }, "required": [ - "duration", "target_author", "timestamp" ], @@ -294,10 +281,6 @@ "type": "boolean" } }, - "required": [ - "captcha", - "use_voice" - ], "type": "object" }, "api.RemoteDeleteRequest": { @@ -342,7 +325,6 @@ } }, "required": [ - "reaction", "recipient", "target_author", "timestamp" @@ -381,7 +363,6 @@ }, "required": [ "account", - "challenge_tokens", "error" ], "type": "object" @@ -409,8 +390,6 @@ } }, "required": [ - "base64_attachment", - "is_group", "message", "number", "recipients" @@ -487,21 +466,9 @@ } }, "required": [ - "base64_attachments", - "edit_timestamp", - "link_preview", - "mentions", "message", - "notify_self", "number", - "quote_author", - "quote_mentions", - "quote_message", - "quote_timestamp", - "recipients", - "sticker", - "text_mode", - "view_once" + "recipients" ], "type": "object" }, @@ -561,10 +528,6 @@ "type": "string" } }, - "required": [ - "trust_all_known_keys", - "verified_safety_number" - ], "type": "object" }, "api.TrustModeRequest": { @@ -626,10 +589,6 @@ "type": "boolean" } }, - "required": [ - "delete_account", - "delete_local_data" - ], "type": "object" }, "api.UpdateAccountSettingsRequest": { @@ -641,10 +600,6 @@ "type": "boolean" } }, - "required": [ - "discoverable_by_number", - "share_number" - ], "type": "object" }, "api.UpdateContactRequest": { @@ -660,8 +615,6 @@ } }, "required": [ - "expiration_in_seconds", - "name", "recipient" ], "type": "object" @@ -692,14 +645,6 @@ "$ref": "#/definitions/data.GroupPermissions" } }, - "required": [ - "base64_avatar", - "description", - "expiration_time", - "group_link", - "name", - "permissions" - ], "type": "object" }, "api.UpdateProfileRequest": { @@ -715,8 +660,6 @@ } }, "required": [ - "about", - "base64_avatar", "name" ], "type": "object" @@ -727,9 +670,6 @@ "type": "string" } }, - "required": [ - "pin" - ], "type": "object" }, "api.VoteRequest": { @@ -1167,12 +1107,6 @@ "type": "string" } }, - "required": [ - "number", - "reason", - "username", - "uuid" - ], "type": "object" }, "data.SendMessageErrors": { @@ -1184,9 +1118,6 @@ "type": "array" } }, - "required": [ - "recipients" - ], "type": "object" }, "data.SendMessageResponse": { @@ -1199,7 +1130,6 @@ } }, "required": [ - "errors", "timestamp" ], "type": "object" From 80e0f34032be465b5a098c53be2484907f370404 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Sat, 26 Sep 2026 21:33:47 +0200 Subject: [PATCH 06/18] fixed crash in json-rpc mode * protect map access with mutex see #885 --- src/client/jsonrpc2.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 94307cd..ccdcf09 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -262,7 +262,10 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) { err = json.Unmarshal([]byte(str), &resp2) if err == nil { if resp2.Id != "" { - if responseChan, ok := r.receivedResponsesById[resp2.Id]; ok { + r.receivedResponsesMutex.Lock() + responseChan, ok := r.receivedResponsesById[resp2.Id] + r.receivedResponsesMutex.Unlock() + if ok { responseChan <- resp2 } } From e9bee58aeb1ab4d2894d41c82ba0a3ded5ca9dcc Mon Sep 17 00:00:00 2001 From: Gara Dorta <1507774+Gara-Dorta@users.noreply.github.com> Date: Sat, 26 Sep 2026 23:20:04 +0200 Subject: [PATCH 07/18] docs: fix wrong annotations for SendV2 and 200 - 201 --- src/api/api.go | 10 +++++----- src/docs/docs.go | 17 ++++++++++------- src/docs/swagger.json | 17 ++++++++++------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/src/api/api.go b/src/api/api.go index 210660b..f0d637c 100644 --- a/src/api/api.go +++ b/src/api/api.go @@ -497,7 +497,7 @@ func (a *Api) Send(c *gin.Context) { // @Description Send a signal message. Set the text_mode to 'styled' in case you want to add formatting to your text message. Styling Options: \*italic text\*, \*\*bold text\*\*, ~strikethrough text~, ||spoiler||, \`monospace\`. If you want to escape a formatting character, prefix it with two backslashes. // @Accept json // @Produce json -// @Success 201 {object} ds.SendMessageResponse +// @Success 201 {array} ds.SendMessageResponse // @Failure 400 {object} SendMessageError // @Param data body SendMessageV2 true "Input Data" // @Router /v2/send [post] @@ -1202,7 +1202,7 @@ func (a *Api) DeleteGroup(c *gin.Context) { // @Description Pin a message in a Signal Group. // @Accept json // @Produce json -// @Success 200 {string} string "OK" +// @Success 201 {string} string "OK" // @Failure 400 {object} Error // @Param data body PinMessageInGroupRequest true "Pin" // @Param number path string true "Registered Phone Number" @@ -1253,7 +1253,7 @@ func (a *Api) PinMessageInGroup(c *gin.Context) { // @Description Unpin a message in a Signal Group. // @Accept json // @Produce json -// @Success 200 {string} string "OK" +// @Success 201 {string} string "OK" // @Failure 400 {object} Error // @Param data body UnpinMessageInGroupRequest true "Unpin" // @Param number path string true "Registered Phone Number" @@ -2426,7 +2426,7 @@ func (a *Api) SubmitRateLimitChallenge(c *gin.Context) { // @Produce json // @Param number path string true "Registered Phone Number" // @Param data body UpdateAccountSettingsRequest true "Request" -// @Success 204 +// @Success 201 // @Failure 400 {object} Error // @Router /v1/accounts/{number}/settings [put] func (a *Api) UpdateAccountSettings(c *gin.Context) { @@ -2557,7 +2557,7 @@ func (a *Api) ListInstalledStickerPacks(c *gin.Context) { // @Accept json // @Produce json // @Param number path string true "Registered Phone Number" -// @Success 204 +// @Success 201 // @Failure 400 {object} Error // @Param data body AddStickerPackRequest true "Request" // @Router /v1/sticker-packs/{number} [post] diff --git a/src/docs/docs.go b/src/docs/docs.go index cda355d..be10280 100644 --- a/src/docs/docs.go +++ b/src/docs/docs.go @@ -2628,8 +2628,8 @@ const docTemplate = `{ "application/json" ], "responses": { - "204": { - "description": "No Content" + "201": { + "description": "Created" }, "400": { "description": "Bad Request", @@ -3938,7 +3938,7 @@ const docTemplate = `{ "application/json" ], "responses": { - "200": { + "201": { "description": "OK", "schema": { "type": "string" @@ -3990,7 +3990,7 @@ const docTemplate = `{ "application/json" ], "responses": { - "200": { + "201": { "description": "OK", "schema": { "type": "string" @@ -4929,8 +4929,8 @@ const docTemplate = `{ "application/json" ], "responses": { - "204": { - "description": "No Content" + "201": { + "description": "Created" }, "400": { "description": "Bad Request", @@ -5104,7 +5104,10 @@ const docTemplate = `{ "201": { "description": "Created", "schema": { - "$ref": "#/definitions/data.SendMessageResponse" + "items": { + "$ref": "#/definitions/data.SendMessageResponse" + }, + "type": "array" } }, "400": { diff --git a/src/docs/swagger.json b/src/docs/swagger.json index 27a3542..2b5494d 100644 --- a/src/docs/swagger.json +++ b/src/docs/swagger.json @@ -2623,8 +2623,8 @@ "application/json" ], "responses": { - "204": { - "description": "No Content" + "201": { + "description": "Created" }, "400": { "description": "Bad Request", @@ -3933,7 +3933,7 @@ "application/json" ], "responses": { - "200": { + "201": { "description": "OK", "schema": { "type": "string" @@ -3985,7 +3985,7 @@ "application/json" ], "responses": { - "200": { + "201": { "description": "OK", "schema": { "type": "string" @@ -4924,8 +4924,8 @@ "application/json" ], "responses": { - "204": { - "description": "No Content" + "201": { + "description": "Created" }, "400": { "description": "Bad Request", @@ -5099,7 +5099,10 @@ "201": { "description": "Created", "schema": { - "$ref": "#/definitions/data.SendMessageResponse" + "items": { + "$ref": "#/definitions/data.SendMessageResponse" + }, + "type": "array" } }, "400": { From 72580cdef59faa3fef0c8765dfbc47913e44f2b3 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Fri, 2 Oct 2026 17:19:00 +0200 Subject: [PATCH 08/18] fixed bug in json-rpc mode * first argument that is passed to the syscall.Exec needs to be the name of the executable. Since it was "--output=json" before, this string was used as the executable's name and not as argument. Therefore it was silently swallowed. see #892 --- src/scripts/jsonrpc2-helper.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scripts/jsonrpc2-helper.go b/src/scripts/jsonrpc2-helper.go index 9773a9b..14d6cf3 100644 --- a/src/scripts/jsonrpc2-helper.go +++ b/src/scripts/jsonrpc2-helper.go @@ -25,8 +25,6 @@ func main() { var tcpPort int64 = 6001 jsonRpc2ClientConfig.AddEntry(utils.MULTI_ACCOUNT_NUMBER, utils.JsonRpc2ClientConfigEntry{TcpPort: tcpPort}) - args := []string{"--output=json", "--config", signalCliConfigDir} - signalCliBinary := "signal-cli" signalMode := utils.GetEnv("MODE", "json-rpc") if signalMode == "json-rpc-native" { @@ -35,6 +33,8 @@ func main() { log.Fatal("The mode needs to be either 'json-rpc' or 'json-rpc-native'") } + args := []string{signalCliBinary, "--output", "json", "--config", signalCliConfigDir} + trustNewIdentitiesEnv := utils.GetEnv("JSON_RPC_TRUST_NEW_IDENTITIES", "") if trustNewIdentitiesEnv == "on-first-use" { args = append(args, []string{"--trust-new-identities", "on-first-use"}...) From a8d6bd664b78a5bb1846637c7306cfcd7ae82035 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Fri, 9 Oct 2026 15:45:45 +0200 Subject: [PATCH 09/18] redirect stdout to /dev/null * avoid spamming stdout with messages when one (or more) websocket listeners are connected and data is received. see #892 --- s6-services/signal-json-rpc/run | 1 + 1 file changed, 1 insertion(+) diff --git a/s6-services/signal-json-rpc/run b/s6-services/signal-json-rpc/run index 2da0ec7..2039486 100644 --- a/s6-services/signal-json-rpc/run +++ b/s6-services/signal-json-rpc/run @@ -7,4 +7,5 @@ if [ "$MODE" != "json-rpc" ] && [ "$MODE" != "json-rpc-native" ]; then exit 0 fi +exec 1>/dev/null exec jsonrpc2-helper From 8990c1fd69805af95952adcc58145b6a5bd804d5 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Fri, 9 Oct 2026 16:16:47 +0200 Subject: [PATCH 10/18] write jsonrpc2.yml atomically * previously, the `jsonrpc2.yml` file was written with `ioutil.WriteFile`, which doesn't write the file automatically. That means, if the signal-cli config directory resides on a slow (network) storage, it's possible that the file is written partially, when the signal-cli-rest-api process attemps to read it. In order to avoid that, write the contents of the `jsonrpc2.yml` to a tmp file first and then rename it with `os.Rename` afterwards. see #893 --- src/utils/api_config.go | 43 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/utils/api_config.go b/src/utils/api_config.go index 85abeb6..be02ba9 100644 --- a/src/utils/api_config.go +++ b/src/utils/api_config.go @@ -2,11 +2,50 @@ package utils import ( "errors" - "gopkg.in/yaml.v2" "io/ioutil" "os" + "path/filepath" + + "gopkg.in/yaml.v2" ) +func writeFileAtomic(name string, data []byte, perm os.FileMode) error { + dir := filepath.Dir(name) + + f, err := os.CreateTemp(dir, ".tmp-*") + if err != nil { + return err + } + + tmp := f.Name() + defer os.Remove(tmp) + + err = f.Chmod(perm) + if err != nil { + f.Close() + return err + } + + _, err = f.Write(data) + if err != nil { + f.Close() + return err + } + + err = f.Sync() + if err != nil { + f.Close() + return err + } + + err = f.Close() + if err != nil { + return err + } + + return os.Rename(tmp, name) +} + type SignalCliTrustMode int const ( @@ -92,5 +131,5 @@ func (c *SignalCliApiConfig) Persist() error { return err } - return ioutil.WriteFile(c.path, out, 0644) + return writeFileAtomic(c.path, out, 0644) } From 39489e3ad65dea66e6fdef05f83a721b8aeb4fd0 Mon Sep 17 00:00:00 2001 From: Bernhard B Date: Fri, 9 Oct 2026 16:34:48 +0200 Subject: [PATCH 11/18] updated signal-cli to v0.14.9 --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9a961de..de5414e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,5 @@ -ARG SIGNAL_CLI_VERSION=0.14.8 -ARG LIBSIGNAL_CLIENT_VERSION=0.102.1 +ARG SIGNAL_CLI_VERSION=0.14.9 +ARG LIBSIGNAL_CLIENT_VERSION=0.103.0 ARG SWAG_VERSION=1.16.4 From 68ca0107f9c69adfccc4762dc1ae27120359f996 Mon Sep 17 00:00:00 2001 From: Mykola Balakin Date: Tue, 13 Oct 2026 22:44:13 +0300 Subject: [PATCH 12/18] Fix double subscription in the on-start receive mode --- src/client/client.go | 6 ++-- src/client/jsonrpc2.go | 74 +++++++++++++++++++----------------------- src/main.go | 4 ++- 3 files changed, 40 insertions(+), 44 deletions(-) diff --git a/src/client/client.go b/src/client/client.go index 276eed6..1df7b47 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -406,10 +406,11 @@ type SignalClient struct { signalCliApiConfig *utils.SignalCliApiConfig cliClient *CliClient receiveWebhookUrl string + receiveManualMode bool } func NewSignalClient(signalCliConfig string, attachmentTmpDir string, avatarTmpDir string, signalCliMode SignalCliMode, - jsonRpc2ClientConfigPath string, signalCliApiConfigPath string, receiveWebhookUrl string) *SignalClient { + jsonRpc2ClientConfigPath string, signalCliApiConfigPath string, receiveWebhookUrl string, receiveManualMode bool) *SignalClient { return &SignalClient{ signalCliConfig: signalCliConfig, attachmentTmpDir: attachmentTmpDir, @@ -419,6 +420,7 @@ func NewSignalClient(signalCliConfig string, attachmentTmpDir string, avatarTmpD jsonRpc2Clients: make(map[string]*JsonRpc2Client), signalCliApiConfigPath: signalCliApiConfigPath, receiveWebhookUrl: receiveWebhookUrl, + receiveManualMode: receiveManualMode, } } @@ -442,7 +444,7 @@ func (s *SignalClient) Init(maxRetries int) error { tcpPortsNumberMapping := s.jsonRpc2ClientConfig.GetTcpPortsForNumbers() for number, tcpPort := range tcpPortsNumberMapping { - s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number) + s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number, s.receiveManualMode) err := s.jsonRpc2Clients[number].Dial("127.0.0.1:"+strconv.FormatInt(tcpPort, 10), maxRetries) if err != nil { return err diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 93f171a..69935eb 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -66,23 +66,25 @@ type receiveSubscription struct { } type JsonRpc2Client struct { - conn net.Conn - receivedResponsesById map[string]chan JsonRpc2MessageResponse - receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage - receiveSubscriptions map[string]*receiveSubscription // account -> sub state - channelAccountByUuid map[string]string // channelUuid -> account - signalCliApiConfig *utils.SignalCliApiConfig - number string - receivedMessagesMutex sync.Mutex - receivedResponsesMutex sync.Mutex - receiveSubscriptionsMutex sync.Mutex - address string + conn net.Conn + receivedResponsesById map[string]chan JsonRpc2MessageResponse + receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage + receiveSubscriptions map[string]*receiveSubscription // account -> sub state + channelAccountByUuid map[string]string // channelUuid -> account + signalCliApiConfig *utils.SignalCliApiConfig + number string + manualReceiveMode bool + receivedMessagesMutex sync.Mutex + receivedResponsesMutex sync.Mutex + receiveSubscriptionsMutex sync.Mutex + address string } -func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client { +func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string, manualRcpReceiveMode bool) *JsonRpc2Client { return &JsonRpc2Client{ signalCliApiConfig: signalCliApiConfig, number: number, + manualReceiveMode: manualRcpReceiveMode, receivedResponsesById: make(map[string]chan JsonRpc2MessageResponse), receivedMessagesChannels: make(map[string]chan JsonRpc2ReceivedMessage), receiveSubscriptions: make(map[string]*receiveSubscription), @@ -301,11 +303,6 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) { } } -// subscribeReceive starts receiving messages for an account by calling -// signal-cli's subscribeReceive JSON-RPC method. Only relevant when -// signal-cli was launched with --receive-mode=manual; in auto mode the -// daemon pushes notifications without an explicit subscribe call. -// Returns the subscription id assigned by signal-cli. func (r *JsonRpc2Client) subscribeReceive(account string) (int64, error) { type subscribeReceiveArgs struct{} resultStr, err := r.getRaw("subscribeReceive", &account, subscribeReceiveArgs{}) @@ -319,8 +316,6 @@ func (r *JsonRpc2Client) subscribeReceive(account string) (int64, error) { return subscriptionId, nil } -// unsubscribeReceive cancels a manual-mode subscription previously -// returned by subscribeReceive. func (r *JsonRpc2Client) unsubscribeReceive(account string, subscriptionId int64) error { type unsubscribeReceiveArgs struct { Subscription int64 `json:"subscription"` @@ -329,10 +324,6 @@ func (r *JsonRpc2Client) unsubscribeReceive(account string, subscriptionId int64 return err } -// acquireReceiveSubscription ensures an active manual-mode subscription -// exists for the given account, refcounting concurrent websocket -// subscribers. The first caller for an account triggers a real -// subscribeReceive RPC; subsequent callers just bump the refcount. func (r *JsonRpc2Client) acquireReceiveSubscription(account string) error { r.receiveSubscriptionsMutex.Lock() defer r.receiveSubscriptionsMutex.Unlock() @@ -341,45 +332,42 @@ func (r *JsonRpc2Client) acquireReceiveSubscription(account string) error { sub.refcount++ return nil } + id, err := r.subscribeReceive(account) if err != nil { return err } + r.receiveSubscriptions[account] = &receiveSubscription{id: id, refcount: 1} log.Infof("Subscribed to receive notifications for account %s (subscription=%d)", account, id) return nil } -// releaseReceiveSubscription decrements the per-account refcount and -// cancels the subscription with signal-cli if it drops to zero. -func (r *JsonRpc2Client) releaseReceiveSubscription(account string) { +func (r *JsonRpc2Client) releaseReceiveSubscription(account string) error { r.receiveSubscriptionsMutex.Lock() defer r.receiveSubscriptionsMutex.Unlock() sub, ok := r.receiveSubscriptions[account] if !ok { - return + return nil } + sub.refcount-- if sub.refcount > 0 { - return + return nil } - if err := r.unsubscribeReceive(account, sub.id); err != nil { + + err := r.unsubscribeReceive(account, sub.id) + if err != nil { log.Warnf("unsubscribeReceive failed for account %s (subscription=%d): %s", account, sub.id, err.Error()) } else { log.Infof("Unsubscribed from receive notifications for account %s (subscription=%d)", account, sub.id) } + delete(r.receiveSubscriptions, account) + return err } -// GetReceiveChannel returns a channel that will receive messages for the -// given account. If signal-cli is in manual receive-mode, it also acquires -// a subscription so that signal-cli starts forwarding messages for the -// account; in auto mode, account is unused (notifications flow regardless). -// -// account may be empty when the caller does not need a subscription -// (e.g. legacy callers in auto mode); in that case no subscribeReceive -// RPC is issued. func (r *JsonRpc2Client) GetReceiveChannel(account string) (chan JsonRpc2ReceivedMessage, string, error) { c := make(chan JsonRpc2ReceivedMessage, 64) @@ -388,8 +376,10 @@ func (r *JsonRpc2Client) GetReceiveChannel(account string) (chan JsonRpc2Receive return c, "", err } - if err := r.acquireReceiveSubscription(account); err != nil { - return c, "", fmt.Errorf("subscribeReceive failed for account %s: %w", account, err) + if r.manualReceiveMode { + if err := r.acquireReceiveSubscription(account); err != nil { + return c, "", fmt.Errorf("Couldn't create subscription for account %s: %w", account, err) + } } r.receivedMessagesMutex.Lock() @@ -407,7 +397,9 @@ func (r *JsonRpc2Client) RemoveReceiveChannel(channelUuid string) { delete(r.channelAccountByUuid, channelUuid) r.receivedMessagesMutex.Unlock() - if account != "" { - r.releaseReceiveSubscription(account) + if r.manualReceiveMode { + if err := r.releaseReceiveSubscription(account); err != nil { + log.Errorf("Couldn't release subscription for account %s: %s", account, err.Error()) + } } } diff --git a/src/main.go b/src/main.go index ede0cc1..bbeb8fa 100644 --- a/src/main.go +++ b/src/main.go @@ -156,6 +156,8 @@ func main() { } } + signalCliManualReceive := utils.GetEnv("JSON_RPC_RECEIVE_MODE", "") == "manual" + webhookUrl := utils.GetEnv("RECEIVE_WEBHOOK_URL", "") if webhookUrl != "" && signalCliMode != client.JsonRpc { log.Fatal("Env variable RECEIVE_WEBHOOK_URL can only be used with mode json-rpc!") @@ -163,7 +165,7 @@ func main() { jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.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, signalCliManualReceive) err = signalClient.Init(60) if err != nil { log.Fatal("Couldn't init Signal Client: ", err.Error()) From 955f78f0839a05b0af07c053a93732f353547d10 Mon Sep 17 00:00:00 2001 From: Mykola Balakin Date: Tue, 13 Oct 2026 23:01:54 +0300 Subject: [PATCH 13/18] Parse the payload as manual only in manual mode --- src/client/jsonrpc2.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 69935eb..15afadf 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -259,12 +259,14 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) { // the envelope directly. Unwrap so the broadcast format is // the same in both modes and downstream consumers (e.g. the // websocket handler) don't have to know which mode is in use. - var manualWrapper struct { - Subscription int64 `json:"subscription"` - Result json.RawMessage `json:"result"` - } - if err := json.Unmarshal(resp1.Params, &manualWrapper); err == nil && len(manualWrapper.Result) > 0 { - resp1.Params = manualWrapper.Result + if r.manualReceiveMode { + var manualWrapper struct { + Subscription int64 `json:"subscription"` + Result json.RawMessage `json:"result"` + } + if err := json.Unmarshal(resp1.Params, &manualWrapper); err == nil && len(manualWrapper.Result) > 0 { + resp1.Params = manualWrapper.Result + } } r.receivedMessagesMutex.Lock() From 53fa03881f61215ea27b0d7d195b711f6e542e2a Mon Sep 17 00:00:00 2001 From: Mykola Balakin Date: Wed, 14 Oct 2026 17:00:53 +0300 Subject: [PATCH 14/18] Minor style fixes --- src/client/jsonrpc2.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 15afadf..9c8c6de 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -69,8 +69,8 @@ type JsonRpc2Client struct { conn net.Conn receivedResponsesById map[string]chan JsonRpc2MessageResponse receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage - receiveSubscriptions map[string]*receiveSubscription // account -> sub state - channelAccountByUuid map[string]string // channelUuid -> account + receiveSubscriptions map[string]*receiveSubscription + accountsByChannelUuid map[string]string signalCliApiConfig *utils.SignalCliApiConfig number string manualReceiveMode bool @@ -88,7 +88,7 @@ func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number stri receivedResponsesById: make(map[string]chan JsonRpc2MessageResponse), receivedMessagesChannels: make(map[string]chan JsonRpc2ReceivedMessage), receiveSubscriptions: make(map[string]*receiveSubscription), - channelAccountByUuid: make(map[string]string), + accountsByChannelUuid: make(map[string]string), } } @@ -313,7 +313,7 @@ func (r *JsonRpc2Client) subscribeReceive(account string) (int64, error) { } var subscriptionId int64 if err := json.Unmarshal([]byte(resultStr), &subscriptionId); err != nil { - return 0, fmt.Errorf("subscribeReceive: couldn't parse subscription id from %q: %w", resultStr, err) + return 0, err } return subscriptionId, nil } @@ -361,7 +361,7 @@ func (r *JsonRpc2Client) releaseReceiveSubscription(account string) error { err := r.unsubscribeReceive(account, sub.id) if err != nil { - log.Warnf("unsubscribeReceive failed for account %s (subscription=%d): %s", account, sub.id, err.Error()) + log.Warnf("Couldn't unsubscribed from receive notifications for account %s (subscription=%d): %s", account, sub.id, err.Error()) } else { log.Infof("Unsubscribed from receive notifications for account %s (subscription=%d)", account, sub.id) } @@ -386,7 +386,7 @@ func (r *JsonRpc2Client) GetReceiveChannel(account string) (chan JsonRpc2Receive r.receivedMessagesMutex.Lock() r.receivedMessagesChannels[channelUuid.String()] = c - r.channelAccountByUuid[channelUuid.String()] = account + r.accountsByChannelUuid[channelUuid.String()] = account r.receivedMessagesMutex.Unlock() return c, channelUuid.String(), nil @@ -395,8 +395,8 @@ func (r *JsonRpc2Client) GetReceiveChannel(account string) (chan JsonRpc2Receive func (r *JsonRpc2Client) RemoveReceiveChannel(channelUuid string) { r.receivedMessagesMutex.Lock() delete(r.receivedMessagesChannels, channelUuid) - account := r.channelAccountByUuid[channelUuid] - delete(r.channelAccountByUuid, channelUuid) + account := r.accountsByChannelUuid[channelUuid] + delete(r.accountsByChannelUuid, channelUuid) r.receivedMessagesMutex.Unlock() if r.manualReceiveMode { From 801b015516edbb942efd167c10f7d9706a117a56 Mon Sep 17 00:00:00 2001 From: Mykola Balakin Date: Sat, 17 Oct 2026 15:49:53 +0300 Subject: [PATCH 15/18] Refactor JSON RPC receive mode into an enum --- src/client/client.go | 15 +++++++++++---- src/client/jsonrpc2.go | 12 ++++++------ src/main.go | 12 ++++++++++-- 3 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/client/client.go b/src/client/client.go index 1df7b47..c892fd4 100644 --- a/src/client/client.go +++ b/src/client/client.go @@ -53,6 +53,13 @@ const ( JsonRpc ) +type JsonRpcReceiveMode int + +const ( + OnStart JsonRpcReceiveMode = iota + 1 + Manual +) + type GroupLinkState int const ( @@ -406,11 +413,11 @@ type SignalClient struct { signalCliApiConfig *utils.SignalCliApiConfig cliClient *CliClient receiveWebhookUrl string - receiveManualMode bool + jsonRpcReceiveMode JsonRpcReceiveMode } func NewSignalClient(signalCliConfig string, attachmentTmpDir string, avatarTmpDir string, signalCliMode SignalCliMode, - jsonRpc2ClientConfigPath string, signalCliApiConfigPath string, receiveWebhookUrl string, receiveManualMode bool) *SignalClient { + jsonRpc2ClientConfigPath string, signalCliApiConfigPath string, receiveWebhookUrl string, jsonRpcReceiveMode JsonRpcReceiveMode) *SignalClient { return &SignalClient{ signalCliConfig: signalCliConfig, attachmentTmpDir: attachmentTmpDir, @@ -420,7 +427,7 @@ func NewSignalClient(signalCliConfig string, attachmentTmpDir string, avatarTmpD jsonRpc2Clients: make(map[string]*JsonRpc2Client), signalCliApiConfigPath: signalCliApiConfigPath, receiveWebhookUrl: receiveWebhookUrl, - receiveManualMode: receiveManualMode, + jsonRpcReceiveMode: jsonRpcReceiveMode, } } @@ -444,7 +451,7 @@ func (s *SignalClient) Init(maxRetries int) error { tcpPortsNumberMapping := s.jsonRpc2ClientConfig.GetTcpPortsForNumbers() for number, tcpPort := range tcpPortsNumberMapping { - s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number, s.receiveManualMode) + s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number, s.jsonRpcReceiveMode) err := s.jsonRpc2Clients[number].Dial("127.0.0.1:"+strconv.FormatInt(tcpPort, 10), maxRetries) if err != nil { return err diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index 9c8c6de..f6876be 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -73,18 +73,18 @@ type JsonRpc2Client struct { accountsByChannelUuid map[string]string signalCliApiConfig *utils.SignalCliApiConfig number string - manualReceiveMode bool + jsonRpcReceiveMode JsonRpcReceiveMode receivedMessagesMutex sync.Mutex receivedResponsesMutex sync.Mutex receiveSubscriptionsMutex sync.Mutex address string } -func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string, manualRcpReceiveMode bool) *JsonRpc2Client { +func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string, jsonRpcReceiveMode JsonRpcReceiveMode) *JsonRpc2Client { return &JsonRpc2Client{ signalCliApiConfig: signalCliApiConfig, number: number, - manualReceiveMode: manualRcpReceiveMode, + jsonRpcReceiveMode: jsonRpcReceiveMode, receivedResponsesById: make(map[string]chan JsonRpc2MessageResponse), receivedMessagesChannels: make(map[string]chan JsonRpc2ReceivedMessage), receiveSubscriptions: make(map[string]*receiveSubscription), @@ -259,7 +259,7 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) { // the envelope directly. Unwrap so the broadcast format is // the same in both modes and downstream consumers (e.g. the // websocket handler) don't have to know which mode is in use. - if r.manualReceiveMode { + if r.jsonRpcReceiveMode == Manual { var manualWrapper struct { Subscription int64 `json:"subscription"` Result json.RawMessage `json:"result"` @@ -378,7 +378,7 @@ func (r *JsonRpc2Client) GetReceiveChannel(account string) (chan JsonRpc2Receive return c, "", err } - if r.manualReceiveMode { + if r.jsonRpcReceiveMode == Manual { if err := r.acquireReceiveSubscription(account); err != nil { return c, "", fmt.Errorf("Couldn't create subscription for account %s: %w", account, err) } @@ -399,7 +399,7 @@ func (r *JsonRpc2Client) RemoveReceiveChannel(channelUuid string) { delete(r.accountsByChannelUuid, channelUuid) r.receivedMessagesMutex.Unlock() - if r.manualReceiveMode { + if r.jsonRpcReceiveMode == Manual { if err := r.releaseReceiveSubscription(account); err != nil { log.Errorf("Couldn't release subscription for account %s: %s", account, err.Error()) } diff --git a/src/main.go b/src/main.go index bbeb8fa..ee8cf0c 100644 --- a/src/main.go +++ b/src/main.go @@ -156,7 +156,15 @@ func main() { } } - signalCliManualReceive := utils.GetEnv("JSON_RPC_RECEIVE_MODE", "") == "manual" + jsonRpcReceiveMode := client.OnStart + jsonRpcReceiveModeValue := utils.GetEnv("JSON_RPC_RECEIVE_MODE", "") + if jsonRpcReceiveModeValue == "on-start" { + jsonRpcReceiveMode = client.OnStart + } else if jsonRpcReceiveModeValue == "manual" { + jsonRpcReceiveMode = client.Manual + } else if jsonRpcReceiveModeValue != "" { + log.Fatal("Invalid JSON_RPC_RECEIVE_MODE environment variable set!") + } webhookUrl := utils.GetEnv("RECEIVE_WEBHOOK_URL", "") if webhookUrl != "" && signalCliMode != client.JsonRpc { @@ -165,7 +173,7 @@ func main() { jsonRpc2ClientConfigPathPath := *signalCliConfig + "/jsonrpc2.yml" signalCliApiConfigPath := *signalCliConfig + "/api-config.yml" - signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath, signalCliApiConfigPath, webhookUrl, signalCliManualReceive) + signalClient := client.NewSignalClient(*signalCliConfig, *attachmentTmpDir, *avatarTmpDir, signalCliMode, jsonRpc2ClientConfigPathPath, signalCliApiConfigPath, webhookUrl, jsonRpcReceiveMode) err = signalClient.Init(60) if err != nil { log.Fatal("Couldn't init Signal Client: ", err.Error()) From 2cb7ad4fb1a4867480ece3a6a73554393b2a71a3 Mon Sep 17 00:00:00 2001 From: Mykola Balakin Date: Sat, 17 Oct 2026 19:23:35 +0300 Subject: [PATCH 16/18] Reduce the level of unimportant logs --- src/client/jsonrpc2.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index f6876be..ceed337 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -341,7 +341,7 @@ func (r *JsonRpc2Client) acquireReceiveSubscription(account string) error { } r.receiveSubscriptions[account] = &receiveSubscription{id: id, refcount: 1} - log.Infof("Subscribed to receive notifications for account %s (subscription=%d)", account, id) + log.Debugf("Subscribed to receive notifications for account %s (subscription=%d)", account, id) return nil } @@ -363,7 +363,7 @@ func (r *JsonRpc2Client) releaseReceiveSubscription(account string) error { if err != nil { log.Warnf("Couldn't unsubscribed from receive notifications for account %s (subscription=%d): %s", account, sub.id, err.Error()) } else { - log.Infof("Unsubscribed from receive notifications for account %s (subscription=%d)", account, sub.id) + log.Debugf("Unsubscribed from receive notifications for account %s (subscription=%d)", account, sub.id) } delete(r.receiveSubscriptions, account) From 090d9b135ec5c40e0484daf4213fd99d24b07489 Mon Sep 17 00:00:00 2001 From: Mykola Balakin Date: Sat, 17 Oct 2026 19:24:17 +0300 Subject: [PATCH 17/18] Shorten the critical section --- src/client/jsonrpc2.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/client/jsonrpc2.go b/src/client/jsonrpc2.go index ceed337..0fd0093 100644 --- a/src/client/jsonrpc2.go +++ b/src/client/jsonrpc2.go @@ -347,26 +347,30 @@ func (r *JsonRpc2Client) acquireReceiveSubscription(account string) error { func (r *JsonRpc2Client) releaseReceiveSubscription(account string) error { r.receiveSubscriptionsMutex.Lock() - defer r.receiveSubscriptionsMutex.Unlock() sub, ok := r.receiveSubscriptions[account] if !ok { + r.receiveSubscriptionsMutex.Unlock() return nil } sub.refcount-- if sub.refcount > 0 { + r.receiveSubscriptionsMutex.Unlock() return nil } + delete(r.receiveSubscriptions, account) err := r.unsubscribeReceive(account, sub.id) + + r.receiveSubscriptionsMutex.Unlock() + if err != nil { log.Warnf("Couldn't unsubscribed from receive notifications for account %s (subscription=%d): %s", account, sub.id, err.Error()) } else { log.Debugf("Unsubscribed from receive notifications for account %s (subscription=%d)", account, sub.id) } - delete(r.receiveSubscriptions, account) return err } From d6e1ff2da19da324f68530078cdeb64734ecf114 Mon Sep 17 00:00:00 2001 From: Mykola Balakin Date: Sat, 17 Oct 2026 19:24:40 +0300 Subject: [PATCH 18/18] Exit if JSON_RCP_RECEIVE_MODE is set in non-JSON-RPC mode --- src/main.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.go b/src/main.go index ee8cf0c..548d39b 100644 --- a/src/main.go +++ b/src/main.go @@ -158,7 +158,9 @@ func main() { jsonRpcReceiveMode := client.OnStart jsonRpcReceiveModeValue := utils.GetEnv("JSON_RPC_RECEIVE_MODE", "") - if jsonRpcReceiveModeValue == "on-start" { + if jsonRpcReceiveModeValue != "" && signalCliMode != client.JsonRpc { + log.Fatal("Env variable JSON_RPC_RECEIVE_MODE can't be used with normal or native mode") + } else if jsonRpcReceiveModeValue == "on-start" { jsonRpcReceiveMode = client.OnStart } else if jsonRpcReceiveModeValue == "manual" { jsonRpcReceiveMode = client.Manual