Skip to content

Commit 8f716fc

Browse files
blink-so[bot]f0ssel
andcommitted
Remove unused HTTPS proxy server
- Remove separate HTTPS proxy server since TLS termination handles both HTTP/HTTPS - Clean up HTTPS-related fields from proxy Server struct and Config - Remove httpsProxyPort from all namespace implementations - Update proxy to use single HTTP server with TLS termination - Remove forwardHTTPSRequest method and use forwardHTTPRequest for all traffic - Add UserInfo to jail Config to fix namespace configuration - Simplify codebase by eliminating duplicate functionality Co-authored-by: f0ssel <[email protected]>
1 parent 91899a3 commit 8f716fc

File tree

8 files changed

+73
-195
lines changed

8 files changed

+73
-195
lines changed

cli/cli.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ func Run(ctx context.Context, config Config, args []string) error {
135135
Auditor: auditor,
136136
CertManager: certManager,
137137
Logger: logger,
138+
UserInfo: userInfo,
138139
Unprivileged: config.Unprivileged,
139140
})
140141
if err != nil {

jail

48.4 KB
Binary file not shown.

jail.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Config struct {
2020
Auditor audit.Auditor
2121
CertManager tls.Manager
2222
Logger *slog.Logger
23+
UserInfo namespace.UserInfo
2324
Unprivileged bool
2425
}
2526

@@ -41,23 +42,22 @@ func New(ctx context.Context, config Config) (*Jail, error) {
4142
// Create proxy server
4243
proxyServer := proxy.NewProxyServer(proxy.Config{
4344
HTTPPort: 8080,
44-
HTTPSPort: 8443,
45-
Auditor: config.Auditor,
4645
RuleEngine: config.RuleEngine,
46+
Auditor: config.Auditor,
4747
Logger: config.Logger,
4848
TLSConfig: tlsConfig,
4949
})
5050

51-
// Create commander
51+
// Create namespace
5252
commander, err := newNamespaceCommander(namespace.Config{
53-
Logger: config.Logger,
54-
HttpProxyPort: 8080,
55-
HttpsProxyPort: 8443,
56-
TlsConfigDir: configDir,
57-
CACertPath: caCertPath,
53+
Logger: config.Logger,
54+
HttpProxyPort: 8080,
55+
TlsConfigDir: configDir,
56+
CACertPath: caCertPath,
57+
UserInfo: config.UserInfo,
5858
}, config.Unprivileged)
5959
if err != nil {
60-
return nil, fmt.Errorf("failed to create commander: %v", err)
60+
return nil, fmt.Errorf("failed to create namespace commander: %v", err)
6161
}
6262

6363
// Create cancellable context for jail

namespace/linux.go

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

namespace/macos.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ type MacOSNetJail struct {
2626
commandEnv []string
2727
procAttr *syscall.SysProcAttr
2828
httpProxyPort int
29-
httpsProxyPort int
3029
tlsConfigDir string
3130
caCertPath string
3231
userInfo UserInfo
@@ -39,14 +38,13 @@ func NewMacOS(config Config) (*MacOSNetJail, error) {
3938
mainRulesPath := fmt.Sprintf("/tmp/%s_main.pf", ns)
4039

4140
return &MacOSNetJail{
42-
pfRulesPath: pfRulesPath,
43-
mainRulesPath: mainRulesPath,
44-
logger: config.Logger,
45-
httpProxyPort: config.HttpProxyPort,
46-
httpsProxyPort: config.HttpsProxyPort,
47-
tlsConfigDir: config.TlsConfigDir,
48-
caCertPath: config.CACertPath,
49-
userInfo: config.UserInfo,
41+
pfRulesPath: pfRulesPath,
42+
mainRulesPath: mainRulesPath,
43+
logger: config.Logger,
44+
httpProxyPort: config.HttpProxyPort,
45+
tlsConfigDir: config.TlsConfigDir,
46+
caCertPath: config.CACertPath,
47+
userInfo: config.UserInfo,
5048
}, nil
5149
}
5250

namespace/namespace.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@ type Commander interface {
1212
}
1313

1414
type Config struct {
15-
Logger *slog.Logger
16-
HttpProxyPort int
17-
HttpsProxyPort int
18-
TlsConfigDir string
19-
CACertPath string
20-
UserInfo UserInfo
15+
Logger *slog.Logger
16+
HttpProxyPort int
17+
TlsConfigDir string
18+
CACertPath string
19+
UserInfo UserInfo
2120
}
2221

2322
type UserInfo struct {

namespace/unprivileged.go

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,21 @@ import (
88
)
99

1010
type Unprivileged struct {
11-
logger *slog.Logger
12-
commandEnv []string
13-
httpProxyPort int
14-
httpsProxyPort int
15-
tlsConfigDir string
16-
caCertPath string
17-
userInfo UserInfo
11+
logger *slog.Logger
12+
commandEnv []string
13+
httpProxyPort int
14+
tlsConfigDir string
15+
caCertPath string
16+
userInfo UserInfo
1817
}
1918

2019
func NewUnprivileged(config Config) (*Unprivileged, error) {
2120
return &Unprivileged{
22-
logger: config.Logger,
23-
httpProxyPort: config.HttpProxyPort,
24-
httpsProxyPort: config.HttpsProxyPort,
25-
tlsConfigDir: config.TlsConfigDir,
26-
caCertPath: config.CACertPath,
27-
userInfo: config.UserInfo,
21+
logger: config.Logger,
22+
httpProxyPort: config.HttpProxyPort,
23+
tlsConfigDir: config.TlsConfigDir,
24+
caCertPath: config.CACertPath,
25+
userInfo: config.UserInfo,
2826
}, nil
2927
}
3028

0 commit comments

Comments
 (0)