/
mqtt.go
168 lines (147 loc) · 4.05 KB
/
mqtt.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package signaling
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"strings"
"sync"
flag "github.com/spf13/pflag"
"github.com/lanikai/alohartc/internal/config"
"github.com/lanikai/alohartc/internal/ice"
"github.com/lanikai/oahu/api/mq"
)
var (
mqttBrokerFlag string
certFlag string
keyFlag string
)
func init() {
flag.StringVarP(&mqttBrokerFlag, "mqtt-address", "m", config.MQTT_BROKER, "MQTT broker address")
flag.StringVarP(&certFlag, "certificate", "c", "/etc/alohartcd/cert.pem", "Client certificate for connecting to MQTT broker")
flag.StringVarP(&keyFlag, "private-key", "k", "/etc/alohartcd/key.pem", "Private key corresponding to client certificate")
RegisterListener(mqttListener)
}
// Connect to the Oahu MQTT broker and subscribe to topics for incoming calls.
func mqttListener(handler SessionHandler) error {
// Load certificate and key.
cert, err := tls.LoadX509KeyPair(certFlag, keyFlag)
if err != nil {
return err
}
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
}
// Extract the subject Common Name from the client certificate, and use it
// as the MQTT client ID.
var clientID string
tlsConfig.BuildNameToCertificate()
for clientID, _ = range tlsConfig.NameToCertificate {
break
}
topicPrefix := fmt.Sprintf("devices/%s", clientID)
// Connect to MQTT broker.
err = mq.Connect(mq.Config{
Server: mqttBrokerFlag,
ClientID: clientID,
TLSConfig: tlsConfig,
WillTopic: topicPrefix + "/status",
WillRetain: true,
WillPayload: []byte("disconnected"),
})
if err != nil {
return err
}
var callLock sync.Mutex
calls := make(map[string]*callState)
ctx := context.TODO()
// Listen for incoming calls.
topicFilter := topicPrefix + "/calls/+/remote/#"
mq.Subscribe(topicFilter, 1, func(msg mq.Message) {
log.Debug("Received MQTT message on topic %s: %q", msg.Topic, msg.Payload)
callID := msg.Wildcards[0]
what := msg.Wildcards[1]
body := string(msg.Payload)
// If this is a new call, invoke the handler.
callLock.Lock()
call, existing := calls[callID]
if !existing {
call = newCall(callID, fmt.Sprintf("%s/calls/%s/local", topicPrefix, callID))
calls[callID] = call
go handler(call.session)
}
callLock.Unlock()
call.handleMessage(what, body)
})
defer mq.Unsubscribe(topicFilter)
mq.Publish(topicPrefix+"/status", 1, []byte("connected"))
<-ctx.Done()
return nil
}
// Creates a new call object.
func newCall(id, topicPrefix string) *callState {
offerCh := make(chan string)
rcandCh := make(chan ice.Candidate)
session := &Session{
Context: context.Background(),
Offer: offerCh,
RemoteCandidates: rcandCh,
SendAnswer: func(sdp string) error {
mq.Publish(topicPrefix+"/sdp-answer", 0, []byte(sdp))
return nil
},
SendLocalCandidate: func(c *ice.Candidate) error {
var payload bytes.Buffer
if c != nil {
fmt.Fprintf(&payload, "%s\nmid:%s\n", c.String(), c.Mid())
}
mq.Publish(topicPrefix+"/ice-candidate", 0, payload.Bytes())
return nil
},
}
return &callState{
id: id,
offerCh: offerCh,
rcandCh: rcandCh,
session: session,
}
}
type callState struct {
id string
offerCh chan string
rcandCh chan ice.Candidate
session *Session
}
func (call *callState) handleMessage(what, body string) {
switch what {
case "sdp-offer":
call.offerCh <- body
case "ice-candidate":
if len(body) == 0 {
close(call.rcandCh)
break
}
var desc, sdpMid string
for _, line := range strings.Split(body, "\n") {
if line == "" {
continue
} else if strings.HasPrefix(line, "candidate:") {
desc = line
} else if strings.HasPrefix(line, "mid:") {
sdpMid = line[4:]
} else {
log.Warn("Invalid 'ice-candidate' payload: %q", body)
}
}
if desc == "" {
log.Debug("Empty ICE candidate: sdpMid = %s", sdpMid)
} else if c, err := ice.ParseCandidate(desc, sdpMid); err != nil {
log.Warn("Invalid ICE candidate (%q, %q): %v", desc, sdpMid, err)
} else {
call.rcandCh <- c
}
default:
log.Warn("Unrecognized MQTT topic level: %s", what)
}
}