gopenfusion/server/client.go

105 lines
2.5 KiB
Go
Raw Normal View History

2023-03-05 08:14:36 +00:00
package main
import (
"encoding/binary"
"fmt"
"log"
"net"
2023-03-07 00:39:41 +00:00
"github.com/CPunch/GopenFusion/protocol"
2023-03-05 08:14:36 +00:00
)
type Client struct {
server *Server
conn net.Conn
key []byte
}
func newClient(server *Server, conn net.Conn, key []byte) *Client {
return &Client{
server: server,
conn: conn,
key: key,
}
}
func (client *Client) Send(data interface{}, typeID uint32) {
// encode
2023-03-07 00:39:41 +00:00
pkt := protocol.NewPacket(make([]byte, 0))
2023-03-05 08:14:36 +00:00
pkt.Encode(data)
2023-03-07 00:39:41 +00:00
log.Printf("Sending %#v, sizeof: %d", data, len(pkt.Buf))
2023-03-05 08:14:36 +00:00
// write packet size
2023-03-07 00:39:41 +00:00
tmp := make([]byte, 4)
2023-03-05 08:14:36 +00:00
binary.LittleEndian.PutUint32(tmp, uint32(len(pkt.Buf)+4))
if _, err := client.conn.Write(tmp); err != nil {
panic(fmt.Errorf("[FATAL] failed to write packet size! %v", err))
}
// prepend the typeID to the packet body
binary.LittleEndian.PutUint32(tmp, uint32(typeID))
2023-03-07 00:39:41 +00:00
tmp = append(tmp, pkt.Buf...)
2023-03-05 08:14:36 +00:00
2023-03-07 00:39:41 +00:00
// encrypt typeID & body
protocol.EncryptData(tmp, client.key)
2023-03-05 08:14:36 +00:00
// write packet body
2023-03-07 00:39:41 +00:00
if _, err := client.conn.Write(tmp); err != nil {
2023-03-05 08:14:36 +00:00
panic(fmt.Errorf("[FATAL] failed to write packet body! %v", err))
}
log.Printf("sent!")
}
func (client *Client) ClientHandler() {
defer func() {
2023-03-05 08:32:11 +00:00
if err := recover(); err != nil {
log.Printf("Client %p panic'd! %v", client, err)
}
2023-03-05 08:14:36 +00:00
client.conn.Close()
client.server.unregister <- client
}()
2023-03-07 00:39:41 +00:00
tmp := make([]byte, 4, protocol.CN_PACKET_BUFFER_SIZE)
2023-03-05 08:14:36 +00:00
for {
// read packet size
if _, err := client.conn.Read(tmp); err != nil {
panic(fmt.Errorf("[FATAL] failed to read packet size! %v", err))
}
sz := int(binary.LittleEndian.Uint32(tmp))
2023-03-07 00:39:41 +00:00
// client should never send a packet size outside of this range
if sz > protocol.CN_PACKET_BUFFER_SIZE || sz < 4 {
panic(fmt.Errorf("[FATAL] malicious packet size received! %d", sz))
}
2023-03-05 08:14:36 +00:00
// read packet body
2023-03-07 00:39:41 +00:00
if _, err := client.conn.Read(tmp[:sz]); err != nil {
2023-03-05 08:14:36 +00:00
panic(fmt.Errorf("[FATAL] failed to read packet body! %v", err))
}
// decrypt && grab typeID
2023-03-07 00:39:41 +00:00
protocol.DecryptData(tmp[:sz], client.key)
typeID := int(binary.LittleEndian.Uint32(tmp[:4]))
2023-03-05 08:14:36 +00:00
2023-03-07 00:39:41 +00:00
log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz)
pkt := protocol.NewPacket(tmp[4:sz])
2023-03-05 08:14:36 +00:00
switch typeID {
case protocol.P_CL2LS_REQ_LOGIN:
var loginPkt protocol.SP_CL2LS_REQ_LOGIN
pkt.Decode(&loginPkt)
log.Printf("Got packet: %#v", loginPkt)
2023-03-07 00:39:41 +00:00
client.Send(&protocol.SP_LS2CL_REP_LOGIN_FAIL{
IErrorCode: protocol.LOGIN_FAIL_EULA_ERROR,
SZID: loginPkt.SZID,
2023-03-07 00:39:41 +00:00
}, protocol.P_LS2CL_REP_LOGIN_FAIL)
2023-03-05 08:14:36 +00:00
default:
log.Printf("[WARN] unsupported packet ID: %x\n", typeID)
}
2023-03-07 00:39:41 +00:00
// reset tmp
tmp = tmp[:4]
2023-03-05 08:14:36 +00:00
}
}