-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
313 lines (275 loc) · 7.44 KB
/
main.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package main
import (
"crypto/tls"
"errors"
"fmt"
"log"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/joho/godotenv"
)
var config *tls.Config
var serverAddress string
var serverPort string
var disableTLS bool
func main() {
log.SetFlags(log.Lshortfile)
err := godotenv.Load()
if err != nil {
log.Fatalln(err)
return
}
disableTLS = len(os.Getenv("PONSE_DISABLE_TLS")) > 0
var cer tls.Certificate
if !disableTLS {
cer, err = tls.LoadX509KeyPair("server.crt", "server.key")
if err != nil {
log.Fatalln(err)
return
}
}
config = &tls.Config{
MinVersion: tls.VersionTLS10, // The 3DS uses TLS 1.0 when doing handshake
InsecureSkipVerify: true,
}
if !disableTLS {
config.Certificates = []tls.Certificate{cer}
}
// Read the iRTSP destination address from the PONSE_SERVER_URI env. This can be timed
// with an HTTP(S) proxy to get the address before starting the proxy. Example:
// irtsp://140.227.187.170:41002
address := os.Getenv("PONSE_SERVER_URI")
filteredAddress, _ := strings.CutPrefix(address, "irtsp://")
serverAddress, serverPort, _ = strings.Cut(filteredAddress, ":")
ln, err := net.Listen("tcp", ":" + serverPort)
if err != nil {
log.Println(err)
return
}
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
continue
}
go handleIRTSPConnection(conn)
}
}
func handleIRTSPConnection(conn net.Conn) {
defer conn.Close()
serverConn, err := net.Dial("tcp", serverAddress + ":" + serverPort)
if err != nil {
log.Println(err)
return
}
defer serverConn.Close()
for {
buffer := make([]byte, 1024)
// TODO - With this hack we change between client->server and server->client messages faster
// when doing everything on the same goroutine. Split interactions into separate goroutines
// and make TLS not break in the process
conn.SetReadDeadline(time.Now().Add(1 * time.Second))
n, err := conn.Read(buffer)
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
log.Println(n, err)
break
}
buffer = buffer[:n]
if len(buffer) > 0 {
req := NewMessage(buffer)
log.Printf("%+v\n", req)
n, err = serverConn.Write(req.ToBytes())
if err != nil {
log.Println(n, err)
break
}
// The client can also send response messages, so we check the message type for logging
var messageType string
if req.Code > 0 {
messageType = "response"
} else {
messageType = "request"
}
log.Printf("[CLIENT] iRTSP %s:\n", messageType)
fmt.Printf("%s\n", req.ToBytes())
}
serverConn.SetReadDeadline(time.Now().Add(1 * time.Second))
buffer = make([]byte, 1024)
n, err = serverConn.Read(buffer)
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
log.Println(n, err)
break
}
buffer = buffer[:n]
if len(buffer) > 0 {
res := NewMessage(buffer)
log.Printf("%+v\n", res)
// When we receive the stream media ports, start a connection on those ports
// for proxying the data
if res.Method == "SETUP" {
videoHeader := res.Headers["v"]
startMediaConnection(videoHeader, "VIDEO")
audioHeader := res.Headers["a"]
// TODO - Is this even possible?
if audioHeader != videoHeader {
startMediaConnection(audioHeader, "AUDIO")
}
controlHeader := res.Headers["c"]
if controlHeader != videoHeader && controlHeader != audioHeader {
startMediaConnection(controlHeader, "CONTROL")
}
}
// When we receive the KNOCK port, start a connection on it for proxying
// the data
// The KNOCK header looks like this:
// iDataChunk/unicast/tcp/40605;
// So we trim the ; at the end
if res.Method == "KNOCK" {
knockHeader := res.Headers["p"]
startMediaConnection(strings.TrimRight(knockHeader, ";"), "KNOCK")
}
if res.Method == "START" && disableTLS {
// The server controls whether the client should do a TLS handshake
// with the "scheme" header
// Disable TLS on the client by clearing out the header
if scheme, ok := res.Headers["sc"]; ok && scheme == "tls" {
res.Headers["sc"] = ""
}
}
n, err = conn.Write(res.ToBytes())
if err != nil {
log.Println(n, err)
break
}
// The server can also send request messages, so we check the message type for logging
var messageType string
if res.Code > 0 {
messageType = "response"
} else {
messageType = "request"
}
log.Printf("[SERVER] iRTSP %s:\n", messageType)
fmt.Printf("%s\n", res.ToBytes())
// When we receive the START response from the server, do the TLS handshake.
// TODO - This assumes that the server wants a TLS handshake
if res.Method == "START" {
if !disableTLS {
conn = tls.Server(conn, config)
}
serverConn = tls.Client(serverConn, config)
}
}
}
}
func startMediaConnection(header, kind string) {
// A media header consists of 4 sections:
// iDataChunk/unicast/tcp/40603
// 1. The streaming type: "iDataChunk"
// 2. The delivery type: "unicast" (or "multicast"?)
// 3. The transmission protocol used: "tcp" or "ust"
// 4. The server port: "40603"
headerStrings := strings.Split(header, "/")
port := headerStrings[len(headerStrings)-1] // Extract the port from the last section
network := headerStrings[len(headerStrings)-2] // Extract the network from the third section
// UST is a custom network protocol over UDP. It is used as a "slow connection" mode,
// but the UST payload is the same as in TCP mode
if network == "ust" {
network = "udp"
portInt, err := strconv.Atoi(port)
if err != nil {
log.Println(err)
return
}
conn, err := net.ListenUDP(network, &net.UDPAddr{IP: net.ParseIP("0.0.0.0"), Port: portInt})
if err != nil {
log.Println(err)
return
}
go handleMediaConnection(conn, network, port, kind)
return
}
ln, err := net.Listen(network, ":" + port)
if err != nil {
log.Println(err)
return
}
go func() {
defer ln.Close()
for {
conn, err := ln.Accept()
if err != nil {
log.Println(err)
continue
}
go handleMediaConnection(conn, network, port, kind)
}
}()
}
func handleMediaConnection(conn net.Conn, network, port, kind string) {
serverConn, err := net.Dial(network, serverAddress + ":" + port)
if err != nil {
log.Println(err)
return
}
defer serverConn.Close()
wg := &sync.WaitGroup{}
wg.Add(2)
go func(wg *sync.WaitGroup) {
for {
buffer := make([]byte, 1024)
n, err := conn.Read(buffer)
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
log.Println(n, err)
break
}
buffer = buffer[:n]
if len(buffer) > 0 {
// TODO - Investigate why UDP isn't working
if network == "udp" {
n, err = conn.(*net.UDPConn).WriteTo(buffer, serverConn.RemoteAddr())
} else {
n, err = serverConn.Write(buffer)
}
if err != nil {
log.Println(n, err)
break
}
log.Printf("[%s] Media request:\n", kind)
// fmt.Printf("%x\n", buffer)
}
}
wg.Done()
}(wg)
go func(wg *sync.WaitGroup) {
for {
buffer := make([]byte, 1024)
n, err := serverConn.Read(buffer)
if err != nil && !errors.Is(err, os.ErrDeadlineExceeded) {
log.Println(n, err)
break
}
buffer = buffer[:n]
if len(buffer) > 0 {
// TODO - Investigate why UDP isn't working
if network == "udp" {
n, err = serverConn.(*net.UDPConn).WriteTo(buffer, conn.RemoteAddr())
} else {
n, err = conn.Write(buffer)
}
if err != nil {
log.Println(n, err)
break
}
log.Printf("[%s] Media response:\n", kind)
// fmt.Printf("%x\n", buffer)
}
}
wg.Done()
}(wg)
wg.Wait()
}