Skip to content

Commit

Permalink
Merge pull request #10 from Snawoot/session_hard_time_limit
Browse files Browse the repository at this point in the history
Session hard time limit
  • Loading branch information
Snawoot authored Oct 1, 2023
2 parents fbd11a2 + 94dc952 commit 57285a2
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Options:
-identity string
client identity sent to server
-idle-time duration
max idle time for UDP session (default 1m30s)
max idle time for UDP session (default 30s)
-key-length uint
generate key with specified length (default 16)
-mtu int
Expand All @@ -91,6 +91,8 @@ Options:
(server only) skip hello verify request. Useful to workaround DPI
-stale-mode value
which stale side of connection makes whole session stale (both, either, left, right) (default either)
-time-limit duration
hard time limit for each session
-timeout duration
network operation timeout (default 10s)
```
Expand Down
13 changes: 11 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Client struct {
cancelCtx func()
staleMode util.StaleMode
workerWG sync.WaitGroup
timeLimit time.Duration
}

func New(cfg *Config) (*Client, error) {
Expand All @@ -45,6 +46,7 @@ func New(cfg *Config) (*Client, error) {
baseCtx: baseCtx,
cancelCtx: cancelCtx,
staleMode: cfg.StaleMode,
timeLimit: cfg.TimeLimit,
}

lAddrPort, err := netip.ParseAddrPort(cfg.BindAddress)
Expand Down Expand Up @@ -101,7 +103,14 @@ func (client *Client) serve(conn net.Conn) {
defer log.Printf("[-] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
defer conn.Close()

dialCtx, cancel := context.WithTimeout(client.baseCtx, client.timeout)
ctx := client.baseCtx
if client.timeLimit != 0 {
newCtx, cancel := context.WithTimeout(ctx, client.timeLimit)
defer cancel()
ctx = newCtx
}

dialCtx, cancel := context.WithTimeout(ctx, client.timeout)
defer cancel()
remoteConn, err := (&net.Dialer{}).DialContext(dialCtx, "udp", client.rAddr)
if err != nil {
Expand All @@ -116,7 +125,7 @@ func (client *Client) serve(conn net.Conn) {
return
}

util.PairConn(client.baseCtx, conn, remoteConn, client.idleTimeout, client.staleMode)
util.PairConn(ctx, conn, remoteConn, client.idleTimeout, client.staleMode)
}

func (client *Client) contextMaker() (context.Context, func()) {
Expand Down
1 change: 1 addition & 0 deletions client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
CipherSuites ciphers.CipherList
EllipticCurves ciphers.CurveList
StaleMode util.StaleMode
TimeLimit time.Duration
}

func (cfg *Config) populateDefaults() *Config {
Expand Down
3 changes: 3 additions & 0 deletions cmd/dtlspipe/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ var (
ciphersuites = cipherlistArg{}
curves = curvelistArg{}
staleMode = util.EitherStale
timeLimit = flag.Duration("time-limit", 0, "hard time limit for each session")
)

func init() {
Expand Down Expand Up @@ -139,6 +140,7 @@ func cmdClient(bindAddress, remoteAddress string) int {
CipherSuites: ciphersuites.Value,
EllipticCurves: curves.Value,
StaleMode: staleMode,
TimeLimit: *timeLimit,
}

clt, err := client.New(&cfg)
Expand Down Expand Up @@ -176,6 +178,7 @@ func cmdServer(bindAddress, remoteAddress string) int {
CipherSuites: ciphersuites.Value,
EllipticCurves: curves.Value,
StaleMode: staleMode,
TimeLimit: *timeLimit,
}

srv, err := server.New(&cfg)
Expand Down
1 change: 1 addition & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct {
CipherSuites ciphers.CipherList
EllipticCurves ciphers.CurveList
StaleMode util.StaleMode
TimeLimit time.Duration
}

func (cfg *Config) populateDefaults() *Config {
Expand Down
13 changes: 11 additions & 2 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Server struct {
cancelCtx func()
staleMode util.StaleMode
workerWG sync.WaitGroup
timeLimit time.Duration
}

func New(cfg *Config) (*Server, error) {
Expand All @@ -46,6 +47,7 @@ func New(cfg *Config) (*Server, error) {
baseCtx: baseCtx,
cancelCtx: cancelCtx,
staleMode: cfg.StaleMode,
timeLimit: cfg.TimeLimit,
}

lAddrPort, err := netip.ParseAddrPort(cfg.BindAddress)
Expand Down Expand Up @@ -119,7 +121,14 @@ func (srv *Server) serve(conn net.Conn) {
defer log.Printf("[-] conn %s <=> %s", conn.LocalAddr(), conn.RemoteAddr())
defer conn.Close()

dialCtx, cancel := context.WithTimeout(srv.baseCtx, srv.timeout)
ctx := srv.baseCtx
if srv.timeLimit != 0 {
newCtx, cancel := context.WithTimeout(ctx, srv.timeLimit)
defer cancel()
ctx = newCtx
}

dialCtx, cancel := context.WithTimeout(ctx, srv.timeout)
defer cancel()
remoteConn, err := (&net.Dialer{}).DialContext(dialCtx, "udp", srv.rAddr)
if err != nil {
Expand All @@ -128,7 +137,7 @@ func (srv *Server) serve(conn net.Conn) {
}
defer remoteConn.Close()

util.PairConn(srv.baseCtx, conn, remoteConn, srv.idleTimeout, srv.staleMode)
util.PairConn(ctx, conn, remoteConn, srv.idleTimeout, srv.staleMode)
}

func (srv *Server) contextMaker() (context.Context, func()) {
Expand Down

0 comments on commit 57285a2

Please sign in to comment.