added trust mode parameter to json-rpc mode

see #240
This commit is contained in:
Bernhard B
2022-05-08 20:23:54 +02:00
parent 25775a4c10
commit 798f897ad1
9 changed files with 314 additions and 4 deletions

View File

@@ -1439,6 +1439,16 @@ func (a *Api) AddDevice(c *gin.Context) {
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
} }
// @Summary Set account specific settings.
// @Tags General
// @Description Set account specific settings.
// @Accept json
// @Produce json
// @Param number path string true "Registered Phone Number"
// @Success 204
// @Param data body TrustModeRequest true "Request"
// @Failure 400 {object} Error
// @Router /v1/configuration/{number}/settings [post]
func (a *Api) SetTrustMode(c *gin.Context) { func (a *Api) SetTrustMode(c *gin.Context) {
number := c.Param("number") number := c.Param("number")
if number == "" { if number == "" {
@@ -1468,6 +1478,16 @@ func (a *Api) SetTrustMode(c *gin.Context) {
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
} }
// @Summary List account specific settings.
// @Tags General
// @Description List account specific settings.
// @Accept json
// @Produce json
// @Param number path string true "Registered Phone Number"
// @Success 200
// @Param data body TrustModeResponse true "Request"
// @Failure 400 {object} Error
// @Router /v1/configuration/{number}/settings [get]
func (a *Api) GetTrustMode(c *gin.Context) { func (a *Api) GetTrustMode(c *gin.Context) {
number := c.Param("number") number := c.Param("number")
if number == "" { if number == "" {

View File

@@ -262,7 +262,7 @@ func (s *SignalClient) Init() error {
tcpPortsNumberMapping := s.jsonRpc2ClientConfig.GetTcpPortsForNumbers() tcpPortsNumberMapping := s.jsonRpc2ClientConfig.GetTcpPortsForNumbers()
for number, tcpPort := range tcpPortsNumberMapping { for number, tcpPort := range tcpPortsNumberMapping {
s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig) s.jsonRpc2Clients[number] = NewJsonRpc2Client(s.signalCliApiConfig, number)
err := s.jsonRpc2Clients[number].Dial("127.0.0.1:" + strconv.FormatInt(tcpPort, 10)) err := s.jsonRpc2Clients[number].Dial("127.0.0.1:" + strconv.FormatInt(tcpPort, 10))
if err != nil { if err != nil {
return err return err

View File

@@ -10,6 +10,7 @@ import (
"github.com/bbernhard/signal-cli-rest-api/utils" "github.com/bbernhard/signal-cli-rest-api/utils"
uuid "github.com/gofrs/uuid" uuid "github.com/gofrs/uuid"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/tidwall/sjson"
) )
type Error struct { type Error struct {
@@ -35,11 +36,13 @@ type JsonRpc2Client struct {
receivedMessages chan JsonRpc2ReceivedMessage receivedMessages chan JsonRpc2ReceivedMessage
lastTimeErrorMessageSent time.Time lastTimeErrorMessageSent time.Time
signalCliApiConfig *utils.SignalCliApiConfig signalCliApiConfig *utils.SignalCliApiConfig
number string
} }
func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig) *JsonRpc2Client { func NewJsonRpc2Client(signalCliApiConfig *utils.SignalCliApiConfig, number string) *JsonRpc2Client {
return &JsonRpc2Client{ return &JsonRpc2Client{
signalCliApiConfig: signalCliApiConfig, signalCliApiConfig: signalCliApiConfig,
number: number,
} }
} }
@@ -64,6 +67,16 @@ func (r *JsonRpc2Client) getRaw(command string, args interface{}) (string, error
Params interface{} `json:"params,omitempty"` Params interface{} `json:"params,omitempty"`
} }
trustModeStr := ""
trustMode, err := r.signalCliApiConfig.GetTrustModeForNumber(r.number)
if err == nil {
trustModeStr, err = utils.TrustModeToString(trustMode)
if err != nil {
trustModeStr = ""
log.Error("Invalid trust mode: ", trustModeStr)
}
}
u, err := uuid.NewV4() u, err := uuid.NewV4()
if err != nil { if err != nil {
return "", err return "", err
@@ -79,6 +92,13 @@ func (r *JsonRpc2Client) getRaw(command string, args interface{}) (string, error
return "", err return "", err
} }
if trustModeStr != "" {
fullCommandBytes, err = sjson.SetBytes(fullCommandBytes, "params.trustNewIdentities", trustModeStr)
if err != nil {
return "", err
}
}
log.Debug("full command: ", string(fullCommandBytes)) log.Debug("full command: ", string(fullCommandBytes))
_, err = r.conn.Write([]byte(string(fullCommandBytes) + "\n")) _, err = r.conn.Write([]byte(string(fullCommandBytes) + "\n"))

View File

@@ -209,6 +209,88 @@ var doc = `{
} }
} }
}, },
"/v1/configuration/{number}/settings": {
"get": {
"description": "List account specific settings.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"General"
],
"summary": "List account specific settings.",
"parameters": [
{
"type": "string",
"description": "Registered Phone Number",
"name": "number",
"in": "path",
"required": true
},
{
"description": "Request",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.TrustModeResponse"
}
}
],
"responses": {
"200": {},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
},
"post": {
"description": "Set account specific settings.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"General"
],
"summary": "Set account specific settings.",
"parameters": [
{
"type": "string",
"description": "Registered Phone Number",
"name": "number",
"in": "path",
"required": true
},
{
"description": "Request",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.TrustModeRequest"
}
}
],
"responses": {
"204": {},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"/v1/contacts{number}": { "/v1/contacts{number}": {
"put": { "put": {
"description": "Updates the info associated to a number on the contact list.", "description": "Updates the info associated to a number on the contact list.",
@@ -1658,6 +1740,22 @@ var doc = `{
} }
} }
}, },
"api.TrustModeRequest": {
"type": "object",
"properties": {
"trust_mode": {
"type": "string"
}
}
},
"api.TrustModeResponse": {
"type": "object",
"properties": {
"trust_mode": {
"type": "string"
}
}
},
"api.TypingIndicatorRequest": { "api.TypingIndicatorRequest": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -193,6 +193,88 @@
} }
} }
}, },
"/v1/configuration/{number}/settings": {
"get": {
"description": "List account specific settings.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"General"
],
"summary": "List account specific settings.",
"parameters": [
{
"type": "string",
"description": "Registered Phone Number",
"name": "number",
"in": "path",
"required": true
},
{
"description": "Request",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.TrustModeResponse"
}
}
],
"responses": {
"200": {},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
},
"post": {
"description": "Set account specific settings.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"General"
],
"summary": "Set account specific settings.",
"parameters": [
{
"type": "string",
"description": "Registered Phone Number",
"name": "number",
"in": "path",
"required": true
},
{
"description": "Request",
"name": "data",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/api.TrustModeRequest"
}
}
],
"responses": {
"204": {},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"/v1/contacts{number}": { "/v1/contacts{number}": {
"put": { "put": {
"description": "Updates the info associated to a number on the contact list.", "description": "Updates the info associated to a number on the contact list.",
@@ -1642,6 +1724,22 @@
} }
} }
}, },
"api.TrustModeRequest": {
"type": "object",
"properties": {
"trust_mode": {
"type": "string"
}
}
},
"api.TrustModeResponse": {
"type": "object",
"properties": {
"trust_mode": {
"type": "string"
}
}
},
"api.TypingIndicatorRequest": { "api.TypingIndicatorRequest": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -141,6 +141,16 @@ definitions:
verified_safety_number: verified_safety_number:
type: string type: string
type: object type: object
api.TrustModeRequest:
properties:
trust_mode:
type: string
type: object
api.TrustModeResponse:
properties:
trust_mode:
type: string
type: object
api.TypingIndicatorRequest: api.TypingIndicatorRequest:
properties: properties:
recipient: recipient:
@@ -356,6 +366,61 @@ paths:
summary: Set the REST API configuration. summary: Set the REST API configuration.
tags: tags:
- General - General
/v1/configuration/{number}/settings:
get:
consumes:
- application/json
description: List account specific settings.
parameters:
- description: Registered Phone Number
in: path
name: number
required: true
type: string
- description: Request
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.TrustModeResponse'
produces:
- application/json
responses:
"200": {}
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.Error'
summary: List account specific settings.
tags:
- General
post:
consumes:
- application/json
description: Set account specific settings.
parameters:
- description: Registered Phone Number
in: path
name: number
required: true
type: string
- description: Request
in: body
name: data
required: true
schema:
$ref: '#/definitions/api.TrustModeRequest'
produces:
- application/json
responses:
"204": {}
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.Error'
summary: Set account specific settings.
tags:
- General
/v1/contacts{number}: /v1/contacts{number}:
put: put:
consumes: consumes:

View File

@@ -20,6 +20,7 @@ require (
github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14 github.com/swaggo/files v0.0.0-20190704085106-630677cd5c14
github.com/swaggo/gin-swagger v1.2.0 github.com/swaggo/gin-swagger v1.2.0
github.com/swaggo/swag v1.6.7 github.com/swaggo/swag v1.6.7
github.com/tidwall/sjson v1.2.4 // indirect
github.com/urfave/cli/v2 v2.2.0 // indirect github.com/urfave/cli/v2 v2.2.0 // indirect
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/text v0.3.3 // indirect golang.org/x/text v0.3.3 // indirect

View File

@@ -128,6 +128,14 @@ github.com/swaggo/gin-swagger v1.2.0/go.mod h1:qlH2+W7zXGZkczuL+r2nEBR2JTT+/lX05
github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y= github.com/swaggo/swag v1.5.1/go.mod h1:1Bl9F/ZBpVWh22nY0zmYyASPO1lI/zIwRDrpZU+tv8Y=
github.com/swaggo/swag v1.6.7 h1:e8GC2xDllJZr3omJkm9YfmK0Y56+rMO3cg0JBKNz09s= github.com/swaggo/swag v1.6.7 h1:e8GC2xDllJZr3omJkm9YfmK0Y56+rMO3cg0JBKNz09s=
github.com/swaggo/swag v1.6.7/go.mod h1:xDhTyuFIujYiN3DKWC/H/83xcfHp+UE/IzWWampG7Zc= github.com/swaggo/swag v1.6.7/go.mod h1:xDhTyuFIujYiN3DKWC/H/83xcfHp+UE/IzWWampG7Zc=
github.com/tidwall/gjson v1.12.1 h1:ikuZsLdhr8Ws0IdROXUS1Gi4v9Z4pGqpX/CvJkxvfpo=
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/sjson v1.2.4 h1:cuiLzLnaMeBhRmEv00Lpk3tkYrcxpmbU81tAY4Dw0tc=
github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0= github.com/ugorji/go v1.1.5-pre/go.mod h1:FwP/aQVg39TXzItUBMwnWp9T9gPQnXw4Poh4/oBQZ/0=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo= github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=

View File

@@ -150,8 +150,8 @@ func main() {
{ {
configuration.GET("", api.GetConfiguration) configuration.GET("", api.GetConfiguration)
configuration.POST("", api.SetConfiguration) configuration.POST("", api.SetConfiguration)
configuration.POST(":number/trustmode", api.SetTrustMode) configuration.POST(":number/settings", api.SetTrustMode)
configuration.GET(":number/trustmode", api.GetTrustMode) configuration.GET(":number/settings", api.GetTrustMode)
} }
health := v1.Group("/health") health := v1.Group("/health")