Skip to content

Commit f0c1591

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

File tree

5 files changed

+133
-28
lines changed

5 files changed

+133
-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

+95-4
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,109 @@
44
package tls
55

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

17+
"github.com/miekg/dns"
1118
"github.com/xtls/xray-core/common/errors"
1219
)
1320

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

0 commit comments

Comments
 (0)