-
Notifications
You must be signed in to change notification settings - Fork 3
/
signal_messages.go
124 lines (105 loc) · 2.9 KB
/
signal_messages.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
package star
import (
"bytes"
"encoding/json"
"errors"
"github.com/gorilla/websocket"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"sync"
"time"
)
const wsPeerAliveTTL = 60 * time.Second
var mSendMessage sync.Mutex
func processMessage(addressBook addressBook, handshakeSubscription *handshakeSubscription, message []byte) error {
if bytes.Index(message, []byte(`["ws-peer",`)) == 0 {
var m []string
err := json.Unmarshal(message, &m)
if err != nil {
return err
} else if len(m) < 2 {
return errors.New("missing peer information")
}
return processWsPeerMessage(addressBook, m[1])
} else if bytes.Index(message, []byte(`["ws-handshake",`)) == 0 {
return processWsHandshakeMessage(handshakeSubscription, message[len(`["ws-handshake",`):len(message)-1])
}
return errors.New("tried to process unknown message")
}
func processWsPeerMessage(addressBook addressBook, message string) error {
peerID, signalMultiaddr, err := extractPeerDestination(message)
if err != nil {
return err
}
addressBook.AddAddr(peerID, signalMultiaddr, wsPeerAliveTTL)
return nil
}
func processWsHandshakeMessage(handshakeSubscription *handshakeSubscription, message []byte) error {
var answer handshakeData
err := json.Unmarshal(message, &answer)
if err != nil {
return err
}
handshakeSubscription.emit(answer)
return nil
}
func extractPeerDestination(peerAddr string) (peer.ID, ma.Multiaddr, error) {
peerMultiaddr, err := ma.NewMultiaddr(peerAddr)
if err != nil {
return "", nil, err
}
value, err := peerMultiaddr.ValueForProtocol(ma.P_IPFS)
if err != nil {
return "", nil, err
}
peerID, err := peer.IDB58Decode(value)
if err != nil {
return "", nil, err
}
ipfsMultiaddr, err := ma.NewMultiaddr("/ipfs/" + peerID.String())
if err != nil {
return "", nil, err
}
return peerID, peerMultiaddr.Decapsulate(ipfsMultiaddr), nil
}
func readMessage(connection *websocket.Conn) ([]byte, error) {
_, message, err := connection.ReadMessage()
if err != nil {
return nil, err
}
i := bytes.IndexAny(message, "[{")
if i < 0 {
return nil, errors.New("message token not found")
}
return message[i:], nil
}
func sendMessage(connection *websocket.Conn, messageType string, messageBody interface{}) error {
var buffer bytes.Buffer
buffer.WriteString(messagePrefix)
buffer.WriteString(`["`)
buffer.WriteString(messageType)
buffer.WriteByte('"')
if messageBody != nil {
b, err := json.Marshal(messageBody)
if err != nil {
return err
}
buffer.WriteByte(',')
buffer.Write(b)
}
buffer.WriteByte(']')
mSendMessage.Lock()
defer mSendMessage.Unlock()
return connection.WriteMessage(websocket.TextMessage, buffer.Bytes())
}
func readEmptyMessage(connection *websocket.Conn) error {
_, message, err := connection.ReadMessage()
if err != nil {
return err
}
i := bytes.IndexByte(message, '{')
if i > 0 {
return errors.New("empty message expected")
}
return nil
}