2023-03-18 21:40:20 +00:00
|
|
|
package protocol
|
2023-03-05 08:14:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2023-03-11 01:56:05 +00:00
|
|
|
"io"
|
2023-03-05 08:14:36 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2023-06-25 23:29:31 +00:00
|
|
|
"time"
|
2023-03-09 07:48:13 +00:00
|
|
|
|
2023-03-22 05:30:58 +00:00
|
|
|
"github.com/CPunch/gopenfusion/core/protocol/pool"
|
2023-03-05 08:14:36 +00:00
|
|
|
)
|
|
|
|
|
2023-03-07 07:20:36 +00:00
|
|
|
const (
|
|
|
|
USE_E = iota
|
|
|
|
USE_FE
|
|
|
|
)
|
|
|
|
|
2023-03-18 21:40:20 +00:00
|
|
|
// CNPeer is a simple wrapper for net.Conn connections to send/recv packets over the Fusionfall packet protocol.
|
|
|
|
type CNPeer struct {
|
2023-03-11 09:28:11 +00:00
|
|
|
conn net.Conn
|
2023-06-25 06:51:21 +00:00
|
|
|
eRecv chan *Event
|
2023-03-11 09:28:11 +00:00
|
|
|
SzID string
|
2023-03-09 07:48:13 +00:00
|
|
|
E_key []byte
|
|
|
|
FE_key []byte
|
|
|
|
AccountID int
|
2023-06-25 03:36:04 +00:00
|
|
|
PlayerID int32
|
2023-03-09 07:48:13 +00:00
|
|
|
whichKey int
|
2023-03-11 09:28:11 +00:00
|
|
|
alive bool
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-06-25 23:29:31 +00:00
|
|
|
func GetTime() uint64 {
|
|
|
|
return uint64(time.Now().UnixMilli())
|
|
|
|
}
|
|
|
|
|
2023-06-25 06:51:21 +00:00
|
|
|
func NewCNPeer(eRecv chan *Event, conn net.Conn) *CNPeer {
|
2023-03-18 21:40:20 +00:00
|
|
|
return &CNPeer{
|
2023-03-17 21:27:47 +00:00
|
|
|
conn: conn,
|
2023-06-25 06:51:21 +00:00
|
|
|
eRecv: eRecv,
|
2023-03-17 21:27:47 +00:00
|
|
|
SzID: "",
|
2023-03-18 21:40:20 +00:00
|
|
|
E_key: []byte(DEFAULT_KEY),
|
2023-03-09 07:48:13 +00:00
|
|
|
FE_key: nil,
|
|
|
|
AccountID: -1,
|
|
|
|
whichKey: USE_E,
|
2023-03-17 21:27:47 +00:00
|
|
|
alive: true,
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-18 21:40:20 +00:00
|
|
|
func (peer *CNPeer) Send(typeID uint32, data ...interface{}) error {
|
2023-03-11 09:28:11 +00:00
|
|
|
// grab buffer from pool
|
2023-03-11 01:56:05 +00:00
|
|
|
buf := pool.Get()
|
2023-03-11 09:28:11 +00:00
|
|
|
defer pool.Put(buf)
|
2023-03-11 01:56:05 +00:00
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
// body start
|
2023-03-18 21:40:20 +00:00
|
|
|
pkt := NewPacket(buf)
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
// encode type id
|
2023-03-11 09:28:11 +00:00
|
|
|
if err := pkt.Encode(typeID); err != nil {
|
2023-03-11 05:59:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
// encode data
|
|
|
|
for _, trailer := range data {
|
|
|
|
if err := pkt.Encode(trailer); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
// encrypt body
|
|
|
|
switch peer.whichKey {
|
2023-03-07 07:20:36 +00:00
|
|
|
case USE_E:
|
2023-03-18 21:40:20 +00:00
|
|
|
EncryptData(buf.Bytes(), peer.E_key)
|
2023-03-07 07:20:36 +00:00
|
|
|
case USE_FE:
|
2023-03-18 21:40:20 +00:00
|
|
|
EncryptData(buf.Bytes(), peer.FE_key)
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
// write packet size
|
|
|
|
if err := binary.Write(peer.conn, binary.LittleEndian, uint32(buf.Len())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write packet body
|
2023-03-11 01:56:05 +00:00
|
|
|
log.Printf("Sending %#v, sizeof: %d", data, buf.Len())
|
2023-03-11 05:59:13 +00:00
|
|
|
if _, err := peer.conn.Write(buf.Bytes()); err != nil {
|
2023-03-11 02:30:16 +00:00
|
|
|
return fmt.Errorf("[FATAL] failed to write packet body! %v", err)
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
2023-03-11 02:30:16 +00:00
|
|
|
|
|
|
|
return nil
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 22:07:16 +00:00
|
|
|
func (peer *CNPeer) SetActiveKey(whichKey int) {
|
|
|
|
peer.whichKey = whichKey
|
|
|
|
}
|
|
|
|
|
2023-03-18 21:40:20 +00:00
|
|
|
func (peer *CNPeer) Kill() {
|
2023-06-25 06:51:21 +00:00
|
|
|
log.Printf("Killing peer %p", peer)
|
2023-03-11 05:59:13 +00:00
|
|
|
if !peer.alive {
|
2023-03-09 07:48:13 +00:00
|
|
|
return
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 05:59:13 +00:00
|
|
|
peer.alive = false
|
|
|
|
peer.conn.Close()
|
2023-06-25 06:51:21 +00:00
|
|
|
peer.eRecv <- &Event{Type: EVENT_CLIENT_DISCONNECT, Peer: peer}
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-18 21:40:20 +00:00
|
|
|
// meant to be invoked as a goroutine
|
|
|
|
func (peer *CNPeer) Handler() {
|
2023-03-11 05:59:13 +00:00
|
|
|
defer peer.Kill()
|
2023-03-05 08:14:36 +00:00
|
|
|
|
|
|
|
for {
|
2023-03-11 05:59:13 +00:00
|
|
|
// read packet size, the goroutine spends most of it's time parked here
|
2023-03-11 01:56:05 +00:00
|
|
|
var sz uint32
|
2023-03-11 05:59:13 +00:00
|
|
|
if err := binary.Read(peer.conn, binary.LittleEndian, &sz); err != nil {
|
2023-03-11 02:30:16 +00:00
|
|
|
log.Printf("[FATAL] failed to read packet size! %v\n", err)
|
|
|
|
return
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-07 00:39:41 +00:00
|
|
|
// client should never send a packet size outside of this range
|
2023-03-18 21:40:20 +00:00
|
|
|
if sz > CN_PACKET_BUFFER_SIZE || sz < 4 {
|
2023-03-11 02:30:16 +00:00
|
|
|
log.Printf("[FATAL] malicious packet size received! %d", sz)
|
|
|
|
return
|
2023-03-07 00:39:41 +00:00
|
|
|
}
|
|
|
|
|
2023-03-11 09:28:11 +00:00
|
|
|
// grab buffer && read packet body
|
2023-03-27 02:08:13 +00:00
|
|
|
if err := func() error {
|
2023-03-11 21:02:01 +00:00
|
|
|
buf := pool.Get()
|
|
|
|
if _, err := buf.ReadFrom(io.LimitReader(peer.conn, int64(sz))); err != nil {
|
|
|
|
return fmt.Errorf("failed to read packet body! %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt
|
2023-03-18 21:40:20 +00:00
|
|
|
DecryptData(buf.Bytes(), peer.E_key)
|
|
|
|
pkt := NewPacket(buf)
|
2023-03-11 21:02:01 +00:00
|
|
|
|
2023-06-25 06:51:21 +00:00
|
|
|
// create packet && read pktID
|
|
|
|
var pktID uint32
|
|
|
|
if err := pkt.Decode(&pktID); err != nil {
|
2023-03-11 21:02:01 +00:00
|
|
|
return fmt.Errorf("failed to read packet type! %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// dispatch packet
|
2023-06-25 06:51:21 +00:00
|
|
|
log.Printf("Got packet ID: %x, with a sizeof: %d\n", pktID, sz)
|
|
|
|
peer.eRecv <- &Event{Type: EVENT_CLIENT_PACKET, Peer: peer, Pkt: buf, PktID: pktID}
|
2023-03-11 21:02:01 +00:00
|
|
|
return nil
|
|
|
|
}(); err != nil {
|
2023-03-11 02:30:16 +00:00
|
|
|
log.Printf("[FATAL] %v", err)
|
|
|
|
return
|
|
|
|
}
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
}
|