-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
126 lines (109 loc) · 3.06 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
package main
import (
"flag"
"fmt"
"log"
"os"
"github.com/coocood/freecache"
gobaresip "github.com/negbie/go-baresip"
)
const v = "v0.1.1"
func main() {
debug := flag.Bool("debug", false, "Set debug mode")
ctrlAddr := flag.String("ctrl_address", "127.0.0.1:4444", "Local control listen address")
guiAddr := flag.String("gui_address", "0.0.0.0:8080", "Local GUI listen address")
logStd := flag.Bool("log_stderr", true, "Log to stderr")
lokiURL := flag.String("loki_url", "", "URL to remote Loki server like http://localhost:3100")
maxCalls := flag.Uint("max_calls", 30, "Maximum number of incoming calls")
rtpNet := flag.String("rtp_interface", "", "RTP interface like eth0")
rtpPorts := flag.String("rtp_ports", "10000-11000", "RTP port range")
rtpTimeout := flag.Uint("rtp_timeout", 10, "Seconds after which a call with no incoming RTP packets will be terminated")
sipAddr := flag.String("sip_address", "", "SIP listen address like 0.0.0.0:5060")
version := flag.Bool("version", false, "Print version")
webhookDelay := flag.Uint("webhook_delay", 600, "Webhook resend delay of warnings and errors in seconds")
webhookURL := flag.String("webhook_url", "", "Send warnings and errors to this Mattermost or Slack webhook URL")
flag.Parse()
if *version {
fmt.Println(v)
os.Exit(0)
}
createConfig(*maxCalls, *rtpNet, *rtpPorts, *rtpTimeout, *ctrlAddr, *sipAddr)
if *webhookDelay == 0 {
*webhookDelay = 1
}
var cache = freecache.NewCache(10 * 1024 * 1024)
var loki *LokiClient
var err error
if *lokiURL != "" {
loki, err = NewLokiClient(*lokiURL, 20, 4)
if err != nil {
log.Fatal(err)
}
defer loki.Close()
}
var lokiELabels = map[string]string{
"job": "go-baresip",
"level": "info",
}
var lokiRLabels = map[string]string{
"job": "go-baresip",
"level": "info",
}
gb, err := gobaresip.New(
gobaresip.SetAudioPath("sounds"),
gobaresip.SetConfigPath("."),
gobaresip.SetDebug(*debug),
gobaresip.SetUserAgent("telefonist"),
gobaresip.SetCtrlTCPAddr(*ctrlAddr),
gobaresip.SetWsAddr(*guiAddr),
)
if err != nil {
log.Fatal(err)
}
eChan := gb.GetEventChan()
rChan := gb.GetResponseChan()
go func() {
for {
select {
case e, ok := <-eChan:
if !ok {
return
}
level := eventLevel(&e)
if *lokiURL != "" {
lokiELabels["level"] = level
loki.Send(lokiELabels, string(e.RawJSON))
}
if *webhookURL != "" && (level == "warning" || level == "error") {
go func() {
key := []byte(e.AccountAOR + e.PeerURI + e.Param)
if _, err := cache.Get(key); err != nil {
if err := page(*webhookURL, level, string(e.RawJSON)); err != nil {
log.Println(err)
}
cache.Set(key, nil, int(*webhookDelay))
}
}()
}
if *logStd {
log.Println(level, ":", string(e.RawJSON))
}
case r, ok := <-rChan:
if !ok {
return
}
if *lokiURL != "" {
loki.Send(lokiRLabels, string(r.RawJSON))
}
if *logStd {
log.Println("info", ":", string(r.RawJSON))
}
}
}
}()
err = gb.Run()
if err != nil {
log.Println(err)
}
gb.Close()
}