can now respond to packets

This commit is contained in:
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"`
}