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

This commit is contained in:
Skyxim 2023-03-14 22:37:07 +08:00
parent 0f24c2f849
commit 09c53e7cb7
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) {
mux.Lock()
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)
updateProxies(cfg.Proxies, cfg.Providers)
updateRules(cfg.Rules, cfg.SubRules, cfg.RuleProviders)
updateSniffer(cfg.Sniffer)
updateHosts(cfg.Hosts)
updateGeneral(cfg.General)
initInnerTcp()
updateDNS(cfg.DNS, cfg.General.IPv6)
loadProxyProvider(cfg.Providers)
updateProfile(cfg)
loadRuleProvider(cfg.RuleProviders)
updateListeners(cfg.General, cfg.Listeners, force)
updateIPTables(cfg)
updateTun(cfg.General)
updateExperimental(cfg)
updateTunnels(cfg.Tunnels)
tunnel.OnInnerLoading()
initInnerTcp()
loadProxyProvider(cfg.Providers)
updateProfile(cfg)
loadRuleProvider(cfg.RuleProviders)
tunnel.OnRunning()
log.SetLevel(cfg.General.LogLevel)
}
@ -144,10 +158,6 @@ func updateListeners(general *config.General, listeners map[string]C.InboundList
return
}
if general.Interface == "" && (!general.Tun.Enable || !general.Tun.AutoDetectInterface) {
dialer.DefaultInterface.Store(general.Interface)
}
allowLan := general.AllowLan
listener.SetAllowLan(allowLan)
@ -168,15 +178,6 @@ func updateExperimental(c *config.Config) {
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) {
if !c.Enable {
resolver.DefaultResolver = nil
@ -342,17 +343,8 @@ func updateGeneral(general *config.General) {
inbound.SetTfo(general.InboundTfo)
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))
if general.RoutingMark > 0 {
log.Infoln("Use routing mark: %#x", general.RoutingMark)

View file

@ -26,6 +26,7 @@ import (
)
var (
status C.TunnelStatus
tcpQueue = make(chan C.ConnContext, 200)
udpQueue = make(chan C.PacketAdapter, 200)
natTable = nat.New()
@ -49,6 +50,18 @@ var (
fakeIPRange netip.Prefix
)
func OnSuspend() {
status = C.TunnelSuspend
}
func OnInnerLoading() {
status = C.TunnelInner
}
func OnRunning() {
status = C.TunnelRunning
}
func SetFakeIPRange(p netip.Prefix) {
fakeIPRange = p
}
@ -158,10 +171,18 @@ func SetFindProcessMode(mode P.FindProcessMode) {
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
func processUDP() {
queue := udpQueue
for conn := range queue {
if !isHandle(conn.Metadata().Type) {
conn.Drop()
continue
}
handleUDPConn(conn)
}
}
@ -177,6 +198,10 @@ func process() {
queue := tcpQueue
for conn := range queue {
if !isHandle(conn.Metadata().Type) {
_ = conn.Conn().Close()
continue
}
go handleTCPConn(conn)
}
}