Skip to content
Snippets Groups Projects
Commit e5f92d28 authored by cyberta's avatar cyberta Committed by cyberta
Browse files

implement cancellableSleep, so that obfsvpn clients in hopping mode stop...

implement cancellableSleep, so that obfsvpn clients in hopping mode stop quicker after Stop() was called. This is especially useful in case obfsvpn is integrated as a library to ensure correct state handling in fast reconnection scenarios e.g. after settings changes
parent c83446d5
Branches
Tags
1 merge request!79state handling improvements
Pipeline #272359 passed with warnings
......@@ -302,25 +302,38 @@ func (c *Client) connectObfs4(obfs4Endpoint *Obfs4Config, sleepSeconds int) {
// If we wait sleepSeconds here to clean up the previous connection, we can guarantee that the
// connection list will not grow unbounded
go func() {
time.Sleep(time.Duration(sleepSeconds) * time.Second)
cancelled := cancellableSleep(c.ctx, time.Duration(sleepSeconds)*time.Second)
if cancelled {
c.log("Sleep for connection cleanup cancelled. Immediately cleaning up old connections")
}
c.cleanupOldConn()
}()
}
}
// cancellableSleep simulates a sleep that can be canceled by a context.
func cancellableSleep(ctx context.Context, duration time.Duration) bool {
timer := time.NewTimer(duration)
defer timer.Stop()
select {
case <-timer.C:
return false // Sleep completed
case <-ctx.Done():
return true // Sleep canceled
}
}
func (c *Client) hop() {
for {
select {
case <-c.ctx.Done():
return
default:
}
// #nosec G404
sleepSeconds := rand.Intn(int(c.hopJitter)) + int(c.minHopSeconds)
c.log("Sleeping %d seconds...", sleepSeconds)
time.Sleep(time.Duration(sleepSeconds) * time.Second)
cancelled := cancellableSleep(c.ctx, time.Duration(sleepSeconds)*time.Second)
if cancelled {
c.log("Hop sleep cancelled")
return
}
obfs4Endpoint := c.pickRandomEndpoint()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment