Default managed SOCKS hostname DNS to verified HTTPS with literal-IP bootstrap and no plaintext fallback. Supervise the loopback forwarder with private anchored readiness and preserve foreign markers. Include configuration, deployment, documentation and offline checks. Co-authored-by: GPT-6 <gpt-6@openai.com>
106 lines
3.0 KiB
Go
106 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const defaultDoHURL = "https://dns.google/dns-query"
|
|
const dnsListenAddress = "127.0.0.1:5353"
|
|
const dnsReadyPath = "/run/zapret-autotune-ss/dns-ready"
|
|
|
|
var errDNSConfig = errors.New("invalid managed DNS configuration")
|
|
|
|
type managedDNSConfig struct {
|
|
Mode string
|
|
URL string
|
|
Host string
|
|
BootstrapIPs []net.IP
|
|
Timeout time.Duration
|
|
NativeServer string
|
|
}
|
|
|
|
func parseManagedDNSConfig(lookup func(string) (string, bool)) (managedDNSConfig, error) {
|
|
cfg := managedDNSConfig{Mode: "doh", URL: defaultDoHURL, Timeout: 5 * time.Second}
|
|
if v, ok := lookup("ZAPRET_AUTOTUNE_DNS_MODE"); ok {
|
|
cfg.Mode = v
|
|
}
|
|
if cfg.Mode == "native" {
|
|
cfg.NativeServer, _ = lookup("SS_DNS_SERVER")
|
|
if cfg.NativeServer != "" && net.ParseIP(cfg.NativeServer) == nil {
|
|
return cfg, errDNSConfig
|
|
}
|
|
return cfg, nil
|
|
}
|
|
if cfg.Mode != "doh" {
|
|
return cfg, errDNSConfig
|
|
}
|
|
if v, ok := lookup("ZAPRET_AUTOTUNE_DOH_URL"); ok {
|
|
cfg.URL = v
|
|
}
|
|
u, err := url.Parse(cfg.URL)
|
|
if err != nil || u.Scheme != "https" || u.Opaque != "" || u.User != nil || u.RawQuery != "" || u.ForceQuery || u.Fragment != "" || strings.Contains(cfg.URL, "#") || u.Host == "" || u.Path == "" || !strings.HasPrefix(u.Path, "/") || (u.Port() != "" && u.Port() != "443") {
|
|
return cfg, errDNSConfig
|
|
}
|
|
cfg.Host = u.Hostname()
|
|
// Do not accept opaque/control-bearing authorities, zone IDs or ambiguous ports.
|
|
if cfg.Host == "" || strings.ContainsAny(cfg.URL, "\x00\r\n\t ") || strings.Contains(cfg.Host, "%") {
|
|
return cfg, errDNSConfig
|
|
}
|
|
if net.ParseIP(cfg.Host) == nil {
|
|
for _, label := range strings.Split(cfg.Host, ".") {
|
|
if len(label) == 0 || len(label) > 63 || label[0] == '-' || label[len(label)-1] == '-' {
|
|
return cfg, errDNSConfig
|
|
}
|
|
for _, c := range label {
|
|
if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-') {
|
|
return cfg, errDNSConfig
|
|
}
|
|
}
|
|
}
|
|
if len(cfg.Host) > 253 {
|
|
return cfg, errDNSConfig
|
|
}
|
|
}
|
|
if u.Host != net.JoinHostPort(cfg.Host, "443") && u.Host != cfg.Host && u.Host != "["+cfg.Host+"]" {
|
|
return cfg, errDNSConfig
|
|
}
|
|
bootstrap, explicit := lookup("ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS")
|
|
if !explicit || bootstrap == "" {
|
|
if cfg.URL != defaultDoHURL {
|
|
return cfg, errDNSConfig
|
|
}
|
|
bootstrap = "8.8.8.8,8.8.4.4"
|
|
}
|
|
parts := strings.Split(bootstrap, ",")
|
|
if len(parts) > 4 {
|
|
return cfg, errDNSConfig
|
|
}
|
|
for _, part := range parts {
|
|
ip := net.ParseIP(part)
|
|
if ip == nil {
|
|
return cfg, errDNSConfig
|
|
}
|
|
cfg.BootstrapIPs = append(cfg.BootstrapIPs, ip)
|
|
}
|
|
if v, ok := lookup("ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC"); ok {
|
|
seconds, err := strconv.Atoi(v)
|
|
if err != nil || seconds < 1 || seconds > 30 || strconv.Itoa(seconds) != v {
|
|
return cfg, errDNSConfig
|
|
}
|
|
cfg.Timeout = time.Duration(seconds) * time.Second
|
|
}
|
|
return cfg, nil
|
|
}
|
|
|
|
func (c managedDNSConfig) serverAddress() string {
|
|
if c.Mode == "doh" {
|
|
return dnsListenAddress
|
|
}
|
|
return c.NativeServer
|
|
}
|