11//go:build linux
22
3- package netjail
3+ package network
44
55import (
66 "fmt"
@@ -11,27 +11,31 @@ import (
1111 "time"
1212)
1313
14- // LinuxNetJail implements NetJail using Linux network namespaces
15- type LinuxNetJail struct {
16- config Config
14+ const (
15+ namespacePrefix = "coder_jail"
16+ )
17+
18+ // LinuxJail implements NetJail using Linux network namespaces
19+ type LinuxJail struct {
20+ config JailConfig
1721 namespace string
1822 logger * slog.Logger
1923}
2024
21- // newLinuxNetJail creates a new Linux network jail instance
22- func newLinuxNetJail (config Config , logger * slog.Logger ) (* LinuxNetJail , error ) {
25+ // newLinuxJail creates a new Linux network jail instance
26+ func newLinuxJail (config JailConfig , logger * slog.Logger ) (* LinuxJail , error ) {
2327 // Generate unique namespace name
24- namespace := fmt .Sprintf ("boundary_%d" , time .Now ().UnixNano ()% 10000000 )
28+ namespace := fmt .Sprintf ("%s_%d" , namespacePrefix , time .Now ().UnixNano ()% 10000000 )
2529
26- return & LinuxNetJail {
30+ return & LinuxJail {
2731 config : config ,
2832 namespace : namespace ,
2933 logger : logger ,
3034 }, nil
3135}
3236
3337// Setup creates network namespace and configures iptables rules
34- func (l * LinuxNetJail ) Setup (httpPort , httpsPort int ) error {
38+ func (l * LinuxJail ) Setup (httpPort , httpsPort int ) error {
3539 l .logger .Debug ("Setup called" , "httpPort" , httpPort , "httpsPort" , httpsPort )
3640 l .config .HTTPPort = httpPort
3741 l .config .HTTPSPort = httpsPort
@@ -70,7 +74,7 @@ func (l *LinuxNetJail) Setup(httpPort, httpsPort int) error {
7074}
7175
7276// Execute runs a command within the network namespace
73- func (l * LinuxNetJail ) Execute (command []string , extraEnv map [string ]string ) error {
77+ func (l * LinuxJail ) Execute (command []string , extraEnv map [string ]string ) error {
7478 l .logger .Debug ("Execute called" , "command" , command )
7579 if len (command ) == 0 {
7680 return fmt .Errorf ("no command specified" )
@@ -81,7 +85,7 @@ func (l *LinuxNetJail) Execute(command []string, extraEnv map[string]string) err
8185 cmdArgs := []string {"ip" , "netns" , "exec" , l .namespace }
8286 cmdArgs = append (cmdArgs , command ... )
8387 l .logger .Debug ("Full command args" , "args" , cmdArgs )
84-
88+
8589 cmd := exec .Command ("ip" , cmdArgs [1 :]... )
8690
8791 // Set up environment
@@ -124,7 +128,7 @@ func (l *LinuxNetJail) Execute(command []string, extraEnv map[string]string) err
124128}
125129
126130// Cleanup removes the network namespace and iptables rules
127- func (l * LinuxNetJail ) Cleanup () error {
131+ func (l * LinuxJail ) Cleanup () error {
128132 if l .config .SkipCleanup {
129133 return nil
130134 }
@@ -152,7 +156,7 @@ func (l *LinuxNetJail) Cleanup() error {
152156}
153157
154158// createNamespace creates a new network namespace
155- func (l * LinuxNetJail ) createNamespace () error {
159+ func (l * LinuxJail ) createNamespace () error {
156160 cmd := exec .Command ("ip" , "netns" , "add" , l .namespace )
157161 if err := cmd .Run (); err != nil {
158162 return fmt .Errorf ("failed to create namespace: %v" , err )
@@ -161,12 +165,12 @@ func (l *LinuxNetJail) createNamespace() error {
161165}
162166
163167// setupNetworking configures networking within the namespace
164- func (l * LinuxNetJail ) setupNetworking () error {
168+ func (l * LinuxJail ) setupNetworking () error {
165169 // Create veth pair with short names (Linux interface names limited to 15 chars)
166170 // Generate unique ID to avoid conflicts
167171 uniqueID := fmt .Sprintf ("%d" , time .Now ().UnixNano ()% 10000000 ) // 7 digits max
168- vethHost := fmt .Sprintf ("veth_h_%s" , uniqueID ) // veth_h_1234567 = 14 chars
169- vethNetJail := fmt .Sprintf ("veth_n_%s" , uniqueID ) // veth_n_1234567 = 14 chars
172+ vethHost := fmt .Sprintf ("veth_h_%s" , uniqueID ) // veth_h_1234567 = 14 chars
173+ vethNetJail := fmt .Sprintf ("veth_n_%s" , uniqueID ) // veth_n_1234567 = 14 chars
170174
171175 cmd := exec .Command ("ip" , "link" , "add" , vethHost , "type" , "veth" , "peer" , "name" , vethNetJail )
172176 if err := cmd .Run (); err != nil {
@@ -218,7 +222,7 @@ func (l *LinuxNetJail) setupNetworking() error {
218222// setupDNS configures DNS resolution for the namespace
219223// This ensures reliable DNS resolution by using public DNS servers
220224// instead of relying on the host's potentially complex DNS configuration
221- func (l * LinuxNetJail ) setupDNS () error {
225+ func (l * LinuxJail ) setupDNS () error {
222226 // Always create namespace-specific resolv.conf with reliable public DNS servers
223227 // This avoids issues with systemd-resolved, Docker DNS, and other complex setups
224228 netnsEtc := fmt .Sprintf ("/etc/netns/%s" , l .namespace )
@@ -228,7 +232,7 @@ func (l *LinuxNetJail) setupDNS() error {
228232
229233 // Write custom resolv.conf with multiple reliable public DNS servers
230234 resolvConfPath := fmt .Sprintf ("%s/resolv.conf" , netnsEtc )
231- dnsConfig := `# Custom DNS for boundary namespace
235+ dnsConfig := `# Custom DNS for network namespace
232236nameserver 8.8.8.8
233237nameserver 8.8.4.4
234238nameserver 1.1.1.1
@@ -244,7 +248,7 @@ options timeout:2 attempts:2
244248}
245249
246250// setupIptables configures iptables rules for traffic redirection
247- func (l * LinuxNetJail ) setupIptables () error {
251+ func (l * LinuxJail ) setupIptables () error {
248252 // Enable IP forwarding
249253 cmd := exec .Command ("sysctl" , "-w" , "net.ipv4.ip_forward=1" )
250254 cmd .Run () // Ignore error
@@ -273,7 +277,7 @@ func (l *LinuxNetJail) setupIptables() error {
273277}
274278
275279// removeIptables removes iptables rules
276- func (l * LinuxNetJail ) removeIptables () error {
280+ func (l * LinuxJail ) removeIptables () error {
277281 // Remove NAT rule
278282 cmd := exec .Command ("iptables" , "-t" , "nat" , "-D" , "POSTROUTING" , "-s" , "192.168.100.0/24" , "-j" , "MASQUERADE" )
279283 cmd .Run () // Ignore errors during cleanup
@@ -282,10 +286,10 @@ func (l *LinuxNetJail) removeIptables() error {
282286}
283287
284288// removeNamespace removes the network namespace
285- func (l * LinuxNetJail ) removeNamespace () error {
289+ func (l * LinuxJail ) removeNamespace () error {
286290 cmd := exec .Command ("ip" , "netns" , "del" , l .namespace )
287291 if err := cmd .Run (); err != nil {
288292 return fmt .Errorf ("failed to remove namespace: %v" , err )
289293 }
290294 return nil
291- }
295+ }
0 commit comments