chore: Chore: adjust the loading order, and then load the resource at last

This commit is contained in:
gVisor bot 2023-03-14 22:37:07 +08:00
parent 7811bb31a8
commit ce14c86e4c
3 changed files with 54 additions and 28 deletions

9
constant/status.go Normal file
View file

@ -0,0 +1,9 @@
package constant
type TunnelStatus uint8
const (
TunnelSuspend TunnelStatus = iota
TunnelInner
TunnelRunning
)

View file

@ -75,24 +75,38 @@ func ParseWithBytes(buf []byte) (*config.Config, error) {
func ApplyConfig(cfg *config.Config, force bool) { func ApplyConfig(cfg *config.Config, force bool) {
mux.Lock() mux.Lock()
defer mux.Unlock() defer mux.Unlock()
preUpdateExperimental(cfg)
tunnel.OnSuspend()
CTLS.ResetCertificate()
for _, c := range cfg.TLS.CustomTrustCert {
if err := CTLS.AddCertificate(c); err != nil {
log.Warnln("%s\nadd error: %s", c, err.Error())
}
}
updateUsers(cfg.Users) updateUsers(cfg.Users)
updateProxies(cfg.Proxies, cfg.Providers) updateProxies(cfg.Proxies, cfg.Providers)
updateRules(cfg.Rules, cfg.SubRules, cfg.RuleProviders) updateRules(cfg.Rules, cfg.SubRules, cfg.RuleProviders)
updateSniffer(cfg.Sniffer) updateSniffer(cfg.Sniffer)
updateHosts(cfg.Hosts) updateHosts(cfg.Hosts)
updateGeneral(cfg.General) updateGeneral(cfg.General)
initInnerTcp()
updateDNS(cfg.DNS, cfg.General.IPv6) updateDNS(cfg.DNS, cfg.General.IPv6)
loadProxyProvider(cfg.Providers)
updateProfile(cfg)
loadRuleProvider(cfg.RuleProviders)
updateListeners(cfg.General, cfg.Listeners, force) updateListeners(cfg.General, cfg.Listeners, force)
updateIPTables(cfg) updateIPTables(cfg)
updateTun(cfg.General) updateTun(cfg.General)
updateExperimental(cfg) updateExperimental(cfg)
updateTunnels(cfg.Tunnels) updateTunnels(cfg.Tunnels)
tunnel.OnInnerLoading()
initInnerTcp()
loadProxyProvider(cfg.Providers)
updateProfile(cfg)
loadRuleProvider(cfg.RuleProviders)
tunnel.OnRunning()
log.SetLevel(cfg.General.LogLevel) log.SetLevel(cfg.General.LogLevel)
} }
@ -144,10 +158,6 @@ func updateListeners(general *config.General, listeners map[string]C.InboundList
return return
} }
if general.Interface == "" && (!general.Tun.Enable || !general.Tun.AutoDetectInterface) {
dialer.DefaultInterface.Store(general.Interface)
}
allowLan := general.AllowLan allowLan := general.AllowLan
listener.SetAllowLan(allowLan) listener.SetAllowLan(allowLan)
@ -168,15 +178,6 @@ func updateExperimental(c *config.Config) {
runtime.GC() runtime.GC()
} }
func preUpdateExperimental(c *config.Config) {
CTLS.ResetCertificate()
for _, c := range c.TLS.CustomTrustCert {
if err := CTLS.AddCertificate(c); err != nil {
log.Warnln("%s\nadd error: %s", c, err.Error())
}
}
}
func updateDNS(c *config.DNS, generalIPv6 bool) { func updateDNS(c *config.DNS, generalIPv6 bool) {
if !c.Enable { if !c.Enable {
resolver.DefaultResolver = nil resolver.DefaultResolver = nil
@ -342,17 +343,8 @@ func updateGeneral(general *config.General) {
inbound.SetTfo(general.InboundTfo) inbound.SetTfo(general.InboundTfo)
adapter.UnifiedDelay.Store(general.UnifiedDelay) adapter.UnifiedDelay.Store(general.UnifiedDelay)
// Avoid reload configuration clean the value, causing traffic loops
if listener.GetTunConf().Enable && listener.GetTunConf().AutoDetectInterface {
// changed only when the name is specified
// if name is empty, setting delay until after tun loaded
if general.Interface != "" && (!general.Tun.Enable || !general.Tun.AutoDetectInterface) {
dialer.DefaultInterface.Store(general.Interface)
}
} else {
dialer.DefaultInterface.Store(general.Interface)
}
dialer.DefaultInterface.Store(general.Interface)
dialer.DefaultRoutingMark.Store(int32(general.RoutingMark)) dialer.DefaultRoutingMark.Store(int32(general.RoutingMark))
if general.RoutingMark > 0 { if general.RoutingMark > 0 {
log.Infoln("Use routing mark: %#x", general.RoutingMark) log.Infoln("Use routing mark: %#x", general.RoutingMark)

View file

@ -26,6 +26,7 @@ import (
) )
var ( var (
status C.TunnelStatus
tcpQueue = make(chan C.ConnContext, 200) tcpQueue = make(chan C.ConnContext, 200)
udpQueue = make(chan C.PacketAdapter, 200) udpQueue = make(chan C.PacketAdapter, 200)
natTable = nat.New() natTable = nat.New()
@ -49,6 +50,18 @@ var (
fakeIPRange netip.Prefix fakeIPRange netip.Prefix
) )
func OnSuspend() {
status = C.TunnelSuspend
}
func OnInnerLoading() {
status = C.TunnelInner
}
func OnRunning() {
status = C.TunnelRunning
}
func SetFakeIPRange(p netip.Prefix) { func SetFakeIPRange(p netip.Prefix) {
fakeIPRange = p fakeIPRange = p
} }
@ -158,10 +171,18 @@ func SetFindProcessMode(mode P.FindProcessMode) {
findProcessMode = mode findProcessMode = mode
} }
func isHandle(t C.Type) bool {
return status == C.TunnelRunning || (status == C.TunnelInner && t == C.INNER)
}
// processUDP starts a loop to handle udp packet // processUDP starts a loop to handle udp packet
func processUDP() { func processUDP() {
queue := udpQueue queue := udpQueue
for conn := range queue { for conn := range queue {
if !isHandle(conn.Metadata().Type) {
conn.Drop()
continue
}
handleUDPConn(conn) handleUDPConn(conn)
} }
} }
@ -177,6 +198,10 @@ func process() {
queue := tcpQueue queue := tcpQueue
for conn := range queue { for conn := range queue {
if !isHandle(conn.Metadata().Type) {
_ = conn.Conn().Close()
continue
}
go handleTCPConn(conn) go handleTCPConn(conn)
} }
} }