Merge remote-tracking branch 'clash/dev' into Meta

# Conflicts:
#	.github/workflows/docker.yml
#	dns/server.go
#	go.mod
#	go.sum
#	hub/executor/executor.go
#	test/go.mod
#	test/go.sum
This commit is contained in:
Maze.tsz
2022-01-04 17:31:07 +08:00
24 changed files with 421 additions and 390 deletions

View File

@ -80,38 +80,50 @@ func SetBindAddress(host string) {
bindAddress = host
}
func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) error {
func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
httpMux.Lock()
defer httpMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start HTTP server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
if httpListener != nil {
if httpListener.RawAddress() == addr {
return nil
return
}
httpListener.Close()
httpListener = nil
}
if portIsZero(addr) {
return nil
return
}
var err error
httpListener, err = http.New(addr, tcpIn)
if err != nil {
return err
return
}
log.Infoln("HTTP proxy listening at: %s", httpListener.Address())
return nil
}
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
socksMux.Lock()
defer socksMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start SOCKS server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
shouldTCPIgnore := false
@ -136,40 +148,46 @@ func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
}
if shouldTCPIgnore && shouldUDPIgnore {
return nil
return
}
if portIsZero(addr) {
return nil
return
}
tcpListener, err := socks.New(addr, tcpIn)
if err != nil {
return err
return
}
udpListener, err := socks.NewUDP(addr, udpIn)
if err != nil {
tcpListener.Close()
return err
return
}
socksListener = tcpListener
socksUDPListener = udpListener
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
return nil
}
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
redirMux.Lock()
defer redirMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start Redir server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
if redirListener != nil {
if redirListener.RawAddress() == addr {
return nil
return
}
redirListener.Close()
redirListener = nil
@ -177,20 +195,19 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
if redirUDPListener != nil {
if redirUDPListener.RawAddress() == addr {
return nil
return
}
redirUDPListener.Close()
redirUDPListener = nil
}
if portIsZero(addr) {
return nil
return
}
var err error
redirListener, err = redir.New(addr, tcpIn)
if err != nil {
return err
return
}
redirUDPListener, err = tproxy.NewUDP(addr, udpIn)
@ -199,18 +216,24 @@ func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
}
log.Infoln("Redirect proxy listening at: %s", redirListener.Address())
return nil
}
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
tproxyMux.Lock()
defer tproxyMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start TProxy server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
if tproxyListener != nil {
if tproxyListener.RawAddress() == addr {
return nil
return
}
tproxyListener.Close()
tproxyListener = nil
@ -218,20 +241,19 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.
if tproxyUDPListener != nil {
if tproxyUDPListener.RawAddress() == addr {
return nil
return
}
tproxyUDPListener.Close()
tproxyUDPListener = nil
}
if portIsZero(addr) {
return nil
return
}
var err error
tproxyListener, err = tproxy.New(addr, tcpIn)
if err != nil {
return err
return
}
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn)
@ -240,13 +262,19 @@ func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.
}
log.Infoln("TProxy server listening at: %s", tproxyListener.Address())
return nil
}
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
mixedMux.Lock()
defer mixedMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start Mixed(http+socks) server error: %s", err.Error())
}
}()
addr := genAddr(bindAddress, port, allowLan)
shouldTCPIgnore := false
@ -270,27 +298,25 @@ func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.P
}
if shouldTCPIgnore && shouldUDPIgnore {
return nil
return
}
if portIsZero(addr) {
return nil
return
}
var err error
mixedListener, err = mixed.New(addr, tcpIn)
if err != nil {
return err
return
}
mixedUDPLister, err = socks.NewUDP(addr, udpIn)
if err != nil {
mixedListener.Close()
return err
return
}
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
return nil
}
func ReCreateTun(conf config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {