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-15 05:17:39 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"encoding/base64"
|
2024-09-15 06:55:44 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
ECHConfig, err = base64.StdEncoding.DecodeString(c.EchConfig)
|
|
|
|
if err != nil {
|
|
|
|
return errors.New("invalid ECH config")
|
|
|
|
}
|
2024-09-15 13:37:57 +00:00
|
|
|
} else { // ECH config > DOH lookup
|
2024-09-15 06:55:44 +00:00
|
|
|
if c.ServerName == "" {
|
|
|
|
return errors.New("Using DOH for ECH needs serverName")
|
|
|
|
}
|
|
|
|
ECHRecord, err := QueryRecord(c.ServerName, c.Ech_DOHserver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ECHConfig, _ = base64.StdEncoding.DecodeString(ECHRecord)
|
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 {
|
|
|
|
record string
|
|
|
|
expire time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
dnsCache = make(map[string]record)
|
|
|
|
mutex sync.RWMutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func QueryRecord(domain string, server string) (string, error) {
|
|
|
|
rec, found := dnsCache[domain]
|
|
|
|
if found && rec.expire.After(time.Now()) {
|
2024-09-15 07:37:56 +00:00
|
|
|
return rec.record, nil
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
2024-09-15 07:37:56 +00:00
|
|
|
mutex.Lock()
|
|
|
|
defer mutex.Unlock()
|
2024-09-15 13:37:57 +00:00
|
|
|
record, ttl, err := dohQuery(server, domain)
|
2024-09-15 06:55:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2024-09-15 13:37:57 +00:00
|
|
|
// Use TTL for good, but many HTTPS records have TTL 60, too short
|
|
|
|
if ttl < 600 {
|
|
|
|
ttl = 600
|
|
|
|
}
|
2024-09-15 06:55:44 +00:00
|
|
|
rec.record = record
|
2024-09-15 13:37:57 +00:00
|
|
|
rec.expire = time.Now().Add(time.Second * time.Duration(ttl))
|
2024-09-15 07:37:56 +00:00
|
|
|
dnsCache[domain] = rec
|
2024-09-15 06:55:44 +00:00
|
|
|
return record, nil
|
|
|
|
}
|
|
|
|
|
2024-09-15 13:37:57 +00:00
|
|
|
func dohQuery(server string, domain string) (string, uint32, error) {
|
2024-09-15 06:55:44 +00:00
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(dns.Fqdn(domain), dns.TypeHTTPS)
|
|
|
|
msg, err := m.Pack()
|
|
|
|
if err != nil {
|
2024-09-15 13:37:57 +00:00
|
|
|
return "", 0, err
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: 5 * time.Second,
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", server, bytes.NewReader(msg))
|
|
|
|
if err != nil {
|
2024-09-15 13:37:57 +00:00
|
|
|
return "", 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-15 13:37:57 +00:00
|
|
|
return "", 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-15 13:37:57 +00:00
|
|
|
return "", 0, err
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2024-09-15 13:37:57 +00:00
|
|
|
return "", 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-15 13:37:57 +00:00
|
|
|
return "", 0, err
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
if len(respMsg.Answer) > 0 {
|
|
|
|
re := regexp.MustCompile(`ech="([^"]+)"`)
|
|
|
|
match := re.FindStringSubmatch(respMsg.Answer[0].String())
|
|
|
|
if match[1] != "" {
|
2024-09-15 13:37:57 +00:00
|
|
|
return match[1], respMsg.Answer[0].Header().Ttl, nil
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-15 13:37:57 +00:00
|
|
|
return "", 0, errors.New("no ech record found")
|
2024-09-15 06:55:44 +00:00
|
|
|
}
|