/
cdn.go
76 lines (64 loc) · 2.71 KB
/
cdn.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
// Package cdn Package example is a CoreDNS plugin that prints "example" to stdout on every packet received.
//
// It serves as an example CoreDNS plugin with numerous code comments.
package cdn
import (
"context"
"fmt"
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metadata"
"github.com/coredns/coredns/plugin/metrics"
clog "github.com/coredns/coredns/plugin/pkg/log"
"github.com/miekg/dns"
"strconv"
)
// Define log to be a logger with the plugin name in it. This way we can just use log.Info and
// friends to log.
var log = clog.NewWithPlugin("cdn")
// CDN Example is an example plugin to show how to write a plugin.
type CDN struct {
Next plugin.Handler
}
// ServeDNS implements the plugin.Handler interface. This method gets called when example is used
// in a Server.
func (e CDN) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
if getContinentCode := metadata.ValueFunc(ctx, "geoip/continent/code"); getContinentCode != nil {
if name, err := strconv.ParseComplex(getContinentCode(), 64); err == nil {
fmt.Println(name)
}
}
//
// This function could be simpler. I.e. just fmt.Println("example") here, but we want to show
// a slightly more complex example as to make this more interesting.
// Here we wrap the dns.ResponseWriter in a new ResponseWriter and call the next plugin, when the
// answer comes back, it will print "example".
// Debug log that we've have seen the query. This will only be shown when the debug plugin is loaded.
log.Debug("Received response", metadata.ValueFunc(ctx, "geoip/timezone"))
//
// if getLongitude := metadata.ValueFunc(ctx, "geoip/longitude"); getLongitude != nil {
// if longitude, err := strconv.ParseFloat(getLongitude(), 64); err == nil {
// // Do something useful with longitude.
// }
// }
// Wrap.
pw := NewResponsePrinter(w)
// Export metric with the server label set to the current server handling the request.
requestCount.WithLabelValues(metrics.WithServer(ctx)).Inc()
// Call next plugin (if any).
return plugin.NextOrFailure(e.Name(), e.Next, ctx, pw, r)
}
// Name implements the Handler interface.
func (e CDN) Name() string { return "cdn" }
// ResponsePrinter wrap a dns.ResponseWriter and will write example to standard output when WriteMsg is called.
type ResponsePrinter struct {
dns.ResponseWriter
}
// NewResponsePrinter returns ResponseWriter.
func NewResponsePrinter(w dns.ResponseWriter) *ResponsePrinter {
return &ResponsePrinter{ResponseWriter: w}
}
// WriteMsg calls the underlying ResponseWriter's WriteMsg method and prints "example" to standard output.
func (r *ResponsePrinter) WriteMsg(res *dns.Msg) error {
log.Info("cdn")
return r.ResponseWriter.WriteMsg(res)
}