can now respond to packets

This commit is contained in:
CPunch 2023-03-05 02:14:36 -06:00
parent 34ce9a7420
commit 115005b6a3
6 changed files with 219 additions and 94 deletions

View File

@ -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

45
protocol/encrypt.go Normal file
View File

@ -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)
}

View File

@ -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{}) {

View File

@ -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"`
}

91
server/client.go Normal file
View File

@ -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)
}
}
}

View File

@ -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
)
type Server struct {
listener net.Listener
clients map[*Client]bool
unregister chan *Client
}
func encrypt_byte_change_A(ERSize int, data []byte) int {
var num, num2, num3 int
func newServer() *Server {
listener, err := net.Listen("tcp", ":23000")
if err != nil {
log.Fatal(err)
}
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
return &Server{
listener: listener,
clients: make(map[*Client]bool),
unregister: make(chan *Client),
}
}
num2 = ERSize - (num + ERSize - len(data))
return num + num2
}
func (server *Server) Start() {
log.Print("Server hosted on 127.0.0.1:23000")
func xorData(buff, key []byte, size int) {
for i := 0; i < size; i++ {
buff[i] ^= key[i%KEY_LENGTH]
}
}
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 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()
}