forked from voc/srtrelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
151 lines (134 loc) · 3.46 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
package main
import (
"context"
"flag"
"io"
"log"
"net"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"strings"
"syscall"
"github.com/haivision/srtgo"
"github.com/voc/srtrelay/api"
"github.com/voc/srtrelay/config"
"github.com/voc/srtrelay/relay"
"github.com/voc/srtrelay/srt"
)
func main() {
// allow specifying config path
configFlags := flag.NewFlagSet("config", flag.ContinueOnError)
configFlags.SetOutput(io.Discard)
configPath := configFlags.String("config", "config.toml", "")
configFlags.Parse(os.Args[1:])
// parse config
conf, err := config.Parse([]string{*configPath, "/etc/srtrelay/config.toml"})
if err != nil {
log.Fatal(err)
}
// flag just for usage
flag.String("config", "config.toml", "path to config file")
// actual flags, use config as default and storage
var addresses string
flag.StringVar(&addresses, "addresses", strings.Join(conf.App.Addresses, ","), "relay bind addresses, separated by commata")
flag.UintVar(&conf.App.Latency, "latency", conf.App.Latency, "srt protocol latency in ms")
flag.UintVar(&conf.App.Buffersize, "buffersize", conf.App.Buffersize,
`relay buffer size in bytes, determines maximum delay of a client`)
profile := flag.String("pprof", "", "enable profiling server on given address")
flag.Parse()
if *profile != "" {
log.Println("Enabling profiling on", *profile)
if err := enablePprof(*profile); err != nil {
log.Println("failed to enable profiling:", err)
}
}
conf.App.Addresses = strings.Split(addresses, ",")
auth, err := config.GetAuthenticator(conf.Auth)
if err != nil {
log.Println(err)
}
serverConfig := srt.Config{
Server: srt.ServerConfig{
Addresses: conf.App.Addresses,
PublicAddress: conf.App.PublicAddress,
Latency: conf.App.Latency,
LossMaxTTL: conf.App.LossMaxTTL,
SyncClients: conf.App.SyncClients,
Auth: auth,
ListenBacklog: conf.App.ListenBacklog,
},
Relay: relay.RelayConfig{
BufferSize: conf.App.Buffersize,
PacketSize: conf.App.PacketSize,
},
}
// setup graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
handleSignal(ctx, cancel)
// create server
srtgo.InitSRT()
srtServer := srt.NewServer(&serverConfig)
err = srtServer.Listen(ctx)
if err != nil {
log.Fatal(err)
}
var apiServer *api.Server
if conf.API.Enabled {
apiServer = api.NewServer(conf.API, srtServer)
err := apiServer.Listen(ctx)
if err != nil {
log.Fatal(err)
}
log.Printf("API listening on %s\n", conf.API.Address)
}
// Wait for graceful shutdown
srtServer.Wait()
if apiServer != nil {
apiServer.Wait()
}
srtgo.CleanupSRT()
}
func enablePprof(addr string) error {
conn, err := net.Listen("tcp", addr)
if err != nil {
return err
}
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
srv := http.Server{
Handler: mux,
}
go func() {
err := srv.Serve(conn)
if err != nil && err != http.ErrServerClosed {
log.Println(err)
}
}()
return nil
}
func handleSignal(ctx context.Context, cancel context.CancelFunc) {
// Set up channel on which to send signal notifications.
// We must use a buffered channel or risk missing the signal
// if we're not ready to receive when the signal is sent.
c := make(chan os.Signal, 1)
signal.Notify(c,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM)
go func() {
for {
select {
case <-ctx.Done():
return
case s := <-c:
log.Println("caught signal", s)
if s == syscall.SIGHUP {
continue
}
cancel()
}
}
}()
}