-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmain.go
157 lines (133 loc) · 3.29 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
package main
import (
"context"
"crypto/rand"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"codeberg.org/UnifiedPush/common-proxies/config"
. "codeberg.org/UnifiedPush/common-proxies/config"
"codeberg.org/UnifiedPush/common-proxies/vapid"
)
var configFile = flag.String("c", "config.toml", "path to toml file for config")
var genVapidFlag = flag.Bool("vapid", false, "Generate a new VAPID private key and exit")
// various translaters
var handlers = []Handler{}
func init() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
}
func genVapid() {
private, err := vapid.GenerateKey(rand.Reader)
if err != nil {
log.Fatalln(err)
}
out, err := vapid.EncodePriv(*private)
fmt.Println(out)
}
func main() {
flag.Parse()
if *genVapidFlag {
genVapid()
return
}
err := ParseConf(*configFile)
if err != nil {
log.Fatalln(err)
}
log.Println("Starting", Config.GetUserAgent())
handlers = []Handler{
&Config.Rewrite.FCM,
&Config.Rewrite.WebPushFCM,
&Config.Gateway.Matrix,
&Config.Gateway.Generic,
&Config.Gateway.Aesgcm,
}
myRouter := http.NewServeMux()
stopTickers := make(chan bool)
for _, i := range handlers {
i.Load()
if i.Path() != "" {
myRouter.HandleFunc(i.Path(), handle(i))
if config.Config.Verbose {
fmt.Println("Handling", i.Path())
}
}
handleTicker(i, stopTickers)
}
myRouter.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(config.Config.GetUserAgent() + " OK"))
})
myRouter.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Endpoint doesn't exist\n"))
})
server := &http.Server{
Addr: Config.ListenAddr,
Handler: myRouter,
}
done := make(chan bool)
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGHUP)
go func() {
for {
switch <-quit {
case syscall.SIGHUP:
err := config.ParseConf(*configFile)
if err != nil {
log.Println("Unable to reload config: ", err)
}
case os.Interrupt:
log.Println("Server is shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
stopTickers <- true
server.SetKeepAlivesEnabled(false)
if err := server.Shutdown(ctx); err != nil {
log.Fatalf("Could not gracefully shutdown the server: %v\n", err)
}
close(done)
return
default:
log.Println("UNKNOWN SIGNAL")
}
}
}()
log.Println("Server is ready to handle requests at", Config.ListenAddr)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Could not listen on %s: %v\n", Config.ListenAddr, err)
}
<-done
log.Println("Server stopped")
}
func handle(handler Handler) HttpHandler {
if h, ok := handler.(Gateway); ok {
return bothHandler(gatewayHandler(h))
} else if h, ok := handler.(Proxy); ok {
return bothHandler(proxyHandler(h))
} else {
//should be const so np abt fatal
log.Fatalf("UNABLE TO HANDLE HANDLER %#v\n", handler)
return nil
}
}
func handleTicker(handler Handler, done chan bool) {
if h, ok := handler.(TickerHandler); ok {
ticker := time.NewTicker(h.Duration())
go func() {
for {
select {
case <-done:
return
case <-ticker.C:
h.Tick()
}
}
}()
}
}