-
Notifications
You must be signed in to change notification settings - Fork 5
/
as.go
161 lines (135 loc) · 3.59 KB
/
as.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
package main
import (
"encoding/json"
"fmt"
"net"
"github.com/pion/webrtc/v2"
"github.com/rs/zerolog/log"
centrifuge "github.com/centrifugal/centrifuge-go"
jwt "github.com/dgrijalva/jwt-go"
)
// As listen to new webRTC connection and proxy it to upstream tcp socket
type As struct {
UpstreamAddr string
SignalServerAddr string
ID string
peerConn *webrtc.PeerConnection
UpstreamChan chan []byte
}
// NewAs returns a new "As" proxy
func NewAs(ID string, secret string, upstreamAddr string, signalServerAddr string) *As {
log.Print("hello")
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"sub": ID,
})
// Sign and get the complete encoded token as a string using the secret
tokenString, err := token.SignedString([]byte(secret))
as := As{ID: ID, UpstreamAddr: upstreamAddr, SignalServerAddr: signalServerAddr}
upChan := make(chan []byte)
as.UpstreamChan = upChan
signalConfig := centrifuge.DefaultConfig()
log.Print(as.SignalServerAddr)
signalClient := centrifuge.New(as.SignalServerAddr, signalConfig)
signalClient.SetToken(tokenString)
signalClient.Connect()
webRTCconfig := webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
}
peerConnection, err := webrtc.NewPeerConnection(webRTCconfig)
if err != nil {
panic(err)
}
dataChannel, err := peerConnection.CreateDataChannel("data", nil)
if err != nil {
panic(err)
}
dataChannel.OnOpen(func() {
log.Printf("Data channel '%s'-'%d' open. Random messages will now be sent to any connected DataChannels every 5 seconds", dataChannel.Label(), dataChannel.ID())
if as.UpstreamAddr == "" {
log.Error().Msgf("cannot connect to upstream \"%s\"", as.UpstreamAddr)
return
}
go func(upChan chan []byte) {
upAddr, err := net.ResolveTCPAddr("tcp", as.UpstreamAddr)
if err != nil {
panic(err)
}
upConn, err := net.DialTCP("tcp", nil, upAddr)
if err != nil {
panic(err)
}
// read from socket
go func() {
for {
data := make([]byte, 1024)
n, err := upConn.Read(data)
if err != nil {
panic(err)
}
log.Printf("sending to down stream %#v", string(data[:n]))
dataChannel.SendText(string(data[:n]))
}
}()
for {
data := <-upChan
if _, err := upConn.Write(data); err != nil {
panic(err)
}
}
}(upChan)
})
dataChannel.OnMessage(func(msg webrtc.DataChannelMessage) {
log.Printf("Message from DataChannel '%s': %#v", dataChannel.Label(), string(msg.Data))
as.UpstreamChan <- msg.Data
})
// Create an offer to send to the browser
offer, err := peerConnection.CreateOffer(nil)
if err != nil {
panic(err)
}
offerData, err := json.Marshal(offer)
if err != nil {
panic(err)
}
err = signalClient.Publish(fmt.Sprintf("offer-%s", ID), offerData)
if err != nil {
panic(err)
}
err = peerConnection.SetLocalDescription(offer)
if err != nil {
panic(err)
}
answerChan := make(chan []byte)
sub, err := signalClient.NewSubscription(fmt.Sprintf("answer-%s", ID))
if err != nil {
panic(err)
}
subHandler := &subEventHandler{answerChan}
sub.OnPublish(subHandler)
sub.OnJoin(subHandler)
sub.OnLeave(subHandler)
err = sub.Subscribe()
if err != nil {
panic(err)
}
log.Print("waiting answer")
// wait answer
answerData := <-answerChan
log.Print("got answer")
var answer webrtc.SessionDescription
err = json.Unmarshal(answerData, &answer)
if err != nil {
panic(err)
}
log.Printf("%v", answer)
// Apply the answer as the remote description
err = peerConnection.SetRemoteDescription(answer)
if err != nil {
panic(err)
}
return &as
}