Merge from remote branch

This commit is contained in:
yaling888
2022-01-05 01:46:37 +08:00
15 changed files with 224 additions and 250 deletions

View File

@ -3,11 +3,13 @@ package proxy
import (
"fmt"
"net"
"os"
"runtime"
"strconv"
"sync"
"github.com/Dreamacro/clash/adapter/inbound"
S "github.com/Dreamacro/clash/component/script"
"github.com/Dreamacro/clash/config"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/http"
@ -80,38 +82,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 +150,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 +197,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 +218,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 +243,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 +264,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,46 +300,53 @@ 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 {
func ReCreateTun(conf config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
tunMux.Lock()
defer tunMux.Unlock()
var err error
defer func() {
if err != nil {
log.Errorln("Start TUN interface error: %s", err.Error())
S.Py_Finalize()
os.Exit(2)
}
}()
if tunAdapter != nil {
tunAdapter.Close()
tunAdapter = nil
}
if !conf.Enable {
return nil
return
}
var err error
tunAdapter, err = tun.New(conf, tcpIn, udpIn)
return err
if err != nil {
log.Warnln("Failed to start TUN interface: %s", err.Error())
}
}
// GetPorts return the ports of proxy servers