Allow server to listen on multiple ports

pull/71/head
Andy Wang 5 years ago
parent 935b481ad2
commit 63f3b4a89f

@ -40,7 +40,8 @@ func dispatchConnection(conn net.Conn, sta *server.State) {
data := buf[:i] data := buf[:i]
goWeb := func() { goWeb := func() {
webConn, err := net.Dial("tcp", sta.RedirAddr) _, remotePort, _ := net.SplitHostPort(conn.LocalAddr().String())
webConn, err := net.Dial("tcp", net.JoinHostPort(sta.RedirAddr.String(), remotePort))
if err != nil { if err != nil {
log.Errorf("Making connection to redirection server: %v", err) log.Errorf("Making connection to redirection server: %v", err)
return return
@ -186,19 +187,20 @@ func dispatchConnection(conn net.Conn, sta *server.State) {
} }
func main() { func main() {
// server in ss config, the outbound listening ip // set TLS bind host through commandline for legacy support, default 0.0.0,0
var bindHost string var ssRemoteHost string
// Outbound listening ip, should be 443 // set TLS bind port through commandline for legacy support, default 443
var bindPort string var ssRemotePort string
var config string var config string
if os.Getenv("SS_LOCAL_HOST") != "" { var pluginMode bool
bindHost = os.Getenv("SS_REMOTE_HOST")
bindPort = os.Getenv("SS_REMOTE_PORT") if os.Getenv("SS_LOCAL_HOST") != "" && os.Getenv("SS_LOCAL_PORT") != "" {
pluginMode = true
ssRemoteHost = os.Getenv("SS_REMOTE_HOST")
ssRemotePort = os.Getenv("SS_REMOTE_PORT")
config = os.Getenv("SS_PLUGIN_OPTIONS") config = os.Getenv("SS_PLUGIN_OPTIONS")
} else { } else {
flag.StringVar(&bindHost, "s", "0.0.0.0", "bindHost: ip to bind to, set to 0.0.0.0 to listen to everything")
flag.StringVar(&bindPort, "p", "443", "bindPort: port to bind to, should be 443")
flag.StringVar(&config, "c", "server.json", "config: path to the configuration file or its content") flag.StringVar(&config, "c", "server.json", "config: path to the configuration file or its content")
askVersion := flag.Bool("v", false, "Print the version number") askVersion := flag.Bool("v", false, "Print the version number")
printUsage := flag.Bool("h", false, "Print this message") printUsage := flag.Bool("h", false, "Print this message")
@ -244,31 +246,67 @@ func main() {
} }
log.SetLevel(lvl) log.SetLevel(lvl)
log.Infof("Starting standalone mode, listening on %v:%v", bindHost, bindPort) log.Infof("Starting standalone mode")
} }
sta, _ := server.InitState(bindHost, bindPort, time.Now) sta, _ := server.InitState(time.Now)
err := sta.ParseConfig(config) err := sta.ParseConfig(config)
if err != nil { if err != nil {
log.Fatalf("Configuration file error: %v", err) log.Fatalf("Configuration file error: %v", err)
} }
if !pluginMode && len(sta.BindAddr) == 0 {
log.Fatalf("bind address cannot be empty")
}
// when cloak is started as a shadowsocks plugin // when cloak is started as a shadowsocks plugin
if os.Getenv("SS_LOCAL_HOST") != "" && os.Getenv("SS_LOCAL_PORT") != "" { if pluginMode {
ssLocalHost := os.Getenv("SS_LOCAL_HOST") ssLocalHost := os.Getenv("SS_LOCAL_HOST")
ssLocalPort := os.Getenv("SS_LOCAL_PORT") ssLocalPort := os.Getenv("SS_LOCAL_PORT")
if net.ParseIP(ssLocalHost).To4() == nil {
ssLocalHost = "[" + ssLocalHost + "]" sta.ProxyBook["shadowsocks"], err = net.ResolveTCPAddr("tcp", net.JoinHostPort(ssLocalHost, ssLocalPort))
}
sta.ProxyBook["shadowsocks"], err = net.ResolveTCPAddr("tcp", ssLocalHost+":"+ssLocalPort)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
var ssBind string
// When listening on an IPv6 and IPv4, SS gives REMOTE_HOST as e.g. ::|0.0.0.0
v4nv6 := len(strings.Split(ssRemoteHost, "|")) == 2
if v4nv6 {
ssBind = ":" + ssRemotePort
} else {
ssBind = net.JoinHostPort(ssRemoteHost, ssRemotePort)
}
ssBindAddr, err := net.ResolveTCPAddr("tcp", ssBind)
if err != nil {
log.Fatalf("unable to resolve bind address provided by SS: %v", err)
}
shouldAppend := true
for i, addr := range sta.BindAddr {
if addr.String() == ssBindAddr.String() {
shouldAppend = false
}
if addr.String() == ":"+ssRemotePort { // already listening on all interfaces
shouldAppend = false
}
if addr.String() == "0.0.0.0:"+ssRemotePort || addr.String() == "[::]:"+ssRemotePort {
// if config listens on one ip version but ss wants to listen on both,
// listen on both
if ssBindAddr.String() == ":"+ssRemotePort {
shouldAppend = true
sta.BindAddr[i] = ssBindAddr
}
}
}
if shouldAppend {
sta.BindAddr = append(sta.BindAddr, ssBindAddr)
}
} }
listen := func(addr, port string) { listen := func(bindAddr net.Addr) {
listener, err := net.Listen("tcp", addr+":"+port) listener, err := net.Listen("tcp", bindAddr.String())
log.Infof("Listening on " + addr + ":" + port) log.Infof("Listening on %v", bindAddr)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -282,19 +320,11 @@ func main() {
} }
} }
// When listening on an IPv6 and IPv4, SS gives REMOTE_HOST as e.g. ::|0.0.0.0 for i, addr := range sta.BindAddr {
listeningIP := strings.Split(sta.BindHost, "|") if i != len(sta.BindAddr)-1 {
for i, ip := range listeningIP { go listen(addr)
if net.ParseIP(ip).To4() == nil {
// IPv6 needs square brackets
ip = "[" + ip + "]"
}
// The last listener must block main() because the program exits on main return.
if i == len(listeningIP)-1 {
listen(ip, sta.BindPort)
} else { } else {
go listen(ip, sta.BindPort) listen(addr)
} }
} }

@ -13,6 +13,10 @@
"127.0.0.1:9001" "127.0.0.1:9001"
] ]
}, },
"BindAddr": [
":443",
":80"
],
"BypassUID": [ "BypassUID": [
"1rmq6Ag1jZJCImLBIL5wzQ==" "1rmq6Ag1jZJCImLBIL5wzQ=="
], ],

