github.com/yaling888/clash@v1.53.0/component/ebpf/ebpf.go (about)

     1  package ebpf
     2  
     3  import (
     4  	"net/netip"
     5  
     6  	"github.com/yaling888/clash/common/cmd"
     7  	"github.com/yaling888/clash/component/resolver"
     8  	C "github.com/yaling888/clash/constant"
     9  	"github.com/yaling888/clash/transport/socks5"
    10  )
    11  
    12  type option struct {
    13  	ipv6           bool
    14  	sysAllIPv6     string
    15  	sysDefaultIPv6 string
    16  }
    17  
    18  type TcEBpfProgram struct {
    19  	opt          *option
    20  	pros         []C.EBpf
    21  	rawNICs      []string
    22  	rawInterface string
    23  }
    24  
    25  func (t *TcEBpfProgram) RawNICs() []string {
    26  	return t.rawNICs
    27  }
    28  
    29  func (t *TcEBpfProgram) RawInterface() string {
    30  	return t.rawInterface
    31  }
    32  
    33  func (t *TcEBpfProgram) Close() {
    34  	for _, p := range t.pros {
    35  		p.Close()
    36  	}
    37  
    38  	resolver.DisableIPv6 = t.opt.ipv6
    39  
    40  	//if t.opt.sysAllIPv6 != "" {
    41  	//	_, _ = cmd.ExecCmd("sysctl -w net.ipv6.conf.all.disable_ipv6=" + t.opt.sysAllIPv6)
    42  	//}
    43  	//if t.opt.sysDefaultIPv6 != "" {
    44  	//	_, _ = cmd.ExecCmd("sysctl -w net.ipv6.conf.default.disable_ipv6=" + t.opt.sysDefaultIPv6)
    45  	//}
    46  }
    47  
    48  func (t *TcEBpfProgram) Lookup(srcAddrPort netip.AddrPort) (addr socks5.Addr, err error) {
    49  	for _, p := range t.pros {
    50  		addr, err = p.Lookup(srcAddrPort)
    51  		if err == nil {
    52  			return
    53  		}
    54  	}
    55  	return
    56  }
    57  
    58  func NewAutoRedirProgram(pros []C.EBpf, rawNICs []string, rawInterface string) *TcEBpfProgram {
    59  	var (
    60  		sysAllIPv6, _     = cmd.ExecCmd("cat /proc/sys/net/ipv6/conf/all/disable_ipv6")
    61  		sysDefaultIPv6, _ = cmd.ExecCmd("cat /proc/sys/net/ipv6/conf/default/disable_ipv6")
    62  	)
    63  
    64  	opt := &option{
    65  		ipv6:           resolver.DisableIPv6,
    66  		sysAllIPv6:     sysAllIPv6,
    67  		sysDefaultIPv6: sysDefaultIPv6,
    68  	}
    69  
    70  	resolver.DisableIPv6 = true
    71  
    72  	//_, _ = cmd.ExecCmd("sysctl -w net.ipv6.conf.all.disable_ipv6=1")
    73  	//_, _ = cmd.ExecCmd("sysctl -w net.ipv6.conf.default.disable_ipv6=1")
    74  
    75  	return &TcEBpfProgram{
    76  		opt:          opt,
    77  		pros:         pros,
    78  		rawNICs:      rawNICs,
    79  		rawInterface: rawInterface,
    80  	}
    81  }