summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorHandingkang <[email protected]>2023-06-14 18:17:01 +0800
committerHandingkang <[email protected]>2023-06-14 18:17:01 +0800
commitcebabedcfa7fa724900aa34addaaf58b202d08e8 (patch)
tree588b36aa4fc78368d27c5350b191e32473a626df /core
parent5ba3fe92c584971b370126f89c3527e3d1941233 (diff)
1. 修复whoami插件错误问题
2. reuseport插件适配windows和linux环境切换
Diffstat (limited to 'core')
-rw-r--r--core/dnsserver/server.go125
1 files changed, 64 insertions, 61 deletions
diff --git a/core/dnsserver/server.go b/core/dnsserver/server.go
index 396c530..f929a68 100644
--- a/core/dnsserver/server.go
+++ b/core/dnsserver/server.go
@@ -3,6 +3,8 @@ package dnsserver
import (
"context"
"fmt"
+ "github.com/coredns/caddy"
+ "github.com/miekg/dns"
ot "github.com/opentracing/opentracing-go"
"net"
"ohmydns2/plugin"
@@ -19,11 +21,13 @@ import (
"strings"
"sync"
"time"
-
- "github.com/coredns/caddy"
- "github.com/miekg/dns"
)
+// Server represents an instance of a server, which serves
+// DNS requests at a particular address (host and port). A
+// server is capable of serving numerous zones on
+// the same address and the listener may be stopped for
+// graceful termination (POSIX only).
type Server struct {
Addr string // Address we listen on
@@ -44,17 +48,6 @@ type Server struct {
tsigSecret map[string]string
}
-type (
- // Key is the context key for the current server added to the context.
- Key struct{}
-
- // LoopKey is the context key to detect server wide loops.
- LoopKey struct{}
-
- // ViewKey is the context key for the current view, if defined
- ViewKey struct{}
-)
-
// MetadataCollector is a plugin that can retrieve metadata functions from all metadata providing plugins
type MetadataCollector interface {
Collect(context.Context, request.Request) context.Context
@@ -195,18 +188,19 @@ func (s *Server) Listen() (net.Listener, error) {
return l, nil
}
+// WrapListener Listen implements caddy.GracefulServer interface.
+func (s *Server) WrapListener(ln net.Listener) net.Listener {
+ return ln
+}
+
// ListenPacket implements caddy.UDPServer interface.
func (s *Server) ListenPacket() (net.PacketConn, error) {
p, err := reuseport.ListenPacket("udp", s.Addr[len(transport.DNS+"://"):])
if err != nil {
return nil, err
}
- return p, nil
-}
-// WrapListener Listen implements caddy.GracefulServer interface.
-func (s *Server) WrapListener(ln net.Listener) net.Listener {
- return ln
+ return p, nil
}
// Stop stops the server. It blocks until the server is
@@ -245,10 +239,8 @@ func (s *Server) Stop() (err error) {
return
}
-// Address implement caddy.GracefulServer.
-func (s *Server) Address() string {
- return s.Addr
-}
+// Address together with Stop() implement caddy.GracefulServer.
+func (s *Server) Address() string { return s.Addr }
// ServeDNS is the entry point for every request to the address that
// is bound to. It acts as a multiplexer for the requests zonename as
@@ -379,22 +371,37 @@ func (s *Server) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg)
errorAndMetricsFunc(s.Addr, w, r, dns.RcodeRefused)
}
-const (
- tcp = 0
- udp = 1
+// passAllFilterFuncs returns true if all filter funcs evaluate to true for the given request
+func passAllFilterFuncs(ctx context.Context, filterFuncs []FilterFunc, req *request.Request) bool {
+ for _, ff := range filterFuncs {
+ if !ff(ctx, req) {
+ return false
+ }
+ }
+ return true
+}
- tcpMaxQueries = -1
-)
+// OnStartupComplete lists the sites served by this server
+// and any relevant information, assuming Quiet is false.
+func (s *Server) OnStartupComplete() {
+ if Quiet {
+ return
+ }
-// EnableChaos is a map with plugin names for which we should open CH class queries as we block these by default.
-var EnableChaos = map[string]struct{}{
- "chaos": {},
- "forward": {},
- "proxy": {},
+ out := startUpZones("", s.Addr, s.zones)
+ if out != "" {
+ fmt.Print(out)
+ }
}
-// Quiet 模式在初始化时将不显示任何内容
-var Quiet bool
+// Tracer returns the tracer in the server if defined.
+func (s *Server) Tracer() ot.Tracer {
+ if s.trace == nil {
+ return nil
+ }
+
+ return s.trace.Tracer()
+}
// errorFunc responds to an DNS request with an error.
func errorFunc(server string, w dns.ResponseWriter, r *dns.Msg, rc int) {
@@ -419,34 +426,30 @@ func errorAndMetricsFunc(server string, w dns.ResponseWriter, r *dns.Msg, rc int
w.WriteMsg(answer)
}
-// passAllFilterFuncs returns true if all filter funcs evaluate to true for the given request
-func passAllFilterFuncs(ctx context.Context, filterFuncs []FilterFunc, req *request.Request) bool {
- for _, ff := range filterFuncs {
- if !ff(ctx, req) {
- return false
- }
- }
- return true
-}
+const (
+ tcp = 0
+ udp = 1
-// OnStartupComplete lists the sites served by this server
-// and any relevant information, assuming Quiet is false.
-func (s *Server) OnStartupComplete() {
- if Quiet {
- return
- }
+ tcpMaxQueries = -1
+)
- out := startUpZones("", s.Addr, s.zones)
- if out != "" {
- fmt.Print(out)
- }
-}
+type (
+ // Key is the context key for the current server added to the context.
+ Key struct{}
-// Tracer returns the tracer in the server if defined.
-func (s *Server) Tracer() ot.Tracer {
- if s.trace == nil {
- return nil
- }
+ // LoopKey is the context key to detect server wide loops.
+ LoopKey struct{}
- return s.trace.Tracer()
+ // ViewKey is the context key for the current view, if defined
+ ViewKey struct{}
+)
+
+// EnableChaos is a map with plugin names for which we should open CH class queries as we block these by default.
+var EnableChaos = map[string]struct{}{
+ "chaos": {},
+ "forward": {},
+ "proxy": {},
}
+
+// Quiet mode will not show any informative output on initialization.
+var Quiet bool