clash/listener/listener.go

605 lines
13 KiB
Go
Raw Normal View History

2018-07-15 14:23:20 +00:00
package proxy
import (
2018-11-21 05:47:46 +00:00
"fmt"
"github.com/Dreamacro/clash/listener/sing_tun"
2022-07-29 01:08:35 +00:00
"golang.org/x/exp/slices"
2018-11-21 05:47:46 +00:00
"net"
2022-05-03 11:13:37 +00:00
"sort"
2018-11-21 05:47:46 +00:00
"strconv"
"sync"
2018-07-15 14:23:20 +00:00
2021-06-13 09:23:10 +00:00
"github.com/Dreamacro/clash/adapter/inbound"
"github.com/Dreamacro/clash/component/ebpf"
2021-11-17 08:03:47 +00:00
"github.com/Dreamacro/clash/config"
2021-06-13 09:23:10 +00:00
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/autoredir"
2021-06-13 09:23:10 +00:00
"github.com/Dreamacro/clash/listener/http"
"github.com/Dreamacro/clash/listener/inner"
2021-06-13 09:23:10 +00:00
"github.com/Dreamacro/clash/listener/mixed"
"github.com/Dreamacro/clash/listener/redir"
"github.com/Dreamacro/clash/listener/socks"
"github.com/Dreamacro/clash/listener/tproxy"
"github.com/Dreamacro/clash/log"
2018-07-15 14:23:20 +00:00
)
var (
allowLan = false
bindAddress = "*"
2022-07-22 07:16:09 +00:00
inboundTfo = false
2018-11-21 05:47:46 +00:00
socksListener *socks.Listener
socksUDPListener *socks.UDPListener
httpListener *http.Listener
redirListener *redir.Listener
redirUDPListener *tproxy.UDPListener
tproxyListener *tproxy.Listener
tproxyUDPListener *tproxy.UDPListener
mixedListener *mixed.Listener
mixedUDPLister *socks.UDPListener
tunLister *sing_tun.Listener
2022-07-29 01:08:35 +00:00
autoRedirListener *autoredir.Listener
autoRedirProgram *ebpf.TcEBpfProgram
2022-08-22 15:17:41 +00:00
tcProgram *ebpf.TcEBpfProgram
// lock for recreate function
2022-07-29 01:08:35 +00:00
socksMux sync.Mutex
httpMux sync.Mutex
redirMux sync.Mutex
tproxyMux sync.Mutex
mixedMux sync.Mutex
tunMux sync.Mutex
autoRedirMux sync.Mutex
tcMux sync.Mutex
2022-11-03 10:56:03 +00:00
LastTunConf config.Tun
2018-07-15 14:23:20 +00:00
)
2018-11-21 05:47:46 +00:00
type Ports struct {
Port int `json:"port"`
SocksPort int `json:"socks-port"`
RedirPort int `json:"redir-port"`
TProxyPort int `json:"tproxy-port"`
MixedPort int `json:"mixed-port"`
2018-11-21 05:47:46 +00:00
}
2022-05-03 11:13:37 +00:00
func GetTunConf() config.Tun {
2022-11-03 10:56:03 +00:00
if tunLister == nil {
2022-05-03 11:13:37 +00:00
return config.Tun{
Enable: false,
}
}
2022-11-03 10:56:03 +00:00
return tunLister.Config()
2022-05-03 11:13:37 +00:00
}
2018-11-21 05:47:46 +00:00
func AllowLan() bool {
return allowLan
}
func BindAddress() string {
return bindAddress
}
2018-11-21 05:47:46 +00:00
func SetAllowLan(al bool) {
allowLan = al
2018-07-15 14:23:20 +00:00
}
func SetBindAddress(host string) {
bindAddress = host
}
2022-07-22 07:16:09 +00:00
func SetInboundTfo(itfo bool) {
inboundTfo = itfo
}
2022-05-07 16:04:16 +00:00
func NewInner(tcpIn chan<- C.ConnContext) {
inner.New(tcpIn)
}
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)
2018-11-21 05:47:46 +00:00
if httpListener != nil {
2021-07-31 16:35:37 +00:00
if httpListener.RawAddress() == addr {
return
2018-11-21 05:47:46 +00:00
}
httpListener.Close()
2018-11-21 05:47:46 +00:00
httpListener = nil
}
if portIsZero(addr) {
return
2018-07-15 14:23:20 +00:00
}
2022-07-22 07:16:09 +00:00
httpListener, err = http.New(addr, inboundTfo, tcpIn)
2018-07-15 14:23:20 +00:00
if err != nil {
2022-01-04 09:58:50 +00:00
log.Errorln("Start HTTP server error: %s", err.Error())
return
2018-07-15 14:23:20 +00:00
}
2021-06-13 09:23:10 +00:00
log.Infoln("HTTP proxy listening at: %s", httpListener.Address())
2018-07-15 14:23:20 +00:00
}
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)
2018-11-21 05:47:46 +00:00
shouldTCPIgnore := false
shouldUDPIgnore := false
2018-11-21 05:47:46 +00:00
if socksListener != nil {
2021-07-31 16:35:37 +00:00
if socksListener.RawAddress() != addr {
socksListener.Close()
socksListener = nil
} else {
shouldTCPIgnore = true
}
}
if socksUDPListener != nil {
2021-07-31 16:35:37 +00:00
if socksUDPListener.RawAddress() != addr {
socksUDPListener.Close()
socksUDPListener = nil
} else {
shouldUDPIgnore = true
2018-11-21 05:47:46 +00:00
}
}
if shouldTCPIgnore && shouldUDPIgnore {
return
}
2018-11-21 05:47:46 +00:00
if portIsZero(addr) {
return
2018-07-15 14:23:20 +00:00
}
2022-07-22 07:16:09 +00:00
tcpListener, err := socks.New(addr, inboundTfo, tcpIn)
2018-07-15 14:23:20 +00:00
if err != nil {
return
2018-07-15 14:23:20 +00:00
}
2021-06-13 09:23:10 +00:00
udpListener, err := socks.NewUDP(addr, udpIn)
if err != nil {
tcpListener.Close()
return
}
socksListener = tcpListener
socksUDPListener = udpListener
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
2018-07-15 14:23:20 +00:00
}
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)
2018-11-21 05:47:46 +00:00
if redirListener != nil {
2021-07-31 16:35:37 +00:00
if redirListener.RawAddress() == addr {
return
2018-11-21 05:47:46 +00:00
}
redirListener.Close()
2018-11-21 05:47:46 +00:00
redirListener = nil
}
if redirUDPListener != nil {
2021-07-31 16:35:37 +00:00
if redirUDPListener.RawAddress() == addr {
return
}
redirUDPListener.Close()
redirUDPListener = nil
}
2018-11-21 05:47:46 +00:00
if portIsZero(addr) {
return
2018-08-11 20:00:34 +00:00
}
2021-06-13 09:23:10 +00:00
redirListener, err = redir.New(addr, tcpIn)
2018-08-11 20:00:34 +00:00
if err != nil {
return
2018-08-11 20:00:34 +00:00
}
2021-06-13 09:23:10 +00:00
redirUDPListener, err = tproxy.NewUDP(addr, udpIn)
if err != nil {
log.Warnln("Failed to start Redir UDP Listener: %s", err)
}
2021-06-13 09:23:10 +00:00
log.Infoln("Redirect proxy listening at: %s", redirListener.Address())
2018-08-11 20:00:34 +00:00
}
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 {
2021-07-31 16:35:37 +00:00
if tproxyListener.RawAddress() == addr {
return
}
tproxyListener.Close()
tproxyListener = nil
}
if tproxyUDPListener != nil {
2021-07-31 16:35:37 +00:00
if tproxyUDPListener.RawAddress() == addr {
return
}
tproxyUDPListener.Close()
tproxyUDPListener = nil
}
if portIsZero(addr) {
return
}
2021-06-13 09:23:10 +00:00
tproxyListener, err = tproxy.New(addr, tcpIn)
if err != nil {
return
}
2021-06-13 09:23:10 +00:00
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn)
if err != nil {
log.Warnln("Failed to start TProxy UDP Listener: %s", err)
}
2021-06-13 09:23:10 +00:00
log.Infoln("TProxy server listening at: %s", tproxyListener.Address())
}
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
shouldUDPIgnore := false
if mixedListener != nil {
2021-07-31 16:35:37 +00:00
if mixedListener.RawAddress() != addr {
mixedListener.Close()
mixedListener = nil
} else {
shouldTCPIgnore = true
}
}
if mixedUDPLister != nil {
2021-07-31 16:35:37 +00:00
if mixedUDPLister.RawAddress() != addr {
mixedUDPLister.Close()
mixedUDPLister = nil
} else {
shouldUDPIgnore = true
}
}
if shouldTCPIgnore && shouldUDPIgnore {
return
}
if portIsZero(addr) {
return
}
2022-07-22 07:16:09 +00:00
mixedListener, err = mixed.New(addr, inboundTfo, tcpIn)
if err != nil {
return
}
2021-06-13 09:23:10 +00:00
mixedUDPLister, err = socks.NewUDP(addr, udpIn)
if err != nil {
mixedListener.Close()
return
}
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
}
2022-11-03 10:56:03 +00:00
func ReCreateTun(tunConf config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
2021-11-17 08:03:47 +00:00
tunMux.Lock()
2022-11-02 15:58:51 +00:00
defer func() {
LastTunConf = tunConf
2022-11-02 15:58:51 +00:00
tunMux.Unlock()
}()
2021-11-17 08:03:47 +00:00
var err error
defer func() {
if err != nil {
2022-03-08 21:08:35 +00:00
log.Errorln("Start TUN listening error: %s", err.Error())
2022-05-19 11:19:19 +00:00
Cleanup(false)
}
}()
if !hasTunConfigChange(&tunConf) {
if tunLister != nil {
tunLister.FlushDefaultInterface()
}
2022-05-03 11:13:37 +00:00
return
}
2022-05-19 11:19:19 +00:00
Cleanup(true)
2021-11-17 08:03:47 +00:00
2022-03-08 21:08:35 +00:00
if !tunConf.Enable {
return
2021-11-17 08:03:47 +00:00
}
2022-05-03 11:13:37 +00:00
2022-11-03 10:04:22 +00:00
tunLister, err = sing_tun.New(tunConf, tcpIn, udpIn)
2021-11-17 08:03:47 +00:00
}
2022-08-22 15:17:41 +00:00
func ReCreateRedirToTun(ifaceNames []string) {
tcMux.Lock()
defer tcMux.Unlock()
nicArr := ifaceNames
slices.Sort(nicArr)
nicArr = slices.Compact(nicArr)
if tcProgram != nil {
tcProgram.Close()
tcProgram = nil
}
if len(nicArr) == 0 {
return
}
2022-11-03 10:56:03 +00:00
tunConf := GetTunConf()
if !tunConf.Enable {
2022-08-22 15:17:41 +00:00
return
}
2022-11-03 10:56:03 +00:00
program, err := ebpf.NewTcEBpfProgram(nicArr, tunConf.Device)
2022-08-22 15:17:41 +00:00
if err != nil {
log.Errorln("Attached tc ebpf program error: %v", err)
return
}
tcProgram = program
log.Infoln("Attached tc ebpf program to interfaces %v", tcProgram.RawNICs())
}
2022-07-29 01:08:35 +00:00
func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- *inbound.PacketAdapter) {
autoRedirMux.Lock()
defer autoRedirMux.Unlock()
var err error
defer func() {
if err != nil {
2022-08-08 02:21:16 +00:00
if autoRedirListener != nil {
_ = autoRedirListener.Close()
autoRedirListener = nil
2022-07-29 01:08:35 +00:00
}
if autoRedirProgram != nil {
autoRedirProgram.Close()
autoRedirProgram = nil
}
log.Errorln("Start auto redirect server error: %s", err.Error())
}
}()
nicArr := ifaceNames
slices.Sort(nicArr)
nicArr = slices.Compact(nicArr)
2022-08-08 02:21:16 +00:00
if autoRedirListener != nil && autoRedirProgram != nil {
_ = autoRedirListener.Close()
2022-07-29 01:08:35 +00:00
autoRedirProgram.Close()
2022-08-08 02:21:16 +00:00
autoRedirListener = nil
2022-07-29 01:08:35 +00:00
autoRedirProgram = nil
}
if len(nicArr) == 0 {
return
}
defaultRouteInterfaceName, err := ebpf.GetAutoDetectInterface()
2022-07-29 01:08:35 +00:00
if err != nil {
return
}
addr := genAddr("*", C.TcpAutoRedirPort, true)
autoRedirListener, err = autoredir.New(addr, tcpIn)
if err != nil {
return
}
autoRedirProgram, err = ebpf.NewRedirEBpfProgram(nicArr, autoRedirListener.TCPAddr().Port(), defaultRouteInterfaceName)
if err != nil {
return
}
autoRedirListener.SetLookupFunc(autoRedirProgram.Lookup)
log.Infoln("Auto redirect proxy listening at: %s, attached tc ebpf program to interfaces %v", autoRedirListener.Address(), autoRedirProgram.RawNICs())
}
2018-11-21 05:47:46 +00:00
// GetPorts return the ports of proxy servers
func GetPorts() *Ports {
ports := &Ports{}
if httpListener != nil {
_, portStr, _ := net.SplitHostPort(httpListener.Address())
2018-11-21 05:47:46 +00:00
port, _ := strconv.Atoi(portStr)
ports.Port = port
}
if socksListener != nil {
_, portStr, _ := net.SplitHostPort(socksListener.Address())
2018-11-21 05:47:46 +00:00
port, _ := strconv.Atoi(portStr)
ports.SocksPort = port
2018-07-15 14:23:20 +00:00
}
2018-11-21 05:47:46 +00:00
if redirListener != nil {
_, portStr, _ := net.SplitHostPort(redirListener.Address())
2018-11-21 05:47:46 +00:00
port, _ := strconv.Atoi(portStr)
ports.RedirPort = port
}
if tproxyListener != nil {
_, portStr, _ := net.SplitHostPort(tproxyListener.Address())
port, _ := strconv.Atoi(portStr)
ports.TProxyPort = port
}
if mixedListener != nil {
_, portStr, _ := net.SplitHostPort(mixedListener.Address())
port, _ := strconv.Atoi(portStr)
ports.MixedPort = port
}
2018-11-21 05:47:46 +00:00
return ports
}
2018-07-15 14:23:20 +00:00
2018-11-21 05:47:46 +00:00
func portIsZero(addr string) bool {
_, port, err := net.SplitHostPort(addr)
if port == "0" || port == "" || err != nil {
return true
}
return false
2018-07-15 14:23:20 +00:00
}
func genAddr(host string, port int, allowLan bool) string {
2018-11-21 05:47:46 +00:00
if allowLan {
if host == "*" {
return fmt.Sprintf(":%d", port)
}
2021-03-23 17:00:21 +00:00
return fmt.Sprintf("%s:%d", host, port)
2018-11-21 05:47:46 +00:00
}
2018-11-21 05:47:46 +00:00
return fmt.Sprintf("127.0.0.1:%d", port)
2018-07-15 14:23:20 +00:00
}
2021-11-17 08:03:47 +00:00
func hasTunConfigChange(tunConf *config.Tun) bool {
if LastTunConf.Enable != tunConf.Enable ||
LastTunConf.Device != tunConf.Device ||
LastTunConf.Stack != tunConf.Stack ||
LastTunConf.AutoRoute != tunConf.AutoRoute ||
LastTunConf.AutoDetectInterface != tunConf.AutoDetectInterface ||
LastTunConf.MTU != tunConf.MTU ||
LastTunConf.StrictRoute != tunConf.StrictRoute ||
LastTunConf.EndpointIndependentNat != tunConf.EndpointIndependentNat ||
LastTunConf.UDPTimeout != tunConf.UDPTimeout {
2022-05-03 11:13:37 +00:00
return true
}
if len(LastTunConf.DNSHijack) != len(tunConf.DNSHijack) {
2022-05-03 11:13:37 +00:00
return true
}
sort.Slice(tunConf.DNSHijack, func(i, j int) bool {
return tunConf.DNSHijack[i].Addr().Less(tunConf.DNSHijack[j].Addr())
})
sort.Slice(tunConf.Inet4Address, func(i, j int) bool {
return tunConf.Inet4Address[i].Build().String() < tunConf.Inet4Address[j].Build().String()
})
sort.Slice(tunConf.Inet6Address, func(i, j int) bool {
return tunConf.Inet6Address[i].Build().String() < tunConf.Inet6Address[j].Build().String()
})
sort.Slice(tunConf.Inet4RouteAddress, func(i, j int) bool {
return tunConf.Inet4RouteAddress[i].Build().String() < tunConf.Inet4RouteAddress[j].Build().String()
})
sort.Slice(tunConf.Inet6RouteAddress, func(i, j int) bool {
return tunConf.Inet6RouteAddress[i].Build().String() < tunConf.Inet6RouteAddress[j].Build().String()
})
sort.Slice(tunConf.IncludeUID, func(i, j int) bool {
return tunConf.IncludeUID[i] < tunConf.IncludeUID[j]
})
sort.Slice(tunConf.IncludeUIDRange, func(i, j int) bool {
return tunConf.IncludeUIDRange[i] < tunConf.IncludeUIDRange[j]
})
sort.Slice(tunConf.ExcludeUID, func(i, j int) bool {
return tunConf.ExcludeUID[i] < tunConf.ExcludeUID[j]
})
sort.Slice(tunConf.ExcludeUIDRange, func(i, j int) bool {
return tunConf.ExcludeUIDRange[i] < tunConf.ExcludeUIDRange[j]
})
sort.Slice(tunConf.IncludeAndroidUser, func(i, j int) bool {
return tunConf.IncludeAndroidUser[i] < tunConf.IncludeAndroidUser[j]
})
sort.Slice(tunConf.IncludePackage, func(i, j int) bool {
return tunConf.IncludePackage[i] < tunConf.IncludePackage[j]
})
sort.Slice(tunConf.ExcludePackage, func(i, j int) bool {
return tunConf.ExcludePackage[i] < tunConf.ExcludePackage[j]
})
2022-05-03 11:13:37 +00:00
if !slices.Equal(tunConf.DNSHijack, LastTunConf.DNSHijack) ||
!slices.Equal(tunConf.Inet4Address, LastTunConf.Inet4Address) ||
!slices.Equal(tunConf.Inet6Address, LastTunConf.Inet6Address) ||
!slices.Equal(tunConf.Inet4RouteAddress, LastTunConf.Inet4RouteAddress) ||
!slices.Equal(tunConf.Inet6RouteAddress, LastTunConf.Inet6RouteAddress) ||
!slices.Equal(tunConf.IncludeUID, LastTunConf.IncludeUID) ||
!slices.Equal(tunConf.IncludeUIDRange, LastTunConf.IncludeUIDRange) ||
!slices.Equal(tunConf.ExcludeUID, LastTunConf.ExcludeUID) ||
!slices.Equal(tunConf.ExcludeUIDRange, LastTunConf.ExcludeUIDRange) ||
!slices.Equal(tunConf.IncludeAndroidUser, LastTunConf.IncludeAndroidUser) ||
!slices.Equal(tunConf.IncludePackage, LastTunConf.IncludePackage) ||
!slices.Equal(tunConf.ExcludePackage, LastTunConf.ExcludePackage) {
2022-05-03 11:13:37 +00:00
return true
}
return false
}
2022-05-19 11:19:19 +00:00
func Cleanup(wait bool) {
if tunLister != nil {
tunLister.Close()
tunLister = nil
2021-11-17 08:03:47 +00:00
}
LastTunConf = config.Tun{}
2021-11-17 08:03:47 +00:00
}