-
-
Notifications
You must be signed in to change notification settings - Fork 235
/
main.go
194 lines (164 loc) · 4.56 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
// Package cmd contains an entrypoint for running an ion-sfu instance.
package main
import (
"flag"
"fmt"
"net"
"net/http"
_ "net/http/pprof"
"os"
"github.com/gorilla/websocket"
"github.com/pion/ion-sfu/cmd/signal/json-rpc/server"
log "github.com/pion/ion-sfu/pkg/logger"
"github.com/pion/ion-sfu/pkg/middlewares/datachannel"
"github.com/pion/ion-sfu/pkg/sfu"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sourcegraph/jsonrpc2"
websocketjsonrpc2 "github.com/sourcegraph/jsonrpc2/websocket"
"github.com/spf13/viper"
)
// logC need to get logger options from config
type logC struct {
Config log.GlobalConfig `mapstructure:"log"`
}
var (
conf = sfu.Config{}
file string
cert string
key string
addr string
metricsAddr string
verbosityLevel int
logConfig logC
logger = log.New()
)
const (
portRangeLimit = 100
)
func showHelp() {
fmt.Printf("Usage:%s {params}\n", os.Args[0])
fmt.Println(" -c {config file}")
fmt.Println(" -cert {cert file}")
fmt.Println(" -key {key file}")
fmt.Println(" -a {listen addr}")
fmt.Println(" -h (show help info)")
fmt.Println(" -v {0-10} (verbosity level, default 0)")
}
func load() bool {
_, err := os.Stat(file)
if err != nil {
return false
}
viper.SetConfigFile(file)
viper.SetConfigType("toml")
err = viper.ReadInConfig()
if err != nil {
logger.Error(err, "config file read failed", "file", file)
return false
}
err = viper.GetViper().Unmarshal(&conf)
if err != nil {
logger.Error(err, "sfu config file loaded failed", "file", file)
return false
}
if len(conf.WebRTC.ICEPortRange) > 2 {
logger.Error(nil, "config file loaded failed. webrtc port must be [min,max]", "file", file)
return false
}
if len(conf.WebRTC.ICEPortRange) != 0 && conf.WebRTC.ICEPortRange[1]-conf.WebRTC.ICEPortRange[0] < portRangeLimit {
logger.Error(nil, "config file loaded failed. webrtc port must be [min, max] and max - min >= portRangeLimit", "file", file, "portRangeLimit", portRangeLimit)
return false
}
if len(conf.Turn.PortRange) > 2 {
logger.Error(nil, "config file loaded failed. turn port must be [min,max]", "file", file)
return false
}
if logConfig.Config.V < 0 {
logger.Error(nil, "Logger V-Level cannot be less than 0")
return false
}
logger.V(0).Info("Config file loaded", "file", file)
return true
}
func parse() bool {
flag.StringVar(&file, "c", "config.toml", "config file")
flag.StringVar(&cert, "cert", "", "cert file")
flag.StringVar(&key, "key", "", "key file")
flag.StringVar(&addr, "a", ":7000", "address to use")
flag.StringVar(&metricsAddr, "m", ":8100", "merics to use")
flag.IntVar(&verbosityLevel, "v", -1, "verbosity level, higher value - more logs")
help := flag.Bool("h", false, "help info")
flag.Parse()
if !load() {
return false
}
if *help {
return false
}
return true
}
func startMetrics(addr string) {
// start metrics server
m := http.NewServeMux()
m.Handle("/metrics", promhttp.Handler())
srv := &http.Server{
Handler: m,
}
metricsLis, err := net.Listen("tcp", addr)
if err != nil {
logger.Error(err, "cannot bind to metrics endpoint", "addr", addr)
os.Exit(1)
}
logger.Info("Metrics Listening", "addr", addr)
err = srv.Serve(metricsLis)
if err != nil {
logger.Error(err, "Metrics server stopped")
}
}
func main() {
if !parse() {
showHelp()
os.Exit(-1)
}
// Check that the -v is not set (default -1)
if verbosityLevel < 0 {
verbosityLevel = logConfig.Config.V
}
log.SetGlobalOptions(log.GlobalConfig{V: verbosityLevel})
logger.Info("--- Starting SFU Node ---")
// Pass logr instance
sfu.Logger = logger
s := sfu.NewSFU(conf)
dc := s.NewDatachannel(sfu.APIChannelLabel)
dc.Use(datachannel.SubscriberAPI)
upgrader := websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
ReadBufferSize: 1024,
WriteBufferSize: 1024,
}
http.Handle("/ws", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
panic(err)
}
defer c.Close()
p := server.NewJSONSignal(sfu.NewPeer(s), logger)
defer p.Close()
jc := jsonrpc2.NewConn(r.Context(), websocketjsonrpc2.NewObjectStream(c), p)
<-jc.DisconnectNotify()
}))
go startMetrics(metricsAddr)
var err error
if key != "" && cert != "" {
logger.Info("Started listening", "addr", "https://"+addr)
err = http.ListenAndServeTLS(addr, cert, key, nil)
} else {
logger.Info("Started listening", "addr", "http://"+addr)
err = http.ListenAndServe(addr, nil)
}
if err != nil {
panic(err)
}
}