mihomo/listener/listener.go

469 lines
9.3 KiB
Go
Raw Normal View History

2018-07-15 22:23:20 +08:00
package proxy
import (
2018-11-21 13:47:46 +08:00
"fmt"
2022-04-20 22:00:05 +08:00
"github.com/Dreamacro/clash/common/cmd"
"github.com/Dreamacro/clash/listener/inner"
2022-05-19 19:19:19 +08:00
"github.com/Dreamacro/clash/listener/tun/ipstack/commons"
2018-11-21 13:47:46 +08:00
"net"
2022-04-20 22:00:05 +08:00
"runtime"
2022-05-03 19:13:37 +08:00
"sort"
2018-11-21 13:47:46 +08:00
"strconv"
"sync"
2018-07-15 22:23:20 +08:00
2021-06-13 17:23:10 +08:00
"github.com/Dreamacro/clash/adapter/inbound"
2021-11-17 16:03:47 +08:00
"github.com/Dreamacro/clash/config"
2021-06-13 17:23:10 +08:00
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/listener/http"
"github.com/Dreamacro/clash/listener/mixed"
"github.com/Dreamacro/clash/listener/redir"
"github.com/Dreamacro/clash/listener/socks"
"github.com/Dreamacro/clash/listener/tproxy"
2021-11-17 16:03:47 +08:00
"github.com/Dreamacro/clash/listener/tun"
"github.com/Dreamacro/clash/listener/tun/ipstack"
"github.com/Dreamacro/clash/log"
2018-07-15 22:23:20 +08:00
)
var (
allowLan = false
bindAddress = "*"
lastTunConf *config.Tun
2018-11-21 13:47:46 +08: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
2022-03-09 05:08:35 +08:00
tunStackListener ipstack.Stack
// lock for recreate function
socksMux sync.Mutex
httpMux sync.Mutex
redirMux sync.Mutex
tproxyMux sync.Mutex
mixedMux sync.Mutex
2021-11-17 16:03:47 +08:00
tunMux sync.Mutex
2018-07-15 22:23:20 +08:00
)
2018-11-21 13:47:46 +08: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 13:47:46 +08:00
}
2022-05-03 19:13:37 +08:00
func GetTunConf() config.Tun {
if lastTunConf == nil {
return config.Tun{
Enable: false,
}
}
return *lastTunConf
}
2018-11-21 13:47:46 +08:00
func AllowLan() bool {
return allowLan
}
func BindAddress() string {
return bindAddress
}
2018-11-21 13:47:46 +08:00
func SetAllowLan(al bool) {
allowLan = al
2018-07-15 22:23:20 +08:00
}
func SetBindAddress(host string) {
bindAddress = host
}
2022-05-08 00:04:16 +08: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 13:47:46 +08:00
if httpListener != nil {
2021-08-01 00:35:37 +08:00
if httpListener.RawAddress() == addr {
return
2018-11-21 13:47:46 +08:00
}
httpListener.Close()
2018-11-21 13:47:46 +08:00
httpListener = nil
}
if portIsZero(addr) {
return
2018-07-15 22:23:20 +08:00
}
2021-06-13 17:23:10 +08:00
httpListener, err = http.New(addr, tcpIn)
2018-07-15 22:23:20 +08:00
if err != nil {
2022-01-04 17:58:50 +08:00
log.Errorln("Start HTTP server error: %s", err.Error())
return
2018-07-15 22:23:20 +08:00
}
2021-06-13 17:23:10 +08:00
log.Infoln("HTTP proxy listening at: %s", httpListener.Address())
2018-07-15 22:23:20 +08: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 13:47:46 +08:00
shouldTCPIgnore := false
shouldUDPIgnore := false
2018-11-21 13:47:46 +08:00
if socksListener != nil {
2021-08-01 00:35:37 +08:00
if socksListener.RawAddress() != addr {
socksListener.Close()
socksListener = nil
} else {
shouldTCPIgnore = true
}
}
if socksUDPListener != nil {
2021-08-01 00:35:37 +08:00
if socksUDPListener.RawAddress() != addr {
socksUDPListener.Close()
socksUDPListener = nil
} else {
shouldUDPIgnore = true
2018-11-21 13:47:46 +08:00
}
}
if shouldTCPIgnore && shouldUDPIgnore {
return
}
2018-11-21 13:47:46 +08:00
if portIsZero(addr) {
return
2018-07-15 22:23:20 +08:00
}
2021-06-13 17:23:10 +08:00
tcpListener, err := socks.New(addr, tcpIn)
2018-07-15 22:23:20 +08:00
if err != nil {
return
2018-07-15 22:23:20 +08:00
}
2021-06-13 17:23:10 +08: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 22:23:20 +08: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 13:47:46 +08:00
if redirListener != nil {
2021-08-01 00:35:37 +08:00
if redirListener.RawAddress() == addr {
return
2018-11-21 13:47:46 +08:00
}
redirListener.Close()
2018-11-21 13:47:46 +08:00
redirListener = nil
}
if redirUDPListener != nil {
2021-08-01 00:35:37 +08:00
if redirUDPListener.RawAddress() == addr {
return
}
redirUDPListener.Close()
redirUDPListener = nil
}
2018-11-21 13:47:46 +08:00
if portIsZero(addr) {
return
2018-08-12 04:00:34 +08:00
}
2021-06-13 17:23:10 +08:00
redirListener, err = redir.New(addr, tcpIn)
2018-08-12 04:00:34 +08:00
if err != nil {
return
2018-08-12 04:00:34 +08:00
}
2021-06-13 17:23:10 +08:00
redirUDPListener, err = tproxy.NewUDP(addr, udpIn)
if err != nil {
log.Warnln("Failed to start Redir UDP Listener: %s", err)
}
2021-06-13 17:23:10 +08:00
log.Infoln("Redirect proxy listening at: %s", redirListener.Address())
2018-08-12 04:00:34 +08: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-08-01 00:35:37 +08:00
if tproxyListener.RawAddress() == addr {
return
}
tproxyListener.Close()
tproxyListener = nil
}
if tproxyUDPListener != nil {
2021-08-01 00:35:37 +08:00
if tproxyUDPListener.RawAddress() == addr {
return
}
tproxyUDPListener.Close()
tproxyUDPListener = nil
}
if portIsZero(addr) {
return
}
2021-06-13 17:23:10 +08:00
tproxyListener, err = tproxy.New(addr, tcpIn)
if err != nil {
return
}
2021-06-13 17:23:10 +08:00
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn)
if err != nil {
log.Warnln("Failed to start TProxy UDP Listener: %s", err)
}
2021-06-13 17:23:10 +08: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-08-01 00:35:37 +08:00
if mixedListener.RawAddress() != addr {
mixedListener.Close()
mixedListener = nil
} else {
shouldTCPIgnore = true
}
}
if mixedUDPLister != nil {
2021-08-01 00:35:37 +08:00
if mixedUDPLister.RawAddress() != addr {
mixedUDPLister.Close()
mixedUDPLister = nil
} else {
shouldUDPIgnore = true
}
}
if shouldTCPIgnore && shouldUDPIgnore {
return
}
if portIsZero(addr) {
return
}
2021-06-13 17:23:10 +08:00
mixedListener, err = mixed.New(addr, tcpIn)
if err != nil {
return
}
2021-06-13 17:23:10 +08: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-05-19 19:19:19 +08:00
func ReCreateTun(tunConf *config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
2021-11-17 16:03:47 +08:00
tunMux.Lock()
defer tunMux.Unlock()
var err error
defer func() {
if err != nil {
2022-03-09 05:08:35 +08:00
log.Errorln("Start TUN listening error: %s", err.Error())
2022-05-19 19:19:19 +08:00
Cleanup(false)
}
}()
2022-05-19 19:19:19 +08:00
if !hasTunConfigChange(tunConf) {
2022-05-03 19:13:37 +08:00
return
}
2022-05-19 19:19:19 +08:00
Cleanup(true)
2021-11-17 16:03:47 +08:00
2022-03-09 05:08:35 +08:00
if !tunConf.Enable {
return
2021-11-17 16:03:47 +08:00
}
2022-05-03 19:13:37 +08:00
2022-05-19 19:19:19 +08:00
tunStackListener, err = tun.New(tunConf, tcpIn, udpIn)
2022-05-03 19:13:37 +08:00
lastTunConf = tunConf
2021-11-17 16:03:47 +08:00
}
2018-11-21 13:47:46 +08:00
// GetPorts return the ports of proxy servers
func GetPorts() *Ports {
ports := &Ports{}
if httpListener != nil {
_, portStr, _ := net.SplitHostPort(httpListener.Address())
2018-11-21 13:47:46 +08:00
port, _ := strconv.Atoi(portStr)
ports.Port = port
}
if socksListener != nil {
_, portStr, _ := net.SplitHostPort(socksListener.Address())
2018-11-21 13:47:46 +08:00
port, _ := strconv.Atoi(portStr)
ports.SocksPort = port
2018-07-15 22:23:20 +08:00
}
2018-11-21 13:47:46 +08:00
if redirListener != nil {
_, portStr, _ := net.SplitHostPort(redirListener.Address())
2018-11-21 13:47:46 +08: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 13:47:46 +08:00
return ports
}
2018-07-15 22:23:20 +08:00
2018-11-21 13:47:46 +08:00
func portIsZero(addr string) bool {
_, port, err := net.SplitHostPort(addr)
if port == "0" || port == "" || err != nil {
return true
}
return false
2018-07-15 22:23:20 +08:00
}
func genAddr(host string, port int, allowLan bool) string {
2018-11-21 13:47:46 +08:00
if allowLan {
if host == "*" {
return fmt.Sprintf(":%d", port)
}
2021-03-24 01:00:21 +08:00
return fmt.Sprintf("%s:%d", host, port)
2018-11-21 13:47:46 +08:00
}
2018-11-21 13:47:46 +08:00
return fmt.Sprintf("127.0.0.1:%d", port)
2018-07-15 22:23:20 +08:00
}
2021-11-17 16:03:47 +08:00
2022-05-19 19:19:19 +08:00
func hasTunConfigChange(tunConf *config.Tun) bool {
2022-05-03 19:13:37 +08:00
if lastTunConf == nil {
return true
}
if len(lastTunConf.DNSHijack) != len(tunConf.DNSHijack) {
return true
}
sort.Slice(lastTunConf.DNSHijack, func(i, j int) bool {
return lastTunConf.DNSHijack[i].Addr().Less(lastTunConf.DNSHijack[j].Addr())
})
sort.Slice(tunConf.DNSHijack, func(i, j int) bool {
return tunConf.DNSHijack[i].Addr().Less(tunConf.DNSHijack[j].Addr())
})
for i, dns := range tunConf.DNSHijack {
if dns != lastTunConf.DNSHijack[i] {
return true
}
}
if lastTunConf.Enable != tunConf.Enable ||
lastTunConf.Device != tunConf.Device ||
lastTunConf.Stack != tunConf.Stack ||
2022-05-19 19:19:19 +08:00
lastTunConf.AutoRoute != tunConf.AutoRoute ||
lastTunConf.AutoDetectInterface != tunConf.AutoDetectInterface {
2022-05-03 19:13:37 +08:00
return true
}
2022-05-19 19:19:19 +08:00
if tunConf.TunAddressPrefix.String() != lastTunConf.TunAddressPrefix.String() {
2022-05-03 19:13:37 +08:00
return true
}
return false
}
2022-05-19 19:19:19 +08:00
func Cleanup(wait bool) {
if tunStackListener != nil {
_ = tunStackListener.Close()
2022-05-19 19:19:19 +08:00
commons.StopDefaultInterfaceChangeMonitor()
if wait {
commons.WaitForTunClose(lastTunConf.Device)
}
2022-04-20 22:00:05 +08:00
if runtime.GOOS == "android" {
prefs := []int{9000, 9001, 9002, 9003, 9004}
2022-04-22 22:25:45 +08:00
for _, pref := range prefs {
_, _ = cmd.ExecCmd(fmt.Sprintf("ip rule del pref %d", pref))
}
2022-04-20 22:00:05 +08:00
}
2021-11-17 16:03:47 +08:00
}
2022-05-19 19:19:19 +08:00
tunStackListener = nil
lastTunConf = nil
2021-11-17 16:03:47 +08:00
}