mirror of
https://github.com/bbernhard/signal-cli-rest-api.git
synced 2026-05-19 13:34:19 +00:00
Compare commits
12 Commits
263a1d31fa
...
e9e912d4a7
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9e912d4a7 | ||
|
|
5714161567 | ||
|
|
57ebd2acb8 | ||
|
|
0c081ec7cf | ||
|
|
8a75633ca9 | ||
|
|
c45b9059b5 | ||
|
|
a7f531d17f | ||
|
|
9ea4407ac2 | ||
|
|
d102da4ba2 | ||
|
|
4a2bf9182d | ||
|
|
8ea7456812 | ||
|
|
95c14a5f2b |
@ -156,3 +156,4 @@ There are a bunch of environmental variables that can be set inside the docker c
|
||||
|
||||
* `JSON_RPC_IGNORE_ATTACHMENTS`: When set to `true`, attachments are not automatically downloaded in json-rpc mode (default: `false`)
|
||||
* `JSON_RPC_IGNORE_STORIES`: When set to `true`, stories 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`)
|
||||
|
||||
@ -23,7 +23,7 @@ e.g:
|
||||
When you try to register a number, if you receive a response like `{"error":"Captcha required for verification (null)\n"}` then Signal is requiring a captcha. To register the number you must do the following:
|
||||
1. Go to [https://signalcaptchas.org/registration/generate.html](https://signalcaptchas.org/registration/generate.html)
|
||||
2. Open the developer console
|
||||
3. Find the line that looks like this: `Prevented navigation to “signalcaptcha://{captcha value}” due to an unknown protocol.` Copy the captcha value
|
||||
3. Find the line that looks like this: `Prevented navigation to “signalcaptcha://{captcha value}” due to an unknown protocol.` Copy the captcha value (e.g. `signal-hcaptcha-short.xxxxx.registration.yyyyyy`). Note: do not include `signalcaptcha://`.
|
||||
4. Use it to make the registration call like this:
|
||||
|
||||
`curl -X POST -H "Content-Type: application/json" -d '{"captcha":"captcha value"}' 'http://127.0.0.1:8080/v1/register/<number>'`
|
||||
|
||||
211
src/api/graylogapi.go
Normal file
211
src/api/graylogapi.go
Normal file
@ -0,0 +1,211 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"fmt"
|
||||
|
||||
_ "runtime/debug"
|
||||
_ "github.com/yassinebenaid/godump"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/bbernhard/signal-cli-rest-api/client"
|
||||
)
|
||||
|
||||
type AlertManagerNotification struct {
|
||||
Receiver string `json:"receiver"`
|
||||
Status string `json:"status"`
|
||||
Alerts []Alert `json:"alerts"`
|
||||
GroupLabels Labels `json:"groupLabels"`
|
||||
CommonLabels Labels `json:"commonLabels"`
|
||||
CommonAnnotations Annotations `json:"commonAnnotations"`
|
||||
ExternalURL string `json:"externalURL"`
|
||||
Version string `json:"version"`
|
||||
GroupKey string `json:"groupKey"`
|
||||
TruncatedAlerts int64 `json:"truncatedAlerts"`
|
||||
OrgID int64 `json:"orgId"`
|
||||
Title string `json:"title"`
|
||||
State string `json:"state"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Alert struct {
|
||||
Status string `json:"status"`
|
||||
Labels Labels `json:"labels"`
|
||||
Annotations Annotations `json:"annotations"`
|
||||
StartsAt string `json:"startsAt"`
|
||||
EndsAt string `json:"endsAt"`
|
||||
GeneratorURL string `json:"generatorURL"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
SilenceURL string `json:"silenceURL"`
|
||||
DashboardURL string `json:"dashboardURL"`
|
||||
PanelURL string `json:"panelURL"`
|
||||
Values interface{} `json:"values"`
|
||||
ValueString string `json:"valueString"`
|
||||
}
|
||||
|
||||
type Annotations struct {
|
||||
Summary string `json:"summary"`
|
||||
}
|
||||
|
||||
type Labels struct {
|
||||
Alertname string `json:"alertname"`
|
||||
Instance string `json:"instance"`
|
||||
}
|
||||
|
||||
type GrafanaMessage struct {
|
||||
Number string `json:"number"`
|
||||
Recipients string `json:"recipients"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type GraylogNotification struct {
|
||||
EventDefinitionID string `json:"event_definition_id"`
|
||||
EventDefinitionType string `json:"event_definition_type"`
|
||||
EventDefinitionTitle string `json:"event_definition_title"`
|
||||
EventDefinitionDescription string `json:"event_definition_description"`
|
||||
JobDefinitionID string `json:"job_definition_id"`
|
||||
JobTriggerID string `json:"job_trigger_id"`
|
||||
Event GraylogEvent `json:"event"`
|
||||
Backlog []interface{} `json:"backlog"`
|
||||
}
|
||||
|
||||
type GraylogEvent struct {
|
||||
ID string `json:"id"`
|
||||
EventDefinitionType string `json:"event_definition_type"`
|
||||
EventDefinitionID string `json:"event_definition_id"`
|
||||
OriginContext string `json:"origin_context"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
TimestampProcessing string `json:"timestamp_processing"`
|
||||
TimerangeStart interface{} `json:"timerange_start"`
|
||||
TimerangeEnd interface{} `json:"timerange_end"`
|
||||
Streams []string `json:"streams"`
|
||||
SourceStreams []interface{} `json:"source_streams"`
|
||||
Message string `json:"message"`
|
||||
Source string `json:"source"`
|
||||
KeyTuple []string `json:"key_tuple"`
|
||||
Key string `json:"key"`
|
||||
Priority int64 `json:"priority"`
|
||||
Alert bool `json:"alert"`
|
||||
Fields Fields `json:"fields"`
|
||||
}
|
||||
|
||||
type Fields struct {
|
||||
Recipients string `json:"recipients"`
|
||||
FromNumber string `json:"fromnumber"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
// @Summary Send a signal message.
|
||||
// @Tags Messages
|
||||
// @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~.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 201 {object} SendMessageResponse
|
||||
// @Failure 400 {object} SendMessageError
|
||||
// @Param data body SendMessageV2 true "Input Data"
|
||||
// @Router /v2/send [post]
|
||||
func (a *Api) SendAlertManagerV2(c *gin.Context) {
|
||||
var req AlertManagerNotification
|
||||
var msg GrafanaMessage
|
||||
base64Attachments := []string{}
|
||||
|
||||
err := c.BindJSON(&req)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": "Couldn't process request - invalid request"})
|
||||
log.Error(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
//fmt.Printf(">>>%s\n",[]byte(req.Message))
|
||||
|
||||
// Unmarshal or Decode the JSON to the interface.
|
||||
json.Unmarshal([]byte(req.Message), &msg)
|
||||
|
||||
// timestamp, err := a.signalClient.SendV1(msg.Number, msg.Message, msg.Recipients, base64Attachments, msg.IsGroup)
|
||||
// if err != nil {
|
||||
// c.JSON(400, Error{Msg: err.Error()})
|
||||
// return
|
||||
// }
|
||||
// c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt(timestamp.Timestamp, 10)})
|
||||
|
||||
data, err := a.signalClient.SendV2(msg.Number, msg.Message, strings.Split(msg.Recipients,","), base64Attachments, "", nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *client.RateLimitErrorType:
|
||||
if rateLimitError, ok := err.(*client.RateLimitErrorType); ok {
|
||||
extendedError := errors.New(err.Error() + ". Use the attached challenge tokens to lift the rate limit restrictions via the '/v1/accounts/{number}/rate-limit-challenge' endpoint.")
|
||||
c.JSON(429, SendMessageError{Msg: extendedError.Error(), ChallengeTokens: rateLimitError.ChallengeTokens, Account: msg.Number})
|
||||
return
|
||||
} else {
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
default:
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*data)[0].Timestamp, 10)})
|
||||
|
||||
}
|
||||
|
||||
// @Summary Send a signal message.
|
||||
// @Tags Messages
|
||||
// @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~.
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 201 {object} SendMessageResponse
|
||||
// @Failure 400 {object} SendMessageError
|
||||
// @Param data body SendMessageV2 true "Input Data"
|
||||
// @Router /v2/send [post]
|
||||
func (a *Api) SendGraylogNotificationV2(c *gin.Context) {
|
||||
var req GraylogNotification
|
||||
base64Attachments := []string{}
|
||||
// jsonData,err2 := io.ReadAll(c.Request.Body)
|
||||
//if err2 != nil {
|
||||
// log.Error(err2.Error())
|
||||
//}
|
||||
//fmt.Printf("<<<%s\n",jsonData)
|
||||
|
||||
err := c.BindJSON(&req)
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": "Couldn't process request - invalid requestttttt"})
|
||||
log.Error(err.Error())
|
||||
fmt.Printf("<<<%s\n",c.Request.Body)
|
||||
return
|
||||
}
|
||||
|
||||
data, err := a.signalClient.SendV2(req.Event.Fields.FromNumber, req.Event.Fields.Message, strings.Split(req.Event.Fields.Recipients,","), base64Attachments, "", nil, nil, nil, nil, nil, nil, nil, nil)
|
||||
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *client.RateLimitErrorType:
|
||||
if rateLimitError, ok := err.(*client.RateLimitErrorType); ok {
|
||||
extendedError := errors.New(err.Error() + ". Use the attached challenge tokens to lift the rate limit restrictions via the '/v1/accounts/{number}/rate-limit-challenge' endpoint.")
|
||||
c.JSON(429, SendMessageError{Msg: extendedError.Error(), ChallengeTokens: rateLimitError.ChallengeTokens, Account: req.Event.Fields.FromNumber})
|
||||
return
|
||||
} else {
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
default:
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(400, Error{Msg: err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(201, SendMessageResponse{Timestamp: strconv.FormatInt((*data)[0].Timestamp, 10)})
|
||||
}
|
||||
|
||||
|
||||
@ -2296,8 +2296,12 @@ func (s *SignalClient) ListDevices(number string) ([]ListDevicesResponse, error)
|
||||
}
|
||||
|
||||
func (s *SignalClient) SetTrustMode(number string, trustMode utils.SignalCliTrustMode) error {
|
||||
s.signalCliApiConfig.SetTrustModeForNumber(number, trustMode)
|
||||
return s.signalCliApiConfig.Persist()
|
||||
if s.signalCliMode == JsonRpc {
|
||||
return errors.New("Not supported in json-rpc mode, use the environment variable JSON_RPC_TRUST_NEW_IDENTITIES instead!")
|
||||
} else {
|
||||
s.signalCliApiConfig.SetTrustModeForNumber(number, trustMode)
|
||||
return s.signalCliApiConfig.Persist()
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SignalClient) GetTrustMode(number string) utils.SignalCliTrustMode {
|
||||
|
||||
@ -11,7 +11,7 @@ require (
|
||||
github.com/gorilla/websocket v1.5.0
|
||||
github.com/h2non/filetype v1.1.3
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/sirupsen/logrus v1.9.0
|
||||
github.com/sirupsen/logrus v1.9.1
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||
github.com/swaggo/files v1.0.1
|
||||
github.com/swaggo/gin-swagger v1.6.0
|
||||
|
||||
@ -84,8 +84,8 @@ github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
|
||||
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
|
||||
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
|
||||
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
|
||||
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
|
||||
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/sirupsen/logrus v1.9.1 h1:Ou41VVR3nMWWmTiEUnj0OlsgOSCUFgsPAOl6jRIcVtQ=
|
||||
github.com/sirupsen/logrus v1.9.1/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0=
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
|
||||
@ -356,6 +356,14 @@ func main() {
|
||||
{
|
||||
sendV2.POST("", api.SendV2)
|
||||
}
|
||||
sendalertmanagerV2 := v2.Group("/sendalertmanager")
|
||||
{
|
||||
sendalertmanagerV2.POST("", api.SendAlertManagerV2)
|
||||
}
|
||||
sendgraylognotificationV2 := v2.Group("/sendgraylognotification")
|
||||
{
|
||||
sendgraylognotificationV2.POST("", api.SendGraylogNotificationV2)
|
||||
}
|
||||
}
|
||||
|
||||
protocol := "http"
|
||||
|
||||
@ -2,18 +2,19 @@ package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/bbernhard/signal-cli-rest-api/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
"github.com/bbernhard/signal-cli-rest-api/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const supervisorctlConfigTemplate = `
|
||||
[program:%s]
|
||||
process_name=%s
|
||||
command=bash -c "nc -l -p %d <%s | signal-cli --output=json --config %s jsonRpc%s%s >%s"
|
||||
command=bash -c "nc -l -p %d <%s | signal-cli --output=json --config %s%s jsonRpc%s%s >%s"
|
||||
autostart=true
|
||||
autorestart=true
|
||||
startretries=10
|
||||
@ -77,16 +78,26 @@ func main() {
|
||||
log.Fatal("Couldn't create log folder ", supervisorctlLogFolder, ": ", err.Error())
|
||||
}
|
||||
|
||||
trustNewIdentities := ""
|
||||
trustNewIdentitiesEnv := utils.GetEnv("JSON_RPC_TRUST_NEW_IDENTITIES", "")
|
||||
if trustNewIdentitiesEnv == "on-first-use" {
|
||||
trustNewIdentities = " --trust-new-identities on-first-use"
|
||||
} else if trustNewIdentitiesEnv == "always" {
|
||||
trustNewIdentities = " --trust-new-identities always"
|
||||
} else if trustNewIdentitiesEnv == "never" {
|
||||
trustNewIdentities = " --trust-new-identities never"
|
||||
} else if trustNewIdentitiesEnv != "" {
|
||||
log.Fatal("Invalid JSON_RPC_TRUST_NEW_IDENTITIES environment variable set!")
|
||||
}
|
||||
|
||||
log.Info("Updated jsonrpc2.yml")
|
||||
|
||||
//write supervisorctl config
|
||||
supervisorctlConfigFilename := "/etc/supervisor/conf.d/" + "signal-cli-json-rpc-1.conf"
|
||||
|
||||
|
||||
supervisorctlConfig := fmt.Sprintf(supervisorctlConfigTemplate, supervisorctlProgramName, supervisorctlProgramName,
|
||||
tcpPort, fifoPathname, signalCliConfigDir, signalCliIgnoreAttachments, signalCliIgnoreStories, fifoPathname,
|
||||
tcpPort, fifoPathname, signalCliConfigDir, trustNewIdentities, signalCliIgnoreAttachments, signalCliIgnoreStories, fifoPathname,
|
||||
supervisorctlProgramName, supervisorctlProgramName)
|
||||
|
||||
|
||||
err = ioutil.WriteFile(supervisorctlConfigFilename, []byte(supervisorctlConfig), 0644)
|
||||
if err != nil {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user