privtracker/main.go

122 lines
3 KiB
Go
Raw Normal View History

2020-12-06 15:36:57 -06:00
package main
import (
2022-03-21 15:56:11 -05:00
"crypto/tls"
"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"
2020-12-06 15:36:57 -06:00
)
func main() {
go Cleanup()
2022-03-21 15:56:11 -05:00
config := fiber.Config{
ServerHeader: "privtracker",
2022-03-22 13:45:25 -05:00
ReadTimeout: time.Second * 245,
2022-03-23 14:23:40 -05:00
WriteTimeout: time.Second * 15,
2022-03-21 15:56:11 -05:00
}
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())
2022-11-09 03:11:53 -06:00
// app.Use(pprof.New())
2022-03-21 15:56:11 -05:00
app.Use(myLogger())
2022-03-22 12:14:58 -05:00
app.Use(hsts)
2022-03-21 15:56:11 -05:00
app.Get("/", docs)
2022-03-22 13:44:23 -05:00
app.Static("/", "docs", fiber.Static{
MaxAge: 3600 * 24 * 7,
Compress: true,
CacheDuration: time.Hour,
})
2022-03-21 15:56:11 -05:00
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, ",")
2022-03-22 12:14:58 -05:00
log.Fatal(app.Listener(myListener(split...)))
2022-03-21 15:56:11 -05:00
} else {
port := os.Getenv("PORT")
if port == "" {
port = "1337"
}
log.Fatal(app.Listen(":" + port))
}
}
2022-03-22 12:14:58 -05:00
func myListener(domains ...string) net.Listener {
2022-03-21 15:56:11 -05:00
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,
// },
2022-03-21 15:56:11 -05:00
}
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("https://privtracker.com/", 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)
}
2022-03-22 12:14:58 -05:00
func hsts(c *fiber.Ctx) error {
c.Set("Strict-Transport-Security", "max-age=31536000")
return c.Next()
}
2022-03-21 15:56:11 -05:00
func docs(c *fiber.Ctx) error {
if c.Hostname() != "privtracker.com" {
return c.Redirect("https://privtracker.com/", fiber.StatusMovedPermanently)
}
return c.Next()
2020-12-06 15:36:57 -06:00
}