chore: better tun config passing

This commit is contained in:
wwqgtxx 2022-11-03 18:56:03 +08:00
parent 1b0d09068b
commit 9a5c0a4b6d
3 changed files with 22 additions and 20 deletions

View file

@ -259,7 +259,10 @@ func loadProxyProvider(proxyProviders map[string]provider.ProxyProvider) {
} }
func updateTun(tun *config.Tun) { func updateTun(tun *config.Tun) {
P.ReCreateTun(tun, tunnel.TCPIn(), tunnel.UDPIn()) if tun == nil {
return
}
P.ReCreateTun(*tun, tunnel.TCPIn(), tunnel.UDPIn())
P.ReCreateRedirToTun(tun.RedirectToTun) P.ReCreateRedirToTun(tun.RedirectToTun)
} }

View file

@ -26,7 +26,6 @@ import (
var ( var (
allowLan = false allowLan = false
bindAddress = "*" bindAddress = "*"
lastTunConf *config.Tun
inboundTfo = false inboundTfo = false
socksListener *socks.Listener socksListener *socks.Listener
@ -52,6 +51,8 @@ var (
tunMux sync.Mutex tunMux sync.Mutex
autoRedirMux sync.Mutex autoRedirMux sync.Mutex
tcMux sync.Mutex tcMux sync.Mutex
lastTunConf config.Tun
) )
type Ports struct { type Ports struct {
@ -63,12 +64,12 @@ type Ports struct {
} }
func GetTunConf() config.Tun { func GetTunConf() config.Tun {
if lastTunConf == nil { if tunLister == nil {
return config.Tun{ return config.Tun{
Enable: false, Enable: false,
} }
} }
return *lastTunConf return tunLister.Config()
} }
func AllowLan() bool { func AllowLan() bool {
@ -335,7 +336,7 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address()) log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
} }
func ReCreateTun(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) { func ReCreateTun(tunConf config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
tunMux.Lock() tunMux.Lock()
defer func() { defer func() {
lastTunConf = tunConf lastTunConf = tunConf
@ -383,11 +384,13 @@ func ReCreateRedirToTun(ifaceNames []string) {
return return
} }
if lastTunConf == nil || !lastTunConf.Enable { tunConf := GetTunConf()
if !tunConf.Enable {
return return
} }
program, err := ebpf.NewTcEBpfProgram(nicArr, lastTunConf.Device) program, err := ebpf.NewTcEBpfProgram(nicArr, tunConf.Device)
if err != nil { if err != nil {
log.Errorln("Attached tc ebpf program error: %v", err) log.Errorln("Attached tc ebpf program error: %v", err)
return return
@ -509,8 +512,12 @@ func genAddr(host string, port int, allowLan bool) string {
return fmt.Sprintf("127.0.0.1:%d", port) return fmt.Sprintf("127.0.0.1:%d", port)
} }
func hasTunConfigChange(tunConf *config.Tun) bool { func hasTunConfigChange(tunConf config.Tun) bool {
if lastTunConf == nil { if lastTunConf.Enable != tunConf.Enable ||
lastTunConf.Device != tunConf.Device ||
lastTunConf.Stack != tunConf.Stack ||
lastTunConf.AutoRoute != tunConf.AutoRoute ||
lastTunConf.AutoDetectInterface != tunConf.AutoDetectInterface {
return true return true
} }
@ -532,14 +539,6 @@ func hasTunConfigChange(tunConf *config.Tun) bool {
} }
} }
if lastTunConf.Enable != tunConf.Enable ||
lastTunConf.Device != tunConf.Device ||
lastTunConf.Stack != tunConf.Stack ||
lastTunConf.AutoRoute != tunConf.AutoRoute ||
lastTunConf.AutoDetectInterface != tunConf.AutoDetectInterface {
return true
}
if slices.Equal(tunConf.Inet4Address, lastTunConf.Inet4Address) && slices.Equal(tunConf.Inet6Address, lastTunConf.Inet6Address) { if slices.Equal(tunConf.Inet4Address, lastTunConf.Inet4Address) && slices.Equal(tunConf.Inet6Address, lastTunConf.Inet6Address) {
return true return true
} }
@ -552,5 +551,5 @@ func Cleanup(wait bool) {
tunLister.Close() tunLister.Close()
tunLister = nil tunLister = nil
} }
lastTunConf = nil lastTunConf = config.Tun{}
} }

View file

@ -65,7 +65,7 @@ func CalculateInterfaceName(name string) (tunName string) {
return return
} }
func New(options *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (l *Listener, err error) { func New(options config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) (l *Listener, err error) {
tunName := options.Device tunName := options.Device
if tunName == "" { if tunName == "" {
tunName = CalculateInterfaceName(InterfaceName) tunName = CalculateInterfaceName(InterfaceName)
@ -122,7 +122,7 @@ func New(options *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.
} }
l = &Listener{ l = &Listener{
closed: false, closed: false,
options: *options, options: options,
handler: handler, handler: handler,
} }
defer func() { defer func() {