Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi backend server support #955

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,39 @@ 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"`
QPP bool `json:"qpp"`
QPPCount int `json:"qpp-count"`
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"`
QPP bool `json:"qpp"`
QPPCount int `json:"qpp-count"`
}

func parseJSONConfig(config *Config, path string) error {
Expand Down
4 changes: 2 additions & 2 deletions client/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import (
)

// dial connects to the remote address
func dial(config *Config, block kcp.BlockCrypt) (*kcp.UDPSession, error) {
mp, err := std.ParseMultiPort(config.RemoteAddr)
func dial(config *Config, block kcp.BlockCrypt, idx uint16) (*kcp.UDPSession, error) {
mp, err := std.ParseMultiPort(config.RemoteAddr[idx])
if err != nil {
return nil, err
}
Expand Down
19 changes: 11 additions & 8 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"strings"
"time"

"golang.org/x/crypto/pbkdf2"
Expand Down Expand Up @@ -76,7 +77,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",
Expand Down Expand Up @@ -238,7 +239,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")
Expand Down Expand Up @@ -403,8 +404,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()")
}
Expand Down Expand Up @@ -449,9 +450,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)
Expand All @@ -475,7 +476,8 @@ func main() {
}

// start listener
numconn := uint16(config.Conn)
rlen := (uint16)(len(config.RemoteAddr))
numconn := uint16(config.Conn * int(rlen))
muxes := make([]timedSession, numconn)
rr := uint16(0)

Expand All @@ -491,11 +493,12 @@ func main() {
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]
Expand Down