-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
101 lines (85 loc) · 2.82 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
package main
import (
"bytes"
"context"
"errors"
"io"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/caarlos0/env/v11"
"github.com/gin-gonic/gin"
"github.com/0xfelix/hetzner-dnsapi-proxy/pkg/config"
"github.com/0xfelix/hetzner-dnsapi-proxy/pkg/data"
"github.com/0xfelix/hetzner-dnsapi-proxy/pkg/status"
"github.com/0xfelix/hetzner-dnsapi-proxy/pkg/update"
)
const (
readHeaderTimeout = 10
shutdownTimeout = 5
)
func runServer(listenAddr string, r *gin.Engine) error {
s := &http.Server{
Addr: listenAddr,
Handler: r,
ReadHeaderTimeout: readHeaderTimeout * time.Second,
}
go func() {
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Fatal(err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.Println("Shutting down hetzner-dnsapi-proxy")
c, cancel := context.WithTimeout(context.Background(), shutdownTimeout*time.Second)
defer cancel()
return s.Shutdown(c)
}
func main() {
cfg := &config.Config{}
if err := env.ParseWithOptions(cfg, env.Options{RequiredIfNoDef: true}); err != nil {
log.Fatal(err)
}
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
if len(cfg.TrustedProxies) > 0 {
if err := r.SetTrustedProxies(cfg.TrustedProxies); err != nil {
log.Fatal(err)
}
}
c := update.NewController(cfg)
r.GET("/plain/update", buildChain(cfg, data.BindPlain(), c.CheckPermissions(), c.UpdateDNS(), status.Ok)...)
r.POST("/acmedns/update", buildChain(cfg, data.BindAcmeDNS(), c.CheckPermissions(), c.UpdateDNS(), status.OkAcmeDNS)...)
r.POST("/acmedns/register", buildChain(cfg, status.Ok)...)
r.POST("/httpreq/present", buildChain(cfg, data.BindHTTPReq(), c.CheckPermissions(), c.UpdateDNS(), status.Ok)...)
r.POST("/httpreq/cleanup", buildChain(cfg, status.Ok)...)
r.GET("/directadmin/CMD_API_SHOW_DOMAINS", buildChain(cfg, data.ShowDomainsDirectAdmin(cfg.AllowedDomains))...)
r.GET("/directadmin/CMD_API_DOMAIN_POINTER", buildChain(cfg, status.Ok)...)
r.GET("/directadmin/CMD_API_DNS_CONTROL",
buildChain(cfg, data.BindDirectAdmin(), c.CheckPermissions(), c.UpdateDNS(), status.OkDirectAdmin)...)
log.Printf("Starting hetzner-dnsapi-proxy, listening on %s\n", cfg.ListenAddr)
if err := runServer(cfg.ListenAddr, r); err != nil {
log.Fatal("Error running server:", err)
}
}
func buildChain(cfg *config.Config, handlers ...gin.HandlerFunc) gin.HandlersChain {
if cfg.Debug {
handlers = append([]gin.HandlerFunc{requestLoggerMiddleware()}, handlers...)
}
return handlers
}
func requestLoggerMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
var buf bytes.Buffer
body, _ := io.ReadAll(io.TeeReader(c.Request.Body, &buf))
c.Request.Body = io.NopCloser(&buf)
log.Printf("BODY %s", string(body))
log.Printf("HEADER %+v", c.Request.Header)
c.Next()
}
}