From 115005b6a300ceb29df5e448f58ca15c4bb5926b Mon Sep 17 00:00:00 2001 From: CPunch Date: Sun, 5 Mar 2023 02:14:36 -0600 Subject: [PATCH] can now respond to packets --- protocol/consts.go | 9 ++++ protocol/encrypt.go | 45 ++++++++++++++++++ protocol/packet.go | 54 +++++++++++++++------- protocol/structs.go | 5 ++ server/client.go | 91 ++++++++++++++++++++++++++++++++++++ server/server.go | 109 +++++++++++++------------------------------- 6 files changed, 219 insertions(+), 94 deletions(-) create mode 100644 protocol/encrypt.go create mode 100644 server/client.go diff --git a/protocol/consts.go b/protocol/consts.go index d860e3d..d17aba0 100644 --- a/protocol/consts.go +++ b/protocol/consts.go @@ -76,6 +76,15 @@ const ( EQUIP_SLOT_HAND_EX = 7 EQUIP_SLOT_VEHICLE = 8 + LOGIN_FAIL_DB_ERROR = 0 + LOGIN_FAIL_ID_ERROR = 1 + LOGIN_FAIL_ID_PW_MATCH_ERROR = 2 + LOGIN_FAIL_DUP_ERROR = 3 + LOGIN_FAIL_VERSION_ERROR = 6 + LOGIN_FAIL_NOT_BETA_ERROR = 7 + LOGIN_FAIL_CN_SERVICE_ERROR = 8 + LOGIN_FAIL_EULA_ERROR = 9 + WPN_EQUIP_TYPE_NONE = 0 WPN_EQUIP_TYPE_OH_BLADE = 1 WPN_EQUIP_TYPE_OH_CLUB = 2 diff --git a/protocol/encrypt.go b/protocol/encrypt.go new file mode 100644 index 0000000..ecf846d --- /dev/null +++ b/protocol/encrypt.go @@ -0,0 +1,45 @@ +package protocol + +const ( + E_KEY = "m@rQn~W#" + KEY_LENGTH = 8 +) + +func encrypt_byte_change_A(ERSize int, data []byte) int { + var num, num2, num3 int + + for num+ERSize <= len(data) { + num4 := num + num3 + num5 := num + (ERSize - 1 - num3) + + b := data[num4] + data[num4] = data[num5] + data[num5] = b + num += ERSize + num3++ + if num3 > ERSize/2 { + num3 = 0 + } + } + + num2 = ERSize - (num + ERSize - len(data)) + return num + num2 +} + +func xorData(buff, key []byte, size int) { + for i := 0; i < size; i++ { + buff[i] ^= key[i%KEY_LENGTH] + } +} + +func EncryptData(buff, key []byte) { + ERSize := len(buff)%(KEY_LENGTH/2+1)*2 + KEY_LENGTH + xorData(buff, key, len(buff)) + encrypt_byte_change_A(ERSize, buff) +} + +func DecryptData(buff, key []byte) { + ERSize := len(buff)%(KEY_LENGTH/2+1)*2 + KEY_LENGTH + size := encrypt_byte_change_A(ERSize, buff) + xorData(buff, key, size) +} diff --git a/protocol/packet.go b/protocol/packet.go index 37f630a..207370a 100644 --- a/protocol/packet.go +++ b/protocol/packet.go @@ -23,8 +23,12 @@ type Packet struct { const PACK_ALIGN = 4 func NewPacket(sz int) *Packet { - newBuf := make([]byte, sz) - return &Packet{ByteOrder: binary.LittleEndian, Buf: newBuf} + pkt := &Packet{ + ByteOrder: binary.LittleEndian, + Buf: make([]byte, sz), + cursor: 0, + } + return pkt } func (pkt *Packet) ResetCursor() { @@ -44,6 +48,10 @@ func (pkt *Packet) writeRaw(data []byte) { func (pkt *Packet) Write(data []byte) { pkt.writeRaw(data) + + if len(pkt.Buf) > CN_PACKET_BUFFER_SIZE { + panic(fmt.Errorf("Failed to write to packet, invalid size!")) + } } func (pkt *Packet) writeByte(data byte) { @@ -59,7 +67,7 @@ func (pkt *Packet) readRaw(sz int) []byte { func (pkt *Packet) Read(sz int) []byte { if sz > len(pkt.Buf) { - panic("Failed to read from packet, invalid size!") + panic(fmt.Errorf("Failed to read from packet, invalid size!")) } return pkt.readRaw(sz) @@ -70,6 +78,8 @@ 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)) + switch field.Type.Kind() { case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to encode it sz, err := strconv.Atoi(field.Tag.Get("size")) @@ -77,24 +87,34 @@ func (pkt *Packet) encodeStructField(field reflect.StructField, value reflect.Va panic(fmt.Errorf("Failed to grab string 'size' tag!!")) } + sz *= 2 buf16 := utf16.Encode([]rune(value.String())) + buf := *(*[]byte)(unsafe.Pointer(&buf16)) - // len(buf16) needs to be the same size as sz - if len(buf16) > sz { + // len(buf) needs to be the same size as sz + if len(buf) > sz { // truncate - buf16 = buf16[:sz] + buf = buf[:sz] } else { // grow - for len(buf16) < sz { - buf16 = append(buf16, 0) + for len(buf) < sz { + buf = append(buf, 0) } } - buf := *(*[]byte)(unsafe.Pointer(&buf16)) + log.Printf("sending %d", len(buf)) pkt.Write(buf) default: pkt.Encode(value.Addr().Interface()) } + + // write padding bytes + pad, err := strconv.Atoi(field.Tag.Get("pad")) + if err == nil { + for i := 0; i < pad; i++ { + pkt.writeByte(0) + } + } } func (pkt *Packet) Encode(data interface{}) { @@ -150,14 +170,6 @@ 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) - // read padding bytes - offset, err := strconv.Atoi(field.Tag.Get("offset")) - if err == nil { - for pkt.cursor < offset { - pkt.readByte() - } - } - switch field.Type.Kind() { case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to decode it sz, err := strconv.Atoi(field.Tag.Get("size")) @@ -186,6 +198,14 @@ func (pkt *Packet) decodeStructField(field reflect.StructField, value reflect.Va default: pkt.Decode(value.Addr().Interface()) } + + // read padding bytes + pad, err := strconv.Atoi(field.Tag.Get("pad")) + if err == nil { + for i := 0; i < pad; i++ { + pkt.readByte() + } + } } func (pkt *Packet) Decode(data interface{}) { diff --git a/protocol/structs.go b/protocol/structs.go index a5be50a..fc48833 100644 --- a/protocol/structs.go +++ b/protocol/structs.go @@ -10,3 +10,8 @@ type SP_CL2LS_REQ_LOGIN struct { Cookie_TEGid [64]uint8 Cookie_authid [255]uint8 } + +type SP_LS2CL_REP_LOGIN_FAIL struct { + ErrorCode int32 + ID string `size:"33" pad:"2"` +} diff --git a/server/client.go b/server/client.go new file mode 100644 index 0000000..e2b9215 --- /dev/null +++ b/server/client.go @@ -0,0 +1,91 @@ +package main + +import ( + "encoding/binary" + "fmt" + "log" + "net" + + "github.com/CPunch/GoFusion/protocol" +) + +type Client struct { + server *Server + conn net.Conn + key []byte +} + +func newClient(server *Server, conn net.Conn, key []byte) *Client { + return &Client{ + server: server, + conn: conn, + key: key, + } +} + +func (client *Client) Send(data interface{}, typeID uint32) { + tmp := make([]byte, 4) + + // encode + pkt := protocol.NewPacket(0) + pkt.Encode(data) + + // write packet size + log.Printf("Sending %#v, sizeof: %d", data, len(pkt.Buf)) + binary.LittleEndian.PutUint32(tmp, uint32(len(pkt.Buf)+4)) + if _, err := client.conn.Write(tmp); err != nil { + panic(fmt.Errorf("[FATAL] failed to write packet size! %v", err)) + } + + // prepend the typeID to the packet body + binary.LittleEndian.PutUint32(tmp, uint32(typeID)) + pkt.Buf = append(tmp, pkt.Buf...) + + // encrypt body + protocol.EncryptData(pkt.Buf[:], client.key) + + // write packet body + if _, err := client.conn.Write(pkt.Buf); err != nil { + panic(fmt.Errorf("[FATAL] failed to write packet body! %v", err)) + } + log.Printf("sent!") +} + +func (client *Client) ClientHandler() { + defer func() { + client.conn.Close() + client.server.unregister <- client + }() + + tmp := make([]byte, 4) + for { + // read packet size + if _, err := client.conn.Read(tmp); err != nil { + panic(fmt.Errorf("[FATAL] failed to read packet size! %v", err)) + } + sz := int(binary.LittleEndian.Uint32(tmp)) + + // read packet body + pkt := protocol.NewPacket(sz) + if _, err := client.conn.Read(pkt.Buf); err != nil { + panic(fmt.Errorf("[FATAL] failed to read packet body! %v", err)) + } + + // decrypt && grab typeID + protocol.DecryptData(pkt.Buf[:], client.key) + typeID := int(binary.LittleEndian.Uint32(pkt.Read(4))) + + log.Printf("Got packet ID: %x, with a sizeof: %d\n", typeID, len(pkt.Buf)) + pkt.ResetCursor() + switch typeID { + case protocol.P_CL2LS_REQ_LOGIN: + var loginPkt protocol.SP_CL2LS_REQ_LOGIN + pkt.Decode(&loginPkt) + log.Printf("Got packet: %#v", loginPkt) + + client.Send(&protocol.SP_LS2CL_REP_LOGIN_FAIL{ErrorCode: protocol.LOGIN_FAIL_VERSION_ERROR, ID: loginPkt.ID}, protocol.P_LS2CL_REP_LOGIN_FAIL) + default: + log.Printf("[WARN] unsupported packet ID: %x\n", typeID) + } + } +} diff --git a/server/server.go b/server/server.go index da09d6f..acd5495 100644 --- a/server/server.go +++ b/server/server.go @@ -1,7 +1,6 @@ package main import ( - "encoding/binary" "fmt" "log" "net" @@ -9,93 +8,49 @@ import ( "github.com/CPunch/GoFusion/protocol" ) -const ( - E_KEY = "m@rQn~W#" - KEY_LENGTH = 8 -) - -func encrypt_byte_change_A(ERSize int, data []byte) int { - var num, num2, num3 int - - for num+ERSize <= len(data) { - num4 := num + num3 - num5 := num + (ERSize - 1 - num3) - - b := data[num4] - data[num4] = data[num5] - data[num5] = b - num += ERSize - num3++ - if num3 > ERSize/2 { - num3 = 0 - } - } - - num2 = ERSize - (num + ERSize - len(data)) - return num + num2 +type Server struct { + listener net.Listener + clients map[*Client]bool + unregister chan *Client } -func xorData(buff, key []byte, size int) { - for i := 0; i < size; i++ { - buff[i] ^= key[i%KEY_LENGTH] +func newServer() *Server { + listener, err := net.Listen("tcp", ":23000") + if err != nil { + log.Fatal(err) + } + + return &Server{ + listener: listener, + clients: make(map[*Client]bool), + unregister: make(chan *Client), } } -func decryptData(buff, key []byte) { - ERSize := len(buff)%(KEY_LENGTH/2+1)*2 + KEY_LENGTH - size := encrypt_byte_change_A(ERSize, buff) - xorData(buff, key, size) -} +func (server *Server) Start() { + log.Print("Server hosted on 127.0.0.1:23000") -func handle(conn net.Conn) { - defer func() { - conn.Close() - }() - - tmp := make([]byte, 4) for { - // grab packet size - if _, err := conn.Read(tmp); err != nil { - panic(fmt.Errorf("[FATAL] failed to read packet size! %v", err)) - } - - sz := int(binary.LittleEndian.Uint32(tmp)) - pkt := protocol.NewPacket(sz) - - if _, err := conn.Read(pkt.Buf); err != nil { - panic(fmt.Errorf("[FATAL] failed to read packet body! %v", err)) - } - - decryptData(pkt.Buf, []byte(E_KEY)) - typeID := int(binary.LittleEndian.Uint32(pkt.Read(4))) - sizeof := len(pkt.Buf) - log.Printf("Got packet ID: %x\n", typeID) - - pkt.ResetCursor() - switch typeID { - case protocol.P_CL2LS_REQ_LOGIN: - var loginPkt protocol.SP_CL2LS_REQ_LOGIN - pkt.Decode(&loginPkt) - log.Printf("Got packet: %#v, with a sizeof: %d", loginPkt, sizeof) + select { + case client := <-server.unregister: + delete(server.clients, client) + fmt.Printf("Client %p disconnected\n", client) default: - log.Printf("[WARN] unsupported packet ID: %x\n", typeID) + conn, err := server.listener.Accept() + if err != nil { + log.Println("Connection error: ", err) + return + } + + client := newClient(server, conn, []byte(protocol.E_KEY)) + server.clients[client] = true + go client.ClientHandler() + fmt.Printf("Client %p connected\n", client) } } } func main() { - server, err := net.Listen("tcp", ":23000") - if err != nil { - log.Fatal(err) - } - - log.Print("Server hosted on 127.0.0.1:23000") - for { - conn, err := server.Accept() - if err != nil { - log.Println("Connection error: ", err) - return - } - go handle(conn) - } + server := newServer() + server.Start() }