-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.go
131 lines (99 loc) · 3 KB
/
admin.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
package main
import (
"errors"
"fmt"
"net/http"
"time"
"github.com/diegohce/droneip/config"
"github.com/diegohce/droneip/dronebl"
"github.com/diegohce/droneip/healthcheck"
"github.com/diegohce/droneip/internal/version"
"github.com/diegohce/droneip/ctcodecs"
_ "github.com/diegohce/droneip/ctcodecs/allcodecs"
"github.com/diegohce/droneip/logger"
mx2 "github.com/diegohce/droneip/mxcache"
"github.com/go-redis/redis/v8"
)
type redisCache interface {
RedisClient() *redis.Client
}
type AdminCentre struct {
cache mx2.MXCacher
r *http.ServeMux
}
func NewAdminCentre(cache mx2.MXCacher) *AdminCentre {
r := http.ServeMux{}
ac := AdminCentre{cache, &r}
hc := healthcheck.HealthCheck(cache)
ac.r.Handle("GET /droneip/keys", http.HandlerFunc(ac.cacheKeys))
ac.r.Handle("GET /droneip/version", &version.Handler)
ac.r.Handle("GET /droneip/health_check", hc)
ac.r.Handle("POST /droneip/is_valid", http.HandlerFunc(ac.ipIsValid))
return &ac
}
func (a *AdminCentre) ServeHTTP(w http.ResponseWriter, r *http.Request) {
a.r.ServeHTTP(w, r)
}
func (a *AdminCentre) cacheKeys(w http.ResponseWriter, r *http.Request) {
codec, err := ctcodecs.New(r.Header.Get("Content-Type"))
if err != nil {
codec, _ = ctcodecs.New("application/json")
}
redCache, ok := a.cache.(redisCache)
if !ok {
logger.LogError("cache keys only implemented for redis cache").Write()
http.Error(w, "cache keys only implemented for redis cache", http.StatusBadRequest)
return
}
rc := redCache.RedisClient()
ctx := r.Context()
result := rc.Keys(ctx, "droneip-*")
if result.Err() != nil {
logger.LogError("error getting keys from cache backend", "error", result.Err().Error()).Write()
http.Error(w, "error getting keys from cache backend: "+result.Err().Error(), http.StatusBadRequest)
return
}
response := struct {
Keys []string `json:"keys"`
}{
Keys: result.Val(),
}
codec.NewEncoder(w).Encode(response)
}
func (a *AdminCentre) ipIsValid(w http.ResponseWriter, r *http.Request) {
rq := struct {
Addr string `json:"addr"`
}{}
ttl, err := time.ParseDuration(config.Get("CACHE_TTL", "24h"))
if err != nil {
ttl, _ = time.ParseDuration("24h")
}
codec, err := ctcodecs.New(r.Header.Get("Content-Type"))
if err != nil {
codec, _ = ctcodecs.New("application/json")
}
if err = codec.NewDecoder(r.Body).Decode(&rq); err != nil {
logger.LogError("error decoding request body", "err", err.Error()).Write()
http.Error(w, "error decoding request body: "+err.Error(), http.StatusBadRequest)
return
}
cacheKey := fmt.Sprintf("droneip-%s", rq.Addr)
cv := CacheValue{}
err = a.cache.Get(cacheKey, &cv)
if errors.Is(err, mx2.ErrNotFound) {
if err := dronebl.Probe(rq.Addr); err != nil {
logger.LogInfo("ip exists in dronebl DB",
"ip", rq.Addr, "source", "dronebl.org").Write()
cv.ValidIP = false
} else {
cv.ValidIP = true
}
a.cache.Set(cacheKey, &cv, int(ttl.Seconds()))
}
res := struct {
IsValid bool `json:"is_valid"`
}{
IsValid: cv.ValidIP,
}
codec.NewEncoder(w).Encode(&res)
}