refactoring; moved client to protocol package

This commit is contained in:
CPunch 2023-03-07 17:04:57 -06:00
parent 4c7696dc92
commit 1fff485f93
4 changed files with 34 additions and 42 deletions

View File

@ -1,4 +1,4 @@
package server package protocol
import ( import (
"encoding/binary" "encoding/binary"
@ -6,8 +6,6 @@ import (
"log" "log"
"net" "net"
"time" "time"
"github.com/CPunch/GopenFusion/protocol"
) )
const ( const (
@ -15,26 +13,31 @@ const (
USE_FE USE_FE
) )
type ClientHandler interface {
HandlePacket(client *Client, typeID uint32, pkt *Packet)
Disconnect(client *Client)
}
type Client struct { type Client struct {
server Server handler ClientHandler
conn net.Conn conn net.Conn
e_key []byte e_key []byte
fe_key []byte fe_key []byte
whichKey int whichKey int
} }
func newClient(server Server, conn net.Conn) *Client { func NewClient(handler ClientHandler, conn net.Conn) *Client {
return &Client{ return &Client{
server: server, handler: handler,
conn: conn, conn: conn,
e_key: []byte(protocol.DEFAULT_KEY), e_key: []byte(DEFAULT_KEY),
whichKey: USE_E, whichKey: USE_E,
} }
} }
func (client *Client) Send(data interface{}, typeID uint32) { func (client *Client) Send(data interface{}, typeID uint32) {
// encode // encode
pkt := protocol.NewPacket(make([]byte, 0)) pkt := NewPacket(make([]byte, 0))
pkt.Encode(data) pkt.Encode(data)
log.Printf("Sending %#v, sizeof: %d", data, len(pkt.Buf)) log.Printf("Sending %#v, sizeof: %d", data, len(pkt.Buf))
@ -52,9 +55,9 @@ func (client *Client) Send(data interface{}, typeID uint32) {
// encrypt typeID & body // encrypt typeID & body
switch client.whichKey { switch client.whichKey {
case USE_E: case USE_E:
protocol.EncryptData(tmp, client.e_key) EncryptData(tmp, client.e_key)
case USE_FE: case USE_FE:
protocol.EncryptData(tmp, client.fe_key) EncryptData(tmp, client.fe_key)
} }
// write packet body // write packet body
@ -63,8 +66,8 @@ func (client *Client) Send(data interface{}, typeID uint32) {
} }
} }
func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) { func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8, data []SP_LS2CL_REP_CHAR_INFO) {
resp := &protocol.SP_LS2CL_REP_LOGIN_SUCC{ resp := &SP_LS2CL_REP_LOGIN_SUCC{
SzID: SZID, SzID: SZID,
ICharCount: int8(len(data)), ICharCount: int8(len(data)),
ISlotNum: ISlotNum, ISlotNum: ISlotNum,
@ -73,21 +76,21 @@ func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8,
UiSvrTime: uint64(time.Now().Unix()), UiSvrTime: uint64(time.Now().Unix()),
} }
client.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC) client.Send(resp, P_LS2CL_REP_LOGIN_SUCC)
client.e_key = protocol.CreateNewKey( client.e_key = CreateNewKey(
resp.UiSvrTime, resp.UiSvrTime,
uint64(resp.ICharCount+1), uint64(resp.ICharCount+1),
uint64(resp.ISlotNum+1), uint64(resp.ISlotNum+1),
) )
client.fe_key = protocol.CreateNewKey( client.fe_key = CreateNewKey(
binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)), binary.LittleEndian.Uint64([]byte(DEFAULT_KEY)),
uint64(IClientVerC), uint64(IClientVerC),
1, 1,
) )
// send characters (if any) // send characters (if any)
for i := 0; i < len(data); i++ { for i := 0; i < len(data); i++ {
client.Send(data[i], protocol.P_LS2CL_REP_CHAR_INFO) client.Send(data[i], P_LS2CL_REP_CHAR_INFO)
} }
} }
@ -97,10 +100,10 @@ func (client *Client) ClientHandler() {
log.Printf("Client %p panic'd! %v", client, err) log.Printf("Client %p panic'd! %v", client, err)
} }
client.conn.Close() client.conn.Close()
client.server.Disconnect(client) client.handler.Disconnect(client)
}() }()
tmp := make([]byte, 4, protocol.CN_PACKET_BUFFER_SIZE) tmp := make([]byte, 4, CN_PACKET_BUFFER_SIZE)
for { for {
// read packet size // read packet size
if _, err := client.conn.Read(tmp); err != nil { if _, err := client.conn.Read(tmp); err != nil {
@ -109,7 +112,7 @@ func (client *Client) ClientHandler() {
sz := int(binary.LittleEndian.Uint32(tmp)) sz := int(binary.LittleEndian.Uint32(tmp))
// client should never send a packet size outside of this range // client should never send a packet size outside of this range
if sz > protocol.CN_PACKET_BUFFER_SIZE || sz < 4 { if sz > CN_PACKET_BUFFER_SIZE || sz < 4 {
panic(fmt.Errorf("[FATAL] malicious packet size received! %d", sz)) panic(fmt.Errorf("[FATAL] malicious packet size received! %d", sz))
} }
@ -119,13 +122,13 @@ func (client *Client) ClientHandler() {
} }
// decrypt && grab typeID // decrypt && grab typeID
protocol.DecryptData(tmp[:sz], client.e_key) DecryptData(tmp[:sz], client.e_key)
typeID := uint32(binary.LittleEndian.Uint32(tmp[:4])) typeID := uint32(binary.LittleEndian.Uint32(tmp[:4]))
// dispatch packet // dispatch packet
log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz) log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz)
pkt := protocol.NewPacket(tmp[4:sz]) pkt := NewPacket(tmp[4:sz])
client.server.HandlePacket(client, typeID, pkt) client.handler.HandlePacket(client, typeID, pkt)
// reset tmp // reset tmp
tmp = tmp[:4] tmp = tmp[:4]

View File

@ -10,8 +10,8 @@ import (
type LoginServer struct { type LoginServer struct {
listener net.Listener listener net.Listener
clients map[*Client]bool clients map[*protocol.Client]bool
unregister chan *Client unregister chan *protocol.Client
} }
func NewLoginServer() *LoginServer { func NewLoginServer() *LoginServer {
@ -22,8 +22,8 @@ func NewLoginServer() *LoginServer {
return &LoginServer{ return &LoginServer{
listener: listener, listener: listener,
clients: make(map[*Client]bool), clients: make(map[*protocol.Client]bool),
unregister: make(chan *Client), unregister: make(chan *protocol.Client),
} }
} }
@ -42,15 +42,15 @@ func (server *LoginServer) Start() {
return return
} }
client := newClient(server, conn) client := protocol.NewClient(server, conn)
server.clients[client] = true server.clients[client] = true
go client.ClientHandler()
fmt.Printf("Client %p connected\n", client) fmt.Printf("Client %p connected\n", client)
go client.ClientHandler()
} }
} }
} }
func (server *LoginServer) HandlePacket(client *Client, typeID uint32, pkt *protocol.Packet) { func (server *LoginServer) HandlePacket(client *protocol.Client, typeID uint32, pkt *protocol.Packet) {
switch typeID { switch typeID {
case protocol.P_CL2LS_REQ_LOGIN: case protocol.P_CL2LS_REQ_LOGIN:
var loginPkt protocol.SP_CL2LS_REQ_LOGIN var loginPkt protocol.SP_CL2LS_REQ_LOGIN
@ -82,6 +82,6 @@ func (server *LoginServer) HandlePacket(client *Client, typeID uint32, pkt *prot
} }
} }
func (server *LoginServer) Disconnect(client *Client) { func (server *LoginServer) Disconnect(client *protocol.Client) {
server.unregister <- client server.unregister <- client
} }

View File

@ -1,10 +0,0 @@
package server
import (
"github.com/CPunch/GopenFusion/protocol"
)
type Server interface {
HandlePacket(client *Client, typeID uint32, pkt *protocol.Packet)
Disconnect(client *Client)
}

View File

@ -376,4 +376,3 @@ if __name__ == '__main__':
os.remove("tmp.c") os.remove("tmp.c")
os.remove("tmp.o") os.remove("tmp.o")