@@ -11,7 +11,7 @@ import (
11
11
)
12
12
13
13
// Linux implements jail.Commander using Linux network namespaces
14
- type LinuxNetNamespace struct {
14
+ type Linux struct {
15
15
logger * slog.Logger
16
16
namespace string
17
17
vethHost string // Host-side veth interface name for iptables rules
@@ -22,8 +22,8 @@ type LinuxNetNamespace struct {
22
22
userInfo UserInfo
23
23
}
24
24
25
- func NewLinux (config Config ) (* LinuxNetNamespace , error ) {
26
- return & LinuxNetNamespace {
25
+ func NewLinux (config Config ) (* Linux , error ) {
26
+ return & Linux {
27
27
logger : config .Logger ,
28
28
namespace : newNamespaceName (),
29
29
httpProxyPort : config .HttpProxyPort ,
@@ -34,7 +34,7 @@ func NewLinux(config Config) (*LinuxNetNamespace, error) {
34
34
}
35
35
36
36
// Start creates network namespace and configures iptables rules
37
- func (l * LinuxNetNamespace ) Start () error {
37
+ func (l * Linux ) Start () error {
38
38
l .logger .Debug ("Setup called" )
39
39
40
40
// Setup DNS configuration BEFORE creating namespace
@@ -76,7 +76,7 @@ func (l *LinuxNetNamespace) Start() error {
76
76
}
77
77
78
78
// Command returns an exec.Cmd configured to run within the network namespace
79
- func (l * LinuxNetNamespace ) Command (command []string ) * exec.Cmd {
79
+ func (l * Linux ) Command (command []string ) * exec.Cmd {
80
80
l .logger .Debug ("Command called" , "command" , command )
81
81
82
82
// Create command with ip netns exec
@@ -96,7 +96,7 @@ func (l *LinuxNetNamespace) Command(command []string) *exec.Cmd {
96
96
}
97
97
98
98
// Close removes the network namespace and iptables rules
99
- func (l * LinuxNetNamespace ) Close () error {
99
+ func (l * Linux ) Close () error {
100
100
// Remove iptables rules
101
101
err := l .removeIptables ()
102
102
if err != nil {
@@ -123,7 +123,7 @@ func (l *LinuxNetNamespace) Close() error {
123
123
}
124
124
125
125
// createNamespace creates a new network namespace
126
- func (l * LinuxNetNamespace ) createNamespace () error {
126
+ func (l * Linux ) createNamespace () error {
127
127
cmd := exec .Command ("ip" , "netns" , "add" , l .namespace )
128
128
err := cmd .Run ()
129
129
if err != nil {
@@ -133,12 +133,12 @@ func (l *LinuxNetNamespace) createNamespace() error {
133
133
}
134
134
135
135
// setupNetworking configures networking within the namespace
136
- func (l * LinuxNetNamespace ) setupNetworking () error {
136
+ func (l * Linux ) setupNetworking () error {
137
137
// Create veth pair with short names (Linux interface names limited to 15 chars)
138
138
// Generate unique ID to avoid conflicts
139
139
uniqueID := fmt .Sprintf ("%d" , time .Now ().UnixNano ()% 10000000 ) // 7 digits max
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
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
142
142
143
143
// Store veth interface name for iptables rules
144
144
l .vethHost = vethHost
@@ -169,7 +169,7 @@ func (l *LinuxNetNamespace) setupNetworking() error {
169
169
// setupDNS configures DNS resolution for the namespace
170
170
// This ensures reliable DNS resolution by using public DNS servers
171
171
// instead of relying on the host's potentially complex DNS configuration
172
- func (l * LinuxNetNamespace ) setupDNS () error {
172
+ func (l * Linux ) setupDNS () error {
173
173
// Always create namespace-specific resolv.conf with reliable public DNS servers
174
174
// This avoids issues with systemd-resolved, Docker DNS, and other complex setups
175
175
netnsEtc := fmt .Sprintf ("/etc/netns/%s" , l .namespace )
@@ -197,7 +197,7 @@ options timeout:2 attempts:2
197
197
}
198
198
199
199
// setupIptables configures iptables rules for comprehensive TCP traffic interception
200
- func (l * LinuxNetNamespace ) setupIptables () error {
200
+ func (l * Linux ) setupIptables () error {
201
201
// Enable IP forwarding
202
202
cmd := exec .Command ("sysctl" , "-w" , "net.ipv4.ip_forward=1" )
203
203
cmd .Run () // Ignore error
@@ -222,7 +222,7 @@ func (l *LinuxNetNamespace) setupIptables() error {
222
222
}
223
223
224
224
// removeIptables removes iptables rules
225
- func (l * LinuxNetNamespace ) removeIptables () error {
225
+ func (l * Linux ) removeIptables () error {
226
226
// Remove comprehensive TCP redirect rule
227
227
cmd := exec .Command ("iptables" , "-t" , "nat" , "-D" , "PREROUTING" , "-i" , l .vethHost , "-p" , "tcp" , "-j" , "REDIRECT" , "--to-ports" , fmt .Sprintf ("%d" , l .httpProxyPort ))
228
228
cmd .Run () // Ignore errors during cleanup
@@ -235,11 +235,11 @@ func (l *LinuxNetNamespace) removeIptables() error {
235
235
}
236
236
237
237
// removeNamespace removes the network namespace
238
- func (l * LinuxNetNamespace ) removeNamespace () error {
238
+ func (l * Linux ) removeNamespace () error {
239
239
cmd := exec .Command ("ip" , "netns" , "del" , l .namespace )
240
240
err := cmd .Run ()
241
241
if err != nil {
242
242
return fmt .Errorf ("failed to remove namespace: %v" , err )
243
243
}
244
244
return nil
245
- }
245
+ }
0 commit comments