Add new doh server format

This commit is contained in:
风扇滑翔翼 2025-02-08 18:29:09 +00:00 committed by GitHub
parent b7529723c6
commit 8322c1919c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,6 +6,7 @@ import (
"crypto/tls" "crypto/tls"
"io" "io"
"net/http" "net/http"
"strings"
"sync" "sync"
"time" "time"
@ -19,13 +20,28 @@ func ApplyECH(c *Config, config *tls.Config) error {
var ECHConfig []byte var ECHConfig []byte
var err error var err error
nameToQuery := c.ServerName
var DOHServer string
parts := strings.Split(c.Ech_DOHserver, "+")
if len(parts) == 2 {
// parse ECH DOH server in format of "example.com+https://1.1.1.1/dns-query"
nameToQuery = parts[0]
DOHServer = parts[1]
} else if len(parts) == 1 {
// normal format
DOHServer = parts[0]
} else {
return errors.New("Invalid ECH DOH server format: ", c.Ech_DOHserver)
}
if len(c.EchConfig) > 0 { if len(c.EchConfig) > 0 {
ECHConfig = c.EchConfig ECHConfig = c.EchConfig
} else { // ECH config > DOH lookup } else { // ECH config > DOH lookup
if config.ServerName == "" { if nameToQuery == "" {
return errors.New("Using DOH for ECH needs serverName") return errors.New("Using DOH for ECH needs serverName or use dohServer format example.com+https://1.1.1.1/dns-query")
} }
ECHConfig, err = QueryRecord(c.ServerName, c.Ech_DOHserver) ECHConfig, err = QueryRecord(nameToQuery, DOHServer)
if err != nil { if err != nil {
return err return err
} }
@ -41,14 +57,13 @@ type record struct {
} }
var ( var (
dnsCache = make(map[string]record) dnsCache = make(map[string]record)
// global Lock? I'm not sure if I need finer grained locks. // global Lock? I'm not sure if I need finer grained locks.
// If we do this, we will need to nest another layer of struct // If we do this, we will need to nest another layer of struct
dnsCacheLock sync.RWMutex dnsCacheLock sync.RWMutex
updating sync.Mutex updating sync.Mutex
) )
// QueryRecord returns the ECH config for given domain. // QueryRecord returns the ECH config for given domain.
// If the record is not in cache or expired, it will query the DOH server and update the cache. // If the record is not in cache or expired, it will query the DOH server and update the cache.
func QueryRecord(domain string, server string) ([]byte, error) { func QueryRecord(domain string, server string) ([]byte, error) {
@ -95,7 +110,6 @@ func QueryRecord(domain string, server string) ([]byte, error) {
return echConfig, nil return echConfig, nil
} }
// dohQuery is the real func for sending type65 query for given domain to given DOH server. // dohQuery is the real func for sending type65 query for given domain to given DOH server.
// return ECH config, TTL and error // return ECH config, TTL and error
func dohQuery(server string, domain string) ([]byte, uint32, error) { func dohQuery(server string, domain string) ([]byte, uint32, error) {