2024-09-15 05:17:39 +00:00
|
|
|
//go:build go1.23
|
|
|
|
// +build go1.23
|
|
|
|
|
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2024-09-15 06:55:44 +00:00
|
|
|
"bytes"
|
2024-09-17 17:14:13 +00:00
|
|
|
"context"
|
2024-09-15 05:17:39 +00:00
|
|
|
"crypto/tls"
|
2024-09-15 06:55:44 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2024-09-15 05:17:39 +00:00
|
|
|
|
2024-09-15 06:55:44 +00:00
|
|
|
"github.com/miekg/dns"
|
2024-09-15 05:17:39 +00:00
|
|
|
"github.com/xtls/xray-core/common/errors"
|
2024-09-17 17:14:13 +00:00
|
|
|
"github.com/xtls/xray-core/common/net"
|
|
|
|
"github.com/xtls/xray-core/transport/internet"
|
2024-09-15 05:17:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ApplyECH(c *Config, config *tls.Config) error {
|
2024-09-15 06:55:44 +00:00
|
|
|
var ECHConfig []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if len(c.EchConfig) > 0 {
|
2024-09-26 14:51:48 +00:00
|
|
|
ECHConfig = c.EchConfig
|
2024-09-15 13:37:57 +00:00
|
|
|
} else { // ECH config > DOH lookup
|
2024-09-26 14:51:48 +00:00
|
|
|
if config.ServerName == "" {
|
2024-09-15 06:55:44 +00:00
|
|
|
return errors.New("Using DOH for ECH needs serverName")
|
|
|
|
}
|
2024-09-26 14:51:48 +00:00
|
|
|
ECHConfig, err = QueryRecord(c.ServerName, c.Ech_DOHserver)
|
2024-09-15 06:55:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-15 05:17:39 +00:00
|
|
|
}
|
2024-09-15 06:55:44 +00:00
|
|
|
|
2024-09-15 05:17:39 +00:00
|
|
|
config.EncryptedClientHelloConfigList = ECHConfig
|
|
|
|
return nil
|
|
|
|
}
|
2024-09-15 06:55:44 +00:00
|
|
|
|
|
|
|
type record struct {
|
2024-09-26 14:51:48 +00:00
|
|
|
record []byte
|
2024-09-15 06:55:44 +00:00
|
|
|
expire time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
dnsCache = make(map[string]record)
|
|
|
|
mutex sync.RWMutex
|
|
|
|
)
|
|
|
|
|
2024-09-26 14:51:48 +00:00
|
|
|
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()
|
|
|
|
|
|
|
|
errors.LogDebug(context.Background(), "Trying to query ECH config for domain: ", domain, " with ECH server: ", server)
|
|
|
|
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
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 14:51:48 +00:00
|
|
|
func dohQuery(server string, domain string) ([]byte, uint32, error) {
|
2024-09-15 06:55:44 +00:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(dns.Fqdn(domain), dns.TypeHTTPS)
|
2024-09-26 14:51:48 +00:00
|
|
|
m.Id = 0
|
2024-09-15 06:55:44 +00:00
|
|
|
msg, err := m.Pack()
|
|
|
|
if err != nil {
|
2024-09-26 14:51:48 +00:00
|
|
|
return []byte{}, 0, err
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
2024-09-17 17:14:13 +00:00
|
|
|
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
|
|
|
|
},
|
|
|
|
}
|
2024-09-15 06:55:44 +00:00
|
|
|
client := &http.Client{
|
2024-09-17 17:14:13 +00:00
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
Transport: tr,
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", server, bytes.NewReader(msg))
|
|
|
|
if err != nil {
|
2024-09-26 14:51:48 +00:00
|
|
|
return []byte{}, 0, err
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/dns-message")
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2024-09-26 14:51:48 +00:00
|
|
|
return []byte{}, 0, err
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
respBody, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2024-09-26 14:51:48 +00:00
|
|
|
return []byte{}, 0, err
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2024-09-26 14:51:48 +00:00
|
|
|
return []byte{}, 0, errors.New("query failed with response code:", resp.StatusCode)
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
respMsg := new(dns.Msg)
|
|
|
|
err = respMsg.Unpack(respBody)
|
|
|
|
if err != nil {
|
2024-09-26 14:51:48 +00:00
|
|
|
return []byte{}, 0, err
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
if len(respMsg.Answer) > 0 {
|
2024-09-26 14:51:48 +00:00
|
|
|
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 {
|
|
|
|
errors.LogDebug(context.Background(), "Get ECH config:", echConfig.String(), " TTL:", respMsg.Answer[0].Header().Ttl)
|
|
|
|
return echConfig.ECH, answer.Header().Ttl, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-26 14:51:48 +00:00
|
|
|
return []byte{}, 0, errors.New("no ech record found")
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|