chore: decrease goroutine used in core tunnel
This commit is contained in:
@ -23,7 +23,7 @@ import (
|
||||
"github.com/Dreamacro/clash/listener/socks"
|
||||
"github.com/Dreamacro/clash/listener/tproxy"
|
||||
"github.com/Dreamacro/clash/listener/tuic"
|
||||
"github.com/Dreamacro/clash/listener/tunnel"
|
||||
LT "github.com/Dreamacro/clash/listener/tunnel"
|
||||
"github.com/Dreamacro/clash/log"
|
||||
|
||||
"github.com/samber/lo"
|
||||
@ -42,8 +42,8 @@ var (
|
||||
tproxyUDPListener *tproxy.UDPListener
|
||||
mixedListener *mixed.Listener
|
||||
mixedUDPLister *socks.UDPListener
|
||||
tunnelTCPListeners = map[string]*tunnel.Listener{}
|
||||
tunnelUDPListeners = map[string]*tunnel.PacketConn{}
|
||||
tunnelTCPListeners = map[string]*LT.Listener{}
|
||||
tunnelUDPListeners = map[string]*LT.PacketConn{}
|
||||
inboundListeners = map[string]C.InboundListener{}
|
||||
tunLister *sing_tun.Listener
|
||||
shadowSocksListener C.MultiAddrListener
|
||||
@ -112,7 +112,7 @@ func SetBindAddress(host string) {
|
||||
bindAddress = host
|
||||
}
|
||||
|
||||
func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
|
||||
func ReCreateHTTP(port int, tunnel C.Tunnel) {
|
||||
httpMux.Lock()
|
||||
defer httpMux.Unlock()
|
||||
|
||||
@ -137,7 +137,7 @@ func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
|
||||
return
|
||||
}
|
||||
|
||||
httpListener, err = http.New(addr, tcpIn)
|
||||
httpListener, err = http.New(addr, tunnel)
|
||||
if err != nil {
|
||||
log.Errorln("Start HTTP server error: %s", err.Error())
|
||||
return
|
||||
@ -146,7 +146,7 @@ func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
|
||||
log.Infoln("HTTP proxy listening at: %s", httpListener.Address())
|
||||
}
|
||||
|
||||
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
||||
func ReCreateSocks(port int, tunnel C.Tunnel) {
|
||||
socksMux.Lock()
|
||||
defer socksMux.Unlock()
|
||||
|
||||
@ -188,12 +188,12 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd
|
||||
return
|
||||
}
|
||||
|
||||
tcpListener, err := socks.New(addr, tcpIn)
|
||||
tcpListener, err := socks.New(addr, tunnel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
udpListener, err := socks.NewUDP(addr, udpIn)
|
||||
udpListener, err := socks.NewUDP(addr, tunnel)
|
||||
if err != nil {
|
||||
tcpListener.Close()
|
||||
return
|
||||
@ -205,7 +205,7 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd
|
||||
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
|
||||
}
|
||||
|
||||
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) {
|
||||
func ReCreateRedir(port int, tunnel C.Tunnel) {
|
||||
redirMux.Lock()
|
||||
defer redirMux.Unlock()
|
||||
|
||||
@ -238,12 +238,12 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd
|
||||
return
|
||||
}
|
||||
|
||||
redirListener, err = redir.New(addr, tcpIn)
|
||||
redirListener, err = redir.New(addr, tunnel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
redirUDPListener, err = tproxy.NewUDP(addr, udpIn, natTable)
|
||||
redirUDPListener, err = tproxy.NewUDP(addr, tunnel)
|
||||
if err != nil {
|
||||
log.Warnln("Failed to start Redir UDP Listener: %s", err)
|
||||
}
|
||||
@ -251,7 +251,7 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd
|
||||
log.Infoln("Redirect proxy listening at: %s", redirListener.Address())
|
||||
}
|
||||
|
||||
func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
||||
func ReCreateShadowSocks(shadowSocksConfig string, tunnel C.Tunnel) {
|
||||
ssMux.Lock()
|
||||
defer ssMux.Unlock()
|
||||
|
||||
@ -292,7 +292,7 @@ func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, u
|
||||
return
|
||||
}
|
||||
|
||||
listener, err := sing_shadowsocks.New(ssConfig, tcpIn, udpIn)
|
||||
listener, err := sing_shadowsocks.New(ssConfig, tunnel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -305,7 +305,7 @@ func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, u
|
||||
return
|
||||
}
|
||||
|
||||
func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
||||
func ReCreateVmess(vmessConfig string, tunnel C.Tunnel) {
|
||||
vmessMux.Lock()
|
||||
defer vmessMux.Unlock()
|
||||
|
||||
@ -344,7 +344,7 @@ func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<-
|
||||
return
|
||||
}
|
||||
|
||||
listener, err := sing_vmess.New(vsConfig, tcpIn, udpIn)
|
||||
listener, err := sing_vmess.New(vsConfig, tunnel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -357,7 +357,7 @@ func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<-
|
||||
return
|
||||
}
|
||||
|
||||
func ReCreateTuic(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
||||
func ReCreateTuic(config LC.TuicServer, tunnel C.Tunnel) {
|
||||
tuicMux.Lock()
|
||||
defer func() {
|
||||
LastTuicConf = config
|
||||
@ -389,7 +389,7 @@ func ReCreateTuic(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<-
|
||||
return
|
||||
}
|
||||
|
||||
listener, err := tuic.New(config, tcpIn, udpIn)
|
||||
listener, err := tuic.New(config, tunnel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -402,7 +402,7 @@ func ReCreateTuic(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<-
|
||||
return
|
||||
}
|
||||
|
||||
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) {
|
||||
func ReCreateTProxy(port int, tunnel C.Tunnel) {
|
||||
tproxyMux.Lock()
|
||||
defer tproxyMux.Unlock()
|
||||
|
||||
@ -435,12 +435,12 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketA
|
||||
return
|
||||
}
|
||||
|
||||
tproxyListener, err = tproxy.New(addr, tcpIn)
|
||||
tproxyListener, err = tproxy.New(addr, tunnel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn, natTable)
|
||||
tproxyUDPListener, err = tproxy.NewUDP(addr, tunnel)
|
||||
if err != nil {
|
||||
log.Warnln("Failed to start TProxy UDP Listener: %s", err)
|
||||
}
|
||||
@ -448,7 +448,7 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketA
|
||||
log.Infoln("TProxy server listening at: %s", tproxyListener.Address())
|
||||
}
|
||||
|
||||
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
||||
func ReCreateMixed(port int, tunnel C.Tunnel) {
|
||||
mixedMux.Lock()
|
||||
defer mixedMux.Unlock()
|
||||
|
||||
@ -489,12 +489,12 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd
|
||||
return
|
||||
}
|
||||
|
||||
mixedListener, err = mixed.New(addr, tcpIn)
|
||||
mixedListener, err = mixed.New(addr, tunnel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
mixedUDPLister, err = socks.NewUDP(addr, udpIn)
|
||||
mixedUDPLister, err = socks.NewUDP(addr, tunnel)
|
||||
if err != nil {
|
||||
mixedListener.Close()
|
||||
return
|
||||
@ -503,7 +503,7 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAd
|
||||
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
|
||||
}
|
||||
|
||||
func ReCreateTun(tunConf LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
||||
func ReCreateTun(tunConf LC.Tun, tunnel C.Tunnel) {
|
||||
tunMux.Lock()
|
||||
defer func() {
|
||||
LastTunConf = tunConf
|
||||
@ -531,7 +531,7 @@ func ReCreateTun(tunConf LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.Pack
|
||||
return
|
||||
}
|
||||
|
||||
lister, err := sing_tun.New(tunConf, tcpIn, udpIn)
|
||||
lister, err := sing_tun.New(tunConf, tunnel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -573,7 +573,7 @@ func ReCreateRedirToTun(ifaceNames []string) {
|
||||
log.Infoln("Attached tc ebpf program to interfaces %v", tcProgram.RawNICs())
|
||||
}
|
||||
|
||||
func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- C.PacketAdapter) {
|
||||
func ReCreateAutoRedir(ifaceNames []string, tunnel C.Tunnel) {
|
||||
autoRedirMux.Lock()
|
||||
defer autoRedirMux.Unlock()
|
||||
|
||||
@ -614,7 +614,7 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<-
|
||||
|
||||
addr := genAddr("*", C.TcpAutoRedirPort, true)
|
||||
|
||||
autoRedirListener, err = autoredir.New(addr, tcpIn)
|
||||
autoRedirListener, err = autoredir.New(addr, tunnel)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@ -629,7 +629,7 @@ func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<-
|
||||
log.Infoln("Auto redirect proxy listening at: %s, attached tc ebpf program to interfaces %v", autoRedirListener.Address(), autoRedirProgram.RawNICs())
|
||||
}
|
||||
|
||||
func PatchTunnel(tunnels []LC.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
||||
func PatchTunnel(tunnels []LC.Tunnel, tunnel C.Tunnel) {
|
||||
tunnelMux.Lock()
|
||||
defer tunnelMux.Unlock()
|
||||
|
||||
@ -699,7 +699,7 @@ func PatchTunnel(tunnels []LC.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- C
|
||||
for _, elm := range needCreate {
|
||||
key := fmt.Sprintf("%s/%s/%s", elm.addr, elm.target, elm.proxy)
|
||||
if elm.network == "tcp" {
|
||||
l, err := tunnel.New(elm.addr, elm.target, elm.proxy, tcpIn)
|
||||
l, err := LT.New(elm.addr, elm.target, elm.proxy, tunnel)
|
||||
if err != nil {
|
||||
log.Errorln("Start tunnel %s error: %s", elm.target, err.Error())
|
||||
continue
|
||||
@ -707,7 +707,7 @@ func PatchTunnel(tunnels []LC.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- C
|
||||
tunnelTCPListeners[key] = l
|
||||
log.Infoln("Tunnel(tcp/%s) proxy %s listening at: %s", elm.target, elm.proxy, tunnelTCPListeners[key].Address())
|
||||
} else {
|
||||
l, err := tunnel.NewUDP(elm.addr, elm.target, elm.proxy, udpIn)
|
||||
l, err := LT.NewUDP(elm.addr, elm.target, elm.proxy, tunnel)
|
||||
if err != nil {
|
||||
log.Errorln("Start tunnel %s error: %s", elm.target, err.Error())
|
||||
continue
|
||||
@ -718,7 +718,7 @@ func PatchTunnel(tunnels []LC.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- C
|
||||
}
|
||||
}
|
||||
|
||||
func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable, dropOld bool) {
|
||||
func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tunnel C.Tunnel, dropOld bool) {
|
||||
inboundMux.Lock()
|
||||
defer inboundMux.Unlock()
|
||||
|
||||
@ -730,7 +730,7 @@ func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tcpIn ch
|
||||
continue
|
||||
}
|
||||
}
|
||||
if err := newListener.Listen(tcpIn, udpIn, natTable); err != nil {
|
||||
if err := newListener.Listen(tunnel); err != nil {
|
||||
log.Errorln("Listener %s listen err: %s", name, err.Error())
|
||||
continue
|
||||
}
|
||||
|
Reference in New Issue
Block a user