clash/proxy/listener.go

167 lines
2.7 KiB
Go
Raw Normal View History

2018-07-15 14:23:20 +00:00
package proxy
import (
2018-11-21 05:47:46 +00:00
"fmt"
"net"
"strconv"
2018-07-15 14:23:20 +00:00
"github.com/Dreamacro/clash/proxy/http"
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 (
2018-11-21 05:47:46 +00:00
allowLan = false
socksListener *listener
httpListener *listener
redirListener *listener
2018-07-15 14:23:20 +00:00
)
2018-11-21 05:47:46 +00:00
type listener struct {
Address string
Done chan<- struct{}
Closed <-chan struct{}
}
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 14:23:20 +00:00
}
2018-11-21 05:47:46 +00:00
func ReCreateHTTP(port int) error {
addr := genAddr(port, allowLan)
if httpListener != nil {
if httpListener.Address == addr {
return nil
}
httpListener.Done <- struct{}{}
<-httpListener.Closed
httpListener = nil
}
if portIsZero(addr) {
return nil
2018-07-15 14:23:20 +00:00
}
2018-11-21 05:47:46 +00:00
done, closed, err := http.NewHttpProxy(addr)
2018-07-15 14:23:20 +00:00
if err != nil {
return err
}
2018-11-21 05:47:46 +00:00
httpListener = &listener{
Address: addr,
Done: done,
Closed: closed,
}
2018-07-15 14:23:20 +00:00
return nil
}
2018-11-21 05:47:46 +00:00
func ReCreateSocks(port int) error {
addr := genAddr(port, allowLan)
if socksListener != nil {
if socksListener.Address == addr {
return nil
}
socksListener.Done <- struct{}{}
<-socksListener.Closed
socksListener = nil
}
if portIsZero(addr) {
return nil
2018-07-15 14:23:20 +00:00
}
2018-11-21 05:47:46 +00:00
done, closed, err := socks.NewSocksProxy(addr)
2018-07-15 14:23:20 +00:00
if err != nil {
return err
}
2018-11-21 05:47:46 +00:00
socksListener = &listener{
Address: addr,
Done: done,
Closed: closed,
}
2018-07-15 14:23:20 +00:00
return nil
}
2018-11-21 05:47:46 +00:00
func ReCreateRedir(port int) error {
addr := genAddr(port, allowLan)
if redirListener != nil {
if redirListener.Address == addr {
return nil
}
redirListener.Done <- struct{}{}
<-redirListener.Closed
redirListener = nil
}
if portIsZero(addr) {
return nil
2018-08-11 20:00:34 +00:00
}
2018-11-21 05:47:46 +00:00
done, closed, err := redir.NewRedirProxy(addr)
2018-08-11 20:00:34 +00:00
if err != nil {
return err
}
2018-11-21 05:47:46 +00:00
redirListener = &listener{
Address: addr,
Done: done,
Closed: closed,
}
2018-08-11 20:00:34 +00:00
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 {
_, portStr, _ := net.SplitHostPort(httpListener.Address)
port, _ := strconv.Atoi(portStr)
ports.Port = port
}
if socksListener != nil {
_, portStr, _ := net.SplitHostPort(socksListener.Address)
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 {
_, portStr, _ := net.SplitHostPort(redirListener.Address)
port, _ := strconv.Atoi(portStr)
ports.RedirPort = port
}
return ports
}
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
}
2018-11-21 05:47:46 +00: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 14:23:20 +00:00
}