Skip to content

Commit

Permalink
Merge branch 'main' into version-print-patch
Browse files Browse the repository at this point in the history
  • Loading branch information
sumire88 authored Jan 11, 2024
2 parents d3cc3d5 + 32ea550 commit 0cb0b9e
Show file tree
Hide file tree
Showing 16 changed files with 442 additions and 41 deletions.
4 changes: 0 additions & 4 deletions .github/ISSUE_TEMPLATE/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ contact_links:
url: https://github.com/daeuniverse/dae/tree/main/docs/zh
about: 'daeuniverse Documentation Link'

- name: '💬 daeuniverse Telegram Support'
url: https://t.me/daeuniverse
about: 'daeuniverse Telegram Support Channel'

- name: '💬 daeuniverse GitHub Discussion Support'
url: https://github.com/daeuniverse/dae/discussions
about: 'daeuniverse GitHub Discussion Portal'
3 changes: 2 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,11 @@ loop:
break loop
}
}
defer os.Remove(PidFilePath)
defer control.GetDaeNetns().Close()
if e := c.Close(); e != nil {
return fmt.Errorf("close control plane: %w", e)
}
_ = os.Remove(PidFilePath)
return nil
}

Expand Down
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
4 changes: 2 additions & 2 deletions component/outbound/dialer/connectivity_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ type CheckOption struct {
func (d *Dialer) ActivateCheck() {
d.tickerMu.Lock()
defer d.tickerMu.Unlock()
if d.InstanceOption.CheckEnabled {
if d.InstanceOption.DisableCheck || d.checkActivated {
return
}
d.InstanceOption.CheckEnabled = true
d.checkActivated = true
go d.aliveBackground()
}

Expand Down
7 changes: 3 additions & 4 deletions component/outbound/dialer/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ type Dialer struct {
checkCh chan time.Time
ctx context.Context
cancel context.CancelFunc

checkActivated bool
}

type GlobalOption struct {
Expand All @@ -48,7 +50,7 @@ type GlobalOption struct {
}

type InstanceOption struct {
CheckEnabled bool
DisableCheck bool
}

type Property struct {
Expand Down Expand Up @@ -78,9 +80,6 @@ func NewDialer(dialer netproxy.Dialer, option *GlobalOption, iOption InstanceOpt
ctx: ctx,
cancel: cancel,
}
if iOption.CheckEnabled {
go d.aliveBackground()
}
return d
}

Expand Down
2 changes: 1 addition & 1 deletion component/outbound/dialer_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var log = logger.NewLogger("trace", false, nil)

func newDirectDialer(option *dialer.GlobalOption, fullcone bool) *dialer.Dialer {
_d, p := dialer.NewDirectDialer(option, true)
d := dialer.NewDialer(_d, option, dialer.InstanceOption{CheckEnabled: false}, p)
d := dialer.NewDialer(_d, option, dialer.InstanceOption{DisableCheck: false}, p)
return d
}

Expand Down
2 changes: 1 addition & 1 deletion component/outbound/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewDialerSetFromLinks(option *dialer.GlobalOption, tagToNodeList map[string
}
for subscriptionTag, nodes := range tagToNodeList {
for _, node := range nodes {
d, err := dialer.NewFromLink(option, dialer.InstanceOption{CheckEnabled: false}, node, subscriptionTag)
d, err := dialer.NewFromLink(option, dialer.InstanceOption{DisableCheck: false}, node, subscriptionTag)
if err != nil {
option.Log.Infof("failed to parse node: %v", err)
continue
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
23 changes: 19 additions & 4 deletions control/control_plane.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,14 @@ func NewControlPlane(
if err = core.setupRoutingPolicy(); err != nil {
return nil, err
}
if global.AutoConfigFirewallRule {
if ok := core.addAcceptInputMark(); ok {
core.deferFuncs = append(core.deferFuncs, func() error {
core.delAcceptInputMark()
return nil
})
}
}
}

/// Bind to links. Binding should be advance of dialerGroups to avoid un-routable old connection.
Expand Down Expand Up @@ -253,9 +261,9 @@ func NewControlPlane(
}
disableKernelAliveCallback := dialMode != consts.DialMode_Ip
_direct, directProperty := dialer.NewDirectDialer(option, true)
direct := dialer.NewDialer(_direct, option, dialer.InstanceOption{CheckEnabled: false}, directProperty)
direct := dialer.NewDialer(_direct, option, dialer.InstanceOption{DisableCheck: true}, directProperty)
_block, blockProperty := dialer.NewBlockDialer(option, func() { /*Dialer Outbound*/ })
block := dialer.NewDialer(_block, option, dialer.InstanceOption{CheckEnabled: false}, blockProperty)
block := dialer.NewDialer(_block, option, dialer.InstanceOption{DisableCheck: true}, blockProperty)
outbounds := []*outbound.DialerGroup{
outbound.NewDialerGroup(option, consts.OutboundDirect.String(),
[]*dialer.Dialer{direct}, []*dialer.Annotation{{}},
Expand Down Expand Up @@ -292,8 +300,6 @@ func NewControlPlane(
log.Infof(`Group "%v" node list:`, group.Name)
for _, d := range dialers {
log.Infoln("\t" + d.Property().Name)
// We only activate check of nodes that have a group.
d.ActivateCheck()
}
if len(dialers) == 0 {
log.Infoln("\t<Empty>")
Expand Down Expand Up @@ -555,6 +561,14 @@ func (c *ControlPlane) dnsUpstreamReadyCallback(dnsUpstream *dns.Upstream) (err
return nil
}

func (c *ControlPlane) ActivateCheck() {
for _, g := range c.outbounds {
for _, d := range g.Dialers {
// We only activate check of nodes that have a group.
d.ActivateCheck()
}
}
}
func (c *ControlPlane) ChooseDialTarget(outbound consts.OutboundIndex, dst netip.AddrPort, domain string) (dialTarget string, shouldReroute bool, dialIp bool) {
dialMode := consts.DialMode_Ip

Expand Down Expand Up @@ -751,6 +765,7 @@ func (c *ControlPlane) Serve(readyChan chan<- bool, listener *Listener) (err err
}(newBuf, newOob, src)
}
}()
c.ActivateCheck()
<-c.ctx.Done()
return nil
}
Expand Down
39 changes: 39 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,43 @@ func (c *controlPlaneCore) delQdisc(ifname string) error {
return nil
}

// TODO: Support more than firewalld and fw4: need more user feedback.
var nftInputChains = [][3]string{
{"inet", "firewalld", "filter_INPUT"},
{"inet", "fw4", "input"},
}

func (c *controlPlaneCore) addAcceptInputMark() (ok bool) {
for _, rule := range nftInputChains {
if err := exec.Command("nft", "insert rule "+strings.Join(rule[:], " ")+" mark & "+consts.TproxyMarkString+" == "+consts.TproxyMarkString+" accept").Run(); err == nil {
ok = true
}
}
return ok
}

func (c *controlPlaneCore) delAcceptInputMark() (ok bool) {
for _, rule := range nftInputChains {
output, err := exec.Command("nft", "--handle", "--numeric", "list", "chain", rule[0], rule[1], rule[2]).Output()
if err != nil {
continue
}
lines := strings.Split(string(output), "\n")
regex := regexp.MustCompile("meta mark & " + consts.TproxyMarkString + " == " + consts.TproxyMarkString + " accept # handle ([0-9]+)")
for _, line := range lines {
matches := regex.FindStringSubmatch(line)
if len(matches) >= 2 {
handle := matches[1]
if err = exec.Command("nft", "delete rule "+strings.Join(rule[:], " ")+" handle "+handle).Run(); err == nil {
ok = true
}
break
}
}
}
return ok
}

func (c *controlPlaneCore) setupRoutingPolicy() (err error) {
/// Insert ip rule / ip route.
var table = 2023 + c.flip
Expand Down
Loading

0 comments on commit 0cb0b9e

Please sign in to comment.