forked from v2fly/v2ray-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathech.go
More file actions
139 lines (126 loc) · 3.16 KB
/
ech.go
File metadata and controls
139 lines (126 loc) · 3.16 KB
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
//go:build go1.23
// +build go1.23
package tls
import (
"bytes"
"context"
"crypto/tls"
"io"
"net/http"
"sync"
"time"
"github.com/miekg/dns"
"github.com/v2fly/v2ray-core/v5/common/net"
"github.com/v2fly/v2ray-core/v5/transport/internet"
)
func ApplyECH(c *Config, config *tls.Config) error {
var ECHConfig []byte
var err error
if len(c.EchConfig) > 0 {
ECHConfig = c.EchConfig
} else { // ECH config > DOH lookup
if config.ServerName == "" {
return newError("Using DOH for ECH needs serverName")
}
ECHConfig, err = QueryRecord(c.ServerName, c.Ech_DOHserver)
if err != nil {
return err
}
}
config.EncryptedClientHelloConfigList = ECHConfig
return nil
}
type record struct {
record []byte
expire time.Time
}
var (
dnsCache = make(map[string]record)
mutex sync.RWMutex
)
func QueryRecord(domain string, server string) ([]byte, error) {
mutex.Lock()
rec, found := dnsCache[domain]
if found && rec.expire.After(time.Now()) {
mutex.Unlock()
return rec.record, nil
}
mutex.Unlock()
newError("Trying to query ECH config for domain: ", domain, " with ECH server: ", server).AtDebug().WriteToLog()
record, ttl, err := dohQuery(server, domain)
if err != nil {
return []byte{}, err
}
if ttl < 600 {
ttl = 600
}
mutex.Lock()
defer mutex.Unlock()
rec.record = record
rec.expire = time.Now().Add(time.Second * time.Duration(ttl))
dnsCache[domain] = rec
return record, nil
}
func dohQuery(server string, domain string) ([]byte, uint32, error) {
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(domain), dns.TypeHTTPS)
m.Id = 0
msg, err := m.Pack()
if err != nil {
return []byte{}, 0, err
}
tr := &http.Transport{
IdleConnTimeout: 90 * time.Second,
ForceAttemptHTTP2: true,
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
dest, err := net.ParseDestination(network + ":" + addr)
if err != nil {
return nil, err
}
conn, err := internet.DialSystem(ctx, dest, nil)
if err != nil {
return nil, err
}
return conn, nil
},
}
client := &http.Client{
Timeout: 5 * time.Second,
Transport: tr,
}
req, err := http.NewRequest("POST", server, bytes.NewReader(msg))
if err != nil {
return []byte{}, 0, err
}
req.Header.Set("Content-Type", "application/dns-message")
resp, err := client.Do(req)
if err != nil {
return []byte{}, 0, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return []byte{}, 0, err
}
if resp.StatusCode != http.StatusOK {
return []byte{}, 0, newError("query failed with response code:", resp.StatusCode)
}
respMsg := new(dns.Msg)
err = respMsg.Unpack(respBody)
if err != nil {
return []byte{}, 0, err
}
if len(respMsg.Answer) > 0 {
for _, answer := range respMsg.Answer {
if https, ok := answer.(*dns.HTTPS); ok && https.Hdr.Name == dns.Fqdn(domain) {
for _, v := range https.Value {
if echConfig, ok := v.(*dns.SVCBECHConfig); ok {
newError(context.Background(), "Get ECH config:", echConfig.String(), " TTL:", respMsg.Answer[0].Header().Ttl).AtDebug().WriteToLog()
return echConfig.ECH, answer.Header().Ttl, nil
}
}
}
}
}
return []byte{}, 0, newError("no ech record found")
}