1
1
//go:build linux
2
2
3
- package netjail
3
+ package network
4
4
5
5
import (
6
6
"fmt"
@@ -11,27 +11,31 @@ import (
11
11
"time"
12
12
)
13
13
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
17
21
namespace string
18
22
logger * slog.Logger
19
23
}
20
24
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 ) {
23
27
// 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 )
25
29
26
- return & LinuxNetJail {
30
+ return & LinuxJail {
27
31
config : config ,
28
32
namespace : namespace ,
29
33
logger : logger ,
30
34
}, nil
31
35
}
32
36
33
37
// 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 {
35
39
l .logger .Debug ("Setup called" , "httpPort" , httpPort , "httpsPort" , httpsPort )
36
40
l .config .HTTPPort = httpPort
37
41
l .config .HTTPSPort = httpsPort
@@ -70,7 +74,7 @@ func (l *LinuxNetJail) Setup(httpPort, httpsPort int) error {
70
74
}
71
75
72
76
// 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 {
74
78
l .logger .Debug ("Execute called" , "command" , command )
75
79
if len (command ) == 0 {
76
80
return fmt .Errorf ("no command specified" )
@@ -81,7 +85,7 @@ func (l *LinuxNetJail) Execute(command []string, extraEnv map[string]string) err
81
85
cmdArgs := []string {"ip" , "netns" , "exec" , l .namespace }
82
86
cmdArgs = append (cmdArgs , command ... )
83
87
l .logger .Debug ("Full command args" , "args" , cmdArgs )
84
-
88
+
85
89
cmd := exec .Command ("ip" , cmdArgs [1 :]... )
86
90
87
91
// Set up environment
@@ -124,7 +128,7 @@ func (l *LinuxNetJail) Execute(command []string, extraEnv map[string]string) err
124
128
}
125
129
126
130
// Cleanup removes the network namespace and iptables rules
127
- func (l * LinuxNetJail ) Cleanup () error {
131
+ func (l * LinuxJail ) Cleanup () error {
128
132
if l .config .SkipCleanup {
129
133
return nil
130
134
}
@@ -152,7 +156,7 @@ func (l *LinuxNetJail) Cleanup() error {
152
156
}
153
157
154
158
// createNamespace creates a new network namespace
155
- func (l * LinuxNetJail ) createNamespace () error {
159
+ func (l * LinuxJail ) createNamespace () error {
156
160
cmd := exec .Command ("ip" , "netns" , "add" , l .namespace )
157
161
if err := cmd .Run (); err != nil {
158
162
return fmt .Errorf ("failed to create namespace: %v" , err )
@@ -161,12 +165,12 @@ func (l *LinuxNetJail) createNamespace() error {
161
165
}
162
166
163
167
// setupNetworking configures networking within the namespace
164
- func (l * LinuxNetJail ) setupNetworking () error {
168
+ func (l * LinuxJail ) setupNetworking () error {
165
169
// Create veth pair with short names (Linux interface names limited to 15 chars)
166
170
// Generate unique ID to avoid conflicts
167
171
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
170
174
171
175
cmd := exec .Command ("ip" , "link" , "add" , vethHost , "type" , "veth" , "peer" , "name" , vethNetJail )
172
176
if err := cmd .Run (); err != nil {
@@ -218,7 +222,7 @@ func (l *LinuxNetJail) setupNetworking() error {
218
222
// setupDNS configures DNS resolution for the namespace
219
223
// This ensures reliable DNS resolution by using public DNS servers
220
224
// instead of relying on the host's potentially complex DNS configuration
221
- func (l * LinuxNetJail ) setupDNS () error {
225
+ func (l * LinuxJail ) setupDNS () error {
222
226
// Always create namespace-specific resolv.conf with reliable public DNS servers
223
227
// This avoids issues with systemd-resolved, Docker DNS, and other complex setups
224
228
netnsEtc := fmt .Sprintf ("/etc/netns/%s" , l .namespace )
@@ -228,7 +232,7 @@ func (l *LinuxNetJail) setupDNS() error {
228
232
229
233
// Write custom resolv.conf with multiple reliable public DNS servers
230
234
resolvConfPath := fmt .Sprintf ("%s/resolv.conf" , netnsEtc )
231
- dnsConfig := `# Custom DNS for boundary namespace
235
+ dnsConfig := `# Custom DNS for network namespace
232
236
nameserver 8.8.8.8
233
237
nameserver 8.8.4.4
234
238
nameserver 1.1.1.1
@@ -244,7 +248,7 @@ options timeout:2 attempts:2
244
248
}
245
249
246
250
// setupIptables configures iptables rules for traffic redirection
247
- func (l * LinuxNetJail ) setupIptables () error {
251
+ func (l * LinuxJail ) setupIptables () error {
248
252
// Enable IP forwarding
249
253
cmd := exec .Command ("sysctl" , "-w" , "net.ipv4.ip_forward=1" )
250
254
cmd .Run () // Ignore error
@@ -273,7 +277,7 @@ func (l *LinuxNetJail) setupIptables() error {
273
277
}
274
278
275
279
// removeIptables removes iptables rules
276
- func (l * LinuxNetJail ) removeIptables () error {
280
+ func (l * LinuxJail ) removeIptables () error {
277
281
// Remove NAT rule
278
282
cmd := exec .Command ("iptables" , "-t" , "nat" , "-D" , "POSTROUTING" , "-s" , "192.168.100.0/24" , "-j" , "MASQUERADE" )
279
283
cmd .Run () // Ignore errors during cleanup
@@ -282,10 +286,10 @@ func (l *LinuxNetJail) removeIptables() error {
282
286
}
283
287
284
288
// removeNamespace removes the network namespace
285
- func (l * LinuxNetJail ) removeNamespace () error {
289
+ func (l * LinuxJail ) removeNamespace () error {
286
290
cmd := exec .Command ("ip" , "netns" , "del" , l .namespace )
287
291
if err := cmd .Run (); err != nil {
288
292
return fmt .Errorf ("failed to remove namespace: %v" , err )
289
293
}
290
294
return nil
291
- }
295
+ }
0 commit comments