Skip to content

Commit

Permalink
multi backend server support
Browse files Browse the repository at this point in the history
  • Loading branch information
xieyongda committed Jun 19, 2024
1 parent f9a4242 commit b5983ed
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
62 changes: 31 additions & 31 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions client/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 9 additions & 6 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
_ "net/http/pprof"
"os"
"strings"
"time"

"golang.org/x/crypto/pbkdf2"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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()")
}
Expand Down Expand Up @@ -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)
Expand All @@ -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]
Expand Down

0 comments on commit b5983ed

Please sign in to comment.