@ -18,6 +18,7 @@ import (
type rawConfig struct { type rawConfig struct {
ProxyBook map[string][]string ProxyBook map[string][]string
BindAddr []string
BypassUID [][]byte BypassUID [][]byte
RedirAddr string RedirAddr string
PrivateKey string PrivateKey string
@ -29,11 +30,9 @@ type rawConfig struct {
// State type stores the global state of the program // State type stores the global state of the program
type State struct { type State struct {
BindAddr []net.Addr
ProxyBook map[string]net.Addr ProxyBook map[string]net.Addr
BindHost string
BindPort string
Now func() time.Time Now func() time.Time
AdminUID []byte AdminUID []byte
Timeout time.Duration Timeout time.Duration
@ -41,7 +40,7 @@ type State struct {
BypassUID map[[16]byte]struct{} BypassUID map[[16]byte]struct{}
staticPv crypto.PrivateKey staticPv crypto.PrivateKey
RedirAddr string RedirAddr net.Addr
usedRandomM sync.RWMutex usedRandomM sync.RWMutex
usedRandom map[[32]byte]int64 usedRandom map[[32]byte]int64
@ -50,10 +49,8 @@ type State struct {
LocalAPIRouter *gmux.Router LocalAPIRouter *gmux.Router
} }
func InitState(bindHost, bindPort string, nowFunc func() time.Time) (*State, error) { func InitState(nowFunc func() time.Time) (*State, error) {
ret := &State{ ret := &State{
BindHost: bindHost,
BindPort: bindPort,
Now: nowFunc, Now: nowFunc,
BypassUID: make(map[[16]byte]struct{}), BypassUID: make(map[[16]byte]struct{}),
ProxyBook: map[string]net.Addr{}, ProxyBook: map[string]net.Addr{},
@ -92,12 +89,25 @@ func (sta *State) ParseConfig(conf string) (err error) {
sta.LocalAPIRouter = manager.Router sta.LocalAPIRouter = manager.Router
} }
sta.RedirAddr = preParse.RedirAddr
sta.Timeout = time.Duration(preParse.StreamTimeout) * time.Second sta.Timeout = time.Duration(preParse.StreamTimeout) * time.Second
sta.RedirAddr, err = net.ResolveIPAddr("ip", preParse.RedirAddr)
if err != nil {
return fmt.Errorf("unable to resolve RedirAddr: %v", err)
}
for _, addr := range preParse.BindAddr {
bindAddr, err := net.ResolveTCPAddr("tcp", addr)
if err != nil {
return err
}
sta.BindAddr = append(sta.BindAddr, bindAddr)
}
for name, pair := range preParse.ProxyBook { for name, pair := range preParse.ProxyBook {
name = strings.ToLower(name)
if len(pair) != 2 { if len(pair) != 2 {
return fmt.Errorf("invalid protocol and address pair for %v: %v", name, pair) return fmt.Errorf("invalid proxy endpoint and address pair for %v: %v", name, pair)
} }
network := strings.ToLower(pair[0]) network := strings.ToLower(pair[0])
switch network { switch network {

Loading…
Cancel
Save