mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-22 23:54:33 +01:00
golangci-lint's errcheck rule requires explicit handling of error return values from Close, Write, and Logout calls. Use blank identifier assignment for cleanup paths where errors are intentionally discarded.
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package conn
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
)
|
|
|
|
// MHFConn wraps a CryptConn and provides convenience methods for MHF connections.
|
|
type MHFConn struct {
|
|
*CryptConn
|
|
RawConn net.Conn
|
|
}
|
|
|
|
// DialWithInit connects to addr and sends the 8 NULL byte initialization
|
|
// required by sign and entrance servers.
|
|
func DialWithInit(addr string) (*MHFConn, error) {
|
|
conn, err := net.Dial("tcp", addr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("dial %s: %w", addr, err)
|
|
}
|
|
|
|
// Sign and entrance servers expect 8 NULL bytes to initialize the connection.
|
|
_, err = conn.Write(make([]byte, 8))
|
|
if err != nil {
|
|
_ = conn.Close()
|
|
return nil, fmt.Errorf("write init bytes to %s: %w", addr, err)
|
|
}
|
|
|
|
return &MHFConn{
|
|
CryptConn: NewCryptConn(conn),
|
|
RawConn: conn,
|
|
}, nil
|
|
}
|
|
|
|
// DialDirect connects to addr without sending initialization bytes.
|
|
// Used for channel server connections.
|
|
func DialDirect(addr string) (*MHFConn, error) {
|
|
conn, err := net.Dial("tcp", addr)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("dial %s: %w", addr, err)
|
|
}
|
|
|
|
return &MHFConn{
|
|
CryptConn: NewCryptConn(conn),
|
|
RawConn: conn,
|
|
}, nil
|
|
}
|
|
|
|
// Close closes the underlying connection.
|
|
func (c *MHFConn) Close() error {
|
|
return c.RawConn.Close()
|
|
}
|