2022-11-18 22:57:33 +08:00
|
|
|
package listener
|
2018-07-15 22:23:20 +08:00
|
|
|
|
|
|
|
import (
|
2018-11-21 13:47:46 +08:00
|
|
|
"fmt"
|
2022-07-29 09:08:35 +08:00
|
|
|
"golang.org/x/exp/slices"
|
2018-11-21 13:47:46 +08:00
|
|
|
"net"
|
2022-05-03 19:13:37 +08:00
|
|
|
"sort"
|
2018-11-21 13:47:46 +08:00
|
|
|
"strconv"
|
2022-11-18 22:57:33 +08:00
|
|
|
"strings"
|
2020-06-07 17:56:21 +08:00
|
|
|
"sync"
|
2018-07-15 22:23:20 +08:00
|
|
|
|
2022-10-06 19:23:38 +08:00
|
|
|
"github.com/Dreamacro/clash/component/ebpf"
|
2021-06-13 17:23:10 +08:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-10-06 19:23:38 +08:00
|
|
|
"github.com/Dreamacro/clash/listener/autoredir"
|
2022-12-04 14:37:52 +08:00
|
|
|
LC "github.com/Dreamacro/clash/listener/config"
|
2021-06-13 17:23:10 +08:00
|
|
|
"github.com/Dreamacro/clash/listener/http"
|
|
|
|
"github.com/Dreamacro/clash/listener/mixed"
|
|
|
|
"github.com/Dreamacro/clash/listener/redir"
|
2022-12-05 10:12:53 +08:00
|
|
|
embedSS "github.com/Dreamacro/clash/listener/shadowsocks"
|
2022-11-11 20:56:08 +08:00
|
|
|
"github.com/Dreamacro/clash/listener/sing_shadowsocks"
|
|
|
|
"github.com/Dreamacro/clash/listener/sing_tun"
|
|
|
|
"github.com/Dreamacro/clash/listener/sing_vmess"
|
2021-06-13 17:23:10 +08:00
|
|
|
"github.com/Dreamacro/clash/listener/socks"
|
|
|
|
"github.com/Dreamacro/clash/listener/tproxy"
|
2022-11-28 17:09:25 +08:00
|
|
|
"github.com/Dreamacro/clash/listener/tuic"
|
2022-11-11 20:56:08 +08:00
|
|
|
"github.com/Dreamacro/clash/listener/tunnel"
|
2020-03-08 21:58:49 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2022-11-18 22:57:33 +08:00
|
|
|
|
|
|
|
"github.com/samber/lo"
|
2018-07-15 22:23:20 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-05-24 12:43:26 +08:00
|
|
|
allowLan = false
|
|
|
|
bindAddress = "*"
|
2018-11-21 13:47:46 +08:00
|
|
|
|
2022-11-11 20:56:08 +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-11-28 20:04:56 +08:00
|
|
|
tunnelTCPListeners = map[string]*tunnel.Listener{}
|
|
|
|
tunnelUDPListeners = map[string]*tunnel.PacketConn{}
|
2022-12-04 21:53:13 +08:00
|
|
|
inboundListeners = map[string]C.InboundListener{}
|
2022-11-11 20:56:08 +08:00
|
|
|
tunLister *sing_tun.Listener
|
2022-12-05 10:12:53 +08:00
|
|
|
shadowSocksListener C.MultiAddrListener
|
2022-11-11 20:56:08 +08:00
|
|
|
vmessListener *sing_vmess.Listener
|
2022-11-28 17:09:25 +08:00
|
|
|
tuicListener *tuic.Listener
|
2022-11-11 20:56:08 +08:00
|
|
|
autoRedirListener *autoredir.Listener
|
|
|
|
autoRedirProgram *ebpf.TcEBpfProgram
|
|
|
|
tcProgram *ebpf.TcEBpfProgram
|
2020-06-07 17:56:21 +08:00
|
|
|
|
|
|
|
// lock for recreate function
|
2022-07-29 09:08:35 +08:00
|
|
|
socksMux sync.Mutex
|
|
|
|
httpMux sync.Mutex
|
|
|
|
redirMux sync.Mutex
|
|
|
|
tproxyMux sync.Mutex
|
|
|
|
mixedMux sync.Mutex
|
2022-11-28 20:04:56 +08:00
|
|
|
tunnelMux sync.Mutex
|
2022-12-04 21:53:13 +08:00
|
|
|
inboundMux sync.Mutex
|
2022-07-29 09:08:35 +08:00
|
|
|
tunMux sync.Mutex
|
2022-11-11 20:56:08 +08:00
|
|
|
ssMux sync.Mutex
|
|
|
|
vmessMux sync.Mutex
|
2022-11-28 17:09:25 +08:00
|
|
|
tuicMux sync.Mutex
|
2022-07-29 09:08:35 +08:00
|
|
|
autoRedirMux sync.Mutex
|
|
|
|
tcMux sync.Mutex
|
2022-11-03 18:56:03 +08:00
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
LastTunConf LC.Tun
|
|
|
|
LastTuicConf LC.TuicServer
|
2018-07-15 22:23:20 +08:00
|
|
|
)
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
type Ports struct {
|
2022-11-11 20:56:08 +08:00
|
|
|
Port int `json:"port"`
|
|
|
|
SocksPort int `json:"socks-port"`
|
|
|
|
RedirPort int `json:"redir-port"`
|
|
|
|
TProxyPort int `json:"tproxy-port"`
|
|
|
|
MixedPort int `json:"mixed-port"`
|
|
|
|
ShadowSocksConfig string `json:"ss-config"`
|
|
|
|
VmessConfig string `json:"vmess-config"`
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func GetTunConf() LC.Tun {
|
2022-11-03 18:56:03 +08:00
|
|
|
if tunLister == nil {
|
2023-09-02 01:14:26 +08:00
|
|
|
return LastTunConf
|
2022-05-03 19:13:37 +08:00
|
|
|
}
|
2022-11-03 18:56:03 +08:00
|
|
|
return tunLister.Config()
|
2022-05-03 19:13:37 +08:00
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func GetTuicConf() LC.TuicServer {
|
2022-11-28 18:53:09 +08:00
|
|
|
if tuicListener == nil {
|
2022-12-04 14:37:52 +08:00
|
|
|
return LC.TuicServer{Enable: false}
|
2022-11-28 18:53:09 +08:00
|
|
|
}
|
|
|
|
return tuicListener.Config()
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func AllowLan() bool {
|
|
|
|
return allowLan
|
|
|
|
}
|
|
|
|
|
2019-08-08 13:45:07 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-08 13:45:07 +08:00
|
|
|
func SetBindAddress(host string) {
|
|
|
|
bindAddress = host
|
|
|
|
}
|
|
|
|
|
2021-12-26 22:08:53 +08:00
|
|
|
func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
|
2020-06-07 17:56:21 +08:00
|
|
|
httpMux.Lock()
|
|
|
|
defer httpMux.Unlock()
|
|
|
|
|
2021-12-26 22:08:53 +08:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start HTTP server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-08 13:45:07 +08:00
|
|
|
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 {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
2018-11-22 11:54:01 +08:00
|
|
|
httpListener.Close()
|
2018-11-21 13:47:46 +08:00
|
|
|
httpListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
2022-11-16 10:43:16 +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())
|
2021-12-26 22:08:53 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
2020-06-07 17:56:21 +08:00
|
|
|
socksMux.Lock()
|
|
|
|
defer socksMux.Unlock()
|
|
|
|
|
2021-12-26 22:08:53 +08:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start SOCKS server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-08 13:45:07 +08:00
|
|
|
addr := genAddr(bindAddress, port, allowLan)
|
2018-11-21 13:47:46 +08:00
|
|
|
|
2019-10-13 23:51:26 +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 {
|
2019-10-13 11:19:46 +08:00
|
|
|
socksListener.Close()
|
|
|
|
socksListener = nil
|
2019-10-13 23:51:26 +08:00
|
|
|
} else {
|
|
|
|
shouldTCPIgnore = true
|
2019-10-13 11:19:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if socksUDPListener != nil {
|
2021-08-01 00:35:37 +08:00
|
|
|
if socksUDPListener.RawAddress() != addr {
|
2019-10-13 11:19:46 +08:00
|
|
|
socksUDPListener.Close()
|
|
|
|
socksUDPListener = nil
|
2019-10-13 23:51:26 +08:00
|
|
|
} else {
|
|
|
|
shouldUDPIgnore = true
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:51:26 +08:00
|
|
|
if shouldTCPIgnore && shouldUDPIgnore {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2019-10-13 23:51:26 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
if portIsZero(addr) {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
2022-11-16 10:43:16 +08:00
|
|
|
tcpListener, err := socks.New(addr, tcpIn)
|
2018-07-15 22:23:20 +08:00
|
|
|
if err != nil {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
2021-06-13 17:23:10 +08:00
|
|
|
udpListener, err := socks.NewUDP(addr, udpIn)
|
2019-07-25 17:47:39 +08:00
|
|
|
if err != nil {
|
2019-10-13 11:19:46 +08:00
|
|
|
tcpListener.Close()
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2019-07-25 17:47:39 +08:00
|
|
|
}
|
|
|
|
|
2019-10-13 11:19:46 +08:00
|
|
|
socksListener = tcpListener
|
|
|
|
socksUDPListener = udpListener
|
|
|
|
|
2021-07-18 16:09:09 +08:00
|
|
|
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
2023-02-18 13:16:07 +08:00
|
|
|
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) {
|
2020-06-07 17:56:21 +08:00
|
|
|
redirMux.Lock()
|
|
|
|
defer redirMux.Unlock()
|
|
|
|
|
2021-12-26 22:08:53 +08:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start Redir server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-08 13:45:07 +08:00
|
|
|
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 {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-11-21 13:47:46 +08:00
|
|
|
}
|
2018-11-22 11:54:01 +08:00
|
|
|
redirListener.Close()
|
2018-11-21 13:47:46 +08:00
|
|
|
redirListener = nil
|
|
|
|
}
|
|
|
|
|
2020-03-08 21:58:49 +08:00
|
|
|
if redirUDPListener != nil {
|
2021-08-01 00:35:37 +08:00
|
|
|
if redirUDPListener.RawAddress() == addr {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2020-03-08 21:58:49 +08:00
|
|
|
}
|
|
|
|
redirUDPListener.Close()
|
|
|
|
redirUDPListener = nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
if portIsZero(addr) {
|
2021-12-26 22:08:53 +08:00
|
|
|
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 {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2018-08-12 04:00:34 +08:00
|
|
|
}
|
|
|
|
|
2023-02-18 13:16:07 +08:00
|
|
|
redirUDPListener, err = tproxy.NewUDP(addr, udpIn, natTable)
|
2020-03-08 21:58:49 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func ReCreateShadowSocks(shadowSocksConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
2022-11-11 20:56:08 +08:00
|
|
|
ssMux.Lock()
|
|
|
|
defer ssMux.Unlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start ShadowSocks server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
var ssConfig LC.ShadowsocksServer
|
|
|
|
if addr, cipher, password, err := embedSS.ParseSSURL(shadowSocksConfig); err == nil {
|
|
|
|
ssConfig = LC.ShadowsocksServer{
|
2022-12-05 16:29:50 +08:00
|
|
|
Enable: len(shadowSocksConfig) > 0,
|
2022-12-05 10:12:53 +08:00
|
|
|
Listen: addr,
|
|
|
|
Password: password,
|
|
|
|
Cipher: cipher,
|
2023-03-21 12:40:36 +08:00
|
|
|
Udp: true,
|
2022-12-05 10:12:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:56:08 +08:00
|
|
|
shouldIgnore := false
|
|
|
|
|
|
|
|
if shadowSocksListener != nil {
|
2022-12-05 10:12:53 +08:00
|
|
|
if shadowSocksListener.Config() != ssConfig.String() {
|
2022-11-11 20:56:08 +08:00
|
|
|
shadowSocksListener.Close()
|
|
|
|
shadowSocksListener = nil
|
|
|
|
} else {
|
|
|
|
shouldIgnore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldIgnore {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
if !ssConfig.Enable {
|
2022-11-11 20:56:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
listener, err := sing_shadowsocks.New(ssConfig, tcpIn, udpIn)
|
2022-11-11 20:56:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
shadowSocksListener = listener
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
for _, addr := range shadowSocksListener.AddrList() {
|
|
|
|
log.Infoln("ShadowSocks proxy listening at: %s", addr.String())
|
|
|
|
}
|
2022-11-11 20:56:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func ReCreateVmess(vmessConfig string, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
2022-11-11 20:56:08 +08:00
|
|
|
vmessMux.Lock()
|
|
|
|
defer vmessMux.Unlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start Vmess server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
var vsConfig LC.VmessServer
|
|
|
|
if addr, username, password, err := sing_vmess.ParseVmessURL(vmessConfig); err == nil {
|
|
|
|
vsConfig = LC.VmessServer{
|
2022-12-05 16:29:50 +08:00
|
|
|
Enable: len(vmessConfig) > 0,
|
2022-12-05 10:12:53 +08:00
|
|
|
Listen: addr,
|
|
|
|
Users: []LC.VmessUser{{Username: username, UUID: password, AlterID: 1}},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:56:08 +08:00
|
|
|
shouldIgnore := false
|
|
|
|
|
|
|
|
if vmessListener != nil {
|
2022-12-05 10:12:53 +08:00
|
|
|
if vmessListener.Config() != vsConfig.String() {
|
2022-11-11 20:56:08 +08:00
|
|
|
vmessListener.Close()
|
|
|
|
vmessListener = nil
|
|
|
|
} else {
|
|
|
|
shouldIgnore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldIgnore {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
if !vsConfig.Enable {
|
2022-11-11 20:56:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
listener, err := sing_vmess.New(vsConfig, tcpIn, udpIn)
|
2022-11-11 20:56:08 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vmessListener = listener
|
|
|
|
|
2022-12-05 10:12:53 +08:00
|
|
|
for _, addr := range vmessListener.AddrList() {
|
|
|
|
log.Infoln("Vmess proxy listening at: %s", addr.String())
|
|
|
|
}
|
2022-11-11 20:56:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func ReCreateTuic(config LC.TuicServer, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
2022-11-28 17:09:25 +08:00
|
|
|
tuicMux.Lock()
|
2022-11-28 18:53:09 +08:00
|
|
|
defer func() {
|
|
|
|
LastTuicConf = config
|
|
|
|
tuicMux.Unlock()
|
|
|
|
}()
|
2022-11-28 17:09:25 +08:00
|
|
|
shouldIgnore := false
|
|
|
|
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start Tuic server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if tuicListener != nil {
|
|
|
|
if tuicListener.Config().String() != config.String() {
|
|
|
|
tuicListener.Close()
|
|
|
|
tuicListener = nil
|
|
|
|
} else {
|
|
|
|
shouldIgnore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldIgnore {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !config.Enable {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
listener, err := tuic.New(config, tcpIn, udpIn)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tuicListener = listener
|
|
|
|
|
2022-12-04 23:05:13 +08:00
|
|
|
for _, addr := range tuicListener.AddrList() {
|
|
|
|
log.Infoln("Tuic proxy listening at: %s", addr.String())
|
|
|
|
}
|
2022-11-28 17:09:25 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-18 13:16:07 +08:00
|
|
|
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable) {
|
2020-11-09 10:46:10 +08:00
|
|
|
tproxyMux.Lock()
|
|
|
|
defer tproxyMux.Unlock()
|
|
|
|
|
2021-12-26 22:08:53 +08:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start TProxy server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-11-09 10:46:10 +08:00
|
|
|
addr := genAddr(bindAddress, port, allowLan)
|
|
|
|
|
|
|
|
if tproxyListener != nil {
|
2021-08-01 00:35:37 +08:00
|
|
|
if tproxyListener.RawAddress() == addr {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2020-11-09 10:46:10 +08:00
|
|
|
}
|
|
|
|
tproxyListener.Close()
|
|
|
|
tproxyListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if tproxyUDPListener != nil {
|
2021-08-01 00:35:37 +08:00
|
|
|
if tproxyUDPListener.RawAddress() == addr {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2020-11-09 10:46:10 +08:00
|
|
|
}
|
|
|
|
tproxyUDPListener.Close()
|
|
|
|
tproxyUDPListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2020-11-09 10:46:10 +08:00
|
|
|
}
|
|
|
|
|
2021-06-13 17:23:10 +08:00
|
|
|
tproxyListener, err = tproxy.New(addr, tcpIn)
|
2020-11-09 10:46:10 +08:00
|
|
|
if err != nil {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2020-11-09 10:46:10 +08:00
|
|
|
}
|
|
|
|
|
2023-02-18 13:16:07 +08:00
|
|
|
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn, natTable)
|
2020-11-09 10:46:10 +08:00
|
|
|
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())
|
2020-11-09 10:46:10 +08:00
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
2020-06-07 17:56:21 +08:00
|
|
|
mixedMux.Lock()
|
|
|
|
defer mixedMux.Unlock()
|
|
|
|
|
2021-12-26 22:08:53 +08:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start Mixed(http+socks) server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-12 11:29:53 +08:00
|
|
|
addr := genAddr(bindAddress, port, allowLan)
|
|
|
|
|
|
|
|
shouldTCPIgnore := false
|
|
|
|
shouldUDPIgnore := false
|
|
|
|
|
|
|
|
if mixedListener != nil {
|
2021-08-01 00:35:37 +08:00
|
|
|
if mixedListener.RawAddress() != addr {
|
2020-05-12 11:29:53 +08:00
|
|
|
mixedListener.Close()
|
|
|
|
mixedListener = nil
|
|
|
|
} else {
|
|
|
|
shouldTCPIgnore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mixedUDPLister != nil {
|
2021-08-01 00:35:37 +08:00
|
|
|
if mixedUDPLister.RawAddress() != addr {
|
2020-05-12 11:29:53 +08:00
|
|
|
mixedUDPLister.Close()
|
|
|
|
mixedUDPLister = nil
|
|
|
|
} else {
|
|
|
|
shouldUDPIgnore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldTCPIgnore && shouldUDPIgnore {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
|
2022-11-16 10:43:16 +08:00
|
|
|
mixedListener, err = mixed.New(addr, tcpIn)
|
2020-05-12 11:29:53 +08:00
|
|
|
if err != nil {
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
|
2021-06-13 17:23:10 +08:00
|
|
|
mixedUDPLister, err = socks.NewUDP(addr, udpIn)
|
2020-05-12 11:29:53 +08:00
|
|
|
if err != nil {
|
|
|
|
mixedListener.Close()
|
2021-12-26 22:08:53 +08:00
|
|
|
return
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
|
2021-07-18 16:09:09 +08:00
|
|
|
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
|
2020-05-12 11:29:53 +08:00
|
|
|
}
|
|
|
|
|
2022-12-04 14:37:52 +08:00
|
|
|
func ReCreateTun(tunConf LC.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
2021-11-17 16:03:47 +08:00
|
|
|
tunMux.Lock()
|
2022-11-02 23:58:51 +08:00
|
|
|
defer func() {
|
2022-11-04 08:52:30 +08:00
|
|
|
LastTunConf = tunConf
|
2022-11-02 23:58:51 +08:00
|
|
|
tunMux.Unlock()
|
|
|
|
}()
|
2021-11-17 16:03:47 +08:00
|
|
|
|
2022-02-04 06:11:24 +08:00
|
|
|
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())
|
2023-08-15 09:10:41 +08:00
|
|
|
tunConf.Enable = false
|
2022-02-04 06:11:24 +08:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-11-04 08:52:30 +08:00
|
|
|
if !hasTunConfigChange(&tunConf) {
|
2022-10-10 09:32:42 +08:00
|
|
|
if tunLister != nil {
|
|
|
|
tunLister.FlushDefaultInterface()
|
|
|
|
}
|
2022-05-03 19:13:37 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-02 03:27:48 +08:00
|
|
|
closeTunListener()
|
2021-11-17 16:03:47 +08:00
|
|
|
|
2022-03-09 05:08:35 +08:00
|
|
|
if !tunConf.Enable {
|
2022-02-04 06:11:24 +08:00
|
|
|
return
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
2022-05-03 19:13:37 +08:00
|
|
|
|
2022-12-08 11:29:39 +08:00
|
|
|
lister, err := sing_tun.New(tunConf, tcpIn, udpIn)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tunLister = lister
|
|
|
|
|
2022-12-05 17:43:50 +08:00
|
|
|
log.Infoln("[TUN] Tun adapter listening at: %s", tunLister.Address())
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
2022-08-22 23:17:41 +08: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 18:56:03 +08:00
|
|
|
tunConf := GetTunConf()
|
|
|
|
|
|
|
|
if !tunConf.Enable {
|
2022-08-22 23:17:41 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-03 18:56:03 +08:00
|
|
|
program, err := ebpf.NewTcEBpfProgram(nicArr, tunConf.Device)
|
2022-08-22 23:17:41 +08: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-12-04 14:37:52 +08:00
|
|
|
func ReCreateAutoRedir(ifaceNames []string, tcpIn chan<- C.ConnContext, _ chan<- C.PacketAdapter) {
|
2022-07-29 09:08:35 +08:00
|
|
|
autoRedirMux.Lock()
|
|
|
|
defer autoRedirMux.Unlock()
|
|
|
|
|
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2022-08-08 10:21:16 +08:00
|
|
|
if autoRedirListener != nil {
|
|
|
|
_ = autoRedirListener.Close()
|
|
|
|
autoRedirListener = nil
|
2022-07-29 09:08:35 +08: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 10:21:16 +08:00
|
|
|
if autoRedirListener != nil && autoRedirProgram != nil {
|
|
|
|
_ = autoRedirListener.Close()
|
2022-07-29 09:08:35 +08:00
|
|
|
autoRedirProgram.Close()
|
2022-08-08 10:21:16 +08:00
|
|
|
autoRedirListener = nil
|
2022-07-29 09:08:35 +08:00
|
|
|
autoRedirProgram = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(nicArr) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-10-06 19:23:38 +08:00
|
|
|
defaultRouteInterfaceName, err := ebpf.GetAutoDetectInterface()
|
2022-07-29 09:08:35 +08: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())
|
|
|
|
}
|
|
|
|
|
2022-12-06 10:13:05 +08:00
|
|
|
func PatchTunnel(tunnels []LC.Tunnel, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter) {
|
2022-11-18 22:57:33 +08:00
|
|
|
tunnelMux.Lock()
|
|
|
|
defer tunnelMux.Unlock()
|
|
|
|
|
|
|
|
type addrProxy struct {
|
|
|
|
network string
|
|
|
|
addr string
|
|
|
|
target string
|
|
|
|
proxy string
|
|
|
|
}
|
|
|
|
|
|
|
|
tcpOld := lo.Map(
|
|
|
|
lo.Keys(tunnelTCPListeners),
|
|
|
|
func(key string, _ int) addrProxy {
|
|
|
|
parts := strings.Split(key, "/")
|
|
|
|
return addrProxy{
|
|
|
|
network: "tcp",
|
|
|
|
addr: parts[0],
|
|
|
|
target: parts[1],
|
|
|
|
proxy: parts[2],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
udpOld := lo.Map(
|
|
|
|
lo.Keys(tunnelUDPListeners),
|
|
|
|
func(key string, _ int) addrProxy {
|
|
|
|
parts := strings.Split(key, "/")
|
|
|
|
return addrProxy{
|
|
|
|
network: "udp",
|
|
|
|
addr: parts[0],
|
|
|
|
target: parts[1],
|
|
|
|
proxy: parts[2],
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
oldElm := lo.Union(tcpOld, udpOld)
|
|
|
|
|
|
|
|
newElm := lo.FlatMap(
|
|
|
|
tunnels,
|
2022-12-06 10:13:05 +08:00
|
|
|
func(tunnel LC.Tunnel, _ int) []addrProxy {
|
2022-11-18 22:57:33 +08:00
|
|
|
return lo.Map(
|
|
|
|
tunnel.Network,
|
|
|
|
func(network string, _ int) addrProxy {
|
|
|
|
return addrProxy{
|
|
|
|
network: network,
|
|
|
|
addr: tunnel.Address,
|
|
|
|
target: tunnel.Target,
|
|
|
|
proxy: tunnel.Proxy,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
needClose, needCreate := lo.Difference(oldElm, newElm)
|
|
|
|
|
|
|
|
for _, elm := range needClose {
|
|
|
|
key := fmt.Sprintf("%s/%s/%s", elm.addr, elm.target, elm.proxy)
|
|
|
|
if elm.network == "tcp" {
|
|
|
|
tunnelTCPListeners[key].Close()
|
|
|
|
delete(tunnelTCPListeners, key)
|
|
|
|
} else {
|
|
|
|
tunnelUDPListeners[key].Close()
|
|
|
|
delete(tunnelUDPListeners, key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
if err != nil {
|
2022-11-22 21:01:51 +08:00
|
|
|
log.Errorln("Start tunnel %s error: %s", elm.target, err.Error())
|
2022-11-18 22:57:33 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
if err != nil {
|
2022-11-22 21:01:51 +08:00
|
|
|
log.Errorln("Start tunnel %s error: %s", elm.target, err.Error())
|
2022-11-18 22:57:33 +08:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
tunnelUDPListeners[key] = l
|
|
|
|
log.Infoln("Tunnel(udp/%s) proxy %s listening at: %s", elm.target, elm.proxy, tunnelUDPListeners[key].Address())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-18 13:16:07 +08:00
|
|
|
func PatchInboundListeners(newListenerMap map[string]C.InboundListener, tcpIn chan<- C.ConnContext, udpIn chan<- C.PacketAdapter, natTable C.NatTable, dropOld bool) {
|
2022-12-04 21:53:13 +08:00
|
|
|
inboundMux.Lock()
|
|
|
|
defer inboundMux.Unlock()
|
|
|
|
|
|
|
|
for name, newListener := range newListenerMap {
|
|
|
|
if oldListener, ok := inboundListeners[name]; ok {
|
|
|
|
if !oldListener.Config().Equal(newListener.Config()) {
|
|
|
|
_ = oldListener.Close()
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-02-18 13:16:07 +08:00
|
|
|
if err := newListener.Listen(tcpIn, udpIn, natTable); err != nil {
|
2022-12-04 21:53:13 +08:00
|
|
|
log.Errorln("Listener %s listen err: %s", name, err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
inboundListeners[name] = newListener
|
|
|
|
}
|
|
|
|
|
|
|
|
if dropOld {
|
|
|
|
for name, oldListener := range inboundListeners {
|
|
|
|
if _, ok := newListenerMap[name]; !ok {
|
|
|
|
_ = oldListener.Close()
|
|
|
|
delete(inboundListeners, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
// GetPorts return the ports of proxy servers
|
|
|
|
func GetPorts() *Ports {
|
|
|
|
ports := &Ports{}
|
|
|
|
|
|
|
|
if httpListener != nil {
|
2018-11-22 11:54:01 +08:00
|
|
|
_, portStr, _ := net.SplitHostPort(httpListener.Address())
|
2018-11-21 13:47:46 +08:00
|
|
|
port, _ := strconv.Atoi(portStr)
|
|
|
|
ports.Port = port
|
|
|
|
}
|
|
|
|
|
|
|
|
if socksListener != nil {
|
2018-11-22 11:54:01 +08:00
|
|
|
_, 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 {
|
2018-11-22 11:54:01 +08:00
|
|
|
_, portStr, _ := net.SplitHostPort(redirListener.Address())
|
2018-11-21 13:47:46 +08:00
|
|
|
port, _ := strconv.Atoi(portStr)
|
|
|
|
ports.RedirPort = port
|
|
|
|
}
|
|
|
|
|
2020-11-09 10:46:10 +08:00
|
|
|
if tproxyListener != nil {
|
|
|
|
_, portStr, _ := net.SplitHostPort(tproxyListener.Address())
|
|
|
|
port, _ := strconv.Atoi(portStr)
|
|
|
|
ports.TProxyPort = port
|
|
|
|
}
|
|
|
|
|
2020-05-12 11:29:53 +08:00
|
|
|
if mixedListener != nil {
|
|
|
|
_, portStr, _ := net.SplitHostPort(mixedListener.Address())
|
|
|
|
port, _ := strconv.Atoi(portStr)
|
|
|
|
ports.MixedPort = port
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:56:08 +08:00
|
|
|
if shadowSocksListener != nil {
|
|
|
|
ports.ShadowSocksConfig = shadowSocksListener.Config()
|
|
|
|
}
|
|
|
|
|
|
|
|
if vmessListener != nil {
|
|
|
|
ports.VmessConfig = vmessListener.Config()
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
return ports
|
2018-07-26 00:04:59 +08:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-08-08 13:45:07 +08:00
|
|
|
func genAddr(host string, port int, allowLan bool) string {
|
2018-11-21 13:47:46 +08:00
|
|
|
if allowLan {
|
2019-08-08 13:45:07 +08:00
|
|
|
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
|
|
|
}
|
2019-08-08 13:45:07 +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-12-04 14:37:52 +08:00
|
|
|
func hasTunConfigChange(tunConf *LC.Tun) bool {
|
2022-11-04 08:52:30 +08:00
|
|
|
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 ||
|
2023-03-15 23:43:58 +08:00
|
|
|
LastTunConf.UDPTimeout != tunConf.UDPTimeout ||
|
|
|
|
LastTunConf.FileDescriptor != tunConf.FileDescriptor {
|
2022-05-03 19:13:37 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-11-04 08:52:30 +08:00
|
|
|
if len(LastTunConf.DNSHijack) != len(tunConf.DNSHijack) {
|
2022-05-03 19:13:37 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(tunConf.DNSHijack, func(i, j int) bool {
|
2022-12-06 09:04:30 +08:00
|
|
|
return tunConf.DNSHijack[i] < tunConf.DNSHijack[j]
|
2022-05-03 19:13:37 +08:00
|
|
|
})
|
|
|
|
|
2022-11-04 08:52:30 +08:00
|
|
|
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 19:13:37 +08:00
|
|
|
|
2022-11-04 08:52:30 +08: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 19:13:37 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-02 03:27:48 +08:00
|
|
|
func closeTunListener() {
|
2022-10-06 19:23:38 +08:00
|
|
|
if tunLister != nil {
|
|
|
|
tunLister.Close()
|
|
|
|
tunLister = nil
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
2023-09-02 03:27:48 +08:00
|
|
|
}
|
|
|
|
|
2023-09-02 14:10:26 +08:00
|
|
|
func Cleanup() {
|
2023-09-02 03:27:48 +08:00
|
|
|
closeTunListener()
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|