Skip to content

Commit

Permalink
handler.go: Actually check for DisableCookies field.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandertv committed May 18, 2024
1 parent 9a29b7c commit 563a53f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (conn *Conn) startTicking() {
if before != 0 && acksLeft == 0 {
conn.closeImmediately()
}
since := time.Since(time.Unix(unix, 0))
since := t.Sub(time.Unix(unix, 0))
if (acksLeft == 0 && since > time.Second) || since > time.Second*5 {
conn.closeImmediately()
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func (conn *Conn) Latency() time.Duration {
return time.Duration(conn.rtt.Load() / 2)
}

// send encodes an encoding.BinaryMarshaler and sends it.
// send encodes an encoding.BinaryMarshaler and writes it to the Conn.
func (conn *Conn) send(pk encoding.BinaryMarshaler) error {
b, _ := pk.MarshalBinary()
_, err := conn.Write(b)
Expand Down
7 changes: 5 additions & 2 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func (h listenerConnectionHandler) close(conn *Conn) {
// cookie calculates a cookie for the net.Addr passed. It is calculated as a
// hash of the random cookie salt and the address.
func (h listenerConnectionHandler) cookie(addr net.Addr) uint32 {
if h.l.conf.DisableCookies {
return 0
}
udp, _ := addr.(*net.UDPAddr)
b := make([]byte, 6, 22)
binary.LittleEndian.PutUint32(b, h.cookieSalt)
Expand Down Expand Up @@ -93,15 +96,15 @@ func (h listenerConnectionHandler) handleOpenConnectionRequest1(b []byte, addr n
return fmt.Errorf("handle OPEN_CONNECTION_REQUEST_1: incompatible protocol version %v (listener protocol = %v)", pk.ClientProtocol, protocolVersion)
}

data, _ := (&message.OpenConnectionReply1{ServerGUID: h.l.id, Cookie: h.cookie(addr), ServerHasSecurity: true, MTU: mtuSize}).MarshalBinary()
data, _ := (&message.OpenConnectionReply1{ServerGUID: h.l.id, Cookie: h.cookie(addr), ServerHasSecurity: !h.l.conf.DisableCookies, MTU: mtuSize}).MarshalBinary()
_, err := h.l.conn.WriteTo(data, addr)
return err
}

// handleOpenConnectionRequest2 handles an open connection request 2 packet
// stored in buffer b, coming from an address.
func (h listenerConnectionHandler) handleOpenConnectionRequest2(b []byte, addr net.Addr) error {
pk := &message.OpenConnectionRequest2{ServerHasSecurity: true}
pk := &message.OpenConnectionRequest2{ServerHasSecurity: !h.l.conf.DisableCookies}
if err := pk.UnmarshalBinary(b); err != nil {
return fmt.Errorf("read OPEN_CONNECTION_REQUEST_2: %w", err)
}
Expand Down

0 comments on commit 563a53f

Please sign in to comment.