-
Notifications
You must be signed in to change notification settings - Fork 301
/
server.go
193 lines (164 loc) · 4.14 KB
/
server.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (C) 2020-2021, IrineSistiana
//
// This file is part of mosdns.
//
// mosdns is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// mosdns is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package server
import (
"context"
"crypto/tls"
"errors"
"fmt"
"github.com/IrineSistiana/mosdns/dispatcher/handler"
"go.uber.org/zap"
"net"
"sync"
"time"
)
// Server errors
var (
ErrServerClosed = errors.New("server closed")
ErrServerStarted = errors.New("server has been started")
errServerExited = errors.New("server exited without err")
)
type Protocol uint8
// DNS protocols
const (
ProtocolUDP Protocol = iota
ProtocolTCP
ProtocolDoT
ProtocolDoH
ProtocolHttp
)
const (
defaultQueryTimeout = time.Second * 5
defaultIdleTimeout = time.Second * 10
)
type Server struct {
Handler DNSServerHandler
Protocol Protocol
Listener net.Listener
PacketConn net.PacketConn
TLSConfig *tls.Config // Used by dot, doh in tls.NewListener.
URLPath string // Used by doh, http. If it's emtpy, any path will be handled.
GetUserIPFromHeader string // Used by doh, http. Get user ip address from http header.
QueryTimeout time.Duration // Used by all protocol as query timeout, default is defaultQueryTimeout.
IdleTimeout time.Duration // Used by tcp, dot, doh as connection idle timeout, default is defaultIdleTimeout.
Logger *zap.Logger // Nil logger disables logging.
mu sync.Mutex
started bool
closed bool
logger *zap.Logger // non-nil logger.
}
func (s *Server) queryTimeout() time.Duration {
if t := s.QueryTimeout; t > 0 {
return t
}
return defaultQueryTimeout
}
func (s *Server) idleTimeout() time.Duration {
if t := s.IdleTimeout; t > 0 {
return t
}
return defaultIdleTimeout
}
// Start starts the udp server.
// Start always returns an non-nil err.
// If server was closed, an ErrServerClosed will be returned.
func (s *Server) Start() error {
s.mu.Lock()
if s.started {
s.mu.Unlock()
return ErrServerStarted
}
s.started = true
s.mu.Unlock()
defer s.Close()
if s.Handler == nil {
return errors.New("nil handler")
}
if logger := s.Logger; logger != nil {
s.logger = logger
} else {
s.logger = zap.NewNop()
}
err := s.startServer()
if err != nil {
if s.isClosed() {
return ErrServerClosed
}
return err
}
return errServerExited
}
func (s *Server) startServer() error {
switch s.Protocol {
case ProtocolUDP:
return s.startUDP()
case ProtocolTCP, ProtocolDoT:
return s.startTCP()
case ProtocolDoH, ProtocolHttp:
return s.startDoH()
default:
return fmt.Errorf("unknown protocol %d", s.Protocol)
}
}
func (s *Server) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.Listener != nil {
s.Listener.Close()
}
if s.PacketConn != nil {
s.PacketConn.Close()
}
s.closed = true
return nil
}
func (s *Server) handleQueryTimeout(ctx context.Context, qCtx *handler.Context, w ResponseWriter) {
queryCtx, cancel := context.WithTimeout(ctx, s.queryTimeout())
defer cancel()
s.Handler.ServeDNS(queryCtx, qCtx, w)
}
func (s *Server) isClosed() bool {
s.mu.Lock()
defer s.mu.Unlock()
return s.closed
}
func checkTLSConfig(c *tls.Config) error {
if c == nil {
return errors.New("nil tls config")
}
if len(c.Certificates) == 0 && c.GetCertificate == nil {
return errors.New("no certificate")
}
return nil
}
func ParseProtocol(s string) (p Protocol, err error) {
switch s {
case "", "udp":
p = ProtocolUDP
case "tcp":
p = ProtocolTCP
case "dot", "tls":
p = ProtocolDoT
case "doh", "https":
p = ProtocolDoT
case "http":
p = ProtocolHttp
default:
err = fmt.Errorf("unsupported protocol: %s", s)
}
return p, err
}