-
Notifications
You must be signed in to change notification settings - Fork 28
/
transport_tls.go
137 lines (120 loc) · 2.97 KB
/
transport_tls.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
package dns
import (
"context"
"crypto/tls"
"net/netip"
"net/url"
"os"
"sync"
E "github.com/sagernet/sing/common/exceptions"
"github.com/sagernet/sing/common/logger"
M "github.com/sagernet/sing/common/metadata"
N "github.com/sagernet/sing/common/network"
"github.com/sagernet/sing/common/x/list"
"github.com/miekg/dns"
)
var _ Transport = (*TLSTransport)(nil)
func init() {
RegisterTransport([]string{"tls"}, func(options TransportOptions) (Transport, error) {
return NewTLSTransport(options)
})
}
type TLSTransport struct {
name string
dialer N.Dialer
logger logger.ContextLogger
serverAddr M.Socksaddr
access sync.Mutex
connections list.List[*tlsDNSConn]
}
type tlsDNSConn struct {
*tls.Conn
queryId uint16
}
func NewTLSTransport(options TransportOptions) (*TLSTransport, error) {
serverURL, err := url.Parse(options.Address)
if err != nil {
return nil, err
}
serverAddr := M.ParseSocksaddr(serverURL.Host)
if !serverAddr.IsValid() {
return nil, E.New("invalid server address")
}
if serverAddr.Port == 0 {
serverAddr.Port = 853
}
return newTLSTransport(options, serverAddr), nil
}
func newTLSTransport(options TransportOptions, serverAddr M.Socksaddr) *TLSTransport {
return &TLSTransport{
name: options.Name,
dialer: options.Dialer,
logger: options.Logger,
serverAddr: serverAddr,
}
}
func (t *TLSTransport) Name() string {
return t.name
}
func (t *TLSTransport) Start() error {
return nil
}
func (t *TLSTransport) Reset() {
t.access.Lock()
defer t.access.Unlock()
for connection := t.connections.Front(); connection != nil; connection = connection.Next() {
connection.Value.Close()
}
t.connections.Init()
}
func (t *TLSTransport) Close() error {
t.Reset()
return nil
}
func (t *TLSTransport) Raw() bool {
return true
}
func (t *TLSTransport) Exchange(ctx context.Context, message *dns.Msg) (*dns.Msg, error) {
t.access.Lock()
conn := t.connections.PopFront()
t.access.Unlock()
if conn != nil {
response, err := t.exchange(message, conn)
if err == nil {
return response, nil
}
}
tcpConn, err := t.dialer.DialContext(ctx, N.NetworkTCP, t.serverAddr)
if err != nil {
return nil, err
}
tlsConn := tls.Client(tcpConn, &tls.Config{
ServerName: t.serverAddr.AddrString(),
})
err = tlsConn.HandshakeContext(ctx)
if err != nil {
tcpConn.Close()
return nil, err
}
return t.exchange(message, &tlsDNSConn{Conn: tlsConn})
}
func (t *TLSTransport) exchange(message *dns.Msg, conn *tlsDNSConn) (*dns.Msg, error) {
conn.queryId++
err := writeMessage(conn, conn.queryId, message)
if err != nil {
conn.Close()
return nil, E.Cause(err, "write request")
}
response, err := readMessage(conn)
if err != nil {
conn.Close()
return nil, E.Cause(err, "read response")
}
t.access.Lock()
t.connections.PushBack(conn)
t.access.Unlock()
return response, nil
}
func (t *TLSTransport) Lookup(ctx context.Context, domain string, strategy DomainStrategy) ([]netip.Addr, error) {
return nil, os.ErrInvalid
}