mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 22:25:07 +03:00
Protect session nonces with mutexes, modify sent/received bytes atomically
This commit is contained in:
parent
ade684beff
commit
e3eadba4b7
2 changed files with 25 additions and 10 deletions
|
@ -3,6 +3,7 @@ package yggdrasil
|
|||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
||||
|
@ -84,7 +85,7 @@ func (c *Conn) Read(b []byte) (int, error) {
|
|||
b = append(b, bs...)
|
||||
c.session.updateNonce(&p.Nonce)
|
||||
c.session.time = time.Now()
|
||||
c.session.bytesRecvd += uint64(len(bs))
|
||||
atomic.AddUint64(&c.session.bytesRecvd, uint64(len(b)))
|
||||
return len(b), nil
|
||||
case <-c.session.closed:
|
||||
return len(b), errors.New("session was closed")
|
||||
|
@ -106,7 +107,9 @@ func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
|
|||
// code isn't multithreaded so appending to this is safe
|
||||
coords := c.session.coords
|
||||
// Prepare the payload
|
||||
c.session.myNonceMutex.Lock()
|
||||
payload, nonce := crypto.BoxSeal(&c.session.sharedSesKey, b, &c.session.myNonce)
|
||||
c.session.myNonceMutex.Unlock()
|
||||
defer util.PutBytes(payload)
|
||||
p := wire_trafficPacket{
|
||||
Coords: coords,
|
||||
|
@ -115,7 +118,7 @@ func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
|
|||
Payload: payload,
|
||||
}
|
||||
packet := p.encode()
|
||||
c.session.bytesSent += uint64(len(b))
|
||||
atomic.AddUint64(&c.session.bytesSent, uint64(len(b)))
|
||||
select {
|
||||
case c.session.send <- packet:
|
||||
case <-c.session.closed:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue