forked from qq906907952/apprtc-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apprtc.go
791 lines (683 loc) · 24.2 KB
/
apprtc.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
package main
import (
"crypto/hmac"
"crypto/sha1"
"crypto/tls"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
"github.com/daozhao/apprtc-go/collider"
// "reflect"
)
/*
const (
CERT = "./mycert.pem"
KEY = "./mycert.key"
wssHostPort = "8089"
wssHost = "192.168.2.30"
PORT = 8888
)
*/
var TURN_SERVER_OVERRIDE string
const STUN_SERVER_FMT = `
{
"urls": [
"stun:%s"
]
}
`
const TURN_SERVER_FMT = `
{
"urls": [
"turn:%s?transport=udp"
],
"username": "%s",
"credential": "%s"
}
`
const TURN_SERVER_FMT_TEST = `
{
"urls": [
"turn:%s?transport=udp"
],
"credential": "%s:%s"
}
`
var TURN_BASE_URL string = "https://computeengineondemand.appspot.com"
var TURN_URL_TEMPLATE string = `"%s/turn?username=%s&key=%s"`
var CEOD_KEY string = "4080218913"
var ICE_SERVER_BASE_URL string = "https://"
var ICE_SERVER_URL_TEMPLATE string = `"%s://%s/iceconfig?key=%s"`
var ICE_SERVER_API_KEY string = "4080218913" //os.environ.get('ICE_SERVER_API_KEY')
var CALLSTATS_PARAMS string = `{"appSecret": "none", "appId": "none"}`
/*
{
'appId': os.environ.get('CALLSTATS_APP_ID'),
'appSecret': os.environ.get('CALLSTATS_APP_SECRET')
}`
*/
/*
var WSS_INSTANCE_HOST_KEY string = "host_port_pair"
var WSS_INSTANCE_NAME_KEY string = "vm_name"
var WSS_INSTANCE_ZONE_KEY string = "zone"
var WSS_INSTANCES string = `[{
# WSS_INSTANCE_HOST_KEY: 'apprtc-ws.webrtc.org:443',
WSS_INSTANCE_HOST_KEY: '192.168.2.97:8089',
WSS_INSTANCE_NAME_KEY: 'wsserver-std',
WSS_INSTANCE_ZONE_KEY: 'us-central1-a'
}, {
# WSS_INSTANCE_HOST_KEY: 'apprtc-ws-2.webrtc.org:443',
WSS_INSTANCE_HOST_KEY: '192.168.2.97:8089',
WSS_INSTANCE_NAME_KEY: 'wsserver-std-2',
WSS_INSTANCE_ZONE_KEY: 'us-central1-f'
}]`
*/
const (
RESPONSE_ERROR = "ERROR"
RESPONSE_ROOM_FULL = "FULL"
RESPONSE_UNKNOWN_ROOM = "UNKNOWN_ROOM"
RESPONSE_UNKNOWN_CLIENT = "UNKNOWN_CLIENT"
RESPONSE_DUPLICATE_CLIENT = "DUPLICATE_CLIENT"
RESPONSE_SUCCESS = "SUCCESS"
RESPONSE_INVALID_REQUEST = "INVALID_REQUEST"
LOOPBACK_CLIENT_ID = "LOOPBACK_CLIENT_ID"
)
type Client struct {
Id string
is_initiator bool
messages []string
messageLen int
}
func NewClient(clientId string, initiator bool) *Client {
return &Client{Id: clientId, is_initiator: initiator, messages: make([]string, 10), messageLen: 0}
}
func (c *Client) AddMessage(msg string) {
if c.messageLen < len(c.messages) {
c.messages[c.messageLen] = msg
c.messageLen = c.messageLen + 1
}
//c.messages = append(c.messages,msg)
}
func (c *Client) CleanMessage() {
c.messageLen = 0
}
func (c *Client) GetMessages() []string {
return c.messages[0:c.messageLen]
}
func (c *Client) SetInitiator(initiator bool) {
c.is_initiator = initiator
}
/*
*/
type Room struct {
Id string
clients map[string]*Client
}
//
func NewRoom(roomId string) *Room {
return &Room{Id: roomId, clients: make(map[string]*Client)}
}
//
func (r *Room) AddClient(c *Client) {
r.clients[c.Id] = c
}
//
func (r *Room) RemoveClient(client_id string) {
_, ok := r.clients[client_id]
if ok {
delete(r.clients, client_id)
}
}
func (r *Room) HasClient(client_id string) bool {
_, ok := r.clients[client_id]
return ok
}
func (r *Room) GetClient(client_id string) (*Client, bool) {
client, ok := r.clients[client_id]
if ok {
return client, true
}
return nil, false
}
func (r *Room) GetOtherClient(client_id string) (*Client, bool) {
for key, client := range r.clients {
if key != client_id {
return client, true
}
}
return nil, false
}
func (r *Room) GetOccupancy() int {
return len(r.clients)
}
func (r *Room) GetStatus() []string {
var result []string
var i int = 0
result = make([]string, len(r.clients))
for key, _ := range r.clients {
result[i] = key
i = i + 1
}
return result
// abc := map[string]int{
// "a": 1,
// "b": 2,
// "c": 3,
// }
// keys := reflect.ValueOf(abc).MapKeys()
// fmt.Println(keys) // [a b c]
}
var RoomList map[string]*Room
func getRequest(r *http.Request, key, def string) string {
value := r.Form.Get(key)
if len(value) == 0 {
return def
}
return value
}
func getWssParameters(r *http.Request) (string, string) {
// wssHostPortPair := r.Form.Get("wshpp")
// isTLS := isTLS(r)
// wssTLS := getRequest(r, "wstls", strconv.FormatBool(isTLS))
// // http://127.0.0.1:8080/?wstls=false&wshpp=192.168.2.97:4443
// if len(wssHostPortPair) == 0 {
// log.Println("getWssParameters, r.Host:", r.Host)
// wssHostPortPair = r.Host
// if len(wssHost) > 0 {
// wssHostPortPair = wssHost //+ ":" + strconv.Itoa(wssHostPort) // "192.168.2.30:8089"
// }
// }
// log.Println("r:",r)
// if strings.Index(r.Scheme,"http://") == 0 {
// wssTLS = "false"
// }
// wssTLS = "false"
var wssUrl, wssPostUrl string
// if strings.EqualFold(wssTLS, "false") {
if !isTLS(r) {
// wssUrl = "ws://" + r.Host + ":" + strconv.Itoa(wsHostPort) + "/ws"
// wssPostUrl = "http://" + r.Host + ":" + strconv.Itoa(httpHostPort)
wssUrl = "ws://" + r.Host + "/ws"
wssPostUrl = "http://" + r.Host
} else {
// wssUrl = "wss://" + r.Host + ":" + strconv.Itoa(wssHostPort) + "/ws"
// wssPostUrl = "https://" + r.Host + ":" + strconv.Itoa(httpHostPort)
wssUrl = "wss://" + r.Host + "/ws"
wssPostUrl = "https://" + r.Host
}
return wssUrl, wssPostUrl
}
func roomPageHandler(w http.ResponseWriter, r *http.Request) {
log.Println("roomPageHandler host:", r.Host, "url:", r.URL.RequestURI(), " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
room_id := strings.Replace(r.URL.Path, "/r/", "", 1)
room_id = strings.Replace(room_id, "/", "", -1)
//todo: 检查房间是否已经超过两人。
log.Println("room page room_id:", room_id)
t, err := template.ParseFiles("./html/index_template.html")
// t, err := template.ParseFiles("./html/params.html")
if err != nil {
log.Println(err)
log.Println("index template render error:",err.Error())
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
room, ok := RoomList[room_id]
if ok {
if room.GetOccupancy() >= 2 {
t, err = template.ParseFiles("./html/full_template.html")
if err!=nil{
log.Println("full template render error:",err.Error())
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
t.Execute(w, nil)
return
}
}
data := getRoomParameters(r, room_id, "", nil)
t.Execute(w, data)
// t.Execute(w, nil)
}
func addClientToRoom(r *http.Request, room_id, client_id string, is_loopback bool) (map[string]interface{}, bool) {
room, ok := RoomList[room_id]
if !ok {
room = NewRoom(room_id)
RoomList[room_id] = room
}
var is_initiator bool
var messages []string
error := ""
occupancy := room.GetOccupancy()
if occupancy >= 2 {
error = RESPONSE_ROOM_FULL
}
if room.HasClient(client_id) {
error = RESPONSE_DUPLICATE_CLIENT
}
if 0 == occupancy {
is_initiator = true
room.AddClient(NewClient(client_id, is_initiator))
if is_loopback {
room.AddClient(NewClient(LOOPBACK_CLIENT_ID, false))
}
messages = make([]string, 0)
} else {
is_initiator = false
other_client, _ := room.GetOtherClient(client_id)
messages = other_client.GetMessages()
room.AddClient(NewClient(client_id, is_initiator))
other_client.CleanMessage()
log.Println("addClientToRoom message:", messages)
}
var params map[string]interface{}
params = make(map[string]interface{})
params["error"] = error
params["is_initiator"] = is_initiator
params["messages"] = messages
params["room_state"] = room.GetStatus()
return params, (len(error) == 0)
}
func joinPageHandler(w http.ResponseWriter, r *http.Request) {
log.Println("joinPageHandler host:", r.Host, "url:", r.URL.RequestURI(), " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
log.Println("joinPageHandler host:", r.Host, "TLS:", r.TLS, " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
room_id := strings.Replace(r.URL.Path, "/join/", "", 1)
room_id = strings.Replace(room_id, "/", "", -1)
log.Println("join page room_id:", room_id)
client_id := fmt.Sprintf("%d", rand.Intn(1000000000))
is_loopback := (getRequest(r, "debug", "") == "loopback")
result, ok := addClientToRoom(r, room_id, client_id, is_loopback)
var resultStr string
var returnData map[string]interface{}
var params map[string]interface{}
if !ok {
log.Println("Error adding client to room:", result["error"], ", room_state=", result["room_state"])
resultStr, _ = result["error"].(string)
returnData = make(map[string]interface{})
//return
} else {
resultStr = "SUCCESS"
is_initiator := strconv.FormatBool(result["is_initiator"].(bool))
log.Println("joinPageHandler is_initiator:", result["is_initiator"], " String:", is_initiator)
returnData = getRoomParameters(r, room_id, client_id, is_initiator)
returnData["messages"] = result["messages"]
// returnData["is_initiator"] = "true"
}
params = make(map[string]interface{})
params["result"] = resultStr
params["params"] = returnData
//todo 输出json数据返回
enc := json.NewEncoder(w)
enc.Encode(¶ms)
}
func removeClientFromRoom(room_id, client_id string) map[string]interface{} {
log.Println("removeClientFromRoom room:", room_id, " client:", client_id)
var result map[string]interface{}
result = make(map[string]interface{})
room, ok := RoomList[room_id]
if !ok {
log.Println("removeClientFromRoom: Unknow room:", room_id)
result["error"] = RESPONSE_UNKNOWN_ROOM
result["room_state"] = ""
return result
}
if !room.HasClient(client_id) {
log.Println("removeClientFromRoom: Unknow client:", client_id)
result["error"] = RESPONSE_UNKNOWN_CLIENT
result["room_state"] = room.GetStatus()
return result
}
room.RemoveClient(client_id)
room.RemoveClient(LOOPBACK_CLIENT_ID)
if room.GetOccupancy() > 0 {
client, _ := room.GetOtherClient(client_id)
client.SetInitiator(true)
} else {
delete(RoomList, room_id)
}
result["error"] = ""
result["room_state"] = room.GetStatus()
return result
}
func leavePageHandler(w http.ResponseWriter, r *http.Request) {
log.Println("leavePageHandler host:", r.Host, "url:", r.URL.RequestURI(), " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
//room_id := strings.Replace(r.URL.Path,"/leave/","",1)
var room_id, client_id string
url := strings.Split(r.URL.Path, "/")
log.Println("url array:", url)
if len(url) >= 3 {
room_id = url[2]
client_id = url[3]
// fmt.Sscanf(r.URL.Path, "/leave/%s/", &room_id, &client_id)
//log.Println("leave room:",room_id," client:",client_id)
result := removeClientFromRoom(room_id, client_id)
//result := removeClientFromRoom(strconv.Itoa(room_id), strconv.Itoa(client_id))
if len(result["error"].(string)) == 0 {
log.Println("Room:", room_id, " has state ", result["room_state"])
}
}
}
func saveMessageFromClient(room_id, client_id, message_json string) map[string]interface{} {
log.Println("saveMessageFrom room:", room_id, " client:", client_id, " msg:", message_json)
var result map[string]interface{}
result = make(map[string]interface{})
room, ok := RoomList[room_id]
result["saved"] = false
if !ok {
log.Println("saveMessageFromClient: Unknow room:", room_id)
result["error"] = RESPONSE_UNKNOWN_ROOM
return result
}
client, has := room.GetClient(client_id)
if !has {
log.Println("saveMessageFromclient: Unknow client:", client_id)
result["error"] = RESPONSE_UNKNOWN_CLIENT
return result
}
if room.GetOccupancy() > 1 {
result["error"] = ""
return result
}
client.AddMessage(message_json)
result["error"] = ""
result["saved"] = true
return result
}
func messagePageHandler(w http.ResponseWriter, r *http.Request) {
log.Println("messagePageHandler host:", r.Host, "url:", r.URL.RequestURI(), " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
var room_id, client_id string
url := strings.Split(r.URL.Path, "/")
log.Println("url array:", url)
if len(url) >= 3 {
room_id = url[2]
client_id = url[3]
body, err := ioutil.ReadAll(r.Body)
if err != nil {
}
message_json := string(body)
result := saveMessageFromClient(room_id, client_id, message_json)
if !result["saved"].(bool) {
_, wss_post_url := getWssParameters(r)
resp, err := http.Post(wss_post_url+"/"+room_id+"/"+client_id, "application/x-www-form-urlencoded", strings.NewReader(message_json))
if err != nil {
fmt.Println(err)
}
if resp.StatusCode != 200 {
log.Println("Failed to send message to collider:", resp.StatusCode)
}
}
var params map[string]interface{}
params = make(map[string]interface{})
params["result"] = RESPONSE_SUCCESS
enc := json.NewEncoder(w)
enc.Encode(¶ms)
}
}
func paramsPageHandler(w http.ResponseWriter, r *http.Request) {
log.Println("paramsPageHandler host:", r.Host, "url:", r.URL.RequestURI(), " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
data := getRoomParameters(r, "", "", nil)
enc := json.NewEncoder(w)
enc.Encode(&data)
}
func paramsHTMLPageHandler(w http.ResponseWriter, r *http.Request) {
log.Println("paramsHTMLPageHandler host:", r.Host, "url:", r.URL.RequestURI(), " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
t, _ := template.ParseFiles("./html/params.html")
t.Execute(w, nil)
}
func aPageHandler(w http.ResponseWriter, r *http.Request) {
log.Println("aPageHandler host:", r.Host, "url:", r.URL.RequestURI(), " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
}
func iceconfigPageHandler(w http.ResponseWriter, r *http.Request) {
turnServer := ""
if len(*flagstun) > 0 {
turnServer += fmt.Sprintf(STUN_SERVER_FMT, *flagstun)
}
if len(*flagturn) > 0 {
if len(turnServer) > 0 {
turnServer += ","
}
username, password := getTurnAuth()
turnServer += fmt.Sprintf(TURN_SERVER_FMT, *flagturn, username, password)
// turnServer += fmt.Sprintf(TURN_SERVER_FMT, *flagturn, "teninefingers", "4080218913")
}
turnServer = `{"iceServers":[` + turnServer + "]}"
log.Println("turnServer:", turnServer)
var dat interface{}
if err := json.Unmarshal([]byte(turnServer), &dat); err != nil {
log.Println("json.Unmarshal error:", err)
return
}
// params :=
enc := json.NewEncoder(w)
enc.Encode(&dat)
}
func getTurnAuth() (username, password string) {
if len(*flagTurnSecret) > 0 {
timestamp := time.Now().Unix() + 60*60
turnUsername := strconv.Itoa(int(timestamp)) + ":" + *flagTurnUser
expectedMAC := Hmac(*flagTurnSecret, turnUsername)
return turnUsername, expectedMAC
}
return *flagTurnUser, *flagTurnPassword
}
func Hmac(key, data string) string {
// https://stackoverflow.com/questions/30745153/turn-server-for-webrtc-with-rest-api-authentication?noredirect=1&lq=1
// key: my_secret
// user: 1433895918506:my_user_name
// 1Dj9XZ5fwvKS6YoQZOoORcFnXaI
hmac := hmac.New(sha1.New, []byte(key))
hmac.Write([]byte(data))
return base64.StdEncoding.EncodeToString(hmac.Sum(nil))
// return base64.StdEncoding.EncodeToString(hmac.Sum([]byte("")))
}
func computePageHandler(w http.ResponseWriter, r *http.Request) {
log.Println("computePagehandler host:", r.Host, "url:", r.URL.RequestURI(), " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
}
func mainPageHandler(w http.ResponseWriter, r *http.Request) {
// if r.URL.Path == "/" {
// http.Redirect(w, r, "/login/index", http.StatusFound)
// }
log.Println("host:", r.Host, "url:", r.URL.RequestURI(), " path:", r.URL.Path, " raw query:", r.URL.RawQuery)
t, err := template.ParseFiles("./html/index_template.html")
// t, err := template.ParseFiles("./html/params.html")
if err != nil {
log.Println(err)
}
data := getRoomParameters(r, "", "", nil)
t.Execute(w, data)
// t.Execute(w, nil)
}
func isTLS(r *http.Request) bool {
return (r.TLS != nil)
}
func getRoomParameters(r *http.Request, room_id, client_id string, is_initiator interface{}) map[string]interface{} {
var data map[string]interface{}
data = make(map[string]interface{})
data["error_messages"] = []string{}
data["warning_messages"] = []string{}
data["is_loopback"] = false // json.dumps(debug == 'loopback'),
data["pc_config"] = template.JS(`{"iceServers": [], "rtcpMuxPolicy": "require", "bundlePolicy": "max-bundle"}`) //json.dumps(pc_config),
data["pc_constraints"] = template.JS(`{"optional": []}`) // json.dumps(pc_constraints),
data["offer_options"] = template.JS("{}") //json.dumps(offer_options),
data["media_constraints"] = template.JS(`{"video": {"optional": [{"minWidth": "1280"}, {"minHeight": "720"}], "mandatory": {}}, "audio": true}`)
// var dat []map[string]interface{}
var dat interface{}
if err := json.Unmarshal([]byte(TURN_SERVER_OVERRIDE), &dat); err != nil {
log.Println("json.Unmarshal error:", err)
}
// log.Println(dat)
data["turn_server_override"] = dat // template.JS(strings.Replace(TURN_SERVER_OVERRIDE,"\n","",-1) )
username := fmt.Sprintf("%d", rand.Intn(1000000000)) //todo:
if len(client_id) > 0 {
username = client_id
}
data["turn_url"] = template.JS(fmt.Sprintf(TURN_URL_TEMPLATE, TURN_BASE_URL, username, CEOD_KEY))
// ice_server_base_url := getRequest(r, "ts", ICE_SERVER_BASE_URL)
if isTLS(r) {
// data["ice_server_url"] = template.JS(fmt.Sprintf(ICE_SERVER_URL_TEMPLATE, "https", r.Host, httpsHostPort, ICE_SERVER_API_KEY))
data["ice_server_url"] = template.JS(fmt.Sprintf(ICE_SERVER_URL_TEMPLATE, "https", r.Host, ICE_SERVER_API_KEY))
} else {
// data["ice_server_url"] = template.JS(fmt.Sprintf(ICE_SERVER_URL_TEMPLATE, "http", r.Host, httpHostPort, ICE_SERVER_API_KEY))
data["ice_server_url"] = template.JS(fmt.Sprintf(ICE_SERVER_URL_TEMPLATE, "http", r.Host, ICE_SERVER_API_KEY))
}
data["ice_server_transports"] = getRequest(r, "tt", "")
var dtls, include_loopback_js string
debug := getRequest(r, "debug", "")
if strings.EqualFold(debug, "loopback") {
dtls = "false"
include_loopback_js = "<script src=\"/js/loopback.js\"></script>"
} else {
dtls = "true"
include_loopback_js = ""
}
data["include_loopback_js"] = include_loopback_js
data["ddtls"] = dtls
include_rtstats_js := ""
//todo:
//include_rtstats_js = "<script src=\"/js/rtstats.js\"></script><script src=\"/pako/pako.min.js\"></script>"
data["include_rtstats_js"] = include_rtstats_js
wss_url, wss_post_url := getWssParameters(r)
data["wss_url"] = template.URL(wss_url)
data["wss_post_url"] = template.URL(wss_post_url)
// bypass_join_confirmation = 'BYPASS_JOIN_CONFIRMATION' in os.environ and os.environ['BYPASS_JOIN_CONFIRMATION'] == 'True'
bypass_join_confirmation := false
data["bypass_join_confirmation"] = bypass_join_confirmation
data["version_info"] = template.JS(`{"time": "Thu Feb 9 15:54:29 2017 +0800", "branch": "master", "gitHash": "9d2692c3a32b1213584ec01cb5f12d462cb82d3e"}`)
data["callstats_params"] = template.JS(strings.Replace(CALLSTATS_PARAMS, "\n", "", -1))
if len(room_id) > 0 {
var room_link string
if isTLS(r) {
room_link = "https://" + r.Host + "/r/" + room_id + "?" + r.URL.RawQuery
} else {
room_link = "http://" + r.Host + "/r/" + room_id + "?" + r.URL.RawQuery
}
// room_link := r.Host + "/r/" + room_id + "?" + r.URL.RawQuery
// log.Println("host:",r.Host," url:",r.URL.String," uri:",r.URL.RequestURI)
data["room_id"] = room_id
data["room_link"] = template.URL(room_link)
}
if len(client_id) > 0 {
data["client_id"] = client_id
}
if is_initiator != nil {
data["is_initiator"] = is_initiator
}
return data
}
// var useTls bool
var wssHostPort int
var wsHostPort int
var httpsHostPort int
var httpHostPort int
// var wssHost string
// var flagUseTls = flag.Bool("tls", true, "whether TLS is used")
// var flagWssHostPort = flag.Int("wssport", 1443, "The TCP port that the server listens on")
// var flagWsHostPort = flag.Int("wsport", 2443, "The TCP port that the server listens on")
var flagHttpsHostPort = flag.Int("httpsport", 8888, "The https port that the server listens on")
var flagHttpHostPort = flag.Int("httpport", 8080, "The http port that the server listens on")
// var flagWssHost = flag.String("host", "192.168.2.30", "Enter your hostname or host ip")
var flagstun = flag.String("stun", "", "Enter stun server ip:port,for example 192.168.2.170:3478,default is null")
var flagturn = flag.String("turn", "", "Enter turn server ip:port,for example 192.168.2.170:3478,default is null")
var flagTurnUser = flag.String("turn-username", "", "Enter turn server username,default is null")
var flagTurnPassword = flag.String("turn-password", "", "Enter turn server user password,default is null")
var flagTurnSecret = flag.String("turn-static-auth-secret", "", "Enter turn server static auth secret,default is null")
var roomSrv = flag.String("room-server", "https://appr.tc", "The origin of the room server")
var CERT = flag.String("cert", "./mycert.pem", "cert pem file ")
var KEY = flag.String("key", "./mycert.key", "cert key file ")
var ice_server_url string
func main() {
flag.Parse()
// useTls = *flagUseTls
// wssHostPort = *flagWssHostPort
// wsHostPort = *flagWsHostPort
httpsHostPort = *flagHttpsHostPort
httpHostPort = *flagHttpHostPort
// wssHost = *flagWssHost
if len(*flagturn) > 0 {
if len(*flagTurnUser) == 0 {
log.Printf("If set turn server,must has turn-username")
return
}
if len(*flagTurnPassword) == 0 && len(*flagTurnSecret) == 0 {
log.Printf("If set turn server,must set turn-password or turn-static-auth-secret")
return
}
}
// TURN_SERVER_OVERRIDE += "["
// if len(*flagstun) > 0 {
// TURN_SERVER_OVERRIDE += fmt.Sprintf(STUN_SERVER_FMT, *flagstun)
// }
// if len(*flagturn) > 0 {
// if len(TURN_SERVER_OVERRIDE) > 0 {
// TURN_SERVER_OVERRIDE += ","
// }
// TURN_SERVER_OVERRIDE += fmt.Sprintf(TURN_SERVER_FMT, *flagturn)
// }
TURN_SERVER_OVERRIDE = "[" + TURN_SERVER_OVERRIDE + "]"
// log.Printf("TURN_SERVER_OVERRIDE:%s", TURN_SERVER_OVERRIDE)
RoomList = make(map[string]*Room)
WebServeMux := http.NewServeMux()
WebServeMux.Handle("/css/", http.FileServer(http.Dir("./")))
WebServeMux.Handle("/js/", http.FileServer(http.Dir("./")))
WebServeMux.Handle("/images/", http.FileServer(http.Dir("./")))
WebServeMux.Handle("/callstats/", http.FileServer(http.Dir("./")))
WebServeMux.Handle("/favicon.ico", http.FileServer(http.Dir("./")))
WebServeMux.Handle("/manifest.json", http.FileServer(http.Dir("./html/")))
WebServeMux.HandleFunc("/r/", roomPageHandler)
WebServeMux.HandleFunc("/join/", joinPageHandler)
WebServeMux.HandleFunc("/leave/", leavePageHandler)
WebServeMux.HandleFunc("/message/", messagePageHandler)
WebServeMux.HandleFunc("/params.html", paramsHTMLPageHandler)
WebServeMux.HandleFunc("/params.htm" , paramsHTMLPageHandler)
WebServeMux.HandleFunc("/params", paramsPageHandler)
WebServeMux.HandleFunc("/params/", paramsPageHandler)
WebServeMux.HandleFunc("/a/", aPageHandler)
WebServeMux.HandleFunc("/compute/", computePageHandler)
WebServeMux.HandleFunc("/iceconfig", iceconfigPageHandler)
WebServeMux.HandleFunc("/iceconfig/", iceconfigPageHandler)
WebServeMux.HandleFunc("/", mainPageHandler)
c := collider.NewCollider(*roomSrv)
c.AddHandle(WebServeMux)
// go c.Run(wssHostPort, wsHostPort, *CERT, *KEY)
var e error
httpsPstr := ":" + strconv.Itoa(httpsHostPort)
httpPstr := ":" + strconv.Itoa(httpHostPort)
// log.Println("Starting webrtc demo on port:", webHostPort, " tls:", useTls)
// 1Dj9XZ5fwvKS6YoQZOoORcFnXaI=
// log.Println("hmac:", Hmac("my_secret", "1433895918506:my_user_name"))
if len(*CERT) > 0 && len(*KEY) > 0 {
config := &tls.Config{
// Only allow ciphers that support forward secrecy for iOS9 compatibility:
// https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/
CipherSuites: []uint16{
tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
},
PreferServerCipherSuites: true,
}
log.Println("Starting webrtc demo on https port:", httpsHostPort)
server := &http.Server{Addr: httpsPstr, Handler: WebServeMux, TLSConfig: config}
go server.ListenAndServeTLS(*CERT, *KEY)
}
log.Println("Starting webrtc demo on http port:", httpHostPort)
e = http.ListenAndServe(httpPstr, WebServeMux)
if e != nil {
log.Fatal("Run: " + e.Error())
}
// http.ListenAndServe(":8888", nil)
}