Files
zapret-autotune/cmd/zapret-autotune/resource_check.go
T
ZeusinaandGPT-6 Bot cd05e75189 feat: add typed generic image and MP4 checks
Preserve strict v1 semantics and fingerprints; bind v2 typed expectations to a new proof namespace. Reuse bounded shared collection across checks, monitoring, qualification and apply health. Document prefix-only MP4 and deferred public GIF/plugin/runtime boundaries.

Co-authored-by: GPT-6 <gpt-6@openai.com>
2026-10-15 15:07:54 +03:00

535 lines
17 KiB
Go

package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"mime"
"net"
"net/netip"
"net/url"
"os"
"os/signal"
"regexp"
"sort"
"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"`
IntervalMS *int `json:"interval_ms,omitempty"`
CooldownMS *int `json:"cooldown_ms,omitempty"`
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 && cfg.Version != 2) || len(cfg.Services) == 0 || len(cfg.Services) > 16 {
return fail()
}
if cfg.Version == 2 {
// Presence matters: an omitted minimum must not silently become zero.
var fields struct {
Services []struct {
Resources []struct {
Kind *string `json:"kind"`
Expect struct {
Statuses *[]int `json:"statuses"`
ContentType *string `json:"content_type"`
MinBytes *int `json:"min_bytes"`
MaxBytes *int `json:"max_bytes"`
} `json:"expect"`
} `json:"resources"`
} `json:"services"`
}
if json.Unmarshal(data, &fields) != nil {
return fail()
}
for _, s := range fields.Services {
for _, r := range s.Resources {
if r.Kind != nil && *r.Kind == "" || r.Expect.Statuses == nil || r.Expect.ContentType == nil || *r.Expect.ContentType == "" || r.Expect.MinBytes == nil || r.Expect.MaxBytes == nil {
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]
if cfg.Version == 2 && r.Kind == "" {
r.Kind = "http"
}
total++
if total > 64 || !resourceID.MatchString(r.ID) || ids[r.ID] || !resourceID.MatchString(r.Group) || (r.Kind != "http" && (cfg.Version != 2 || r.Kind != "image" && r.Kind != "mp4_range")) {
return fail()
}
ids[r.ID] = true
for _, scheduling := range []struct {
value **int
fallback int
}{{&r.IntervalMS, 300000}, {&r.CooldownMS, 30000}} {
if *scheduling.value == nil {
v := scheduling.fallback
*scheduling.value = &v
}
if **scheduling.value < 100 || **scheduling.value > 86400000 {
return fail()
}
}
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 r.Kind != "http" {
status := 200
if r.Kind == "mp4_range" {
status = 206
}
if r.Method != "GET" || len(r.Expect.Statuses) != 1 || r.Expect.Statuses[0] != status {
return fail()
}
if r.Kind == "image" {
// ponytail: public GIF awaits an aggregate frame/pixel-bounded decoder;
// legacy image adapters retain their existing GIF contract.
if r.Expect.ContentType != "image/png" && r.Expect.ContentType != "image/jpeg" {
return fail()
}
} else if (r.Expect.ContentType != "video/mp4" && r.Expect.ContentType != "audio/mp4") || r.Expect.MinBytes < 16 || r.Expect.MaxBytes > 65536 {
return fail()
}
}
if len(r.HeadersEnv) > 16 {
return fail()
}
headers := map[string]bool{}
names := make([]string, 0, len(r.HeadersEnv))
for name := range r.HeadersEnv {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
env := r.HeadersEnv[name]
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) {
return readResourceChecksMode(path, lookup, false)
}
func readResourceChecksMode(path string, lookup func(string) (string, bool), owned 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
}
if owned {
st, ok := info.Sys().(*syscall.Stat_t)
if !ok || (st.Uid != 0 && st.Uid != uint32(os.Geteuid())) || st.Nlink != 1 || info.Mode().Perm()&0022 != 0 {
return resourceCheckConfig{}, errResourceConfig
}
}
data, err := io.ReadAll(io.LimitReader(f, (1<<20)+1))
if err != nil {
return resourceCheckConfig{}, errResourceConfig
}
if owned {
current, err := os.Lstat(path)
if err != nil || !os.SameFile(info, current) {
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 {
// Qualification must distinguish infrastructure from verified peer negatives.
// Monitoring retains its existing coarse outcome behavior.
qualification bool
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"`
}
// A synchronous runner: subprocess cancellation joins before this returns.
// The guard is also called after DNS, immediately before the pinned transfer.
func runResourceProtocol(ctx context.Context, service string, r resourceCheckSpec, protocol, server, proxy string, d resourceCheckDeps) (resourceCheckResult, error) {
result := resourceCheckResult{Service: service, Resource: r.ID, Group: r.Group, TLS: protocol, Outcome: "dns_failed"}
if err := d.guard(); err != nil {
return result, qualificationOperation(err, stagePreDNSGuard)
}
probeCtx, cancel := context.WithTimeout(ctx, time.Duration(r.TimeoutMS)*time.Millisecond)
defer cancel()
ips, err := d.resolve(probeCtx, server, r.host)
if d.qualification && err != nil {
var dns *net.DNSError
knownTimeout := probeCtx.Err() != nil && managedRecoveryTimeoutSafe(err)
if !knownTimeout && (!errors.As(err, &dns) || !dns.IsNotFound) {
return result, qualificationOperation(err, stageDNS)
}
}
if d.qualification && err == nil && (len(ips) == 0 || len(ips) > 64) {
return result, qualificationOperation(errResourceConfig, stageDNS)
}
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 err := d.guard(); err != nil {
return result, qualificationOperation(err, stagePostDNSGuard)
}
pin := r.host + ":443:" + net.JoinHostPort(ips[0].String(), "443")
options := resourceTransferOptions{method: r.Method, limit: r.Expect.MaxBytes, timeout: time.Duration(r.TimeoutMS) * time.Millisecond, config: r.privateConfig + "connect-to = " + privateCurlValue(pin) + "\n", expect: &r.Expect}
switch r.Kind {
case "image":
options.response = resourceResponseImage
case "mp4_range":
options.response, options.mediaType = resourceResponseMP4, r.Expect.ContentType
}
transfer, _, err := collectResourceTransferOptions(probeCtx, proxy, r.URL, r.host, protocol, d.runner, options)
if d.qualification && err != nil && err != errResourceExpectation {
knownTimeout := probeCtx.Err() != nil && managedRecoveryTimeoutSafe(err)
if !knownTimeout && !resourceRejectionOnly(err) && !managedAssetTransferNonpass(err) {
return result, qualificationOperation(err, stageTransfer)
}
}
result.Outcome = "transfer_failed"
if err == nil || err == errResourceExpectation {
result.Bytes, result.TimeMS, result.Status = len(transfer.Body), transfer.LatencyMS, transfer.Status
result.Outcome = "expectation_failed"
if err == nil {
result.Outcome = "pass"
}
}
}
}
if probeCtx.Err() != nil {
result.Outcome = "timeout"
if ctx.Err() != nil {
result.Outcome = "cancelled"
}
}
return result, nil
}
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
}
result, err := runResourceProtocol(ctx, s.ID, r, protocol, server, proxy, d)
if err != nil {
fmt.Fprintln(stderr, "blocked: resource check ownership unavailable")
return 2
}
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())
}