/
api.go
151 lines (120 loc) · 3.66 KB
/
api.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
package api
import (
"context"
"encoding/base64"
"net/http"
"net/http/pprof"
"os"
"strings"
"time"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/semihalev/log"
"github.com/semihalev/sdns/config"
"github.com/semihalev/sdns/dnsutil"
"github.com/semihalev/sdns/middleware"
"github.com/semihalev/sdns/middleware/blocklist"
)
// API type
type API struct {
addr string
router *Router
blocklist *blocklist.BlockList
}
var debugpprof bool
func init() {
_, debugpprof = os.LookupEnv("SDNS_PPROF")
}
// New return new api
func New(cfg *config.Config) *API {
var bl *blocklist.BlockList
b := middleware.Get("blocklist")
if b != nil {
bl = b.(*blocklist.BlockList)
}
a := &API{
addr: cfg.API,
blocklist: bl,
router: NewRouter(),
}
return a
}
func (a *API) existsBlock(ctx *Context) {
ctx.JSON(http.StatusOK, Json{"exists": a.blocklist.Exists(ctx.Param("key"))})
}
func (a *API) getBlock(ctx *Context) {
if ok, _ := a.blocklist.Get(ctx.Param("key")); !ok {
ctx.JSON(http.StatusNotFound, Json{"error": ctx.Param("key") + " not found"})
} else {
ctx.JSON(http.StatusOK, Json{"success": ok})
}
}
func (a *API) removeBlock(ctx *Context) {
ctx.JSON(http.StatusOK, Json{"success": a.blocklist.Remove(ctx.Param("key"))})
}
func (a *API) setBlock(ctx *Context) {
ctx.JSON(http.StatusOK, Json{"success": a.blocklist.Set(ctx.Param("key"))})
}
func (a *API) metrics(ctx *Context) {
promhttp.Handler().ServeHTTP(ctx.Writer, ctx.Request)
}
func (a *API) purge(ctx *Context) {
qtype := strings.ToUpper(ctx.Param("qtype"))
qname := dns.Fqdn(ctx.Param("qname"))
bqname := base64.StdEncoding.EncodeToString([]byte(qtype + ":" + qname))
req := new(dns.Msg)
req.SetQuestion(dns.Fqdn(bqname), dns.TypeNULL)
req.Question[0].Qclass = dns.ClassCHAOS
_, _ = dnsutil.ExchangeInternal(context.Background(), req)
ctx.JSON(http.StatusOK, Json{"success": true})
}
// Run API server
func (a *API) Run(ctx context.Context) {
if a.addr == "" {
return
}
if debugpprof {
profiler := a.router.Group("/debug")
{
profiler.GET("/", func(ctx *Context) {
http.Redirect(ctx.Writer, ctx.Request, profiler.path+"/pprof/", http.StatusMovedPermanently)
})
profiler.GET("/pprof/", func(ctx *Context) { pprof.Index(ctx.Writer, ctx.Request) })
profiler.GET("/pprof/*", func(ctx *Context) { pprof.Index(ctx.Writer, ctx.Request) })
profiler.GET("/pprof/cmdline", func(ctx *Context) { pprof.Cmdline(ctx.Writer, ctx.Request) })
profiler.GET("/pprof/profile", func(ctx *Context) { pprof.Profile(ctx.Writer, ctx.Request) })
profiler.GET("/pprof/symbol", func(ctx *Context) { pprof.Symbol(ctx.Writer, ctx.Request) })
profiler.GET("/pprof/trace", func(ctx *Context) { pprof.Trace(ctx.Writer, ctx.Request) })
}
}
if a.blocklist != nil {
block := a.router.Group("/api/v1/block")
{
block.GET("/exists/:key", a.existsBlock)
block.GET("/get/:key", a.getBlock)
block.GET("/remove/:key", a.removeBlock)
block.GET("/set/:key", a.setBlock)
}
}
a.router.GET("/api/v1/purge/:qname/:qtype", a.purge)
a.router.GET("/metrics", a.metrics)
srv := &http.Server{
Addr: a.addr,
Handler: a.router,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Error("Start API server failed", "error", err.Error())
}
}()
log.Info("API server listening...", "addr", a.addr)
go func() {
<-ctx.Done()
log.Info("API server stopping...", "addr", a.addr)
apiCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(apiCtx); err != nil {
log.Error("Shutdown API server failed:", "error", err.Error())
}
}()
}