From 2e5215a35d9145edf72e9e088801699c423bf7e7 Mon Sep 17 00:00:00 2001 From: xtaci Date: Sat, 8 Oct 2022 18:03:33 +0800 Subject: [PATCH] add multiport support on server --- generic/multiport.go | 4 +- .../dial_test.go => generic/multiport_test.go | 17 +++++++- server/main.go | 39 ++++++++++++------- 3 files changed, 43 insertions(+), 17 deletions(-) rename client/dial_test.go => generic/multiport_test.go (67%) diff --git a/generic/multiport.go b/generic/multiport.go index a953f08..a76e4a1 100644 --- a/generic/multiport.go +++ b/generic/multiport.go @@ -18,7 +18,7 @@ func ParseMultiPort(addr string) (*MultiPort, error) { remoteAddrMatcher := regexp.MustCompile(`(.*)\:([0-9]{1,5})-?([0-9]{1,5})?`) matches := remoteAddrMatcher.FindStringSubmatch(addr) - if len(matches) >= 3 { + if len(matches) >= 4 { var minPort, maxPort int minPort, err := strconv.Atoi(matches[2]) if err != nil { @@ -27,7 +27,7 @@ func ParseMultiPort(addr string) (*MultiPort, error) { maxPort = minPort // multiport assignment - if len(matches) >= 4 { + if matches[3] != "" { maxPort, err = strconv.Atoi(matches[3]) if err != nil { return nil, err diff --git a/client/dial_test.go b/generic/multiport_test.go similarity index 67% rename from client/dial_test.go rename to generic/multiport_test.go index 321ad73..12f68a3 100644 --- a/client/dial_test.go +++ b/generic/multiport_test.go @@ -1,4 +1,4 @@ -package main +package generic import ( "fmt" @@ -26,7 +26,7 @@ func TestDial(t *testing.T) { t.Log("minport:", minPort) t.Log("maxport:", maxPort) - remoteAddr := fmt.Sprintf("%v:%v", matches[1], uint64(minPort)+dialCount%uint64(maxPort-minPort+1)) + remoteAddr := fmt.Sprintf("%v:%v", matches[1], uint64(minPort)+1000%uint64(maxPort-minPort+1)) t.Log("RemoteAddr:", remoteAddr) @@ -35,4 +35,17 @@ func TestDial(t *testing.T) { for i := 0; i < len(matches); i++ { t.Log(testcase2, "submatch", i, matches[i]) } + + testcase3 := ":20000-20001" + matches = reg.FindStringSubmatch(testcase3) + for i := 0; i < len(matches); i++ { + t.Log(testcase3, "submatch", i, matches[i]) + } + + testcase4 := ":20000" + matches = reg.FindStringSubmatch(testcase4) + for i := 0; i < len(matches); i++ { + t.Log(testcase4, "submatch", i, matches[i]) + } + } diff --git a/server/main.go b/server/main.go index 1b5979f..1cf4d3b 100644 --- a/server/main.go +++ b/server/main.go @@ -432,22 +432,35 @@ func main() { } } - if config.TCP { // tcp dual stack - if conn, err := tcpraw.Listen("tcp", config.Listen); err == nil { - lis, err := kcp.ServeConn(block, config.DataShard, config.ParityShard, conn) - checkError(err) - wg.Add(1) - go loop(lis) - } else { - log.Println(err) + mp, err := generic.ParseMultiPort(config.Listen) + if err != nil { + log.Println(err) + return err + } + + // create multiple listener + for port := mp.MinPort; port <= mp.MaxPort; port++ { + listenAddr := fmt.Sprintf("%v:%v", mp.Host, port) + if config.TCP { // tcp dual stack + if conn, err := tcpraw.Listen("tcp", listenAddr); err == nil { + log.Printf("Listening on: %v/tcp", listenAddr) + lis, err := kcp.ServeConn(block, config.DataShard, config.ParityShard, conn) + checkError(err) + wg.Add(1) + go loop(lis) + } else { + log.Println(err) + } } + + // udp stack + log.Printf("Listening on: %v/udp", listenAddr) + lis, err := kcp.ListenWithOptions(listenAddr, block, config.DataShard, config.ParityShard) + checkError(err) + wg.Add(1) + go loop(lis) } - // udp stack - lis, err := kcp.ListenWithOptions(config.Listen, block, config.DataShard, config.ParityShard) - checkError(err) - wg.Add(1) - go loop(lis) wg.Wait() return nil }