2018-07-15 22:23:20 +08:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
2018-11-21 13:47:46 +08:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strconv"
|
2018-07-15 22:23:20 +08:00
|
|
|
|
|
|
|
"github.com/Dreamacro/clash/proxy/http"
|
2018-08-12 04:00:34 +08:00
|
|
|
"github.com/Dreamacro/clash/proxy/redir"
|
2018-07-15 22:23:20 +08:00
|
|
|
"github.com/Dreamacro/clash/proxy/socks"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-11-21 13:47:46 +08:00
|
|
|
allowLan = false
|
|
|
|
|
2018-11-22 11:54:01 +08:00
|
|
|
socksListener listener
|
|
|
|
httpListener listener
|
|
|
|
redirListener listener
|
2018-07-15 22:23:20 +08:00
|
|
|
)
|
|
|
|
|
2018-11-22 11:54:01 +08:00
|
|
|
type listener interface {
|
|
|
|
Close()
|
|
|
|
Address() string
|
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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func AllowLan() bool {
|
|
|
|
return allowLan
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetAllowLan(al bool) {
|
|
|
|
allowLan = al
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func ReCreateHTTP(port int) error {
|
|
|
|
addr := genAddr(port, allowLan)
|
|
|
|
|
|
|
|
if httpListener != nil {
|
2018-11-22 11:54:01 +08:00
|
|
|
if httpListener.Address() == addr {
|
2018-11-21 13:47:46 +08:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-22 11:54:01 +08:00
|
|
|
httpListener.Close()
|
2018-11-21 13:47:46 +08:00
|
|
|
httpListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
|
|
|
return nil
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
2018-11-22 11:54:01 +08:00
|
|
|
var err error
|
|
|
|
httpListener, err = http.NewHttpProxy(addr)
|
2018-07-15 22:23:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func ReCreateSocks(port int) error {
|
|
|
|
addr := genAddr(port, allowLan)
|
|
|
|
|
|
|
|
if socksListener != nil {
|
2018-11-22 11:54:01 +08:00
|
|
|
if socksListener.Address() == addr {
|
2018-11-21 13:47:46 +08:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-22 11:54:01 +08:00
|
|
|
socksListener.Close()
|
2018-11-21 13:47:46 +08:00
|
|
|
socksListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
|
|
|
return nil
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|
|
|
|
|
2018-11-22 11:54:01 +08:00
|
|
|
var err error
|
|
|
|
socksListener, err = socks.NewSocksProxy(addr)
|
2018-07-15 22:23:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func ReCreateRedir(port int) error {
|
|
|
|
addr := genAddr(port, allowLan)
|
|
|
|
|
|
|
|
if redirListener != nil {
|
2018-11-22 11:54:01 +08:00
|
|
|
if redirListener.Address() == addr {
|
2018-11-21 13:47:46 +08:00
|
|
|
return nil
|
|
|
|
}
|
2018-11-22 11:54:01 +08:00
|
|
|
redirListener.Close()
|
2018-11-21 13:47:46 +08:00
|
|
|
redirListener = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if portIsZero(addr) {
|
|
|
|
return nil
|
2018-08-12 04:00:34 +08:00
|
|
|
}
|
|
|
|
|
2018-11-22 11:54:01 +08:00
|
|
|
var err error
|
|
|
|
redirListener, err = redir.NewRedirProxy(addr)
|
2018-08-12 04:00:34 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-21 13:47:46 +08:00
|
|
|
func genAddr(port int, allowLan bool) string {
|
|
|
|
if allowLan {
|
|
|
|
return fmt.Sprintf(":%d", port)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("127.0.0.1:%d", port)
|
2018-07-15 22:23:20 +08:00
|
|
|
}
|