mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-02 20:01:51 +08:00
feat(traffic,sandbox): add TUN/netstack transparent gateway, egress controls, CAP_NET_RAW hardening, and i18n fixes
- 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.
This commit is contained in:
1 parent
9c43e694a3
commit
d786de90af
69 files changed
+13082
-1433
No files matched your search
@@ -212,6 +212,26 @@ type dockerContainerInspect struct {
|
||||
NetworkMode string `json:"NetworkMode"`
|
||||
Memory int64 `json:"Memory"`
|
||||
} `json:"HostConfig"`
|
||||
NetworkSettings struct {
|
||||
Networks map[string]struct {
|
||||
IPAddress string `json:"IPAddress"`
|
||||
} `json:"Networks"`
|
||||
} `json:"NetworkSettings"`
|
||||
}
|
||||
|
||||
func (i *dockerContainerInspect) IPOnNetwork(network string) string {
|
||||
if i == nil {
|
||||
return ""
|
||||
}
|
||||
if nw, ok := i.NetworkSettings.Networks[network]; ok && strings.TrimSpace(nw.IPAddress) != "" {
|
||||
return strings.TrimSpace(nw.IPAddress)
|
||||
}
|
||||
for _, nw := range i.NetworkSettings.Networks {
|
||||
if strings.TrimSpace(nw.IPAddress) != "" {
|
||||
return strings.TrimSpace(nw.IPAddress)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (c *dockerClient) InspectContainer(ctx context.Context, idOrName string) (*dockerContainerInspect, error) {
|
||||
@@ -224,15 +244,34 @@ func (c *dockerClient) InspectContainer(ctx context.Context, idOrName string) (*
|
||||
}
|
||||
|
||||
type dockerContainerListItem struct {
|
||||
ID string `json:"Id"`
|
||||
Names []string `json:"Names"`
|
||||
Image string `json:"Image"`
|
||||
State string `json:"State"`
|
||||
Status string `json:"Status"`
|
||||
Created int64 `json:"Created"`
|
||||
Labels map[string]string `json:"Labels"`
|
||||
ID string `json:"Id"`
|
||||
Names []string `json:"Names"`
|
||||
Image string `json:"Image"`
|
||||
State string `json:"State"`
|
||||
Status string `json:"Status"`
|
||||
Created int64 `json:"Created"`
|
||||
Labels map[string]string `json:"Labels"`
|
||||
NetworkSettings struct {
|
||||
Networks map[string]struct {
|
||||
IPAddress string `json:"IPAddress"`
|
||||
} `json:"Networks"`
|
||||
} `json:"NetworkSettings"`
|
||||
}
|
||||
|
||||
func (i *dockerContainerListItem) IPOnNetwork(network string) string {
|
||||
if i == nil {
|
||||
return ""
|
||||
}
|
||||
if nw, ok := i.NetworkSettings.Networks[network]; ok && strings.TrimSpace(nw.IPAddress) != "" {
|
||||
return strings.TrimSpace(nw.IPAddress)
|
||||
}
|
||||
for _, nw := range i.NetworkSettings.Networks {
|
||||
if strings.TrimSpace(nw.IPAddress) != "" {
|
||||
return strings.TrimSpace(nw.IPAddress)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
func (c *dockerClient) ListContainersByLabel(ctx context.Context, labels map[string]string) ([]dockerContainerListItem, error) {
|
||||
labelList := make([]string, 0, len(labels))
|
||||
for k, v := range labels {
|
||||
|
||||
+259
-33
@@ -50,10 +50,14 @@ type Manager struct {
|
||||
statsMu sync.Mutex
|
||||
statsCache map[string]cachedStats
|
||||
statsInFlight map[string]struct{}
|
||||
// Detected sandbox network CIDRs (used by HTTP server to block :8787 access).
|
||||
// Detected sandbox network CIDRs & bridge gateway IP.
|
||||
netCIDRMu sync.RWMutex
|
||||
netCIDRs []*net.IPNet
|
||||
gatewayIP string
|
||||
|
||||
// Real-time container IP -> sandbox owner identity map for SNAT-free traffic attribution (#218).
|
||||
ipMu sync.RWMutex
|
||||
ipMap map[string]ContainerIdentity
|
||||
stopReaper context.CancelFunc
|
||||
reaperWG sync.WaitGroup
|
||||
}
|
||||
@@ -84,6 +88,7 @@ func New(cfg Config) (*Manager, error) {
|
||||
states: map[string]*targetState{},
|
||||
statsCache: map[string]cachedStats{},
|
||||
statsInFlight: map[string]struct{}{},
|
||||
ipMap: map[string]ContainerIdentity{},
|
||||
}
|
||||
if cfg.Mode == ModeDocker {
|
||||
dc, err := newDockerClient(cfg.DockerHost)
|
||||
@@ -163,32 +168,88 @@ func (m *Manager) SetProxyConfig(proxyURL, caCertPath string) {
|
||||
}
|
||||
}
|
||||
|
||||
// SetSystemCABundlePath sets the host path to the merged system + ARTEX MITM CA
|
||||
// bundle mounted read-only into sandbox containers at /etc/ssl/certs/ca-certificates.crt.
|
||||
func (m *Manager) SetSystemCABundlePath(bundlePath string) {
|
||||
if m == nil {
|
||||
return
|
||||
}
|
||||
m.mu.Lock()
|
||||
m.cfg.SystemCABundlePath = strings.TrimSpace(bundlePath)
|
||||
m.mu.Unlock()
|
||||
}
|
||||
|
||||
// GatewayIP returns the detected bridge gateway IP of the sandbox network (e.g. "172.30.0.1").
|
||||
func (m *Manager) GatewayIP() string {
|
||||
if m == nil {
|
||||
return ""
|
||||
}
|
||||
m.netCIDRMu.RLock()
|
||||
defer m.netCIDRMu.RUnlock()
|
||||
return m.gatewayIP
|
||||
}
|
||||
|
||||
// ContainerIPSnapshot returns a copy of the live container IP -> identity map
|
||||
// for transparent gateway policy sync (#218 Phase 3).
|
||||
func (m *Manager) ContainerIPSnapshot() map[string]ContainerIdentity {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
m.ipMu.RLock()
|
||||
defer m.ipMu.RUnlock()
|
||||
out := make(map[string]ContainerIdentity, len(m.ipMap))
|
||||
for k, v := range m.ipMap {
|
||||
out[k] = v
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// ProxyEnv returns HTTP(S)_PROXY + CA cert environment variables for processes
|
||||
// running inside a sandbox container. Empty when capture/proxy is disabled.
|
||||
func (m *Manager) ProxyEnv(proxyActive bool) []string {
|
||||
if m == nil || !proxyActive {
|
||||
return nil
|
||||
}
|
||||
cfg := m.Config()
|
||||
if strings.TrimSpace(cfg.ProxyURL) == "" {
|
||||
return m.ProxyEnvWithURL(m.Config().ProxyURL)
|
||||
}
|
||||
|
||||
// ProxyEnvWithURL returns HTTP(S)_PROXY + CA cert environment variables for
|
||||
// processes inside a sandbox container using an explicit proxy URL (e.g. a
|
||||
// Proxy-Authorization tagged recording proxy URL for per-intent attribution).
|
||||
func (m *Manager) ProxyEnvWithURL(proxyURL string) []string {
|
||||
if m == nil {
|
||||
return nil
|
||||
}
|
||||
proxyURL = strings.TrimSpace(proxyURL)
|
||||
if proxyURL == "" {
|
||||
return nil
|
||||
}
|
||||
cfg := m.Config()
|
||||
env := []string{
|
||||
"HTTP_PROXY=" + cfg.ProxyURL,
|
||||
"HTTPS_PROXY=" + cfg.ProxyURL,
|
||||
"http_proxy=" + cfg.ProxyURL,
|
||||
"https_proxy=" + cfg.ProxyURL,
|
||||
"ALL_PROXY=" + cfg.ProxyURL,
|
||||
"all_proxy=" + cfg.ProxyURL,
|
||||
"HTTP_PROXY=" + proxyURL,
|
||||
"HTTPS_PROXY=" + proxyURL,
|
||||
"http_proxy=" + proxyURL,
|
||||
"https_proxy=" + proxyURL,
|
||||
"ALL_PROXY=" + proxyURL,
|
||||
"all_proxy=" + proxyURL,
|
||||
"NODE_USE_ENV_PROXY=1",
|
||||
}
|
||||
caBundle := ""
|
||||
if cfg.SystemCABundlePath != "" {
|
||||
if _, err := os.Stat(cfg.SystemCABundlePath); err == nil {
|
||||
caBundle = ContainerSystemCABundlePath
|
||||
}
|
||||
}
|
||||
if cfg.CACertPath != "" {
|
||||
if _, err := os.Stat(cfg.CACertPath); err == nil {
|
||||
if caBundle == "" {
|
||||
caBundle = ContainerCACertPath
|
||||
}
|
||||
env = append(env,
|
||||
"SSL_CERT_FILE="+ContainerCACertPath,
|
||||
"CURL_CA_BUNDLE="+ContainerCACertPath,
|
||||
"REQUESTS_CA_BUNDLE="+ContainerCACertPath,
|
||||
"GIT_SSL_CAINFO="+ContainerCACertPath,
|
||||
"SSL_CERT_FILE="+caBundle,
|
||||
"CURL_CA_BUNDLE="+caBundle,
|
||||
"REQUESTS_CA_BUNDLE="+caBundle,
|
||||
"GIT_SSL_CAINFO="+caBundle,
|
||||
"NODE_EXTRA_CA_CERTS="+ContainerCACertPath,
|
||||
)
|
||||
}
|
||||
@@ -486,16 +547,108 @@ func (m *Manager) recordNetworkCIDRs(netInfo *dockerNetworkInspect) {
|
||||
}
|
||||
m.netCIDRMu.Lock()
|
||||
m.netCIDRs = cidrs
|
||||
m.netCIDRMu.Unlock()
|
||||
if gateway != "" {
|
||||
m.gatewayIP = gateway
|
||||
}
|
||||
m.netCIDRMu.Unlock()
|
||||
// attached directly to artex-sandbox (eliminates bridge SNAT so the proxy
|
||||
// observes each sandbox container's real source IP). Fall back to the
|
||||
// bridge gateway on single-network setups.
|
||||
proxyHost := detectLocalIPInSubnets(cidrs)
|
||||
if proxyHost == "" {
|
||||
proxyHost = gateway
|
||||
}
|
||||
if proxyHost != "" {
|
||||
m.mu.Lock()
|
||||
if m.cfg.ProxyURL == "" || strings.EqualFold(m.cfg.ProxyURL, "auto") {
|
||||
m.cfg.ProxyURL = "http://" + gateway + ":8788"
|
||||
m.cfg.ProxyURL = "http://" + proxyHost + ":8788"
|
||||
}
|
||||
m.mu.Unlock()
|
||||
}
|
||||
}
|
||||
|
||||
func detectLocalIPInSubnets(cidrs []*net.IPNet) string {
|
||||
if len(cidrs) == 0 {
|
||||
return ""
|
||||
}
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
for _, a := range addrs {
|
||||
ipnet, ok := a.(*net.IPNet)
|
||||
if !ok || ipnet.IP == nil {
|
||||
continue
|
||||
}
|
||||
ip := ipnet.IP.To4()
|
||||
if ip == nil {
|
||||
continue
|
||||
}
|
||||
for _, sub := range cidrs {
|
||||
if sub.Contains(ip) {
|
||||
return ip.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// ResolveContainerByIP returns the managed sandbox container owning srcIP.
|
||||
func (m *Manager) ResolveContainerByIP(srcIP string) (ContainerIdentity, bool) {
|
||||
if m == nil {
|
||||
return ContainerIdentity{}, false
|
||||
}
|
||||
raw := strings.TrimSpace(srcIP)
|
||||
if h, _, err := net.SplitHostPort(raw); err == nil {
|
||||
raw = h
|
||||
}
|
||||
ip := net.ParseIP(strings.Trim(raw, "[]"))
|
||||
if ip == nil {
|
||||
return ContainerIdentity{}, false
|
||||
}
|
||||
if v4 := ip.To4(); v4 != nil {
|
||||
ip = v4
|
||||
}
|
||||
m.ipMu.RLock()
|
||||
id, ok := m.ipMap[ip.String()]
|
||||
m.ipMu.RUnlock()
|
||||
return id, ok
|
||||
}
|
||||
|
||||
func (m *Manager) registerContainerIP(ip string, id ContainerIdentity) {
|
||||
parsed := net.ParseIP(strings.TrimSpace(ip))
|
||||
if parsed == nil {
|
||||
return
|
||||
}
|
||||
if v4 := parsed.To4(); v4 != nil {
|
||||
parsed = v4
|
||||
}
|
||||
ipStr := parsed.String()
|
||||
id.IP = ipStr
|
||||
m.ipMu.Lock()
|
||||
// Evict stale IP entries for the same container name/id first.
|
||||
for k, existing := range m.ipMap {
|
||||
if k != ipStr && ((id.ContainerName != "" && existing.ContainerName == id.ContainerName) || (id.ContainerID != "" && existing.ContainerID == id.ContainerID)) {
|
||||
delete(m.ipMap, k)
|
||||
}
|
||||
}
|
||||
m.ipMap[ipStr] = id
|
||||
m.ipMu.Unlock()
|
||||
}
|
||||
|
||||
func (m *Manager) evictContainerIP(name string) {
|
||||
if name == "" {
|
||||
return
|
||||
}
|
||||
m.ipMu.Lock()
|
||||
for k, existing := range m.ipMap {
|
||||
if existing.ContainerName == name {
|
||||
delete(m.ipMap, k)
|
||||
}
|
||||
}
|
||||
m.ipMu.Unlock()
|
||||
}
|
||||
|
||||
// Ensure guarantees that target's sandbox container exists and is running.
|
||||
func (m *Manager) Ensure(ctx context.Context, t Target) (string, error) {
|
||||
if !m.Enabled() {
|
||||
@@ -523,6 +676,18 @@ func (m *Manager) Ensure(ctx context.Context, t Target) (string, error) {
|
||||
if err := m.docker.StartContainer(ctx, info.ID); err != nil {
|
||||
return "", fmt.Errorf("start sandbox container %s: %w", name, err)
|
||||
}
|
||||
if refreshed, ierr := m.docker.InspectContainer(ctx, info.ID); ierr == nil {
|
||||
info = refreshed
|
||||
}
|
||||
}
|
||||
if ip := info.IPOnNetwork(cfg.Network); ip != "" {
|
||||
m.registerContainerIP(ip, ContainerIdentity{
|
||||
Scope: t.Scope,
|
||||
OwnerID: t.OwnerID,
|
||||
ContainerID: info.ID,
|
||||
ShortID: shortID(info.ID),
|
||||
ContainerName: name,
|
||||
})
|
||||
}
|
||||
return info.ID, nil
|
||||
} else if !isDockerNotFound(err) {
|
||||
@@ -537,10 +702,32 @@ func (m *Manager) Ensure(ctx context.Context, t Target) (string, error) {
|
||||
|
||||
hostRoot := cfg.MapDataPathToHost(localRoot)
|
||||
binds := []string{hostRoot + ":" + ContainerWorkDir + ":rw"}
|
||||
baseEnv := []string{
|
||||
"HOME=/tmp",
|
||||
"TERM=dumb",
|
||||
"ARTEX_SANDBOX=1",
|
||||
"ARTEX_WORKSPACE=" + ContainerWorkDir,
|
||||
}
|
||||
if cfg.AllowNetRaw {
|
||||
baseEnv = append(baseEnv, "NMAP_PRIVILEGED=1")
|
||||
}
|
||||
if cfg.CACertPath != "" {
|
||||
if _, err := os.Stat(cfg.CACertPath); err == nil {
|
||||
hostCA := cfg.MapDataPathToHost(cfg.CACertPath)
|
||||
binds = append(binds, hostCA+":"+ContainerCACertPath+":ro")
|
||||
baseEnv = append(baseEnv, "NODE_EXTRA_CA_CERTS="+ContainerCACertPath)
|
||||
}
|
||||
}
|
||||
if cfg.SystemCABundlePath != "" {
|
||||
if _, err := os.Stat(cfg.SystemCABundlePath); err == nil {
|
||||
hostBundle := cfg.MapDataPathToHost(cfg.SystemCABundlePath)
|
||||
binds = append(binds, hostBundle+":"+ContainerSystemCABundlePath+":ro")
|
||||
baseEnv = append(baseEnv,
|
||||
"SSL_CERT_FILE="+ContainerSystemCABundlePath,
|
||||
"CURL_CA_BUNDLE="+ContainerSystemCABundlePath,
|
||||
"REQUESTS_CA_BUNDLE="+ContainerSystemCABundlePath,
|
||||
"GIT_SSL_CAINFO="+ContainerSystemCABundlePath,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -550,18 +737,38 @@ func (m *Manager) Ensure(ctx context.Context, t Target) (string, error) {
|
||||
pidsLimit := int64(cfg.PidsLimit)
|
||||
|
||||
hostCfg := map[string]any{
|
||||
"Binds": binds,
|
||||
"NetworkMode": cfg.Network,
|
||||
"Memory": memBytes,
|
||||
"MemorySwap": memBytes, // disable swap thrashing
|
||||
"NanoCpus": nanoCPUs,
|
||||
"PidsLimit": pidsLimit,
|
||||
"ShmSize": shmBytes,
|
||||
"CapDrop": []string{"ALL"},
|
||||
"SecurityOpt": []string{"no-new-privileges:true"},
|
||||
"Init": true, // tini/docker-init reaps zombies inside container
|
||||
"Tmpfs": map[string]string{"/tmp": fmt.Sprintf("rw,nosuid,size=%dm", cfg.TmpSizeMB)},
|
||||
"RestartPolicy": map[string]any{"Name": "no"},
|
||||
"Binds": binds,
|
||||
"NetworkMode": cfg.Network,
|
||||
"Memory": memBytes,
|
||||
"MemorySwap": memBytes, // disable swap thrashing
|
||||
"NanoCpus": nanoCPUs,
|
||||
"PidsLimit": pidsLimit,
|
||||
"ShmSize": shmBytes,
|
||||
"CapDrop": []string{"ALL"},
|
||||
"Init": true, // tini/docker-init reaps zombies inside container
|
||||
"Tmpfs": map[string]string{"/tmp": fmt.Sprintf("rw,nosuid,nodev,size=%dm", cfg.TmpSizeMB)},
|
||||
"Sysctls": map[string]string{"net.ipv4.ping_group_range": "0 2147483647"},
|
||||
"RestartPolicy": map[string]any{"Name": "no"},
|
||||
}
|
||||
if cfg.AllowNetRaw {
|
||||
// Permit CAP_NET_RAW in the bounding set ONLY (paired with file-cap cap_net_raw+ep
|
||||
// on /usr/bin/nmap, /usr/bin/nping, and /usr/bin/ping in Dockerfile.sandbox, with
|
||||
// all SUID/SGID bits stripped). Ordinary non-root processes keep CapEff=0.
|
||||
hostCfg["CapAdd"] = []string{"NET_RAW"}
|
||||
} else {
|
||||
// Strict lockdown for untrusted custom images that may contain SUID binaries.
|
||||
hostCfg["SecurityOpt"] = []string{"no-new-privileges:true"}
|
||||
}
|
||||
// Route container DNS through the sandbox gateway IP when transparent mode is enabled
|
||||
// so Docker's embedded DNS forwards queries across the sandbox bridge with real container src_ip.
|
||||
if cfg.TransparentGateway {
|
||||
dnsIP := strings.TrimSpace(cfg.GatewayDNSIP)
|
||||
if dnsIP == "" {
|
||||
dnsIP = m.GatewayIP()
|
||||
}
|
||||
if dnsIP != "" {
|
||||
hostCfg["Dns"] = []string{dnsIP}
|
||||
}
|
||||
}
|
||||
if cfg.Runtime != "" {
|
||||
hostCfg["Runtime"] = cfg.Runtime
|
||||
@@ -577,12 +784,7 @@ func (m *Manager) Ensure(ctx context.Context, t Target) (string, error) {
|
||||
"AttachStdout": false,
|
||||
"AttachStderr": false,
|
||||
"Tty": false,
|
||||
"Env": []string{
|
||||
"HOME=/tmp",
|
||||
"TERM=dumb",
|
||||
"ARTEX_SANDBOX=1",
|
||||
"ARTEX_WORKSPACE=" + ContainerWorkDir,
|
||||
},
|
||||
"Env": baseEnv,
|
||||
"Labels": map[string]string{
|
||||
labelManaged: "true",
|
||||
labelInstance: cfg.InstanceID,
|
||||
@@ -600,6 +802,17 @@ func (m *Manager) Ensure(ctx context.Context, t Target) (string, error) {
|
||||
_ = m.docker.RemoveContainer(ctx, id)
|
||||
return "", fmt.Errorf("start sandbox container %s: %w", name, err)
|
||||
}
|
||||
if info, ierr := m.docker.InspectContainer(ctx, id); ierr == nil {
|
||||
if ip := info.IPOnNetwork(cfg.Network); ip != "" {
|
||||
m.registerContainerIP(ip, ContainerIdentity{
|
||||
Scope: t.Scope,
|
||||
OwnerID: t.OwnerID,
|
||||
ContainerID: id,
|
||||
ShortID: shortID(id),
|
||||
ContainerName: name,
|
||||
})
|
||||
}
|
||||
}
|
||||
log.Printf("[sandbox] created & started %s (%s, image=%s, mem=%dMiB, cpus=%.2f)", name, shortID(id), cfg.Image, cfg.MemoryMB, cfg.CPUs)
|
||||
return id, nil
|
||||
}
|
||||
@@ -617,6 +830,7 @@ func (m *Manager) Stop(ctx context.Context, t Target) error {
|
||||
return err
|
||||
}
|
||||
m.dropStatsCache(name)
|
||||
m.evictContainerIP(name)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -633,6 +847,7 @@ func (m *Manager) Remove(ctx context.Context, t Target) error {
|
||||
return err
|
||||
}
|
||||
m.dropStatsCache(name)
|
||||
m.evictContainerIP(name)
|
||||
m.stateMu.Lock()
|
||||
delete(m.states, t.Key())
|
||||
m.stateMu.Unlock()
|
||||
@@ -790,12 +1005,14 @@ func (m *Manager) StatusWithStats(ctx context.Context, withStats bool) GlobalSta
|
||||
if len(item.Names) > 0 {
|
||||
name = strings.TrimPrefix(item.Names[0], "/")
|
||||
}
|
||||
ipAddr := item.IPOnNetwork(cfg.Network)
|
||||
ci := ContainerInfo{
|
||||
Scope: t.Scope,
|
||||
OwnerID: t.OwnerID,
|
||||
Name: name,
|
||||
ContainerID: item.ID,
|
||||
ShortID: shortID(item.ID),
|
||||
IPAddress: ipAddr,
|
||||
Image: item.Image,
|
||||
State: item.State,
|
||||
StatusText: item.Status,
|
||||
@@ -805,6 +1022,15 @@ func (m *Manager) StatusWithStats(ctx context.Context, withStats bool) GlobalSta
|
||||
Network: cfg.Network,
|
||||
Runtime: cfg.Runtime,
|
||||
}
|
||||
if item.State == "running" && ipAddr != "" {
|
||||
m.registerContainerIP(ipAddr, ContainerIdentity{
|
||||
Scope: t.Scope,
|
||||
OwnerID: t.OwnerID,
|
||||
ContainerID: item.ID,
|
||||
ShortID: shortID(item.ID),
|
||||
ContainerName: name,
|
||||
})
|
||||
}
|
||||
if item.State == "running" {
|
||||
if st, fresh, ok := m.peekStatsCacheAny(name); ok {
|
||||
ci.CPUPercent = st.CPUPercent
|
||||
|
||||
@@ -134,3 +134,48 @@ func TestHostToContainerAndDataPathMapping(t *testing.T) {
|
||||
t.Fatalf("HostToContainerPath root = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestManagerContainerIPMap(t *testing.T) {
|
||||
m := &Manager{ipMap: map[string]ContainerIdentity{}}
|
||||
m.registerContainerIP("172.28.0.5", ContainerIdentity{
|
||||
Scope: ScopeTask,
|
||||
OwnerID: "12",
|
||||
ContainerID: "abc123def456",
|
||||
ContainerName: "artex-sbx-inst-task-12",
|
||||
IP: "172.28.0.5",
|
||||
})
|
||||
got, ok := m.ResolveContainerByIP("172.28.0.5:54321")
|
||||
if !ok || got.Scope != ScopeTask || got.OwnerID != "12" || got.ContainerID != "abc123def456" {
|
||||
t.Fatalf("ResolveContainerByIP failed: ok=%v got=%+v", ok, got)
|
||||
}
|
||||
// Re-registering same container with a new IP must evict the stale IP.
|
||||
m.registerContainerIP("172.28.0.9", ContainerIdentity{
|
||||
Scope: ScopeTask,
|
||||
OwnerID: "12",
|
||||
ContainerID: "new999888777",
|
||||
ContainerName: "artex-sbx-inst-task-12",
|
||||
IP: "172.28.0.9",
|
||||
})
|
||||
if _, stale := m.ResolveContainerByIP("172.28.0.5"); stale {
|
||||
t.Fatalf("stale IP 172.28.0.5 should have been evicted")
|
||||
}
|
||||
if got2, ok2 := m.ResolveContainerByIP("172.28.0.9"); !ok2 || got2.ContainerID != "new999888777" {
|
||||
t.Fatalf("new IP 172.28.0.9 lookup failed: ok=%v got=%+v", ok2, got2)
|
||||
}
|
||||
// Explicit eviction by container name.
|
||||
m.evictContainerIP("artex-sbx-inst-task-12")
|
||||
if _, ok3 := m.ResolveContainerByIP("172.28.0.9"); ok3 {
|
||||
t.Fatalf("evicted container IP should no longer resolve")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigFromEnvAllowNetRaw(t *testing.T) {
|
||||
t.Setenv("ARTEX_SANDBOX_ALLOW_NET_RAW", "")
|
||||
if cfg := ConfigFromEnv(t.TempDir(), ""); !cfg.AllowNetRaw {
|
||||
t.Fatalf("AllowNetRaw should default to true")
|
||||
}
|
||||
t.Setenv("ARTEX_SANDBOX_ALLOW_NET_RAW", "false")
|
||||
if cfg := ConfigFromEnv(t.TempDir(), ""); cfg.AllowNetRaw {
|
||||
t.Fatalf("AllowNetRaw should respect ARTEX_SANDBOX_ALLOW_NET_RAW=false")
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,10 @@ const ContainerWorkDir = "/workspace"
|
||||
// ContainerCACertPath is where the recording MITM proxy CA cert is mounted read-only.
|
||||
const ContainerCACertPath = "/etc/artex/ca.crt"
|
||||
|
||||
// ContainerSystemCABundlePath is where the merged system + ARTEX MITM CA bundle
|
||||
// is mounted read-only so static Go binaries and system TLS libraries trust the
|
||||
// recording proxy without needing root or runtime update-ca-certificates (#218 Phase 3).
|
||||
const ContainerSystemCABundlePath = "/etc/ssl/certs/ca-certificates.crt"
|
||||
// Target identifies a logical sandbox instance.
|
||||
type Target struct {
|
||||
Scope Scope
|
||||
@@ -62,12 +66,16 @@ type Config struct {
|
||||
HostDataDir string
|
||||
ProxyURL string
|
||||
CACertPath string
|
||||
SystemCABundlePath string
|
||||
TransparentGateway bool
|
||||
GatewayDNSIP string
|
||||
MemoryMB int
|
||||
CPUs float64
|
||||
PidsLimit int
|
||||
ShmSizeMB int
|
||||
TmpSizeMB int
|
||||
User string
|
||||
AllowNetRaw bool
|
||||
AutoStopOnComplete bool
|
||||
SessionIdleMinutes int
|
||||
InstanceID string
|
||||
@@ -129,12 +137,15 @@ func ConfigFromEnv(dataDir, caCertPath string) Config {
|
||||
HostDataDir: filepath.Clean(hostDataDir),
|
||||
ProxyURL: strings.TrimSpace(os.Getenv("ARTEX_SANDBOX_PROXY_URL")),
|
||||
CACertPath: strings.TrimSpace(caCertPath),
|
||||
TransparentGateway: envBool("ARTEX_SANDBOX_TRANSPARENT", true),
|
||||
GatewayDNSIP: strings.TrimSpace(os.Getenv("ARTEX_SANDBOX_DNS")),
|
||||
MemoryMB: envInt("ARTEX_SANDBOX_MEMORY_MB", 2048, 256),
|
||||
CPUs: envFloat("ARTEX_SANDBOX_CPUS", 2.0, 0.25),
|
||||
PidsLimit: envInt("ARTEX_SANDBOX_PIDS_LIMIT", 256, 32),
|
||||
ShmSizeMB: envInt("ARTEX_SANDBOX_SHM_MB", 512, 64),
|
||||
TmpSizeMB: envInt("ARTEX_SANDBOX_TMP_MB", 512, 64),
|
||||
User: user,
|
||||
AllowNetRaw: envBool("ARTEX_SANDBOX_ALLOW_NET_RAW", true),
|
||||
AutoStopOnComplete: envBool("ARTEX_SANDBOX_AUTO_STOP", true),
|
||||
SessionIdleMinutes: envInt("ARTEX_SANDBOX_SESSION_IDLE_MIN", 20, 1),
|
||||
InstanceID: instanceID,
|
||||
@@ -178,6 +189,15 @@ func envBool(key string, def bool) bool {
|
||||
}
|
||||
|
||||
// ContainerInfo describes one sandbox container for API/UI reporting.
|
||||
type ContainerIdentity struct {
|
||||
Scope Scope `json:"scope"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
ContainerID string `json:"container_id"`
|
||||
ShortID string `json:"short_id"`
|
||||
ContainerName string `json:"container_name"`
|
||||
IP string `json:"ip"`
|
||||
}
|
||||
|
||||
type ContainerInfo struct {
|
||||
Scope Scope `json:"scope"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
@@ -186,6 +206,7 @@ type ContainerInfo struct {
|
||||
Name string `json:"name"`
|
||||
ContainerID string `json:"container_id,omitempty"`
|
||||
ShortID string `json:"short_id,omitempty"`
|
||||
IPAddress string `json:"ip_address,omitempty"`
|
||||
Image string `json:"image"`
|
||||
State string `json:"state"` // running | exited | created | missing | local | error
|
||||
StatusText string `json:"status_text,omitempty"`
|
||||
|
||||
Reference in new issue
Block a user