From 4d0bd4b1c1ab9d0b7857b10870296192b02da726 Mon Sep 17 00:00:00 2001 From: CPunch Date: Tue, 7 Mar 2023 02:06:40 -0600 Subject: [PATCH] absolute minimal login server --- protocol/packet.go | 8 ++------ server/client.go | 31 +++++++++++++------------------ server/server.go | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 24 deletions(-) diff --git a/protocol/packet.go b/protocol/packet.go index 8ac0db6..ddd26ea 100644 --- a/protocol/packet.go +++ b/protocol/packet.go @@ -3,7 +3,6 @@ package protocol import ( "encoding/binary" "fmt" - "log" "reflect" "strconv" "unicode/utf16" @@ -78,7 +77,7 @@ func (pkt *Packet) readByte() byte { } func (pkt *Packet) encodeStructField(field reflect.StructField, value reflect.Value) { - log.Printf("Encoding '%s', current cursor: %d", field.Name, len(pkt.Buf)) + // log.Printf("Encoding '%s', current cursor: %d", field.Name, len(pkt.Buf)) switch field.Type.Kind() { case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to encode it @@ -102,7 +101,6 @@ func (pkt *Packet) encodeStructField(field reflect.StructField, value reflect.Va } } - log.Printf("sending %d", len(buf)) pkt.Write(buf) default: pkt.Encode(value.Addr().Interface()) @@ -168,7 +166,7 @@ func (pkt *Packet) Encode(data interface{}) { } func (pkt *Packet) decodeStructField(field reflect.StructField, value reflect.Value) { - log.Printf("Decoding '%s', current cursor: %d", field.Name, pkt.cursor) + // log.Printf("Decoding '%s', current cursor: %d", field.Name, pkt.cursor) switch field.Type.Kind() { case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to decode it @@ -193,7 +191,6 @@ func (pkt *Packet) decodeStructField(field reflect.StructField, value reflect.Va } str := string(utf16.Decode(buf16[:realSize])) - log.Printf(" got: %#.v or '%s'", buf16, str) value.SetString(str) default: pkt.Decode(value.Addr().Interface()) @@ -222,7 +219,6 @@ func (pkt *Packet) Decode(data interface{}) { sz := rv.Len() // decode data - log.Print("reading array length ", sz, " cursor: ", pkt.cursor) for i := 0; i < sz; i++ { elem := rv.Index(i) pkt.Decode(elem.Addr().Interface()) diff --git a/server/client.go b/server/client.go index e3f0189..d1f6a37 100644 --- a/server/client.go +++ b/server/client.go @@ -61,19 +61,13 @@ func (client *Client) Send(data interface{}, typeID uint32) { if _, err := client.conn.Write(tmp); err != nil { panic(fmt.Errorf("[FATAL] failed to write packet body! %v", err)) } - log.Printf("sent!") } -func (client *Client) Login(pkt *protocol.Packet) { - var loginPkt protocol.SP_CL2LS_REQ_LOGIN - pkt.Decode(&loginPkt) - log.Printf("Got packet: %#v", loginPkt) - - // !! TODO +func (client *Client) AcceptLogin(SZID string, IClientVerC int32, ISlotNum int8, data []protocol.SP_LS2CL_REP_CHAR_INFO) { resp := &protocol.SP_LS2CL_REP_LOGIN_SUCC{ - SZID: loginPkt.SZID, - ICharCount: 0, - ISlotNum: 1, + SZID: SZID, + ICharCount: int8(len(data)), + ISlotNum: ISlotNum, IPaymentFlag: 1, IOpenBetaFlag: 0, UISvrTime: uint64(time.Now().Unix()), @@ -87,9 +81,14 @@ func (client *Client) Login(pkt *protocol.Packet) { ) client.fe_key = protocol.CreateNewKey( binary.LittleEndian.Uint64([]byte(protocol.DEFAULT_KEY)), - uint64(loginPkt.IClientVerC), + uint64(IClientVerC), 1, ) + + // send characters (if any) + for i := 0; i < len(data); i++ { + client.Send(data[i], protocol.P_LS2CL_REP_CHAR_INFO) + } } func (client *Client) ClientHandler() { @@ -121,16 +120,12 @@ func (client *Client) ClientHandler() { // decrypt && grab typeID protocol.DecryptData(tmp[:sz], client.e_key) - typeID := int(binary.LittleEndian.Uint32(tmp[:4])) + 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]) - switch typeID { - case protocol.P_CL2LS_REQ_LOGIN: - client.Login(pkt) - default: - log.Printf("[WARN] unsupported packet ID: %x\n", typeID) - } + client.server.handlePacket(client, typeID, pkt) // reset tmp tmp = tmp[:4] diff --git a/server/server.go b/server/server.go index a5c380d..1a45abe 100644 --- a/server/server.go +++ b/server/server.go @@ -4,6 +4,8 @@ import ( "fmt" "log" "net" + + "github.com/CPunch/GopenFusion/protocol" ) type Server struct { @@ -48,6 +50,38 @@ func (server *Server) Start() { } } +func (server *Server) handlePacket(client *Client, typeID uint32, pkt *protocol.Packet) { + switch typeID { + case protocol.P_CL2LS_REQ_LOGIN: + var loginPkt protocol.SP_CL2LS_REQ_LOGIN + pkt.Decode(&loginPkt) + + // TODO: for now, we're a dummy server + client.AcceptLogin(loginPkt.SZID, loginPkt.IClientVerC, 1, []protocol.SP_LS2CL_REP_CHAR_INFO{}) + case protocol.P_CL2LS_REQ_CHECK_CHAR_NAME: + var charPkt protocol.SP_CL2LS_REQ_CHECK_CHAR_NAME + pkt.Decode(&charPkt) + + client.Send(&protocol.SP_LS2CL_REP_CHECK_CHAR_NAME_SUCC{ + SZFirstName: charPkt.SZFirstName, + SZLastName: charPkt.SZLastName, + }, protocol.P_LS2CL_REP_CHECK_CHAR_NAME_SUCC) + case protocol.P_CL2LS_REQ_SAVE_CHAR_NAME: + var charPkt protocol.SP_CL2LS_REQ_SAVE_CHAR_NAME + pkt.Decode(&charPkt) + + client.Send(&protocol.SP_LS2CL_REP_SAVE_CHAR_NAME_SUCC{ + IPC_UID: 1, + ISlotNum: charPkt.ISlotNum, + IGender: charPkt.IGender, + SZFirstName: charPkt.SZFirstName, + SZLastName: charPkt.SZLastName, + }, protocol.P_LS2CL_REP_SAVE_CHAR_NAME_SUCC) + default: + log.Printf("[WARN] unsupported packet ID: %x\n", typeID) + } +} + func main() { server := newServer() server.Start()