Skip to content

Commit

Permalink
wireguard: tweaks
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Pashmfouroush <[email protected]>
  • Loading branch information
markpash committed Apr 11, 2024
1 parent 977642d commit 2b12585
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
8 changes: 1 addition & 7 deletions wireguard/device/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -267,10 +265,6 @@ func (peer *Peer) Stop() {
return
}

select {
case peer.stopCh <- 1:
default:
}
peer.device.log.Verbosef("%v - Stopping", peer)

peer.timersStop()
Expand Down
23 changes: 11 additions & 12 deletions wireguard/device/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions wiresocks/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 2b12585

Please sign in to comment.