mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-26 14:44:15 +00:00
Compare commits
8 Commits
2675f013e9
...
39c850bcdb
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39c850bcdb | ||
|
|
db63fd15e0 | ||
|
|
650367e88a | ||
|
|
69457e8f81 | ||
|
|
2e8171d84c | ||
|
|
419b18331d | ||
|
|
e98c03e702 | ||
|
|
ad0c231032 |
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@ -8,6 +8,8 @@ on:
|
|||||||
branches:
|
branches:
|
||||||
- '**' #every branch
|
- '**' #every branch
|
||||||
|
|
||||||
|
permissions: {}
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
setup:
|
setup:
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
@ -25,7 +27,7 @@ jobs:
|
|||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
needs: setup
|
needs: setup
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@master
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.ref }}
|
ref: ${{ github.ref }}
|
||||||
- name: Login to Docker Hub
|
- name: Login to Docker Hub
|
||||||
|
|||||||
6
.github/workflows/release-dev-version.yml
vendored
6
.github/workflows/release-dev-version.yml
vendored
@ -4,9 +4,11 @@ on:
|
|||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
version:
|
version:
|
||||||
description: 'Version'
|
description: 'Version'
|
||||||
required: true
|
required: true
|
||||||
|
|
||||||
|
permissions: {}
|
||||||
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
setup:
|
setup:
|
||||||
@ -24,7 +26,7 @@ jobs:
|
|||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
needs: setup
|
needs: setup
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@master
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.ref }}
|
ref: ${{ github.ref }}
|
||||||
- name: Login to Docker Hub
|
- name: Login to Docker Hub
|
||||||
|
|||||||
@ -4,9 +4,11 @@ on:
|
|||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
version:
|
version:
|
||||||
description: 'Version'
|
description: 'Version'
|
||||||
required: true
|
required: true
|
||||||
|
|
||||||
|
permissions: {}
|
||||||
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
setup:
|
setup:
|
||||||
@ -24,7 +26,7 @@ jobs:
|
|||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
needs: setup
|
needs: setup
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@master
|
- uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.ref }}
|
ref: ${{ github.ref }}
|
||||||
- name: Login to Docker Hub
|
- name: Login to Docker Hub
|
||||||
|
|||||||
68
Dockerfile
68
Dockerfile
@ -1,9 +1,7 @@
|
|||||||
ARG SIGNAL_CLI_VERSION=0.14.3
|
ARG SIGNAL_CLI_VERSION=0.14.3
|
||||||
ARG LIBSIGNAL_CLIENT_VERSION=0.92.1
|
ARG LIBSIGNAL_CLIENT_VERSION=0.92.1
|
||||||
ARG SIGNAL_CLI_NATIVE_PACKAGE_VERSION=0.14.3+morph027+1
|
|
||||||
|
|
||||||
ARG SWAG_VERSION=1.16.4
|
ARG SWAG_VERSION=1.16.4
|
||||||
ARG GRAALVM_VERSION=25.0.2
|
|
||||||
|
|
||||||
ARG BUILD_VERSION_ARG=unset
|
ARG BUILD_VERSION_ARG=unset
|
||||||
|
|
||||||
@ -12,9 +10,7 @@ FROM golang:1.26-trixie AS buildcontainer
|
|||||||
ARG SIGNAL_CLI_VERSION
|
ARG SIGNAL_CLI_VERSION
|
||||||
ARG LIBSIGNAL_CLIENT_VERSION
|
ARG LIBSIGNAL_CLIENT_VERSION
|
||||||
ARG SWAG_VERSION
|
ARG SWAG_VERSION
|
||||||
ARG GRAALVM_VERSION
|
|
||||||
ARG BUILD_VERSION_ARG
|
ARG BUILD_VERSION_ARG
|
||||||
ARG SIGNAL_CLI_NATIVE_PACKAGE_VERSION
|
|
||||||
|
|
||||||
RUN dpkg-reconfigure debconf --frontend=noninteractive \
|
RUN dpkg-reconfigure debconf --frontend=noninteractive \
|
||||||
&& apt-get update \
|
&& apt-get update \
|
||||||
@ -23,8 +19,6 @@ RUN dpkg-reconfigure debconf --frontend=noninteractive \
|
|||||||
file build-essential libz-dev zlib1g-dev binutils \
|
file build-essential libz-dev zlib1g-dev binutils \
|
||||||
&& rm -rf /var/lib/apt/lists/*
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
COPY ext/libraries/libsignal-client/signal-cli-native.patch /tmp/signal-cli-native.patch
|
|
||||||
|
|
||||||
#COPY ext/libraries/libsignal-client/v${LIBSIGNAL_CLIENT_VERSION} /tmp/libsignal-client-libraries
|
#COPY ext/libraries/libsignal-client/v${LIBSIGNAL_CLIENT_VERSION} /tmp/libsignal-client-libraries
|
||||||
RUN wget https://github.com/bbernhard/libsignal-client-builds/releases/download/v${LIBSIGNAL_CLIENT_VERSION}/libsignal-client-build-v${LIBSIGNAL_CLIENT_VERSION}.tar.gz -O /tmp/libsignal-client.tar.gz
|
RUN wget https://github.com/bbernhard/libsignal-client-builds/releases/download/v${LIBSIGNAL_CLIENT_VERSION}/libsignal-client-build-v${LIBSIGNAL_CLIENT_VERSION}.tar.gz -O /tmp/libsignal-client.tar.gz
|
||||||
RUN cd /tmp && mkdir -p /tmp/libsignal-client-libraries && tar xf libsignal-client.tar.gz && mv x86-64 armv7 arm64 -t libsignal-client-libraries
|
RUN cd /tmp && mkdir -p /tmp/libsignal-client-libraries && tar xf libsignal-client.tar.gz && mv x86-64 armv7 arm64 -t libsignal-client-libraries
|
||||||
@ -46,71 +40,27 @@ ENV JAVA_OPTS="-Djdk.lang.Process.launchMechanism=vfork"
|
|||||||
|
|
||||||
ENV LANG en_US.UTF-8
|
ENV LANG en_US.UTF-8
|
||||||
|
|
||||||
#RUN cd /tmp/ \
|
|
||||||
# && git clone https://github.com/swaggo/swag.git swag-${SWAG_VERSION} \
|
|
||||||
# && cd swag-${SWAG_VERSION} \
|
|
||||||
# && git checkout -q v${SWAG_VERSION} \
|
|
||||||
# && make -s < /dev/null > /dev/null \
|
|
||||||
# && cp /tmp/swag-${SWAG_VERSION}/swag /usr/bin/swag \
|
|
||||||
# && rm -r /tmp/swag-${SWAG_VERSION}
|
|
||||||
|
|
||||||
|
|
||||||
RUN go install github.com/swaggo/swag/cmd/swag@v${SWAG_VERSION}
|
RUN go install github.com/swaggo/swag/cmd/swag@v${SWAG_VERSION}
|
||||||
|
|
||||||
|
|
||||||
RUN cd /tmp/ \
|
RUN cd /tmp/ \
|
||||||
&& wget -nv https://github.com/AsamK/signal-cli/releases/download/v${SIGNAL_CLI_VERSION}/signal-cli-${SIGNAL_CLI_VERSION}.tar.gz -O /tmp/signal-cli.tar.gz \
|
&& wget -nv https://github.com/AsamK/signal-cli/releases/download/v${SIGNAL_CLI_VERSION}/signal-cli-${SIGNAL_CLI_VERSION}.tar.gz -O /tmp/signal-cli.tar.gz \
|
||||||
&& tar xf signal-cli.tar.gz
|
&& tar xf signal-cli.tar.gz
|
||||||
|
|
||||||
# build native image with graalvm
|
|
||||||
|
|
||||||
RUN arch="$(uname -m)"; \
|
|
||||||
case "$arch" in \
|
|
||||||
aarch64) wget -nv https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-${GRAALVM_VERSION}/graalvm-community-jdk-${GRAALVM_VERSION}_linux-aarch64_bin.tar.gz -O /tmp/gvm.tar.gz ;; \
|
|
||||||
armv7l) echo "GRAALVM doesn't support 32bit" ;; \
|
|
||||||
x86_64) wget -nv https://github.com/graalvm/graalvm-ce-builds/releases/download/jdk-${GRAALVM_VERSION}/graalvm-community-jdk-${GRAALVM_VERSION}_linux-x64_bin.tar.gz -O /tmp/gvm.tar.gz ;; \
|
|
||||||
*) echo "Invalid architecture" ;; \
|
|
||||||
esac;
|
|
||||||
|
|
||||||
RUN if [ "$(uname -m)" = "x86_64" ]; then \
|
RUN if [ "$(uname -m)" = "x86_64" ]; then \
|
||||||
cd /tmp \
|
cd /tmp \
|
||||||
&& git clone https://github.com/AsamK/signal-cli.git signal-cli-${SIGNAL_CLI_VERSION}-source \
|
&& wget https://github.com/bbernhard/signal-cli-native-builds/releases/download/v${SIGNAL_CLI_VERSION}/signal-cli-native-v${SIGNAL_CLI_VERSION}.tar.gz \
|
||||||
&& cd signal-cli-${SIGNAL_CLI_VERSION}-source \
|
&& tar xvf signal-cli-native-v${SIGNAL_CLI_VERSION}.tar.gz \
|
||||||
&& git checkout -q v${SIGNAL_CLI_VERSION} \
|
&& cp signal-cli-native-v${SIGNAL_CLI_VERSION}/x86-64/signal-cli-native /tmp/signal-cli-native; \
|
||||||
&& cd /tmp && mkdir -p /tmp/graalvm && tar xf gvm.tar.gz -C /tmp/graalvm --strip-components=1 \
|
|
||||||
&& export GRAALVM_HOME=/tmp/graalvm \
|
|
||||||
&& export PATH=/tmp/graalvm/bin:$PATH \
|
|
||||||
&& cd /tmp/signal-cli-${SIGNAL_CLI_VERSION}-source \
|
|
||||||
&& sed -i 's/Signal-Android\/5.22.3/Signal-Android\/5.51.7/g' src/main/java/org/asamk/signal/BaseConfig.java \
|
|
||||||
&& ./gradlew build \
|
|
||||||
&& ./gradlew installDist \
|
|
||||||
&& ls build/install/signal-cli/lib/libsignal-client-${LIBSIGNAL_CLIENT_VERSION}.jar || (echo "\n\nsignal-client jar file with version ${LIBSIGNAL_CLIENT_VERSION} not found. Maybe the version needs to be bumped in the signal-cli-rest-api Dockerfile?\n\n" && echo "Available version: \n" && ls build/install/signal-cli/lib/libsignal-client-* && echo "\n\n" && exit 1) \
|
|
||||||
&& cd /tmp \
|
|
||||||
&& cp signal-cli-${SIGNAL_CLI_VERSION}-source/build/install/signal-cli/lib/libsignal-client-${LIBSIGNAL_CLIENT_VERSION}.jar libsignal-client.jar \
|
|
||||||
&& zip -qu libsignal-client.jar libsignal_jni.so \
|
|
||||||
&& cd /tmp/signal-cli-${SIGNAL_CLI_VERSION}-source \
|
|
||||||
&& git apply /tmp/signal-cli-native.patch \
|
|
||||||
&& ./gradlew -q nativeCompile; \
|
|
||||||
elif [ "$(uname -m)" = "aarch64" ] ; then \
|
elif [ "$(uname -m)" = "aarch64" ] ; then \
|
||||||
echo "Use native image from @morph027 (https://packaging.gitlab.io/signal-cli/) for arm64 - many thanks to @morph027" \
|
cd /tmp \
|
||||||
&& curl -fsSL https://packaging.gitlab.io/signal-cli/gpg.key | gpg -o /usr/share/keyrings/signal-cli-native.pgp --dearmor \
|
&& wget https://github.com/bbernhard/signal-cli-native-builds/releases/download/v${SIGNAL_CLI_VERSION}/signal-cli-native-v${SIGNAL_CLI_VERSION}.tar.gz \
|
||||||
&& echo "deb [signed-by=/usr/share/keyrings/signal-cli-native.pgp] https://packaging.gitlab.io/signal-cli signalcli main" > /etc/apt/sources.list.d/morph027-signal-cli.list \
|
&& tar xvf signal-cli-native-v${SIGNAL_CLI_VERSION}.tar.gz \
|
||||||
&& mkdir -p /tmp/signal-cli-native \
|
&& cp signal-cli-native-v${SIGNAL_CLI_VERSION}/arm64/signal-cli-native /tmp/signal-cli-native; \
|
||||||
&& cd /tmp/signal-cli-native \
|
|
||||||
#&& wget https://gitlab.com/packaging/signal-cli/-/jobs/14049119045/artifacts/download?file_type=archive -O /tmp/signal-cli-native/archive.zip \
|
|
||||||
#&& unzip archive.zip \
|
|
||||||
#&& mv signal-cli-native-arm64-trigger/*deb . \
|
|
||||||
&& apt-get update \
|
|
||||||
&& apt-get download signal-cli-native=${SIGNAL_CLI_NATIVE_PACKAGE_VERSION} \
|
|
||||||
&& ar x *.deb \
|
|
||||||
&& tar xf data.tar.gz \
|
|
||||||
&& mkdir -p /tmp/signal-cli-${SIGNAL_CLI_VERSION}-source/build/native/nativeCompile \
|
|
||||||
&& cp /tmp/signal-cli-native/usr/bin/signal-cli-native /tmp/signal-cli-${SIGNAL_CLI_VERSION}-source/build/native/nativeCompile/signal-cli; \
|
|
||||||
elif [ "$(uname -m)" = "armv7l" ] ; then \
|
elif [ "$(uname -m)" = "armv7l" ] ; then \
|
||||||
echo "GRAALVM doesn't support 32bit" \
|
echo "GRAALVM doesn't support 32bit" \
|
||||||
&& echo "Creating temporary file, otherwise the below copy doesn't work for armv7" \
|
&& echo "Creating temporary file, otherwise the below copy doesn't work for armv7" \
|
||||||
&& mkdir -p /tmp/signal-cli-${SIGNAL_CLI_VERSION}-source/build/native/nativeCompile \
|
&& mkdir -p /tmp/signal-cli-${SIGNAL_CLI_VERSION}-source/build/native/nativeCompile \
|
||||||
&& touch /tmp/signal-cli-${SIGNAL_CLI_VERSION}-source/build/native/nativeCompile/signal-cli; \
|
&& touch /tmp/signal-cli-native; \
|
||||||
else \
|
else \
|
||||||
echo "Unknown architecture"; \
|
echo "Unknown architecture"; \
|
||||||
fi;
|
fi;
|
||||||
@ -180,7 +130,7 @@ RUN dpkg-reconfigure debconf --frontend=noninteractive \
|
|||||||
|
|
||||||
COPY --from=buildcontainer /tmp/signal-cli-rest-api-src/signal-cli-rest-api /usr/bin/signal-cli-rest-api
|
COPY --from=buildcontainer /tmp/signal-cli-rest-api-src/signal-cli-rest-api /usr/bin/signal-cli-rest-api
|
||||||
COPY --from=buildcontainer /opt/signal-cli-${SIGNAL_CLI_VERSION} /opt/signal-cli-${SIGNAL_CLI_VERSION}
|
COPY --from=buildcontainer /opt/signal-cli-${SIGNAL_CLI_VERSION} /opt/signal-cli-${SIGNAL_CLI_VERSION}
|
||||||
COPY --from=buildcontainer /tmp/signal-cli-${SIGNAL_CLI_VERSION}-source/build/native/nativeCompile/signal-cli /opt/signal-cli-${SIGNAL_CLI_VERSION}/bin/signal-cli-native
|
COPY --from=buildcontainer /tmp/signal-cli-native /opt/signal-cli-${SIGNAL_CLI_VERSION}/bin/signal-cli-native
|
||||||
COPY --from=buildcontainer /tmp/signal-cli-rest-api-src/scripts/jsonrpc2-helper /usr/bin/jsonrpc2-helper
|
COPY --from=buildcontainer /tmp/signal-cli-rest-api-src/scripts/jsonrpc2-helper /usr/bin/jsonrpc2-helper
|
||||||
COPY --from=buildcontainer /tmp/signal-cli-rest-api-src/signal-cli-rest-api_plugin_loader.so /usr/bin/signal-cli-rest-api_plugin_loader.so
|
COPY --from=buildcontainer /tmp/signal-cli-rest-api-src/signal-cli-rest-api_plugin_loader.so /usr/bin/signal-cli-rest-api_plugin_loader.so
|
||||||
COPY entrypoint.sh /entrypoint.sh
|
COPY entrypoint.sh /entrypoint.sh
|
||||||
|
|||||||
@ -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_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_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_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.
|
||||||
|
|||||||
@ -578,7 +578,7 @@ func (a *Api) SendV2(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *Api) handleSignalReceive(ws *websocket.Conn, number string, stop chan struct{}) {
|
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 {
|
if err != nil {
|
||||||
log.Error("Couldn't get receive channel: ", err.Error())
|
log.Error("Couldn't get receive channel: ", err.Error())
|
||||||
return
|
return
|
||||||
|
|||||||
@ -1067,12 +1067,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()
|
jsonRpc2Client, err := s.getJsonRpc2Client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, "", err
|
return nil, "", err
|
||||||
}
|
}
|
||||||
return jsonRpc2Client.GetReceiveChannel()
|
return jsonRpc2Client.GetReceiveChannel(number)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SignalClient) RemoveReceiveChannel(channelUuid string) {
|
func (s *SignalClient) RemoveReceiveChannel(channelUuid string) {
|
||||||
|
|||||||
@ -5,6 +5,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -56,15 +57,26 @@ func (r *RateLimitErrorType) Error() string {
|
|||||||
return r.Err.Error()
|
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 {
|
type JsonRpc2Client struct {
|
||||||
conn net.Conn
|
conn net.Conn
|
||||||
receivedResponsesById map[string]chan JsonRpc2MessageResponse
|
receivedResponsesById map[string]chan JsonRpc2MessageResponse
|
||||||
receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage
|
receivedMessagesChannels map[string]chan JsonRpc2ReceivedMessage
|
||||||
signalCliApiConfig *utils.SignalCliApiConfig
|
receiveSubscriptions map[string]*receiveSubscription // account -> sub state
|
||||||
number string
|
channelAccountByUuid map[string]string // channelUuid -> account
|
||||||
receivedMessagesMutex sync.Mutex
|
signalCliApiConfig *utils.SignalCliApiConfig
|
||||||
receivedResponsesMutex sync.Mutex
|
number string
|
||||||
address string
|
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) *JsonRpc2Client {
|
||||||
@ -73,6 +85,8 @@ func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number stri
|
|||||||
number: number,
|
number: number,
|
||||||
receivedResponsesById: make(map[string]chan JsonRpc2MessageResponse),
|
receivedResponsesById: make(map[string]chan JsonRpc2MessageResponse),
|
||||||
receivedMessagesChannels: make(map[string]chan JsonRpc2ReceivedMessage),
|
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
|
var resp1 JsonRpc2ReceivedMessage
|
||||||
json.Unmarshal([]byte(str), &resp1)
|
json.Unmarshal([]byte(str), &resp1)
|
||||||
if resp1.Method == "receive" {
|
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()
|
r.receivedMessagesMutex.Lock()
|
||||||
for _, c := range r.receivedMessagesChannels {
|
for _, c := range r.receivedMessagesChannels {
|
||||||
select {
|
select {
|
||||||
@ -244,7 +271,6 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) {
|
|||||||
default:
|
default:
|
||||||
log.Debug("Couldn't send message to golang channel, as there's no receiver")
|
log.Debug("Couldn't send message to golang channel, as there's no receiver")
|
||||||
}
|
}
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
r.receivedMessagesMutex.Unlock()
|
r.receivedMessagesMutex.Unlock()
|
||||||
|
|
||||||
@ -270,16 +296,102 @@ func (r *JsonRpc2Client) ReceiveData(number string, receiveWebhookUrl string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *JsonRpc2Client) GetReceiveChannel() (chan JsonRpc2ReceivedMessage, string, error) {
|
// subscribeReceive starts receiving messages for an account by calling
|
||||||
c := make(chan JsonRpc2ReceivedMessage)
|
// 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()
|
channelUuid, err := uuid.NewV4()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c, "", err
|
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.receivedMessagesMutex.Lock()
|
||||||
r.receivedMessagesChannels[channelUuid.String()] = c
|
r.receivedMessagesChannels[channelUuid.String()] = c
|
||||||
|
r.channelAccountByUuid[channelUuid.String()] = account
|
||||||
r.receivedMessagesMutex.Unlock()
|
r.receivedMessagesMutex.Unlock()
|
||||||
|
|
||||||
return c, channelUuid.String(), nil
|
return c, channelUuid.String(), nil
|
||||||
@ -288,5 +400,11 @@ func (r *JsonRpc2Client) GetReceiveChannel() (chan JsonRpc2ReceivedMessage, stri
|
|||||||
func (r *JsonRpc2Client) RemoveReceiveChannel(channelUuid string) {
|
func (r *JsonRpc2Client) RemoveReceiveChannel(channelUuid string) {
|
||||||
r.receivedMessagesMutex.Lock()
|
r.receivedMessagesMutex.Lock()
|
||||||
delete(r.receivedMessagesChannels, channelUuid)
|
delete(r.receivedMessagesChannels, channelUuid)
|
||||||
|
account := r.channelAccountByUuid[channelUuid]
|
||||||
|
delete(r.channelAccountByUuid, channelUuid)
|
||||||
r.receivedMessagesMutex.Unlock()
|
r.receivedMessagesMutex.Unlock()
|
||||||
|
|
||||||
|
if account != "" {
|
||||||
|
r.releaseReceiveSubscription(account)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import (
|
|||||||
const supervisorctlConfigTemplate = `
|
const supervisorctlConfigTemplate = `
|
||||||
[program:%s]
|
[program:%s]
|
||||||
process_name=%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
|
autostart=true
|
||||||
autorestart=true
|
autorestart=true
|
||||||
startretries=10
|
startretries=10
|
||||||
@ -75,6 +75,22 @@ func main() {
|
|||||||
signalCliIgnoreStickers = " --ignore-stickers"
|
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"
|
supervisorctlProgramName := "signal-cli-json-rpc-1"
|
||||||
supervisorctlLogFolder := "/var/log/" + supervisorctlProgramName
|
supervisorctlLogFolder := "/var/log/" + supervisorctlProgramName
|
||||||
_, err := exec.Command("mkdir", "-p", supervisorctlLogFolder).Output()
|
_, err := exec.Command("mkdir", "-p", supervisorctlLogFolder).Output()
|
||||||
@ -100,7 +116,7 @@ func main() {
|
|||||||
supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf"
|
supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf"
|
||||||
|
|
||||||
supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName, signalCliBinary,
|
supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName, signalCliBinary,
|
||||||
signalCliConfigDir, trustNewIdentities, signalCliIgnoreAttachments, signalCliIgnoreStories,
|
signalCliConfigDir, trustNewIdentities, signalCliReceiveMode, signalCliIgnoreAttachments, signalCliIgnoreStories,
|
||||||
signalCliIgnoreAvatars, signalCliIgnoreStickers, tcpPort,
|
signalCliIgnoreAvatars, signalCliIgnoreStickers, tcpPort,
|
||||||
supervisorctlProgramName, supervisorctlProgramName)
|
supervisorctlProgramName, supervisorctlProgramName)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user