From 8b818ffc4162367d2159095048c803ad1d102e31 Mon Sep 17 00:00:00 2001 From: Zakk Date: Thu, 24 Sep 2026 00:53:37 +1000 Subject: [PATCH] fix(iptables): match the transparent-proxy whitelist with ipset The iptables path wrote each whitelisted CIDR with its own iptables process. With the mainland China group (about 15,000 CIDRs) a core start spent minutes on this, and every packet of a direct connection walked the list linearly. Load each family into one hash:net set with a single `ipset restore` and match it with one rule at the old position. Without ipset, write the rules with one `iptables-restore --noflush` per family; without that, keep the per-rule commands. Cleanup destroys the sets. --- service/common/cmds/command.go | 10 + service/common/cmds/command_test.go | 22 ++ service/kernel/iptables/iptables.go | 56 +++- service/kernel/iptables/legacy_whitelist.go | 119 +++++++ .../kernel/iptables/legacy_whitelist_test.go | 314 ++++++++++++++++++ service/kernel/iptables/redirect.go | 23 +- service/kernel/iptables/tproxy.go | 25 +- 7 files changed, 526 insertions(+), 43 deletions(-) create mode 100644 service/common/cmds/command_test.go create mode 100644 service/kernel/iptables/legacy_whitelist.go create mode 100644 service/kernel/iptables/legacy_whitelist_test.go diff --git a/service/common/cmds/command.go b/service/common/cmds/command.go index 6df2fc17..12c10d3e 100644 --- a/service/common/cmds/command.go +++ b/service/common/cmds/command.go @@ -12,6 +12,16 @@ func IsCommandValid(command string) bool { return err == nil } +func ExecCommandWithInput(command string, args []string, input string) error { + cmd := exec.Command(command, args...) + cmd.Stdin = strings.NewReader(input) + out, err := cmd.CombinedOutput() + if err != nil { + return fmt.Errorf("ExecCommandWithInput: %s %s: %s: %w", command, strings.Join(args, " "), out, err) + } + return nil +} + func ExecCommands(commands string, stopWhenError bool) error { lines := strings.Split(commands, "\n") var e error diff --git a/service/common/cmds/command_test.go b/service/common/cmds/command_test.go new file mode 100644 index 00000000..9b1eeda0 --- /dev/null +++ b/service/common/cmds/command_test.go @@ -0,0 +1,22 @@ +package cmds + +import ( + "os" + "path/filepath" + "testing" +) + +func TestExecCommandWithInput(t *testing.T) { + output := filepath.Join(t.TempDir(), "stdin") + err := ExecCommandWithInput("sh", []string{"-c", `IFS= read -r value; printf %s "$value" > "$1"`, "sh", output}, "payload\n") + if err != nil { + t.Fatal(err) + } + got, err := os.ReadFile(output) + if err != nil { + t.Fatal(err) + } + if string(got) != "payload" { + t.Fatalf("stdin payload = %q, want payload", got) + } +} diff --git a/service/kernel/iptables/iptables.go b/service/kernel/iptables/iptables.go index f9699006..b857d61e 100644 --- a/service/kernel/iptables/iptables.go +++ b/service/kernel/iptables/iptables.go @@ -13,13 +13,19 @@ import ( const watcherInterval = 3 * time.Second -var watcher *LocalIPWatcher -var mutex sync.Mutex +var ( + watcher *LocalIPWatcher + mutex sync.Mutex + commandAvailable = cmds.IsCommandValid + executeCommands = cmds.ExecCommands + executeCommandWithInput = cmds.ExecCommandWithInput +) type Setter struct { Cmds string AfterFunc func() error PreFunc func() error + steps []setterStep } func NewErrorSetter(err error) Setter { @@ -53,15 +59,26 @@ func CloseWatcher() { func (c Setter) Run(stopAtError bool) error { mutex.Lock() defer mutex.Unlock() - commands := c.Cmds + return c.run(stopAtError, rewriteIptablesCommands) +} + +func rewriteIptablesCommands(commands string) string { if common.IsDocker() { - commands = strings.ReplaceAll(commands, "iptables", "iptables-legacy") - commands = strings.ReplaceAll(commands, "ip6tables", "ip6tables-legacy") - } else if (!cmds.IsCommandValid("iptables") || IsNftablesSupported()) && - cmds.IsCommandValid("iptables-nft") { - commands = strings.ReplaceAll(commands, "iptables", "iptables-nft") - commands = strings.ReplaceAll(commands, "ip6tables", "ip6tables-nft") + return rewriteIptablesBinaries(commands, "legacy") } + if (!commandAvailable("iptables") || IsNftablesSupported()) && + commandAvailable("iptables-nft") { + return rewriteIptablesBinaries(commands, "nft") + } + return commands +} + +func rewriteIptablesBinaries(commands, variant string) string { + commands = strings.ReplaceAll(commands, "iptables", "iptables-"+variant) + return strings.ReplaceAll(commands, "ip6tables", "ip6tables-"+variant) +} + +func (c Setter) run(stopAtError bool, rewrite func(string) string) error { var errs []error if c.PreFunc != nil { e := c.PreFunc() @@ -72,11 +89,26 @@ func (c Setter) Run(stopAtError bool) error { } } } - if len(commands) > 0 { - e := cmds.ExecCommands(commands, stopAtError) + if len(c.steps) > 0 { + for _, step := range c.steps { + var e error + if step.whitelist != nil { + e = step.whitelist.run(stopAtError, rewrite) + } else if len(step.commands) > 0 { + e = executeCommands(rewrite(step.commands), stopAtError) + } + if e != nil { + errs = append(errs, e) + if stopAtError { + return errs[0] + } + } + } + } else if len(c.Cmds) > 0 { + e := executeCommands(rewrite(c.Cmds), stopAtError) if e != nil { errs = append(errs, e) - if stopAtError && len(errs) > 0 { + if stopAtError { return errs[0] } } diff --git a/service/kernel/iptables/legacy_whitelist.go b/service/kernel/iptables/legacy_whitelist.go new file mode 100644 index 00000000..0b44d0ea --- /dev/null +++ b/service/kernel/iptables/legacy_whitelist.go @@ -0,0 +1,119 @@ +package iptables + +import ( + "fmt" + "strings" + "sync" + + "github.com/v2rayA/v2rayA/pkg/util/log" +) + +const ( + legacyWhitelist4Marker = "__V2RAYA_WHITE4__" + legacyWhitelist6Marker = "__V2RAYA_WHITE6__" +) + +var ( + legacySetupValues = getTproxySetupValues + legacyIPv6Supported = IsIPv6Supported +) + +type setterStep struct { + commands string + whitelist *legacyWhitelist +} + +type legacyWhitelist struct { + binary string + table string + setName string + family string + cidrs []string + warnOnce *sync.Once +} + +func newLegacyWhitelistSetter(commands, table string, ipv4, ipv6 []string) Setter { + warnOnce := &sync.Once{} + families := []struct { + marker string + binary string + setName string + setFamily string + cidrs []string + }{ + {legacyWhitelist4Marker, "iptables", "v2raya_white4", "inet", ipv4}, + {legacyWhitelist6Marker, "ip6tables", "v2raya_white6", "inet6", ipv6}, + } + var steps []setterStep + for _, family := range families { + before, after, found := strings.Cut(commands, family.marker) + if !found { + continue + } + steps = append(steps, setterStep{commands: before}) + if len(family.cidrs) > 0 { + steps = append(steps, setterStep{whitelist: &legacyWhitelist{ + binary: family.binary, + table: table, + setName: family.setName, + family: family.setFamily, + cidrs: family.cidrs, + warnOnce: warnOnce, + }}) + } + commands = after + } + steps = append(steps, setterStep{commands: commands}) + return Setter{steps: steps} +} + +func (w *legacyWhitelist) run(stopAtError bool, rewrite func(string) string) error { + if commandAvailable("ipset") { + if err := executeCommandWithInput("ipset", []string{"restore", "-!"}, w.ipsetPayload()); err == nil { + args := []string{"-w", "2", "-t", w.table, "-A", "TP_RULE", "-m", "set", "--match-set", w.setName, "dst", "-j", "RETURN"} + if err := executeCommandWithInput(rewrite(w.binary), args, ""); err == nil { + return nil + } + } + _ = executeCommandWithInput("ipset", []string{"destroy", w.setName}, "") + } + + w.warnOnce.Do(func() { + log.Warn("transparent proxy IP whitelist matching remains linear; install ipset with xt_set support or use nftables") + }) + restore := rewrite(w.binary + "-restore") + if commandAvailable(restore) { + if err := executeCommandWithInput(restore, []string{"-w", "2", "--noflush"}, w.restorePayload()); err == nil { + return nil + } + } + return executeCommands(rewrite(w.perLineCommands()), stopAtError) +} + +func (w *legacyWhitelist) ipsetPayload() string { + var payload strings.Builder + fmt.Fprintf(&payload, "create %s hash:net family %s maxelem 65536\n", w.setName, w.family) + fmt.Fprintf(&payload, "flush %s\n", w.setName) + for _, cidr := range w.cidrs { + fmt.Fprintf(&payload, "add %s %s\n", w.setName, cidr) + } + return payload.String() +} + +func (w *legacyWhitelist) restorePayload() string { + var payload strings.Builder + fmt.Fprintf(&payload, "*%s\n", w.table) + for _, cidr := range w.cidrs { + fmt.Fprintf(&payload, "-A TP_RULE -d %s -j RETURN\n", cidr) + } + payload.WriteString("COMMIT\n") + return payload.String() +} + +func (w *legacyWhitelist) perLineCommands() string { + var commands strings.Builder + for _, cidr := range w.cidrs { + fmt.Fprintf(&commands, "%s -w 2 -t %s -A TP_RULE -d %s -j RETURN\n", w.binary, w.table, cidr) + } + return commands.String() +} diff --git a/service/kernel/iptables/legacy_whitelist_test.go b/service/kernel/iptables/legacy_whitelist_test.go new file mode 100644 index 00000000..501bac45 --- /dev/null +++ b/service/kernel/iptables/legacy_whitelist_test.go @@ -0,0 +1,314 @@ +package iptables + +import ( + "errors" + "reflect" + "strings" + "testing" +) + +type recordedCommand struct { + name string + args []string + input string +} + +func recordLegacyWhitelistCommands(t *testing.T, available map[string]bool, failSetMatch bool, failCommands ...string) *[]recordedCommand { + t.Helper() + oldAvailable := commandAvailable + oldCommands := executeCommands + oldWithInput := executeCommandWithInput + var calls []recordedCommand + commandAvailable = func(command string) bool { return available[command] } + executeCommands = func(commands string, _ bool) error { + calls = append(calls, recordedCommand{name: "commands", input: commands}) + return nil + } + executeCommandWithInput = func(command string, args []string, input string) error { + calls = append(calls, recordedCommand{name: command, args: args, input: input}) + if failSetMatch && command != "ipset" && containsArg(args, "--match-set") { + return errors.New("xt_set unavailable") + } + for _, failCommand := range failCommands { + if command+" "+strings.Join(args, " ") == failCommand { + return errors.New("command failed") + } + } + return nil + } + t.Cleanup(func() { + commandAvailable = oldAvailable + executeCommands = oldCommands + executeCommandWithInput = oldWithInput + }) + return &calls +} + +func containsArg(args []string, want string) bool { + for _, arg := range args { + if arg == want { + return true + } + } + return false +} + +func testWhitelistSetter(table string, ipv4, ipv6 []string) Setter { + commands := "before\n" + legacyWhitelist4Marker + "\nbetween\n" + legacyWhitelist6Marker + "\nafter\n" + return newLegacyWhitelistSetter(commands, table, ipv4, ipv6) +} + +func TestLegacyWhitelistUsesIPSetInChainOrder(t *testing.T) { + ipv4 := []string{"1.0.0.0/8", "2.0.0.0/8", "3.0.0.0/8"} + ipv6 := []string{"2001:db8:1::/48", "2001:db8:2::/48", "2001:db8:3::/48"} + calls := recordLegacyWhitelistCommands(t, map[string]bool{"ipset": true}, false) + + if err := testWhitelistSetter("mangle", ipv4, ipv6).run(false, func(command string) string { return command }); err != nil { + t.Fatal(err) + } + if len(*calls) != 7 { + t.Fatalf("calls = %#v", *calls) + } + if !strings.Contains((*calls)[0].input, "before") || !strings.Contains((*calls)[3].input, "between") || !strings.Contains((*calls)[6].input, "after") { + t.Fatalf("command chunks are out of order: %#v", *calls) + } + want4 := "create v2raya_white4 hash:net family inet maxelem 65536\nflush v2raya_white4\nadd v2raya_white4 1.0.0.0/8\nadd v2raya_white4 2.0.0.0/8\nadd v2raya_white4 3.0.0.0/8\n" + want6 := "create v2raya_white6 hash:net family inet6 maxelem 65536\nflush v2raya_white6\nadd v2raya_white6 2001:db8:1::/48\nadd v2raya_white6 2001:db8:2::/48\nadd v2raya_white6 2001:db8:3::/48\n" + if (*calls)[1].name != "ipset" || !reflect.DeepEqual((*calls)[1].args, []string{"restore", "-!"}) || (*calls)[1].input != want4 { + t.Errorf("IPv4 ipset call = %#v", (*calls)[1]) + } + if (*calls)[4].name != "ipset" || !reflect.DeepEqual((*calls)[4].args, []string{"restore", "-!"}) || (*calls)[4].input != want6 { + t.Errorf("IPv6 ipset call = %#v", (*calls)[4]) + } + wantMatch4 := []string{"-w", "2", "-t", "mangle", "-A", "TP_RULE", "-m", "set", "--match-set", "v2raya_white4", "dst", "-j", "RETURN"} + wantMatch6 := []string{"-w", "2", "-t", "mangle", "-A", "TP_RULE", "-m", "set", "--match-set", "v2raya_white6", "dst", "-j", "RETURN"} + if (*calls)[2].name != "iptables" || !reflect.DeepEqual((*calls)[2].args, wantMatch4) { + t.Errorf("IPv4 match call = %#v", (*calls)[2]) + } + if (*calls)[5].name != "ip6tables" || !reflect.DeepEqual((*calls)[5].args, wantMatch6) { + t.Errorf("IPv6 match call = %#v", (*calls)[5]) + } + for _, call := range *calls { + if call.name == "commands" && (strings.Contains(call.input, "1.0.0.0/8") || strings.Contains(call.input, "2001:db8:1::/48")) { + t.Errorf("per-CIDR rule leaked into command chunks: %q", call.input) + } + } +} + +func TestLegacyWhitelistFallsBackToRestore(t *testing.T) { + ipv4 := []string{"1.0.0.0/8", "2.0.0.0/8", "3.0.0.0/8"} + ipv6 := []string{"2001:db8:1::/48", "2001:db8:2::/48", "2001:db8:3::/48"} + for _, tc := range []struct { + name string + available map[string]bool + failSetMatch bool + }{ + {name: "without ipset", available: map[string]bool{"iptables-restore": true, "ip6tables-restore": true}}, + {name: "without xt_set", available: map[string]bool{"ipset": true, "iptables-restore": true, "ip6tables-restore": true}, failSetMatch: true}, + } { + t.Run(tc.name, func(t *testing.T) { + calls := recordLegacyWhitelistCommands(t, tc.available, tc.failSetMatch) + if err := testWhitelistSetter("nat", ipv4, ipv6).run(false, func(command string) string { return command }); err != nil { + t.Fatal(err) + } + var restores []recordedCommand + for _, call := range *calls { + if strings.HasSuffix(call.name, "tables-restore") { + restores = append(restores, call) + } + } + if len(restores) != 2 { + t.Fatalf("restore calls = %#v", restores) + } + wantArgs := []string{"-w", "2", "--noflush"} + if restores[0].name != "iptables-restore" || !reflect.DeepEqual(restores[0].args, wantArgs) || restores[0].input != "*nat\n-A TP_RULE -d 1.0.0.0/8 -j RETURN\n-A TP_RULE -d 2.0.0.0/8 -j RETURN\n-A TP_RULE -d 3.0.0.0/8 -j RETURN\nCOMMIT\n" { + t.Errorf("IPv4 restore call = %#v", restores[0]) + } + if restores[1].name != "ip6tables-restore" || !reflect.DeepEqual(restores[1].args, wantArgs) || restores[1].input != "*nat\n-A TP_RULE -d 2001:db8:1::/48 -j RETURN\n-A TP_RULE -d 2001:db8:2::/48 -j RETURN\n-A TP_RULE -d 2001:db8:3::/48 -j RETURN\nCOMMIT\n" { + t.Errorf("IPv6 restore call = %#v", restores[1]) + } + }) + } +} + +func TestLegacyWhitelistFallsBackWhenIPSetRestoreFails(t *testing.T) { + ipv4 := []string{"1.0.0.0/8", "2.0.0.0/8"} + ipv6 := []string{"2001:db8:1::/48", "2001:db8:2::/48"} + calls := recordLegacyWhitelistCommands(t, map[string]bool{"ipset": true, "iptables-restore": true, "ip6tables-restore": true}, false, "ipset restore -!") + + if err := testWhitelistSetter("nat", ipv4, ipv6).run(false, func(command string) string { return command }); err != nil { + t.Fatal(err) + } + var fallbackCalls []recordedCommand + for _, call := range *calls { + if (call.name == "ipset" && containsArg(call.args, "destroy")) || strings.HasSuffix(call.name, "tables-restore") { + fallbackCalls = append(fallbackCalls, call) + } + if call.name == "commands" && strings.Contains(call.input, "-d ") { + t.Errorf("unexpected per-line commands: %q", call.input) + } + } + want := []recordedCommand{ + {name: "ipset", args: []string{"destroy", "v2raya_white4"}}, + {name: "iptables-restore", args: []string{"-w", "2", "--noflush"}, input: "*nat\n-A TP_RULE -d 1.0.0.0/8 -j RETURN\n-A TP_RULE -d 2.0.0.0/8 -j RETURN\nCOMMIT\n"}, + {name: "ipset", args: []string{"destroy", "v2raya_white6"}}, + {name: "ip6tables-restore", args: []string{"-w", "2", "--noflush"}, input: "*nat\n-A TP_RULE -d 2001:db8:1::/48 -j RETURN\n-A TP_RULE -d 2001:db8:2::/48 -j RETURN\nCOMMIT\n"}, + } + if !reflect.DeepEqual(fallbackCalls, want) { + t.Fatalf("fallback calls = %#v, want %#v", fallbackCalls, want) + } +} + +func TestLegacyWhitelistFallsBackWhenRestoreFails(t *testing.T) { + ipv4 := []string{"1.0.0.0/8", "2.0.0.0/8"} + ipv6 := []string{"2001:db8:1::/48", "2001:db8:2::/48"} + calls := recordLegacyWhitelistCommands(t, map[string]bool{"iptables-restore": true, "ip6tables-restore": true}, false, "iptables-restore -w 2 --noflush", "ip6tables-restore -w 2 --noflush") + + if err := testWhitelistSetter("mangle", ipv4, ipv6).run(false, func(command string) string { return command }); err != nil { + t.Fatal(err) + } + var fallbackCalls []recordedCommand + for _, call := range *calls { + if strings.HasSuffix(call.name, "tables-restore") || (call.name == "commands" && strings.Contains(call.input, "-d ")) { + fallbackCalls = append(fallbackCalls, call) + } + } + want := []recordedCommand{ + {name: "iptables-restore", args: []string{"-w", "2", "--noflush"}, input: "*mangle\n-A TP_RULE -d 1.0.0.0/8 -j RETURN\n-A TP_RULE -d 2.0.0.0/8 -j RETURN\nCOMMIT\n"}, + {name: "commands", input: "iptables -w 2 -t mangle -A TP_RULE -d 1.0.0.0/8 -j RETURN\niptables -w 2 -t mangle -A TP_RULE -d 2.0.0.0/8 -j RETURN\n"}, + {name: "ip6tables-restore", args: []string{"-w", "2", "--noflush"}, input: "*mangle\n-A TP_RULE -d 2001:db8:1::/48 -j RETURN\n-A TP_RULE -d 2001:db8:2::/48 -j RETURN\nCOMMIT\n"}, + {name: "commands", input: "ip6tables -w 2 -t mangle -A TP_RULE -d 2001:db8:1::/48 -j RETURN\nip6tables -w 2 -t mangle -A TP_RULE -d 2001:db8:2::/48 -j RETURN\n"}, + } + if !reflect.DeepEqual(fallbackCalls, want) { + t.Fatalf("fallback calls = %#v, want %#v", fallbackCalls, want) + } +} + +func TestLegacyWhitelistFallsBackToPerLineCommands(t *testing.T) { + calls := recordLegacyWhitelistCommands(t, nil, false) + if err := testWhitelistSetter("mangle", []string{"1.0.0.0/8", "2.0.0.0/8", "3.0.0.0/8"}, []string{"2001:db8:1::/48", "2001:db8:2::/48", "2001:db8:3::/48"}).run(false, func(command string) string { return command }); err != nil { + t.Fatal(err) + } + var cidrCommands []string + for _, call := range *calls { + if call.name == "commands" && strings.Contains(call.input, "-d ") { + cidrCommands = append(cidrCommands, call.input) + } + } + want := []string{ + "iptables -w 2 -t mangle -A TP_RULE -d 1.0.0.0/8 -j RETURN\niptables -w 2 -t mangle -A TP_RULE -d 2.0.0.0/8 -j RETURN\niptables -w 2 -t mangle -A TP_RULE -d 3.0.0.0/8 -j RETURN\n", + "ip6tables -w 2 -t mangle -A TP_RULE -d 2001:db8:1::/48 -j RETURN\nip6tables -w 2 -t mangle -A TP_RULE -d 2001:db8:2::/48 -j RETURN\nip6tables -w 2 -t mangle -A TP_RULE -d 2001:db8:3::/48 -j RETURN\n", + } + if !reflect.DeepEqual(cidrCommands, want) { + t.Fatalf("per-line commands = %#v", cidrCommands) + } +} + +func TestLegacyWhitelistSkipsEmptyFamilies(t *testing.T) { + calls := recordLegacyWhitelistCommands(t, map[string]bool{"ipset": true, "iptables-restore": true, "ip6tables-restore": true}, false) + if err := testWhitelistSetter("nat", nil, nil).run(false, func(command string) string { return command }); err != nil { + t.Fatal(err) + } + for _, call := range *calls { + if call.name != "commands" { + t.Fatalf("empty whitelist executed %#v", call) + } + } +} + +func TestLegacyCleanupDestroysWhitelistSetsAfterRules(t *testing.T) { + for _, commands := range []string{(&legacyTproxy{}).GetCleanCommands().Cmds, (&legacyRedirect{}).GetCleanCommands().Cmds} { + destroy4 := strings.LastIndex(commands, "ipset destroy v2raya_white4") + destroy6 := strings.LastIndex(commands, "ipset destroy v2raya_white6") + deleteRules := strings.LastIndex(commands, "-X TP_RULE") + if destroy4 < deleteRules || destroy6 < deleteRules { + t.Fatalf("cleanup order is wrong:\n%s", commands) + } + } +} + +func TestRewriteIptablesBinariesIncludesRestore(t *testing.T) { + commands := "iptables -L\nip6tables -L\niptables-restore\nip6tables-restore\n" + if got, want := rewriteIptablesBinaries(commands, "legacy"), "iptables-legacy -L\nip6tables-legacy -L\niptables-legacy-restore\nip6tables-legacy-restore\n"; got != want { + t.Errorf("legacy rewrite = %q, want %q", got, want) + } + if got, want := rewriteIptablesBinaries(commands, "nft"), "iptables-nft -L\nip6tables-nft -L\niptables-nft-restore\nip6tables-nft-restore\n"; got != want { + t.Errorf("nft rewrite = %q, want %q", got, want) + } +} + +func TestLegacySetupPlacesSetRulesAtWhitelistPosition(t *testing.T) { + oldValues := legacySetupValues + oldIPv6 := legacyIPv6Supported + ipv4 := []string{"1.0.0.0/8", "2.0.0.0/8", "3.0.0.0/8"} + ipv6 := []string{"2001:db8:1::/48", "2001:db8:2::/48", "2001:db8:3::/48"} + legacySetupValues = func() ([]string, []string, []string, error) { + return nil, ipv4, ipv6, nil + } + legacyIPv6Supported = func() bool { return true } + t.Cleanup(func() { + legacySetupValues = oldValues + legacyIPv6Supported = oldIPv6 + }) + + for _, tc := range []struct { + name string + setter Setter + table string + before4 string + after4 string + before6 string + after6 string + }{ + { + name: "tproxy", + setter: (&legacyTproxy{}).GetSetupCommands(), + table: "mangle", + before4: "iptables -w 2 -t mangle -A TP_RULE -m mark --mark 0x40/0xc0 -j RETURN", + after4: "iptables -w 2 -t mangle -A TP_RULE -j TP_MARK", + before6: "ip6tables -w 2 -t mangle -A TP_RULE -m mark --mark 0x40/0xc0 -j RETURN", + after6: "ip6tables -w 2 -t mangle -A TP_RULE -j TP_MARK", + }, + { + name: "redirect", + setter: (&legacyRedirect{}).GetSetupCommands(), + table: "nat", + before4: "iptables -w 2 -t nat -A TP_RULE -m mark --mark 0x80/0x80 -j RETURN", + after4: "iptables -w 2 -t nat -A TP_RULE -p tcp -j REDIRECT --to-ports 52345", + before6: "ip6tables -w 2 -t nat -A TP_RULE -m mark --mark 0x80/0x80 -j RETURN", + after6: "ip6tables -w 2 -t nat -A TP_RULE -p tcp -j REDIRECT --to-ports 52345", + }, + } { + t.Run(tc.name, func(t *testing.T) { + calls := recordLegacyWhitelistCommands(t, map[string]bool{"ipset": true}, false) + if err := tc.setter.run(false, func(command string) string { return command }); err != nil { + t.Fatal(err) + } + assertSetRulePosition(t, *calls, "iptables", tc.table, "v2raya_white4", tc.before4, tc.after4) + assertSetRulePosition(t, *calls, "ip6tables", tc.table, "v2raya_white6", tc.before6, tc.after6) + }) + } +} + +func assertSetRulePosition(t *testing.T, calls []recordedCommand, binary, table, setName, before, after string) { + t.Helper() + var matches []int + for i, call := range calls { + if call.name == binary && containsArg(call.args, setName) { + matches = append(matches, i) + } + } + if len(matches) != 1 { + t.Fatalf("%s match rules = %d in %#v", setName, len(matches), calls) + } + i := matches[0] + if i < 2 || i+1 >= len(calls) || + calls[i-2].name != "commands" || !strings.Contains(calls[i-2].input, before) || + calls[i+1].name != "commands" || !strings.Contains(calls[i+1].input, after) { + t.Fatalf("%s rule is out of position in %#v", setName, calls) + } + wantArgs := []string{"-w", "2", "-t", table, "-A", "TP_RULE", "-m", "set", "--match-set", setName, "dst", "-j", "RETURN"} + if !reflect.DeepEqual(calls[i].args, wantArgs) { + t.Errorf("%s match args = %#v, want %#v", setName, calls[i].args, wantArgs) + } +} diff --git a/service/kernel/iptables/redirect.go b/service/kernel/iptables/redirect.go index 920251d3..d68cf102 100644 --- a/service/kernel/iptables/redirect.go +++ b/service/kernel/iptables/redirect.go @@ -52,7 +52,7 @@ func (r *legacyRedirect) RemoveIPWhitelist(cidr string) { } func (r *legacyRedirect) GetSetupCommands() Setter { - excludedInterfaces, whiteIpv4List, whiteIpv6List, err := getTproxySetupValues() + excludedInterfaces, whiteIpv4List, whiteIpv6List, err := legacySetupValues() if err != nil { return NewErrorSetter(err) } @@ -91,11 +91,7 @@ iptables -w 2 -t nat -A DNS_REDIRECT -p udp -j REDIRECT --to-port 52353 for _, v := range excludedInterfaces { commands += fmt.Sprintf("iptables -w 2 -t nat -A TP_RULE -o %s -j RETURN\n", strings.ReplaceAll(v, "*", "+")) } - if len(whiteIpv4List) > 0 { - for _, v := range whiteIpv4List { - commands += fmt.Sprintf("iptables -w 2 -t nat -A TP_RULE -d %s -j RETURN\n", v) - } - } + commands += legacyWhitelist4Marker + "\n" commands += ` iptables -w 2 -t nat -A TP_RULE -p tcp -j REDIRECT --to-ports 52345 @@ -111,7 +107,7 @@ iptables -w 2 -t nat -I PREROUTING -p tcp --dport 53 -j DNS_REDIRECT iptables -w 2 -t nat -I OUTPUT -p udp --dport 53 -j DNS_REDIRECT iptables -w 2 -t nat -I OUTPUT -p tcp --dport 53 -j DNS_REDIRECT ` - if IsIPv6Supported() { + if legacyIPv6Supported() { commands += ` ip6tables -w 2 -t nat -N TP_OUT ip6tables -w 2 -t nat -N TP_PRE @@ -141,11 +137,7 @@ ip6tables -w 2 -t nat -A TP_RULE -m mark --mark 0x80/0x80 -j RETURN for _, v := range excludedInterfaces { commands += fmt.Sprintf("ip6tables -w 2 -t nat -A TP_RULE -o %s -j RETURN\n", strings.ReplaceAll(v, "*", "+")) } - if len(whiteIpv6List) > 0 { - for _, v := range whiteIpv6List { - commands += fmt.Sprintf("ip6tables -w 2 -t nat -A TP_RULE -d %s -j RETURN\n", v) - } - } + commands += legacyWhitelist6Marker + "\n" commands += ` ip6tables -w 2 -t nat -A TP_RULE -p tcp -j REDIRECT --to-ports 52345 @@ -160,9 +152,7 @@ ip6tables -w 2 -t nat -I OUTPUT -p udp --dport 53 -j DNS_REDIRECT ip6tables -w 2 -t nat -I OUTPUT -p tcp --dport 53 -j DNS_REDIRECT ` } - return Setter{ - Cmds: withDnsModulePort(commands), - } + return newLegacyWhitelistSetter(withDnsModulePort(commands), "nat", whiteIpv4List, whiteIpv6List) } func (r *legacyRedirect) GetCleanCommands() Setter { @@ -200,6 +190,9 @@ ip6tables -w 2 -t nat -F TP_RULE ip6tables -w 2 -t nat -X TP_RULE ` } + commands += `ipset destroy v2raya_white4 2>/dev/null || true +ipset destroy v2raya_white6 2>/dev/null || true +` return Setter{ Cmds: withDnsModulePort(commands), } diff --git a/service/kernel/iptables/tproxy.go b/service/kernel/iptables/tproxy.go index 6cf0b5d6..c3beeb09 100644 --- a/service/kernel/iptables/tproxy.go +++ b/service/kernel/iptables/tproxy.go @@ -63,7 +63,7 @@ func (t *legacyTproxy) RemoveIPWhitelist(cidr string) { } func (t *legacyTproxy) GetSetupCommands() Setter { - excludedInterfaces, whiteIpv4List, whiteIpv6List, err := getTproxySetupValues() + excludedInterfaces, whiteIpv4List, whiteIpv6List, err := legacySetupValues() if err != nil { return NewErrorSetter(err) } @@ -120,11 +120,7 @@ iptables -w 2 -t mangle -A TP_RULE -p tcp --dport 53 -j TP_MARK iptables -w 2 -t mangle -A TP_RULE -m mark --mark 0x40/0xc0 -j RETURN ` - if len(whiteIpv4List) > 0 { - for _, v := range whiteIpv4List { - commands += fmt.Sprintf("iptables -w 2 -t mangle -A TP_RULE -d %s -j RETURN\n", v) - } - } + commands += legacyWhitelist4Marker + "\n" commands += ` iptables -w 2 -t mangle -A TP_RULE -j TP_MARK @@ -136,7 +132,7 @@ iptables -w 2 -t mangle -A DNS_MARK -m mark --mark 0x80/0x80 -j RETURN iptables -w 2 -t mangle -A DNS_MARK -j MARK --set-xmark 0x40/0x40 iptables -w 2 -t mangle -A DNS_MARK -j ACCEPT ` - if IsIPv6Supported() { + if legacyIPv6Supported() { commands += ` ip -6 rule add fwmark 0x40/0xc0 table 100 ip -6 route add local ::/0 dev lo table 100 @@ -188,11 +184,7 @@ ip6tables -w 2 -t mangle -A TP_RULE -p udp --dport 53 -j TP_MARK ip6tables -w 2 -t mangle -A TP_RULE -p tcp --dport 53 -j TP_MARK ip6tables -w 2 -t mangle -A TP_RULE -m mark --mark 0x40/0xc0 -j RETURN ` - if len(whiteIpv6List) > 0 { - for _, v := range whiteIpv6List { - commands += fmt.Sprintf("ip6tables -w 2 -t mangle -A TP_RULE -d %s -j RETURN\n", v) - } - } + commands += legacyWhitelist6Marker + "\n" commands += ` ip6tables -w 2 -t mangle -A TP_RULE -j TP_MARK @@ -205,9 +197,7 @@ ip6tables -w 2 -t mangle -A DNS_MARK -j MARK --set-xmark 0x40/0x40 ip6tables -w 2 -t mangle -A DNS_MARK -j ACCEPT ` } - return Setter{ - Cmds: withDnsModulePort(commands), - } + return newLegacyWhitelistSetter(withDnsModulePort(commands), "mangle", whiteIpv4List, whiteIpv6List) } func (t *legacyTproxy) GetCleanCommands() Setter { @@ -255,7 +245,10 @@ ip6tables -w 2 -t mangle -D OUTPUT -p tcp --dport 53 -j DNS_MARK ip6tables -w 2 -t mangle -X DNS_MARK ` } - commands += "conntrack -D --mark 0x40 2>/dev/null || true\n" + commands += `conntrack -D --mark 0x40 2>/dev/null || true +ipset destroy v2raya_white4 2>/dev/null || true +ipset destroy v2raya_white6 2>/dev/null || true +` return Setter{ Cmds: withDnsModulePort(commands), }