Fix nftablesCreateLocalAddressSets

This commit is contained in:
世界 2026-02-23 17:12:39 +08:00
parent 2a33d64abc
commit 5715a3919a
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
2 changed files with 16 additions and 8 deletions

View file

@ -4,6 +4,7 @@ package tun
import (
"net/netip"
"strings"
"github.com/sagernet/nftables"
"github.com/sagernet/nftables/binaryutil"
@ -298,6 +299,10 @@ func (r *autoRedirect) setupNFTables() error {
// TODO: test if this works
func (r *autoRedirect) nftablesUpdateLocalAddressSet() error {
err := r.interfaceFinder.Update()
if err != nil {
return err
}
newLocalAddresses := common.FlatMap(r.interfaceFinder.Interfaces(), func(it control.Interface) []netip.Prefix {
return common.Filter(it.Addresses, func(prefix netip.Prefix) bool {
return it.Name == "lo" || prefix.Addr().IsGlobalUnicast()
@ -306,6 +311,11 @@ func (r *autoRedirect) nftablesUpdateLocalAddressSet() error {
if slices.Equal(newLocalAddresses, r.localAddresses) {
return nil
}
if r.logger != nil {
r.logger.Debug("updating local address set to [", strings.Join(common.Map(newLocalAddresses, func(it netip.Prefix) string {
return it.String()
}), ", ")+"]")
}
nft, err := nftables.New()
if err != nil {
return err