-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathe2eTools.go
146 lines (130 loc) · 2.96 KB
/
e2eTools.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package go_utils
import (
"bytes"
"compress/gzip"
"encoding/base64"
"github.com/hktalent/PipelineHttp"
"github.com/pion/webrtc/v3"
"io/ioutil"
"log"
"net/http"
)
const (
E2ePath = "/e2eRelay"
// Allows compressing offer/answer to bypass terminal input limits.
compress = true
)
var PipE = PipelineHttp.NewPipelineHttp()
// 发送通讯信号
func SignalCandidate(addr string, c *webrtc.ICECandidate, hd map[string]string, cbk func(resp *http.Response, err error, szU string), kv ...string) {
payload := []byte(c.ToJSON().Candidate)
SendE2eData(addr, payload, hd, cbk, kv...)
}
func SendE2eData(addr string, data []byte, hd map[string]string, cbk func(resp *http.Response, err error, szU string), kv ...string) {
PipE.DoGetWithClient4SetHd(
nil,
addr,
"POST",
bytes.NewReader(data),
cbk, func() map[string]string {
var m1 = map[string]string{"Content-Type": "application/json; charset=utf-8"}
for k, v := range hd {
m1[k] = v
}
if nil != kv && 0 < len(kv) {
for i := 0; i < len(kv); i += 2 {
m1[kv[i]] = kv[i+1]
}
}
return m1
}, true)
}
// key 标识不同用户,对等的p2p
func GetPeerConnection(key string, certificates *[]webrtc.Certificate) *webrtc.PeerConnection {
config := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{
"stun:stun.l.google.com:19302", // 108.177.125.127
"stun:stun1.l.google.com:19302", // 142.250.21.127
"stun:stun2.l.google.com:19302", // 172.253.56.127
"stun:stun3.l.google.com:19302", // 74.125.197.127
"stun:stun4.l.google.com:19302", // 142.251.2.127
},
},
},
}
if nil != certificates && 0 < len(*certificates) {
config.Certificates = *certificates
}
if 0 < len(key) {
config.PeerIdentity = key
}
// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewPeerConnection(config)
if err != nil {
log.Println(err)
return nil
}
return peerConnection
}
// Encode encodes the input in base64
// It can optionally zip the input before encoding
func Encode(obj interface{}) string {
b, err := Json.Marshal(obj)
if err != nil {
panic(err)
}
if compress {
b = zip(b)
}
return base64.StdEncoding.EncodeToString(b)
}
// Decode decodes the input from base64
// It can optionally unzip the input after decoding
func Decode(in string, obj interface{}) {
b, err := base64.StdEncoding.DecodeString(in)
if err != nil {
panic(err)
}
if compress {
b = unzip(b)
}
err = Json.Unmarshal(b, obj)
if err != nil {
panic(err)
}
}
func zip(in []byte) []byte {
var b bytes.Buffer
gz := gzip.NewWriter(&b)
_, err := gz.Write(in)
if err != nil {
panic(err)
}
err = gz.Flush()
if err != nil {
panic(err)
}
err = gz.Close()
if err != nil {
panic(err)
}
return b.Bytes()
}
func unzip(in []byte) []byte {
var b bytes.Buffer
_, err := b.Write(in)
if err != nil {
panic(err)
}
r, err := gzip.NewReader(&b)
if err != nil {
panic(err)
}
res, err := ioutil.ReadAll(r)
if err != nil {
panic(err)
}
return res
}