Skip to content

Commit df370f0

Browse files
authored
Many fixes
1 parent 72892da commit df370f0

File tree

4 files changed

+60
-49
lines changed

4 files changed

+60
-49
lines changed

infra/conf/transport_internet.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,15 @@ func (c *TLSConfig) Build() (proto.Message, error) {
445445
}
446446
}
447447

448+
if c.ECHConfig != "" {
449+
ECHConfig, err := base64.StdEncoding.DecodeString(c.ECHConfig)
450+
if err != nil {
451+
return nil, errors.New("invalid ECH Config", c.ECHConfig)
452+
}
453+
config.EchConfig = ECHConfig
454+
}
455+
448456
config.MasterKeyLog = c.MasterKeyLog
449-
config.EchConfig = c.ECHConfig
450457
config.Ech_DOHserver = c.ECHDOHServer
451458

452459
return config, nil

transport/internet/tls/config.pb.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

transport/internet/tls/config.proto

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,6 @@ message Config {
8888
repeated bytes pinned_peer_certificate_public_key_sha256 = 14;
8989

9090
string master_key_log = 15;
91-
string ech_config = 16;
91+
bytes ech_config = 16;
9292
string ech_DOHserver = 17;
9393
}

transport/internet/tls/ech.go

+47-43
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ import (
77
"bytes"
88
"context"
99
"crypto/tls"
10-
"encoding/base64"
1110
"io"
1211
"net/http"
13-
"regexp"
1412
"sync"
1513
"time"
1614

@@ -25,27 +23,23 @@ func ApplyECH(c *Config, config *tls.Config) error {
2523
var err error
2624

2725
if len(c.EchConfig) > 0 {
28-
ECHConfig, err = base64.StdEncoding.DecodeString(c.EchConfig)
29-
if err != nil {
30-
return errors.New("invalid ECH config")
31-
}
26+
ECHConfig = c.EchConfig
3227
} else { // ECH config > DOH lookup
33-
if c.ServerName == "" {
28+
if config.ServerName == "" {
3429
return errors.New("Using DOH for ECH needs serverName")
3530
}
36-
ECHRecord, err := QueryRecord(c.ServerName, c.Ech_DOHserver)
31+
ECHConfig, err = QueryRecord(c.ServerName, c.Ech_DOHserver)
3732
if err != nil {
3833
return err
3934
}
40-
ECHConfig, _ = base64.StdEncoding.DecodeString(ECHRecord)
4135
}
4236

4337
config.EncryptedClientHelloConfigList = ECHConfig
4438
return nil
4539
}
4640

4741
type record struct {
48-
record string
42+
record []byte
4943
expire time.Time
5044
}
5145

@@ -54,34 +48,40 @@ var (
5448
mutex sync.RWMutex
5549
)
5650

57-
func QueryRecord(domain string, server string) (string, error) {
58-
rec, found := dnsCache[domain]
59-
if found && rec.expire.After(time.Now()) {
60-
return rec.record, nil
61-
}
62-
mutex.Lock()
63-
defer mutex.Unlock()
64-
errors.LogDebug(context.Background(), "Tring to query ECH config for domain: ", domain, " with ECH server: ", server)
65-
record, ttl, err := dohQuery(server, domain)
66-
if err != nil {
67-
return "", err
68-
}
69-
// Use TTL for good, but many HTTPS records have TTL 60, too short
70-
if ttl < 600 {
71-
ttl = 600
72-
}
73-
rec.record = record
74-
rec.expire = time.Now().Add(time.Second * time.Duration(ttl))
75-
dnsCache[domain] = rec
76-
return record, nil
51+
func QueryRecord(domain string, server string) ([]byte, error) {
52+
mutex.Lock()
53+
rec, found := dnsCache[domain]
54+
if found && rec.expire.After(time.Now()) {
55+
mutex.Unlock()
56+
return rec.record, nil
57+
}
58+
mutex.Unlock()
59+
60+
errors.LogDebug(context.Background(), "Trying to query ECH config for domain: ", domain, " with ECH server: ", server)
61+
record, ttl, err := dohQuery(server, domain)
62+
if err != nil {
63+
return []byte{}, err
64+
}
65+
66+
if ttl < 600 {
67+
ttl = 600
68+
}
69+
70+
mutex.Lock()
71+
defer mutex.Unlock()
72+
rec.record = record
73+
rec.expire = time.Now().Add(time.Second * time.Duration(ttl))
74+
dnsCache[domain] = rec
75+
return record, nil
7776
}
7877

79-
func dohQuery(server string, domain string) (string, uint32, error) {
78+
func dohQuery(server string, domain string) ([]byte, uint32, error) {
8079
m := new(dns.Msg)
8180
m.SetQuestion(dns.Fqdn(domain), dns.TypeHTTPS)
81+
m.Id = 0
8282
msg, err := m.Pack()
8383
if err != nil {
84-
return "", 0, err
84+
return []byte{}, 0, err
8585
}
8686
tr := &http.Transport{
8787
IdleConnTimeout: 90 * time.Second,
@@ -104,33 +104,37 @@ func dohQuery(server string, domain string) (string, uint32, error) {
104104
}
105105
req, err := http.NewRequest("POST", server, bytes.NewReader(msg))
106106
if err != nil {
107-
return "", 0, err
107+
return []byte{}, 0, err
108108
}
109109
req.Header.Set("Content-Type", "application/dns-message")
110110
resp, err := client.Do(req)
111111
if err != nil {
112-
return "", 0, err
112+
return []byte{}, 0, err
113113
}
114114
defer resp.Body.Close()
115115
respBody, err := io.ReadAll(resp.Body)
116116
if err != nil {
117-
return "", 0, err
117+
return []byte{}, 0, err
118118
}
119119
if resp.StatusCode != http.StatusOK {
120-
return "", 0, errors.New("query failed with response code:", resp.StatusCode)
120+
return []byte{}, 0, errors.New("query failed with response code:", resp.StatusCode)
121121
}
122122
respMsg := new(dns.Msg)
123123
err = respMsg.Unpack(respBody)
124124
if err != nil {
125-
return "", 0, err
125+
return []byte{}, 0, err
126126
}
127127
if len(respMsg.Answer) > 0 {
128-
re := regexp.MustCompile(`ech="([^"]+)"`)
129-
match := re.FindStringSubmatch(respMsg.Answer[0].String())
130-
if match[1] != "" {
131-
errors.LogDebug(context.Background(), "Get ECH config:", match[1], " TTL:", respMsg.Answer[0].Header().Ttl)
132-
return match[1], respMsg.Answer[0].Header().Ttl, nil
128+
for _, answer := range respMsg.Answer {
129+
if https, ok := answer.(*dns.HTTPS); ok && https.Hdr.Name == dns.Fqdn(domain) {
130+
for _, v := range https.Value {
131+
if echConfig, ok := v.(*dns.SVCBECHConfig); ok {
132+
errors.LogDebug(context.Background(), "Get ECH config:", echConfig.String(), " TTL:", respMsg.Answer[0].Header().Ttl)
133+
return echConfig.ECH, answer.Header().Ttl, nil
134+
}
135+
}
136+
}
133137
}
134138
}
135-
return "", 0, errors.New("no ech record found")
139+
return []byte{}, 0, errors.New("no ech record found")
136140
}

0 commit comments

Comments
 (0)