Fix channel join flooding and enhance TUI features

- Implemented separate PacketID counters for Ping, Pong, and Ack (protocol compliance).
- Encrypted outgoing Pong packets after handshake.
- Fixed 'clientmove' command by omitting empty 'cpw' parameter.
- Added fullscreen log view toggle ('f' key).
- Improved logging with multi-writer and timestamps.
- Updated .gitignore to exclude binaries and logs.
This commit is contained in:
Jose Luis Montañes Ojados
2026-01-16 16:02:17 +01:00
parent c0b1217536
commit 184fff202f
15 changed files with 239 additions and 39 deletions

View File

@@ -1,6 +1,8 @@
package client
import (
"encoding/binary"
"fmt"
"log"
"sync"
"time"
@@ -29,8 +31,11 @@ type Client struct {
ClientID uint16
// Counters
PacketIDCounterC2S uint16
VoicePacketID uint16
PacketIDCounterC2S uint16 // Commands (Type 0x02)
VoicePacketID uint16 // Voice (Type 0x00)
PingPacketID uint16 // Type 0x04
PongPacketID uint16 // Type 0x05
AckPacketID uint16 // Type 0x06
// State
Connected bool
@@ -136,25 +141,52 @@ func (c *Client) Connect(address string) error {
if !c.Connected {
continue // Don't send pings if not connected yet
}
ping := protocol.NewPacket(protocol.PacketTypePing, nil)
c.PacketIDCounterC2S++
ping.Header.PacketID = c.PacketIDCounterC2S
ping.Header.ClientID = c.ClientID
// Must NOT have NewProtocol (0x20) flag for Pings/Pongs
ping.Header.Type = uint8(protocol.PacketTypePing) | protocol.PacketFlagUnencrypted
// Use SharedMac if available, otherwise zeros (as per ts3j InitPacketTransformation)
if c.Handshake != nil && len(c.Handshake.SharedMac) > 0 {
copy(ping.Header.MAC[:], c.Handshake.SharedMac)
} else {
// Initialize Header.MAC with zeros
for i := 0; i < 8; i++ {
ping.Header.MAC[i] = 0
}
// Send KeepAlive Ping (Encrypted, No NewProtocol)
if err := c.sendPing(); err != nil {
log.Printf("Error sending Ping: %v", err)
}
log.Printf("Sending KeepAlive Ping (PID=%d)", ping.Header.PacketID)
c.Conn.SendPacket(ping)
}
}
}
// sendPing sends an encrypted Ping packet WITHOUT the NewProtocol flag
func (c *Client) sendPing() error {
pType := protocol.PacketTypePing
pkt := protocol.NewPacket(pType, nil)
c.PingPacketID++
pkt.Header.PacketID = c.PingPacketID
pkt.Header.ClientID = c.ClientID
// Note: We do NOT set PacketFlagNewProtocol for Pings
// Encryption
key := protocol.HandshakeKey[:]
nonce := protocol.HandshakeNonce[:]
if c.Handshake != nil && c.Handshake.Step >= 6 && len(c.Handshake.SharedIV) > 0 {
crypto := &protocol.CryptoState{
SharedIV: c.Handshake.SharedIV,
SharedMac: c.Handshake.SharedMac,
GenerationID: 0,
}
keyArr, nonceArr := crypto.GenerateKeyNonce(&pkt.Header, true) // Client->Server=true
key = keyArr
nonce = nonceArr
}
// Meta for Client->Server: PID(2) + CID(2) + PT(1) = 5 bytes
meta := make([]byte, 5)
binary.BigEndian.PutUint16(meta[0:2], pkt.Header.PacketID)
binary.BigEndian.PutUint16(meta[2:4], pkt.Header.ClientID)
meta[4] = pkt.Header.Type
encData, mac, err := protocol.EncryptEAX(key, nonce, meta, pkt.Data)
if err != nil {
return fmt.Errorf("encryption failed: %w", err)
}
pkt.Data = encData
copy(pkt.Header.MAC[:], mac)
log.Printf("Sending proper Encrypted Ping (PID=%d)", pkt.Header.PacketID)
return c.Conn.SendPacket(pkt)
}