mirror of
https://github.com/RuoJi6/ARTEX.git
synced 2026-12-04 20:41: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
@@ -0,0 +1,170 @@
|
||||
//go:build linux
|
||||
|
||||
package traffic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
const (
|
||||
soOriginalDst = 80 // linux/netfilter_ipv4.h SO_ORIGINAL_DST
|
||||
ip6tOriginalDst = 80 // linux/netfilter_ipv6/ip6_tables.h IP6T_SO_ORIGINAL_DST
|
||||
)
|
||||
|
||||
// originalDstTCP retrieves the pre-REDIRECT destination address of a netfilter-
|
||||
// redirected TCP connection via SO_ORIGINAL_DST / IP6T_SO_ORIGINAL_DST.
|
||||
func originalDstTCP(conn net.Conn) (net.IP, int, error) {
|
||||
sc, ok := conn.(syscall.Conn)
|
||||
if !ok {
|
||||
return nil, 0, errors.New("connection does not implement syscall.Conn")
|
||||
}
|
||||
rc, err := sc.SyscallConn()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
var (
|
||||
ip net.IP
|
||||
port int
|
||||
sockErr error
|
||||
)
|
||||
if err := rc.Control(func(fd uintptr) {
|
||||
var addr4 unix.RawSockaddrInet4
|
||||
vallen := uint32(unsafe.Sizeof(addr4))
|
||||
_, _, errno := unix.Syscall6(
|
||||
unix.SYS_GETSOCKOPT,
|
||||
fd,
|
||||
unix.SOL_IP,
|
||||
soOriginalDst,
|
||||
uintptr(unsafe.Pointer(&addr4)),
|
||||
uintptr(unsafe.Pointer(&vallen)),
|
||||
0,
|
||||
)
|
||||
if errno == 0 {
|
||||
ip = net.IPv4(addr4.Addr[0], addr4.Addr[1], addr4.Addr[2], addr4.Addr[3])
|
||||
port = int(binary.BigEndian.Uint16((*(*[2]byte)(unsafe.Pointer(&addr4.Port)))[:]))
|
||||
return
|
||||
}
|
||||
var addr6 unix.RawSockaddrInet6
|
||||
vallen6 := uint32(unsafe.Sizeof(addr6))
|
||||
_, _, errno6 := unix.Syscall6(
|
||||
unix.SYS_GETSOCKOPT,
|
||||
fd,
|
||||
unix.SOL_IPV6,
|
||||
ip6tOriginalDst,
|
||||
uintptr(unsafe.Pointer(&addr6)),
|
||||
uintptr(unsafe.Pointer(&vallen6)),
|
||||
0,
|
||||
)
|
||||
if errno6 == 0 {
|
||||
b := make([]byte, net.IPv6len)
|
||||
copy(b, addr6.Addr[:])
|
||||
ip = net.IP(b)
|
||||
port = int(binary.BigEndian.Uint16((*(*[2]byte)(unsafe.Pointer(&addr6.Port)))[:]))
|
||||
return
|
||||
}
|
||||
sockErr = fmt.Errorf("getsockopt SO_ORIGINAL_DST failed: ipv4=%v ipv6=%v", errno, errno6)
|
||||
}); err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
if sockErr != nil {
|
||||
return nil, 0, sockErr
|
||||
}
|
||||
return normalizeIP(ip), port, nil
|
||||
}
|
||||
|
||||
// listenTransparentUDP creates a UDP listener configured with IP_TRANSPARENT
|
||||
// and IP_RECVORIGDSTADDR for Linux iptables TPROXY interception.
|
||||
func listenTransparentUDP(addr string) (*net.UDPConn, error) {
|
||||
lc := net.ListenConfig{
|
||||
Control: func(network, address string, c syscall.RawConn) error {
|
||||
var ctrlErr error
|
||||
err := c.Control(func(fd uintptr) {
|
||||
_ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
|
||||
if err := unix.SetsockoptInt(int(fd), unix.SOL_IP, unix.IP_TRANSPARENT, 1); err != nil {
|
||||
ctrlErr = fmt.Errorf("setsockopt IP_TRANSPARENT: %w", err)
|
||||
return
|
||||
}
|
||||
if err := unix.SetsockoptInt(int(fd), unix.SOL_IP, unix.IP_RECVORIGDSTADDR, 1); err != nil {
|
||||
ctrlErr = fmt.Errorf("setsockopt IP_RECVORIGDSTADDR: %w", err)
|
||||
return
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ctrlErr
|
||||
},
|
||||
}
|
||||
pc, err := lc.ListenPacket(context.Background(), "udp4", addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uc, ok := pc.(*net.UDPConn)
|
||||
if !ok {
|
||||
_ = pc.Close()
|
||||
return nil, errors.New("expected *net.UDPConn")
|
||||
}
|
||||
return uc, nil
|
||||
}
|
||||
|
||||
// readTransparentUDP reads one TPROXY UDP datagram along with its source and
|
||||
// original destination addresses from the IP_ORIGDSTADDR control message.
|
||||
func readTransparentUDP(conn *net.UDPConn, buf []byte) (n int, src, origDst *net.UDPAddr, err error) {
|
||||
oob := make([]byte, 1024)
|
||||
n, oobn, _, src, err := conn.ReadMsgUDP(buf, oob)
|
||||
if err != nil {
|
||||
return 0, nil, nil, err
|
||||
}
|
||||
msgs, parseErr := unix.ParseSocketControlMessage(oob[:oobn])
|
||||
if parseErr != nil {
|
||||
return n, src, nil, parseErr
|
||||
}
|
||||
for _, msg := range msgs {
|
||||
if msg.Header.Level == unix.SOL_IP && msg.Header.Type == unix.IP_ORIGDSTADDR && len(msg.Data) >= 8 {
|
||||
port := int(binary.BigEndian.Uint16(msg.Data[2:4]))
|
||||
ip := net.IPv4(msg.Data[4], msg.Data[5], msg.Data[6], msg.Data[7])
|
||||
origDst = &net.UDPAddr{IP: ip, Port: port}
|
||||
break
|
||||
}
|
||||
}
|
||||
if origDst == nil {
|
||||
return n, src, nil, errors.New("missing IP_ORIGDSTADDR control message")
|
||||
}
|
||||
return n, src, origDst, nil
|
||||
}
|
||||
|
||||
// dialTransparentUDPReply sends a UDP reply packet back to clientAddr spoofed
|
||||
// as coming from origDst (requires CAP_NET_ADMIN / IP_TRANSPARENT).
|
||||
func sendTransparentUDPReply(origDst, clientAddr *net.UDPAddr, payload []byte) error {
|
||||
d := net.Dialer{
|
||||
LocalAddr: origDst,
|
||||
Control: func(network, address string, c syscall.RawConn) error {
|
||||
var ctrlErr error
|
||||
err := c.Control(func(fd uintptr) {
|
||||
_ = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
|
||||
if err := unix.SetsockoptInt(int(fd), unix.SOL_IP, unix.IP_TRANSPARENT, 1); err != nil {
|
||||
ctrlErr = err
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ctrlErr
|
||||
},
|
||||
}
|
||||
conn, err := d.Dial("udp4", clientAddr.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
_, err = conn.Write(payload)
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user