Skip to content

Commit

Permalink
Make keepalive optional on client -> server and server -> proxy conne…
Browse files Browse the repository at this point in the history
…ctions. Use KeepAlive value in config (seconds).
  • Loading branch information
notsure2 committed Apr 4, 2020
1 parent 2327420 commit 7c315bf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd/ck-client/ck-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
var version string

func makeSession(sta *client.State, isAdmin bool) *mux.Session {
log.Info("Attemtping to start a new session")
log.Info("Attempting to start a new session")
if !isAdmin {
// sessionID is usergenerated. There shouldn't be a security concern because the scope of
// sessionID is limited to its UID.
Expand All @@ -32,7 +32,7 @@ func makeSession(sta *client.State, isAdmin bool) *mux.Session {
atomic.StoreUint32(&sta.SessionID, binary.BigEndian.Uint32(quad))
}

d := net.Dialer{Control: protector}
d := net.Dialer{Control: protector, KeepAlive: sta.KeepAlive}
connsCh := make(chan net.Conn, sta.NumConn)
var _sessionKey atomic.Value
var wg sync.WaitGroup
Expand Down
3 changes: 2 additions & 1 deletion cmd/ck-server/ck-server.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ func dispatchConnection(conn net.Conn, sta *server.State) {
}
}
proxyAddr := sta.ProxyBook[ci.ProxyMethod]
localConn, err := net.Dial(proxyAddr.Network(), proxyAddr.String())
d := net.Dialer{KeepAlive: sta.KeepAlive}
localConn, err := d.Dial(proxyAddr.Network(), proxyAddr.String())
if err != nil {
log.Errorf("Failed to connect to %v: %v", ci.ProxyMethod, err)
user.CloseSession(ci.SessionId, "Failed to connect to proxy server")
Expand Down
7 changes: 7 additions & 0 deletions internal/client/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type rawConfig struct {
Transport string
NumConn int
StreamTimeout int
KeepAlive int
RemoteHost string
RemotePort int
}
Expand All @@ -50,6 +51,7 @@ type State struct {
ServerName string
NumConn int
Timeout time.Duration
KeepAlive time.Duration
}

// semi-colon separated value. This is for Android plugin options
Expand Down Expand Up @@ -138,6 +140,11 @@ func (sta *State) ParseConfig(conf string) (err error) {
} else {
sta.Timeout = time.Duration(preParse.StreamTimeout) * time.Second
}
if preParse.KeepAlive <= 0 {
sta.KeepAlive = -1
} else {
sta.KeepAlive = time.Duration(preParse.KeepAlive) * time.Second
}
sta.UID = preParse.UID

pub, ok := ecdh.Unmarshal(preParse.PublicKey)
Expand Down
14 changes: 11 additions & 3 deletions internal/server/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type rawConfig struct {
AdminUID []byte
DatabasePath string
StreamTimeout int
KeepAlive int
CncMode bool
}

Expand All @@ -32,9 +33,10 @@ type State struct {
BindAddr []net.Addr
ProxyBook map[string]net.Addr

Now func() time.Time
AdminUID []byte
Timeout time.Duration
Now func() time.Time
AdminUID []byte
Timeout time.Duration
KeepAlive time.Duration

BypassUID map[[16]byte]struct{}
staticPv crypto.PrivateKey
Expand Down Expand Up @@ -173,6 +175,12 @@ func (sta *State) ParseConfig(conf string) (err error) {
sta.Timeout = time.Duration(preParse.StreamTimeout) * time.Second
}

if preParse.KeepAlive <= 0 {
sta.KeepAlive = -1
} else {
sta.KeepAlive = time.Duration(preParse.KeepAlive) * time.Second
}

sta.RedirHost, sta.RedirPort, err = parseRedirAddr(preParse.RedirAddr)
if err != nil {
return fmt.Errorf("unable to parse RedirAddr: %v", err)
Expand Down

0 comments on commit 7c315bf

Please sign in to comment.