2023-03-09 07:48:13 +00:00
|
|
|
package server
|
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-03-09 07:48:13 +00:00
|
|
|
|
2023-03-10 07:02:28 +00:00
|
|
|
"github.com/CPunch/gopenfusion/db"
|
|
|
|
"github.com/CPunch/gopenfusion/protocol"
|
2023-03-11 01:56:05 +00:00
|
|
|
"github.com/CPunch/gopenfusion/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-09 22:42:13 +00:00
|
|
|
type PeerHandler interface {
|
2023-03-11 02:00:06 +00:00
|
|
|
HandlePacket(client *Peer, typeID uint32, pkt protocol.Packet)
|
2023-03-09 22:42:13 +00:00
|
|
|
Connect(client *Peer)
|
|
|
|
Disconnect(client *Peer)
|
2023-03-07 23:04:57 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:13 +00:00
|
|
|
type Peer struct {
|
2023-03-09 07:48:13 +00:00
|
|
|
E_key []byte
|
|
|
|
FE_key []byte
|
|
|
|
SzID string
|
|
|
|
AccountID int
|
|
|
|
Player *db.Player
|
2023-03-09 22:42:13 +00:00
|
|
|
handler PeerHandler
|
2023-03-09 07:48:13 +00:00
|
|
|
conn net.Conn
|
|
|
|
alive bool
|
|
|
|
whichKey int
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:13 +00:00
|
|
|
func NewPeer(handler PeerHandler, conn net.Conn) *Peer {
|
|
|
|
return &Peer{
|
2023-03-09 07:48:13 +00:00
|
|
|
E_key: []byte(protocol.DEFAULT_KEY),
|
|
|
|
FE_key: nil,
|
|
|
|
SzID: "",
|
|
|
|
AccountID: -1,
|
|
|
|
Player: nil,
|
|
|
|
handler: handler,
|
|
|
|
conn: conn,
|
|
|
|
alive: true,
|
|
|
|
whichKey: USE_E,
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:13 +00:00
|
|
|
func (client *Peer) Send(data interface{}, typeID uint32) {
|
2023-03-11 01:56:05 +00:00
|
|
|
buf := pool.Get()
|
|
|
|
defer func() { // always return the buffer to the pool
|
|
|
|
pool.Put(buf)
|
|
|
|
}()
|
|
|
|
|
2023-03-05 08:14:36 +00:00
|
|
|
// encode
|
2023-03-11 01:56:05 +00:00
|
|
|
pkt := protocol.NewPacket(buf)
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 01:56:05 +00:00
|
|
|
// write the typeID and packet body
|
|
|
|
pkt.Encode(uint32(typeID))
|
|
|
|
pkt.Encode(data)
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 01:56:05 +00:00
|
|
|
// write the packet size
|
|
|
|
binary.Write(client.conn, binary.LittleEndian, uint32(buf.Len()))
|
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-11 01:56:05 +00:00
|
|
|
protocol.EncryptData(buf.Bytes(), client.E_key)
|
2023-03-07 07:20:36 +00:00
|
|
|
case USE_FE:
|
2023-03-11 01:56:05 +00:00
|
|
|
protocol.EncryptData(buf.Bytes(), client.FE_key)
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
2023-03-05 08:14:36 +00:00
|
|
|
|
2023-03-11 01:56:05 +00:00
|
|
|
// write packet type && packet body
|
|
|
|
log.Printf("Sending %#v, sizeof: %d", data, buf.Len())
|
|
|
|
if _, err := client.conn.Write(buf.Bytes()); err != nil {
|
2023-03-05 08:14:36 +00:00
|
|
|
panic(fmt.Errorf("[FATAL] failed to write packet body! %v", err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:13 +00:00
|
|
|
func (client *Peer) Kill() {
|
2023-03-09 07:48:13 +00:00
|
|
|
if !client.alive {
|
|
|
|
return
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 07:48:13 +00:00
|
|
|
client.alive = false
|
|
|
|
client.conn.Close()
|
|
|
|
client.handler.Disconnect(client)
|
2023-03-07 07:20:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 22:42:13 +00:00
|
|
|
func (client *Peer) ClientHandler() {
|
2023-03-05 08:14:36 +00:00
|
|
|
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-09 07:48:13 +00:00
|
|
|
client.Kill()
|
2023-03-05 08:14:36 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// read packet size
|
2023-03-11 01:56:05 +00:00
|
|
|
var sz uint32
|
|
|
|
if err := binary.Read(client.conn, binary.LittleEndian, &sz); err != nil {
|
2023-03-05 08:14:36 +00:00
|
|
|
panic(fmt.Errorf("[FATAL] failed to read packet size! %v", err))
|
|
|
|
}
|
|
|
|
|
2023-03-07 00:39:41 +00:00
|
|
|
// client should never send a packet size outside of this range
|
2023-03-09 07:48:13 +00:00
|
|
|
if sz > protocol.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-11 01:56:05 +00:00
|
|
|
buf := pool.Get()
|
|
|
|
if _, err := buf.ReadFrom(io.LimitReader(client.conn, int64(sz))); err != nil {
|
2023-03-05 08:14:36 +00:00
|
|
|
panic(fmt.Errorf("[FATAL] failed to read packet body! %v", err))
|
|
|
|
}
|
|
|
|
|
2023-03-11 01:56:05 +00:00
|
|
|
// decrypt
|
|
|
|
protocol.DecryptData(buf.Bytes(), client.E_key)
|
|
|
|
|
|
|
|
// create packet && read typeID
|
|
|
|
var typeID uint32
|
|
|
|
pkt := protocol.NewPacket(buf)
|
|
|
|
pkt.Decode(&typeID)
|
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
|
|
|
client.handler.HandlePacket(client, typeID, pkt)
|
2023-03-07 00:39:41 +00:00
|
|
|
|
2023-03-11 01:56:05 +00:00
|
|
|
// restore buffer to pool
|
|
|
|
pool.Put(buf)
|
2023-03-05 08:14:36 +00:00
|
|
|
}
|
|
|
|
}
|