added search endpoint

* check whether one or more phone numbers are registered with
  the signal service.

see #50
This commit is contained in:
Bernhard B
2022-01-02 14:52:45 +01:00
parent 18dcc49ff4
commit 43747af9d4
6 changed files with 270 additions and 0 deletions

View File

@@ -110,6 +110,11 @@ var connectionUpgrader = websocket.Upgrader{
}, },
} }
type SearchResponse struct {
Number string `json:"number"`
Registered bool `json:"registered"`
}
type Api struct { type Api struct {
signalClient *client.SignalClient signalClient *client.SignalClient
} }
@@ -1050,3 +1055,34 @@ func (a *Api) SendStopTyping(c *gin.Context) {
} }
c.Status(http.StatusNoContent) c.Status(http.StatusNoContent)
} }
// @Summary Check if one or more phone numbers are registered with the Signal Service.
// @Tags Search
// @Description Check if one or more phone numbers are registered with the Signal Service.
// @Accept json
// @Produce json
// @Param numbers query []string true "Numbers to check" collectionFormat(multi)
// @Success 204 {object} SearchResponse
// @Failure 400 {object} Error
// @Router /v1/search [get]
func (a *Api) SearchForNumbers(c *gin.Context) {
query := c.Request.URL.Query()
if _, ok := query["numbers"]; !ok {
c.JSON(400, Error{Msg: "Please provide numbers to query for"})
return
}
searchResults, err := a.signalClient.SearchForNumbers(query["numbers"])
if err != nil {
c.JSON(400, Error{Msg: err.Error()})
return
}
searchResponse := []SearchResponse{}
for _, val := range searchResults {
entry := SearchResponse{Number: val.Number, Registered: val.Registered}
searchResponse = append(searchResponse, entry)
}
c.JSON(200, searchResponse)
}

View File

@@ -122,6 +122,11 @@ type About struct {
Version string `json:"version"` Version string `json:"version"`
} }
type SearchResultEntry struct {
Number string `json:"number"`
Registered bool `json:"registered"`
}
func cleanupTmpFiles(paths []string) { func cleanupTmpFiles(paths []string) {
for _, path := range paths { for _, path := range paths {
os.Remove(path) os.Remove(path)
@@ -514,6 +519,14 @@ func (s *SignalClient) getJsonRpc2Client(number string) (*JsonRpc2Client, error)
return nil, errors.New("Number not registered with JSON-RPC") return nil, errors.New("Number not registered with JSON-RPC")
} }
func (s *SignalClient) getJsonRpc2Clients() ([]*JsonRpc2Client) {
jsonRpc2Clients := []*JsonRpc2Client{}
for _, client := range s.jsonRpc2Clients {
jsonRpc2Clients = append(jsonRpc2Clients, client)
}
return jsonRpc2Clients
}
func (s *SignalClient) SendV2(number string, message string, recps []string, base64Attachments []string) (*[]SendResponse, error) { func (s *SignalClient) SendV2(number string, message string, recps []string, base64Attachments []string) (*[]SendResponse, error) {
if len(recps) == 0 { if len(recps) == 0 {
return nil, errors.New("Please provide at least one recipient") return nil, errors.New("Please provide at least one recipient")
@@ -1155,3 +1168,57 @@ func (s *SignalClient) SendStopTyping(number string, recipient string) error {
return err return err
} }
func (s *SignalClient) SearchForNumbers(numbers []string) ([]SearchResultEntry, error) {
searchResultEntries := []SearchResultEntry{}
var err error
var rawData string
if s.signalCliMode == JsonRpc {
type Request struct {
Numbers []string `json:"recipient"`
}
request := Request{Numbers: numbers}
jsonRpc2Clients := s.getJsonRpc2Clients()
if len(jsonRpc2Clients) == 0 {
return searchResultEntries, errors.New("No JsonRpc2Client registered!")
}
for _, jsonRpc2Client := range jsonRpc2Clients {
rawData, err = jsonRpc2Client.getRaw("getUserStatus", request)
if err == nil { //getUserStatus doesn't need an account to work, so try all the registered acounts and stop until we succeed
break
}
}
if err != nil {
return searchResultEntries, err
}
} else {
cmd := []string{"--config", s.signalCliConfig, "--output", "json", "getUserStatus"}
cmd = append(cmd, numbers...)
rawData, err = runSignalCli(true, cmd, "", s.signalCliMode)
}
if err != nil {
return searchResultEntries, err
}
type SignalCliResponse struct {
Number string `json:"number"`
IsRegistered bool `json:"isRegistered"`
}
var resp []SignalCliResponse
err = json.Unmarshal([]byte(rawData), &resp)
if err != nil {
return searchResultEntries, err
}
for _, val := range resp {
searchResultEntry := SearchResultEntry{Number: val.Number, Registered: val.IsRegistered}
searchResultEntries = append(searchResultEntries, searchResultEntry)
}
return searchResultEntries, err
}

View File

@@ -913,6 +913,48 @@ var doc = `{
} }
} }
}, },
"/v1/search": {
"get": {
"description": "Check if one or more phone numbers are registered with the Signal Service.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Search"
],
"summary": "Check if one or more phone numbers are registered with the Signal Service.",
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi",
"description": "Numbers to check",
"name": "numbers",
"in": "query",
"required": true
}
],
"responses": {
"204": {
"description": "No Content",
"schema": {
"$ref": "#/definitions/api.SearchResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"/v1/send": { "/v1/send": {
"post": { "post": {
"description": "Send a signal message", "description": "Send a signal message",
@@ -1197,6 +1239,17 @@ var doc = `{
} }
} }
}, },
"api.SearchResponse": {
"type": "object",
"properties": {
"number": {
"type": "string"
},
"registered": {
"type": "boolean"
}
}
},
"api.SendMessageResponse": { "api.SendMessageResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
@@ -1295,6 +1348,9 @@ var doc = `{
"mode": { "mode": {
"type": "string" "type": "string"
}, },
"version": {
"type": "string"
},
"versions": { "versions": {
"type": "array", "type": "array",
"items": { "items": {
@@ -1394,6 +1450,10 @@ var doc = `{
{ {
"description": "React to messages.", "description": "React to messages.",
"name": "Reactions" "name": "Reactions"
},
{
"description": "Search the Signal Service.",
"name": "Search"
} }
] ]
}` }`

