refactor: standardize logging on zap across all packages

Replace all fmt.Printf/Println and log.Printf/Fatal with structured
zap.Logger calls to eliminate inconsistent logging (anti-pattern #12).

- network/crypt_conn: inject logger via NewCryptConn, replace 6 fmt calls
- signserver/session: use existing s.logger for debug packet dumps
- entranceserver: use s.logger for inbound/outbound debug logging
- api/utils: accept logger param in verifyPath, replace fmt.Println
- api/endpoints: use s.logger for screenshot path diagnostics
- config: replace log.Fatal with error return in getOutboundIP4
- deltacomp: replace log.Printf with zap.L() global logger
This commit is contained in:
Houmgaor
2026-02-20 18:59:12 +01:00
parent e5133e5dcf
commit 06cb3afa57
15 changed files with 134 additions and 117 deletions

View File

@@ -5,9 +5,10 @@ import (
"errors"
"erupe-ce/config"
"erupe-ce/network/crypto"
"fmt"
"io"
"net"
"go.uber.org/zap"
)
// Conn defines the interface for a packet-based connection.
@@ -23,6 +24,7 @@ type Conn interface {
// CryptConn represents a MHF encrypted two-way connection,
// it automatically handles encryption, decryption, and key rotation via it's methods.
type CryptConn struct {
logger *zap.Logger
conn net.Conn
realClientMode _config.Mode
readKeyRot uint32
@@ -33,8 +35,12 @@ type CryptConn struct {
}
// NewCryptConn creates a new CryptConn with proper default values.
func NewCryptConn(conn net.Conn, mode _config.Mode) *CryptConn {
func NewCryptConn(conn net.Conn, mode _config.Mode, logger *zap.Logger) *CryptConn {
if logger == nil {
logger = zap.NewNop()
}
cc := &CryptConn{
logger: logger,
conn: conn,
realClientMode: mode,
readKeyRot: 995117,
@@ -80,18 +86,19 @@ func (cc *CryptConn) ReadPacket() ([]byte, error) {
out, combinedCheck, check0, check1, check2 := crypto.Crypto(encryptedPacketBody, cc.readKeyRot, false, nil)
if cph.Check0 != check0 || cph.Check1 != check1 || cph.Check2 != check2 {
fmt.Printf("got c0 %X, c1 %X, c2 %X\n", check0, check1, check2)
fmt.Printf("want c0 %X, c1 %X, c2 %X\n", cph.Check0, cph.Check1, cph.Check2)
fmt.Printf("headerData:\n%s\n", hex.Dump(headerData))
fmt.Printf("encryptedPacketBody:\n%s\n", hex.Dump(encryptedPacketBody))
cc.logger.Warn("Crypto checksum mismatch",
zap.String("got", hex.EncodeToString([]byte{byte(check0 >> 8), byte(check0), byte(check1 >> 8), byte(check1), byte(check2 >> 8), byte(check2)})),
zap.String("want", hex.EncodeToString([]byte{byte(cph.Check0 >> 8), byte(cph.Check0), byte(cph.Check1 >> 8), byte(cph.Check1), byte(cph.Check2 >> 8), byte(cph.Check2)})),
zap.String("headerData", hex.Dump(headerData)),
zap.String("encryptedPacketBody", hex.Dump(encryptedPacketBody)),
)
// Attempt to bruteforce it.
fmt.Println("Crypto out of sync? Attempting bruteforce")
cc.logger.Warn("Crypto out of sync, attempting bruteforce")
for key := byte(0); key < 255; key++ {
out, combinedCheck, check0, check1, check2 = crypto.Crypto(encryptedPacketBody, 0, false, &key)
//fmt.Printf("Key: 0x%X\n%s\n", key, hex.Dump(out))
if cph.Check0 == check0 && cph.Check1 == check1 && cph.Check2 == check2 {
fmt.Printf("Bruceforce successful, override key: 0x%X\n", key)
cc.logger.Info("Bruteforce successful", zap.Uint8("overrideKey", key))
// Try to fix key for subsequent packets?
//cc.readKeyRot = (uint32(key) << 1) + 999983

View File

@@ -2,9 +2,9 @@ package network
import (
"bytes"
"errors"
_config "erupe-ce/config"
"erupe-ce/network/crypto"
"errors"
"io"
"net"
"testing"
@@ -54,7 +54,7 @@ func (m *mockConn) SetWriteDeadline(t time.Time) error { return nil }
func TestNewCryptConn(t *testing.T) {
mockConn := newMockConn(nil)
cc := NewCryptConn(mockConn, _config.ZZ)
cc := NewCryptConn(mockConn, _config.ZZ, nil)
if cc == nil {
t.Fatal("NewCryptConn() returned nil")
@@ -111,7 +111,7 @@ func TestCryptConn_SendPacket(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockConn := newMockConn(nil)
cc := NewCryptConn(mockConn, _config.ZZ)
cc := NewCryptConn(mockConn, _config.ZZ, nil)
err := cc.SendPacket(tt.data)
if err != nil {
@@ -155,7 +155,7 @@ func TestCryptConn_SendPacket(t *testing.T) {
func TestCryptConn_SendPacket_MultiplePackets(t *testing.T) {
mockConn := newMockConn(nil)
cc := NewCryptConn(mockConn, _config.ZZ)
cc := NewCryptConn(mockConn, _config.ZZ, nil)
// Send first packet
err := cc.SendPacket([]byte{0x01, 0x02})
@@ -190,7 +190,7 @@ func TestCryptConn_SendPacket_MultiplePackets(t *testing.T) {
func TestCryptConn_SendPacket_KeyRotation(t *testing.T) {
mockConn := newMockConn(nil)
cc := NewCryptConn(mockConn, _config.ZZ)
cc := NewCryptConn(mockConn, _config.ZZ, nil)
initialKey := cc.sendKeyRot
@@ -209,7 +209,7 @@ func TestCryptConn_SendPacket_KeyRotation(t *testing.T) {
func TestCryptConn_SendPacket_WriteError(t *testing.T) {
mockConn := newMockConn(nil)
mockConn.writeErr = errors.New("write error")
cc := NewCryptConn(mockConn, _config.ZZ)
cc := NewCryptConn(mockConn, _config.ZZ, nil)
err := cc.SendPacket([]byte{0x01, 0x02, 0x03})
// Note: Current implementation doesn't return write error
@@ -244,7 +244,7 @@ func TestCryptConn_ReadPacket_Success(t *testing.T) {
packet := append(headerBytes, encryptedData...)
mockConn := newMockConn(packet)
cc := NewCryptConn(mockConn, _config.Z1)
cc := NewCryptConn(mockConn, _config.Z1, nil)
// Set the key to match what we used for encryption
cc.readKeyRot = key
@@ -290,7 +290,7 @@ func TestCryptConn_ReadPacket_KeyRotation(t *testing.T) {
packet := append(headerBytes, encryptedData...)
mockConn := newMockConn(packet)
cc := NewCryptConn(mockConn, _config.Z1)
cc := NewCryptConn(mockConn, _config.Z1, nil)
cc.readKeyRot = key
result, err := cc.ReadPacket()
@@ -330,7 +330,7 @@ func TestCryptConn_ReadPacket_NoKeyRotation(t *testing.T) {
packet := append(headerBytes, encryptedData...)
mockConn := newMockConn(packet)
cc := NewCryptConn(mockConn, _config.Z1)
cc := NewCryptConn(mockConn, _config.Z1, nil)
cc.readKeyRot = key
originalKeyRot := cc.readKeyRot
@@ -352,7 +352,7 @@ func TestCryptConn_ReadPacket_NoKeyRotation(t *testing.T) {
func TestCryptConn_ReadPacket_HeaderReadError(t *testing.T) {
mockConn := newMockConn([]byte{0x01, 0x02}) // Only 2 bytes, header needs 14
cc := NewCryptConn(mockConn, _config.ZZ)
cc := NewCryptConn(mockConn, _config.ZZ, nil)
_, err := cc.ReadPacket()
if err == nil {
@@ -368,7 +368,7 @@ func TestCryptConn_ReadPacket_InvalidHeader(t *testing.T) {
// Create invalid header data (wrong endianness or malformed)
invalidHeader := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
mockConn := newMockConn(invalidHeader)
cc := NewCryptConn(mockConn, _config.ZZ)
cc := NewCryptConn(mockConn, _config.ZZ, nil)
_, err := cc.ReadPacket()
if err == nil {
@@ -395,7 +395,7 @@ func TestCryptConn_ReadPacket_BodyReadError(t *testing.T) {
packet := append(headerBytes, incompleteBody...)
mockConn := newMockConn(packet)
cc := NewCryptConn(mockConn, _config.Z1)
cc := NewCryptConn(mockConn, _config.Z1, nil)
_, err := cc.ReadPacket()
if err == nil {
@@ -425,7 +425,7 @@ func TestCryptConn_ReadPacket_ChecksumMismatch(t *testing.T) {
packet := append(headerBytes, encryptedData...)
mockConn := newMockConn(packet)
cc := NewCryptConn(mockConn, _config.Z1)
cc := NewCryptConn(mockConn, _config.Z1, nil)
cc.readKeyRot = key
_, err := cc.ReadPacket()