Skip to content

Commit d2e79c2

Browse files
authored
Support using DOH server for type 65 query
1 parent d7c2c33 commit d2e79c2

File tree

5 files changed

+131
-28
lines changed

5 files changed

+131
-28
lines changed

infra/conf/transport_internet.go

+15-13
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ type TLSConfig struct {
390390
PinnedPeerCertificatePublicKeySha256 *[]string `json:"pinnedPeerCertificatePublicKeySha256"`
391391
MasterKeyLog string `json:"masterKeyLog"`
392392
ECHConfig string `json:"echConfig"`
393+
ECHDOHServer string `json:"echDohServer"`
393394
}
394395

395396
// Build implements Buildable.
@@ -446,6 +447,7 @@ func (c *TLSConfig) Build() (proto.Message, error) {
446447

447448
config.MasterKeyLog = c.MasterKeyLog
448449
config.EchConfig = c.ECHConfig
450+
config.Ech_DOHserver = c.ECHDOHServer
449451

450452
return config, nil
451453
}
@@ -759,19 +761,19 @@ func (c *SocketConfig) Build() (*internet.SocketConfig, error) {
759761
}
760762

761763
type StreamConfig struct {
762-
Network *TransportProtocol `json:"network"`
763-
Security string `json:"security"`
764-
TLSSettings *TLSConfig `json:"tlsSettings"`
765-
REALITYSettings *REALITYConfig `json:"realitySettings"`
766-
TCPSettings *TCPConfig `json:"tcpSettings"`
767-
KCPSettings *KCPConfig `json:"kcpSettings"`
768-
WSSettings *WebSocketConfig `json:"wsSettings"`
769-
HTTPSettings *HTTPConfig `json:"httpSettings"`
770-
SocketSettings *SocketConfig `json:"sockopt"`
771-
GRPCConfig *GRPCConfig `json:"grpcSettings"`
772-
GUNConfig *GRPCConfig `json:"gunSettings"`
773-
HTTPUPGRADESettings *HttpUpgradeConfig `json:"httpupgradeSettings"`
774-
SplitHTTPSettings *SplitHTTPConfig `json:"splithttpSettings"`
764+
Network *TransportProtocol `json:"network"`
765+
Security string `json:"security"`
766+
TLSSettings *TLSConfig `json:"tlsSettings"`
767+
REALITYSettings *REALITYConfig `json:"realitySettings"`
768+
TCPSettings *TCPConfig `json:"tcpSettings"`
769+
KCPSettings *KCPConfig `json:"kcpSettings"`
770+
WSSettings *WebSocketConfig `json:"wsSettings"`
771+
HTTPSettings *HTTPConfig `json:"httpSettings"`
772+
SocketSettings *SocketConfig `json:"sockopt"`
773+
GRPCConfig *GRPCConfig `json:"grpcSettings"`
774+
GUNConfig *GRPCConfig `json:"gunSettings"`
775+
HTTPUPGRADESettings *HttpUpgradeConfig `json:"httpupgradeSettings"`
776+
SplitHTTPSettings *SplitHTTPConfig `json:"splithttpSettings"`
775777
}
776778

777779
// Build implements Buildable.

transport/internet/tls/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ func (c *Config) GetTLSConfig(opts ...Option) *tls.Config {
390390
config.KeyLogWriter = writer
391391
}
392392
}
393-
if len(c.EchConfig) > 0 {
393+
if len(c.EchConfig) > 0 || len(c.Ech_DOHserver) > 0 {
394394
err := ApplyECH(c, config)
395395
if err != nil {
396396
errors.LogError(context.Background(), err)

transport/internet/tls/config.pb.go

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

transport/internet/tls/config.proto

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,5 +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+
string ech_config = 16;
92+
string ech_DOHserver = 17;
9293
}

transport/internet/tls/ech.go

+93-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,107 @@
44
package tls
55

66
import (
7-
"context"
7+
"bytes"
88
"crypto/tls"
99
"encoding/base64"
10+
"io"
11+
"net/http"
12+
"regexp"
13+
"sync"
14+
"time"
1015

16+
"github.com/miekg/dns"
1117
"github.com/xtls/xray-core/common/errors"
1218
)
1319

1420
func ApplyECH(c *Config, config *tls.Config) error {
15-
ECHConfig, err := base64.StdEncoding.DecodeString(c.EchConfig)
16-
if err != nil {
17-
errors.LogError(context.Background(), "invalid ECH config")
21+
var ECHConfig []byte
22+
var err error
23+
24+
if len(c.EchConfig) > 0 {
25+
ECHConfig, err = base64.StdEncoding.DecodeString(c.EchConfig)
26+
if err != nil {
27+
return errors.New("invalid ECH config")
28+
}
29+
} else {
30+
if c.ServerName == "" {
31+
return errors.New("Using DOH for ECH needs serverName")
32+
}
33+
ECHRecord, err := QueryRecord(c.ServerName, c.Ech_DOHserver)
34+
if err != nil {
35+
return err
36+
}
37+
ECHConfig, _ = base64.StdEncoding.DecodeString(ECHRecord)
1838
}
39+
1940
config.EncryptedClientHelloConfigList = ECHConfig
2041
return nil
2142
}
43+
44+
type record struct {
45+
record string
46+
expire time.Time
47+
}
48+
49+
var (
50+
dnsCache = make(map[string]record)
51+
mutex sync.RWMutex
52+
)
53+
54+
func QueryRecord(domain string, server string) (string, error) {
55+
mutex.RLock()
56+
defer mutex.RUnlock()
57+
rec, found := dnsCache[domain]
58+
if found && rec.expire.After(time.Now()) {
59+
return "", nil
60+
}
61+
record, err := dohQuery(server, domain)
62+
if err != nil {
63+
return "", err
64+
}
65+
rec.record = record
66+
rec.expire = time.Now().Add(time.Second * 600)
67+
return record, nil
68+
}
69+
70+
func dohQuery(server string, domain string) (string, error) {
71+
m := new(dns.Msg)
72+
m.SetQuestion(dns.Fqdn(domain), dns.TypeHTTPS)
73+
msg, err := m.Pack()
74+
if err != nil {
75+
return "", err
76+
}
77+
client := &http.Client{
78+
Timeout: 5 * time.Second,
79+
}
80+
req, err := http.NewRequest("POST", server, bytes.NewReader(msg))
81+
if err != nil {
82+
return "", err
83+
}
84+
req.Header.Set("Content-Type", "application/dns-message")
85+
resp, err := client.Do(req)
86+
if err != nil {
87+
return "", err
88+
}
89+
defer resp.Body.Close()
90+
respBody, err := io.ReadAll(resp.Body)
91+
if err != nil {
92+
return "", err
93+
}
94+
if resp.StatusCode != http.StatusOK {
95+
return "", errors.New("query failed with response code:", resp.StatusCode)
96+
}
97+
respMsg := new(dns.Msg)
98+
err = respMsg.Unpack(respBody)
99+
if err != nil {
100+
return "", err
101+
}
102+
if len(respMsg.Answer) > 0 {
103+
re := regexp.MustCompile(`ech="([^"]+)"`)
104+
match := re.FindStringSubmatch(respMsg.Answer[0].String())
105+
if match[1] != "" {
106+
return match[1], nil
107+
}
108+
}
109+
return "", errors.New("no ech record found")
110+
}

0 commit comments

Comments
 (0)