-
Notifications
You must be signed in to change notification settings - Fork 135
/
galene.go
170 lines (151 loc) · 3.94 KB
/
galene.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
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"syscall"
"time"
"github.com/jech/galene/diskwriter"
"github.com/jech/galene/group"
"github.com/jech/galene/ice"
"github.com/jech/galene/limit"
"github.com/jech/galene/token"
"github.com/jech/galene/turnserver"
"github.com/jech/galene/webserver"
)
func main() {
var cpuprofile, memprofile, mutexprofile, httpAddr string
var udpRange string
flag.StringVar(&httpAddr, "http", ":8443", "web server `address`")
flag.StringVar(&webserver.StaticRoot, "static", "./static/",
"web server root `directory`")
flag.BoolVar(&webserver.Insecure, "insecure", false,
"act as an HTTP server rather than HTTPS")
flag.StringVar(&group.DataDirectory, "data", "./data/",
"data `directory`")
flag.StringVar(&group.Directory, "groups", "./groups/",
"group description `directory`")
flag.StringVar(&diskwriter.Directory, "recordings", "./recordings/",
"recordings `directory`")
flag.StringVar(&cpuprofile, "cpuprofile", "",
"store CPU profile in `file`")
flag.StringVar(&memprofile, "memprofile", "",
"store memory profile in `file`")
flag.StringVar(&mutexprofile, "mutexprofile", "",
"store mutex profile in `file`")
flag.StringVar(&udpRange, "udp-range", "",
"UDP port `range`")
flag.BoolVar(&group.UseMDNS, "mdns", false, "gather mDNS addresses")
flag.BoolVar(&ice.ICERelayOnly, "relay-only", false,
"require use of TURN relays for all media traffic")
flag.StringVar(&turnserver.Address, "turn", "auto",
"built-in TURN server `address` (\"\" to disable)")
flag.Parse()
if udpRange != "" {
var min, max uint16
n, err := fmt.Sscanf(udpRange, "%v-%v", &min, &max)
if err != nil {
log.Printf("UDP range: %v", err)
os.Exit(1)
}
if n != 2 || min <= 0 || max <= 0 || min > max {
log.Printf("UDP range: bad range")
os.Exit(1)
}
group.UDPMin = min
group.UDPMax = max
}
if cpuprofile != "" {
f, err := os.Create(cpuprofile)
if err != nil {
log.Printf("Create(cpuprofile): %v", err)
return
}
pprof.StartCPUProfile(f)
defer func() {
pprof.StopCPUProfile()
f.Close()
}()
}
if memprofile != "" {
defer func() {
f, err := os.Create(memprofile)
if err != nil {
log.Printf("Create(memprofile): %v", err)
return
}
pprof.WriteHeapProfile(f)
f.Close()
}()
}
if mutexprofile != "" {
runtime.SetMutexProfileFraction(1)
defer func() {
f, err := os.Create(mutexprofile)
if err != nil {
log.Printf("Create(mutexprofile): %v", err)
return
}
pprof.Lookup("mutex").WriteTo(f, 0)
f.Close()
}()
}
n, err := limit.Nofile()
if err != nil {
log.Printf("Couldn't get file descriptor limit: %v", err)
} else if n < 0xFFFF {
log.Printf("File descriptor limit is %v, please increase it!", n)
}
ice.ICEFilename = filepath.Join(group.DataDirectory, "ice-servers.json")
token.SetStatefulFilename(
filepath.Join(
filepath.Join(group.DataDirectory, "var"),
"tokens.jsonl",
),
)
// make sure the list of public groups is updated early
go group.Update()
// causes the built-in server to start if required
ice.Update()
defer turnserver.Stop()
err = webserver.Serve(httpAddr, group.DataDirectory)
if err != nil {
log.Fatalf("Server: %v", err)
}
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM)
go relayTest()
ticker := time.NewTicker(15 * time.Minute)
defer ticker.Stop()
slowTicker := time.NewTicker(12 * time.Hour)
defer slowTicker.Stop()
for {
select {
case <-ticker.C:
go func() {
group.Update()
token.Expire()
}()
case <-slowTicker.C:
go relayTest()
case <-terminate:
webserver.Shutdown()
return
}
}
}
func relayTest() {
now := time.Now()
d, err := ice.RelayTest(20 * time.Second)
if err != nil {
log.Printf("Relay test failed: %v", err)
log.Printf("Perhaps you didn't configure a TURN server?")
return
}
log.Printf("Relay test successful in %v, RTT = %v", time.Since(now), d)
}