Skip to content

Commit

Permalink
Add new doh server format
Browse files Browse the repository at this point in the history
  • Loading branch information
Fangliding authored Feb 13, 2025
1 parent b752972 commit 8322c19
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions transport/internet/tls/ech.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/tls"
"io"
"net/http"
"strings"
"sync"
"time"

Expand All @@ -19,13 +20,28 @@ func ApplyECH(c *Config, config *tls.Config) error {
var ECHConfig []byte
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 {
ECHConfig = c.EchConfig
} else { // ECH config > DOH lookup
if config.ServerName == "" {
return errors.New("Using DOH for ECH needs serverName")
if nameToQuery == "" {
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 {
return err
}
Expand All @@ -41,14 +57,13 @@ type record struct {
}

var (
dnsCache = make(map[string]record)
dnsCache = make(map[string]record)
// 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
dnsCacheLock sync.RWMutex
updating sync.Mutex
)


// 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.
func QueryRecord(domain string, server string) ([]byte, error) {
Expand Down Expand Up @@ -95,7 +110,6 @@ func QueryRecord(domain string, server string) ([]byte, error) {
return echConfig, nil
}


// dohQuery is the real func for sending type65 query for given domain to given DOH server.
// return ECH config, TTL and error
func dohQuery(server string, domain string) ([]byte, uint32, error) {
Expand Down

0 comments on commit 8322c19

Please sign in to comment.