mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 20:01:51 +08:00
feat(deploy,gateway): add dual-edition deployment/migration/rollback and TUN route reconciliation
- Add standalone edition (`Dockerfile.standalone`, `docker-compose.standalone.yml`) alongside sandbox edition and publish both in `.github/workflows/release.yml`. - Implement `scripts/deploy-lib.sh`, `install.sh`, `update.sh`, and `scripts/artex-sandbox-firewall.sh --cleanup` with edition detection, legacy standalone -> sandbox migration (symlink-safe workspace permissions), automatic snapshots with pinned `artex-rollback/*` images + PG dump, auto/manual cross-edition rollback, and post-rollback upstream unpinning. - Update `ensureWritableDir` in `internal/sandbox/manager.go` to safely migrate nested root-owned workspace files without following symlinks. - Expose `deploy_edition` in `/api/update/check` and update `UpdateCard` UI/i18n warnings for sandbox vs standalone in-app update/rollback. - Reconcile `artex-gateway` TUN policy routing (`priority 218`, `raw PREROUTING`, `nat POSTROUTING`, `DOCKER-USER`) every 15s to self-heal after host network rule flushes or bridge changes. - Add unit and shell tests (`scripts/deploy_test.sh`, `cmd/artex-gateway/tun_linux_test.go`, `internal/sandbox/sandbox_test.go`, `server/update_test.go`) and update `README.md` / `.env.example`.
This commit is contained in:
1 parent
d786de90af
commit
07309e4530
23 files changed
+2107
-144
No files matched your search
+18
-10
@@ -237,20 +237,20 @@ func main() {
|
||||
}
|
||||
|
||||
if applyRules {
|
||||
iptCfg = iptablesConfig{
|
||||
Subnet: subnet,
|
||||
GatewayIP: gatewayIP,
|
||||
TCPPort: tcpPort,
|
||||
UDPPort: udpPort,
|
||||
DNSPort: dnsPort,
|
||||
ProxyPort: proxyPort,
|
||||
AdminPort: adminPort,
|
||||
}
|
||||
br, err := discoverSandboxBridge(subnet, gatewayIP, 30*time.Second)
|
||||
if err != nil {
|
||||
log.Printf("[gateway] warn: %v (listeners running; check ARTEX_SANDBOX_SUBNET)", err)
|
||||
} else {
|
||||
iptCfg = iptablesConfig{
|
||||
Subnet: subnet,
|
||||
GatewayIP: gatewayIP,
|
||||
TCPPort: tcpPort,
|
||||
UDPPort: udpPort,
|
||||
DNSPort: dnsPort,
|
||||
ProxyPort: proxyPort,
|
||||
AdminPort: adminPort,
|
||||
BridgeName: br,
|
||||
}
|
||||
iptCfg.BridgeName = br
|
||||
if tunModeActive {
|
||||
if err := applyTunRouting(iptCfg); err != nil {
|
||||
log.Printf("[gateway] warn: apply TUN routing failed: %v", err)
|
||||
@@ -276,8 +276,10 @@ func main() {
|
||||
|
||||
policyTicker := time.NewTicker(2 * time.Second)
|
||||
flushTicker := time.NewTicker(500 * time.Millisecond)
|
||||
routeTicker := time.NewTicker(15 * time.Second)
|
||||
defer policyTicker.Stop()
|
||||
defer flushTicker.Stop()
|
||||
defer routeTicker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
@@ -302,6 +304,12 @@ func main() {
|
||||
syncPolicy()
|
||||
case <-flushTicker.C:
|
||||
flushEvents()
|
||||
case <-routeTicker.C:
|
||||
if applyRules && tunModeActive {
|
||||
if reconcileTunRouting(&iptCfg) {
|
||||
iptablesApplied = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
@@ -194,6 +195,93 @@ func applyTunRouting(cfg iptablesConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// rawPreroutingTopAcceptsTun reports whether the first rule in raw PREROUTING
|
||||
// (immediately after the chain policy) accepts inbound artex-tun0 packets for subnet.
|
||||
func rawPreroutingTopAcceptsTun(rawRules, tunDev, subnet string) bool {
|
||||
for _, line := range strings.Split(rawRules, "\n") {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "-P ") || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
return strings.HasPrefix(line, "-A PREROUTING ") &&
|
||||
strings.Contains(line, "-i "+tunDev+" ") &&
|
||||
strings.Contains(line, "-d "+subnet+" ") &&
|
||||
strings.HasSuffix(line, "-j ACCEPT")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// reconcileTunRouting verifies policy routing and critical anti-drop iptables
|
||||
// rules stay intact while the gateway is running (guarding against systemd-networkd
|
||||
// / netplan rule flushes or Docker inserting raw PREROUTING DROP rules above us).
|
||||
// Returns true when routing is active for cfg.BridgeName.
|
||||
func reconcileTunRouting(cfg *iptablesConfig) bool {
|
||||
if cfg == nil {
|
||||
return false
|
||||
}
|
||||
subnet := strings.TrimSpace(cfg.Subnet)
|
||||
if subnet == "" {
|
||||
subnet = "172.30.0.0/16"
|
||||
}
|
||||
gwIP := strings.TrimSpace(cfg.GatewayIP)
|
||||
if gwIP == "" {
|
||||
gwIP = "172.30.0.1"
|
||||
}
|
||||
if curBr, err := discoverSandboxBridge(subnet, gwIP, 2*time.Second); err == nil && curBr != "" && curBr != cfg.BridgeName {
|
||||
log.Printf("[gateway] sandbox bridge changed %q -> %q; reapplying TUN routing", cfg.BridgeName, curBr)
|
||||
if cfg.BridgeName != "" {
|
||||
cleanupTunRouting(*cfg)
|
||||
}
|
||||
cfg.BridgeName = curBr
|
||||
if err := applyTunRouting(*cfg); err != nil {
|
||||
log.Printf("[gateway] warn: reapply TUN routing failed: %v", err)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
br := cfg.BridgeName
|
||||
if br == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
// 1. Ensure policy rule priority 218 and route table 218 still exist.
|
||||
ruleOut, _ := exec.Command("ip", "rule", "show", "priority", tunRulePrio).Output()
|
||||
if !strings.Contains(string(ruleOut), "iif "+br) || !strings.Contains(string(ruleOut), "lookup "+tunRouteTable) {
|
||||
_ = runCmd("ip", "link", "set", "dev", tunDeviceName, "mtu", "1500", "up")
|
||||
_ = runCmd("ip", "addr", "replace", tunLinkIP, "dev", tunDeviceName)
|
||||
_ = os.WriteFile("/proc/sys/net/ipv4/conf/"+tunDeviceName+"/rp_filter", []byte("0\n"), 0o644)
|
||||
_ = os.WriteFile("/proc/sys/net/ipv4/conf/"+br+"/rp_filter", []byte("0\n"), 0o644)
|
||||
_ = runCmd("ip", "route", "replace", subnet, "dev", br, "table", tunRouteTable)
|
||||
_ = runCmd("ip", "route", "replace", "default", "dev", tunDeviceName, "table", tunRouteTable)
|
||||
runCmdIgnoreErr("ip", "rule", "del", "iif", br, "lookup", tunRouteTable, "priority", tunRulePrio)
|
||||
if err := runCmd("ip", "rule", "add", "iif", br, "lookup", tunRouteTable, "priority", tunRulePrio); err != nil {
|
||||
log.Printf("[gateway] warn: restore ip rule %s failed: %v", tunRulePrio, err)
|
||||
} else {
|
||||
log.Printf("[gateway] restored missing policy routing rule priority=%s bridge=%s", tunRulePrio, br)
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Ensure raw PREROUTING top rule still accepts artex-tun0 -> subnet replies.
|
||||
rawOut, err := exec.Command("iptables", "-t", "raw", "-S", "PREROUTING").Output()
|
||||
if err == nil && !rawPreroutingTopAcceptsTun(string(rawOut), tunDeviceName, subnet) {
|
||||
runCmdIgnoreErr("iptables", "-t", "raw", "-D", "PREROUTING", "-i", tunDeviceName, "-d", subnet, "-j", "ACCEPT")
|
||||
if err := runCmd("iptables", "-t", "raw", "-I", "PREROUTING", "1", "-i", tunDeviceName, "-d", subnet, "-j", "ACCEPT"); err != nil {
|
||||
log.Printf("[gateway] warn: restore raw PREROUTING rule failed: %v", err)
|
||||
} else {
|
||||
log.Printf("[gateway] restored raw PREROUTING top ACCEPT for %s -> %s", tunDeviceName, subnet)
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Ensure nat POSTROUTING exemption and DOCKER-USER reply accept exist.
|
||||
if exec.Command("iptables", "-t", "nat", "-C", "POSTROUTING", "-o", tunDeviceName, "-j", "ACCEPT").Run() != nil {
|
||||
_ = runCmd("iptables", "-t", "nat", "-I", "POSTROUTING", "1", "-o", tunDeviceName, "-j", "ACCEPT")
|
||||
}
|
||||
if exec.Command("iptables", "-t", "filter", "-C", "DOCKER-USER", "-i", tunDeviceName, "-o", br, "-j", "ACCEPT").Run() != nil {
|
||||
_ = runCmd("iptables", "-t", "filter", "-I", "DOCKER-USER", "1", "-i", tunDeviceName, "-o", br, "-j", "ACCEPT")
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func cleanupTunRouting(cfg iptablesConfig) {
|
||||
br := cfg.BridgeName
|
||||
if br != "" {
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
//go:build linux
|
||||
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestRawPreroutingTopAcceptsTun(t *testing.T) {
|
||||
topOK := `-P PREROUTING ACCEPT
|
||||
-A PREROUTING -d 172.30.0.0/16 -i artex-tun0 -j ACCEPT
|
||||
-A PREROUTING -d 172.30.0.2/32 ! -i br-25277657432c -j DROP
|
||||
`
|
||||
if !rawPreroutingTopAcceptsTun(topOK, "artex-tun0", "172.30.0.0/16") {
|
||||
t.Fatal("expected top ACCEPT rule to be recognized")
|
||||
}
|
||||
|
||||
dropAbove := `# Warning: iptables-legacy tables present
|
||||
-P PREROUTING ACCEPT
|
||||
-A PREROUTING -d 172.30.0.3/32 ! -i br-25277657432c -j DROP
|
||||
-A PREROUTING -d 172.30.0.0/16 -i artex-tun0 -j ACCEPT
|
||||
`
|
||||
if rawPreroutingTopAcceptsTun(dropAbove, "artex-tun0", "172.30.0.0/16") {
|
||||
t.Fatal("expected DROP rule above ACCEPT to fail top check")
|
||||
}
|
||||
|
||||
missing := `-P PREROUTING ACCEPT
|
||||
-A PREROUTING -d 172.30.0.2/32 ! -i br-25277657432c -j DROP
|
||||
`
|
||||
if rawPreroutingTopAcceptsTun(missing, "artex-tun0", "172.30.0.0/16") {
|
||||
t.Fatal("expected missing rule to fail top check")
|
||||
}
|
||||
}
|
||||
@@ -16,5 +16,5 @@ func openTUN(_ string) (io.ReadWriteCloser, error) {
|
||||
func applyTunRouting(_ iptablesConfig) error {
|
||||
return fmt.Errorf("TUN policy routing is only supported on Linux")
|
||||
}
|
||||
|
||||
func reconcileTunRouting(_ *iptablesConfig) bool { return false }
|
||||
func cleanupTunRouting(_ iptablesConfig) {}
|
||||
Reference in new issue
Block a user