mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-15 01:09:20 +02:00
Support using DOH server for type 65 query
This commit is contained in:
parent
d7c2c33547
commit
d2e79c281d
|
@ -390,6 +390,7 @@ type TLSConfig struct {
|
|||
PinnedPeerCertificatePublicKeySha256 *[]string `json:"pinnedPeerCertificatePublicKeySha256"`
|
||||
MasterKeyLog string `json:"masterKeyLog"`
|
||||
ECHConfig string `json:"echConfig"`
|
||||
ECHDOHServer string `json:"echDohServer"`
|
||||
}
|
||||
|
||||
// Build implements Buildable.
|
||||
|
@ -446,6 +447,7 @@ func (c *TLSConfig) Build() (proto.Message, error) {
|
|||
|
||||
config.MasterKeyLog = c.MasterKeyLog
|
||||
config.EchConfig = c.ECHConfig
|
||||
config.Ech_DOHserver = c.ECHDOHServer
|
||||
|
||||
return config, nil
|
||||
}
|
||||
|
|
|
@ -390,7 +390,7 @@ func (c *Config) GetTLSConfig(opts ...Option) *tls.Config {
|
|||
config.KeyLogWriter = writer
|
||||
}
|
||||
}
|
||||
if len(c.EchConfig) > 0 {
|
||||
if len(c.EchConfig) > 0 || len(c.Ech_DOHserver) > 0 {
|
||||
err := ApplyECH(c, config)
|
||||
if err != nil {
|
||||
errors.LogError(context.Background(), err)
|
||||
|
|
|
@ -221,6 +221,7 @@ type Config struct {
|
|||
PinnedPeerCertificatePublicKeySha256 [][]byte `protobuf:"bytes,14,rep,name=pinned_peer_certificate_public_key_sha256,json=pinnedPeerCertificatePublicKeySha256,proto3" json:"pinned_peer_certificate_public_key_sha256,omitempty"`
|
||||
MasterKeyLog string `protobuf:"bytes,15,opt,name=master_key_log,json=masterKeyLog,proto3" json:"master_key_log,omitempty"`
|
||||
EchConfig string `protobuf:"bytes,16,opt,name=ech_config,json=echConfig,proto3" json:"ech_config,omitempty"`
|
||||
Ech_DOHserver string `protobuf:"bytes,17,opt,name=ech_DOHserver,json=echDOHserver,proto3" json:"ech_DOHserver,omitempty"`
|
||||
}
|
||||
|
||||
func (x *Config) Reset() {
|
||||
|
@ -368,6 +369,13 @@ func (x *Config) GetEchConfig() string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (x *Config) GetEch_DOHserver() string {
|
||||
if x != nil {
|
||||
return x.Ech_DOHserver
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
var File_transport_internet_tls_config_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_transport_internet_tls_config_proto_rawDesc = []byte{
|
||||
|
@ -399,7 +407,7 @@ var file_transport_internet_tls_config_proto_rawDesc = []byte{
|
|||
0x4e, 0x43, 0x49, 0x50, 0x48, 0x45, 0x52, 0x4d, 0x45, 0x4e, 0x54, 0x10, 0x00, 0x12, 0x14, 0x0a,
|
||||
0x10, 0x41, 0x55, 0x54, 0x48, 0x4f, 0x52, 0x49, 0x54, 0x59, 0x5f, 0x56, 0x45, 0x52, 0x49, 0x46,
|
||||
0x59, 0x10, 0x01, 0x12, 0x13, 0x0a, 0x0f, 0x41, 0x55, 0x54, 0x48, 0x4f, 0x52, 0x49, 0x54, 0x59,
|
||||
0x5f, 0x49, 0x53, 0x53, 0x55, 0x45, 0x10, 0x02, 0x22, 0x95, 0x06, 0x0a, 0x06, 0x43, 0x6f, 0x6e,
|
||||
0x5f, 0x49, 0x53, 0x53, 0x55, 0x45, 0x10, 0x02, 0x22, 0xba, 0x06, 0x0a, 0x06, 0x43, 0x6f, 0x6e,
|
||||
0x66, 0x69, 0x67, 0x12, 0x25, 0x0a, 0x0e, 0x61, 0x6c, 0x6c, 0x6f, 0x77, 0x5f, 0x69, 0x6e, 0x73,
|
||||
0x65, 0x63, 0x75, 0x72, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x0d, 0x61, 0x6c, 0x6c,
|
||||
0x6f, 0x77, 0x49, 0x6e, 0x73, 0x65, 0x63, 0x75, 0x72, 0x65, 0x12, 0x4a, 0x0a, 0x0b, 0x63, 0x65,
|
||||
|
@ -449,14 +457,17 @@ var file_transport_internet_tls_config_proto_rawDesc = []byte{
|
|||
0x01, 0x28, 0x09, 0x52, 0x0c, 0x6d, 0x61, 0x73, 0x74, 0x65, 0x72, 0x4b, 0x65, 0x79, 0x4c, 0x6f,
|
||||
0x67, 0x12, 0x1d, 0x0a, 0x0a, 0x65, 0x63, 0x68, 0x5f, 0x63, 0x6f, 0x6e, 0x66, 0x69, 0x67, 0x18,
|
||||
0x10, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x65, 0x63, 0x68, 0x43, 0x6f, 0x6e, 0x66, 0x69, 0x67,
|
||||
0x42, 0x73, 0x0a, 0x1f, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x74, 0x72, 0x61,
|
||||
0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e,
|
||||
0x74, 0x6c, 0x73, 0x50, 0x01, 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f,
|
||||
0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65,
|
||||
0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f, 0x69, 0x6e, 0x74, 0x65, 0x72,
|
||||
0x6e, 0x65, 0x74, 0x2f, 0x74, 0x6c, 0x73, 0xaa, 0x02, 0x1b, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x54,
|
||||
0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
|
||||
0x74, 0x2e, 0x54, 0x6c, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
0x12, 0x23, 0x0a, 0x0d, 0x65, 0x63, 0x68, 0x5f, 0x44, 0x4f, 0x48, 0x73, 0x65, 0x72, 0x76, 0x65,
|
||||
0x72, 0x18, 0x11, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x65, 0x63, 0x68, 0x44, 0x4f, 0x48, 0x73,
|
||||
0x65, 0x72, 0x76, 0x65, 0x72, 0x42, 0x73, 0x0a, 0x1f, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61,
|
||||
0x79, 0x2e, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x69, 0x6e, 0x74, 0x65,
|
||||
0x72, 0x6e, 0x65, 0x74, 0x2e, 0x74, 0x6c, 0x73, 0x50, 0x01, 0x5a, 0x30, 0x67, 0x69, 0x74, 0x68,
|
||||
0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79,
|
||||
0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x74, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2f,
|
||||
0x69, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2f, 0x74, 0x6c, 0x73, 0xaa, 0x02, 0x1b, 0x58,
|
||||
0x72, 0x61, 0x79, 0x2e, 0x54, 0x72, 0x61, 0x6e, 0x73, 0x70, 0x6f, 0x72, 0x74, 0x2e, 0x49, 0x6e,
|
||||
0x74, 0x65, 0x72, 0x6e, 0x65, 0x74, 0x2e, 0x54, 0x6c, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74,
|
||||
0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
|
@ -89,4 +89,5 @@ message Config {
|
|||
|
||||
string master_key_log = 15;
|
||||
string ech_config = 16;
|
||||
string ech_DOHserver = 17;
|
||||
}
|
||||
|
|
|
@ -4,18 +4,107 @@
|
|||
package tls
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"encoding/base64"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/miekg/dns"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
)
|
||||
|
||||
func ApplyECH(c *Config, config *tls.Config) error {
|
||||
ECHConfig, err := base64.StdEncoding.DecodeString(c.EchConfig)
|
||||
var ECHConfig []byte
|
||||
var err error
|
||||
|
||||
if len(c.EchConfig) > 0 {
|
||||
ECHConfig, err = base64.StdEncoding.DecodeString(c.EchConfig)
|
||||
if err != nil {
|
||||
errors.LogError(context.Background(), "invalid ECH config")
|
||||
return errors.New("invalid ECH config")
|
||||
}
|
||||
} else {
|
||||
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)
|
||||
}
|
||||
|
||||
config.EncryptedClientHelloConfigList = ECHConfig
|
||||
return nil
|
||||
}
|
||||
|
||||
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) {
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
rec, found := dnsCache[domain]
|
||||
if found && rec.expire.After(time.Now()) {
|
||||
return "", nil
|
||||
}
|
||||
record, err := dohQuery(server, domain)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
rec.record = record
|
||||
rec.expire = time.Now().Add(time.Second * 600)
|
||||
return record, nil
|
||||
}
|
||||
|
||||
func dohQuery(server string, domain string) (string, error) {
|
||||
m := new(dns.Msg)
|
||||
m.SetQuestion(dns.Fqdn(domain), dns.TypeHTTPS)
|
||||
msg, err := m.Pack()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
client := &http.Client{
|
||||
Timeout: 5 * time.Second,
|
||||
}
|
||||
req, err := http.NewRequest("POST", server, bytes.NewReader(msg))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/dns-message")
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", errors.New("query failed with response code:", resp.StatusCode)
|
||||
}
|
||||
respMsg := new(dns.Msg)
|
||||
err = respMsg.Unpack(respBody)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(respMsg.Answer) > 0 {
|
||||
re := regexp.MustCompile(`ech="([^"]+)"`)
|
||||
match := re.FindStringSubmatch(respMsg.Answer[0].String())
|
||||
if match[1] != "" {
|
||||
return match[1], nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("no ech record found")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue