moved bin 8 out and removed clientctx

moved crypto bin8 out of entrance server

removed unused clientctx

missed import

fix accidental commit and rename ack_helpers
This commit is contained in:
stratic-dev
2024-10-09 09:15:29 +01:00
parent 701d88166f
commit 62fe5cf277
447 changed files with 949 additions and 1385 deletions

View File

@@ -0,0 +1,54 @@
package channelserver
import (
"erupe-ce/network/mhfpacket"
"erupe-ce/utils/byteframe"
)
func doAckEarthSucceed(s *Session, ackHandle uint32, data []*byteframe.ByteFrame) {
bf := byteframe.NewByteFrame()
bf.WriteUint32(uint32(s.server.erupeConfig.EarthID))
bf.WriteUint32(0)
bf.WriteUint32(0)
bf.WriteUint32(uint32(len(data)))
for i := range data {
bf.WriteBytes(data[i].Data())
}
doAckBufSucceed(s, ackHandle, bf.Data())
}
func doAckBufSucceed(s *Session, ackHandle uint32, data []byte) {
s.QueueSendMHF(&mhfpacket.MsgSysAck{
AckHandle: ackHandle,
IsBufferResponse: true,
ErrorCode: 0,
AckData: data,
})
}
func doAckBufFail(s *Session, ackHandle uint32, data []byte) {
s.QueueSendMHF(&mhfpacket.MsgSysAck{
AckHandle: ackHandle,
IsBufferResponse: true,
ErrorCode: 1,
AckData: data,
})
}
func doAckSimpleSucceed(s *Session, ackHandle uint32, data []byte) {
s.QueueSendMHF(&mhfpacket.MsgSysAck{
AckHandle: ackHandle,
IsBufferResponse: false,
ErrorCode: 0,
AckData: data,
})
}
func doAckSimpleFail(s *Session, ackHandle uint32, data []byte) {
s.QueueSendMHF(&mhfpacket.MsgSysAck{
AckHandle: ackHandle,
IsBufferResponse: false,
ErrorCode: 1,
AckData: data,
})
}

View File

@@ -31,54 +31,6 @@ func stubEnumerateNoResults(s *Session, ackHandle uint32) {
doAckBufSucceed(s, ackHandle, enumBf.Data())
}
func doAckEarthSucceed(s *Session, ackHandle uint32, data []*byteframe.ByteFrame) {
bf := byteframe.NewByteFrame()
bf.WriteUint32(uint32(s.server.erupeConfig.EarthID))
bf.WriteUint32(0)
bf.WriteUint32(0)
bf.WriteUint32(uint32(len(data)))
for i := range data {
bf.WriteBytes(data[i].Data())
}
doAckBufSucceed(s, ackHandle, bf.Data())
}
func doAckBufSucceed(s *Session, ackHandle uint32, data []byte) {
s.QueueSendMHF(&mhfpacket.MsgSysAck{
AckHandle: ackHandle,
IsBufferResponse: true,
ErrorCode: 0,
AckData: data,
})
}
func doAckBufFail(s *Session, ackHandle uint32, data []byte) {
s.QueueSendMHF(&mhfpacket.MsgSysAck{
AckHandle: ackHandle,
IsBufferResponse: true,
ErrorCode: 1,
AckData: data,
})
}
func doAckSimpleSucceed(s *Session, ackHandle uint32, data []byte) {
s.QueueSendMHF(&mhfpacket.MsgSysAck{
AckHandle: ackHandle,
IsBufferResponse: false,
ErrorCode: 0,
AckData: data,
})
}
func doAckSimpleFail(s *Session, ackHandle uint32, data []byte) {
s.QueueSendMHF(&mhfpacket.MsgSysAck{
AckHandle: ackHandle,
IsBufferResponse: false,
ErrorCode: 1,
AckData: data,
})
}
func updateRights(s *Session) {
rightsInt := uint32(2)
s.server.db.QueryRow("SELECT rights FROM users u INNER JOIN characters c ON u.id = c.user_id WHERE c.id = $1", s.charID).Scan(&rightsInt)

View File

@@ -188,7 +188,7 @@ func parseChatCommand(s *Session, command string) {
}
temp = &mhfpacket.MsgSysDeleteObject{ObjID: object.id}
deleteNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(deleteNotif, s.clientContext)
temp.Build(deleteNotif)
}
for _, session := range s.server.sessions {
if s == session {
@@ -196,7 +196,7 @@ func parseChatCommand(s *Session, command string) {
}
temp = &mhfpacket.MsgSysDeleteUser{CharID: session.charID}
deleteNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(deleteNotif, s.clientContext)
temp.Build(deleteNotif)
}
deleteNotif.WriteUint16(uint16(network.MSG_SYS_END))
s.QueueSend(deleteNotif.Data())
@@ -208,14 +208,14 @@ func parseChatCommand(s *Session, command string) {
}
temp = &mhfpacket.MsgSysInsertUser{CharID: session.charID}
reloadNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(reloadNotif, s.clientContext)
temp.Build(reloadNotif)
for i := 0; i < 3; i++ {
temp = &mhfpacket.MsgSysNotifyUserBinary{
CharID: session.charID,
BinaryType: uint8(i + 1),
}
reloadNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(reloadNotif, s.clientContext)
temp.Build(reloadNotif)
}
}
for _, obj := range s.stage.objects {
@@ -231,7 +231,7 @@ func parseChatCommand(s *Session, command string) {
OwnerCharID: obj.ownerCharID,
}
reloadNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(reloadNotif, s.clientContext)
temp.Build(reloadNotif)
}
reloadNotif.WriteUint16(uint16(network.MSG_SYS_END))
s.QueueSend(reloadNotif.Data())

View File

@@ -184,7 +184,7 @@ func SendMailNotification(s *Session, m *Mail, recipient *Session) {
RawDataPayload: bf.Data(),
}
castedBinary.Build(bf, s.clientContext)
castedBinary.Build(bf)
recipient.QueueSendMHF(castedBinary)
}

View File

@@ -119,13 +119,13 @@ func (s *Session) notifyRavi() {
raviNotif := byteframe.NewByteFrame()
temp = &mhfpacket.MsgSysNotifyRegister{RegisterID: 0x40000}
raviNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(raviNotif, s.clientContext)
temp.Build(raviNotif)
temp = &mhfpacket.MsgSysNotifyRegister{RegisterID: 0x50000}
raviNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(raviNotif, s.clientContext)
temp.Build(raviNotif)
temp = &mhfpacket.MsgSysNotifyRegister{RegisterID: 0x60000}
raviNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(raviNotif, s.clientContext)
temp.Build(raviNotif)
raviNotif.WriteUint16(0x0010) // End it.
if s.server.erupeConfig.GameplayOptions.LowLatencyRaviente {
for session := range sema.clients {

View File

@@ -78,14 +78,14 @@ func doStageTransfer(s *Session, ackHandle uint32, stageID string) {
}
temp = &mhfpacket.MsgSysInsertUser{CharID: session.charID}
newNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(newNotif, s.clientContext)
temp.Build(newNotif)
for i := 0; i < 3; i++ {
temp = &mhfpacket.MsgSysNotifyUserBinary{
CharID: session.charID,
BinaryType: uint8(i + 1),
}
newNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(newNotif, s.clientContext)
temp.Build(newNotif)
}
}
}
@@ -108,7 +108,7 @@ func doStageTransfer(s *Session, ackHandle uint32, stageID string) {
OwnerCharID: obj.ownerCharID,
}
newNotif.WriteUint16(uint16(temp.Opcode()))
temp.Build(newNotif, s.clientContext)
temp.Build(newNotif)
}
s.stage.RUnlock()
}

View File

@@ -294,7 +294,7 @@ func (s *Server) BroadcastMHF(pkt mhfpacket.MHFPacket, ignoredSession *Session)
bf.WriteUint16(uint16(pkt.Opcode()))
// Build the packet onto the byteframe.
pkt.Build(bf, session.clientContext)
pkt.Build(bf)
// Enqueue in a non-blocking way that drops the packet if the connections send buffer channel is full.
session.QueueSendNonBlocking(bf.Data())

View File

@@ -51,7 +51,7 @@ func (s *Semaphore) BroadcastMHF(pkt mhfpacket.MHFPacket, ignoredSession *Sessio
bf.WriteUint16(uint16(pkt.Opcode()))
// Build the packet onto the byteframe.
pkt.Build(bf, session.clientContext)
pkt.Build(bf)
// Enqueue in a non-blocking way that drops the packet if the connections send buffer channel is full.
session.QueueSendNonBlocking(bf.Data())

View File

@@ -13,7 +13,6 @@ import (
"time"
"erupe-ce/network"
"erupe-ce/network/clientctx"
"erupe-ce/network/mhfpacket"
"erupe-ce/utils/byteframe"
"erupe-ce/utils/stringstack"
@@ -29,13 +28,12 @@ type packet struct {
// Session holds state for the channel server connection.
type Session struct {
sync.Mutex
logger *zap.Logger
server *Server
rawConn net.Conn
cryptConn *network.CryptConn
sendPackets chan packet
clientContext *clientctx.ClientContext
lastPacket time.Time
logger *zap.Logger
server *Server
rawConn net.Conn
cryptConn *network.CryptConn
sendPackets chan packet
lastPacket time.Time
objectIndex uint16
userEnteredStage bool // If the user has entered a stage before
@@ -79,7 +77,6 @@ func NewSession(server *Server, conn net.Conn) *Session {
rawConn: conn,
cryptConn: network.NewCryptConn(conn),
sendPackets: make(chan packet, 20),
clientContext: &clientctx.ClientContext{}, // Unused
lastPacket: time.Now(),
sessionStart: gametime.TimeAdjusted().Unix(),
stageMoveStack: stringstack.New(),
@@ -138,7 +135,7 @@ func (s *Session) QueueSendMHF(pkt mhfpacket.MHFPacket) {
bf.WriteUint16(uint16(pkt.Opcode()))
// Build the packet onto the byteframe.
pkt.Build(bf, s.clientContext)
pkt.Build(bf)
// Queue it.
s.QueueSend(bf.Data())
@@ -231,7 +228,7 @@ func (s *Session) handlePacketGroup(pktGroup []byte) {
return
}
// Parse the packet.
err := mhfPkt.Parse(bf, s.clientContext)
err := mhfPkt.Parse(bf)
if err != nil {
fmt.Printf("\n!!! [%s] %s NOT IMPLEMENTED !!! \n\n\n", s.Name, opcode)
return

View File

@@ -78,7 +78,7 @@ func (s *Stage) BroadcastMHF(pkt mhfpacket.MHFPacket, ignoredSession *Session) {
bf.WriteUint16(uint16(pkt.Opcode()))
// Build the packet onto the byteframe.
pkt.Build(bf, session.clientContext)
pkt.Build(bf)
// Enqueue in a non-blocking way that drops the packet if the connections send buffer channel is full.
session.QueueSendNonBlocking(bf.Data())

View File

@@ -1,51 +0,0 @@
package entranceserver
import (
"encoding/binary"
)
var (
_bin8Key = []byte{0x01, 0x23, 0x34, 0x45, 0x56, 0xAB, 0xCD, 0xEF}
_sum32Table0 = []byte{0x35, 0x7A, 0xAA, 0x97, 0x53, 0x66, 0x12}
_sum32Table1 = []byte{0x7A, 0xAA, 0x97, 0x53, 0x66, 0x12, 0xDE, 0xDE, 0x35}
)
// CalcSum32 calculates the custom MHF "sum32" checksum of the given data.
func CalcSum32(data []byte) uint32 {
tableIdx0 := (len(data) + 1) & 0xFF
tableIdx1 := int((data[len(data)>>1] + 1) & 0xFF)
out := make([]byte, 4)
for i := 0; i < len(data); i++ {
key := data[i] ^ _sum32Table0[(tableIdx0+i)%7] ^ _sum32Table1[(tableIdx1+i)%9]
out[i&3] = (out[i&3] + key) & 0xFF
}
return binary.BigEndian.Uint32(out)
}
func rotate(k *uint32) {
*k = uint32(((54323 * uint(*k)) + 1) & 0xFFFFFFFF)
}
// EncryptBin8 encrypts the given data using MHF's "binary8" encryption.
func EncryptBin8(data []byte, key byte) []byte {
_key := uint32(key)
var output []byte
for i := 0; i < len(data); i++ {
rotate(&_key)
tmp := _bin8Key[i&7] ^ byte((_key>>13)&0xFF)
output = append(output, data[i]^tmp)
}
return output
}
// DecryptBin8 decrypts the given MHF "binary8" data.
func DecryptBin8(data []byte, key byte) []byte {
_key := uint32(key)
var output []byte
for i := 0; i < len(data); i++ {
rotate(&_key)
tmp := data[i] ^ byte((_key>>13)&0xFF)
output = append(output, tmp^_bin8Key[i&7])
}
return output
}

View File

@@ -1,73 +0,0 @@
package entranceserver
import (
"bytes"
"encoding/hex"
"fmt"
"testing"
)
var tests = []struct {
data []byte
sum uint32
encryptedData []byte
encryptionKey byte
}{
{
[]byte{0x4C, 0x6F, 0x72, 0x65, 0x6D, 0x20},
0xAE6CA2C,
[]byte{0x7E, 0x4C, 0x1D, 0x16, 0x9D, 0x46},
0x55,
},
{
[]byte{0x69, 0x70, 0x73, 0x75, 0x6D, 0x20, 0x64, 0x6F, 0x6C, 0x6F, 0x72, 0x20, 0x73, 0x69, 0x74, 0x20, 0x61, 0x6D, 0x65, 0x74, 0x2C, 0x20},
0xCE5F1E96,
[]byte{0x41, 0x65, 0xFF, 0x74, 0x64, 0x45, 0xB8, 0xB1, 0x18, 0xB0, 0x94, 0xA3, 0xF8, 0xD, 0xBF, 0x3C, 0xC8, 0x24, 0xE2, 0xEC, 0x3B, 0xCE},
0x7A,
},
{
[]byte{0x63, 0x6F, 0x6E, 0x73, 0x65, 0x63, 0x74, 0x65, 0x74, 0x75, 0x72, 0x20, 0x61, 0x64, 0x69, 0x70, 0x69, 0x73, 0x63, 0x69, 0x6E, 0x67, 0x20, 0x65, 0x6C, 0x69, 0x74, 0x2C, 0x20},
0xF3EECEBB,
[]byte{0xE, 0xBB, 0x19, 0xA5, 0xB9, 0x34, 0xFE, 0x51, 0x0, 0x61, 0x2D, 0x38, 0xB2, 0x98, 0xC2, 0xE0, 0x17, 0xDE, 0x6E, 0xE3, 0x6C, 0x1E, 0x19, 0xB6, 0x8C, 0x57, 0x32, 0x32, 0xD8},
0xF8,
},
}
func TestSum32(t *testing.T) {
for k, test := range tests {
testname := fmt.Sprintf("sum32_test_%d", k)
t.Run(testname, func(t *testing.T) {
gotSum := CalcSum32(test.data)
if gotSum != test.sum {
t.Errorf("got sum32 0x%X, want 0x%X", gotSum, test.sum)
}
})
}
}
func TestEncryptBin8(t *testing.T) {
for k, test := range tests {
testname := fmt.Sprintf("encrypt_bin8_test_%d", k)
t.Run(testname, func(t *testing.T) {
gotEncData := EncryptBin8(test.data, test.encryptionKey)
if !bytes.Equal(gotEncData, test.encryptedData) {
t.Errorf("got\n\t%s\nwant\n\t%s", hex.Dump(gotEncData), hex.Dump(test.encryptedData))
}
})
}
}
func TestDecryptBin8(t *testing.T) {
for k, test := range tests {
testname := fmt.Sprintf("decrypt_bin8_test_%d", k)
t.Run(testname, func(t *testing.T) {
gotDecData := DecryptBin8(test.encryptedData, test.encryptionKey)
if !bytes.Equal(gotDecData, test.data) {
t.Errorf("got\n\t%s\nwant\n\t%s", hex.Dump(gotDecData), hex.Dump(test.data))
}
})
}
}

View File

@@ -4,12 +4,12 @@ import (
"encoding/binary"
"encoding/hex"
_config "erupe-ce/config"
"erupe-ce/network/crypto/bin8"
"erupe-ce/utils/byteframe"
"erupe-ce/utils/gametime"
"erupe-ce/utils/stringsupport"
"fmt"
"net"
"erupe-ce/utils/byteframe"
)
func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
@@ -96,7 +96,7 @@ func makeHeader(data []byte, respType string, entryCount uint16, key byte) []byt
bf.WriteUint16(entryCount)
bf.WriteUint16(uint16(len(data)))
if len(data) > 0 {
bf.WriteUint32(CalcSum32(data))
bf.WriteUint32(bin8.CalcSum32(data))
bf.WriteBytes(data)
}
@@ -104,7 +104,7 @@ func makeHeader(data []byte, respType string, entryCount uint16, key byte) []byt
bf = byteframe.NewByteFrame()
bf.WriteUint8(key)
bf.WriteBytes(EncryptBin8(dataToEncrypt, key))
bf.WriteBytes(bin8.EncryptBin8(dataToEncrypt, key))
return bf.Data()
}