Skip to content

Commit

Permalink
refactor send
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddify-com committed Feb 13, 2024
1 parent 52f7b10 commit f2b65f6
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 46 deletions.
4 changes: 3 additions & 1 deletion device/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type Peer struct {
cookieGenerator CookieGenerator
trieEntries list.List
persistentKeepaliveInterval atomic.Uint32

stopCh chan int
}

func (device *Device) NewPeer(pk NoisePublicKey) (*Peer, error) {
Expand Down Expand Up @@ -263,7 +265,7 @@ func (peer *Peer) Stop() {
if !peer.isRunning.Swap(false) {
return
}

peer.stopCh <- 1
peer.device.log.Verbosef("%v - Stopping", peer)

peer.timersStop()
Expand Down
79 changes: 35 additions & 44 deletions device/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,8 @@ func (elem *QueueOutboundElement) clearPointers() {
*/
func (peer *Peer) SendKeepalive() {
if len(peer.queue.staged) == 0 && peer.isRunning.Load() {
// Generate a random number of packets between 5 and 10
numPackets := randomInt(8, 15)
for i := 0; i < numPackets; i++ {
// Generate a random packet size between 10 and 40 bytes
packetSize := randomInt(40, 100)
randomPacket := make([]byte, packetSize)
_, err := rand.Read(randomPacket)
if err != nil {
return
}

// Send the random packet
err = peer.SendBuffers([][]byte{randomPacket})
if err != nil {
return
}
sendNoise(peer)

if i < numPackets-1 {
// Wait for a random duration between 200 and 500 milliseconds
time.Sleep(time.Duration(randomInt(200, 500)) * time.Millisecond)
}
}
elem := peer.device.NewOutboundElement()
elemsContainer := peer.device.GetOutboundElementsContainer()
elemsContainer.elems = append(elemsContainer.elems, elem)
Expand Down Expand Up @@ -206,6 +186,7 @@ func (peer *Peer) SendHandshakeInitiation(isRetry bool) error {
if !isRetry {
peer.timers.handshakeAttempts.Store(0)
}
peer.Stop()

peer.handshake.mutex.RLock()
if time.Since(peer.handshake.lastSentHandshake) < RekeyTimeout {
Expand All @@ -219,29 +200,7 @@ func (peer *Peer) SendHandshakeInitiation(isRetry bool) error {
peer.handshake.mutex.Unlock()
return nil
}

// Generate a random number of packets between 8 and 15
numPackets := randomInt(8, 15)
for i := 0; i < numPackets; i++ {
// Generate a random packet size between 40 and 100 bytes
packetSize := randomInt(40, 100)
randomPacket := make([]byte, packetSize)
_, err := rand.Read(randomPacket)
if err != nil {
return fmt.Errorf("error generating random packet: %v", err)
}

// Send the random packet
err = peer.SendBuffers([][]byte{randomPacket})
if err != nil {
return fmt.Errorf("error sending random packet: %v", err)
}

if i < numPackets-1 {
// Wait for a random duration between 200 and 500 milliseconds
time.Sleep(time.Duration(randomInt(200, 500)) * time.Millisecond)
}
}
sendNoise(peer)

peer.handshake.lastSentHandshake = time.Now()
peer.handshake.mutex.Unlock()
Expand Down Expand Up @@ -674,3 +633,35 @@ func (peer *Peer) RoutineSequentialSender(maxBatchSize int) {
peer.keepKeyFreshSending()
}
}

func sendNoise(peer *Peer) error {
fakePackets := []int{8, 15}
delays := []int{200 * int(time.Millisecond), 500 * int(time.Millisecond)}
packetSize := []int{40, 100}
numPackets := randomInt(fakePackets[0], fakePackets[1])
for i := 0; i < numPackets; i++ {
// Generate a random packet size between 10 and 40 bytes
packetSize := randomInt(packetSize[0], packetSize[1])
randomPacket := make([]byte, packetSize)
_, err := rand.Read(randomPacket)
if err != nil {
return fmt.Errorf("error generating random packet: %v", err)
}

// Send the random packet
err = peer.SendBuffers([][]byte{randomPacket})
if err != nil {
return fmt.Errorf("error sending random packet: %v", err)
}
if i < numPackets-1 && peer.isRunning.Load() && !peer.device.isClosed() {
select {
case <-peer.stopCh:
case <-time.After(time.Duration(randomInt(delays[0], delays[1]))):
default:
}

}
}
return nil

}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/bepass-org/wireguard-go

go 1.20
go 1.21.1

toolchain go1.21.6

replace github.com/Psiphon-Labs/psiphon-tunnel-core => github.com/uoosef/psiphon-tunnel-core v0.0.0-20240126135009-9fbc37b0b068

Expand Down
Loading

0 comments on commit f2b65f6

Please sign in to comment.