View File

@@ -898,6 +898,48 @@
} }
} }
}, },
"/v1/search": {
"get": {
"description": "Check if one or more phone numbers are registered with the Signal Service.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Search"
],
"summary": "Check if one or more phone numbers are registered with the Signal Service.",
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi",
"description": "Numbers to check",
"name": "numbers",
"in": "query",
"required": true
}
],
"responses": {
"204": {
"description": "No Content",
"schema": {
"$ref": "#/definitions/api.SearchResponse"
}
},
"400": {
"description": "Bad Request",
"schema": {
"$ref": "#/definitions/api.Error"
}
}
}
}
},
"/v1/send": { "/v1/send": {
"post": { "post": {
"description": "Send a signal message", "description": "Send a signal message",
@@ -1182,6 +1224,17 @@
} }
} }
}, },
"api.SearchResponse": {
"type": "object",
"properties": {
"number": {
"type": "string"
},
"registered": {
"type": "boolean"
}
}
},
"api.SendMessageResponse": { "api.SendMessageResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
@@ -1280,6 +1333,9 @@
"mode": { "mode": {
"type": "string" "type": "string"
}, },
"version": {
"type": "string"
},
"versions": { "versions": {
"type": "array", "type": "array",
"items": { "items": {
@@ -1379,6 +1435,10 @@
{ {
"description": "React to messages.", "description": "React to messages.",
"name": "Reactions" "name": "Reactions"
},
{
"description": "Search the Signal Service.",
"name": "Search"
} }
] ]
} }

View File

@@ -72,6 +72,13 @@ definitions:
use_voice: use_voice:
type: boolean type: boolean
type: object type: object
api.SearchResponse:
properties:
number:
type: string
registered:
type: boolean
type: object
api.SendMessageResponse: api.SendMessageResponse:
properties: properties:
timestamp: timestamp:
@@ -135,6 +142,8 @@ definitions:
type: integer type: integer
mode: mode:
type: string type: string
version:
type: string
versions: versions:
items: items:
type: string type: string
@@ -773,6 +782,34 @@ paths:
summary: Verify a registered phone number. summary: Verify a registered phone number.
tags: tags:
- Devices - Devices
/v1/search:
get:
consumes:
- application/json
description: Check if one or more phone numbers are registered with the Signal Service.
parameters:
- collectionFormat: multi
description: Numbers to check
in: query
items:
type: string
name: numbers
required: true
type: array
produces:
- application/json
responses:
"204":
description: No Content
schema:
$ref: '#/definitions/api.SearchResponse'
"400":
description: Bad Request
schema:
$ref: '#/definitions/api.Error'
summary: Check if one or more phone numbers are registered with the Signal Service.
tags:
- Search
/v1/send: /v1/send:
post: post:
consumes: consumes:
@@ -905,3 +942,5 @@ tags:
name: Identities name: Identities
- description: React to messages. - description: React to messages.
name: Reactions name: Reactions
- description: Search the Signal Service.
name: Search

View File

@@ -49,6 +49,9 @@ import (
// @tag.name Reactions // @tag.name Reactions
// @tag.description React to messages. // @tag.description React to messages.
// @tag.name Search
// @tag.description Search the Signal Service.
// @host 127.0.0.1:8080 // @host 127.0.0.1:8080
// @BasePath / // @BasePath /
func main() { func main() {
@@ -204,6 +207,11 @@ func main() {
reactions.POST(":number", api.SendReaction) reactions.POST(":number", api.SendReaction)
reactions.DELETE(":number", api.RemoveReaction) reactions.DELETE(":number", api.RemoveReaction)
} }
search := v1.Group("/search")
{
search.GET("", api.SearchForNumbers)
}
} }
v2 := router.Group("/v2") v2 := router.Group("/v2")