From 1fff485f93d724226c6b58c4658a37085900e9b9 Mon Sep 17 00:00:00 2001 From: CPunch Date: Tue, 7 Mar 2023 17:04:57 -0600 Subject: [PATCH] refactoring; moved client to protocol package --- {server => protocol}/client.go | 49 ++++++++++++++++++---------------- server/loginserver.go | 16 +++++------ server/server.go | 10 ------- tools/genstructs.py | 1 - 4 files changed, 34 insertions(+), 42 deletions(-) rename {server => protocol}/client.go (70%) delete mode 100644 server/server.go diff --git a/server/client.go b/protocol/client.go similarity index 70% rename from server/client.go rename to protocol/client.go index 49af5ba..153d5a3 100644 --- a/server/client.go +++ b/protocol/client.go @@ -1,4 +1,4 @@ -package server +package protocol import ( "encoding/binary" @@ -6,8 +6,6 @@ import ( "log" "net" "time" - - "github.com/CPunch/GopenFusion/protocol" ) const ( @@ -15,26 +13,31 @@ const ( USE_FE ) +type ClientHandler interface { + HandlePacket(client *Client, typeID uint32, pkt *Packet) + Disconnect(client *Client) +} + type Client struct { - server Server + handler ClientHandler conn net.Conn e_key []byte fe_key []byte whichKey int } -func newClient(server Server, conn net.Conn) *Client { +func NewClient(handler ClientHandler, conn net.Conn) *Client { return &Client{ - server: server, + handler: handler, conn: conn, - e_key: []byte(protocol.DEFAULT_KEY), + e_key: []byte(DEFAULT_KEY), whichKey: USE_E, } } func (client *Client) Send(data interface{}, typeID uint32) { // encode - pkt := protocol.NewPacket(make([]byte, 0)) + pkt := NewPacket(make([]byte, 0)) pkt.Encode(data) 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 switch client.whichKey { case USE_E: - protocol.EncryptData(tmp, client.e_key) + EncryptData(tmp, client.e_key) case USE_FE: - protocol.EncryptData(tmp, client.fe_key) + EncryptData(tmp, client.fe_key) } // 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) { - resp := &protocol.SP_LS2CL_REP_LOGIN_SUCC{ +func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8, data []SP_LS2CL_REP_CHAR_INFO) { + resp := &SP_LS2CL_REP_LOGIN_SUCC{ SzID: SZID, ICharCount: int8(len(data)), ISlotNum: ISlotNum, @@ -73,21 +76,21 @@ func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8, UiSvrTime: uint64(time.Now().Unix()), } - client.Send(resp, protocol.P_LS2CL_REP_LOGIN_SUCC) - client.e_key = protocol.CreateNewKey( + client.Send(resp, P_LS2CL_REP_LOGIN_SUCC) + client.e_key = CreateNewKey( resp.UiSvrTime, uint64(resp.ICharCount+1), uint64(resp.ISlotNum+1), ) - client.fe_key = protocol.CreateNewKey( - binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)), + client.fe_key = CreateNewKey( + binary.LittleEndian.Uint64([]byte(DEFAULT_KEY)), uint64(IClientVerC), 1, ) // send characters (if any) 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) } 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 { // read packet size if _, err := client.conn.Read(tmp); err != nil { @@ -109,7 +112,7 @@ func (client *Client) ClientHandler() { sz := int(binary.LittleEndian.Uint32(tmp)) // 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)) } @@ -119,13 +122,13 @@ func (client *Client) ClientHandler() { } // decrypt && grab typeID - protocol.DecryptData(tmp[:sz], client.e_key) + DecryptData(tmp[:sz], client.e_key) typeID := uint32(binary.LittleEndian.Uint32(tmp[:4])) // dispatch packet log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, sz) - pkt := protocol.NewPacket(tmp[4:sz]) - client.server.HandlePacket(client, typeID, pkt) + pkt := NewPacket(tmp[4:sz]) + client.handler.HandlePacket(client, typeID, pkt) // reset tmp tmp = tmp[:4] diff --git a/server/loginserver.go b/server/loginserver.go index 9db3e8f..98ccbd9 100644 --- a/server/loginserver.go +++ b/server/loginserver.go @@ -10,8 +10,8 @@ import ( type LoginServer struct { listener net.Listener - clients map[*Client]bool - unregister chan *Client + clients map[*protocol.Client]bool + unregister chan *protocol.Client } func NewLoginServer() *LoginServer { @@ -22,8 +22,8 @@ func NewLoginServer() *LoginServer { return &LoginServer{ listener: listener, - clients: make(map[*Client]bool), - unregister: make(chan *Client), + clients: make(map[*protocol.Client]bool), + unregister: make(chan *protocol.Client), } } @@ -42,15 +42,15 @@ func (server *LoginServer) Start() { return } - client := newClient(server, conn) + client := protocol.NewClient(server, conn) server.clients[client] = true - go client.ClientHandler() 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 { case protocol.P_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 } diff --git a/server/server.go b/server/server.go deleted file mode 100644 index b792ff0..0000000 --- a/server/server.go +++ /dev/null @@ -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) -} diff --git a/tools/genstructs.py b/tools/genstructs.py index b6bbc86..eb01944 100755 --- a/tools/genstructs.py +++ b/tools/genstructs.py @@ -376,4 +376,3 @@ if __name__ == '__main__': os.remove("tmp.c") os.remove("tmp.o") -