2018-07-15 14:23:20 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2018-11-21 05:47:46 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2021-11-17 08:03:47 +00:00
|
|
|
"runtime"
|
2018-11-21 05:47:46 +00:00
|
|
|
"strconv"
|
2020-06-07 09:56:21 +00:00
|
|
|
"sync"
|
2018-07-15 14:23:20 +00:00
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
"github.com/Dreamacro/clash/adapter/inbound"
|
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/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 08:03:47 +00:00
|
|
|
"github.com/Dreamacro/clash/listener/tun"
|
|
|
|
"github.com/Dreamacro/clash/listener/tun/ipstack"
|
2020-03-08 13:58:49 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2018-07-15 14:23:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-08 05:45:07 +00:00
|
|
|
allowLan = false
|
|
|
|
bindAddress = "*"
|
2018-11-21 05:47:46 +00:00
|
|
|
|
2021-08-04 15:52:50 +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
|
2021-11-17 08:03:47 +00:00
|
|
|
tunAdapter ipstack.TunAdapter
|
2020-06-07 09:56:21 +00:00
|
|
|
|
|
|
|
// lock for recreate function
|
2020-11-09 02:46:10 +00:00
|
|
|
socksMux sync.Mutex
|
|
|
|
httpMux sync.Mutex
|
|
|
|
redirMux sync.Mutex
|
|
|
|
tproxyMux sync.Mutex
|
|
|
|
mixedMux sync.Mutex
|
2021-11-17 08:03:47 +00:00
|
|
|
tunMux sync.Mutex
|
2018-07-15 14:23:20 +00:00
|
|
|
)
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
type Ports struct {
|
2020-11-09 02:46:10 +00: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"`
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func AllowLan() bool {
|
|
|
|
return allowLan
|
|
|
|
}
|
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-11-17 08:03:47 +00:00
|
|
|
func Tun() config.Tun {
|
|
|
|
if tunAdapter == nil {
|
|
|
|
return config.Tun{}
|
|
|
|
}
|
|
|
|
return config.Tun{
|
|
|
|
Enable: true,
|
|
|
|
Stack: tunAdapter.Stack(),
|
|
|
|
DNSListen: tunAdapter.DNSListen(),
|
|
|
|
AutoRoute: tunAdapter.AutoRoute(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
func SetBindAddress(host string) {
|
|
|
|
bindAddress = host
|
|
|
|
}
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
func ReCreateHTTP(port int, tcpIn chan<- C.ConnContext) {
|
2020-06-07 09:56:21 +00:00
|
|
|
httpMux.Lock()
|
|
|
|
defer httpMux.Unlock()
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start HTTP server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
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 {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
2018-11-22 03:54:01 +00:00
|
|
|
httpListener.Close()
|
2018-11-21 05:47:46 +00:00
|
|
|
httpListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2018-07-15 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
httpListener, err = http.New(addr, 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())
|
2021-12-26 14:08:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
func ReCreateSocks(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
|
2020-06-07 09:56:21 +00:00
|
|
|
socksMux.Lock()
|
|
|
|
defer socksMux.Unlock()
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start SOCKS server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
addr := genAddr(bindAddress, port, allowLan)
|
2018-11-21 05:47:46 +00:00
|
|
|
|
2019-10-13 15:51:26 +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 {
|
2019-10-13 03:19:46 +00:00
|
|
|
socksListener.Close()
|
|
|
|
socksListener = nil
|
2019-10-13 15:51:26 +00:00
|
|
|
} else {
|
|
|
|
shouldTCPIgnore = true
|
2019-10-13 03:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if socksUDPListener != nil {
|
2021-07-31 16:35:37 +00:00
|
|
|
if socksUDPListener.RawAddress() != addr {
|
2019-10-13 03:19:46 +00:00
|
|
|
socksUDPListener.Close()
|
|
|
|
socksUDPListener = nil
|
2019-10-13 15:51:26 +00:00
|
|
|
} else {
|
|
|
|
shouldUDPIgnore = true
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 15:51:26 +00:00
|
|
|
if shouldTCPIgnore && shouldUDPIgnore {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2019-10-13 15:51:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
if portIsZero(addr) {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2018-07-15 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
tcpListener, err := socks.New(addr, tcpIn)
|
2018-07-15 14:23:20 +00:00
|
|
|
if err != nil {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2018-07-15 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
udpListener, err := socks.NewUDP(addr, udpIn)
|
2019-07-25 09:47:39 +00:00
|
|
|
if err != nil {
|
2019-10-13 03:19:46 +00:00
|
|
|
tcpListener.Close()
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2019-07-25 09:47:39 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 03:19:46 +00:00
|
|
|
socksListener = tcpListener
|
|
|
|
socksUDPListener = udpListener
|
|
|
|
|
2021-07-18 08:09:09 +00:00
|
|
|
log.Infoln("SOCKS proxy listening at: %s", socksListener.Address())
|
2018-07-15 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
func ReCreateRedir(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
|
2020-06-07 09:56:21 +00:00
|
|
|
redirMux.Lock()
|
|
|
|
defer redirMux.Unlock()
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start Redir server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
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 {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2018-11-21 05:47:46 +00:00
|
|
|
}
|
2018-11-22 03:54:01 +00:00
|
|
|
redirListener.Close()
|
2018-11-21 05:47:46 +00:00
|
|
|
redirListener = nil
|
|
|
|
}
|
|
|
|
|
2020-03-08 13:58:49 +00:00
|
|
|
if redirUDPListener != nil {
|
2021-07-31 16:35:37 +00:00
|
|
|
if redirUDPListener.RawAddress() == addr {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2020-03-08 13:58:49 +00:00
|
|
|
}
|
|
|
|
redirUDPListener.Close()
|
|
|
|
redirUDPListener = nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
if portIsZero(addr) {
|
2021-12-26 14:08:53 +00:00
|
|
|
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 {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2018-08-11 20:00:34 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
redirUDPListener, err = tproxy.NewUDP(addr, udpIn)
|
2020-03-08 13:58:49 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
func ReCreateTProxy(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
|
2020-11-09 02:46:10 +00:00
|
|
|
tproxyMux.Lock()
|
|
|
|
defer tproxyMux.Unlock()
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start TProxy server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-11-09 02:46:10 +00:00
|
|
|
addr := genAddr(bindAddress, port, allowLan)
|
|
|
|
|
|
|
|
if tproxyListener != nil {
|
2021-07-31 16:35:37 +00:00
|
|
|
if tproxyListener.RawAddress() == addr {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2020-11-09 02:46:10 +00:00
|
|
|
}
|
|
|
|
tproxyListener.Close()
|
|
|
|
tproxyListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if tproxyUDPListener != nil {
|
2021-07-31 16:35:37 +00:00
|
|
|
if tproxyUDPListener.RawAddress() == addr {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2020-11-09 02:46:10 +00:00
|
|
|
}
|
|
|
|
tproxyUDPListener.Close()
|
|
|
|
tproxyUDPListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2020-11-09 02:46:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
tproxyListener, err = tproxy.New(addr, tcpIn)
|
2020-11-09 02:46:10 +00:00
|
|
|
if err != nil {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2020-11-09 02:46:10 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
tproxyUDPListener, err = tproxy.NewUDP(addr, udpIn)
|
2020-11-09 02:46:10 +00:00
|
|
|
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())
|
2020-11-09 02:46:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
func ReCreateMixed(port int, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) {
|
2020-06-07 09:56:21 +00:00
|
|
|
mixedMux.Lock()
|
|
|
|
defer mixedMux.Unlock()
|
|
|
|
|
2021-12-26 14:08:53 +00:00
|
|
|
var err error
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
log.Errorln("Start Mixed(http+socks) server error: %s", err.Error())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-05-12 03:29:53 +00:00
|
|
|
addr := genAddr(bindAddress, port, allowLan)
|
|
|
|
|
|
|
|
shouldTCPIgnore := false
|
|
|
|
shouldUDPIgnore := false
|
|
|
|
|
|
|
|
if mixedListener != nil {
|
2021-07-31 16:35:37 +00:00
|
|
|
if mixedListener.RawAddress() != addr {
|
2020-05-12 03:29:53 +00:00
|
|
|
mixedListener.Close()
|
|
|
|
mixedListener = nil
|
|
|
|
} else {
|
|
|
|
shouldTCPIgnore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mixedUDPLister != nil {
|
2021-07-31 16:35:37 +00:00
|
|
|
if mixedUDPLister.RawAddress() != addr {
|
2020-05-12 03:29:53 +00:00
|
|
|
mixedUDPLister.Close()
|
|
|
|
mixedUDPLister = nil
|
|
|
|
} else {
|
|
|
|
shouldUDPIgnore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldTCPIgnore && shouldUDPIgnore {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2020-05-12 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2020-05-12 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
mixedListener, err = mixed.New(addr, tcpIn)
|
2020-05-12 03:29:53 +00:00
|
|
|
if err != nil {
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2020-05-12 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
2021-06-13 09:23:10 +00:00
|
|
|
mixedUDPLister, err = socks.NewUDP(addr, udpIn)
|
2020-05-12 03:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
mixedListener.Close()
|
2021-12-26 14:08:53 +00:00
|
|
|
return
|
2020-05-12 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 08:09:09 +00:00
|
|
|
log.Infoln("Mixed(http+socks) proxy listening at: %s", mixedListener.Address())
|
2020-05-12 03:29:53 +00:00
|
|
|
}
|
|
|
|
|
2021-11-17 08:03:47 +00:00
|
|
|
func ReCreateTun(conf config.Tun, tcpIn chan<- C.ConnContext, udpIn chan<- *inbound.PacketAdapter) error {
|
|
|
|
tunMux.Lock()
|
|
|
|
defer tunMux.Unlock()
|
|
|
|
|
|
|
|
if tunAdapter != nil {
|
|
|
|
tunAdapter.Close()
|
|
|
|
tunAdapter = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !conf.Enable {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
tunAdapter, err = tun.New(conf, tcpIn, udpIn)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
// GetPorts return the ports of proxy servers
|
|
|
|
func GetPorts() *Ports {
|
|
|
|
ports := &Ports{}
|
|
|
|
|
|
|
|
if httpListener != nil {
|
2018-11-22 03:54:01 +00:00
|
|
|
_, portStr, _ := net.SplitHostPort(httpListener.Address())
|
2018-11-21 05:47:46 +00:00
|
|
|
port, _ := strconv.Atoi(portStr)
|
|
|
|
ports.Port = port
|
|
|
|
}
|
|
|
|
|
|
|
|
if socksListener != nil {
|
2018-11-22 03:54:01 +00:00
|
|
|
_, 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 {
|
2018-11-22 03:54:01 +00:00
|
|
|
_, portStr, _ := net.SplitHostPort(redirListener.Address())
|
2018-11-21 05:47:46 +00:00
|
|
|
port, _ := strconv.Atoi(portStr)
|
|
|
|
ports.RedirPort = port
|
|
|
|
}
|
|
|
|
|
2020-11-09 02:46:10 +00:00
|
|
|
if tproxyListener != nil {
|
|
|
|
_, portStr, _ := net.SplitHostPort(tproxyListener.Address())
|
|
|
|
port, _ := strconv.Atoi(portStr)
|
|
|
|
ports.TProxyPort = port
|
|
|
|
}
|
|
|
|
|
2020-05-12 03:29:53 +00:00
|
|
|
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-25 16:04:59 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
func genAddr(host string, port int, allowLan bool) string {
|
2018-11-21 05:47:46 +00:00
|
|
|
if allowLan {
|
2019-08-08 05:45:07 +00:00
|
|
|
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
|
|
|
}
|
2019-08-08 05:45:07 +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
|
|
|
|
|
|
|
// CleanUp clean up something
|
|
|
|
func CleanUp() {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
if tunAdapter != nil {
|
|
|
|
tunAdapter.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|