2026-01-07 23:05:08 +01:00
|
|
|
package tun
|
2026-04-13 21:38:10 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"net"
|
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
|
|
"github.com/xtls/xray-core/common/errors"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type InterfaceUpdater struct {
|
|
|
|
|
sync.Mutex
|
|
|
|
|
|
|
|
|
|
tunIndex int
|
|
|
|
|
fixedName string
|
|
|
|
|
iface *net.Interface
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var updater *InterfaceUpdater
|
|
|
|
|
|
|
|
|
|
func (updater *InterfaceUpdater) Get() *net.Interface {
|
|
|
|
|
updater.Lock()
|
|
|
|
|
defer updater.Unlock()
|
|
|
|
|
|
|
|
|
|
return updater.iface
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (updater *InterfaceUpdater) Update() {
|
|
|
|
|
updater.Lock()
|
|
|
|
|
defer updater.Unlock()
|
|
|
|
|
|
2026-06-24 19:06:00 +08:00
|
|
|
got, err := findOutboundInterface(updater.tunIndex, updater.fixedName)
|
2026-04-13 21:38:10 +08:00
|
|
|
if err != nil {
|
|
|
|
|
errors.LogInfoInner(context.Background(), err, "[tun] failed to update interface")
|
2026-06-24 19:06:00 +08:00
|
|
|
updater.iface = nil
|
2026-04-13 21:38:10 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if got == nil {
|
|
|
|
|
errors.LogInfo(context.Background(), "[tun] failed to update interface > got == nil")
|
2026-06-24 19:06:00 +08:00
|
|
|
updater.iface = nil
|
2026-04-13 21:38:10 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 19:06:00 +08:00
|
|
|
if updater.iface != nil && updater.iface.Index == got.Index && updater.iface.Name == got.Name {
|
|
|
|
|
return
|
2026-05-02 20:18:25 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-24 19:06:00 +08:00
|
|
|
updater.iface = got
|
|
|
|
|
errors.LogInfo(context.Background(), "[tun] update interface ", got.Name, " ", got.Index)
|
2026-05-02 20:18:25 +08:00
|
|
|
}
|