forked from deepch/RTSPtoWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamRemoteAuthorization.go
74 lines (53 loc) · 1.24 KB
/
streamRemoteAuthorization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"bytes"
"encoding/json"
"io"
"net/http"
"time"
)
type AuthorizationReq struct {
Proto string `json:"proto,omitempty"`
Stream string `json:"stream,omitempty"`
Channel string `json:"channel,omitempty"`
Token string `json:"token,omitempty"`
IP string `json:"ip,omitempty"`
}
type AuthorizationRes struct {
Status string `json:"status,omitempty"`
}
func RemoteAuthorization(proto string, stream string, channel string, token string, ip string) bool {
if !Storage.ServerTokenEnable() {
return true
}
buf, err := json.Marshal(&AuthorizationReq{proto, stream, channel, token, ip})
if err != nil {
return false
}
request, err := http.NewRequest("POST", Storage.ServerTokenBackend(), bytes.NewBuffer(buf))
if err != nil {
return false
}
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
client := &http.Client{
Timeout: 1 * time.Second,
}
response, err := client.Do(request)
if err != nil {
return false
}
defer response.Body.Close()
bodyBytes, err := io.ReadAll(response.Body)
if err != nil {
return false
}
var res AuthorizationRes
err = json.Unmarshal(bodyBytes, &res)
if err != nil {
return false
}
if res.Status == "1" {
return true
}
return false
}