From 7b8f9beedae4472b51a01656d12d6c6a9cb71122 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Tue, 15 Oct 2024 14:03:06 +0000 Subject: [PATCH] wip --- internal/vpn/interfaces.go | 1 + internal/wireguard/netlinker.go | 1 + internal/wireguard/netlinker_mock_test.go | 15 +++++++++++++++ internal/wireguard/rule.go | 13 +++++++++++++ 4 files changed, 30 insertions(+) diff --git a/internal/vpn/interfaces.go b/internal/vpn/interfaces.go index 68103690b..11b8fa621 100644 --- a/internal/vpn/interfaces.go +++ b/internal/vpn/interfaces.go @@ -72,6 +72,7 @@ type Router interface { type Ruler interface { RuleAdd(rule netlink.Rule) error RuleDel(rule netlink.Rule) error + RuleList(family int) (rules []netlink.Rule, err error) } type Linker interface { diff --git a/internal/wireguard/netlinker.go b/internal/wireguard/netlinker.go index 6b077016b..f4dbd161b 100644 --- a/internal/wireguard/netlinker.go +++ b/internal/wireguard/netlinker.go @@ -20,6 +20,7 @@ type Router interface { type Ruler interface { RuleAdd(rule netlink.Rule) error RuleDel(rule netlink.Rule) error + RuleList(family int) ([]netlink.Rule, error) } type Linker interface { diff --git a/internal/wireguard/netlinker_mock_test.go b/internal/wireguard/netlinker_mock_test.go index 124a659b7..ba0cd313e 100644 --- a/internal/wireguard/netlinker_mock_test.go +++ b/internal/wireguard/netlinker_mock_test.go @@ -207,3 +207,18 @@ func (mr *MockNetLinkerMockRecorder) RuleDel(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RuleDel", reflect.TypeOf((*MockNetLinker)(nil).RuleDel), arg0) } + +// RuleList mocks base method. +func (m *MockNetLinker) RuleList(arg0 int) ([]netlink.Rule, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RuleList", arg0) + ret0, _ := ret[0].([]netlink.Rule) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RuleList indicates an expected call of RuleList. +func (mr *MockNetLinkerMockRecorder) RuleList(arg0 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RuleList", reflect.TypeOf((*MockNetLinker)(nil).RuleList), arg0) +} diff --git a/internal/wireguard/rule.go b/internal/wireguard/rule.go index 7586b11aa..1f7b3e243 100644 --- a/internal/wireguard/rule.go +++ b/internal/wireguard/rule.go @@ -2,6 +2,7 @@ package wireguard import ( "fmt" + "strings" "github.com/qdm12/gluetun/internal/netlink" ) @@ -16,6 +17,18 @@ func (w *Wireguard) addRule(rulePriority int, firewallMark uint32, rule.Table = int(firewallMark) rule.Family = family if err := w.netlink.RuleAdd(rule); err != nil { + if strings.Contains(err.Error(), "file exists") { + rules, listErr := w.netlink.RuleList(family) + if listErr != nil { + return nil, fmt.Errorf("listing rules for family %d due to %q: %w", + family, err, listErr) + } + ruleStrings := make([]string, len(rules)) + for i := range rules { + ruleStrings[i] = rules[i].String() + } + w.logger.Info("existing rules are:\n" + strings.Join(ruleStrings, "\n")) + } return nil, fmt.Errorf("adding rule %s: %w", rule, err) }