2023-03-07 23:04:57 +00:00
|
|
|
package protocol
|
2023-03-05 08:14:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
2023-03-07 07:20:36 +00:00
|
|
|
"time"
|
2023-03-05 08:14:36 +00:00
|
|
|
)
|
|
|
|
|
2023-03-07 07:20:36 +00:00
|
|
|
const (
|
|
|
|
USE_E = iota
|
|
|
|
USE_FE
|
|
|
|
)
|
|
|
|
|
2023-03-07 23:04:57 +00:00
|
|
|
type ClientHandler interface {
|
|
|
|
HandlePacket(client *Client, typeID uint32, pkt *Packet)
|
|
|
|
Disconnect(client *Client)
|
|
|
|
}
|
|
|
|
|
2023-03-05 08:14:36 +00:00
|
|
|
type Client struct {
|
2023-03-07 23:04:57 +00:00
|
|
|
handler ClientHandler
|
2023-03-07 07:20:36 +00:00
|
|
|
conn net.Conn
|
|
|
|
e_key []byte
|
|
|
|
fe_key []byte
|
|
|
|
whichKey int
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 23:04:57 +00:00
|
|
|
func NewClient(handler ClientHandler, conn net.Conn) *Client {
|
2023-03-05 08:14:36 +00:00
|
|
|
return &Client{
|
2023-03-07 23:04:57 +00:00
|
|
|
handler: handler,
|
2023-03-07 07:20:36 +00:00
|
|
|
conn: conn,
|
2023-03-07 23:04:57 +00:00
|
|
|
e_key: []byte(DEFAULT_KEY),
|
2023-03-07 07:20:36 +00:00
|
|
|
whichKey: USE_E,
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) Send(data interface{}, typeID uint32) {
|
|
|
|
// encode
|
2023-03-07 23:04:57 +00:00
|
|
|
pkt := 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
|
2023-03-07 07:20:36 +00:00
|
|
|
switch client.whichKey {
|
|
|
|
case USE_E:
|
2023-03-07 23:04:57 +00:00
|
|
|
EncryptData(tmp, client.e_key)
|
2023-03-07 07:20:36 +00:00
|
|
|
case USE_FE:
|
2023-03-07 23:04:57 +00:00
|
|
|
EncryptData(tmp, client.fe_key)
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-07 23:04:57 +00:00
|
|
|
func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8, data []SP_LS2CL_REP_CHAR_INFO) {
|
|
|
|
resp := &SP_LS2CL_REP_LOGIN_SUCC{
|
2023-03-07 20:23:53 +00:00
|
|
|
SzID: SZID,
|
2023-03-07 08:06:40 +00:00
|
|
|
ICharCount: int8(len(data)),
|
|
|
|
ISlotNum: ISlotNum,
|
2023-03-07 07:20:36 +00:00
|
|
|
IPaymentFlag: 1,
|
|
|
|
IOpenBetaFlag: 0,
|
2023-03-07 20:23:53 +00:00
|
|
|
UiSvrTime: uint64(time.Now().Unix()),
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 23:04:57 +00:00
|
|
|
client.Send(resp, P_LS2CL_REP_LOGIN_SUCC)
|
|
|
|
client.e_key = CreateNewKey(
|
2023-03-07 20:23:53 +00:00
|
|
|
resp.UiSvrTime,
|
2023-03-07 07:20:36 +00:00
|
|
|
uint64(resp.ICharCount+1),
|
|
|
|
uint64(resp.ISlotNum+1),
|
|
|
|
)
|
2023-03-07 23:04:57 +00:00
|
|
|
client.fe_key = CreateNewKey(
|
|
|
|
binary.LittleEndian.Uint64([]byte(DEFAULT_KEY)),
|
2023-03-07 08:06:40 +00:00
|
|
|
uint64(IClientVerC),
|
2023-03-07 07:20:36 +00:00
|
|
|
1,
|
|
|
|
)
|
2023-03-07 08:06:40 +00:00
|
|
|
|
|
|
|
// send characters (if any)
|
|
|
|
for i := 0; i < len(data); i++ {
|
2023-03-07 23:04:57 +00:00
|
|
|
client.Send(data[i], P_LS2CL_REP_CHAR_INFO)
|
2023-03-07 08:06:40 +00:00
|
|
|
}
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-05 08:14:36 +00:00
|
|
|
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()
|
2023-03-07 23:04:57 +00:00
|
|
|
client.handler.Disconnect(client)
|
2023-03-05 08:14:36 +00:00
|
|
|
}()
|
|
|
|
|
2023-03-07 23:04:57 +00:00
|
|
|
tmp := make([]byte, 4, 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
|
2023-03-07 23:04:57 +00:00
|
|
|
if sz > CN_PACKET_BUFFER_SIZE || sz < 4 {
|
2023-03-07 00:39:41 +00:00
|
|
|
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 23:04:57 +00:00
|
|
|
DecryptData(tmp[:sz], client.e_key)
|
2023-03-07 08:06:40 +00:00
|
|
|
typeID := uint32(binary.LittleEndian.Uint32(tmp[:4]))
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-07 08:06:40 +00:00
|
|
|
// dispatch packet
|
2023-03-07 00:39:41 +00:00
|
|
|
log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz)
|
2023-03-07 23:04:57 +00:00
|
|
|
pkt := NewPacket(tmp[4:sz])
|
|
|
|
client.handler.HandlePacket(client, typeID, pkt)
|
2023-03-07 00:39:41 +00:00
|
|
|
|
|
|
|
// reset tmp
|
|
|
|
tmp = tmp[:4]
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
}
|