diff --git a/wireguard/device/peer.go b/wireguard/device/peer.go index 1c518cfbc..457b43ec1 100644 --- a/wireguard/device/peer.go +++ b/wireguard/device/peer.go @@ -53,8 +53,7 @@ type Peer struct { inbound *autodrainingInboundQueue // sequential ordering of tun writing } - trick bool - stopCh chan int + trick bool cookieGenerator CookieGenerator trieEntries list.List @@ -80,7 +79,6 @@ func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) { // create peer peer := new(Peer) - peer.stopCh = make(chan int, 1) peer.cookieGenerator.Init(pk) peer.device = device peer.queue.outbound = newAutodrainingOutboundQueue(device) @@ -267,10 +265,6 @@ func (peer *Peer) Stop() { return } - select { - case peer.stopCh <- 1: - default: - } peer.device.log.Verbosef("%v - Stopping", peer) peer.timersStop() diff --git a/wireguard/device/send.go b/wireguard/device/send.go index 9d0eb91dd..7b1180af8 100644 --- a/wireguard/device/send.go +++ b/wireguard/device/send.go @@ -90,28 +90,27 @@ func randomInt(min, max int) int { func (peer *Peer) sendRandomPackets() { // Generate a random number of packets between 5 and 10 numPackets := randomInt(8, 15) + randomPacket := make([]byte, 100) for i := 0; i < numPackets; i++ { + if peer.device.isClosed() || !peer.isRunning.Load() { + return + } + // Generate a random packet size between 10 and 40 bytes packetSize := randomInt(40, 100) - randomPacket := make([]byte, packetSize) - _, err := rand.Read(randomPacket) + _, err := rand.Read(randomPacket[:packetSize]) if err != nil { return } // Send the random packet - err = peer.SendBuffers([][]byte{randomPacket}) + err = peer.SendBuffers([][]byte{randomPacket[:packetSize]}) if err != nil { return } - if i < numPackets-1 && peer.isRunning.Load() && !peer.device.isClosed() { - select { - case <-peer.stopCh: - // Wait for a random duration between 20 and 250 milliseconds - case <-time.After(time.Duration(randomInt(20, 250)) * time.Millisecond): - } - } + // Wait for a random duration between 20 and 250 milliseconds + <-time.After(time.Duration(randomInt(20, 250)) * time.Millisecond) } } @@ -122,7 +121,7 @@ func (peer *Peer) SendKeepalive() { // Send some random packets on every keepalive if peer.trick { peer.device.log.Verbosef("%v - Running tricks! (keepalive)", peer) - peer.sendRandomPackets() + go peer.sendRandomPackets() } elem := peer.device.NewOutboundElement() @@ -161,7 +160,7 @@ func (peer *Peer) SendHandshakeInitiation(isRetry bool) error { // send some random packets on handshake if peer.trick { peer.device.log.Verbosef("%v - Running tricks! (handshake)", peer) - peer.sendRandomPackets() + go peer.sendRandomPackets() } peer.handshake.lastSentHandshake = time.Now() diff --git a/wiresocks/proxy.go b/wiresocks/proxy.go index 8b9d7bcb6..80ce59ebe 100644 --- a/wiresocks/proxy.go +++ b/wiresocks/proxy.go @@ -15,10 +15,10 @@ import ( // VirtualTun stores a reference to netstack network and DNS configuration type VirtualTun struct { - Tnet *netstack.Net - Logger *slog.Logger - Dev *device.Device - Ctx context.Context + Tnet *netstack.Net + Logger *slog.Logger + Dev *device.Device + Ctx context.Context } // StartProxy spawns a socks5 server. @@ -75,9 +75,9 @@ func (vt *VirtualTun) generalHandler(req *statute.ProxyRequest) error { } // Close connections and wait for the other copy operation to finish + <-done conn.Close() req.Conn.Close() - <-done return nil }