gopenfusion/server/peer.go

149 lines
3.0 KiB
Go
Raw Normal View History

package server
2023-03-05 08:14:36 +00:00
import (
"encoding/binary"
"fmt"
"io"
2023-03-05 08:14:36 +00:00
"log"
"net"
2023-03-10 07:02:28 +00:00
"github.com/CPunch/gopenfusion/db"
"github.com/CPunch/gopenfusion/protocol"
"github.com/CPunch/gopenfusion/protocol/pool"
2023-03-05 08:14:36 +00:00
)
const (
USE_E = iota
USE_FE
)
2023-03-09 22:42:13 +00:00
type PeerHandler interface {
HandlePacket(client *Peer, typeID uint32, pkt protocol.Packet) error
2023-03-09 22:42:13 +00:00
Connect(client *Peer)
Disconnect(client *Peer)
}
2023-03-09 22:42:13 +00:00
type Peer struct {
E_key []byte
FE_key []byte
SzID string
AccountID int
Player *db.Player
2023-03-09 22:42:13 +00:00
handler PeerHandler
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{
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
}
}
func (peer *Peer) Send(typeID uint32, data ...interface{}) error {
buf := pool.Get()
defer pool.Put(buf) // always return the buffer to the pool
// body start
pkt := protocol.NewPacket(buf)
2023-03-05 08:14:36 +00:00
// encode type id
if err := pkt.Encode(uint32(typeID)); err != nil {
return err
}
2023-03-05 08:14:36 +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
// encrypt body
switch peer.whichKey {
case USE_E:
protocol.EncryptData(buf.Bytes(), peer.E_key)
case USE_FE:
protocol.EncryptData(buf.Bytes(), peer.FE_key)
}
2023-03-05 08:14:36 +00:00
// write packet size
if err := binary.Write(peer.conn, binary.LittleEndian, uint32(buf.Len())); err != nil {
return err
}
// write packet body
log.Printf("Sending %#v, sizeof: %d", data, buf.Len())
if _, err := peer.conn.Write(buf.Bytes()); err != nil {
return fmt.Errorf("[FATAL] failed to write packet body! %v", err)
2023-03-05 08:14:36 +00:00
}
return nil
2023-03-05 08:14:36 +00:00
}
func (peer *Peer) Kill() {
if !peer.alive {
return
}
peer.alive = false
peer.conn.Close()
peer.handler.Disconnect(peer)
}
func (peer *Peer) Handler() {
defer peer.Kill()
2023-03-05 08:14:36 +00:00
for {
// read packet size, the goroutine spends most of it's time parked here
var sz uint32
if err := binary.Read(peer.conn, binary.LittleEndian, &sz); err != nil {
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
if sz > protocol.CN_PACKET_BUFFER_SIZE || sz < 4 {
log.Printf("[FATAL] malicious packet size received! %d", sz)
return
2023-03-07 00:39:41 +00:00
}
2023-03-05 08:14:36 +00:00
// read packet body
buf := pool.Get()
if _, err := buf.ReadFrom(io.LimitReader(peer.conn, int64(sz))); err != nil {
log.Printf("[FATAL] failed to read packet body! %v", err)
return
2023-03-05 08:14:36 +00:00
}
// decrypt
protocol.DecryptData(buf.Bytes(), peer.E_key)
pkt := protocol.NewPacket(buf)
// create packet && read typeID
var typeID uint32
if err := pkt.Decode(&typeID); err != nil {
log.Printf("[FATAL] failed to read packet type! %v", err)
return
}
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)
if err := peer.handler.HandlePacket(peer, typeID, pkt); err != nil {
log.Printf("[FATAL] %v", err)
return
}
2023-03-07 00:39:41 +00:00
// restore buffer to pool
pool.Put(buf)
2023-03-05 08:14:36 +00:00
}
}