feat: add declarative HTTPS resource checks

Add strict check-only resource configuration, owned DNS resolution and pinned HTTPS probes with bounded private output. Document the schema limits and pending remote acceptance; leave generic controller and strategy migration out of scope.

Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
ZeusinaandGPT-6 Bot committed 2026-10-13 18:38:46 +03:00
1 parent 0623fa3323
commit 4d7c4b33c3
9 files changed
+1840 -6

No files matched your search

+427
View File
@@ -0,0 +1,427 @@
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net"
"net/netip"
"net/url"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
)
var errResourceConfig = errors.New("invalid resource configuration")
var resourceID = regexp.MustCompile(`^[a-z][a-z0-9_-]{0,47}$`)
var resourceEnv = regexp.MustCompile(`^[A-Z_][A-Z0-9_]{0,127}$`)
var resourceHeader = regexp.MustCompile("^[!#$%&'*+.^_`|~0-9A-Za-z-]{1,64}$")
var resourceNumericHost = regexp.MustCompile(`(?i)^(?:[0-9]+|0x[0-9a-f]+)(?:\.(?:[0-9]+|0x[0-9a-f]+))*$`)
type resourceCheckConfig struct {
Version int `json:"version"`
Services []resourceCheckService `json:"services"`
}
type resourceCheckService struct {
ID string `json:"id"`
Resources []resourceCheckSpec `json:"resources"`
}
type resourceCheckSpec struct {
ID string `json:"id"`
Group string `json:"group"`
Kind string `json:"kind"`
URL string `json:"url,omitempty"`
URLEnv string `json:"url_env,omitempty"`
Method string `json:"method"`
TLS string `json:"tls"`
TimeoutMS int `json:"timeout_ms"`
Expect resourceCheckExpect `json:"expect"`
HeadersEnv map[string]string `json:"headers_env,omitempty"`
AllowPrivate bool `json:"allow_private,omitempty"`
AllowedNetworks []string `json:"allowed_networks,omitempty"`
host, privateConfig string
networks []netip.Prefix
}
type resourceCheckExpect struct {
Statuses []int `json:"statuses"`
ContentType string `json:"content_type,omitempty"`
MinBytes int `json:"min_bytes"`
MaxBytes int `json:"max_bytes"`
}
// encoding/json otherwise silently accepts duplicate keys and null scalars.
func uniqueResourceJSON(d *json.Decoder, depth int) error {
if depth > 16 {
return errResourceConfig
}
t, err := d.Token()
if err != nil || t == nil {
return errResourceConfig
}
if delim, ok := t.(json.Delim); ok {
switch delim {
case '{':
seen := map[string]bool{}
for d.More() {
key, err := d.Token()
if err != nil {
return errResourceConfig
}
s, ok := key.(string)
if !ok || seen[s] {
return errResourceConfig
}
seen[s] = true
if err := uniqueResourceJSON(d, depth+1); err != nil {
return err
}
}
case '[':
for d.More() {
if err := uniqueResourceJSON(d, depth+1); err != nil {
return err
}
}
default:
return errResourceConfig
}
if _, err := d.Token(); err != nil {
return errResourceConfig
}
}
return nil
}
func privateCurlValue(v string) string {
return "\"" + strings.NewReplacer("\\", "\\\\", "\"", "\\\"").Replace(v) + "\""
}
func safeResourceValue(v string) bool {
return len(v) <= 16<<10 && strings.IndexFunc(v, func(c rune) bool { return c < 32 || c == 127 }) < 0
}
func parseResourceChecks(data []byte, lookup func(string) (string, bool)) (resourceCheckConfig, error) {
var cfg resourceCheckConfig
fail := func() (resourceCheckConfig, error) { return resourceCheckConfig{}, errResourceConfig }
if len(data) > 1<<20 || lookup == nil {
return fail()
}
d := json.NewDecoder(bytes.NewReader(data))
if uniqueResourceJSON(d, 0) != nil {
return fail()
}
if _, err := d.Token(); err != io.EOF {
return fail()
}
d = json.NewDecoder(bytes.NewReader(data))
d.DisallowUnknownFields()
if d.Decode(&cfg) != nil || cfg.Version != 1 || len(cfg.Services) == 0 || len(cfg.Services) > 16 {
return fail()
}
services, total := map[string]bool{}, 0
for si := range cfg.Services {
s := &cfg.Services[si]
if !resourceID.MatchString(s.ID) || services[s.ID] || len(s.Resources) == 0 {
return fail()
}
services[s.ID] = true
ids := map[string]bool{}
for ri := range s.Resources {
r := &s.Resources[ri]
total++
if total > 64 || !resourceID.MatchString(r.ID) || ids[r.ID] || !resourceID.MatchString(r.Group) || r.Kind != "http" {
return fail()
}
ids[r.ID] = true
if (r.URL == "") == (r.URLEnv == "") {
return fail()
}
if r.URLEnv != "" {
if !resourceEnv.MatchString(r.URLEnv) {
return fail()
}
var ok bool
r.URL, ok = lookup(r.URLEnv)
if !ok || r.URL == "" {
return fail()
}
}
u, err := url.Parse(r.URL)
if err != nil {
return fail()
}
r.host = u.Hostname()
// curl normalizes legacy IPv4 spellings before matching --connect-to.
// Only canonical literals may bypass DNS without defeating that match.
if resourceNumericHost.MatchString(r.host) {
ip := net.ParseIP(r.host)
if ip == nil || ip.To4() == nil || ip.String() != r.host {
return fail()
}
}
if _, err := privateResourceURL(r.URL, r.host); err != nil {
return fail()
}
// Reject encoded controls too, without ever displaying the decoded path.
decoded, err := url.PathUnescape(r.URL)
if err != nil || !safeResourceValue(decoded) {
return fail()
}
if r.Method != "GET" && r.Method != "HEAD" {
return fail()
}
if r.TLS != "tls12" && r.TLS != "tls13" && r.TLS != "both" {
return fail()
}
if r.TimeoutMS < 100 || r.TimeoutMS > 30000 || r.Expect.MaxBytes < 1 || r.Expect.MaxBytes > 5<<20 || r.Expect.MinBytes < 0 || r.Expect.MinBytes > r.Expect.MaxBytes || (r.Method == "HEAD" && r.Expect.MinBytes != 0) {
return fail()
}
if len(r.Expect.Statuses) == 0 || len(r.Expect.Statuses) > 16 {
return fail()
}
statuses := map[int]bool{}
for _, status := range r.Expect.Statuses {
if status < 200 || status > 599 || status >= 300 && status < 400 || statuses[status] {
return fail()
}
statuses[status] = true
}
if r.Expect.ContentType != "" {
mt, params, err := mime.ParseMediaType(r.Expect.ContentType)
if err != nil || len(params) != 0 || mt != r.Expect.ContentType {
return fail()
}
}
if len(r.HeadersEnv) > 16 {
return fail()
}
headers := map[string]bool{}
for name, env := range r.HeadersEnv {
lower := strings.ToLower(name)
if !resourceHeader.MatchString(name) || headers[lower] || !resourceEnv.MatchString(env) {
return fail()
}
headers[lower] = true
switch lower {
case "host", "connection", "transfer-encoding", "content-length", "expect", "upgrade", "proxy-authorization", "proxy-connection", "te", "trailer", "range", "accept-encoding":
return fail()
}
value, ok := lookup(env)
if !ok || value == "" || !safeResourceValue(value) {
return fail()
}
r.privateConfig += "header = " + privateCurlValue(name+": "+value) + "\n"
}
if len(r.AllowedNetworks) > 16 || r.AllowPrivate && len(r.AllowedNetworks) == 0 {
return fail()
}
for _, raw := range r.AllowedNetworks {
prefix, err := netip.ParsePrefix(raw)
if err != nil || prefix != prefix.Masked() || prefix.Addr().Is4In6() {
return fail()
}
r.networks = append(r.networks, prefix)
}
}
}
return cfg, nil
}
func readResourceChecks(path string, lookup func(string) (string, bool)) (resourceCheckConfig, error) {
fd, err := syscall.Open(path, syscall.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK|syscall.O_CLOEXEC, 0)
if err != nil {
return resourceCheckConfig{}, errResourceConfig
}
f := os.NewFile(uintptr(fd), "resource config")
defer f.Close()
info, err := f.Stat()
if err != nil || !info.Mode().IsRegular() || info.Size() > 1<<20 {
return resourceCheckConfig{}, errResourceConfig
}
data, err := io.ReadAll(io.LimitReader(f, (1<<20)+1))
if err != nil {
return resourceCheckConfig{}, errResourceConfig
}
return parseResourceChecks(data, lookup)
}
var resourceDeniedNetworks = []string{"0.0.0.0/8", "100.64.0.0/10", "192.0.0.0/24", "192.0.2.0/24", "198.18.0.0/15", "198.51.100.0/24", "203.0.113.0/24", "240.0.0.0/4", "2001:db8::/32", "2001::/23", "2002::/16", "64:ff9b::/96", "64:ff9b:1::/48", "100::/64"}
func approvedResourceIP(r resourceCheckSpec, ip net.IP) bool {
a, ok := netip.AddrFromSlice(ip)
if !ok {
return false
}
a = a.Unmap()
// Link-local and metadata are never enabled by allow_private.
if !a.IsGlobalUnicast() || a.IsLoopback() || a.IsLinkLocalUnicast() || a.IsLinkLocalMulticast() || a.String() == "100.100.100.200" || a.String() == "fd00:ec2::254" || a.String() == "168.63.129.16" {
return false
}
for _, raw := range resourceDeniedNetworks {
p := netip.MustParsePrefix(raw)
if p.Contains(a) {
return false
}
}
if a.Is6() && !a.IsPrivate() && !netip.MustParsePrefix("2000::/3").Contains(a) {
return false
}
if a.IsPrivate() || a.IsLoopback() {
if !r.AllowPrivate {
return false
}
}
if len(r.networks) != 0 {
for _, p := range r.networks {
if p.Contains(a) {
return true
}
}
return false
}
return true
}
type resourceCheckDeps struct {
lookup func(string) (string, bool)
guard func() error
resolve func(context.Context, string, string) ([]net.IP, error)
runner subprocessRunner
}
func resolveResourceIPs(ctx context.Context, server, host string) ([]net.IP, error) {
return resolveResourceIPsWithDial(ctx, server, host, (&net.Dialer{}).DialContext)
}
func productionResourceCheckDeps() resourceCheckDeps {
d := managedDependencies(runPlayerCurl)
return resourceCheckDeps{lookup: os.LookupEnv, guard: func() error {
if _, err := managedRuntimeGuard(d); err != nil {
return err
}
// The provisioned local.json and owned SS chain use the target's fixed
// internal port, not an additional ss-local listener in this namespace.
if port, _ := d.lookup("SOCKS_PORT"); port != "1080" {
return errDNSForwarder
}
cfg, err := parseManagedDNSConfig(d.lookup)
if err != nil {
return err
}
return verifyResourceDNSRuntime(cfg)
}, resolve: resolveResourceIPs, runner: runResourceCurl}
}
type resourceCheckResult struct {
Service string `json:"service"`
Resource string `json:"resource"`
Group string `json:"group"`
Outcome string `json:"outcome"`
TLS string `json:"tls"`
Bytes int `json:"bytes"`
TimeMS float64 `json:"time_ms"`
Status int `json:"status"`
}
func resourceCheckCLI(ctx context.Context, path string, stdout, stderr io.Writer, d resourceCheckDeps) int {
if d.lookup == nil {
fmt.Fprintln(stderr, "configerror: invalid resource check configuration")
return 2
}
cfg, err := readResourceChecks(path, d.lookup)
dns, dnsErr := parseManagedDNSConfig(d.lookup)
proxy, _ := d.lookup("ZAPRET_AUTOTUNE_SOCKS_PROXY")
proxy, proxyErr := managedProbeProxy(proxy)
if err != nil || dnsErr != nil || proxyErr != nil || d.guard == nil || d.resolve == nil || d.runner == nil || (dns.Mode == "native" && dns.NativeServer == "") {
fmt.Fprintln(stderr, "configerror: invalid resource check configuration")
return 2
}
server := dns.serverAddress()
if dns.Mode == "native" {
server = net.JoinHostPort(server, "53")
}
code := 0
for _, s := range cfg.Services {
for _, r := range s.Resources {
protocols := []string{r.TLS}
if r.TLS == "both" {
protocols = []string{"tls12", "tls13"}
}
for _, protocol := range protocols {
if ctx.Err() != nil {
return 1
}
if d.guard() != nil {
fmt.Fprintln(stderr, "blocked: resource check ownership unavailable")
return 2
}
result := resourceCheckResult{Service: s.ID, Resource: r.ID, Group: r.Group, TLS: protocol, Outcome: "dns_failed"}
probeCtx, cancel := context.WithTimeout(ctx, time.Duration(r.TimeoutMS)*time.Millisecond)
ips, err := d.resolve(probeCtx, server, r.host)
if err == nil && len(ips) > 0 && len(ips) <= 64 {
approved := true
for _, ip := range ips {
if !approvedResourceIP(r, ip) {
approved = false
}
}
result.Outcome = "destination_denied"
if approved {
if d.guard() != nil {
cancel()
fmt.Fprintln(stderr, "blocked: resource check ownership unavailable")
return 2
}
// --connect-to changes the SOCKS CONNECT destination even for socks5h;
// URL authority, HTTP Host, SNI and certificate verification stay original.
pin := r.host + ":443:" + net.JoinHostPort(ips[0].String(), "443")
options := resourceTransferOptions{generic: true, method: r.Method, limit: r.Expect.MaxBytes, timeout: time.Duration(r.TimeoutMS) * time.Millisecond, config: r.privateConfig + "connect-to = " + privateCurlValue(pin) + "\n"}
transfer, _, err := collectResourceTransferOptions(probeCtx, proxy, r.URL, r.host, protocol, false, d.runner, options)
result.Outcome = "transfer_failed"
if err == nil {
result.Bytes, result.TimeMS, result.Status = len(transfer.Body), transfer.LatencyMS, transfer.Status
result.Outcome = "expectation_failed"
statusOK := false
for _, status := range r.Expect.Statuses {
if status == transfer.Status {
statusOK = true
}
}
if statusOK {
if len(transfer.Body) >= r.Expect.MinBytes && (r.Expect.ContentType == "" || transfer.ContentType == r.Expect.ContentType) {
result.Outcome = "pass"
}
}
}
}
}
if probeCtx.Err() != nil {
result.Outcome = "timeout"
if ctx.Err() != nil {
result.Outcome = "cancelled"
}
}
cancel()
if result.Outcome != "pass" {
code = 1
}
if json.NewEncoder(stdout).Encode(result) != nil {
return 1
}
}
}
}
return code
}
func configuredResourceCheck(path string, stdout, stderr io.Writer) int {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
return resourceCheckCLI(ctx, path, stdout, stderr, productionResourceCheckDeps())
}