-
Notifications
You must be signed in to change notification settings - Fork 12
/
type.go
50 lines (41 loc) · 919 Bytes
/
type.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
// Taken from github.com/coredns/plugin/forward/type.go with modification
package dnsredir
import (
"crypto/tls"
"fmt"
"net"
)
type transportType int
const (
typeUdp transportType = iota
typeTcp
typeTls
typeTotalCount // Dummy type
)
func stringToTransportType(s string) transportType {
switch s {
case "udp":
return typeUdp
case "tcp":
return typeTcp
case "tcp-tls":
return typeTls
}
log.Warningf("Unknown protocol %q, fallback to UDP", s)
return typeUdp
}
func (t *Transport) transportTypeFromConn(pc *persistConn) transportType {
if _, ok := pc.c.Conn.(*net.UDPConn); ok {
return typeUdp
}
if t.tlsConfig == nil {
if _, ok := pc.c.Conn.(*net.TCPConn); !ok {
panic(fmt.Sprintf("Expected TCP connection, got %T", pc.c.Conn))
}
return typeTcp
}
if _, ok := pc.c.Conn.(*tls.Conn); !ok {
panic(fmt.Sprintf("Expected TLS connection, got %T", pc.c.Conn))
}
return typeTls
}