mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 20:01:51 +08:00
- Implement full-traffic transparent gateway (cmd/artex-gateway, traffic/netstack.go, traffic/transparent.go) using Linux TUN + gVisor netstack deferred TCP handshakes (eliminating nmap/nc false-positive open ports), real ICMPv4 Echo proxying, UDP/DNS interception, and per-container upstream proxy overrides. - Add egress blacklist + asset-intercept sync, non-HTTP flow/DNS SQLite persistence and APIs, and /system/proxy & /function/traffic TCP/UDP/DNS UI panels. - Fix sandbox Playwright MCP browser loopback --proxy-server rewrite and NODE_EXTRA_CA_CERTS override in server/mcpstdio.go. - Strip all SUID/SGID binaries in Dockerfile.sandbox, grant minimal cap_net_raw+ep to nmap/nping/ping with ARTEX_SANDBOX_ALLOW_NET_RAW toggle, and enforce IPv4/IPv6 anti-spoofing in iptables and netstack. - Remove hardcoded Chinese "(全局)" from GET /api/proxy/routes, localize ContainerRoutesTab / NonHTTPTrafficPanel labels, and expose reactive locale/isEn from useLiveTranslation for React Compiler compatibility.
254 lines
9.0 KiB
Go
254 lines
9.0 KiB
Go
//go:build linux
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os/exec"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
chainNAT = "ARTEX_GW_NAT"
|
|
chainMangle = "ARTEX_GW_MANGLE"
|
|
chainFwd = "ARTEX_GW_FWD"
|
|
chainInput = "ARTEX_GW_INPUT"
|
|
tproxyMark = "0x218"
|
|
tproxyTable = "218"
|
|
)
|
|
|
|
type iptablesConfig struct {
|
|
Subnet string
|
|
GatewayIP string
|
|
TCPPort int
|
|
UDPPort int
|
|
DNSPort int
|
|
ProxyPort int // explicit MITM proxy port (8788) allowed direct to artex
|
|
AdminPort int // admin UI/API port (8787) explicitly rejected from sandbox
|
|
BridgeName string
|
|
}
|
|
|
|
// discoverSandboxBridge finds the Linux bridge interface owning gatewayIP or
|
|
// subnet (e.g. br-xxxxxxxxxxxx for artex-sandbox).
|
|
func discoverSandboxBridge(subnet, gatewayIP string, wait time.Duration) (string, error) {
|
|
_, cidr, _ := net.ParseCIDR(strings.TrimSpace(subnet))
|
|
gwIP := net.ParseIP(strings.TrimSpace(gatewayIP))
|
|
if gwIP != nil {
|
|
gwIP = gwIP.To4()
|
|
}
|
|
|
|
deadline := time.Now().Add(wait)
|
|
for {
|
|
ifaces, err := net.Interfaces()
|
|
if err == nil {
|
|
for _, iface := range ifaces {
|
|
addrs, aerr := iface.Addrs()
|
|
if aerr != nil {
|
|
continue
|
|
}
|
|
for _, a := range addrs {
|
|
ipnet, ok := a.(*net.IPNet)
|
|
if !ok || ipnet.IP == nil {
|
|
continue
|
|
}
|
|
v4 := ipnet.IP.To4()
|
|
if v4 == nil {
|
|
continue
|
|
}
|
|
if gwIP != nil && v4.Equal(gwIP) {
|
|
return iface.Name, nil
|
|
}
|
|
if cidr != nil && cidr.Contains(v4) {
|
|
return iface.Name, nil
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if time.Now().After(deadline) {
|
|
break
|
|
}
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
return "", fmt.Errorf("sandbox bridge interface not found for subnet=%q gateway=%q", subnet, gatewayIP)
|
|
}
|
|
|
|
func runCmd(name string, args ...string) error {
|
|
out, err := exec.Command(name, args...).CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("%s %s: %w (%s)", name, strings.Join(args, " "), err, strings.TrimSpace(string(out)))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func runCmdIgnoreErr(name string, args ...string) {
|
|
_ = exec.Command(name, args...).Run()
|
|
}
|
|
|
|
func ensureJump(table, parent, chain string, matchArgs ...string) error {
|
|
checkArgs := append([]string{"-t", table, "-C", parent}, matchArgs...)
|
|
checkArgs = append(checkArgs, "-j", chain)
|
|
if exec.Command("iptables", checkArgs...).Run() == nil {
|
|
return nil
|
|
}
|
|
insArgs := append([]string{"-t", table, "-I", parent, "1"}, matchArgs...)
|
|
insArgs = append(insArgs, "-j", chain)
|
|
return runCmd("iptables", insArgs...)
|
|
}
|
|
|
|
func ensureChain(table, chain string) error {
|
|
_ = exec.Command("iptables", "-t", table, "-N", chain).Run()
|
|
return runCmd("iptables", "-t", table, "-F", chain)
|
|
}
|
|
|
|
// applyIPTables installs transparent TCP/DNS/UDP interception and anti-bypass
|
|
// egress lockdown rules scoped strictly to the sandbox bridge interface.
|
|
func applyIPTables(cfg iptablesConfig) error {
|
|
br := cfg.BridgeName
|
|
if br == "" {
|
|
return fmt.Errorf("bridge name is empty")
|
|
}
|
|
subnet := strings.TrimSpace(cfg.Subnet)
|
|
if subnet == "" {
|
|
subnet = "172.30.0.0/16"
|
|
}
|
|
|
|
// 1. Policy routing for UDP TPROXY (fwmark 0x218 -> local table 218).
|
|
if cfg.UDPPort > 0 {
|
|
runCmdIgnoreErr("ip", "rule", "del", "fwmark", tproxyMark, "lookup", tproxyTable)
|
|
if err := runCmd("ip", "rule", "add", "fwmark", tproxyMark, "lookup", tproxyTable); err != nil {
|
|
log.Printf("[gateway] warn: ip rule for UDP TPROXY failed (TCP/DNS still active): %v", err)
|
|
} else {
|
|
if err := runCmd("ip", "route", "replace", "local", "0.0.0.0/0", "dev", "lo", "table", tproxyTable); err != nil {
|
|
log.Printf("[gateway] warn: ip route local for UDP TPROXY failed: %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 2. NAT PREROUTING: redirect ALL DNS (UDP/TCP 53) and ALL non-sandbox TCP to gateway.
|
|
if err := ensureChain("nat", chainNAT); err != nil {
|
|
return err
|
|
}
|
|
// Allow explicit HTTP MITM proxy traffic straight to ARTEX (:8788) inside the sandbox subnet
|
|
// so Phase 2 Proxy-Authorization tags and SNAT-free attribution keep working.
|
|
if cfg.ProxyPort > 0 {
|
|
if err := runCmd("iptables", "-t", "nat", "-A", chainNAT,
|
|
"-d", subnet, "-p", "tcp", "--dport", strconv.Itoa(cfg.ProxyPort), "-j", "RETURN"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// Never redirect :8787 admin traffic (explicitly rejected by INPUT / DOCKER-USER).
|
|
if cfg.AdminPort > 0 {
|
|
if err := runCmd("iptables", "-t", "nat", "-A", chainNAT,
|
|
"-d", subnet, "-p", "tcp", "--dport", strconv.Itoa(cfg.AdminPort), "-j", "RETURN"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// Intercept ALL DNS queries (both to bridge gateway and to external DNS like 8.8.8.8:53).
|
|
if cfg.DNSPort > 0 {
|
|
if err := runCmd("iptables", "-t", "nat", "-A", chainNAT,
|
|
"-p", "udp", "--dport", "53", "-j", "REDIRECT", "--to-ports", strconv.Itoa(cfg.DNSPort)); err != nil {
|
|
return err
|
|
}
|
|
if err := runCmd("iptables", "-t", "nat", "-A", chainNAT,
|
|
"-p", "tcp", "--dport", "53", "-j", "REDIRECT", "--to-ports", strconv.Itoa(cfg.DNSPort)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// Intercept ALL external TCP traffic (HTTP, HTTPS, SSH, MySQL, Redis, nmap -sT, nc, raw sockets).
|
|
if cfg.TCPPort > 0 {
|
|
if err := runCmd("iptables", "-t", "nat", "-A", chainNAT,
|
|
"-p", "tcp", "!", "-d", subnet, "-j", "REDIRECT", "--to-ports", strconv.Itoa(cfg.TCPPort)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := ensureJump("nat", "PREROUTING", chainNAT, "-i", br); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 3. MANGLE PREROUTING: TPROXY all non-DNS external UDP packets to gateway UDP listener.
|
|
if cfg.UDPPort > 0 {
|
|
if err := ensureChain("mangle", chainMangle); err != nil {
|
|
log.Printf("[gateway] warn: create mangle chain failed: %v", err)
|
|
} else {
|
|
_ = runCmd("iptables", "-t", "mangle", "-A", chainMangle, "-d", subnet, "-j", "RETURN")
|
|
_ = runCmd("iptables", "-t", "mangle", "-A", chainMangle, "-p", "udp", "--dport", "53", "-j", "RETURN")
|
|
if err := runCmd("iptables", "-t", "mangle", "-A", chainMangle,
|
|
"-p", "udp", "-j", "TPROXY", "--on-port", strconv.Itoa(cfg.UDPPort),
|
|
"--on-ip", "127.0.0.1", "--tproxy-mark", tproxyMark+"/"+tproxyMark); err != nil {
|
|
log.Printf("[gateway] warn: UDP TPROXY rule failed (TCP/DNS unaffected): %v", err)
|
|
} else {
|
|
_ = ensureJump("mangle", "PREROUTING", chainMangle, "-i", br)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 4. INPUT: allow redirected gateway listeners on sandbox bridge, reject :8787 admin port.
|
|
if err := ensureChain("filter", chainInput); err != nil {
|
|
return err
|
|
}
|
|
if cfg.AdminPort > 0 {
|
|
_ = runCmd("iptables", "-t", "filter", "-A", chainInput, "-p", "tcp", "--dport", strconv.Itoa(cfg.AdminPort), "-j", "REJECT")
|
|
}
|
|
if cfg.TCPPort > 0 {
|
|
_ = runCmd("iptables", "-t", "filter", "-A", chainInput, "-p", "tcp", "--dport", strconv.Itoa(cfg.TCPPort), "-j", "ACCEPT")
|
|
}
|
|
if cfg.DNSPort > 0 {
|
|
_ = runCmd("iptables", "-t", "filter", "-A", chainInput, "-p", "udp", "--dport", strconv.Itoa(cfg.DNSPort), "-j", "ACCEPT")
|
|
_ = runCmd("iptables", "-t", "filter", "-A", chainInput, "-p", "tcp", "--dport", strconv.Itoa(cfg.DNSPort), "-j", "ACCEPT")
|
|
}
|
|
if cfg.UDPPort > 0 {
|
|
_ = runCmd("iptables", "-t", "filter", "-A", chainInput, "-p", "udp", "--dport", strconv.Itoa(cfg.UDPPort), "-j", "ACCEPT")
|
|
}
|
|
if err := ensureJump("filter", "INPUT", chainInput, "-i", br); err != nil {
|
|
return err
|
|
}
|
|
|
|
// 5. DOCKER-USER: hard anti-bypass kill switch. Any packet from the sandbox bridge
|
|
// trying to leave to another interface (direct NAT egress, artex-internal, metadata)
|
|
// is REJECTED so sandbox containers can NEVER bypass the transparent gateway.
|
|
if err := ensureChain("filter", chainFwd); err != nil {
|
|
return err
|
|
}
|
|
_ = runCmd("iptables", "-t", "filter", "-A", chainFwd, "-d", "169.254.169.254/32", "-j", "REJECT")
|
|
if cfg.AdminPort > 0 {
|
|
_ = runCmd("iptables", "-t", "filter", "-A", chainFwd, "-p", "tcp", "--dport", strconv.Itoa(cfg.AdminPort), "-j", "REJECT")
|
|
}
|
|
// Reject any packet leaving the sandbox bridge to the outside / other Docker networks.
|
|
if err := runCmd("iptables", "-t", "filter", "-A", chainFwd, "!", "-o", br, "-j", "REJECT"); err != nil {
|
|
return err
|
|
}
|
|
if err := ensureJump("filter", "DOCKER-USER", chainFwd, "-i", br); err != nil {
|
|
log.Printf("[gateway] warn: attach DOCKER-USER failed (chain may not exist yet): %v", err)
|
|
}
|
|
|
|
log.Printf("[gateway] iptables transparent interception active on bridge=%s subnet=%s tcp=:%d dns=:%d udp=:%d",
|
|
br, subnet, cfg.TCPPort, cfg.DNSPort, cfg.UDPPort)
|
|
return nil
|
|
}
|
|
|
|
// cleanupIPTables removes all gateway-owned iptables jumps and chains on shutdown.
|
|
func cleanupIPTables(cfg iptablesConfig) {
|
|
br := cfg.BridgeName
|
|
if br != "" {
|
|
runCmdIgnoreErr("iptables", "-t", "nat", "-D", "PREROUTING", "-i", br, "-j", chainNAT)
|
|
runCmdIgnoreErr("iptables", "-t", "mangle", "-D", "PREROUTING", "-i", br, "-j", chainMangle)
|
|
runCmdIgnoreErr("iptables", "-t", "filter", "-D", "INPUT", "-i", br, "-j", chainInput)
|
|
runCmdIgnoreErr("iptables", "-t", "filter", "-D", "DOCKER-USER", "-i", br, "-j", chainFwd)
|
|
}
|
|
for _, tc := range [][2]string{
|
|
{"nat", chainNAT},
|
|
{"mangle", chainMangle},
|
|
{"filter", chainInput},
|
|
{"filter", chainFwd},
|
|
} {
|
|
runCmdIgnoreErr("iptables", "-t", tc[0], "-F", tc[1])
|
|
runCmdIgnoreErr("iptables", "-t", tc[0], "-X", tc[1])
|
|
}
|
|
runCmdIgnoreErr("ip", "rule", "del", "fwmark", tproxyMark, "lookup", tproxyTable)
|
|
runCmdIgnoreErr("ip", "route", "flush", "table", tproxyTable)
|
|
}
|