feat: add configurable PID1-owned DoH forwarding
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>
This commit is contained in:
1 parent
ea886e5bc8
commit
367d956ae3
20 files changed
+2383
-37
No files matched your search
@@ -17,6 +17,9 @@ RUN if [ ! -e /opt/zapret2/lua ] && [ ! -L /opt/zapret2/lua ]; then \
|
|||||||
fi \
|
fi \
|
||||||
&& test -d /opt/zapret2/lua \
|
&& test -d /opt/zapret2/lua \
|
||||||
&& command -v setsid \
|
&& command -v setsid \
|
||||||
|
&& command -v stat \
|
||||||
|
&& test "$(/usr/local/bin/zapret-autotune dns-forwarder mode)" = doh \
|
||||||
|
&& test "$(/usr/local/bin/zapret-autotune dns-forwarder address)" = 127.0.0.1:5353 \
|
||||||
&& (ss-server -h 2>&1 | grep -q -- '-d <addr>') \
|
&& (ss-server -h 2>&1 | grep -q -- '-d <addr>') \
|
||||||
&& mkdir -p /etc/zapret-autotune /var/lib/zapret-autotune/target \
|
&& mkdir -p /etc/zapret-autotune /var/lib/zapret-autotune/target \
|
||||||
&& printf 'zapret-autotune-managed-v1\n' > /etc/zapret-autotune/target-id \
|
&& printf 'zapret-autotune-managed-v1\n' > /etc/zapret-autotune/target-id \
|
||||||
|
|||||||
+23
-3
@@ -320,7 +320,11 @@ signed URLs; use `docker compose ... config --quiet` for validation.
|
|||||||
| --- | --- |
|
| --- | --- |
|
||||||
| `ZAPRET_AUTOTUNE_IMAGE` | Local build/test default `zapret-autotune-managed-target:local`; production promotion requires a managed-target `repository@sha256:<digest>` |
|
| `ZAPRET_AUTOTUNE_IMAGE` | Local build/test default `zapret-autotune-managed-target:local`; production promotion requires a managed-target `repository@sha256:<digest>` |
|
||||||
| `SS_PASSWORD` | Required independent hex-only secret, at least 32 characters; deployment README generates 64 |
|
| `SS_PASSWORD` | Required independent hex-only secret, at least 32 characters; deployment README generates 64 |
|
||||||
| `SS_DNS_SERVER` | Empty (unchanged resolver); optional literal IPv4/IPv6 for target-only native ss-server DNS, validated before launch |
|
| `ZAPRET_AUTOTUNE_DNS_MODE` | `doh` default; explicit `native` opts into the old resolver, never automatic fallback |
|
||||||
|
| `ZAPRET_AUTOTUNE_DOH_URL` | `https://dns.google/dns-query`; HTTPS/443 path, no userinfo/query/fragment; path may be secret |
|
||||||
|
| `ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS` | Compose default empty: parser selects `8.8.8.8,8.8.4.4` only for exact default Google URL; custom provider requires explicit 1–4 comma-separated literal IPs without spaces |
|
||||||
|
| `ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC` | `5`; integer 1–30 |
|
||||||
|
| `SS_DNS_SERVER` | Explicit native mode only: empty retains base resolver or literal IPv4/IPv6; ignored in DoH mode |
|
||||||
| `ZAPRET_AUTOTUNE_YOUTUBE_SERVICE` | `0` (legacy) or opt-in `1`; explicitly forwarded by Compose |
|
| `ZAPRET_AUTOTUNE_YOUTUBE_SERVICE` | `0` (legacy) or opt-in `1`; explicitly forwarded by Compose |
|
||||||
| `ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE` | `cold` default or `adaptive` (requires service `1`); explicitly forwarded |
|
| `ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE` | `cold` default or `adaptive` (requires service `1`); explicitly forwarded |
|
||||||
| `ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS` | JSON array of 1..5 unique VideoIDs; defaults to only the primary ID; explicitly forwarded |
|
| `ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS` | JSON array of 1..5 unique VideoIDs; defaults to only the primary ID; explicitly forwarded |
|
||||||
@@ -375,10 +379,26 @@ errors never authorize retry. No new dependency, host service installation or
|
|||||||
external process manager is required. SIGTERM/SIGINT cancels backoff or the running
|
external process manager is required. SIGTERM/SIGINT cancels backoff or the running
|
||||||
monitor, waits for bounded restoration and joins children before exit.
|
monitor, waits for bounded restoration and joins children before exit.
|
||||||
|
|
||||||
|
In default DoH mode PID 1 first starts the Go UDP/TCP loopback DNS forwarder,
|
||||||
|
waits at most five seconds for its owned private PID marker, and only then starts
|
||||||
|
ss-server with `-d 127.0.0.1:5353`. No host DNS port or DNS/iptables changes are
|
||||||
|
made. Hostname SOCKS traffic and managed `socks5h` checks use the same encrypted
|
||||||
|
HTTPS resolver; clients must enable remote SOCKS DNS, not resolve locally or
|
||||||
|
send IP literals. Bootstrap uses configured literal IPs with verified TLS and
|
||||||
|
marked direct sockets excluding probe/live NFQUEUE. Endpoint failures produce
|
||||||
|
SERVFAIL without child exit or plaintext fallback. A forwarder crash, unlike an
|
||||||
|
upstream outage, uses the same bounded technical retry budget. The DNS child
|
||||||
|
stays alive through monitor restoration, zapret stop and SS joins; it is then
|
||||||
|
terminated/joined within five seconds and only its validated readiness inode is
|
||||||
|
removed using PID1-held directory/file anchors. Foreign/stale/replaced readiness
|
||||||
|
is preserved and vetoes startup/retry; SIGKILL does not lose owned cleanup proof. Explicit
|
||||||
|
native mode has no DNS child. See the deployment README for provider settings
|
||||||
|
and scope limits. New DoH runtime acceptance remains pending.
|
||||||
|
|
||||||
Compose restart remains disabled: a blanket container restart could loop over a
|
Compose restart remains disabled: a blanket container restart could loop over a
|
||||||
safety refusal or reset the in-container retry budget. Compose stop grace is 240s:
|
safety refusal or reset the in-container retry budget. Compose stop grace is 240s:
|
||||||
the sequential cleanup budget is 140s (45s monitor join, 45s reconciliation plus
|
the sequential cleanup budget is 145s (45s monitor join, 45s reconciliation plus
|
||||||
5s kill grace, 30s zapret stop plus 5s kill grace, and two 5s SS joins), with room
|
5s kill grace, 30s zapret stop plus 5s kill grace, two 5s SS joins and a 5s DNS join), with room
|
||||||
for a deferred 45s startup command plus kill grace and scheduling overhead.
|
for a deferred 45s startup command plus kill grace and scheduling overhead.
|
||||||
The join watchdog runs in a fresh shell and is cancelled when its child finishes;
|
The join watchdog runs in a fresh shell and is cancelled when its child finishes;
|
||||||
graceful shutdown does not wait out the watchdog's full deadline.
|
graceful shutdown does not wait out the watchdog's full deadline.
|
||||||
|
|||||||
@@ -4,6 +4,13 @@
|
|||||||
|
|
||||||
## Текущий статус
|
## Текущий статус
|
||||||
|
|
||||||
|
### Managed DoH integration (2026-10-13): local implementation, remote gate pending
|
||||||
|
|
||||||
|
- Managed target defaults to the stdlib Go UDP/TCP loopback DoH forwarder; PID 1 validates owned readiness before ss-server `-d 127.0.0.1:5353`. SOCKS remote-hostname traffic and managed `socks5h` checks share this path. Provider URL/bootstrap IPs/timeout are private-env configurable; explicit native mode alone preserves legacy DNS, never automatic fallback.
|
||||||
|
- DNS stays available during monitor restoration, zapret stop and SS joins, then is terminated/joined with a five-second watchdog and readiness cleanup. DNS child death uses existing bounded retries; upstream failures return SERVFAIL without stopping the forwarder. No host DNS port, global DNS changes, new dependency or capability.
|
||||||
|
- Security follow-up: PID 1 retains directory/readiness inode descriptors for validated owned cleanup after graceful exit or SIGKILL; foreign/stale/replaced markers are preserved and fail closed. Compose passes empty bootstrap by default, allowing Google defaults only for the exact Google URL; custom URLs require explicit IPs.
|
||||||
|
- Offline shell lifecycle/static/config fixtures (real readiness CLI with inherited anchors), sh/bash syntax checks, `go test ./... -count=1 -timeout 180s` and targeted readiness/entrypoint `-race` tests passed locally. Coverage includes foreign regular/symlink preservation, SIGKILL owned cleanup/retry, custom-provider Compose validation, signal/cooldown cancellation and DNS-last cleanup. No deployment or external-network tests performed here. Remote encrypted-path, browser/check and upstream-outage acceptance remains pending; earlier PASS is not DoH qualification.
|
||||||
|
|
||||||
### Autonomous three-CDN field acceptance (2026-10-13): media qualified, NOT READY
|
### Autonomous three-CDN field acceptance (2026-10-13): media qualified, NOT READY
|
||||||
|
|
||||||
- Supplied field results for source `3fc8563995e890310580ba8f78850b92fea42136`, managed `Dockerfile.target` image `sha256:a902e4efd949a7ffce4414fd99ab4a815a643b40cf9b9e30e2bd2db7b452817a`, container `4c6b34…`, Compose project `rc0398e6f…`, isolated SOCKS `127.0.0.1:11082`. This documentation update performs no deployment or network probes. Private sanitized artifact: `/home/agent/zapret2-autotune-rc-3fc8563/field-private/proof/final.safe.json`; its contents are reported by the orchestrator, not independently reread here. Later docs commits are not the tested image source.
|
- Supplied field results for source `3fc8563995e890310580ba8f78850b92fea42136`, managed `Dockerfile.target` image `sha256:a902e4efd949a7ffce4414fd99ab4a815a643b40cf9b9e30e2bd2db7b452817a`, container `4c6b34…`, Compose project `rc0398e6f…`, isolated SOCKS `127.0.0.1:11082`. This documentation update performs no deployment or network probes. Private sanitized artifact: `/home/agent/zapret2-autotune-rc-3fc8563/field-private/proof/final.safe.json`; its contents are reported by the orchestrator, not independently reread here. Later docs commits are not the tested image source.
|
||||||
|
|||||||
@@ -4,6 +4,13 @@
|
|||||||
|
|
||||||
Go CLI for scanning and checking YouTube TLS bypass strategies with `blockcheckw`.
|
Go CLI for scanning and checking YouTube TLS bypass strategies with `blockcheckw`.
|
||||||
|
|
||||||
|
The managed target now defaults to encrypted DoH for both SOCKS hostname traffic
|
||||||
|
and managed checks, with no plaintext fallback. Configure provider URL/bootstrap
|
||||||
|
IPs in the private deployment `.env`; native DNS requires an explicit opt-out.
|
||||||
|
Clients must enable SOCKS5 remote DNS (e.g. browser remote DNS on the chosen
|
||||||
|
loopback SOCKS port). See [DNS configuration and limits](deploy/managed-target/README.md).
|
||||||
|
This new integration is not covered by the earlier clean-run acceptance.
|
||||||
|
|
||||||
## Production preparation (isolated clean-run acceptance passed)
|
## Production preparation (isolated clean-run acceptance passed)
|
||||||
|
|
||||||
The standalone deployment candidate is a **new isolated target**, never an
|
The standalone deployment candidate is a **new isolated target**, never an
|
||||||
|
|||||||
@@ -0,0 +1,663 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/tls"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"mime"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const dnsSocketMark = 0x50000000 // Exclude both deployed (0x40000000) and probe (0x10000000) queues.
|
||||||
|
const dnsMaxQuery = 4096
|
||||||
|
const dnsConcurrency = 16
|
||||||
|
const dnsMaxUDPPayload = 65507 // Fixed IPv4 listener: maximum UDP payload, excluding IP/UDP headers.
|
||||||
|
|
||||||
|
var errDNSForwarder = errors.New("DNS forwarder unavailable")
|
||||||
|
|
||||||
|
func markDNSSocket(_, _ string, raw syscall.RawConn) error {
|
||||||
|
return setDNSSocketMark(raw, syscall.SetsockoptInt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func setDNSSocketMark(raw syscall.RawConn, set func(int, int, int, int) error) error {
|
||||||
|
var markErr error
|
||||||
|
err := raw.Control(func(fd uintptr) { markErr = set(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, dnsSocketMark) })
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return markErr
|
||||||
|
}
|
||||||
|
|
||||||
|
type dnsDialFunc func(context.Context, string, string) (net.Conn, error)
|
||||||
|
|
||||||
|
func dnsBootstrapDial(cfg managedDNSConfig, dial dnsDialFunc) dnsDialFunc {
|
||||||
|
var next atomic.Uint64
|
||||||
|
return func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
|
if network != "tcp" || address != net.JoinHostPort(cfg.Host, "443") || len(cfg.BootstrapIPs) == 0 {
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
ctx, cancelAll := context.WithTimeout(ctx, cfg.Timeout)
|
||||||
|
defer cancelAll()
|
||||||
|
start := int((next.Add(1) - 1) % uint64(len(cfg.BootstrapIPs)))
|
||||||
|
for i := range cfg.BootstrapIPs {
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// Give every bootstrap a bounded share of the remaining deadline.
|
||||||
|
remaining := cfg.Timeout
|
||||||
|
if deadline, ok := ctx.Deadline(); ok {
|
||||||
|
remaining = time.Until(deadline)
|
||||||
|
}
|
||||||
|
attempt, cancel := context.WithTimeout(ctx, remaining/time.Duration(len(cfg.BootstrapIPs)-i))
|
||||||
|
conn, err := dial(attempt, "tcp", net.JoinHostPort(cfg.BootstrapIPs[(start+i)%len(cfg.BootstrapIPs)].String(), "443"))
|
||||||
|
cancel()
|
||||||
|
if err == nil {
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newDNSHTTPClient(cfg managedDNSConfig, control func(string, string, syscall.RawConn) error) (*http.Client, *http.Transport) {
|
||||||
|
dialer := &net.Dialer{Timeout: cfg.Timeout, Control: control}
|
||||||
|
transport := &http.Transport{
|
||||||
|
Proxy: nil, DialContext: dnsBootstrapDial(cfg, dialer.DialContext),
|
||||||
|
TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12, ServerName: cfg.Host},
|
||||||
|
TLSHandshakeTimeout: cfg.Timeout, ResponseHeaderTimeout: cfg.Timeout,
|
||||||
|
DisableCompression: true, MaxConnsPerHost: dnsConcurrency,
|
||||||
|
MaxIdleConns: dnsConcurrency, MaxIdleConnsPerHost: dnsConcurrency,
|
||||||
|
IdleConnTimeout: 30 * time.Second, MaxResponseHeaderBytes: 8192,
|
||||||
|
}
|
||||||
|
return &http.Client{Transport: transport, Timeout: cfg.Timeout, CheckRedirect: func(*http.Request, []*http.Request) error { return errDNSForwarder }}, transport
|
||||||
|
}
|
||||||
|
|
||||||
|
// DNS names may use compression. Bound both pointer traversal and expanded name size.
|
||||||
|
func dnsNameEnd(wire []byte, start int) (int, bool) {
|
||||||
|
end, _, ok := dnsName(wire, start)
|
||||||
|
return end, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsName(wire []byte, start int) (int, []byte, bool) {
|
||||||
|
pos, end, expanded := start, -1, 0
|
||||||
|
var name []byte
|
||||||
|
for steps := 0; steps < 256; steps++ {
|
||||||
|
if pos < 12 || pos >= len(wire) {
|
||||||
|
return 0, nil, false
|
||||||
|
}
|
||||||
|
n := int(wire[pos])
|
||||||
|
pos++
|
||||||
|
if n == 0 {
|
||||||
|
if end < 0 {
|
||||||
|
end = pos
|
||||||
|
}
|
||||||
|
return end, append(name, 0), true
|
||||||
|
}
|
||||||
|
if n&0xc0 == 0xc0 {
|
||||||
|
if pos >= len(wire) {
|
||||||
|
return 0, nil, false
|
||||||
|
}
|
||||||
|
target := (n&0x3f)<<8 | int(wire[pos])
|
||||||
|
if target < 12 || target >= len(wire) {
|
||||||
|
return 0, nil, false
|
||||||
|
}
|
||||||
|
if end < 0 {
|
||||||
|
end = pos + 1
|
||||||
|
}
|
||||||
|
pos = target
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if n > 63 || pos+n >= len(wire) {
|
||||||
|
return 0, nil, false
|
||||||
|
}
|
||||||
|
expanded += n + 1
|
||||||
|
if expanded > 254 {
|
||||||
|
return 0, nil, false
|
||||||
|
}
|
||||||
|
name = append(name, byte(n))
|
||||||
|
name = append(name, wire[pos:pos+n]...)
|
||||||
|
pos += n
|
||||||
|
}
|
||||||
|
return 0, nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsQuestionEnd(wire []byte) (int, bool) {
|
||||||
|
end, _, ok := dnsQuestion(wire)
|
||||||
|
return end, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsQuestion(wire []byte) (int, []byte, bool) {
|
||||||
|
if len(wire) < 12 || binary.BigEndian.Uint16(wire[4:6]) != 1 {
|
||||||
|
return 0, nil, false
|
||||||
|
}
|
||||||
|
end, name, ok := dnsName(wire, 12)
|
||||||
|
if !ok || end+4 > len(wire) {
|
||||||
|
return 0, nil, false
|
||||||
|
}
|
||||||
|
if binary.BigEndian.Uint16(wire[end:end+2]) == 0 || binary.BigEndian.Uint16(wire[end+2:end+4]) == 0 {
|
||||||
|
return 0, nil, false
|
||||||
|
}
|
||||||
|
return end + 4, append(name, wire[end:end+4]...), true
|
||||||
|
}
|
||||||
|
|
||||||
|
func validDNSQuery(wire []byte) bool {
|
||||||
|
if len(wire) < 12 || len(wire) > dnsMaxQuery || wire[2]&0xfe != 0 || wire[3]&0xcf != 0 || binary.BigEndian.Uint16(wire[6:8]) != 0 || binary.BigEndian.Uint16(wire[8:10]) != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
end, ok := dnsQuestionEnd(wire)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, ok = dnsQueryUDPLimit(wire, end)
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Query additional records have the same framing as answers. Only OPT affects
|
||||||
|
// downstream UDP sizing; its root owner and option framing must be valid.
|
||||||
|
func dnsQueryUDPLimit(wire []byte, end int) (int, bool) {
|
||||||
|
limit, seenOPT := 512, false
|
||||||
|
for count := int(binary.BigEndian.Uint16(wire[10:12])); count > 0; count-- {
|
||||||
|
header, name, ok := dnsName(wire, end)
|
||||||
|
if !ok || header+10 > len(wire) {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
end = header + 10 + int(binary.BigEndian.Uint16(wire[header+8:header+10]))
|
||||||
|
if end > len(wire) {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
if binary.BigEndian.Uint16(wire[header:header+2]) != 41 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if seenOPT || len(name) != 1 || wire[header+4] != 0 || wire[header+5] != 0 || binary.BigEndian.Uint16(wire[header+6:header+8])&0x7fff != 0 {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
seenOPT = true
|
||||||
|
limit = int(binary.BigEndian.Uint16(wire[header+2 : header+4]))
|
||||||
|
if limit < 512 {
|
||||||
|
limit = 512
|
||||||
|
}
|
||||||
|
if limit > dnsMaxUDPPayload {
|
||||||
|
limit = dnsMaxUDPPayload
|
||||||
|
}
|
||||||
|
for option := header + 10; option < end; {
|
||||||
|
if option+4 > end {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
option += 4 + int(binary.BigEndian.Uint16(wire[option+2:option+4]))
|
||||||
|
if option > end {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return limit, end == len(wire)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate only record boundaries (including EDNS/DNSSEC), never decode or rewrite RDATA.
|
||||||
|
func validDNSRecordFraming(wire []byte, end, count int) bool {
|
||||||
|
for ; count > 0; count-- {
|
||||||
|
var ok bool
|
||||||
|
end, ok = dnsNameEnd(wire, end)
|
||||||
|
if !ok || end+10 > len(wire) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
end += 10 + int(binary.BigEndian.Uint16(wire[end+8:end+10]))
|
||||||
|
if end > len(wire) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return end == len(wire)
|
||||||
|
}
|
||||||
|
|
||||||
|
func validDNSResponse(query, response []byte) bool {
|
||||||
|
if len(query) < 12 || len(response) < 12 || len(response) > 65535 || response[2]&0x80 == 0 || response[2]&0x78 != query[2]&0x78 || !bytes.Equal(query[:2], response[:2]) || response[3]&0x40 != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
_, question, qok := dnsQuestion(query)
|
||||||
|
re, responseQuestion, rok := dnsQuestion(response)
|
||||||
|
if !qok || !rok || !bytes.Equal(question, responseQuestion) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
count := int(binary.BigEndian.Uint16(response[6:8])) + int(binary.BigEndian.Uint16(response[8:10])) + int(binary.BigEndian.Uint16(response[10:12]))
|
||||||
|
return validDNSRecordFraming(response, re, count)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsServfail(query []byte) []byte {
|
||||||
|
if !validDNSQuery(query) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
reply := dnsQuestionReply(query, query)
|
||||||
|
reply[2] = 0x80 | query[2]&0x79 // QR, opcode and RD, not AA/TC.
|
||||||
|
reply[3] = 0x02 // SERVFAIL, no fabricated recursion/DNSSEC assertions.
|
||||||
|
return reply
|
||||||
|
}
|
||||||
|
|
||||||
|
// Re-encode the question without pointers into records omitted from this reply.
|
||||||
|
func dnsQuestionReply(query, header []byte) []byte {
|
||||||
|
_, question, ok := dnsQuestion(query)
|
||||||
|
if !ok || len(header) < 12 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
reply := append([]byte(nil), header[:12]...)
|
||||||
|
clear(reply[6:12])
|
||||||
|
return append(reply, question...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsUDPReply(query, response []byte) []byte {
|
||||||
|
if response == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
end, ok := dnsQuestionEnd(query)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
limit, ok := dnsQueryUDPLimit(query, end)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if len(response) <= limit {
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
reply := dnsQuestionReply(query, response)
|
||||||
|
if reply != nil {
|
||||||
|
reply[2] |= 0x82 // QR and TC; preserve upstream opcode, RD, AA and other flags.
|
||||||
|
reply[3] &^= 0x40 // Reserved Z bit; no records/assertions are invented.
|
||||||
|
}
|
||||||
|
return reply
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeDNSDatagram(udp net.PacketConn, addr net.Addr, query, response []byte) {
|
||||||
|
if reply := dnsUDPReply(query, response); reply != nil {
|
||||||
|
if _, err := udp.WriteTo(reply, addr); err != nil {
|
||||||
|
return
|
||||||
|
} // Drop only this datagram; never retry plaintext or stop the server.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type dnsForwarder struct {
|
||||||
|
cfg managedDNSConfig
|
||||||
|
client *http.Client
|
||||||
|
slots chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *dnsForwarder) exchange(ctx context.Context, query []byte) []byte {
|
||||||
|
if !validDNSQuery(query) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(ctx, f.cfg.Timeout)
|
||||||
|
defer cancel()
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, f.cfg.URL, bytes.NewReader(query))
|
||||||
|
if err != nil {
|
||||||
|
return dnsServfail(query)
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/dns-message")
|
||||||
|
req.Header.Set("Accept", "application/dns-message")
|
||||||
|
resp, err := f.client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return dnsServfail(query)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
typ, _, err := mime.ParseMediaType(resp.Header.Get("Content-Type"))
|
||||||
|
if err != nil || typ != "application/dns-message" || resp.StatusCode != http.StatusOK || resp.Header.Get("Content-Encoding") != "" {
|
||||||
|
return dnsServfail(query)
|
||||||
|
}
|
||||||
|
body, err := io.ReadAll(io.LimitReader(resp.Body, 65536))
|
||||||
|
if err != nil || resp.ContentLength >= 0 && resp.ContentLength != int64(len(body)) || !validDNSResponse(query, body) {
|
||||||
|
return dnsServfail(query)
|
||||||
|
}
|
||||||
|
return body
|
||||||
|
}
|
||||||
|
|
||||||
|
// One connection owns one request slot; frames are sequential, capped in number
|
||||||
|
// and total lifetime, so slow/idle clients cannot create unbounded workers.
|
||||||
|
func (f *dnsForwarder) tcpWorker(ctx context.Context, conn net.Conn) {
|
||||||
|
defer conn.Close()
|
||||||
|
stop := context.AfterFunc(ctx, func() { conn.Close() })
|
||||||
|
defer stop()
|
||||||
|
lifetime := time.Now().Add(16 * f.cfg.Timeout)
|
||||||
|
for frame := 0; frame < 16; frame++ {
|
||||||
|
deadline := time.Now().Add(f.cfg.Timeout)
|
||||||
|
if lifetime.Before(deadline) {
|
||||||
|
deadline = lifetime
|
||||||
|
}
|
||||||
|
if err := conn.SetDeadline(deadline); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var size [2]byte
|
||||||
|
if _, err := io.ReadFull(conn, size[:]); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
n := int(binary.BigEndian.Uint16(size[:]))
|
||||||
|
if n < 12 || n > dnsMaxQuery {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
query := make([]byte, n)
|
||||||
|
if _, err := io.ReadFull(conn, query); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
reply := f.exchange(ctx, query)
|
||||||
|
if reply == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
binary.BigEndian.PutUint16(size[:], uint16(len(reply)))
|
||||||
|
if err := writeDNSFrame(conn, size[:], reply); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeDNSFrame(w io.Writer, size, reply []byte) error {
|
||||||
|
for _, part := range [][]byte{size, reply} {
|
||||||
|
for len(part) > 0 {
|
||||||
|
n, err := w.Write(part)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return io.ErrShortWrite
|
||||||
|
}
|
||||||
|
part = part[n:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *dnsForwarder) run(ctx context.Context, udp net.PacketConn, tcp net.Listener, ready func() (func(), error)) error {
|
||||||
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
defer cancel()
|
||||||
|
stop := context.AfterFunc(ctx, func() { udp.Close(); tcp.Close() })
|
||||||
|
defer stop()
|
||||||
|
defer udp.Close()
|
||||||
|
defer tcp.Close()
|
||||||
|
cleanup, err := ready()
|
||||||
|
if err != nil {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
defer cleanup()
|
||||||
|
var workers sync.WaitGroup
|
||||||
|
var loops sync.WaitGroup
|
||||||
|
var failed atomic.Bool
|
||||||
|
loops.Add(2)
|
||||||
|
go func() {
|
||||||
|
defer loops.Done()
|
||||||
|
buffer := make([]byte, dnsMaxQuery+1)
|
||||||
|
for {
|
||||||
|
n, addr, err := udp.ReadFrom(buffer)
|
||||||
|
if err != nil {
|
||||||
|
if ctx.Err() == nil {
|
||||||
|
failed.Store(true)
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !validDNSQuery(buffer[:n]) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case f.slots <- struct{}{}:
|
||||||
|
query := append([]byte(nil), buffer[:n]...)
|
||||||
|
workers.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer workers.Done()
|
||||||
|
defer func() { <-f.slots }()
|
||||||
|
if reply := f.exchange(ctx, query); reply != nil {
|
||||||
|
writeDNSDatagram(udp, addr, query, reply)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
default:
|
||||||
|
writeDNSDatagram(udp, addr, buffer[:n], dnsServfail(buffer[:n]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
go func() {
|
||||||
|
defer loops.Done()
|
||||||
|
for {
|
||||||
|
conn, err := tcp.Accept()
|
||||||
|
if err != nil {
|
||||||
|
if ctx.Err() == nil {
|
||||||
|
failed.Store(true)
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case f.slots <- struct{}{}:
|
||||||
|
workers.Add(1)
|
||||||
|
go func() { defer workers.Done(); defer func() { <-f.slots }(); f.tcpWorker(ctx, conn) }()
|
||||||
|
default:
|
||||||
|
conn.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
loops.Wait()
|
||||||
|
workers.Wait()
|
||||||
|
if failed.Load() {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func createDNSReady(path string) (func(), error) {
|
||||||
|
dir := filepath.Dir(path)
|
||||||
|
if err := managedDirectory(dir); err != nil {
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
info, err := os.Lstat(dir)
|
||||||
|
if err != nil || info.Mode().Perm() != 0700 || info.Mode()&(os.ModeSetuid|os.ModeSetgid|os.ModeSticky) != 0 {
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
fd, err := syscall.Open(dir, syscall.O_RDONLY|syscall.O_DIRECTORY|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
parent := os.NewFile(uintptr(fd), dir)
|
||||||
|
opened, err := parent.Stat()
|
||||||
|
if err != nil || !os.SameFile(info, opened) {
|
||||||
|
parent.Close()
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
heldDir := fmt.Sprintf("/proc/self/fd/%d", fd)
|
||||||
|
file, err := os.CreateTemp(heldDir, ".dns-ready-")
|
||||||
|
if err != nil {
|
||||||
|
parent.Close()
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
owned, statErr := file.Stat()
|
||||||
|
if statErr == nil && owned.Mode().Perm() != 0600 {
|
||||||
|
statErr = errDNSForwarder
|
||||||
|
}
|
||||||
|
var once sync.Once
|
||||||
|
cleanup := func() {
|
||||||
|
once.Do(func() {
|
||||||
|
// Inspect and unlink relative to the held private parent, not a replacement directory.
|
||||||
|
for _, name := range []string{filepath.Base(path), filepath.Base(file.Name())} {
|
||||||
|
current, e := os.Lstat(filepath.Join(heldDir, name))
|
||||||
|
if e == nil && owned != nil && os.SameFile(owned, current) {
|
||||||
|
syscall.Unlinkat(fd, name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
parent.Close()
|
||||||
|
// Hold the inode until all identity checks/unlinks finish, preventing reuse.
|
||||||
|
file.Close()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if statErr == nil {
|
||||||
|
err = publishDNSReady(file, filepath.Join(heldDir, filepath.Base(path)))
|
||||||
|
}
|
||||||
|
if statErr != nil || err != nil {
|
||||||
|
cleanup()
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
if err := syscall.Unlinkat(fd, filepath.Base(file.Name())); err != nil {
|
||||||
|
cleanup()
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
return cleanup, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func publishDNSReady(file *os.File, destination string) error {
|
||||||
|
if _, err := fmt.Fprintf(file, "%d\n", os.Getpid()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := file.Sync(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.Link(file.Name(), destination) // Atomic complete-PID publication, EXCL even for a symlink destination.
|
||||||
|
}
|
||||||
|
|
||||||
|
func serveDNSForwarder(ctx context.Context, cfg managedDNSConfig) error {
|
||||||
|
udp, err := net.ListenPacket("udp4", dnsListenAddress)
|
||||||
|
if err != nil {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
tcp, err := net.Listen("tcp4", dnsListenAddress)
|
||||||
|
if err != nil {
|
||||||
|
udp.Close()
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
client, transport := newDNSHTTPClient(cfg, markDNSSocket)
|
||||||
|
defer transport.CloseIdleConnections()
|
||||||
|
f := &dnsForwarder{cfg: cfg, client: client, slots: make(chan struct{}, dnsConcurrency)}
|
||||||
|
return f.run(ctx, udp, tcp, func() (func(), error) { return createDNSReady(dnsReadyPath) })
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsForwarderCLI(args []string, stdout, stderr io.Writer) int {
|
||||||
|
if len(args) > 0 && (args[0] == "readiness-check" || args[0] == "readiness-cleanup") {
|
||||||
|
return dnsReadinessCLI(args, stderr)
|
||||||
|
}
|
||||||
|
if len(args) != 1 || (args[0] != "mode" && args[0] != "address" && args[0] != "serve") {
|
||||||
|
fmt.Fprintln(stderr, "configerror: invalid DNS forwarder command")
|
||||||
|
return 64
|
||||||
|
}
|
||||||
|
cfg, err := parseManagedDNSConfig(os.LookupEnv)
|
||||||
|
if err != nil || args[0] == "serve" && cfg.Mode != "doh" {
|
||||||
|
fmt.Fprintln(stderr, "configerror: invalid managed DNS configuration")
|
||||||
|
return 64
|
||||||
|
}
|
||||||
|
switch args[0] {
|
||||||
|
case "mode":
|
||||||
|
_, err = fmt.Fprintln(stdout, cfg.Mode)
|
||||||
|
case "address":
|
||||||
|
_, err = fmt.Fprintln(stdout, cfg.serverAddress())
|
||||||
|
case "serve":
|
||||||
|
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
defer stop()
|
||||||
|
err = serveDNSForwarder(ctx, cfg)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(stderr, "error: DNS forwarder unavailable")
|
||||||
|
return 75
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
// PID 1 holds FD 8 (private directory) and FD 9 (readiness inode) across the
|
||||||
|
// child's lifetime, including SIGKILL. Helpers borrow duplicates, never offsets.
|
||||||
|
func duplicateDNSAnchor(fd int) (*os.File, error) {
|
||||||
|
// Keep duplicates above both reserved descriptors; a missing anchor must
|
||||||
|
// never be filled by duplicating the other one.
|
||||||
|
duplicate, _, err := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd), syscall.F_DUPFD_CLOEXEC, 10)
|
||||||
|
if err != 0 {
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
}
|
||||||
|
return os.NewFile(duplicate, "DNS readiness anchor"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsReadinessCLI(args []string, stderr io.Writer) int {
|
||||||
|
if len(args) != 2 {
|
||||||
|
fmt.Fprintln(stderr, "configerror: invalid DNS readiness command")
|
||||||
|
return 64
|
||||||
|
}
|
||||||
|
pid, err := strconv.ParseInt(args[1], 10, 32)
|
||||||
|
if err != nil || pid <= 1 || strconv.FormatInt(pid, 10) != args[1] {
|
||||||
|
fmt.Fprintln(stderr, "configerror: invalid DNS readiness command")
|
||||||
|
return 64
|
||||||
|
}
|
||||||
|
cfg, err := parseManagedDNSConfig(os.LookupEnv)
|
||||||
|
if err != nil || cfg.Mode != "doh" {
|
||||||
|
fmt.Fprintln(stderr, "configerror: invalid managed DNS configuration")
|
||||||
|
return 64
|
||||||
|
}
|
||||||
|
anchor, _ := duplicateDNSAnchor(9)
|
||||||
|
if anchor != nil {
|
||||||
|
defer anchor.Close()
|
||||||
|
}
|
||||||
|
parent, err := duplicateDNSAnchor(8)
|
||||||
|
if err == nil {
|
||||||
|
defer parent.Close()
|
||||||
|
err = dnsReadinessAction(parent, anchor, int(pid), args[0] == "readiness-cleanup")
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(stderr, "error: DNS readiness ownership unavailable")
|
||||||
|
return 78
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsOwnedAnchor(info os.FileInfo, directory bool) bool {
|
||||||
|
stat, ok := info.Sys().(*syscall.Stat_t)
|
||||||
|
if !ok || stat.Uid != uint32(os.Geteuid()) || info.Mode()&(os.ModeSetuid|os.ModeSetgid|os.ModeSticky) != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if directory {
|
||||||
|
return info.IsDir() && info.Mode().Perm() == 0700
|
||||||
|
}
|
||||||
|
return info.Mode().IsRegular() && info.Mode().Perm() == 0600
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsReadinessAction(parent, anchor *os.File, expectedPID int, cleanup bool) error {
|
||||||
|
if parent == nil || expectedPID <= 1 || int64(expectedPID) > 2147483647 {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
info, err := parent.Stat()
|
||||||
|
if err != nil || !dnsOwnedAnchor(info, true) {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
name := filepath.Base(dnsReadyPath)
|
||||||
|
fd, err := syscall.Openat(int(parent.Fd()), name, syscall.O_RDONLY|syscall.O_NONBLOCK|syscall.O_NOFOLLOW|syscall.O_CLOEXEC, 0)
|
||||||
|
if err != nil {
|
||||||
|
if cleanup && errors.Is(err, syscall.ENOENT) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
current := os.NewFile(uintptr(fd), "DNS readiness entry")
|
||||||
|
defer current.Close()
|
||||||
|
if anchor == nil {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
owned, err := anchor.Stat()
|
||||||
|
if err != nil || !dnsOwnedAnchor(owned, false) {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
entry, err := current.Stat()
|
||||||
|
if err != nil || !dnsOwnedAnchor(entry, false) || !os.SameFile(owned, entry) {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
expected := strconv.Itoa(expectedPID) + "\n"
|
||||||
|
if owned.Size() != int64(len(expected)) {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
buffer := make([]byte, len(expected))
|
||||||
|
if _, err := anchor.ReadAt(buffer, 0); err != nil || string(buffer) != expected {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
if cleanup {
|
||||||
|
if err := syscall.Unlinkat(int(parent.Fd()), name); err != nil && !errors.Is(err, syscall.ENOENT) {
|
||||||
|
return errDNSForwarder
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dnsConfigForTest(t *testing.T, env map[string]string) managedDNSConfig {
|
||||||
|
t.Helper()
|
||||||
|
cfg, err := parseManagedDNSConfig(func(k string) (string, bool) { v, ok := env[k]; return v, ok })
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return cfg
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSConfigDefaultsAndNative(t *testing.T) {
|
||||||
|
cfg := dnsConfigForTest(t, map[string]string{"SS_DNS_SERVER": "ignored\nsecret"})
|
||||||
|
if cfg.Mode != "doh" || cfg.URL != defaultDoHURL || cfg.Host != "dns.google" || cfg.Timeout != 5*time.Second || cfg.serverAddress() != dnsListenAddress || len(cfg.BootstrapIPs) != 2 {
|
||||||
|
t.Fatal(cfg)
|
||||||
|
}
|
||||||
|
emptyBootstrap := dnsConfigForTest(t, map[string]string{"ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS": ""})
|
||||||
|
if len(emptyBootstrap.BootstrapIPs) != 2 || emptyBootstrap.BootstrapIPs[0].String() != "8.8.8.8" || emptyBootstrap.BootstrapIPs[1].String() != "8.8.4.4" {
|
||||||
|
t.Fatal("empty default bootstrap did not use default IPs")
|
||||||
|
}
|
||||||
|
for _, ip := range []string{"", "8.8.8.8", "::1"} {
|
||||||
|
cfg = dnsConfigForTest(t, map[string]string{"ZAPRET_AUTOTUNE_DNS_MODE": "native", "SS_DNS_SERVER": ip, "ZAPRET_AUTOTUNE_DOH_URL": "ignored"})
|
||||||
|
if cfg.serverAddress() != ip {
|
||||||
|
t.Fatal(cfg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cfg = dnsConfigForTest(t, map[string]string{"ZAPRET_AUTOTUNE_DOH_URL": "https://resolver.example:443/dns-query", "ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS": "1.1.1.1,2001:db8::1", "ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC": "30"})
|
||||||
|
if cfg.Host != "resolver.example" || cfg.Timeout != 30*time.Second {
|
||||||
|
t.Fatal(cfg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSConfigRejectsOpaqueInputs(t *testing.T) {
|
||||||
|
cases := []map[string]string{
|
||||||
|
{"ZAPRET_AUTOTUNE_DNS_MODE": ""}, {"ZAPRET_AUTOTUNE_DNS_MODE": "auto"},
|
||||||
|
{"ZAPRET_AUTOTUNE_DNS_MODE": "native", "SS_DNS_SERVER": "dns.google"},
|
||||||
|
{"ZAPRET_AUTOTUNE_DNS_MODE": "native", "SS_DNS_SERVER": "8.8.8.8:53"},
|
||||||
|
{"ZAPRET_AUTOTUNE_DOH_URL": "https://other.example/dns-query"},
|
||||||
|
{"ZAPRET_AUTOTUNE_DOH_URL": "https://other.example/dns-query", "ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS": ""},
|
||||||
|
{"ZAPRET_AUTOTUNE_DOH_URL": "https://dns.google/custom-path", "ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS": ""},
|
||||||
|
}
|
||||||
|
for _, raw := range []string{"", "http://dns.google/dns-query", "https://token@dns.google/dns-query", "https://dns.google/dns-query?token=secret", "https://dns.google/dns-query#", "https://dns.google:5353/dns-query", "https://dns.google:/dns-query", "https://dns.google", "https://dns.google/\nsecret", "https://dns_google/dns-query"} {
|
||||||
|
cases = append(cases, map[string]string{"ZAPRET_AUTOTUNE_DOH_URL": raw, "ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS": "8.8.8.8"})
|
||||||
|
}
|
||||||
|
for _, raw := range []string{"dns.google", "1.1.1.1,", " 1.1.1.1", "1.1.1.1:443", "[::1]", "fe80::1%eth0", "1.1.1.1,1.1.1.1,1.1.1.1,1.1.1.1,1.1.1.1"} {
|
||||||
|
cases = append(cases, map[string]string{"ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS": raw})
|
||||||
|
}
|
||||||
|
for _, raw := range []string{"", "0", "31", "-1", "05", "1\n"} {
|
||||||
|
cases = append(cases, map[string]string{"ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC": raw})
|
||||||
|
}
|
||||||
|
for i, env := range cases {
|
||||||
|
_, err := parseManagedDNSConfig(func(k string) (string, bool) { v, ok := env[k]; return v, ok })
|
||||||
|
if err != errDNSConfig {
|
||||||
|
t.Errorf("case %d: %v", i, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSForwarderCLI(t *testing.T) {
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DNS_MODE", "doh")
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DOH_URL", defaultDoHURL)
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS", "8.8.8.8")
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC", "5")
|
||||||
|
t.Setenv("SS_DNS_SERVER", "secret-invalid-legacy")
|
||||||
|
for cmd, want := range map[string]string{"mode": "doh\n", "address": dnsListenAddress + "\n"} {
|
||||||
|
var out, diagnostic bytes.Buffer
|
||||||
|
if code := run([]string{"dns-forwarder", cmd}, &out, &diagnostic); code != 0 || out.String() != want || diagnostic.Len() != 0 {
|
||||||
|
t.Fatal(code, out.String(), diagnostic.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DNS_MODE", "native")
|
||||||
|
t.Setenv("SS_DNS_SERVER", "")
|
||||||
|
var out, diagnostic bytes.Buffer
|
||||||
|
if code := run([]string{"dns-forwarder", "address"}, &out, &diagnostic); code != 0 || out.String() != "\n" {
|
||||||
|
t.Fatal(code, out.String())
|
||||||
|
}
|
||||||
|
for _, args := range [][]string{{"serve"}, {"wrong"}, {"mode", "extra"}} {
|
||||||
|
out.Reset()
|
||||||
|
diagnostic.Reset()
|
||||||
|
if code := dnsForwarderCLI(args, &out, &diagnostic); code != 64 || out.Len() != 0 || !strings.HasPrefix(diagnostic.String(), "configerror:") {
|
||||||
|
t.Fatal(code, out.String(), diagnostic.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,295 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dnsReadyAnchorsForTest(t *testing.T) (string, *os.File, *os.File, func()) {
|
||||||
|
t.Helper()
|
||||||
|
dir := t.TempDir()
|
||||||
|
if err := os.Chmod(dir, 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
path := filepath.Join(dir, "dns-ready")
|
||||||
|
cleanup, err := createDNSReady(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(cleanup)
|
||||||
|
parent, err := os.Open(dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { parent.Close() })
|
||||||
|
anchor, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { anchor.Close() })
|
||||||
|
return path, parent, anchor, cleanup
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSReadinessAnchorActions(t *testing.T) {
|
||||||
|
path, parent, anchor, childCleanup := dnsReadyAnchorsForTest(t)
|
||||||
|
pid := os.Getpid()
|
||||||
|
if _, err := anchor.Seek(1, io.SeekStart); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := dnsReadinessAction(parent, anchor, pid, false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if offset, _ := anchor.Seek(0, io.SeekCurrent); offset != 1 {
|
||||||
|
t.Fatal("shared anchor offset changed", offset)
|
||||||
|
}
|
||||||
|
if err := dnsReadinessAction(parent, anchor, pid+1, false); err == nil {
|
||||||
|
t.Fatal("wrong PID accepted")
|
||||||
|
}
|
||||||
|
if err := dnsReadinessAction(parent, nil, pid, false); err == nil {
|
||||||
|
t.Fatal("missing file anchor accepted")
|
||||||
|
}
|
||||||
|
if err := dnsReadinessAction(parent, nil, pid, true); err == nil {
|
||||||
|
t.Fatal("unanchored entry removed")
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(path); err != nil {
|
||||||
|
t.Fatal("unanchored entry not preserved", err)
|
||||||
|
}
|
||||||
|
if err := dnsReadinessAction(parent, anchor, pid, true); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||||
|
t.Fatal("owned entry not removed", err)
|
||||||
|
}
|
||||||
|
if err := dnsReadinessAction(parent, nil, pid, true); err != nil {
|
||||||
|
t.Fatal("missing entry cleanup not idempotent", err)
|
||||||
|
}
|
||||||
|
if err := dnsReadinessAction(parent, anchor, pid, false); err == nil {
|
||||||
|
t.Fatal("unlinked entry reported ready")
|
||||||
|
}
|
||||||
|
childCleanup() // Graceful child cleanup after parent cleanup also remains safe.
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSReadinessForeignReplacement(t *testing.T) {
|
||||||
|
for _, kind := range []string{"same-PID-copy", "symlink", "directory", "FIFO"} {
|
||||||
|
t.Run(kind, func(t *testing.T) {
|
||||||
|
path, parent, anchor, childCleanup := dnsReadyAnchorsForTest(t)
|
||||||
|
os.Remove(path)
|
||||||
|
switch kind {
|
||||||
|
case "same-PID-copy":
|
||||||
|
if err := os.WriteFile(path, []byte(fmt.Sprintf("%d\n", os.Getpid())), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
case "symlink":
|
||||||
|
if err := os.Symlink("missing", path); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
case "directory":
|
||||||
|
if err := os.Mkdir(path, 0700); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
case "FIFO":
|
||||||
|
if err := syscall.Mkfifo(path, 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreign, err := os.Lstat(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, cleanup := range []bool{false, true} {
|
||||||
|
if err := dnsReadinessAction(parent, anchor, os.Getpid(), cleanup); err == nil {
|
||||||
|
t.Fatal("foreign entry accepted", cleanup)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
childCleanup()
|
||||||
|
current, err := os.Lstat(path)
|
||||||
|
if err != nil || !os.SameFile(foreign, current) {
|
||||||
|
t.Fatal("foreign entry deleted/replaced", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSReadinessRejectsUnsafeAnchors(t *testing.T) {
|
||||||
|
path, parent, anchor, _ := dnsReadyAnchorsForTest(t)
|
||||||
|
pid := os.Getpid()
|
||||||
|
if err := dnsReadinessAction(nil, anchor, pid, true); err == nil {
|
||||||
|
t.Fatal("missing directory anchor accepted")
|
||||||
|
}
|
||||||
|
if err := dnsReadinessAction(anchor, anchor, pid, true); err == nil {
|
||||||
|
t.Fatal("regular file used as directory anchor")
|
||||||
|
}
|
||||||
|
for _, body := range []string{"", strconv.Itoa(pid), strconv.Itoa(pid) + "\nextra", "secret-content\n", strings.Repeat("x", 1024)} {
|
||||||
|
if err := os.WriteFile(path, []byte(body), 0600); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := dnsReadinessAction(parent, anchor, pid, true); err == nil {
|
||||||
|
t.Fatal("malformed PID body accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
os.WriteFile(path, []byte(fmt.Sprintf("%d\n", pid)), 0600)
|
||||||
|
os.Chmod(path, 0644)
|
||||||
|
if err := dnsReadinessAction(parent, anchor, pid, true); err == nil {
|
||||||
|
t.Fatal("public file accepted")
|
||||||
|
}
|
||||||
|
os.Chmod(path, 0600)
|
||||||
|
os.Chmod(filepath.Dir(path), 0755)
|
||||||
|
if err := dnsReadinessAction(parent, anchor, pid, true); err == nil {
|
||||||
|
t.Fatal("public directory accepted")
|
||||||
|
}
|
||||||
|
os.Chmod(filepath.Dir(path), 0700)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subprocess helper exercises the actual inherited FD 8/9 CLI contract.
|
||||||
|
func TestDNSReadinessCLIHelper(t *testing.T) {
|
||||||
|
if os.Getenv("DNS_TEST_READINESS_CLI") != "1" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
for i, arg := range os.Args {
|
||||||
|
if arg == "--" {
|
||||||
|
os.Exit(run(os.Args[i+1:], os.Stdout, os.Stderr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
os.Exit(99)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsReadinessSubprocess(t *testing.T, parent, anchor *os.File, command, pid string) (int, string) {
|
||||||
|
t.Helper()
|
||||||
|
binary, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
cmd := exec.CommandContext(ctx, binary, "-test.run=^TestDNSReadinessCLIHelper$", "--", "dns-forwarder", command, pid)
|
||||||
|
cmd.Env = append(os.Environ(), "DNS_TEST_READINESS_CLI=1", "ZAPRET_AUTOTUNE_DNS_MODE=doh", "ZAPRET_AUTOTUNE_DOH_URL="+defaultDoHURL, "ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS=", "ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC=5")
|
||||||
|
padding, err := os.Open(os.DevNull)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer padding.Close()
|
||||||
|
cmd.ExtraFiles = []*os.File{padding, padding, padding, padding, padding, parent}
|
||||||
|
if anchor != nil {
|
||||||
|
cmd.ExtraFiles = append(cmd.ExtraFiles, anchor)
|
||||||
|
}
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
t.Fatal("readiness helper timed out")
|
||||||
|
}
|
||||||
|
if err == nil {
|
||||||
|
return 0, string(output)
|
||||||
|
}
|
||||||
|
if exit, ok := err.(*exec.ExitError); ok {
|
||||||
|
return exit.ExitCode(), string(output)
|
||||||
|
}
|
||||||
|
t.Fatal(err)
|
||||||
|
return -1, ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSReadinessInheritedCLI(t *testing.T) {
|
||||||
|
path, parent, anchor, _ := dnsReadyAnchorsForTest(t)
|
||||||
|
pid := strconv.Itoa(os.Getpid())
|
||||||
|
for _, bad := range []string{"0", "1", "-2", "+2", "02", "2\n", "2147483648", "secret-token"} {
|
||||||
|
code, out := dnsReadinessSubprocess(t, parent, anchor, "readiness-check", bad)
|
||||||
|
if code != 64 || out != "configerror: invalid DNS readiness command\n" {
|
||||||
|
t.Fatal(code, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if code, out := dnsReadinessSubprocess(t, parent, anchor, "readiness-check", pid); code != 0 || out != "" {
|
||||||
|
t.Fatal(code, out)
|
||||||
|
}
|
||||||
|
if code, out := dnsReadinessSubprocess(t, nil, anchor, "readiness-cleanup", pid); code != 78 || out != "error: DNS readiness ownership unavailable\n" {
|
||||||
|
t.Fatal("missing directory anchor accepted", code, out)
|
||||||
|
}
|
||||||
|
if offset, _ := anchor.Seek(0, io.SeekCurrent); offset != 0 {
|
||||||
|
t.Fatal("CLI changed parent's offset", offset)
|
||||||
|
}
|
||||||
|
if code, out := dnsReadinessSubprocess(t, parent, nil, "readiness-cleanup", pid); code != 78 || out != "error: DNS readiness ownership unavailable\n" {
|
||||||
|
t.Fatal(code, out)
|
||||||
|
}
|
||||||
|
if code, out := dnsReadinessSubprocess(t, parent, anchor, "readiness-cleanup", pid); code != 0 || out != "" {
|
||||||
|
t.Fatal(code, out)
|
||||||
|
}
|
||||||
|
if _, err := os.Stat(path); !os.IsNotExist(err) {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if code, out := dnsReadinessSubprocess(t, parent, nil, "readiness-cleanup", pid); code != 0 || out != "" {
|
||||||
|
t.Fatal(code, out)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSReadinessPublisherHelper(t *testing.T) {
|
||||||
|
dir := os.Getenv("DNS_TEST_READINESS_PUBLISH_DIR")
|
||||||
|
if dir == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cleanup, err := createDNSReady(filepath.Join(dir, "dns-ready"))
|
||||||
|
if err != nil {
|
||||||
|
os.Exit(99)
|
||||||
|
}
|
||||||
|
defer cleanup()
|
||||||
|
fmt.Fprintln(os.Stdout, "published")
|
||||||
|
time.Sleep(time.Hour)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSReadinessAnchorsSurviveSIGKILL(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
os.Chmod(dir, 0700)
|
||||||
|
binary, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
cmd := exec.CommandContext(ctx, binary, "-test.run=^TestDNSReadinessPublisherHelper$")
|
||||||
|
cmd.Env = append(os.Environ(), "DNS_TEST_READINESS_PUBLISH_DIR="+dir)
|
||||||
|
stdout, err := cmd.StdoutPipe()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
var diagnostic bytes.Buffer
|
||||||
|
cmd.Stderr = &diagnostic
|
||||||
|
if err := cmd.Start(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer cmd.Process.Kill()
|
||||||
|
if line, err := bufio.NewReader(stdout).ReadString('\n'); err != nil || line != "published\n" {
|
||||||
|
t.Fatal("publisher failed", line, err)
|
||||||
|
}
|
||||||
|
parent, err := os.Open(dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer parent.Close()
|
||||||
|
path := filepath.Join(dir, "dns-ready")
|
||||||
|
anchor, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer anchor.Close()
|
||||||
|
pid := cmd.Process.Pid
|
||||||
|
if err := dnsReadinessAction(parent, anchor, pid, false); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := cmd.Process.Kill(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
cmd.Wait()
|
||||||
|
if err := dnsReadinessAction(parent, anchor, pid, true); err != nil {
|
||||||
|
t.Fatal("anchored SIGKILL cleanup failed", err)
|
||||||
|
}
|
||||||
|
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
||||||
|
t.Fatal("SIGKILL left owned readiness entry", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,664 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"crypto/x509/pkix"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"math/big"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type dnsRoundTripFunc func(*http.Request) (*http.Response, error)
|
||||||
|
|
||||||
|
func (f dnsRoundTripFunc) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
|
||||||
|
|
||||||
|
func testDNSQuery() []byte {
|
||||||
|
return []byte{0x12, 0x34, 1, 0x20, 0, 1, 0, 0, 0, 0, 0, 0, 3, 'w', 'w', 'w', 7, 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0, 0, 1, 0, 1}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testDNSResponse(query []byte) []byte {
|
||||||
|
response := append([]byte(nil), query...)
|
||||||
|
response[2] |= 0x80
|
||||||
|
response[3] |= 0x80
|
||||||
|
return response
|
||||||
|
}
|
||||||
|
|
||||||
|
func testDNSForwarder(t *testing.T, roundTrip dnsRoundTripFunc) *dnsForwarder {
|
||||||
|
t.Helper()
|
||||||
|
return &dnsForwarder{cfg: dnsConfigForTest(t, nil), client: &http.Client{Transport: roundTrip}, slots: make(chan struct{}, dnsConcurrency)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsHTTPResponse(body []byte) *http.Response {
|
||||||
|
return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"application/dns-message"}}, Body: io.NopCloser(bytes.NewReader(body)), ContentLength: int64(len(body))}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSWireValidation(t *testing.T) {
|
||||||
|
query := testDNSQuery()
|
||||||
|
if !validDNSQuery(query) || !validDNSResponse(query, testDNSResponse(query)) {
|
||||||
|
t.Fatal("valid wire rejected")
|
||||||
|
}
|
||||||
|
edns := append([]byte(nil), query...)
|
||||||
|
edns[11] = 1
|
||||||
|
edns = append(edns, 0, 0, 41, 0x10, 0, 0, 0, 0x80, 0, 0, 0)
|
||||||
|
if !validDNSQuery(edns) {
|
||||||
|
t.Fatal("EDNS/DNSSEC query rejected")
|
||||||
|
}
|
||||||
|
failure := dnsServfail(edns)
|
||||||
|
if len(failure) != len(query) || !bytes.Equal(failure[:2], query[:2]) || failure[2] != 0x81 || failure[3] != 2 || !bytes.Equal(failure[12:], query[12:]) || !bytes.Equal(failure[6:12], make([]byte, 6)) {
|
||||||
|
t.Fatal(failure)
|
||||||
|
}
|
||||||
|
for _, mutate := range []func([]byte) []byte{
|
||||||
|
func(q []byte) []byte { return q[:11] },
|
||||||
|
func(q []byte) []byte { q[2] |= 0x80; return q },
|
||||||
|
func(q []byte) []byte { q[2] |= 0x08; return q },
|
||||||
|
func(q []byte) []byte { q[2] |= 0x04; return q },
|
||||||
|
func(q []byte) []byte { q[3] |= 0x40; return q },
|
||||||
|
func(q []byte) []byte { q[5] = 2; return q },
|
||||||
|
func(q []byte) []byte { q[7] = 1; return q },
|
||||||
|
func(q []byte) []byte { q[11] = 255; return q },
|
||||||
|
func(q []byte) []byte { q[12] = 64; return q },
|
||||||
|
func(q []byte) []byte { q[12] = 0xc0; q[13] = 12; return q },
|
||||||
|
func(q []byte) []byte { q[12] = 0xc0; q[13] = 255; return q },
|
||||||
|
func(q []byte) []byte { return q[:len(q)-1] },
|
||||||
|
func(q []byte) []byte { return append(q, 0) },
|
||||||
|
func(q []byte) []byte { return append(q, make([]byte, 4096)...) },
|
||||||
|
} {
|
||||||
|
bad := mutate(append([]byte(nil), query...))
|
||||||
|
if validDNSQuery(bad) || dnsServfail(bad) != nil {
|
||||||
|
t.Fatal("malformed query accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// A backwards compression reference is legal; a self-reference is not.
|
||||||
|
if end, ok := dnsNameEnd(append(query, 0xc0, 12), len(query)); !ok || end != len(query)+2 {
|
||||||
|
t.Fatal(end, ok)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSExchangePassThroughAndRejects(t *testing.T) {
|
||||||
|
query := testDNSQuery()
|
||||||
|
response := testDNSResponse(query)
|
||||||
|
// Opaque record bytes (including DNSSEC and TTL) must remain untouched.
|
||||||
|
response[7] = 1
|
||||||
|
response = append(response, 0xc0, 12, 0, 1, 0, 1, 0, 0, 0, 37, 0, 4, 1, 2, 3, 4)
|
||||||
|
f := testDNSForwarder(t, func(r *http.Request) (*http.Response, error) {
|
||||||
|
body, _ := io.ReadAll(r.Body)
|
||||||
|
if r.Method != "POST" || r.URL.String() != defaultDoHURL || r.Header.Get("Content-Type") != "application/dns-message" || r.Header.Get("Accept") != "application/dns-message" || !bytes.Equal(body, query) {
|
||||||
|
t.Error("incorrect DoH request")
|
||||||
|
}
|
||||||
|
return dnsHTTPResponse(response), nil
|
||||||
|
})
|
||||||
|
if actual := f.exchange(context.Background(), query); !bytes.Equal(actual, response) {
|
||||||
|
t.Fatal(actual)
|
||||||
|
}
|
||||||
|
for name, change := range map[string]func(*http.Response){
|
||||||
|
"status": func(r *http.Response) { r.StatusCode = 503 },
|
||||||
|
"type": func(r *http.Response) { r.Header.Set("Content-Type", "text/plain") },
|
||||||
|
"encoding": func(r *http.Response) { r.Header.Set("Content-Encoding", "gzip") },
|
||||||
|
"length": func(r *http.Response) { r.ContentLength++ },
|
||||||
|
"overflow": func(r *http.Response) {
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(make([]byte, 65536)))
|
||||||
|
r.ContentLength = 65536
|
||||||
|
},
|
||||||
|
"short": func(r *http.Response) { r.Body = io.NopCloser(bytes.NewReader(response[:11])); r.ContentLength = 11 },
|
||||||
|
"id": func(r *http.Response) {
|
||||||
|
b := append([]byte(nil), response...)
|
||||||
|
b[0]++
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
|
},
|
||||||
|
"qr": func(r *http.Response) {
|
||||||
|
b := append([]byte(nil), response...)
|
||||||
|
b[2] &= 0x7f
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
|
},
|
||||||
|
"opcode": func(r *http.Response) {
|
||||||
|
b := append([]byte(nil), response...)
|
||||||
|
b[2] |= 0x08
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
|
},
|
||||||
|
"question": func(r *http.Response) {
|
||||||
|
b := append([]byte(nil), response...)
|
||||||
|
b[13] = 'x'
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
|
},
|
||||||
|
"qdcount": func(r *http.Response) {
|
||||||
|
b := append([]byte(nil), response...)
|
||||||
|
b[5] = 0
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
|
},
|
||||||
|
"record-count": func(r *http.Response) {
|
||||||
|
b := append([]byte(nil), response...)
|
||||||
|
b[7] = 2
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
|
},
|
||||||
|
"record-length": func(r *http.Response) {
|
||||||
|
b := append([]byte(nil), response...)
|
||||||
|
b[len(query)+11] = 255
|
||||||
|
r.Body = io.NopCloser(bytes.NewReader(b))
|
||||||
|
},
|
||||||
|
"truncated": func(r *http.Response) { r.Body = io.NopCloser(dnsBrokenReader{}) },
|
||||||
|
} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
f := testDNSForwarder(t, func(*http.Request) (*http.Response, error) { r := dnsHTTPResponse(response); change(r); return r, nil })
|
||||||
|
if actual := f.exchange(context.Background(), query); !bytes.Equal(actual, dnsServfail(query)) {
|
||||||
|
t.Fatal(actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
f = testDNSForwarder(t, func(*http.Request) (*http.Response, error) { return nil, errors.New("secret upstream") })
|
||||||
|
if !bytes.Equal(f.exchange(context.Background(), query), dnsServfail(query)) {
|
||||||
|
t.Fatal("outage not SERVFAIL")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type dnsBrokenReader struct{}
|
||||||
|
|
||||||
|
func (dnsBrokenReader) Read([]byte) (int, error) { return 0, io.ErrUnexpectedEOF }
|
||||||
|
|
||||||
|
func TestDNSBootstrapAndHTTPPolicy(t *testing.T) {
|
||||||
|
cfg := dnsConfigForTest(t, nil)
|
||||||
|
var addresses []string
|
||||||
|
dial := dnsBootstrapDial(cfg, func(ctx context.Context, network, address string) (net.Conn, error) {
|
||||||
|
if network != "tcp" {
|
||||||
|
t.Fatal(network)
|
||||||
|
}
|
||||||
|
if _, ok := ctx.Deadline(); !ok {
|
||||||
|
t.Fatal("no bounded deadline")
|
||||||
|
}
|
||||||
|
addresses = append(addresses, address)
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
})
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
dial(context.Background(), "tcp", "dns.google:443")
|
||||||
|
}
|
||||||
|
if len(addresses) != 4 || addresses[0] != "8.8.8.8:443" || addresses[2] != "8.8.4.4:443" {
|
||||||
|
t.Fatal(addresses)
|
||||||
|
}
|
||||||
|
for _, addr := range []string{"other:443", "dns.google:80", "8.8.8.8:443"} {
|
||||||
|
dial(context.Background(), "tcp", addr)
|
||||||
|
}
|
||||||
|
if len(addresses) != 4 {
|
||||||
|
t.Fatal("unexpected endpoint dialed")
|
||||||
|
}
|
||||||
|
cfg.BootstrapIPs = []net.IP{net.ParseIP("2001:db8::1")}
|
||||||
|
dnsBootstrapDial(cfg, func(_ context.Context, _, addr string) (net.Conn, error) {
|
||||||
|
if addr != "[2001:db8::1]:443" {
|
||||||
|
t.Fatal(addr)
|
||||||
|
}
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
})(context.Background(), "tcp", "dns.google:443")
|
||||||
|
client, transport := newDNSHTTPClient(cfg, markDNSSocket)
|
||||||
|
defer transport.CloseIdleConnections()
|
||||||
|
if transport.Proxy != nil || transport.TLSClientConfig.InsecureSkipVerify || transport.TLSClientConfig.ServerName != "dns.google" || transport.TLSClientConfig.MinVersion != tls.VersionTLS12 || !transport.DisableCompression || transport.MaxConnsPerHost != 16 {
|
||||||
|
t.Fatal("unsafe transport policy")
|
||||||
|
}
|
||||||
|
if client.CheckRedirect(&http.Request{}, nil) == nil {
|
||||||
|
t.Fatal("redirect allowed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type dnsFakeRaw struct{ err error }
|
||||||
|
|
||||||
|
func (r dnsFakeRaw) Control(f func(uintptr)) error {
|
||||||
|
if r.err == nil {
|
||||||
|
f(42)
|
||||||
|
}
|
||||||
|
return r.err
|
||||||
|
}
|
||||||
|
func (dnsFakeRaw) Read(func(uintptr) bool) error { return nil }
|
||||||
|
func (dnsFakeRaw) Write(func(uintptr) bool) error { return nil }
|
||||||
|
|
||||||
|
func TestDNSSocketMarkStrict(t *testing.T) {
|
||||||
|
called := false
|
||||||
|
if err := setDNSSocketMark(dnsFakeRaw{}, func(fd, level, opt, mark int) error {
|
||||||
|
called = true
|
||||||
|
if fd != 42 || level != syscall.SOL_SOCKET || opt != syscall.SO_MARK || mark != 0x50000000 || mark&0x40000000 == 0 || mark&0x10000000 == 0 {
|
||||||
|
t.Fatal(fd, level, opt, mark)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}); err != nil || !called {
|
||||||
|
t.Fatal(err, called)
|
||||||
|
}
|
||||||
|
failure := errors.New("mark denied")
|
||||||
|
if err := setDNSSocketMark(dnsFakeRaw{}, func(int, int, int, int) error { return failure }); err != failure {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if err := setDNSSocketMark(dnsFakeRaw{err: failure}, func(int, int, int, int) error { t.Fatal("called on raw failure"); return nil }); err != failure {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSTCPFramesAndCancellation(t *testing.T) {
|
||||||
|
f := testDNSForwarder(t, func(r *http.Request) (*http.Response, error) {
|
||||||
|
q, _ := io.ReadAll(r.Body)
|
||||||
|
return dnsHTTPResponse(testDNSResponse(q)), nil
|
||||||
|
})
|
||||||
|
server, client := net.Pipe()
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() { defer close(done); f.tcpWorker(ctx, server) }()
|
||||||
|
defer client.Close()
|
||||||
|
client.SetDeadline(time.Now().Add(3 * time.Second))
|
||||||
|
q := testDNSQuery()
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
q[1] = byte(i)
|
||||||
|
var size [2]byte
|
||||||
|
binary.BigEndian.PutUint16(size[:], uint16(len(q)))
|
||||||
|
if err := writeDNSFrame(client, size[:], q); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := io.ReadFull(client, size[:]); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
b := make([]byte, int(binary.BigEndian.Uint16(size[:])))
|
||||||
|
if _, err := io.ReadFull(client, b); err != nil || !bytes.Equal(b, testDNSResponse(q)) {
|
||||||
|
t.Fatal(err, b)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("idle TCP worker did not join")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSTCPLimits(t *testing.T) {
|
||||||
|
for _, n := range []int{11, 4097, 65535} {
|
||||||
|
t.Run(fmt.Sprint(n), func(t *testing.T) {
|
||||||
|
f := testDNSForwarder(t, func(*http.Request) (*http.Response, error) {
|
||||||
|
t.Error("invalid frame forwarded")
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
})
|
||||||
|
server, client := net.Pipe()
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() { defer close(done); f.tcpWorker(context.Background(), server) }()
|
||||||
|
client.SetDeadline(time.Now().Add(time.Second))
|
||||||
|
var size [2]byte
|
||||||
|
binary.BigEndian.PutUint16(size[:], uint16(n))
|
||||||
|
client.Write(size[:])
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("invalid frame not closed")
|
||||||
|
}
|
||||||
|
client.Close()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
f := testDNSForwarder(t, func(r *http.Request) (*http.Response, error) {
|
||||||
|
q, _ := io.ReadAll(r.Body)
|
||||||
|
return dnsHTTPResponse(testDNSResponse(q)), nil
|
||||||
|
})
|
||||||
|
server, client := net.Pipe()
|
||||||
|
defer client.Close()
|
||||||
|
client.SetDeadline(time.Now().Add(3 * time.Second))
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() { defer close(done); f.tcpWorker(context.Background(), server) }()
|
||||||
|
q := testDNSQuery()
|
||||||
|
var size [2]byte
|
||||||
|
for i := 0; i < 16; i++ {
|
||||||
|
binary.BigEndian.PutUint16(size[:], uint16(len(q)))
|
||||||
|
if err := writeDNSFrame(client, size[:], q); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := io.ReadFull(client, size[:]); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := io.CopyN(io.Discard, client, int64(binary.BigEndian.Uint16(size[:]))); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("frame lifetime cap not enforced")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSRedirectNeverFollowed(t *testing.T) {
|
||||||
|
cfg := dnsConfigForTest(t, nil)
|
||||||
|
client, transport := newDNSHTTPClient(cfg, markDNSSocket)
|
||||||
|
defer transport.CloseIdleConnections()
|
||||||
|
var calls int
|
||||||
|
client.Transport = dnsRoundTripFunc(func(*http.Request) (*http.Response, error) {
|
||||||
|
calls++
|
||||||
|
return &http.Response{StatusCode: 302, Header: http.Header{"Location": {"https://secret.example/dns-query"}}, Body: io.NopCloser(bytes.NewReader(nil))}, nil
|
||||||
|
})
|
||||||
|
f := &dnsForwarder{cfg: cfg, client: client}
|
||||||
|
if !bytes.Equal(f.exchange(context.Background(), testDNSQuery()), dnsServfail(testDNSQuery())) || calls != 1 {
|
||||||
|
t.Fatal("redirect followed", calls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSReadinessFailureClosesListeners(t *testing.T) {
|
||||||
|
f := testDNSForwarder(t, func(*http.Request) (*http.Response, error) {
|
||||||
|
t.Error("request before ready")
|
||||||
|
return nil, errDNSForwarder
|
||||||
|
})
|
||||||
|
p := &dnsFakePacketConn{closed: make(chan struct{})}
|
||||||
|
l := &dnsIdleListener{closed: make(chan struct{})}
|
||||||
|
if err := f.run(context.Background(), p, l, func() (func(), error) { return nil, errDNSForwarder }); err == nil {
|
||||||
|
t.Fatal("readiness error ignored")
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-p.closed:
|
||||||
|
default:
|
||||||
|
t.Fatal("UDP not closed")
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case <-l.closed:
|
||||||
|
default:
|
||||||
|
t.Fatal("TCP not closed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSReadyOwnership(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
os.Chmod(dir, 0700)
|
||||||
|
path := filepath.Join(dir, "dns-ready")
|
||||||
|
cleanup, err := createDNSReady(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
info, _ := os.Stat(path)
|
||||||
|
body, _ := os.ReadFile(path)
|
||||||
|
if info.Mode().Perm() != 0600 || string(body) != fmt.Sprintf("%d\n", os.Getpid()) {
|
||||||
|
t.Fatal(info, string(body))
|
||||||
|
}
|
||||||
|
if _, err := createDNSReady(path); err == nil {
|
||||||
|
t.Fatal("existing readiness overwritten")
|
||||||
|
}
|
||||||
|
if entries, err := os.ReadDir(dir); err != nil || len(entries) != 1 || entries[0].Name() != "dns-ready" {
|
||||||
|
t.Fatal("readiness publication/failure left a temporary link", entries, err)
|
||||||
|
}
|
||||||
|
// The forwarder itself must hold both descriptors, not the test, so an
|
||||||
|
// unlinked readiness inode cannot be recycled for a foreign replacement.
|
||||||
|
fileFD := dnsHeldFD(t, info)
|
||||||
|
parentInfo, _ := os.Stat(dir)
|
||||||
|
parentFD := dnsHeldFD(t, parentInfo)
|
||||||
|
os.Remove(path)
|
||||||
|
var held syscall.Stat_t
|
||||||
|
if err := syscall.Fstat(fileFD, &held); err != nil || held.Nlink != 0 {
|
||||||
|
t.Fatal("readiness inode not held after unlink", err, held.Nlink)
|
||||||
|
}
|
||||||
|
os.WriteFile(path, []byte("replacement"), 0600)
|
||||||
|
cleanup()
|
||||||
|
cleanup() // Idempotent even after its descriptors have been closed.
|
||||||
|
for _, fd := range []int{fileFD, parentFD} {
|
||||||
|
if err := syscall.Fstat(fd, &held); err != syscall.EBADF {
|
||||||
|
t.Fatal("cleanup leaked descriptor", fd, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if b, err := os.ReadFile(path); err != nil || string(b) != "replacement" {
|
||||||
|
t.Fatal("foreign readiness removed", err)
|
||||||
|
}
|
||||||
|
os.Remove(path)
|
||||||
|
cleanup, err = createDNSReady(path)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
cleanup()
|
||||||
|
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
os.Symlink("missing", path)
|
||||||
|
if _, err := createDNSReady(path); err == nil {
|
||||||
|
t.Fatal("symlink accepted")
|
||||||
|
}
|
||||||
|
os.Remove(path)
|
||||||
|
os.Chmod(dir, 0755)
|
||||||
|
if _, err := createDNSReady(path); err == nil {
|
||||||
|
t.Fatal("public parent accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsHeldFD(t *testing.T, info os.FileInfo) int {
|
||||||
|
t.Helper()
|
||||||
|
entries, err := os.ReadDir("/proc/self/fd")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
want := info.Sys().(*syscall.Stat_t)
|
||||||
|
for _, entry := range entries {
|
||||||
|
fd, err := strconv.Atoi(entry.Name())
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var stat syscall.Stat_t
|
||||||
|
if syscall.Fstat(fd, &stat) == nil && stat.Ino == want.Ino && stat.Dev == want.Dev {
|
||||||
|
return fd
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatal("owned inode has no held descriptor")
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSReadyAtomicPublication(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
file, err := os.CreateTemp(dir, ".dns-ready-")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
path := filepath.Join(dir, "dns-ready")
|
||||||
|
if _, err := os.Lstat(path); !os.IsNotExist(err) {
|
||||||
|
t.Fatal("readiness published before PID", err)
|
||||||
|
}
|
||||||
|
if err := publishDNSReady(file, path); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if body, err := os.ReadFile(path); err != nil || string(body) != fmt.Sprintf("%d\n", os.Getpid()) {
|
||||||
|
t.Fatal("partial PID published", string(body), err)
|
||||||
|
}
|
||||||
|
other, err := os.CreateTemp(dir, "foreign-")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
defer other.Close()
|
||||||
|
if err := publishDNSReady(other, path); !os.IsExist(err) {
|
||||||
|
t.Fatal("publication overwrote existing inode", err)
|
||||||
|
}
|
||||||
|
if body, _ := os.ReadFile(path); string(body) != fmt.Sprintf("%d\n", os.Getpid()) {
|
||||||
|
t.Fatal("original PID changed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify actual certificate validation over net.Pipe, never a TCP network listener.
|
||||||
|
func TestDNSVerifiedTLSOverPipe(t *testing.T) {
|
||||||
|
key, err := rsa.GenerateKey(rand.Reader, 2048)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
template := &x509.Certificate{SerialNumber: big.NewInt(1), Subject: pkix.Name{CommonName: "dns.google"}, DNSNames: []string{"dns.google"}, NotBefore: time.Now().Add(-time.Hour), NotAfter: time.Now().Add(time.Hour), KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, IsCA: true, BasicConstraintsValid: true}
|
||||||
|
der, err := x509.CreateCertificate(rand.Reader, template, template, &key.PublicKey, key)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
cert, err := x509.ParseCertificate(der)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
for _, name := range []string{"trusted", "untrusted", "wrong-name"} {
|
||||||
|
t.Run(name, func(t *testing.T) {
|
||||||
|
cfg := dnsConfigForTest(t, nil)
|
||||||
|
client, transport := newDNSHTTPClient(cfg, markDNSSocket)
|
||||||
|
defer transport.CloseIdleConnections()
|
||||||
|
if name != "untrusted" {
|
||||||
|
roots := x509.NewCertPool()
|
||||||
|
roots.AddCert(cert)
|
||||||
|
transport.TLSClientConfig.RootCAs = roots
|
||||||
|
}
|
||||||
|
if name == "wrong-name" {
|
||||||
|
transport.TLSClientConfig.ServerName = "wrong.example"
|
||||||
|
}
|
||||||
|
var servers sync.WaitGroup
|
||||||
|
transport.DialContext = dnsBootstrapDial(cfg, func(ctx context.Context, _, addr string) (net.Conn, error) {
|
||||||
|
if addr != "8.8.8.8:443" && addr != "8.8.4.4:443" {
|
||||||
|
t.Error(addr)
|
||||||
|
}
|
||||||
|
a, b := net.Pipe()
|
||||||
|
servers.Add(1)
|
||||||
|
go func() {
|
||||||
|
defer servers.Done()
|
||||||
|
defer b.Close()
|
||||||
|
b.SetDeadline(time.Now().Add(3 * time.Second))
|
||||||
|
tlsConn := tls.Server(b, &tls.Config{Certificates: []tls.Certificate{{Certificate: [][]byte{der}, PrivateKey: key}}, MinVersion: tls.VersionTLS12})
|
||||||
|
if err := tlsConn.Handshake(); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if tlsConn.ConnectionState().ServerName != "dns.google" {
|
||||||
|
t.Error("SNI changed")
|
||||||
|
}
|
||||||
|
request, err := http.ReadRequest(bufio.NewReader(tlsConn))
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
q, _ := io.ReadAll(request.Body)
|
||||||
|
request.Body.Close()
|
||||||
|
response := dnsHTTPResponse(testDNSResponse(q))
|
||||||
|
response.Close = true
|
||||||
|
response.Write(tlsConn)
|
||||||
|
}()
|
||||||
|
return a, nil
|
||||||
|
})
|
||||||
|
f := &dnsForwarder{cfg: cfg, client: client}
|
||||||
|
actual := f.exchange(context.Background(), testDNSQuery())
|
||||||
|
want := dnsServfail(testDNSQuery())
|
||||||
|
if name == "trusted" {
|
||||||
|
want = testDNSResponse(testDNSQuery())
|
||||||
|
}
|
||||||
|
if !bytes.Equal(actual, want) {
|
||||||
|
t.Fatal(actual)
|
||||||
|
}
|
||||||
|
transport.CloseIdleConnections()
|
||||||
|
servers.Wait()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type dnsTestPacket struct {
|
||||||
|
data []byte
|
||||||
|
addr net.Addr
|
||||||
|
}
|
||||||
|
type dnsFakePacketConn struct {
|
||||||
|
incoming chan dnsTestPacket
|
||||||
|
outgoing chan dnsTestPacket
|
||||||
|
closed chan struct{}
|
||||||
|
once sync.Once
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *dnsFakePacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
||||||
|
select {
|
||||||
|
case q := <-p.incoming:
|
||||||
|
return copy(b, q.data), q.addr, nil
|
||||||
|
case <-p.closed:
|
||||||
|
return 0, nil, net.ErrClosed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (p *dnsFakePacketConn) WriteTo(b []byte, a net.Addr) (int, error) {
|
||||||
|
select {
|
||||||
|
case p.outgoing <- dnsTestPacket{append([]byte(nil), b...), a}:
|
||||||
|
return len(b), nil
|
||||||
|
case <-p.closed:
|
||||||
|
return 0, net.ErrClosed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func (p *dnsFakePacketConn) Close() error { p.once.Do(func() { close(p.closed) }); return nil }
|
||||||
|
func (p *dnsFakePacketConn) LocalAddr() net.Addr { return &net.UDPAddr{} }
|
||||||
|
func (p *dnsFakePacketConn) SetDeadline(time.Time) error { return nil }
|
||||||
|
func (p *dnsFakePacketConn) SetReadDeadline(time.Time) error { return nil }
|
||||||
|
func (p *dnsFakePacketConn) SetWriteDeadline(time.Time) error { return nil }
|
||||||
|
|
||||||
|
type dnsIdleListener struct {
|
||||||
|
closed chan struct{}
|
||||||
|
once sync.Once
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *dnsIdleListener) Accept() (net.Conn, error) { <-l.closed; return nil, net.ErrClosed }
|
||||||
|
func (l *dnsIdleListener) Close() error { l.once.Do(func() { close(l.closed) }); return nil }
|
||||||
|
func (l *dnsIdleListener) Addr() net.Addr { return &net.TCPAddr{} }
|
||||||
|
|
||||||
|
func TestDNSUDPBoundedCopiesAndJoins(t *testing.T) {
|
||||||
|
var active, maximum atomic.Int32
|
||||||
|
started := make(chan struct{}, dnsConcurrency)
|
||||||
|
f := testDNSForwarder(t, func(r *http.Request) (*http.Response, error) {
|
||||||
|
n := active.Add(1)
|
||||||
|
defer active.Add(-1)
|
||||||
|
for old := maximum.Load(); n > old; old = maximum.Load() {
|
||||||
|
if maximum.CompareAndSwap(old, n) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q, _ := io.ReadAll(r.Body)
|
||||||
|
started <- struct{}{}
|
||||||
|
<-r.Context().Done()
|
||||||
|
if q[1] >= dnsConcurrency {
|
||||||
|
t.Error("UDP buffer was reused")
|
||||||
|
}
|
||||||
|
return nil, r.Context().Err()
|
||||||
|
})
|
||||||
|
p := &dnsFakePacketConn{incoming: make(chan dnsTestPacket, 32), outgoing: make(chan dnsTestPacket, 32), closed: make(chan struct{})}
|
||||||
|
l := &dnsIdleListener{closed: make(chan struct{})}
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
ready := make(chan struct{})
|
||||||
|
cleaned := make(chan struct{})
|
||||||
|
done := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
done <- f.run(ctx, p, l, func() (func(), error) { close(ready); return func() { close(cleaned) }, nil })
|
||||||
|
}()
|
||||||
|
<-ready
|
||||||
|
for i := 0; i < dnsConcurrency; i++ {
|
||||||
|
q := testDNSQuery()
|
||||||
|
q[1] = byte(i)
|
||||||
|
p.incoming <- dnsTestPacket{q, &net.UDPAddr{}}
|
||||||
|
}
|
||||||
|
for i := 0; i < dnsConcurrency; i++ {
|
||||||
|
select {
|
||||||
|
case <-started:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("worker not started")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q := testDNSQuery()
|
||||||
|
q[1] = 99
|
||||||
|
p.incoming <- dnsTestPacket{q, &net.UDPAddr{}}
|
||||||
|
select {
|
||||||
|
case response := <-p.outgoing:
|
||||||
|
if !bytes.Equal(response.data, dnsServfail(q)) {
|
||||||
|
t.Fatal(response)
|
||||||
|
}
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("saturation not SERVFAIL")
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
select {
|
||||||
|
case err := <-done:
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("workers not joined")
|
||||||
|
}
|
||||||
|
<-cleaned
|
||||||
|
if maximum.Load() != dnsConcurrency || active.Load() != 0 || len(f.slots) != 0 {
|
||||||
|
t.Fatal(maximum.Load(), active.Load(), len(f.slots))
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,169 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dnsTestEDNSQuery(payload uint16) []byte {
|
||||||
|
q := testDNSQuery()
|
||||||
|
q[11] = 1
|
||||||
|
return append(q, 0, 0, 41, byte(payload>>8), byte(payload), 0, 0, 0x80, 0, 0, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dnsTestSizedResponse(query []byte, size int) []byte {
|
||||||
|
response := dnsQuestionReply(query, query)
|
||||||
|
response[2] = 0x81
|
||||||
|
response[3] = 0xa0 // RA and AD survive truncation.
|
||||||
|
response[7] = 1
|
||||||
|
dataSize := size - len(response) - 12
|
||||||
|
response = append(response, 0xc0, 12, 0, 16, 0, 1, 0, 0, 0, 37, byte(dataSize>>8), byte(dataSize))
|
||||||
|
return append(response, make([]byte, dataSize)...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSUDPPayloadLimits(t *testing.T) {
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
query []byte
|
||||||
|
size, limit int
|
||||||
|
truncated bool
|
||||||
|
}{
|
||||||
|
{"no-EDNS-full-TCP", testDNSQuery(), 65535, 512, true},
|
||||||
|
{"no-EDNS-at-limit", testDNSQuery(), 512, 512, false},
|
||||||
|
{"EDNS-at-limit", dnsTestEDNSQuery(1232), 1232, 1232, false},
|
||||||
|
{"EDNS-over-limit", dnsTestEDNSQuery(1232), 1233, 1232, true},
|
||||||
|
{"EDNS-small-clamp", dnsTestEDNSQuery(1), 513, 512, true},
|
||||||
|
{"EDNS-max-at-IPv4-limit", dnsTestEDNSQuery(65535), 65507, 65507, false},
|
||||||
|
{"EDNS-max-over-IPv4-limit", dnsTestEDNSQuery(65535), 65535, 65507, true},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
response := dnsTestSizedResponse(tc.query, tc.size)
|
||||||
|
if !validDNSQuery(tc.query) || !validDNSResponse(tc.query, response) {
|
||||||
|
t.Fatal("invalid fixture")
|
||||||
|
}
|
||||||
|
actual := dnsUDPReply(tc.query, response)
|
||||||
|
if len(actual) > tc.limit || (actual[2]&2 != 0) != tc.truncated {
|
||||||
|
t.Fatal("incorrect UDP ceiling/TC", len(actual), actual[:12])
|
||||||
|
}
|
||||||
|
if !tc.truncated {
|
||||||
|
if !bytes.Equal(actual, response) {
|
||||||
|
t.Fatal("in-range answer changed")
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !validDNSResponse(tc.query, actual) || !bytes.Equal(actual[:2], response[:2]) || actual[2] != response[2]|2 || actual[3] != response[3] || !bytes.Equal(actual[6:12], make([]byte, 6)) {
|
||||||
|
t.Fatal("invalid truncated reply", actual)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSOversizedUDPThenFullTCP(t *testing.T) {
|
||||||
|
q := testDNSQuery()
|
||||||
|
response := dnsTestSizedResponse(q, 65535)
|
||||||
|
f := testDNSForwarder(t, func(*http.Request) (*http.Response, error) { return dnsHTTPResponse(response), nil })
|
||||||
|
p := &dnsFakePacketConn{outgoing: make(chan dnsTestPacket, 1), closed: make(chan struct{})}
|
||||||
|
writeDNSDatagram(p, &net.UDPAddr{}, q, f.exchange(context.Background(), q))
|
||||||
|
if packet := <-p.outgoing; len(packet.data) > 512 || packet.data[2]&2 == 0 {
|
||||||
|
t.Fatal("oversized UDP did not force TCP")
|
||||||
|
}
|
||||||
|
server, client := net.Pipe()
|
||||||
|
defer client.Close()
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
done := make(chan struct{})
|
||||||
|
go func() { defer close(done); f.tcpWorker(ctx, server) }()
|
||||||
|
client.SetDeadline(time.Now().Add(3 * time.Second))
|
||||||
|
var size [2]byte
|
||||||
|
binary.BigEndian.PutUint16(size[:], uint16(len(q)))
|
||||||
|
if err := writeDNSFrame(client, size[:], q); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := io.ReadFull(client, size[:]); err != nil || binary.BigEndian.Uint16(size[:]) != 65535 {
|
||||||
|
t.Fatal("incorrect full TCP frame", size, err)
|
||||||
|
}
|
||||||
|
actual := make([]byte, 65535)
|
||||||
|
if _, err := io.ReadFull(client, actual); err != nil || !bytes.Equal(actual, response) {
|
||||||
|
t.Fatal("full TCP reply changed", err)
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
case <-time.After(time.Second):
|
||||||
|
t.Fatal("TCP worker did not join")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSCompressedQuestionReplies(t *testing.T) {
|
||||||
|
plain := testDNSQuery()
|
||||||
|
// Question points forward into an additional RR's owner; copying just the
|
||||||
|
// original question would leave a dangling pointer after dropping that RR.
|
||||||
|
q := append([]byte(nil), plain[:12]...)
|
||||||
|
q[11] = 1
|
||||||
|
q = append(q, 0xc0, 18, 0, 1, 0, 1)
|
||||||
|
q = append(q, plain[12:len(plain)-4]...)
|
||||||
|
q = append(q, 0, 1, 0, 1, 0, 0, 0, 37, 0, 4, 1, 2, 3, 4)
|
||||||
|
if !validDNSQuery(q) {
|
||||||
|
t.Fatal("compressed query rejected")
|
||||||
|
}
|
||||||
|
for _, reply := range [][]byte{dnsServfail(q), dnsUDPReply(q, dnsTestSizedResponse(q, 65535))} {
|
||||||
|
if !validDNSResponse(q, reply) || !bytes.Equal(reply[12:], plain[12:]) || reply[12]&0xc0 != 0 || !bytes.Equal(reply[6:12], make([]byte, 6)) {
|
||||||
|
t.Fatal("dangling/compressed synthesized question", reply)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
response := dnsTestSizedResponse(q, 65535)
|
||||||
|
f := testDNSForwarder(t, func(*http.Request) (*http.Response, error) { return dnsHTTPResponse(response), nil })
|
||||||
|
if !bytes.Equal(f.exchange(context.Background(), q), response) {
|
||||||
|
t.Fatal("equivalent uncompressed upstream question rejected")
|
||||||
|
}
|
||||||
|
q[18] = 0xc0
|
||||||
|
q[19] = 12 // Question -> owner -> question compression loop.
|
||||||
|
if validDNSQuery(q) || dnsServfail(q) != nil {
|
||||||
|
t.Fatal("compression loop accepted")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSMalformedEDNSQueries(t *testing.T) {
|
||||||
|
q := dnsTestEDNSQuery(1232)
|
||||||
|
duplicate := append(append([]byte(nil), q...), q[len(testDNSQuery()):]...)
|
||||||
|
duplicate[11] = 2
|
||||||
|
badOwner := append([]byte(nil), q...)
|
||||||
|
start := len(testDNSQuery())
|
||||||
|
badOwner = append(badOwner[:start], append([]byte{1, 'x'}, badOwner[start:]...)...)
|
||||||
|
badOptions := append([]byte(nil), q...)
|
||||||
|
badOptions[len(badOptions)-1] = 3
|
||||||
|
badOptions = append(badOptions, 0, 1, 0)
|
||||||
|
for i, bad := range [][]byte{duplicate, badOwner, badOptions, q[:len(q)-1]} {
|
||||||
|
if validDNSQuery(bad) {
|
||||||
|
t.Fatal("malformed EDNS accepted", i)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type dnsFailingPacketConn struct {
|
||||||
|
*dnsFakePacketConn
|
||||||
|
writes int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *dnsFailingPacketConn) WriteTo([]byte, net.Addr) (int, error) {
|
||||||
|
p.writes++
|
||||||
|
return 0, errors.New("datagram dropped")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestDNSUDPWriteErrorDropsOnlyDatagram(t *testing.T) {
|
||||||
|
p := &dnsFailingPacketConn{}
|
||||||
|
q := testDNSQuery()
|
||||||
|
for i := 0; i < 2; i++ {
|
||||||
|
writeDNSDatagram(p, &net.UDPAddr{}, q, dnsTestSizedResponse(q, 65535))
|
||||||
|
}
|
||||||
|
if p.writes != 2 {
|
||||||
|
t.Fatal("write error stopped subsequent datagrams", p.writes)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,13 +3,16 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
const usage = `Usage: zapret-autotune <command>
|
const usage = `Usage: zapret-autotune <command>
|
||||||
|
|
||||||
Commands:
|
Commands:
|
||||||
|
dns-forwarder mode|address|serve
|
||||||
|
Managed DNS configuration or loopback DoH forwarder
|
||||||
|
dns-forwarder readiness-check|readiness-cleanup <expected-dns-pid>
|
||||||
|
Validate or clean readiness using inherited PID 1 anchors
|
||||||
status Show latest recorded tune run/proposal (not live service health)
|
status Show latest recorded tune run/proposal (not live service health)
|
||||||
history [--limit N] List recorded tune attempts (default 20; proposals not active)
|
history [--limit N] List recorded tune attempts (default 20; proposals not active)
|
||||||
strategies youtube List recorded proposals by group (not active)
|
strategies youtube List recorded proposals by group (not active)
|
||||||
@@ -36,13 +39,16 @@ func run(args []string, stdout, stderr io.Writer) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner) int {
|
func runWithPlayer(args []string, stdout, stderr io.Writer, player playerRunner) int {
|
||||||
|
if len(args) > 0 && args[0] == "dns-forwarder" {
|
||||||
|
return dnsForwarderCLI(args[1:], stdout, stderr)
|
||||||
|
}
|
||||||
if len(args) == 1 && args[0] == "target-config-check" {
|
if len(args) == 1 && args[0] == "target-config-check" {
|
||||||
if _, err := parseManagedHealthConfig(os.LookupEnv); err != nil {
|
if _, err := parseManagedHealthConfig(os.LookupEnv); err != nil {
|
||||||
fmt.Fprintln(stderr, "configerror: invalid target health/service configuration")
|
fmt.Fprintln(stderr, "configerror: invalid target health/service configuration")
|
||||||
return 64
|
return 64
|
||||||
}
|
}
|
||||||
if dns := os.Getenv("SS_DNS_SERVER"); dns != "" && net.ParseIP(dns) == nil {
|
if _, err := parseManagedDNSConfig(os.LookupEnv); err != nil {
|
||||||
fmt.Fprintln(stderr, "configerror: SS_DNS_SERVER requires a literal IPv4 or IPv6 address")
|
fmt.Fprintln(stderr, "configerror: invalid managed DNS configuration")
|
||||||
return 64
|
return 64
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -34,6 +34,9 @@ func TestManagedPrestartHelper(t *testing.T) {
|
|||||||
// Use the production validator, not a fixture-only success response.
|
// Use the production validator, not a fixture-only success response.
|
||||||
os.Exit(run(args, os.Stdout, os.Stderr))
|
os.Exit(run(args, os.Stdout, os.Stderr))
|
||||||
}
|
}
|
||||||
|
if len(args) == 2 && args[0] == "dns-forwarder" && (args[1] == "mode" || args[1] == "address") {
|
||||||
|
os.Exit(run(args, os.Stdout, os.Stderr))
|
||||||
|
}
|
||||||
if len(args) < 2 || args[0] != "managed" {
|
if len(args) < 2 || args[0] != "managed" {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -165,7 +168,8 @@ func TestManagedEntrypointPrestartReconciliation(t *testing.T) {
|
|||||||
// use the fixture config, via its own script rather than CLI path flags.
|
// use the fixture config, via its own script rather than CLI path flags.
|
||||||
cmd.Env = append(cmd.Env, "FIXTURE_CONFIG="+filepath.Join(d.directory, "config"))
|
cmd.Env = append(cmd.Env, "FIXTURE_CONFIG="+filepath.Join(d.directory, "config"))
|
||||||
childStarted := filepath.Join(root, "child-started")
|
childStarted := filepath.Join(root, "child-started")
|
||||||
cmd.Env = append(cmd.Env, "FIXTURE_CHILD_STARTED="+childStarted, "SS_DNS_SERVER=8.8.8.8")
|
// This legacy daemon fixture deliberately exercises explicit native DNS.
|
||||||
|
cmd.Env = append(cmd.Env, "FIXTURE_CHILD_STARTED="+childStarted, "ZAPRET_AUTOTUNE_DNS_MODE=native", "SS_DNS_SERVER=8.8.8.8")
|
||||||
wantExit := 78
|
wantExit := 78
|
||||||
if mode == "invalid-dns" {
|
if mode == "invalid-dns" {
|
||||||
cmd.Env = append(cmd.Env, "SS_DNS_SERVER=8.8.8.8\n--invalid")
|
cmd.Env = append(cmd.Env, "SS_DNS_SERVER=8.8.8.8\n--invalid")
|
||||||
@@ -672,6 +676,14 @@ func TestManagedEntrypointSupervision(t *testing.T) {
|
|||||||
write("ss-local", child)
|
write("ss-local", child)
|
||||||
zapret := write("zapret", "#!/bin/sh\necho zapret-$1 >> \"$LOG\"\nstage=$1\n"+startupFailure+"if [ \"$MODE:$1\" = ss-early:start ] && [ \"$(grep -c zapret-start \"$LOG\")\" -gt 1 ]; then\nuntil [ \"$(grep -c 'stopped-.*/ss-local' \"$LOG\")\" -ge \"$(grep -c zapret-start \"$LOG\")\" ]; do sleep 0.02; done\nsleep 0.02\nfi\nif [ \"$MODE:$1\" = stop-failure:stop ]; then exit 1; fi\nif [ \"$MODE:$1\" = stop-hang:stop ]; then trap '' TERM INT; exec sleep 300; fi\n")
|
zapret := write("zapret", "#!/bin/sh\necho zapret-$1 >> \"$LOG\"\nstage=$1\n"+startupFailure+"if [ \"$MODE:$1\" = ss-early:start ] && [ \"$(grep -c zapret-start \"$LOG\")\" -gt 1 ]; then\nuntil [ \"$(grep -c 'stopped-.*/ss-local' \"$LOG\")\" -ge \"$(grep -c zapret-start \"$LOG\")\" ]; do sleep 0.02; done\nsleep 0.02\nfi\nif [ \"$MODE:$1\" = stop-failure:stop ]; then exit 1; fi\nif [ \"$MODE:$1\" = stop-hang:stop ]; then trap '' TERM INT; exec sleep 300; fi\n")
|
||||||
monitor := write("autotune", "#!/bin/sh\nif [ \"$2\" != monitor ]; then\necho managed-$2 >> \"$LOG\"\nstage=$2\n"+startupFailure+"exit 0\nfi\nsleeper=\ntrap '"+joinSleeper+"echo monitor-stopped >> \"$LOG\"; case \"$MODE\" in *unsafe) exit 78;; *unknown) exit 7;; ss-technical) exit 75;; esac; exit 0' TERM INT\necho managed-monitor >> \"$LOG\"\nif [ \"$MODE\" = term-hang ]; then trap '' TERM INT; exec sleep 300; fi\ncase \"$MODE\" in fatal) exit 78;; technical|stop-failure|stop-hang) exit 75;; panic) exit 2;; killed) kill -KILL $$;; segv) exit 139;; zero) exit 0;; unknown) exit 7;; esac\nwhile :; do sleep 0.1 & sleeper=$!; wait \"$sleeper\"; done\n")
|
monitor := write("autotune", "#!/bin/sh\nif [ \"$2\" != monitor ]; then\necho managed-$2 >> \"$LOG\"\nstage=$2\n"+startupFailure+"exit 0\nfi\nsleeper=\ntrap '"+joinSleeper+"echo monitor-stopped >> \"$LOG\"; case \"$MODE\" in *unsafe) exit 78;; *unknown) exit 7;; ss-technical) exit 75;; esac; exit 0' TERM INT\necho managed-monitor >> \"$LOG\"\nif [ \"$MODE\" = term-hang ]; then trap '' TERM INT; exec sleep 300; fi\ncase \"$MODE\" in fatal) exit 78;; technical|stop-failure|stop-hang) exit 75;; panic) exit 2;; killed) kill -KILL $$;; segv) exit 139;; zero) exit 0;; unknown) exit 7;; esac\nwhile :; do sleep 0.1 & sleeper=$!; wait \"$sleeper\"; done\n")
|
||||||
|
// This legacy lifecycle fixture has no DNS service. Implement only
|
||||||
|
// the explicit native getters; dedicated Python fixtures own DoH ACKs.
|
||||||
|
monitorData, err := os.ReadFile(monitor)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
getters := "if [ \"$1\" = dns-forwarder ]; then\ncase \"$2\" in mode) echo native;; address) echo;; *) exit 64;; esac\nexit 0\nfi\n"
|
||||||
|
write("autotune", strings.Replace(string(monitorData), "#!/bin/sh\n", "#!/bin/sh\n"+getters, 1))
|
||||||
data, err := os.ReadFile("../../managed-target-entrypoint.sh")
|
data, err := os.ReadFile("../../managed-target-entrypoint.sh")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@@ -696,7 +708,7 @@ func TestManagedEntrypointSupervision(t *testing.T) {
|
|||||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
cmd := exec.CommandContext(ctx, "/bin/sh", entry)
|
cmd := exec.CommandContext(ctx, "/bin/sh", entry)
|
||||||
cmd.Env = append(os.Environ(), "PATH="+root+":"+os.Getenv("PATH"), "LOG="+log, "MODE="+mode, "ZAPRET_CONFIG="+filepath.Join(root, "config"), "SS_PASSWORD="+strings.Repeat("a", 32))
|
cmd.Env = append(os.Environ(), "PATH="+root+":"+os.Getenv("PATH"), "LOG="+log, "MODE="+mode, "ZAPRET_CONFIG="+filepath.Join(root, "config"), "SS_PASSWORD="+strings.Repeat("a", 32), "ZAPRET_AUTOTUNE_DNS_MODE=native")
|
||||||
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
|
||||||
var shutdownSnapshot string
|
var shutdownSnapshot string
|
||||||
stopMonitorSession := func() {
|
stopMonitorSession := func() {
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestTargetConfigCheck(t *testing.T) {
|
func TestTargetConfigCheck(t *testing.T) {
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DNS_MODE", "native")
|
||||||
t.Setenv("ZAPRET_AUTOTUNE_YOUTUBE_SERVICE", "0")
|
t.Setenv("ZAPRET_AUTOTUNE_YOUTUBE_SERVICE", "0")
|
||||||
t.Setenv("ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE", "cold")
|
t.Setenv("ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE", "cold")
|
||||||
t.Setenv("ZAPRET_AUTOTUNE_VIDEO_ID", "dQw4w9WgXcQ")
|
t.Setenv("ZAPRET_AUTOTUNE_VIDEO_ID", "dQw4w9WgXcQ")
|
||||||
@@ -23,6 +24,21 @@ func TestTargetConfigCheck(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
t.Run("DoH ignores deprecated native DNS", func(t *testing.T) {
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DNS_MODE", "doh")
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DOH_URL", defaultDoHURL)
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS", "8.8.8.8")
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC", "5")
|
||||||
|
t.Setenv("SS_DNS_SERVER", "ignored invalid legacy setting")
|
||||||
|
var out, errOut bytes.Buffer
|
||||||
|
if code := run([]string{"target-config-check"}, &out, &errOut); code != 0 || out.Len() != 0 || errOut.Len() != 0 {
|
||||||
|
t.Fatal(code, out.String(), errOut.String())
|
||||||
|
}
|
||||||
|
t.Setenv("ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS", "secret-bootstrap-host")
|
||||||
|
if code := run([]string{"target-config-check"}, &out, &errOut); code != 64 || strings.Contains(errOut.String(), "secret-bootstrap-host") {
|
||||||
|
t.Fatal(code, errOut.String())
|
||||||
|
}
|
||||||
|
})
|
||||||
t.Setenv("SS_DNS_SERVER", "")
|
t.Setenv("SS_DNS_SERVER", "")
|
||||||
for key, value := range map[string]string{
|
for key, value := range map[string]string{
|
||||||
"ZAPRET_AUTOTUNE_YOUTUBE_SERVICE": "true",
|
"ZAPRET_AUTOTUNE_YOUTUBE_SERVICE": "true",
|
||||||
|
|||||||
@@ -6,5 +6,12 @@ ZAPRET_AUTOTUNE_VIDEO_ID=dQw4w9WgXcQ
|
|||||||
# Missing/empty selects automatic YouTube inventory: primary + two public samples.
|
# Missing/empty selects automatic YouTube inventory: primary + two public samples.
|
||||||
# Nonempty JSON overrides it: 1..5 unique 11-character VideoIDs (not signed URLs).
|
# Nonempty JSON overrides it: 1..5 unique 11-character VideoIDs (not signed URLs).
|
||||||
# ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS='["dQw4w9WgXcQ","aqz-KE-bpKQ","jNQXAC9IVRw"]'
|
# ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS='["dQw4w9WgXcQ","aqz-KE-bpKQ","jNQXAC9IVRw"]'
|
||||||
# Optional target ss-server native DNS; empty retains the base resolver behavior.
|
# Encrypted target DNS for both SOCKS hostname traffic and managed checks.
|
||||||
|
ZAPRET_AUTOTUNE_DNS_MODE=doh
|
||||||
|
ZAPRET_AUTOTUNE_DOH_URL=https://dns.google/dns-query
|
||||||
|
# Empty selects Google bootstrap only for the exact default URL above.
|
||||||
|
# Custom provider: change URL AND supply its literal bootstrap IPs (comma-separated).
|
||||||
|
ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS=
|
||||||
|
ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC=5
|
||||||
|
# Explicit native mode only; ignored in doh mode. Never an automatic fallback.
|
||||||
SS_DNS_SERVER=
|
SS_DNS_SERVER=
|
||||||
@@ -202,14 +202,35 @@ asset checks cover sampled thumbnail/avatar routes, not the whole thumbnail pool
|
|||||||
The current service/adaptive hardware acceptance gate is **pending**; the old
|
The current service/adaptive hardware acceptance gate is **pending**; the old
|
||||||
clean-run PASS below does not qualify these new modes.
|
clean-run PASS below does not qualify these new modes.
|
||||||
|
|
||||||
Optional `SS_DNS_SERVER` is an unbracketed literal IPv4/IPv6 address only (for
|
**Managed DNS defaults to DoH**, independently of service/YouTube mode. PID 1
|
||||||
example `8.8.8.8` or `2001:4860:4860::8888`); empty preserves existing resolver
|
starts the stdlib Go forwarder on container loopback `127.0.0.1:5353` (UDP and
|
||||||
behavior. No domain, port, zone, control characters or option strings are
|
TCP), validates its private PID readiness acknowledgement, then starts ss-server
|
||||||
accepted. Validation runs before service launch and reports opaque `configerror`
|
with `-d 127.0.0.1:5353`. No DNS port is published. Both client hostname traffic
|
||||||
without echoing input. DNS applies only to this target's native `ss-server`.
|
and managed checks share this encrypted resolver path; it works for arbitrary
|
||||||
The image build checks installed `ss-server -h` for native `-d <addr>` support;
|
services, not only YouTube. HTTPS uses normal certificate verification, direct
|
||||||
the validated nonsecret address is passed as a separate `-d` argv value, not
|
literal-IP bootstrap and a socket mark excluding live/probe NFQUEUE rules;
|
||||||
eval or an assumed JSON key. Passwords still go only to private JSON configs.
|
there is no recursive SOCKS resolution or plaintext fallback. Endpoint errors
|
||||||
|
return SERVFAIL while the forwarder stays alive, rather than switching DNS modes.
|
||||||
|
|
||||||
|
The private `.env` controls `ZAPRET_AUTOTUNE_DNS_MODE=doh|native`,
|
||||||
|
`ZAPRET_AUTOTUNE_DOH_URL` (default `https://dns.google/dns-query`),
|
||||||
|
`ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS` (missing/empty selects `8.8.8.8,8.8.4.4`
|
||||||
|
**only for the exact default Google URL**; otherwise explicit 1–4 literal
|
||||||
|
IPs, comma-separated without spaces), and `ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC`
|
||||||
|
(default `5`, integer 1–30). Changing provider requires changing **both** URL
|
||||||
|
and bootstrap IPs. Compose/example leave bootstrap empty, so changing only the
|
||||||
|
URL cannot silently reuse Google bootstrap. URL must use HTTPS/443 with a path, without userinfo, query
|
||||||
|
or fragment. A path can contain a private provider token: do not print it.
|
||||||
|
Validation reports opaque errors before any services start. The subprocess
|
||||||
|
gets provider configuration through env, never endpoint argv/logging.
|
||||||
|
|
||||||
|
Only explicit `ZAPRET_AUTOTUNE_DNS_MODE=native` restores the old resolver
|
||||||
|
behavior, with no forwarder child. In that mode optional `SS_DNS_SERVER` accepts
|
||||||
|
only an unbracketed literal IPv4/IPv6 address; empty retains the base resolver.
|
||||||
|
It is ignored in DoH mode and is **not** a fallback. Native DNS can be plaintext.
|
||||||
|
The pinned ss-server's native `-d <addr>` is used, not an assumed JSON key;
|
||||||
|
passwords still go only to private JSON configs. No extra capability is added;
|
||||||
|
Compose sets container-local `ip_unprivileged_port_start=0` for port 5353.
|
||||||
Managed probes use `socks5h://127.0.0.1:1080`: Player, metadata, image,
|
Managed probes use `socks5h://127.0.0.1:1080`: Player, metadata, image,
|
||||||
media, recovery and warmup hostnames go through the owned SS listener rather than
|
media, recovery and warmup hostnames go through the owned SS listener rather than
|
||||||
curl's local DNS. Older private `socks5` overrides are validated and canonicalized
|
curl's local DNS. Older private `socks5` overrides are validated and canonicalized
|
||||||
@@ -217,8 +238,13 @@ internally; credentials and unowned endpoints remain forbidden. Public legacy
|
|||||||
discovery and root checker local `--resolve` pinning are unchanged.
|
discovery and root checker local `--resolve` pinning are unchanged.
|
||||||
This corrects a DNS path bypass, not a proven cause of earlier peer certificate
|
This corrects a DNS path bypass, not a proven cause of earlier peer certificate
|
||||||
failures, and does not fix/prove all thumbnail routes. It is **not full asset
|
failures, and does not fix/prove all thumbnail routes. It is **not full asset
|
||||||
availability proof**. No DoH bridge, permanent Google IP pins or global host DNS
|
availability proof**. No permanent service IP pins or global host DNS changes
|
||||||
changes are supplied. PID 1 remains the sole startup/recovery owner.
|
are supplied. Clients must request remote DNS (browser SOCKS5 remote DNS or
|
||||||
|
`curl --socks5-hostname 127.0.0.1:11082` when that is the chosen host port).
|
||||||
|
Client-local resolution and IP-literal requests bypass this hostname DNS path;
|
||||||
|
the bridge does not encrypt unrelated container processes' DNS. The new DoH
|
||||||
|
integration still needs remote runtime acceptance; earlier clean-run PASS does
|
||||||
|
not qualify it. PID 1 remains the sole startup/recovery owner.
|
||||||
|
|
||||||
Compose has no automatic restart. PID 1 restarts the entire owned runtime only
|
Compose has no automatic restart. PID 1 restarts the entire owned runtime only
|
||||||
for technical failures, with three starts per rolling six hours (including initial
|
for technical failures, with three starts per rolling six hours (including initial
|
||||||
@@ -283,7 +309,13 @@ Any startup command failure/timeout exits 78, never the retry code 75. These
|
|||||||
bounds do not shorten the monitor's 90-minute bootstrap/recovery scan budget.
|
bounds do not shorten the monitor's 90-minute bootstrap/recovery scan budget.
|
||||||
|
|
||||||
SIGTERM/SIGINT cancels and waits for the monitor before stopping target-local
|
SIGTERM/SIGINT cancels and waits for the monitor before stopping target-local
|
||||||
zapret and Shadowsocks children. Nonretry monitor fatal exits stop the
|
zapret and Shadowsocks children, then terminates/joins DNS within five seconds
|
||||||
|
and removes only its anchored, validated readiness inode. PID 1 retains private
|
||||||
|
directory/file descriptors through child death, including SIGKILL. Preexisting,
|
||||||
|
replaced or symlink readiness evidence is preserved and fails closed rather
|
||||||
|
than being deleted for a restart. DNS remains available throughout rollback and
|
||||||
|
SS shutdown. A DNS child crash participates in the existing technical retry
|
||||||
|
budget; upstream outages do not kill that child. Nonretry monitor fatal exits stop the
|
||||||
entrypoint; technical exits and Shadowsocks failures restart from private configs
|
entrypoint; technical exits and Shadowsocks failures restart from private configs
|
||||||
only after safe cleanup. Legacy monitor health is YouTube Player API status
|
only after safe cleanup. Legacy monitor health is YouTube Player API status
|
||||||
through the owned SOCKS, not video playback/SABR; legacy GGC candidates are scoped
|
through the owned SOCKS, not video playback/SABR; legacy GGC candidates are scoped
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ with tempfile.TemporaryDirectory(prefix='target-config-') as tmp:
|
|||||||
('example.org', False), ('8.8.8.8\n', False), ('-d 8.8.8.8', False)):
|
('example.org', False), ('8.8.8.8\n', False), ('-d 8.8.8.8', False)):
|
||||||
for path in private.iterdir():
|
for path in private.iterdir():
|
||||||
path.unlink()
|
path.unlink()
|
||||||
result = subprocess.run(['sh', str(entry)], env=dict(base, SS_DNS_SERVER=dns),
|
result = subprocess.run(['sh', str(entry)], env=dict(base, ZAPRET_AUTOTUNE_DNS_MODE='native', SS_DNS_SERVER=dns),
|
||||||
capture_output=True, timeout=5)
|
capture_output=True, timeout=5)
|
||||||
assert result.returncode == (0 if valid else 64), result.stderr
|
assert result.returncode == (0 if valid else 64), result.stderr
|
||||||
assert not result.stdout and base['SS_PASSWORD'].encode() not in result.stderr
|
assert not result.stdout and base['SS_PASSWORD'].encode() not in result.stderr
|
||||||
@@ -43,6 +43,24 @@ with tempfile.TemporaryDirectory(prefix='target-config-') as tmp:
|
|||||||
path = private / (name + '.json')
|
path = private / (name + '.json')
|
||||||
assert path.stat().st_mode & 0o777 == 0o600
|
assert path.stat().st_mode & 0o777 == 0o600
|
||||||
assert json.loads(path.read_text())['password'] == base['SS_PASSWORD']
|
assert json.loads(path.read_text())['password'] == base['SS_PASSWORD']
|
||||||
|
for overrides, valid in (({}, True), ({'SS_DNS_SERVER': 'ignored.example'}, True),
|
||||||
|
({'ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS': ''}, True),
|
||||||
|
({'ZAPRET_AUTOTUNE_DNS_MODE': ''}, False),
|
||||||
|
({'ZAPRET_AUTOTUNE_DOH_URL': 'http://secret.invalid/token'}, False),
|
||||||
|
({'ZAPRET_AUTOTUNE_DOH_URL': 'https://secret.invalid/token'}, False),
|
||||||
|
({'ZAPRET_AUTOTUNE_DOH_URL': 'https://secret.invalid/token',
|
||||||
|
'ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS': ''}, False),
|
||||||
|
({'ZAPRET_AUTOTUNE_DOH_URL': 'https://secret.invalid/token',
|
||||||
|
'ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS': '192.0.2.1'}, True),
|
||||||
|
({'ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC': '31'}, False)):
|
||||||
|
for path in private.iterdir():
|
||||||
|
path.unlink()
|
||||||
|
result = subprocess.run(['sh', str(entry)], env=dict(base, **overrides), capture_output=True, timeout=5)
|
||||||
|
assert result.returncode == (0 if valid else 64), result.stderr
|
||||||
|
assert b'secret.invalid' not in result.stderr and b'token' not in result.stderr
|
||||||
|
assert base['SS_PASSWORD'].encode() not in result.stdout + result.stderr
|
||||||
|
if not valid:
|
||||||
|
assert not list(private.iterdir())
|
||||||
# Render privately, never print the config/password; config does not deploy.
|
# Render privately, never print the config/password; config does not deploy.
|
||||||
compose_env = {k: v for k, v in base.items() if not k.startswith('ZAPRET_AUTOTUNE_')}
|
compose_env = {k: v for k, v in base.items() if not k.startswith('ZAPRET_AUTOTUNE_')}
|
||||||
factory_cases = []
|
factory_cases = []
|
||||||
@@ -74,9 +92,36 @@ with tempfile.TemporaryDirectory(prefix='target-config-') as tmp:
|
|||||||
assert settings['ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE'] == mode
|
assert settings['ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE'] == mode
|
||||||
assert settings['ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS'] == overrides.get('ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS', '')
|
assert settings['ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS'] == overrides.get('ZAPRET_AUTOTUNE_GOOGLEVIDEO_VIDEO_IDS', '')
|
||||||
assert settings['SS_DNS_SERVER'] == overrides.get('SS_DNS_SERVER', '')
|
assert settings['SS_DNS_SERVER'] == overrides.get('SS_DNS_SERVER', '')
|
||||||
|
assert settings['ZAPRET_AUTOTUNE_DNS_MODE'] == 'doh'
|
||||||
|
assert settings['ZAPRET_AUTOTUNE_DOH_URL'] == 'https://dns.google/dns-query'
|
||||||
|
assert settings['ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS'] == ''
|
||||||
|
assert settings['ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC'] == '5'
|
||||||
assert settings['ZAPRET_AUTOTUNE_SOCKS_PROXY'] == 'socks5h://127.0.0.1:1080'
|
assert settings['ZAPRET_AUTOTUNE_SOCKS_PROXY'] == 'socks5h://127.0.0.1:1080'
|
||||||
factory_cases.append({'environment': {k: v for k, v in settings.items()
|
factory_cases.append({'environment': {k: v for k, v in settings.items()
|
||||||
if k.startswith('ZAPRET_AUTOTUNE_')}, 'ids': ids})
|
if k.startswith('ZAPRET_AUTOTUNE_')}, 'ids': ids})
|
||||||
|
# Validate the actual rendered DNS environment, not an invented equivalent.
|
||||||
|
# Compose must not inject Google bootstrap into a custom-provider request.
|
||||||
|
for service in ('0', '1'):
|
||||||
|
for provider in ({}, {'ZAPRET_AUTOTUNE_DOH_URL': 'https://secret.invalid/token'}):
|
||||||
|
for bootstrap in ({}, {'ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS': ''},
|
||||||
|
{'ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS': '192.0.2.1'}):
|
||||||
|
overrides = dict(provider, **bootstrap, ZAPRET_AUTOTUNE_YOUTUBE_SERVICE=service)
|
||||||
|
result = subprocess.run(['docker', 'compose', '--env-file', '/dev/null', '-f',
|
||||||
|
str(ROOT / 'deploy/managed-target/compose.yaml'),
|
||||||
|
'config', '--format', 'json'], env=dict(compose_env, **overrides),
|
||||||
|
capture_output=True, timeout=10)
|
||||||
|
assert result.returncode == 0, 'Compose config failed (output suppressed)'
|
||||||
|
settings = json.loads(result.stdout)['services']['target']['environment']
|
||||||
|
assert settings['ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS'] == bootstrap.get('ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS', '')
|
||||||
|
for path in private.iterdir():
|
||||||
|
path.unlink()
|
||||||
|
validated = subprocess.run(['sh', str(entry)], env=dict(env, **settings), capture_output=True, timeout=5)
|
||||||
|
valid = not provider or bool(bootstrap.get('ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS'))
|
||||||
|
assert validated.returncode == (0 if valid else 64), 'Rendered DNS config validation failed'
|
||||||
|
assert b'secret.invalid' not in validated.stderr and b'token' not in validated.stderr
|
||||||
|
assert base['SS_PASSWORD'].encode() not in validated.stdout + validated.stderr
|
||||||
|
if not valid:
|
||||||
|
assert not list(private.iterdir()), 'Invalid DNS config created service configs'
|
||||||
# Only sanitized fixture settings cross into Go; never persist the password.
|
# Only sanitized fixture settings cross into Go; never persist the password.
|
||||||
fixture = root / 'compose-factory.json'
|
fixture = root / 'compose-factory.json'
|
||||||
fixture.write_text(json.dumps(factory_cases))
|
fixture.write_text(json.dumps(factory_cases))
|
||||||
|
|||||||
@@ -23,6 +23,10 @@ assert " create_host_path: false\n" in compose
|
|||||||
assert "SS_PASSWORD: ${SS_PASSWORD:?Set SS_PASSWORD in the deployment environment}" in compose
|
assert "SS_PASSWORD: ${SS_PASSWORD:?Set SS_PASSWORD in the deployment environment}" in compose
|
||||||
for setting in (
|
for setting in (
|
||||||
'SS_DNS_SERVER: ${SS_DNS_SERVER-}',
|
'SS_DNS_SERVER: ${SS_DNS_SERVER-}',
|
||||||
|
'ZAPRET_AUTOTUNE_DNS_MODE: ${ZAPRET_AUTOTUNE_DNS_MODE-doh}',
|
||||||
|
'ZAPRET_AUTOTUNE_DOH_URL: ${ZAPRET_AUTOTUNE_DOH_URL-https://dns.google/dns-query}',
|
||||||
|
'ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS: ${ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS-}',
|
||||||
|
'ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC: ${ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC-5}',
|
||||||
'ZAPRET_AUTOTUNE_YOUTUBE_SERVICE: ${ZAPRET_AUTOTUNE_YOUTUBE_SERVICE-0}',
|
'ZAPRET_AUTOTUNE_YOUTUBE_SERVICE: ${ZAPRET_AUTOTUNE_YOUTUBE_SERVICE-0}',
|
||||||
'ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE: ${ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE-cold}',
|
'ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE: ${ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE-cold}',
|
||||||
'ZAPRET_AUTOTUNE_VIDEO_ID: ${ZAPRET_AUTOTUNE_VIDEO_ID-dQw4w9WgXcQ}',
|
'ZAPRET_AUTOTUNE_VIDEO_ID: ${ZAPRET_AUTOTUNE_VIDEO_ID-dQw4w9WgXcQ}',
|
||||||
@@ -30,7 +34,17 @@ for setting in (
|
|||||||
):
|
):
|
||||||
assert setting in compose, setting
|
assert setting in compose, setting
|
||||||
assert entrypoint.index('target-config-check || exit 64') < entrypoint.index('cat > "$config_dir/server.json"')
|
assert entrypoint.index('target-config-check || exit 64') < entrypoint.index('cat > "$config_dir/server.json"')
|
||||||
assert 'set -- "$@" -d "$SS_DNS_SERVER"' in entrypoint
|
assert 'set -- "$@" -d "$dns_address"' in entrypoint
|
||||||
|
assert entrypoint.index('start_dns || return $?') < entrypoint.index('set -- ss-server')
|
||||||
|
assert 'dns-forwarder readiness-check "$dns_pid"' in entrypoint
|
||||||
|
assert 'dns-forwarder readiness-cleanup "$dns_pid"' in entrypoint
|
||||||
|
assert 'exec 8<"$config_dir"' in entrypoint
|
||||||
|
assert 'exec 9<"$config_dir/dns-ready"' in entrypoint
|
||||||
|
assert 'rm -f "$config_dir/dns-ready"' not in entrypoint
|
||||||
|
assert 'while dns_alive && kill -0 "$server_pid"' in entrypoint
|
||||||
|
assert entrypoint.index('bounded_wait "$pid" 5') < entrypoint.index('bounded_wait "$dns_pid" 5')
|
||||||
|
assert '5353' not in compose
|
||||||
|
assert 'net.ipv4.ip_unprivileged_port_start: "0"' in compose
|
||||||
assert 'eval ' not in entrypoint
|
assert 'eval ' not in entrypoint
|
||||||
assert "secrets:" not in compose
|
assert "secrets:" not in compose
|
||||||
assert "ss_password=${SS_PASSWORD-}" in entrypoint
|
assert "ss_password=${SS_PASSWORD-}" in entrypoint
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ Only a temporary COPY of the entrypoint is redirected to fake executables;
|
|||||||
production has no test switches, real network probes, or accelerated timers.
|
production has no test switches, real network probes, or accelerated timers.
|
||||||
"""
|
"""
|
||||||
import ctypes
|
import ctypes
|
||||||
|
import atexit
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import shutil
|
import shutil
|
||||||
@@ -13,6 +14,15 @@ import time
|
|||||||
|
|
||||||
|
|
||||||
ROOT = Path(__file__).resolve().parents[2]
|
ROOT = Path(__file__).resolve().parents[2]
|
||||||
|
# Real readiness CLI checks root ownership. Use an isolated user namespace for
|
||||||
|
# offline fixtures when invoked as an ordinary user; no host-root requirement.
|
||||||
|
if os.geteuid() != 0:
|
||||||
|
os.execvp('unshare', ['unshare', '-Ur', 'python3', str(Path(__file__).resolve())])
|
||||||
|
CLI_TMP = tempfile.TemporaryDirectory(prefix='dns-readiness-cli-')
|
||||||
|
atexit.register(CLI_TMP.cleanup)
|
||||||
|
DNS_CLI = Path(CLI_TMP.name) / 'autotune'
|
||||||
|
subprocess.run(['go', 'build', '-o', str(DNS_CLI), './cmd/zapret-autotune'],
|
||||||
|
cwd=ROOT, env=dict(os.environ, GOPROXY='off', GOTOOLCHAIN='local'), check=True)
|
||||||
FAKE = r'''#!/usr/bin/env python3
|
FAKE = r'''#!/usr/bin/env python3
|
||||||
import ctypes, os, pathlib, signal, subprocess, sys, time
|
import ctypes, os, pathlib, signal, subprocess, sys, time
|
||||||
root = pathlib.Path(os.environ['FIXTURE'])
|
root = pathlib.Path(os.environ['FIXTURE'])
|
||||||
@@ -27,7 +37,11 @@ def increment(key):
|
|||||||
path.write_text(str(n))
|
path.write_text(str(n))
|
||||||
return n
|
return n
|
||||||
def linger(code=0):
|
def linger(code=0):
|
||||||
signal.signal(signal.SIGTERM, lambda *_: sys.exit(code))
|
def stop(*_):
|
||||||
|
if os.environ.get('ZAPRET_AUTOTUNE_DNS_MODE', 'doh') == 'doh' and scenario not in ('dns-death', 'dns-killed', 'dns-replace', 'dns-replace-symlink', 'dns-prestart'):
|
||||||
|
os.kill(int((root / 'dns-pid').read_text()), 0)
|
||||||
|
sys.exit(code)
|
||||||
|
signal.signal(signal.SIGTERM, stop)
|
||||||
while True:
|
while True:
|
||||||
time.sleep(.01)
|
time.sleep(.01)
|
||||||
def assert_workers_gone():
|
def assert_workers_gone():
|
||||||
@@ -74,8 +88,12 @@ if name in ('ss-server', 'ss-local'):
|
|||||||
f.write(str(os.getpid()) + '\n')
|
f.write(str(os.getpid()) + '\n')
|
||||||
assert 'SS_PASSWORD' not in os.environ
|
assert 'SS_PASSWORD' not in os.environ
|
||||||
assert sys.argv[1] == '-c'
|
assert sys.argv[1] == '-c'
|
||||||
dns = os.environ.get('SS_DNS_SERVER', '') if name == 'ss-server' else ''
|
dns = ('127.0.0.1:5353' if os.environ.get('ZAPRET_AUTOTUNE_DNS_MODE', 'doh') == 'doh'
|
||||||
|
else os.environ.get('SS_DNS_SERVER', '')) if name == 'ss-server' else ''
|
||||||
assert sys.argv[3:] == (['-d', dns] if dns else [])
|
assert sys.argv[3:] == (['-d', dns] if dns else [])
|
||||||
|
if dns == '127.0.0.1:5353':
|
||||||
|
assert (root / 'private' / 'dns-ready').read_text().strip() == (root / 'dns-pid').read_text()
|
||||||
|
os.kill(int((root / 'dns-pid').read_text()), 0)
|
||||||
assert (pathlib.Path(sys.argv[2]).stat().st_mode & 0o777) == 0o600
|
assert (pathlib.Path(sys.argv[2]).stat().st_mode & 0o777) == 0o600
|
||||||
log(name)
|
log(name)
|
||||||
if name == 'ss-local' and scenario in ('ss', 'mixed', 'joinfail') and increment('ss_runs') == 1:
|
if name == 'ss-local' and scenario in ('ss', 'mixed', 'joinfail') and increment('ss_runs') == 1:
|
||||||
@@ -96,6 +114,71 @@ if name == 'zapret2':
|
|||||||
assert name == 'autotune'
|
assert name == 'autotune'
|
||||||
if sys.argv[1:] == ['target-config-check']:
|
if sys.argv[1:] == ['target-config-check']:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
if sys.argv[1] == 'dns-forwarder':
|
||||||
|
if sys.argv[2] in ('readiness-check', 'readiness-cleanup'):
|
||||||
|
# exec preserves PID1's actual inherited anchors; no simulated unlink.
|
||||||
|
os.execv(os.environ['DNS_CLI'], [os.environ['DNS_CLI'], *sys.argv[1:]])
|
||||||
|
mode = os.environ.get('ZAPRET_AUTOTUNE_DNS_MODE', 'doh')
|
||||||
|
if sys.argv[2] == 'mode':
|
||||||
|
print(mode)
|
||||||
|
sys.exit(0)
|
||||||
|
if sys.argv[2] == 'address':
|
||||||
|
print('127.0.0.1:5353' if mode == 'doh' else os.environ.get('SS_DNS_SERVER', ''))
|
||||||
|
sys.exit(0)
|
||||||
|
assert sys.argv[2:] == ['serve'] and mode == 'doh'
|
||||||
|
assert 'SS_PASSWORD' not in os.environ
|
||||||
|
with (root / 'children').open('a') as f:
|
||||||
|
f.write(str(os.getpid()) + '\n')
|
||||||
|
(root / 'dns-pid').write_text(str(os.getpid()))
|
||||||
|
log('dns-start')
|
||||||
|
if scenario == 'dns-early':
|
||||||
|
sys.exit(1)
|
||||||
|
ready = root / 'private' / 'dns-ready'
|
||||||
|
owned = None
|
||||||
|
if scenario == 'dns-preack-foreign':
|
||||||
|
ready.write_text('foreign evidence\n')
|
||||||
|
sys.exit(1)
|
||||||
|
if scenario not in ('dns-noack', 'dns-term', 'dns-int'):
|
||||||
|
with ready.open('x') as f:
|
||||||
|
os.chmod(ready, 0o600)
|
||||||
|
f.write(str(os.getpid() + (1 if scenario == 'dns-wrongack' else 0)) + '\n')
|
||||||
|
owned = ready.stat()
|
||||||
|
if scenario == 'dns-permissions':
|
||||||
|
ready.chmod(0o644)
|
||||||
|
if scenario == 'dns-symlink':
|
||||||
|
payload = ready.with_name('dns-payload')
|
||||||
|
ready.rename(payload)
|
||||||
|
ready.symlink_to(payload)
|
||||||
|
log('dns-ready')
|
||||||
|
def stop_dns(*_):
|
||||||
|
assert not (root / 'prepared').exists(), 'DNS stopped before restoration'
|
||||||
|
# SS children have been joined, not merely signalled.
|
||||||
|
for pid in (root / 'children').read_text().splitlines():
|
||||||
|
if int(pid) != os.getpid():
|
||||||
|
assert not pathlib.Path('/proc/' + pid).exists(), ('DNS stopped before child join', pid)
|
||||||
|
log('dns-stop')
|
||||||
|
if ready.exists() and not ready.is_symlink() and owned and os.path.samestat(owned, ready.stat()):
|
||||||
|
ready.unlink()
|
||||||
|
sys.exit(0)
|
||||||
|
signal.signal(signal.SIGTERM, stop_dns)
|
||||||
|
if scenario in ('dns-term', 'dns-int'):
|
||||||
|
os.kill(os.getppid(), signal.SIGTERM if scenario == 'dns-term' else signal.SIGINT)
|
||||||
|
if scenario in ('dns-death', 'dns-killed', 'dns-replace', 'dns-replace-symlink') and increment('dns_runs') == 1:
|
||||||
|
while not (root / 'monitor_runs').exists():
|
||||||
|
time.sleep(.01)
|
||||||
|
if scenario.startswith('dns-replace'):
|
||||||
|
# Retain old inode so a replacement cannot reuse its number.
|
||||||
|
ready.rename(ready.with_name('retired-ready'))
|
||||||
|
payload = ready.with_name('foreign-ready')
|
||||||
|
payload.write_text('foreign evidence\n')
|
||||||
|
payload.chmod(0o600)
|
||||||
|
if scenario == 'dns-replace-symlink':
|
||||||
|
ready.symlink_to(payload)
|
||||||
|
else:
|
||||||
|
payload.rename(ready)
|
||||||
|
os.kill(os.getpid(), signal.SIGKILL)
|
||||||
|
while True:
|
||||||
|
time.sleep(.01)
|
||||||
command = sys.argv[2]
|
command = sys.argv[2]
|
||||||
if command == 'init':
|
if command == 'init':
|
||||||
log('init')
|
log('init')
|
||||||
@@ -107,6 +190,9 @@ elif command == 'reconcile':
|
|||||||
assert_workers_gone()
|
assert_workers_gone()
|
||||||
if scenario == 'prestartfail':
|
if scenario == 'prestartfail':
|
||||||
sys.exit(78)
|
sys.exit(78)
|
||||||
|
if scenario == 'dns-prestart':
|
||||||
|
os.kill(int((root / 'dns-pid').read_text()), signal.SIGKILL)
|
||||||
|
time.sleep(.03)
|
||||||
if scenario in ('unknown', 'unknown75') and n == 2:
|
if scenario in ('unknown', 'unknown75') and n == 2:
|
||||||
sys.exit(78)
|
sys.exit(78)
|
||||||
(root / 'prepared').unlink(missing_ok=True)
|
(root / 'prepared').unlink(missing_ok=True)
|
||||||
@@ -116,6 +202,11 @@ elif command == 'monitor':
|
|||||||
n = increment('monitor_runs')
|
n = increment('monitor_runs')
|
||||||
log('monitor ' + str(n))
|
log('monitor ' + str(n))
|
||||||
assert os.getsid(0) == os.getpid(), 'monitor not in its own verified session'
|
assert os.getsid(0) == os.getpid(), 'monitor not in its own verified session'
|
||||||
|
if scenario in ('dns-death', 'dns-killed', 'dns-replace', 'dns-replace-symlink') and n == 1:
|
||||||
|
linger()
|
||||||
|
if scenario == 'dns-outage':
|
||||||
|
# Upstream SERVFAIL is modelled by a live DNS child, not child failure.
|
||||||
|
assert (root / 'dns-pid').exists()
|
||||||
if n == 1 and scenario == 'orphans':
|
if n == 1 and scenario == 'orphans':
|
||||||
subprocess.Popen([str(root / 'bin' / 'blockcheck')], preexec_fn=os.setpgrp)
|
subprocess.Popen([str(root / 'bin' / 'blockcheck')], preexec_fn=os.setpgrp)
|
||||||
while not (root / 'workers').exists() or len((root / 'workers').read_text().splitlines()) != 2:
|
while not (root / 'workers').exists() or len((root / 'workers').read_text().splitlines()) != 2:
|
||||||
@@ -144,7 +235,7 @@ elif command == 'monitor':
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
def run(scenario, expected, dns=''):
|
def run(scenario, expected, dns='', mode='native'):
|
||||||
with tempfile.TemporaryDirectory(prefix='supervision-') as tmp:
|
with tempfile.TemporaryDirectory(prefix='supervision-') as tmp:
|
||||||
root = Path(tmp)
|
root = Path(tmp)
|
||||||
binary = root / 'bin'
|
binary = root / 'bin'
|
||||||
@@ -156,6 +247,11 @@ def run(scenario, expected, dns=''):
|
|||||||
(binary / name).symlink_to(fake)
|
(binary / name).symlink_to(fake)
|
||||||
configs = root / 'private'
|
configs = root / 'private'
|
||||||
configs.mkdir()
|
configs.mkdir()
|
||||||
|
if scenario.startswith('dns-stale'):
|
||||||
|
if scenario == 'dns-stale-symlink':
|
||||||
|
(configs / 'dns-ready').symlink_to(configs / 'missing')
|
||||||
|
else:
|
||||||
|
(configs / 'dns-ready').write_text('foreign evidence\n')
|
||||||
text = (ROOT / 'managed-target-entrypoint.sh').read_text()
|
text = (ROOT / 'managed-target-entrypoint.sh').read_text()
|
||||||
helper = ROOT / 'managed-target-supervision.sh'
|
helper = ROOT / 'managed-target-supervision.sh'
|
||||||
if scenario == 'ackfail':
|
if scenario == 'ackfail':
|
||||||
@@ -170,7 +266,11 @@ def run(scenario, expected, dns=''):
|
|||||||
entry.write_text(text)
|
entry.write_text(text)
|
||||||
env = dict(os.environ, PATH=f'{binary}:{os.environ["PATH"]}', FIXTURE=tmp,
|
env = dict(os.environ, PATH=f'{binary}:{os.environ["PATH"]}', FIXTURE=tmp,
|
||||||
SCENARIO=scenario, SS_PASSWORD='ab' * 32, SS_DNS_SERVER=dns,
|
SCENARIO=scenario, SS_PASSWORD='ab' * 32, SS_DNS_SERVER=dns,
|
||||||
ZAPRET_CONFIG=str(root / 'config'))
|
ZAPRET_CONFIG=str(root / 'config'), DNS_CLI=str(DNS_CLI))
|
||||||
|
if mode is None:
|
||||||
|
env.pop('ZAPRET_AUTOTUNE_DNS_MODE', None)
|
||||||
|
else:
|
||||||
|
env['ZAPRET_AUTOTUNE_DNS_MODE'] = mode
|
||||||
peer = None
|
peer = None
|
||||||
if scenario == 'orphans':
|
if scenario == 'orphans':
|
||||||
# Local fixture is not PID 1. Adopt orphan workers here so the test
|
# Local fixture is not PID 1. Adopt orphan workers here so the test
|
||||||
@@ -186,10 +286,10 @@ def run(scenario, expected, dns=''):
|
|||||||
if peer:
|
if peer:
|
||||||
peer.terminate()
|
peer.terminate()
|
||||||
peer.wait(timeout=2)
|
peer.wait(timeout=2)
|
||||||
events = (root / 'events').read_text().splitlines()
|
events = (root / 'events').read_text().splitlines() if (root / 'events').exists() else []
|
||||||
assert result.returncode == expected, (scenario, result.returncode, result.stderr, events)
|
assert result.returncode == expected, (scenario, result.returncode, result.stderr, events)
|
||||||
assert b'ab' * 32 not in result.stdout + result.stderr
|
assert b'ab' * 32 not in result.stdout + result.stderr
|
||||||
for pid in (root / 'children').read_text().splitlines():
|
for pid in ((root / 'children').read_text().splitlines() if (root / 'children').exists() else []):
|
||||||
assert not Path(f'/proc/{pid}').exists(), (scenario, 'child not joined', pid)
|
assert not Path(f'/proc/{pid}').exists(), (scenario, 'child not joined', pid)
|
||||||
if scenario == 'orphans':
|
if scenario == 'orphans':
|
||||||
for pid in (root / 'workers').read_text().splitlines():
|
for pid in (root / 'workers').read_text().splitlines():
|
||||||
@@ -205,6 +305,16 @@ def run(scenario, expected, dns=''):
|
|||||||
for i, event in enumerate(events):
|
for i, event in enumerate(events):
|
||||||
if event in ('start', 'stop'):
|
if event in ('start', 'stop'):
|
||||||
assert events[i - 1] == 'reconcile', (scenario, events)
|
assert events[i - 1] == 'reconcile', (scenario, events)
|
||||||
|
foreign = scenario in ('dns-symlink', 'dns-preack-foreign', 'dns-replace', 'dns-replace-symlink', 'dns-stale', 'dns-stale-symlink')
|
||||||
|
if foreign:
|
||||||
|
ready = configs / 'dns-ready'
|
||||||
|
assert ready.exists() or ready.is_symlink(), (scenario, 'foreign readiness deleted')
|
||||||
|
if scenario != 'dns-stale-symlink' and scenario != 'dns-symlink':
|
||||||
|
assert ready.read_text() == 'foreign evidence\n', (scenario, 'foreign bytes changed')
|
||||||
|
else:
|
||||||
|
assert not (configs / 'dns-ready').exists(), (scenario, 'stale DNS readiness')
|
||||||
|
if mode == 'native':
|
||||||
|
assert 'dns-start' not in events
|
||||||
return events
|
return events
|
||||||
|
|
||||||
|
|
||||||
@@ -233,6 +343,30 @@ for dns in ('8.8.8.8', '2001:4860:4860::8888'):
|
|||||||
events = run('fatal64', 64, dns)
|
events = run('fatal64', 64, dns)
|
||||||
assert events.count('ss-server') == 1 and events.count('ss-local') == 1
|
assert events.count('ss-server') == 1 and events.count('ss-local') == 1
|
||||||
|
|
||||||
|
for scenario in ('dns-outage', 'technical', 'ss', 'cancel', 'dns-death', 'dns-killed'):
|
||||||
|
events = run(scenario, 0, dns='8.8.8.8', mode=None)
|
||||||
|
assert events.index('dns-ready') < events.index('ss-server')
|
||||||
|
assert events.index('dns-ready') < events.index('ss-local')
|
||||||
|
if scenario not in ('dns-death', 'dns-killed'):
|
||||||
|
assert events.count('dns-start') == events.count('dns-stop')
|
||||||
|
assert events.index('stop') < events.index('dns-stop')
|
||||||
|
if scenario == 'dns-outage':
|
||||||
|
assert events.count('start') == 1 and 'cooldown 15' not in events
|
||||||
|
for scenario, code in (('dns-early', 78), ('dns-noack', 78), ('dns-wrongack', 78),
|
||||||
|
('dns-permissions', 78), ('dns-symlink', 78), ('dns-prestart', 78)):
|
||||||
|
events = run(scenario, code, mode='doh')
|
||||||
|
assert not any(e.startswith('monitor ') for e in events)
|
||||||
|
if scenario != 'dns-prestart':
|
||||||
|
assert 'ss-server' not in events and 'ss-local' not in events
|
||||||
|
for scenario in ('dns-stale', 'dns-stale-symlink', 'dns-preack-foreign', 'dns-replace', 'dns-replace-symlink'):
|
||||||
|
events = run(scenario, 78, mode='doh')
|
||||||
|
assert events.count('dns-start') <= 1 and 'cooldown 15' not in events
|
||||||
|
if scenario.startswith('dns-stale'):
|
||||||
|
assert not events
|
||||||
|
for scenario in ('dns-term', 'dns-int'):
|
||||||
|
events = run(scenario, 0, mode='doh')
|
||||||
|
assert events == ['dns-start', 'dns-stop'], events
|
||||||
|
|
||||||
|
|
||||||
def check_watchdog_traps(shell):
|
def check_watchdog_traps(shell):
|
||||||
"""Same bounded_wait from normal flow, plain EXIT, and TERM -> EXIT traps."""
|
"""Same bounded_wait from normal flow, plain EXIT, and TERM -> EXIT traps."""
|
||||||
|
|||||||
@@ -13,6 +13,10 @@ services:
|
|||||||
environment:
|
environment:
|
||||||
SS_PASSWORD: ${SS_PASSWORD:?Set SS_PASSWORD in the deployment environment}
|
SS_PASSWORD: ${SS_PASSWORD:?Set SS_PASSWORD in the deployment environment}
|
||||||
SS_DNS_SERVER: ${SS_DNS_SERVER-}
|
SS_DNS_SERVER: ${SS_DNS_SERVER-}
|
||||||
|
ZAPRET_AUTOTUNE_DNS_MODE: ${ZAPRET_AUTOTUNE_DNS_MODE-doh}
|
||||||
|
ZAPRET_AUTOTUNE_DOH_URL: ${ZAPRET_AUTOTUNE_DOH_URL-https://dns.google/dns-query}
|
||||||
|
ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS: ${ZAPRET_AUTOTUNE_DOH_BOOTSTRAP_IPS-}
|
||||||
|
ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC: ${ZAPRET_AUTOTUNE_DOH_TIMEOUT_SEC-5}
|
||||||
ZAPRET_AUTOTUNE_YOUTUBE_SERVICE: ${ZAPRET_AUTOTUNE_YOUTUBE_SERVICE-0}
|
ZAPRET_AUTOTUNE_YOUTUBE_SERVICE: ${ZAPRET_AUTOTUNE_YOUTUBE_SERVICE-0}
|
||||||
ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE: ${ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE-cold}
|
ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE: ${ZAPRET_AUTOTUNE_GOOGLEVIDEO_MODE-cold}
|
||||||
ZAPRET_AUTOTUNE_VIDEO_ID: ${ZAPRET_AUTOTUNE_VIDEO_ID-dQw4w9WgXcQ}
|
ZAPRET_AUTOTUNE_VIDEO_ID: ${ZAPRET_AUTOTUNE_VIDEO_ID-dQw4w9WgXcQ}
|
||||||
@@ -38,6 +42,8 @@ services:
|
|||||||
networks: [target]
|
networks: [target]
|
||||||
restart: "no"
|
restart: "no"
|
||||||
stop_grace_period: 240s
|
stop_grace_period: 240s
|
||||||
|
sysctls:
|
||||||
|
net.ipv4.ip_unprivileged_port_start: "0"
|
||||||
networks:
|
networks:
|
||||||
target:
|
target:
|
||||||
driver: bridge
|
driver: bridge
|
||||||
@@ -14,6 +14,9 @@ esac
|
|||||||
[ "${#ss_password}" -ge 32 ] || { echo 'error: target secret must be at least 32 hex characters' >&2; exit 64; }
|
[ "${#ss_password}" -ge 32 ] || { echo 'error: target secret must be at least 32 hex characters' >&2; exit 64; }
|
||||||
# Validate before creating configs or launching any service. Errors never echo input.
|
# Validate before creating configs or launching any service. Errors never echo input.
|
||||||
/usr/local/bin/zapret-autotune target-config-check || exit 64
|
/usr/local/bin/zapret-autotune target-config-check || exit 64
|
||||||
|
dns_mode=$(/usr/local/bin/zapret-autotune dns-forwarder mode) || exit 64
|
||||||
|
dns_address=$(/usr/local/bin/zapret-autotune dns-forwarder address) || exit 64
|
||||||
|
case "$dns_mode" in doh|native) ;; *) exit 64 ;; esac
|
||||||
config_dir=/run/zapret-autotune-ss
|
config_dir=/run/zapret-autotune-ss
|
||||||
awk '$2 == "/run/zapret-autotune-ss" && $3 == "tmpfs" { found=1 } END { exit !found }' /proc/mounts || {
|
awk '$2 == "/run/zapret-autotune-ss" && $3 == "tmpfs" { found=1 } END { exit !found }' /proc/mounts || {
|
||||||
echo 'error: target Shadowsocks config directory must be a dedicated tmpfs' >&2; exit 78;
|
echo 'error: target Shadowsocks config directory must be a dedicated tmpfs' >&2; exit 78;
|
||||||
@@ -29,6 +32,9 @@ chmod 0600 "$config_dir/server.json" "$config_dir/local.json"
|
|||||||
unset ss_password
|
unset ss_password
|
||||||
|
|
||||||
server_pid=
|
server_pid=
|
||||||
|
dns_pid=
|
||||||
|
dns_anchor_dir=0
|
||||||
|
dns_anchor_file=0
|
||||||
local_pid=
|
local_pid=
|
||||||
monitor_pid=
|
monitor_pid=
|
||||||
monitor_sid=
|
monitor_sid=
|
||||||
@@ -39,7 +45,7 @@ cooldown_pid=
|
|||||||
# descendant signalling is reserved for SS shutdown and forced watchdog cleanup.
|
# descendant signalling is reserved for SS shutdown and forced watchdog cleanup.
|
||||||
. /opt/managed-target-supervision.sh
|
. /opt/managed-target-supervision.sh
|
||||||
pause() {
|
pause() {
|
||||||
sleep "$1" </dev/null >/dev/null 2>&1 & cooldown_pid=$!
|
sleep "$1" 8<&- 9<&- </dev/null >/dev/null 2>&1 & cooldown_pid=$!
|
||||||
wait "$cooldown_pid"
|
wait "$cooldown_pid"
|
||||||
cooldown_pid=
|
cooldown_pid=
|
||||||
}
|
}
|
||||||
@@ -82,7 +88,7 @@ bounded_wait() {
|
|||||||
rm -f "$config_dir/join-timeout" "$config_dir/join-ready"
|
rm -f "$config_dir/join-timeout" "$config_dir/join-ready"
|
||||||
# Fresh interpreter: a forked shell can inherit a deferred EXIT/TERM trap
|
# Fresh interpreter: a forked shell can inherit a deferred EXIT/TERM trap
|
||||||
# context and never run its cancellation handler during PID 1 shutdown.
|
# context and never run its cancellation handler during PID 1 shutdown.
|
||||||
/bin/sh /opt/managed-target-supervision.sh --watchdog "$child" "$seconds" "$config_dir" </dev/null >/dev/null 2>&1 &
|
/bin/sh /opt/managed-target-supervision.sh --watchdog "$child" "$seconds" "$config_dir" 8<&- 9<&- </dev/null >/dev/null 2>&1 &
|
||||||
watchdog=$!
|
watchdog=$!
|
||||||
# A fast-exiting child must not cancel the watchdog before its trap exists.
|
# A fast-exiting child must not cancel the watchdog before its trap exists.
|
||||||
# Fail closed if exec/setup fails before acknowledgement; never wait forever.
|
# Fail closed if exec/setup fails before acknowledgement; never wait forever.
|
||||||
@@ -146,6 +152,16 @@ cleanup_cycle() {
|
|||||||
done
|
done
|
||||||
server_pid=
|
server_pid=
|
||||||
local_pid=
|
local_pid=
|
||||||
|
# DNS remains available through monitor rollback, zapret stop and SS joins.
|
||||||
|
if [ -n "$dns_pid" ]; then
|
||||||
|
kill -TERM "$dns_pid" 2>/dev/null || true
|
||||||
|
bounded_wait "$dns_pid" 5
|
||||||
|
[ "$join_timed_out" -eq 0 ] || { echo 'error: DNS join timeout' >&2; cleanup_code=78; }
|
||||||
|
/usr/local/bin/zapret-autotune dns-forwarder readiness-cleanup "$dns_pid" || cleanup_code=78
|
||||||
|
dns_pid=
|
||||||
|
fi
|
||||||
|
if [ "$dns_anchor_file" -eq 1 ]; then exec 9<&-; dns_anchor_file=0; fi
|
||||||
|
if [ "$dns_anchor_dir" -eq 1 ]; then exec 8<&-; dns_anchor_dir=0; fi
|
||||||
return "$cleanup_code"
|
return "$cleanup_code"
|
||||||
}
|
}
|
||||||
cleanup() {
|
cleanup() {
|
||||||
@@ -164,23 +180,57 @@ trap cleanup EXIT
|
|||||||
trap 'exit 0' INT TERM
|
trap 'exit 0' INT TERM
|
||||||
|
|
||||||
# Never invoke /opt/entrypoint.sh: zapret must start AFTER reconciliation.
|
# Never invoke /opt/entrypoint.sh: zapret must start AFTER reconciliation.
|
||||||
|
dns_alive() {
|
||||||
|
[ "$dns_mode" = native ] || { [ -n "$dns_pid" ] && kill -0 "$dns_pid" 2>/dev/null; }
|
||||||
|
}
|
||||||
|
start_dns() {
|
||||||
|
[ "$dns_mode" = doh ] || return 0
|
||||||
|
exec 9<&-
|
||||||
|
exec 8<"$config_dir"
|
||||||
|
dns_anchor_dir=1
|
||||||
|
# Unknown readiness is evidence, not debris. Never delete it to start.
|
||||||
|
if [ -e "$config_dir/dns-ready" ] || [ -L "$config_dir/dns-ready" ]; then
|
||||||
|
echo 'error: target DNS readiness already exists' >&2
|
||||||
|
return 78
|
||||||
|
fi
|
||||||
|
/usr/local/bin/zapret-autotune dns-forwarder serve &
|
||||||
|
dns_pid=$!
|
||||||
|
dns_deadline=$(($(date +%s) + 5))
|
||||||
|
while :; do
|
||||||
|
dns_alive || { echo 'error: target DNS child exited' >&2; return 75; }
|
||||||
|
if [ -s "$config_dir/dns-ready" ]; then
|
||||||
|
[ -f "$config_dir/dns-ready" ] && [ ! -L "$config_dir/dns-ready" ] &&
|
||||||
|
[ "$(stat -c '%u:%a' "$config_dir/dns-ready")" = "$(id -u):600" ] || return 78
|
||||||
|
# Retain inode/directory anchors in PID 1 even after child SIGKILL.
|
||||||
|
exec 9<"$config_dir/dns-ready"
|
||||||
|
dns_anchor_file=1
|
||||||
|
/usr/local/bin/zapret-autotune dns-forwarder readiness-check "$dns_pid" || return 78
|
||||||
|
dns_alive || return 75
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
[ "$(date +%s)" -lt "$dns_deadline" ] || { echo 'error: target DNS readiness timeout' >&2; return 75; }
|
||||||
|
pause 1
|
||||||
|
done
|
||||||
|
}
|
||||||
run_cycle() {
|
run_cycle() {
|
||||||
|
start_dns || return $?
|
||||||
set -- ss-server -c "$config_dir/server.json"
|
set -- ss-server -c "$config_dir/server.json"
|
||||||
# DNS is not a secret. Native -d avoids assuming support for a JSON DNS key.
|
# DNS is not a secret. Native -d avoids assuming support for a JSON DNS key.
|
||||||
[ -z "${SS_DNS_SERVER-}" ] || set -- "$@" -d "$SS_DNS_SERVER"
|
[ -z "$dns_address" ] || set -- "$@" -d "$dns_address"
|
||||||
"$@" &
|
"$@" 8<&- 9<&- &
|
||||||
server_pid=$!
|
server_pid=$!
|
||||||
ss-local -c "$config_dir/local.json" &
|
ss-local -c "$config_dir/local.json" 8<&- 9<&- &
|
||||||
local_pid=$!
|
local_pid=$!
|
||||||
|
|
||||||
# Bounded readiness, no network probe. Managed commands verify socket ownership.
|
# Bounded readiness, no network probe. Managed commands verify socket ownership.
|
||||||
attempt=0
|
attempt=0
|
||||||
until awk '$2 == "00000000:0438" && $4 == "0A" { found=1 } END { exit !found }' /proc/net/tcp; do
|
until awk '$2 == "00000000:0438" && $4 == "0A" { found=1 } END { exit !found }' /proc/net/tcp; do
|
||||||
{ kill -0 "$server_pid" && kill -0 "$local_pid"; } || return 75
|
{ dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid"; } || return 75
|
||||||
attempt=$((attempt + 1))
|
attempt=$((attempt + 1))
|
||||||
[ "$attempt" -lt 30 ] || { echo 'error: target SOCKS listener timeout' >&2; return 75; }
|
[ "$attempt" -lt 30 ] || { echo 'error: target SOCKS listener timeout' >&2; return 75; }
|
||||||
pause 1
|
pause 1
|
||||||
done
|
done
|
||||||
|
dns_alive || return 75
|
||||||
# Reject unsafe operator requests before init/reconcile can mutate owned state.
|
# Reject unsafe operator requests before init/reconcile can mutate owned state.
|
||||||
timeout -k 5 45 /usr/local/bin/zapret-autotune managed experimental-request-check || exit 78
|
timeout -k 5 45 /usr/local/bin/zapret-autotune managed experimental-request-check || exit 78
|
||||||
if [ ! -e "$ZAPRET_CONFIG" ]; then
|
if [ ! -e "$ZAPRET_CONFIG" ]; then
|
||||||
@@ -201,9 +251,9 @@ fi
|
|||||||
# A replacement SS child can fail during reconciliation/activation. Do not
|
# A replacement SS child can fail during reconciliation/activation. Do not
|
||||||
# fork a monitor we already need to cancel: an early TERM can race interpreter
|
# fork a monitor we already need to cancel: an early TERM can race interpreter
|
||||||
# startup before its cancellation handler exists. Clean this cycle directly.
|
# startup before its cancellation handler exists. Clean this cycle directly.
|
||||||
{ kill -0 "$server_pid" 2>/dev/null && kill -0 "$local_pid" 2>/dev/null; } || return 75
|
{ dns_alive && kill -0 "$server_pid" 2>/dev/null && kill -0 "$local_pid" 2>/dev/null; } || return 75
|
||||||
rm -f "$config_dir/monitor-ready"
|
rm -f "$config_dir/monitor-ready"
|
||||||
setsid /bin/sh /opt/managed-target-supervision.sh --monitor "$config_dir" /usr/local/bin/zapret-autotune managed monitor &
|
setsid /bin/sh /opt/managed-target-supervision.sh --monitor "$config_dir" /usr/local/bin/zapret-autotune managed monitor 8<&- 9<&- &
|
||||||
monitor_pid=$!
|
monitor_pid=$!
|
||||||
monitor_ack_deadline=$(($(date +%s) + 5))
|
monitor_ack_deadline=$(($(date +%s) + 5))
|
||||||
while [ ! -e "$config_dir/monitor-ready" ]; do
|
while [ ! -e "$config_dir/monitor-ready" ]; do
|
||||||
@@ -217,7 +267,7 @@ if proc_identity "$monitor_pid" && [ "$proc_sid" != "$monitor_sid" ]; then
|
|||||||
monitor_sid=
|
monitor_sid=
|
||||||
exit 78
|
exit 78
|
||||||
fi
|
fi
|
||||||
while kill -0 "$server_pid" && kill -0 "$local_pid" && kill -0 "$monitor_pid"; do
|
while dns_alive && kill -0 "$server_pid" && kill -0 "$local_pid" && kill -0 "$monitor_pid"; do
|
||||||
pause 1
|
pause 1
|
||||||
done
|
done
|
||||||
if ! kill -0 "$monitor_pid" 2>/dev/null; then
|
if ! kill -0 "$monitor_pid" 2>/dev/null; then
|
||||||
|
|||||||
Reference in new issue
Block a user