2018-07-15 14:23:20 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2018-11-21 05:47:46 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2020-06-07 09:56:21 +00:00
|
|
|
"sync"
|
2018-07-15 14:23:20 +00:00
|
|
|
|
2020-03-08 13:58:49 +00:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2018-07-15 14:23:20 +00:00
|
|
|
"github.com/Dreamacro/clash/proxy/http"
|
2020-05-12 03:29:53 +00:00
|
|
|
"github.com/Dreamacro/clash/proxy/mixed"
|
2018-08-11 20:00:34 +00:00
|
|
|
"github.com/Dreamacro/clash/proxy/redir"
|
2018-07-15 14:23:20 +00:00
|
|
|
"github.com/Dreamacro/clash/proxy/socks"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-08-08 05:45:07 +00:00
|
|
|
allowLan = false
|
|
|
|
bindAddress = "*"
|
2018-11-21 05:47:46 +00:00
|
|
|
|
2019-07-25 09:47:39 +00:00
|
|
|
socksListener *socks.SockListener
|
|
|
|
socksUDPListener *socks.SockUDPListener
|
|
|
|
httpListener *http.HttpListener
|
|
|
|
redirListener *redir.RedirListener
|
2020-03-08 13:58:49 +00:00
|
|
|
redirUDPListener *redir.RedirUDPListener
|
2020-05-12 03:29:53 +00:00
|
|
|
mixedListener *mixed.MixedListener
|
|
|
|
mixedUDPLister *socks.SockUDPListener
|
2020-06-07 09:56:21 +00:00
|
|
|
|
|
|
|
// lock for recreate function
|
|
|
|
socksMux sync.Mutex
|
|
|
|
httpMux sync.Mutex
|
|
|
|
redirMux sync.Mutex
|
|
|
|
mixedMux sync.Mutex
|
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"`
|
2020-05-12 03:29:53 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
func SetBindAddress(host string) {
|
|
|
|
bindAddress = host
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
func ReCreateHTTP(port int) error {
|
2020-06-07 09:56:21 +00:00
|
|
|
httpMux.Lock()
|
|
|
|
defer httpMux.Unlock()
|
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
addr := genAddr(bindAddress, port, allowLan)
|
2018-11-21 05:47:46 +00:00
|
|
|
|
|
|
|
if httpListener != nil {
|
2018-11-22 03:54:01 +00:00
|
|
|
if httpListener.Address() == addr {
|
2018-11-21 05:47:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-22 03:54:01 +00:00
|
|
|
httpListener.Close()
|
2018-11-21 05:47:46 +00:00
|
|
|
httpListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
|
|
|
return nil
|
2018-07-15 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 03:54:01 +00:00
|
|
|
var err error
|
|
|
|
httpListener, err = http.NewHttpProxy(addr)
|
2018-07-15 14:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
func ReCreateSocks(port int) error {
|
2020-06-07 09:56:21 +00:00
|
|
|
socksMux.Lock()
|
|
|
|
defer socksMux.Unlock()
|
|
|
|
|
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 {
|
2019-10-13 03:19:46 +00:00
|
|
|
if socksListener.Address() != addr {
|
|
|
|
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 {
|
|
|
|
if socksUDPListener.Address() != addr {
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
if portIsZero(addr) {
|
|
|
|
return nil
|
2018-07-15 14:23:20 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 03:19:46 +00:00
|
|
|
tcpListener, err := socks.NewSocksProxy(addr)
|
2018-07-15 14:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-13 03:19:46 +00:00
|
|
|
udpListener, err := socks.NewSocksUDPProxy(addr)
|
2019-07-25 09:47:39 +00:00
|
|
|
if err != nil {
|
2019-10-13 03:19:46 +00:00
|
|
|
tcpListener.Close()
|
2019-07-25 09:47:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-13 03:19:46 +00:00
|
|
|
socksListener = tcpListener
|
|
|
|
socksUDPListener = udpListener
|
|
|
|
|
2018-07-15 14:23:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
func ReCreateRedir(port int) error {
|
2020-06-07 09:56:21 +00:00
|
|
|
redirMux.Lock()
|
|
|
|
defer redirMux.Unlock()
|
|
|
|
|
2019-08-08 05:45:07 +00:00
|
|
|
addr := genAddr(bindAddress, port, allowLan)
|
2018-11-21 05:47:46 +00:00
|
|
|
|
|
|
|
if redirListener != nil {
|
2018-11-22 03:54:01 +00:00
|
|
|
if redirListener.Address() == addr {
|
2018-11-21 05:47:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
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 {
|
|
|
|
if redirUDPListener.Address() == addr {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
redirUDPListener.Close()
|
|
|
|
redirUDPListener = nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 05:47:46 +00:00
|
|
|
if portIsZero(addr) {
|
|
|
|
return nil
|
2018-08-11 20:00:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-22 03:54:01 +00:00
|
|
|
var err error
|
|
|
|
redirListener, err = redir.NewRedirProxy(addr)
|
2018-08-11 20:00:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-08 13:58:49 +00:00
|
|
|
redirUDPListener, err = redir.NewRedirUDPProxy(addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("Failed to start Redir UDP Listener: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-08-11 20:00:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-12 03:29:53 +00:00
|
|
|
func ReCreateMixed(port int) error {
|
2020-06-07 09:56:21 +00:00
|
|
|
mixedMux.Lock()
|
|
|
|
defer mixedMux.Unlock()
|
|
|
|
|
2020-05-12 03:29:53 +00:00
|
|
|
addr := genAddr(bindAddress, port, allowLan)
|
|
|
|
|
|
|
|
shouldTCPIgnore := false
|
|
|
|
shouldUDPIgnore := false
|
|
|
|
|
|
|
|
if mixedListener != nil {
|
|
|
|
if mixedListener.Address() != addr {
|
|
|
|
mixedListener.Close()
|
|
|
|
mixedListener = nil
|
|
|
|
} else {
|
|
|
|
shouldTCPIgnore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if mixedUDPLister != nil {
|
|
|
|
if mixedUDPLister.Address() != addr {
|
|
|
|
mixedUDPLister.Close()
|
|
|
|
mixedUDPLister = nil
|
|
|
|
} else {
|
|
|
|
shouldUDPIgnore = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if shouldTCPIgnore && shouldUDPIgnore {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
mixedListener, err = mixed.NewMixedProxy(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mixedUDPLister, err = socks.NewSocksUDPProxy(addr)
|
|
|
|
if err != nil {
|
|
|
|
mixedListener.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-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)
|
|
|
|
} else {
|
|
|
|
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
|
|
|
}
|