Skip to content

Commit

Permalink
Fix PingRecord race condition (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrntknr authored Dec 26, 2024
1 parent 3098c39 commit d710683
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 5 additions & 1 deletion routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"path"
"strconv"
"strings"
"sync"
"time"

"github.com/sourcegraph/conc"
Expand Down Expand Up @@ -48,7 +49,8 @@ type VirtualTun struct {
SystemDNS bool
Conf *DeviceConfig
// PingRecord stores the last time an IP was pinged
PingRecord map[string]uint64
PingRecord map[string]uint64
PingRecordLock *sync.Mutex
}

// RoutineSpawner spawns a routine (e.g. socks5, tcp static routes) after the configuration is parsed
Expand Down Expand Up @@ -475,7 +477,9 @@ func (d VirtualTun) pingIPs() {
}
}

d.PingRecordLock.Lock()
d.PingRecord[addr.String()] = uint64(time.Now().Unix())
d.PingRecordLock.Unlock()

defer socket.Close()
}()
Expand Down
12 changes: 7 additions & 5 deletions wireguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package wireproxy
import (
"bytes"
"fmt"
"sync"

"net/netip"

Expand Down Expand Up @@ -81,10 +82,11 @@ func StartWireguard(conf *DeviceConfig, logLevel int) (*VirtualTun, error) {
}

return &VirtualTun{
Tnet: tnet,
Dev: dev,
Conf: conf,
SystemDNS: len(setting.DNS) == 0,
PingRecord: make(map[string]uint64),
Tnet: tnet,
Dev: dev,
Conf: conf,
SystemDNS: len(setting.DNS) == 0,
PingRecord: make(map[string]uint64),
PingRecordLock: new(sync.Mutex),
}, nil
}

0 comments on commit d710683

Please sign in to comment.