From b5983ed8c811b01f8d1326ed59dcc5a85aa89462 Mon Sep 17 00:00:00 2001 From: xieyongda Date: Wed, 19 Jun 2024 11:40:19 +0800 Subject: [PATCH 1/4] multi backend server support --- client/config.go | 62 ++++++++++++++++++++++++------------------------ client/dial.go | 4 ++-- client/main.go | 15 +++++++----- 3 files changed, 42 insertions(+), 39 deletions(-) diff --git a/client/config.go b/client/config.go index def0aaf53..26b3ff074 100644 --- a/client/config.go +++ b/client/config.go @@ -7,37 +7,37 @@ import ( // Config for client type Config struct { - LocalAddr string `json:"localaddr"` - RemoteAddr string `json:"remoteaddr"` - Key string `json:"key"` - Crypt string `json:"crypt"` - Mode string `json:"mode"` - Conn int `json:"conn"` - AutoExpire int `json:"autoexpire"` - ScavengeTTL int `json:"scavengettl"` - MTU int `json:"mtu"` - SndWnd int `json:"sndwnd"` - RcvWnd int `json:"rcvwnd"` - DataShard int `json:"datashard"` - ParityShard int `json:"parityshard"` - DSCP int `json:"dscp"` - NoComp bool `json:"nocomp"` - AckNodelay bool `json:"acknodelay"` - NoDelay int `json:"nodelay"` - Interval int `json:"interval"` - Resend int `json:"resend"` - NoCongestion int `json:"nc"` - SockBuf int `json:"sockbuf"` - SmuxVer int `json:"smuxver"` - SmuxBuf int `json:"smuxbuf"` - StreamBuf int `json:"streambuf"` - KeepAlive int `json:"keepalive"` - Log string `json:"log"` - SnmpLog string `json:"snmplog"` - SnmpPeriod int `json:"snmpperiod"` - Quiet bool `json:"quiet"` - TCP bool `json:"tcp"` - Pprof bool `json:"pprof"` + LocalAddr string `json:"localaddr"` + RemoteAddr []string `json:"remoteaddr"` + Key string `json:"key"` + Crypt string `json:"crypt"` + Mode string `json:"mode"` + Conn int `json:"conn"` + AutoExpire int `json:"autoexpire"` + ScavengeTTL int `json:"scavengettl"` + MTU int `json:"mtu"` + SndWnd int `json:"sndwnd"` + RcvWnd int `json:"rcvwnd"` + DataShard int `json:"datashard"` + ParityShard int `json:"parityshard"` + DSCP int `json:"dscp"` + NoComp bool `json:"nocomp"` + AckNodelay bool `json:"acknodelay"` + NoDelay int `json:"nodelay"` + Interval int `json:"interval"` + Resend int `json:"resend"` + NoCongestion int `json:"nc"` + SockBuf int `json:"sockbuf"` + SmuxVer int `json:"smuxver"` + SmuxBuf int `json:"smuxbuf"` + StreamBuf int `json:"streambuf"` + KeepAlive int `json:"keepalive"` + Log string `json:"log"` + SnmpLog string `json:"snmplog"` + SnmpPeriod int `json:"snmpperiod"` + Quiet bool `json:"quiet"` + TCP bool `json:"tcp"` + Pprof bool `json:"pprof"` } func parseJSONConfig(config *Config, path string) error { diff --git a/client/dial.go b/client/dial.go index 089c8b927..7ba4b7b26 100644 --- a/client/dial.go +++ b/client/dial.go @@ -11,8 +11,8 @@ import ( "github.com/xtaci/tcpraw" ) -func dial(config *Config, block kcp.BlockCrypt) (*kcp.UDPSession, error) { - mp, err := generic.ParseMultiPort(config.RemoteAddr) +func dial(config *Config, block kcp.BlockCrypt, idx uint16) (*kcp.UDPSession, error) { + mp, err := generic.ParseMultiPort(config.RemoteAddr[idx]) if err != nil { return nil, err } diff --git a/client/main.go b/client/main.go index 728780a55..b08345c98 100644 --- a/client/main.go +++ b/client/main.go @@ -10,6 +10,7 @@ import ( "net/http" _ "net/http/pprof" "os" + "strings" "time" "golang.org/x/crypto/pbkdf2" @@ -253,7 +254,7 @@ func main() { myApp.Action = func(c *cli.Context) error { config := Config{} config.LocalAddr = c.String("localaddr") - config.RemoteAddr = c.String("remoteaddr") + config.RemoteAddr = strings.Split(c.String("remoteaddr"), "_") // remote address split by "_" config.Key = c.String("key") config.Crypt = c.String("crypt") config.Mode = c.String("mode") @@ -391,8 +392,8 @@ func main() { block, _ = kcp.NewAESBlockCrypt(pass) } - createConn := func() (*smux.Session, error) { - kcpconn, err := dial(&config, block) + createConn := func(idx uint16) (*smux.Session, error) { + kcpconn, err := dial(&config, block, idx) if err != nil { return nil, errors.Wrap(err, "dial()") } @@ -437,9 +438,9 @@ func main() { } // wait until a connection is ready - waitConn := func() *smux.Session { + waitConn := func(idx uint16) *smux.Session { for { - if session, err := createConn(); err == nil { + if session, err := createConn(idx); err == nil { return session } else { log.Println("re-connecting:", err) @@ -464,17 +465,19 @@ func main() { numconn := uint16(config.Conn) muxes := make([]timedSession, numconn) rr := uint16(0) + rlen := (uint16)(len(config.RemoteAddr)) for { p1, err := listener.Accept() if err != nil { log.Fatalf("%+v", err) } idx := rr % numconn + ridx := rr % rlen // do auto expiration && reconnection if muxes[idx].session == nil || muxes[idx].session.IsClosed() || (config.AutoExpire > 0 && time.Now().After(muxes[idx].expiryDate)) { - muxes[idx].session = waitConn() + muxes[idx].session = waitConn(ridx) muxes[idx].expiryDate = time.Now().Add(time.Duration(config.AutoExpire) * time.Second) if config.AutoExpire > 0 { // only when autoexpire set chScavenger <- muxes[idx] From 7196ed809a27839ddf6b50dcad2b87cff95162dd Mon Sep 17 00:00:00 2001 From: xieyongda Date: Wed, 19 Jun 2024 12:19:27 +0800 Subject: [PATCH 2/4] fix out of range --- client/main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/main.go b/client/main.go index b08345c98..8d7f86446 100644 --- a/client/main.go +++ b/client/main.go @@ -462,10 +462,10 @@ func main() { go scavenger(chScavenger, &config) // start listener - numconn := uint16(config.Conn) + rlen := (uint16)(len(config.RemoteAddr)) + numconn := uint16(config.Conn * int(rlen)) muxes := make([]timedSession, numconn) rr := uint16(0) - rlen := (uint16)(len(config.RemoteAddr)) for { p1, err := listener.Accept() if err != nil { From cc93cd4f143a9dd009ce76d8b83a42fbdd2537ba Mon Sep 17 00:00:00 2001 From: xieyongda Date: Wed, 19 Jun 2024 15:08:47 +0800 Subject: [PATCH 3/4] update usage --- client/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/main.go b/client/main.go index 8d7f86446..ab26bb060 100644 --- a/client/main.go +++ b/client/main.go @@ -101,7 +101,7 @@ func main() { cli.StringFlag{ Name: "remoteaddr, r", Value: "vps:29900", - Usage: `kcp server address, eg: "IP:29900" a for single port, "IP:minport-maxport" for port range`, + Usage: `kcp server address, eg: "IP:29900" a for single server/port, "IP1:minport-maxport_IP2:minport-maxport..." for multi server and port range`, }, cli.StringFlag{ Name: "key", From ad133dd60155b9838ceae7f6429c8edeb926d3df Mon Sep 17 00:00:00 2001 From: crazyevent Date: Thu, 4 Jul 2024 10:40:19 +0800 Subject: [PATCH 4/4] Update config.go --- client/config.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/client/config.go b/client/config.go index 26b3ff074..520440ad4 100644 --- a/client/config.go +++ b/client/config.go @@ -38,6 +38,8 @@ type Config struct { Quiet bool `json:"quiet"` TCP bool `json:"tcp"` Pprof bool `json:"pprof"` + QPP bool `json:"qpp"` + QPPCount int `json:"qpp-count"` } func parseJSONConfig(config *Config, path string) error {