Skip to content

Commit d710683

Browse files
authored
Fix PingRecord race condition (#149)
1 parent 3098c39 commit d710683

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

routine.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"path"
2222
"strconv"
2323
"strings"
24+
"sync"
2425
"time"
2526

2627
"github.com/sourcegraph/conc"
@@ -48,7 +49,8 @@ type VirtualTun struct {
4849
SystemDNS bool
4950
Conf *DeviceConfig
5051
// PingRecord stores the last time an IP was pinged
51-
PingRecord map[string]uint64
52+
PingRecord map[string]uint64
53+
PingRecordLock *sync.Mutex
5254
}
5355

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

480+
d.PingRecordLock.Lock()
478481
d.PingRecord[addr.String()] = uint64(time.Now().Unix())
482+
d.PingRecordLock.Unlock()
479483

480484
defer socket.Close()
481485
}()

wireguard.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package wireproxy
33
import (
44
"bytes"
55
"fmt"
6+
"sync"
67

78
"net/netip"
89

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

8384
return &VirtualTun{
84-
Tnet: tnet,
85-
Dev: dev,
86-
Conf: conf,
87-
SystemDNS: len(setting.DNS) == 0,
88-
PingRecord: make(map[string]uint64),
85+
Tnet: tnet,
86+
Dev: dev,
87+
Conf: conf,
88+
SystemDNS: len(setting.DNS) == 0,
89+
PingRecord: make(map[string]uint64),
90+
PingRecordLock: new(sync.Mutex),
8991
}, nil
9092
}

0 commit comments

Comments
 (0)