@@ -7,39 +7,34 @@ import (
77 "log/slog"
88 "os"
99 "os/exec"
10- "syscall"
1110 "time"
1211)
1312
1413// Linux implements jail.Commander using Linux network namespaces
15- type Linux struct {
16- namespace string
17- vethHost string // Host-side veth interface name for iptables rules
18- logger * slog.Logger
19- procAttr * syscall.SysProcAttr
20- commandEnv []string
21- httpProxyPort int
22- httpsProxyPort int
23- tlsConfigDir string
24- caCertPath string
25- userInfo UserInfo
14+ type LinuxNetNamespace struct {
15+ logger * slog.Logger
16+ namespace string
17+ vethHost string // Host-side veth interface name for iptables rules
18+ commandEnv []string
19+ httpProxyPort int
20+ tlsConfigDir string
21+ caCertPath string
22+ userInfo UserInfo
2623}
2724
28- // NewLinux creates a new Linux network jail instance
29- func NewLinux (config Config ) (* Linux , error ) {
30- return & Linux {
31- namespace : newNamespaceName (),
32- logger : config .Logger ,
33- httpProxyPort : config .HttpProxyPort ,
34- httpsProxyPort : config .HttpsProxyPort ,
35- tlsConfigDir : config .TlsConfigDir ,
36- caCertPath : config .CACertPath ,
37- userInfo : config .UserInfo ,
25+ func NewLinux (config Config ) (* LinuxNetNamespace , error ) {
26+ return & LinuxNetNamespace {
27+ logger : config .Logger ,
28+ namespace : newNamespaceName (),
29+ httpProxyPort : config .HttpProxyPort ,
30+ tlsConfigDir : config .TlsConfigDir ,
31+ caCertPath : config .CACertPath ,
32+ userInfo : config .UserInfo ,
3833 }, nil
3934}
4035
41- // Setup creates network namespace and configures iptables rules
42- func (l * Linux ) Start () error {
36+ // Start creates network namespace and configures iptables rules
37+ func (l * LinuxNetNamespace ) Start () error {
4338 l .logger .Debug ("Setup called" )
4439
4540 // Setup DNS configuration BEFORE creating namespace
@@ -76,19 +71,12 @@ func (l *Linux) Start() error {
7671 "LOGNAME" : l .userInfo .Username ,
7772 })
7873
79- l .procAttr = & syscall.SysProcAttr {
80- Credential : & syscall.Credential {
81- Uid : uint32 (l .userInfo .Uid ),
82- Gid : uint32 (l .userInfo .Gid ),
83- },
84- }
85-
8674 l .logger .Debug ("Setup completed successfully" )
8775 return nil
8876}
8977
9078// Command returns an exec.Cmd configured to run within the network namespace
91- func (l * Linux ) Command (command []string ) * exec.Cmd {
79+ func (l * LinuxNetNamespace ) Command (command []string ) * exec.Cmd {
9280 l .logger .Debug ("Command called" , "command" , command )
9381
9482 // Create command with ip netns exec
@@ -104,14 +92,11 @@ func (l *Linux) Command(command []string) *exec.Cmd {
10492 cmd .Stdout = os .Stdout
10593 cmd .Stderr = os .Stderr
10694
107- // Use prepared process attributes from Open method
108- cmd .SysProcAttr = l .procAttr
109-
11095 return cmd
11196}
11297
113- // Cleanup removes the network namespace and iptables rules
114- func (l * Linux ) Close () error {
98+ // Close removes the network namespace and iptables rules
99+ func (l * LinuxNetNamespace ) Close () error {
115100 // Remove iptables rules
116101 err := l .removeIptables ()
117102 if err != nil {
@@ -138,7 +123,7 @@ func (l *Linux) Close() error {
138123}
139124
140125// createNamespace creates a new network namespace
141- func (l * Linux ) createNamespace () error {
126+ func (l * LinuxNetNamespace ) createNamespace () error {
142127 cmd := exec .Command ("ip" , "netns" , "add" , l .namespace )
143128 err := cmd .Run ()
144129 if err != nil {
@@ -148,12 +133,12 @@ func (l *Linux) createNamespace() error {
148133}
149134
150135// setupNetworking configures networking within the namespace
151- func (l * Linux ) setupNetworking () error {
136+ func (l * LinuxNetNamespace ) setupNetworking () error {
152137 // Create veth pair with short names (Linux interface names limited to 15 chars)
153138 // Generate unique ID to avoid conflicts
154139 uniqueID := fmt .Sprintf ("%d" , time .Now ().UnixNano ()% 10000000 ) // 7 digits max
155- vethHost := fmt .Sprintf ("veth_h_%s" , uniqueID ) // veth_h_1234567 = 14 chars
156- vethNetJail := fmt .Sprintf ("veth_n_%s" , uniqueID ) // veth_n_1234567 = 14 chars
140+ vethHost := fmt .Sprintf ("veth_h_%s" , uniqueID ) // veth_h_1234567 = 14 chars
141+ vethNetJail := fmt .Sprintf ("veth_n_%s" , uniqueID ) // veth_n_1234567 = 14 chars
157142
158143 // Store veth interface name for iptables rules
159144 l .vethHost = vethHost
@@ -184,7 +169,7 @@ func (l *Linux) setupNetworking() error {
184169// setupDNS configures DNS resolution for the namespace
185170// This ensures reliable DNS resolution by using public DNS servers
186171// instead of relying on the host's potentially complex DNS configuration
187- func (l * Linux ) setupDNS () error {
172+ func (l * LinuxNetNamespace ) setupDNS () error {
188173 // Always create namespace-specific resolv.conf with reliable public DNS servers
189174 // This avoids issues with systemd-resolved, Docker DNS, and other complex setups
190175 netnsEtc := fmt .Sprintf ("/etc/netns/%s" , l .namespace )
@@ -212,7 +197,7 @@ options timeout:2 attempts:2
212197}
213198
214199// setupIptables configures iptables rules for comprehensive TCP traffic interception
215- func (l * Linux ) setupIptables () error {
200+ func (l * LinuxNetNamespace ) setupIptables () error {
216201 // Enable IP forwarding
217202 cmd := exec .Command ("sysctl" , "-w" , "net.ipv4.ip_forward=1" )
218203 cmd .Run () // Ignore error
@@ -237,7 +222,7 @@ func (l *Linux) setupIptables() error {
237222}
238223
239224// removeIptables removes iptables rules
240- func (l * Linux ) removeIptables () error {
225+ func (l * LinuxNetNamespace ) removeIptables () error {
241226 // Remove comprehensive TCP redirect rule
242227 cmd := exec .Command ("iptables" , "-t" , "nat" , "-D" , "PREROUTING" , "-i" , l .vethHost , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-ports" , fmt .Sprintf ("%d" , l .httpProxyPort ))
243228 cmd .Run () // Ignore errors during cleanup
@@ -250,11 +235,11 @@ func (l *Linux) removeIptables() error {
250235}
251236
252237// removeNamespace removes the network namespace
253- func (l * Linux ) removeNamespace () error {
238+ func (l * LinuxNetNamespace ) removeNamespace () error {
254239 cmd := exec .Command ("ip" , "netns" , "del" , l .namespace )
255240 err := cmd .Run ()
256241 if err != nil {
257242 return fmt .Errorf ("failed to remove namespace: %v" , err )
258243 }
259244 return nil
260- }
245+ }
0 commit comments