Skip to content

Commit

Permalink
feat: support to auto config firewall (firewalld)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzz2017 committed Jan 8, 2024
1 parent 35094f3 commit e24c368
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions common/consts/ebpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ var (
)

const (
TproxyMark uint32 = 0x8000000
Recognize uint16 = 0x2017
LoopbackIfIndex = 1
TproxyMark uint32 = 0x08000000
TproxyMarkString string = "0x08000000" // Should be aligned with nftables
Recognize uint16 = 0x2017
LoopbackIfIndex = 1
)

type LanWanFlag uint8
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Global struct {
DialMode string `mapstructure:"dial_mode" default:"domain"`
DisableWaitingNetwork bool `mapstructure:"disable_waiting_network" default:"false"`
AutoConfigKernelParameter bool `mapstructure:"auto_config_kernel_parameter" default:"false"`
AutoConfigFirewallRule bool `mapstructure:"auto_config_firewall_rule" default:"false"`
SniffingTimeout time.Duration `mapstructure:"sniffing_timeout" default:"100ms"`
TlsImplementation string `mapstructure:"tls_implementation" default:"tls"`
UtlsImitate string `mapstructure:"utls_imitate" default:"chrome_auto"`
Expand Down
3 changes: 3 additions & 0 deletions control/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,9 @@ func NewControlPlane(
if err = core.setupRoutingPolicy(); err != nil {
return nil, err
}
if err = core.addAcceptInputMark(); err == nil {
core.deferFuncs = append(core.deferFuncs, core.delAcceptInputMark)
}
}

/// Bind to links. Binding should be advance of dialerGroups to avoid un-routable old connection.
Expand Down
24 changes: 24 additions & 0 deletions control/control_plane_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import (
"net"
"net/netip"
"os"
"os/exec"
"regexp"
"strings"
"sync"

"github.com/cilium/ebpf"
Expand Down Expand Up @@ -192,6 +194,28 @@ func (c *controlPlaneCore) delQdisc(ifname string) error {
return nil
}

func (c *controlPlaneCore) addAcceptInputMark() error {
// TODO: Support more than firewalld.
return exec.Command("sh", "-c", "nft list table inet firewalld && nft 'insert rule inet firewalld filter_INPUT mark "+consts.TproxyMarkString+" accept'").Run()
}

func (c *controlPlaneCore) delAcceptInputMark() error {
output, err := exec.Command("sh", "-c", "nft --handle --numeric list chain inet firewalld filter_INPUT").Output()
if err != nil {
// No firewalld.
return nil
}
lines := strings.Split(string(output), "\n")
regex := regexp.MustCompile("meta mark " + consts.TproxyMarkString + " accept # handle ([0-9]+)")
for _, line := range lines {
matches := regex.FindStringSubmatch(line)
if len(matches) > 0 {
return exec.Command("sh", "-c", "nft 'delete rule inet firewalld filter_INPUT handle "+matches[1]+"'").Run()
}
}
return fmt.Errorf("no such rule")
}

func (c *controlPlaneCore) setupRoutingPolicy() (err error) {
/// Insert ip rule / ip route.
var table = 2023 + c.flip
Expand Down
3 changes: 3 additions & 0 deletions example.dae
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ global {
# https://github.com/daeuniverse/dae/blob/main/docs/en/user-guide/kernel-parameters.md to see what will dae do.
auto_config_kernel_parameter: true

# Automatically configure firewall rules like firewalld.
# For example: nft list table inet firewalld && nft 'insert rule inet firewalld filter_INPUT mark 0x8000000 accept'
auto_config_firewall_rule: true

##### Node connectivity check.

Expand Down

0 comments on commit e24c368

Please sign in to comment.