-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
146 lines (131 loc) · 3.39 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
package main
import (
"crypto/tls"
"fmt"
"log"
"net"
"os"
"path/filepath"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/monitor"
"github.com/gofiber/fiber/v2/middleware/recover"
"golang.org/x/crypto/acme/autocert"
)
var envConfig = getEnvConfig()
func main() {
go Cleanup()
config := fiber.Config{
ServerHeader: "privtracker",
ReadTimeout: time.Second * 245,
WriteTimeout: time.Second * 15,
Network: fiber.NetworkTCP,
}
domains, tls := os.LookupEnv("DOMAINS")
if !tls {
config.EnableTrustedProxyCheck = true
config.TrustedProxies = []string{"127.0.0.1"}
config.ProxyHeader = fiber.HeaderXForwardedFor
}
app := fiber.New(config)
app.Use(recover.New())
// app.Use(pprof.New())
app.Use(myLogger())
app.Use(hsts)
app.Get("/", docs)
app.Static("/", "docs", fiber.Static{
MaxAge: 3600 * 24 * 7,
Compress: true,
CacheDuration: time.Hour,
})
app.Get("/dashboard", monitor.New())
app.Get("/:room/announce", announce)
app.Get("/:room/scrape", scrape)
app.Server().LogAllErrors = true
if tls {
go redirect80(config)
split := strings.Split(domains, ",")
log.Fatal(app.Listener(myListener(split...)))
} else {
log.Fatal(app.Listen(":" + envConfig.port))
}
}
type EnvConfig struct {
domain string
port string
}
func getEnvConfig() EnvConfig {
config := EnvConfig{
domain: "privtracker.com",
port: "1337",
}
port := os.Getenv("PORT")
domain := os.Getenv("DOMAIN")
if domain != "" {
config.domain = domain
}
if port != "" {
config.port = port
}
return config
}
func myListener(domains ...string) net.Listener {
homeDir, err := os.UserHomeDir()
if err != nil {
panic(err)
}
cacheDir := filepath.Join(homeDir, ".cache", "golang-autocert")
m := &autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(domains...),
Cache: autocert.DirCache(cacheDir),
}
cfg := &tls.Config{
GetCertificate: m.GetCertificate,
NextProtos: []string{
"http/1.1", "acme-tls/1",
},
// MinVersion: tls.VersionTLS12,
// CipherSuites: []uint16{
// tls.TLS_AES_128_GCM_SHA256,
// tls.TLS_AES_256_GCM_SHA384,
// tls.TLS_CHACHA20_POLY1305_SHA256,
// tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
// tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
// tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
// tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
// tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
// tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
// },
}
ln, err := tls.Listen("tcp", ":443", cfg)
if err != nil {
panic(err)
}
return ln
}
func redirect80(config fiber.Config) {
config.DisableStartupMessage = true
app := fiber.New(config)
app.Use(func(c *fiber.Ctx) error {
return c.Redirect(fmt.Sprintf("https://%s/", envConfig.domain), fiber.StatusMovedPermanently)
})
log.Print(app.Listen(":80"))
}
func myLogger() fiber.Handler {
loggerConfig := logger.ConfigDefault
loggerConfig.Format = "${status} - ${latency} ${ip} ${method} ${path} ${bytesSent} - ${referer} - ${ua}\n"
return logger.New(loggerConfig)
}
func hsts(c *fiber.Ctx) error {
c.Set("Strict-Transport-Security", "max-age=31536000")
return c.Next()
}
func docs(c *fiber.Ctx) error {
if c.Hostname() != envConfig.domain {
return c.Redirect(fmt.Sprintf("https://%s/", envConfig.domain), fiber.StatusMovedPermanently)
}
return c.Next()
}