mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-14 16:04:38 +01:00
Merge remote-tracking branch 'origin/main' into fix/chat-commands-args
# Conflicts: # server/channelserver/handlers_cast_binary.go
This commit is contained in:
@@ -111,7 +111,12 @@ func handleMsgSysTerminalLog(s *Session, p mhfpacket.MHFPacket) {
|
||||
s.server.logger.Info("SysTerminalLog",
|
||||
zap.Uint8("Type1", pkt.Entries[i].Type1),
|
||||
zap.Uint8("Type2", pkt.Entries[i].Type2),
|
||||
zap.Int16s("Data", pkt.Entries[i].Data))
|
||||
zap.Int16("Unk0", pkt.Entries[i].Unk0),
|
||||
zap.Int32("Unk1", pkt.Entries[i].Unk1),
|
||||
zap.Int32("Unk2", pkt.Entries[i].Unk2),
|
||||
zap.Int32("Unk3", pkt.Entries[i].Unk3),
|
||||
zap.Int32s("Unk4", pkt.Entries[i].Unk4),
|
||||
)
|
||||
}
|
||||
resp := byteframe.NewByteFrame()
|
||||
resp.WriteUint32(pkt.LogID + 1) // LogID to use for requests after this.
|
||||
@@ -177,6 +182,7 @@ func logoutPlayer(s *Session) {
|
||||
delete(s.server.sessions, s.rawConn)
|
||||
}
|
||||
s.rawConn.Close()
|
||||
delete(s.server.objectIDs, s)
|
||||
s.server.Unlock()
|
||||
|
||||
for _, stage := range s.server.stages {
|
||||
@@ -266,13 +272,12 @@ func handleMsgSysPing(s *Session, p mhfpacket.MHFPacket) {
|
||||
}
|
||||
|
||||
func handleMsgSysTime(s *Session, p mhfpacket.MHFPacket) {
|
||||
//pkt := p.(*mhfpacket.MsgSysTime)
|
||||
|
||||
resp := &mhfpacket.MsgSysTime{
|
||||
GetRemoteTime: false,
|
||||
Timestamp: uint32(TimeAdjusted().Unix()), // JP timezone
|
||||
}
|
||||
s.QueueSendMHF(resp)
|
||||
s.notifyRavi()
|
||||
}
|
||||
|
||||
func handleMsgSysIssueLogkey(s *Session, p mhfpacket.MHFPacket) {
|
||||
@@ -1691,7 +1696,14 @@ func handleMsgMhfGetEarthValue(s *Session, p mhfpacket.MHFPacket) {
|
||||
|
||||
func handleMsgMhfDebugPostValue(s *Session, p mhfpacket.MHFPacket) {}
|
||||
|
||||
func handleMsgMhfGetRandFromTable(s *Session, p mhfpacket.MHFPacket) {}
|
||||
func handleMsgMhfGetRandFromTable(s *Session, p mhfpacket.MHFPacket) {
|
||||
pkt := p.(*mhfpacket.MsgMhfGetRandFromTable)
|
||||
bf := byteframe.NewByteFrame()
|
||||
for i := uint16(0); i < pkt.Results; i++ {
|
||||
bf.WriteUint32(0)
|
||||
}
|
||||
doAckBufSucceed(s, pkt.AckHandle, bf.Data())
|
||||
}
|
||||
|
||||
func handleMsgMhfGetSenyuDailyCount(s *Session, p mhfpacket.MHFPacket) {
|
||||
pkt := p.(*mhfpacket.MsgMhfGetSenyuDailyCount)
|
||||
@@ -1847,61 +1859,49 @@ func handleMsgMhfGetDailyMissionPersonal(s *Session, p mhfpacket.MHFPacket) {}
|
||||
|
||||
func handleMsgMhfSetDailyMissionPersonal(s *Session, p mhfpacket.MHFPacket) {}
|
||||
|
||||
func equipSkinHistSize() int {
|
||||
size := 3200
|
||||
if _config.ErupeConfig.RealClientMode <= _config.Z2 {
|
||||
size = 2560
|
||||
}
|
||||
if _config.ErupeConfig.RealClientMode <= _config.Z1 {
|
||||
size = 1280
|
||||
}
|
||||
return size
|
||||
}
|
||||
|
||||
func handleMsgMhfGetEquipSkinHist(s *Session, p mhfpacket.MHFPacket) {
|
||||
pkt := p.(*mhfpacket.MsgMhfGetEquipSkinHist)
|
||||
// Transmog / reskin system, bitmask of 3200 bytes length
|
||||
// presumably divided by 5 sections for 5120 armour IDs covered
|
||||
// +10,000 for actual ID to be unlocked by each bit
|
||||
// Returning 3200 bytes of FF just unlocks everything for now
|
||||
size := equipSkinHistSize()
|
||||
var data []byte
|
||||
err := s.server.db.QueryRow("SELECT COALESCE(skin_hist::bytea, $2::bytea) FROM characters WHERE id = $1", s.charID, make([]byte, 0xC80)).Scan(&data)
|
||||
err := s.server.db.QueryRow("SELECT COALESCE(skin_hist::bytea, $2::bytea) FROM characters WHERE id = $1", s.charID, make([]byte, size)).Scan(&data)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to load skin_hist", zap.Error(err))
|
||||
data = make([]byte, 3200)
|
||||
data = make([]byte, size)
|
||||
}
|
||||
doAckBufSucceed(s, pkt.AckHandle, data)
|
||||
}
|
||||
|
||||
func handleMsgMhfUpdateEquipSkinHist(s *Session, p mhfpacket.MHFPacket) {
|
||||
pkt := p.(*mhfpacket.MsgMhfUpdateEquipSkinHist)
|
||||
// sends a raw armour ID back that needs to be mapped into the persistent bitmask above (-10,000)
|
||||
size := equipSkinHistSize()
|
||||
var data []byte
|
||||
err := s.server.db.QueryRow("SELECT COALESCE(skin_hist, $2) FROM characters WHERE id = $1", s.charID, make([]byte, 0xC80)).Scan(&data)
|
||||
err := s.server.db.QueryRow("SELECT COALESCE(skin_hist, $2) FROM characters WHERE id = $1", s.charID, make([]byte, size)).Scan(&data)
|
||||
if err != nil {
|
||||
s.logger.Error("Failed to save skin_hist", zap.Error(err))
|
||||
doAckSimpleSucceed(s, pkt.AckHandle, []byte{0x00, 0x00, 0x00, 0x00})
|
||||
s.logger.Error("Failed to get skin_hist", zap.Error(err))
|
||||
doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4))
|
||||
return
|
||||
}
|
||||
|
||||
var bit int
|
||||
var startByte int
|
||||
switch pkt.MogType {
|
||||
case 0: // legs
|
||||
bit = int(pkt.ArmourID) - 10000
|
||||
startByte = 0
|
||||
case 1:
|
||||
bit = int(pkt.ArmourID) - 10000
|
||||
startByte = 640
|
||||
case 2:
|
||||
bit = int(pkt.ArmourID) - 10000
|
||||
startByte = 1280
|
||||
case 3:
|
||||
bit = int(pkt.ArmourID) - 10000
|
||||
startByte = 1920
|
||||
case 4:
|
||||
bit = int(pkt.ArmourID) - 10000
|
||||
startByte = 2560
|
||||
}
|
||||
bit := int(pkt.ArmourID) - 10000
|
||||
startByte := (size / 5) * int(pkt.MogType)
|
||||
// psql set_bit could also work but I couldn't get it working
|
||||
byteInd := (bit / 8)
|
||||
byteInd := bit / 8
|
||||
bitInByte := bit % 8
|
||||
data[startByte+byteInd] |= bits.Reverse8((1 << uint(bitInByte)))
|
||||
data[startByte+byteInd] |= bits.Reverse8(1 << uint(bitInByte))
|
||||
dumpSaveData(s, data, "skinhist")
|
||||
_, err = s.server.db.Exec("UPDATE characters SET skin_hist=$1 WHERE id=$2", data, s.charID)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
doAckSimpleSucceed(s, pkt.AckHandle, []byte{0x00, 0x00, 0x00, 0x00})
|
||||
s.server.db.Exec("UPDATE characters SET skin_hist=$1 WHERE id=$2", data, s.charID)
|
||||
doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4))
|
||||
}
|
||||
|
||||
func handleMsgMhfGetUdShopCoin(s *Session, p mhfpacket.MHFPacket) {
|
||||
@@ -1945,20 +1945,45 @@ func handleMsgMhfGetLobbyCrowd(s *Session, p mhfpacket.MHFPacket) {
|
||||
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 0x320))
|
||||
}
|
||||
|
||||
type TrendWeapon struct {
|
||||
WeaponType uint8
|
||||
WeaponID uint16
|
||||
}
|
||||
|
||||
func handleMsgMhfGetTrendWeapon(s *Session, p mhfpacket.MHFPacket) {
|
||||
pkt := p.(*mhfpacket.MsgMhfGetTrendWeapon)
|
||||
// TODO (Fist): Work out actual format limitations, seems to be final upgrade
|
||||
// for weapons and it traverses its upgrade tree to recommend base as final
|
||||
// 423C correlates with most popular magnet spike in use on JP
|
||||
// 2A 00 3C 44 00 3C 76 00 3F EA 01 0F 20 01 0F 50 01 0F F8 02 3C 7E 02 3D
|
||||
// F3 02 40 2A 03 3D 65 03 3F 2A 03 40 36 04 3D 59 04 41 E7 04 43 3E 05 0A
|
||||
// ED 05 0F 4C 05 0F F2 06 3A FE 06 41 E8 06 41 FA 07 3B 02 07 3F ED 07 40
|
||||
// 24 08 3D 37 08 3F 66 08 41 EC 09 3D 38 09 3F 8A 09 41 EE 0A 0E 78 0A 0F
|
||||
// AA 0A 0F F9 0B 3E 2E 0B 41 EF 0B 42 FB 0C 41 F0 0C 43 3F 0C 43 EE 0D 41 F1 0D 42 10 0D 42 3C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||||
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 0xA9))
|
||||
trendWeapons := [14][3]TrendWeapon{}
|
||||
for i := uint8(0); i < 14; i++ {
|
||||
rows, err := s.server.db.Query(`SELECT weapon_id FROM trend_weapons WHERE weapon_type=$1 ORDER BY count DESC LIMIT 3`, i)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
j := 0
|
||||
for rows.Next() {
|
||||
trendWeapons[i][j].WeaponType = i
|
||||
rows.Scan(&trendWeapons[i][j].WeaponID)
|
||||
j++
|
||||
}
|
||||
}
|
||||
|
||||
x := uint8(0)
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteUint8(0)
|
||||
for _, weaponType := range trendWeapons {
|
||||
for _, weapon := range weaponType {
|
||||
bf.WriteUint8(weapon.WeaponType)
|
||||
bf.WriteUint16(weapon.WeaponID)
|
||||
x++
|
||||
}
|
||||
}
|
||||
bf.Seek(0, 0)
|
||||
bf.WriteUint8(x)
|
||||
doAckBufSucceed(s, pkt.AckHandle, bf.Data())
|
||||
}
|
||||
|
||||
func handleMsgMhfUpdateUseTrendWeaponLog(s *Session, p mhfpacket.MHFPacket) {
|
||||
pkt := p.(*mhfpacket.MsgMhfUpdateUseTrendWeaponLog)
|
||||
doAckSimpleSucceed(s, pkt.AckHandle, []byte{0x00, 0x00, 0x00, 0x00})
|
||||
s.server.db.Exec(`INSERT INTO trend_weapons (weapon_id, weapon_type, count) VALUES ($1, $2, 1) ON CONFLICT (weapon_id) DO
|
||||
UPDATE SET count = trend_weapons.count+1`, pkt.WeaponID, pkt.WeaponType)
|
||||
doAckSimpleSucceed(s, pkt.AckHandle, make([]byte, 4))
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"erupe-ce/common/byteframe"
|
||||
ps "erupe-ce/common/pascalstring"
|
||||
"erupe-ce/common/stringsupport"
|
||||
_config "erupe-ce/config"
|
||||
"erupe-ce/network/mhfpacket"
|
||||
"time"
|
||||
)
|
||||
@@ -67,8 +68,10 @@ func handleMsgMhfEnumerateCampaign(s *Session, p mhfpacket.MHFPacket) {
|
||||
bf.WriteInt16(event.MaxHR)
|
||||
bf.WriteInt16(event.MinSR)
|
||||
bf.WriteInt16(event.MaxSR)
|
||||
bf.WriteInt16(event.MinGR)
|
||||
bf.WriteInt16(event.MaxGR)
|
||||
if _config.ErupeConfig.RealClientMode >= _config.G3 {
|
||||
bf.WriteInt16(event.MinGR)
|
||||
bf.WriteInt16(event.MaxGR)
|
||||
}
|
||||
bf.WriteUint16(event.Unk1)
|
||||
bf.WriteUint8(event.Unk2)
|
||||
bf.WriteUint8(event.Unk3)
|
||||
|
||||
@@ -88,9 +88,15 @@ func parseChatCommand(s *Session, command string) {
|
||||
case commands["PSN"].Prefix:
|
||||
if commands["PSN"].Enabled {
|
||||
if len(args) > 1 {
|
||||
_, err := s.server.db.Exec(`UPDATE users u SET psn_id=$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)`, args[1], s.charID)
|
||||
if err == nil {
|
||||
sendServerChatMessage(s, fmt.Sprintf(s.server.dict["commandPSNSuccess"], args[1]))
|
||||
var exists int
|
||||
s.server.db.QueryRow(`SELECT count(*) FROM users WHERE psn_id = $1`, id).Scan(&exists)
|
||||
if exists == 0 {
|
||||
_, err := s.server.db.Exec(`UPDATE users u SET psn_id=$1 WHERE u.id=(SELECT c.user_id FROM characters c WHERE c.id=$2)`, args[1], s.charID)
|
||||
if err == nil {
|
||||
sendServerChatMessage(s, fmt.Sprintf(s.server.dict["commandPSNSuccess"], args[1]))
|
||||
}
|
||||
} else {
|
||||
sendServerChatMessage(s, s.server.dict["commandPSNExists"])
|
||||
}
|
||||
} else {
|
||||
sendServerChatMessage(s, fmt.Sprintf(s.server.dict["commandPSNError"], commands["PSN"].Prefix))
|
||||
|
||||
@@ -239,19 +239,19 @@ func dumpSaveData(s *Session, data []byte, suffix string) {
|
||||
_, err := os.Stat(dir)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
err = os.Mkdir(dir, os.ModePerm)
|
||||
err = os.MkdirAll(dir, os.ModePerm)
|
||||
if err != nil {
|
||||
s.logger.Warn("Error dumping savedata, could not create folder")
|
||||
s.logger.Error("Error dumping savedata, could not create folder")
|
||||
return
|
||||
}
|
||||
} else {
|
||||
s.logger.Warn("Error dumping savedata")
|
||||
s.logger.Error("Error dumping savedata")
|
||||
return
|
||||
}
|
||||
}
|
||||
err = os.WriteFile(path, data, 0644)
|
||||
if err != nil {
|
||||
s.logger.Warn("Error dumping savedata, could not write file", zap.Error(err))
|
||||
s.logger.Error("Error dumping savedata, could not write file", zap.Error(err))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"erupe-ce/common/byteframe"
|
||||
ps "erupe-ce/common/pascalstring"
|
||||
"erupe-ce/network/mhfpacket"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
|
||||
@@ -49,14 +49,14 @@ func handleMsgMhfReleaseEvent(s *Session, p mhfpacket.MHFPacket) {
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
Unk0 uint16
|
||||
Unk1 uint16
|
||||
Unk2 uint16
|
||||
Unk3 uint16
|
||||
Unk4 uint16
|
||||
Unk5 uint32
|
||||
Unk6 uint32
|
||||
Unk7 []uint16
|
||||
EventType uint16
|
||||
Unk1 uint16
|
||||
Unk2 uint16
|
||||
Unk3 uint16
|
||||
Unk4 uint16
|
||||
Unk5 uint32
|
||||
Unk6 uint32
|
||||
QuestFileIDs []uint16
|
||||
}
|
||||
|
||||
func handleMsgMhfEnumerateEvent(s *Session, p mhfpacket.MHFPacket) {
|
||||
@@ -67,17 +67,17 @@ func handleMsgMhfEnumerateEvent(s *Session, p mhfpacket.MHFPacket) {
|
||||
|
||||
bf.WriteUint8(uint8(len(events)))
|
||||
for _, event := range events {
|
||||
bf.WriteUint16(event.Unk0)
|
||||
bf.WriteUint16(event.EventType)
|
||||
bf.WriteUint16(event.Unk1)
|
||||
bf.WriteUint16(event.Unk2)
|
||||
bf.WriteUint16(event.Unk3)
|
||||
bf.WriteUint16(event.Unk4)
|
||||
bf.WriteUint32(event.Unk5)
|
||||
bf.WriteUint32(event.Unk6)
|
||||
if event.Unk0 == 2 {
|
||||
bf.WriteUint8(uint8(len(event.Unk7)))
|
||||
for _, u := range event.Unk7 {
|
||||
bf.WriteUint16(u)
|
||||
if event.EventType == 2 {
|
||||
bf.WriteUint8(uint8(len(event.QuestFileIDs)))
|
||||
for _, qf := range event.QuestFileIDs {
|
||||
bf.WriteUint16(qf)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -123,24 +123,24 @@ func handleMsgMhfGetWeeklySchedule(s *Session, p mhfpacket.MHFPacket) {
|
||||
}
|
||||
|
||||
func generateFeatureWeapons(count int) activeFeature {
|
||||
max := 14
|
||||
_max := 14
|
||||
if _config.ErupeConfig.RealClientMode < _config.ZZ {
|
||||
max = 13
|
||||
_max = 13
|
||||
}
|
||||
if _config.ErupeConfig.RealClientMode < _config.G10 {
|
||||
max = 12
|
||||
_max = 12
|
||||
}
|
||||
if _config.ErupeConfig.RealClientMode < _config.GG {
|
||||
max = 11
|
||||
_max = 11
|
||||
}
|
||||
if count > max {
|
||||
count = max
|
||||
if count > _max {
|
||||
count = _max
|
||||
}
|
||||
nums := make([]int, 0)
|
||||
var result int
|
||||
for len(nums) < count {
|
||||
rng := token.RNG()
|
||||
num := rng.Intn(max)
|
||||
num := rng.Intn(_max)
|
||||
exist := false
|
||||
for _, v := range nums {
|
||||
if v == num {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"erupe-ce/common/byteframe"
|
||||
ps "erupe-ce/common/pascalstring"
|
||||
"erupe-ce/common/token"
|
||||
_config "erupe-ce/config"
|
||||
"erupe-ce/network/mhfpacket"
|
||||
"sort"
|
||||
"time"
|
||||
@@ -265,7 +266,14 @@ func handleMsgMhfInfoFesta(s *Session, p mhfpacket.MHFPacket) {
|
||||
bf.WriteUint8(reward.Unk7)
|
||||
}
|
||||
|
||||
bf.WriteUint32(s.server.erupeConfig.GameplayOptions.MaximumFP)
|
||||
if _config.ErupeConfig.RealClientMode <= _config.G61 {
|
||||
if s.server.erupeConfig.GameplayOptions.MaximumFP > 0xFFFF {
|
||||
s.server.erupeConfig.GameplayOptions.MaximumFP = 0xFFFF
|
||||
}
|
||||
bf.WriteUint16(uint16(s.server.erupeConfig.GameplayOptions.MaximumFP))
|
||||
} else {
|
||||
bf.WriteUint32(s.server.erupeConfig.GameplayOptions.MaximumFP)
|
||||
}
|
||||
bf.WriteUint16(500)
|
||||
|
||||
categoryWinners := uint16(0) // NYI
|
||||
|
||||
@@ -10,25 +10,9 @@ import (
|
||||
func handleMsgSysCreateObject(s *Session, p mhfpacket.MHFPacket) {
|
||||
pkt := p.(*mhfpacket.MsgSysCreateObject)
|
||||
|
||||
// Prevent reusing an object index
|
||||
var nextID uint32
|
||||
for {
|
||||
exists := false
|
||||
nextID = s.stage.NextObjectID()
|
||||
for _, object := range s.stage.objects {
|
||||
if object.id == nextID {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if exists == false {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
s.stage.Lock()
|
||||
newObj := &Object{
|
||||
id: nextID,
|
||||
id: s.NextObjectID(),
|
||||
ownerCharID: s.charID,
|
||||
x: pkt.X,
|
||||
y: pkt.Y,
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
package channelserver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"erupe-ce/common/byteframe"
|
||||
"erupe-ce/common/decryption"
|
||||
ps "erupe-ce/common/pascalstring"
|
||||
_config "erupe-ce/config"
|
||||
"erupe-ce/network/mhfpacket"
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func handleMsgSysGetFile(s *Session, p mhfpacket.MHFPacket) {
|
||||
@@ -35,29 +40,47 @@ func handleMsgSysGetFile(s *Session, p mhfpacket.MHFPacket) {
|
||||
}
|
||||
doAckBufSucceed(s, pkt.AckHandle, data)
|
||||
} else {
|
||||
if _, err := os.Stat(filepath.Join(s.server.erupeConfig.BinPath, "quest_override.bin")); err == nil {
|
||||
data, err := os.ReadFile(filepath.Join(s.server.erupeConfig.BinPath, "quest_override.bin"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
doAckBufSucceed(s, pkt.AckHandle, data)
|
||||
} else {
|
||||
if s.server.erupeConfig.DevModeOptions.QuestDebugTools && s.server.erupeConfig.DevMode {
|
||||
s.logger.Debug(
|
||||
"Quest",
|
||||
zap.String("Filename", pkt.Filename),
|
||||
)
|
||||
}
|
||||
// Get quest file.
|
||||
data, err := os.ReadFile(filepath.Join(s.server.erupeConfig.BinPath, fmt.Sprintf("quests/%s.bin", pkt.Filename)))
|
||||
if err != nil {
|
||||
s.logger.Error(fmt.Sprintf("Failed to open file: %s/quests/%s.bin", s.server.erupeConfig.BinPath, pkt.Filename))
|
||||
// This will crash the game.
|
||||
doAckBufSucceed(s, pkt.AckHandle, data)
|
||||
return
|
||||
}
|
||||
doAckBufSucceed(s, pkt.AckHandle, data)
|
||||
if s.server.erupeConfig.DevModeOptions.QuestDebugTools && s.server.erupeConfig.DevMode {
|
||||
s.logger.Debug(
|
||||
"Quest",
|
||||
zap.String("Filename", pkt.Filename),
|
||||
)
|
||||
}
|
||||
|
||||
if s.server.erupeConfig.GameplayOptions.SeasonOverride {
|
||||
pkt.Filename = seasonConversion(s, pkt.Filename)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(filepath.Join(s.server.erupeConfig.BinPath, fmt.Sprintf("quests/%s.bin", pkt.Filename)))
|
||||
if err != nil {
|
||||
s.logger.Error(fmt.Sprintf("Failed to open file: %s/quests/%s.bin", s.server.erupeConfig.BinPath, pkt.Filename))
|
||||
// This will crash the game.
|
||||
doAckBufSucceed(s, pkt.AckHandle, data)
|
||||
return
|
||||
}
|
||||
doAckBufSucceed(s, pkt.AckHandle, data)
|
||||
}
|
||||
}
|
||||
|
||||
func questSuffix(s *Session) string {
|
||||
// Determine the letter to append for day / night
|
||||
var timeSet string
|
||||
if TimeGameAbsolute() > 2880 {
|
||||
timeSet = "d"
|
||||
} else {
|
||||
timeSet = "n"
|
||||
}
|
||||
return fmt.Sprintf("%s%d", timeSet, s.server.Season())
|
||||
}
|
||||
|
||||
func seasonConversion(s *Session, questFile string) string {
|
||||
filename := fmt.Sprintf("%s%s", questFile[:5], questSuffix(s))
|
||||
|
||||
// Return original file if file doesn't exist
|
||||
if _, err := os.Stat(filename); err == nil {
|
||||
return filename
|
||||
} else {
|
||||
return questFile
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,34 +102,125 @@ func handleMsgMhfSaveFavoriteQuest(s *Session, p mhfpacket.MHFPacket) {
|
||||
doAckSimpleSucceed(s, pkt.AckHandle, []byte{0x00, 0x00, 0x00, 0x00})
|
||||
}
|
||||
|
||||
func loadQuestFile(s *Session, questId int) []byte {
|
||||
file, err := os.ReadFile(filepath.Join(s.server.erupeConfig.BinPath, fmt.Sprintf("quests/%05dd0.bin", questId)))
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
decrypted := decryption.UnpackSimple(file)
|
||||
fileBytes := byteframe.NewByteFrameFromBytes(decrypted)
|
||||
fileBytes.SetLE()
|
||||
fileBytes.Seek(int64(fileBytes.ReadUint32()), 0)
|
||||
|
||||
// The 320 bytes directly following the data pointer must go directly into the event's body, after the header and before the string pointers.
|
||||
questBody := byteframe.NewByteFrameFromBytes(fileBytes.ReadBytes(320))
|
||||
questBody.SetLE()
|
||||
// Find the master quest string pointer
|
||||
questBody.Seek(40, 0)
|
||||
fileBytes.Seek(int64(questBody.ReadUint32()), 0)
|
||||
questBody.Seek(40, 0)
|
||||
// Overwrite it
|
||||
questBody.WriteUint32(320)
|
||||
questBody.Seek(0, 2)
|
||||
|
||||
// Rewrite the quest strings and their pointers
|
||||
var tempString []byte
|
||||
newStrings := byteframe.NewByteFrame()
|
||||
tempPointer := 352
|
||||
for i := 0; i < 8; i++ {
|
||||
questBody.WriteUint32(uint32(tempPointer))
|
||||
temp := int64(fileBytes.Index())
|
||||
fileBytes.Seek(int64(fileBytes.ReadUint32()), 0)
|
||||
tempString = fileBytes.ReadNullTerminatedBytes()
|
||||
fileBytes.Seek(temp+4, 0)
|
||||
tempPointer += len(tempString) + 1
|
||||
newStrings.WriteNullTerminatedBytes(tempString)
|
||||
}
|
||||
questBody.WriteBytes(newStrings.Data())
|
||||
|
||||
return questBody.Data()
|
||||
}
|
||||
|
||||
func makeEventQuest(s *Session, rows *sql.Rows) ([]byte, error) {
|
||||
var id, mark uint32
|
||||
var questId int
|
||||
var maxPlayers, questType uint8
|
||||
rows.Scan(&id, &maxPlayers, &questType, &questId, &mark)
|
||||
|
||||
data := loadQuestFile(s, questId)
|
||||
if data == nil {
|
||||
return nil, fmt.Errorf("failed to load quest file")
|
||||
}
|
||||
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteUint32(id)
|
||||
bf.WriteUint32(0)
|
||||
bf.WriteUint8(0) // Indexer
|
||||
switch questType {
|
||||
case 16:
|
||||
bf.WriteUint8(s.server.erupeConfig.GameplayOptions.RegularRavienteMaxPlayers)
|
||||
case 22:
|
||||
bf.WriteUint8(s.server.erupeConfig.GameplayOptions.ViolentRavienteMaxPlayers)
|
||||
case 40:
|
||||
bf.WriteUint8(s.server.erupeConfig.GameplayOptions.BerserkRavienteMaxPlayers)
|
||||
case 50:
|
||||
bf.WriteUint8(s.server.erupeConfig.GameplayOptions.ExtremeRavienteMaxPlayers)
|
||||
case 51:
|
||||
bf.WriteUint8(s.server.erupeConfig.GameplayOptions.SmallBerserkRavienteMaxPlayers)
|
||||
default:
|
||||
bf.WriteUint8(maxPlayers)
|
||||
}
|
||||
bf.WriteUint8(questType)
|
||||
if questType == 9 {
|
||||
bf.WriteBool(false)
|
||||
} else {
|
||||
bf.WriteBool(true)
|
||||
}
|
||||
bf.WriteUint16(0)
|
||||
if _config.ErupeConfig.RealClientMode >= _config.G1 {
|
||||
bf.WriteUint32(mark)
|
||||
}
|
||||
bf.WriteUint16(0)
|
||||
bf.WriteUint16(uint16(len(data)))
|
||||
bf.WriteBytes(data)
|
||||
ps.Uint8(bf, "", true) // What is this string for?
|
||||
|
||||
return bf.Data(), nil
|
||||
}
|
||||
|
||||
func handleMsgMhfEnumerateQuest(s *Session, p mhfpacket.MHFPacket) {
|
||||
pkt := p.(*mhfpacket.MsgMhfEnumerateQuest)
|
||||
var totalCount, returnedCount uint16
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteUint16(0)
|
||||
filepath.Walk(fmt.Sprintf("%s/events/", s.server.erupeConfig.BinPath), func(path string, info os.FileInfo, err error) error {
|
||||
|
||||
rows, _ := s.server.db.Query("SELECT id, COALESCE(max_players, 4) AS max_players, quest_type, quest_id, COALESCE(mark, 0) AS mark FROM event_quests ORDER BY quest_id")
|
||||
for rows.Next() {
|
||||
data, err := makeEventQuest(s, rows)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
continue
|
||||
} else {
|
||||
if len(data) > 850 || len(data) < 400 {
|
||||
return nil // Could be more or less strict with size limits
|
||||
if len(data) > 896 || len(data) < 352 {
|
||||
continue
|
||||
} else {
|
||||
totalCount++
|
||||
if totalCount > pkt.Offset && len(bf.Data()) < 60000 {
|
||||
returnedCount++
|
||||
bf.WriteBytes(data)
|
||||
return nil
|
||||
if _config.ErupeConfig.RealClientMode == _config.F5 {
|
||||
if totalCount > pkt.Offset && len(bf.Data()) < 21550 {
|
||||
returnedCount++
|
||||
bf.WriteBytes(data)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
if totalCount > pkt.Offset && len(bf.Data()) < 60000 {
|
||||
returnedCount++
|
||||
bf.WriteBytes(data)
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
type tuneValue struct {
|
||||
ID uint16
|
||||
@@ -518,6 +632,27 @@ func handleMsgMhfEnumerateQuest(s *Session, p mhfpacket.MHFPacket) {
|
||||
|
||||
offset := uint16(time.Now().Unix())
|
||||
bf.WriteUint16(offset)
|
||||
|
||||
if _config.ErupeConfig.RealClientMode <= _config.F5 {
|
||||
tuneValues = tuneValues[:256]
|
||||
} else if _config.ErupeConfig.RealClientMode <= _config.G3 {
|
||||
tuneValues = tuneValues[:283]
|
||||
} else if _config.ErupeConfig.RealClientMode <= _config.GG {
|
||||
tuneValues = tuneValues[:315]
|
||||
} else if _config.ErupeConfig.RealClientMode <= _config.G61 {
|
||||
tuneValues = tuneValues[:332]
|
||||
} else if _config.ErupeConfig.RealClientMode <= _config.G7 {
|
||||
tuneValues = tuneValues[:339]
|
||||
} else if _config.ErupeConfig.RealClientMode <= _config.G81 {
|
||||
tuneValues = tuneValues[:396]
|
||||
} else if _config.ErupeConfig.RealClientMode <= _config.G91 {
|
||||
tuneValues = tuneValues[:694]
|
||||
} else if _config.ErupeConfig.RealClientMode <= _config.G101 {
|
||||
tuneValues = tuneValues[:704]
|
||||
} else if _config.ErupeConfig.RealClientMode <= _config.Z2 {
|
||||
tuneValues = tuneValues[:750]
|
||||
}
|
||||
|
||||
bf.WriteUint16(uint16(len(tuneValues)))
|
||||
for i := range tuneValues {
|
||||
bf.WriteUint16(tuneValues[i].ID ^ offset)
|
||||
@@ -537,7 +672,8 @@ func handleMsgMhfEnumerateQuest(s *Session, p mhfpacket.MHFPacket) {
|
||||
{false, 10000},
|
||||
}
|
||||
bf.WriteUint16(uint16(len(vsQuestItems)))
|
||||
bf.WriteUint32(uint32(len(vsQuestBets)))
|
||||
bf.WriteUint16(0) // Unk array of uint16s
|
||||
bf.WriteUint16(uint16(len(vsQuestBets)))
|
||||
bf.WriteUint16(0) // Unk
|
||||
|
||||
for i := range vsQuestItems {
|
||||
@@ -554,6 +690,7 @@ func handleMsgMhfEnumerateQuest(s *Session, p mhfpacket.MHFPacket) {
|
||||
bf.WriteUint16(pkt.Offset)
|
||||
bf.Seek(0, io.SeekStart)
|
||||
bf.WriteUint16(returnedCount)
|
||||
|
||||
doAckBufSucceed(s, pkt.AckHandle, bf.Data())
|
||||
}
|
||||
|
||||
|
||||
@@ -196,7 +196,9 @@ func handleMsgSysOperateRegister(s *Session, p mhfpacket.MHFPacket) {
|
||||
resp.WriteUint8(0)
|
||||
doAckBufSucceed(s, pkt.AckHandle, resp.Data())
|
||||
}
|
||||
s.notifyRavi()
|
||||
if s.server.erupeConfig.GameplayOptions.LowLatencyRaviente {
|
||||
s.notifyRavi()
|
||||
}
|
||||
s.server.raviente.Unlock()
|
||||
}
|
||||
|
||||
@@ -241,6 +243,10 @@ func handleMsgSysLoadRegister(s *Session, p mhfpacket.MHFPacket) {
|
||||
}
|
||||
|
||||
func (s *Session) notifyRavi() {
|
||||
sema := getRaviSemaphore(s.server)
|
||||
if sema == nil {
|
||||
return
|
||||
}
|
||||
var temp mhfpacket.MHFPacket
|
||||
raviNotif := byteframe.NewByteFrame()
|
||||
temp = &mhfpacket.MsgSysNotifyRegister{RegisterID: 4}
|
||||
@@ -253,11 +259,16 @@ func (s *Session) notifyRavi() {
|
||||
raviNotif.WriteUint16(uint16(temp.Opcode()))
|
||||
temp.Build(raviNotif, s.clientContext)
|
||||
raviNotif.WriteUint16(0x0010) // End it.
|
||||
sema := getRaviSemaphore(s.server)
|
||||
if sema != nil {
|
||||
if s.server.erupeConfig.GameplayOptions.LowLatencyRaviente {
|
||||
for session := range sema.clients {
|
||||
session.QueueSend(raviNotif.Data())
|
||||
}
|
||||
} else {
|
||||
for session := range sema.clients {
|
||||
if session.charID == s.charID {
|
||||
session.QueueSend(raviNotif.Data())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -85,13 +85,13 @@ func handleMsgSysCreateAcquireSemaphore(s *Session, p mhfpacket.MHFPacket) {
|
||||
if !exists {
|
||||
s.server.semaphoreLock.Lock()
|
||||
if strings.HasPrefix(SemaphoreID, "hs_l0u3B5") {
|
||||
suffix, _ := strconv.ParseUint(pkt.SemaphoreID[len(pkt.SemaphoreID)-1:], 10, 32)
|
||||
suffix, _ := strconv.Atoi(pkt.SemaphoreID[len(pkt.SemaphoreID)-1:])
|
||||
s.server.semaphore[SemaphoreID] = &Semaphore{
|
||||
id_semaphore: pkt.SemaphoreID,
|
||||
id: uint32(suffix + 1),
|
||||
clients: make(map[*Session]uint32),
|
||||
reservedClientSlots: make(map[uint32]interface{}),
|
||||
maxPlayers: 32,
|
||||
maxPlayers: 127,
|
||||
}
|
||||
} else {
|
||||
s.server.semaphore[SemaphoreID] = NewSemaphore(s.server, SemaphoreID, 1)
|
||||
|
||||
@@ -3,6 +3,7 @@ package channelserver
|
||||
import (
|
||||
"erupe-ce/common/byteframe"
|
||||
ps "erupe-ce/common/pascalstring"
|
||||
_config "erupe-ce/config"
|
||||
"erupe-ce/network/mhfpacket"
|
||||
"math/rand"
|
||||
)
|
||||
@@ -40,13 +41,14 @@ type GachaEntry struct {
|
||||
EntryType uint8 `db:"entry_type"`
|
||||
ID uint32 `db:"id"`
|
||||
ItemType uint8 `db:"item_type"`
|
||||
ItemNumber uint16 `db:"item_number"`
|
||||
ItemNumber uint32 `db:"item_number"`
|
||||
ItemQuantity uint16 `db:"item_quantity"`
|
||||
Weight float64 `db:"weight"`
|
||||
Rarity uint8 `db:"rarity"`
|
||||
Rolls uint8 `db:"rolls"`
|
||||
FrontierPoints uint16 `db:"frontier_points"`
|
||||
DailyLimit uint8 `db:"daily_limit"`
|
||||
Name string `db:"name"`
|
||||
}
|
||||
|
||||
type GachaItem struct {
|
||||
@@ -108,6 +110,12 @@ func handleMsgMhfEnumerateShop(s *Session, p mhfpacket.MHFPacket) {
|
||||
// 8: special item
|
||||
switch pkt.ShopType {
|
||||
case 1: // Running gachas
|
||||
// Fundamentally, gacha works completely differently, just hide it for now.
|
||||
if _config.ErupeConfig.RealClientMode <= _config.G7 {
|
||||
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 4))
|
||||
return
|
||||
}
|
||||
|
||||
var count uint16
|
||||
shopEntries, err := s.server.db.Queryx("SELECT id, min_gr, min_hr, name, url_banner, url_feature, url_thumbnail, wide, recommended, gacha_type, hidden FROM gacha_shop")
|
||||
if err != nil {
|
||||
@@ -151,7 +159,7 @@ func handleMsgMhfEnumerateShop(s *Session, p mhfpacket.MHFPacket) {
|
||||
bf.WriteUint32(pkt.ShopID)
|
||||
var gachaType int
|
||||
s.server.db.QueryRow(`SELECT gacha_type FROM gacha_shop WHERE id = $1`, pkt.ShopID).Scan(&gachaType)
|
||||
entries, err := s.server.db.Queryx(`SELECT entry_type, id, item_type, item_number, item_quantity, weight, rarity, rolls, daily_limit, frontier_points FROM gacha_entries WHERE gacha_id = $1 ORDER BY weight DESC`, pkt.ShopID)
|
||||
entries, err := s.server.db.Queryx(`SELECT entry_type, id, item_type, item_number, item_quantity, weight, rarity, rolls, daily_limit, frontier_points, name FROM gacha_entries WHERE gacha_id = $1 ORDER BY weight DESC`, pkt.ShopID)
|
||||
if err != nil {
|
||||
doAckBufSucceed(s, pkt.AckHandle, make([]byte, 4))
|
||||
return
|
||||
@@ -168,8 +176,7 @@ func handleMsgMhfEnumerateShop(s *Session, p mhfpacket.MHFPacket) {
|
||||
bf.WriteUint8(gachaEntry.EntryType)
|
||||
bf.WriteUint32(gachaEntry.ID)
|
||||
bf.WriteUint8(gachaEntry.ItemType)
|
||||
bf.WriteUint16(0)
|
||||
bf.WriteUint16(gachaEntry.ItemNumber)
|
||||
bf.WriteUint32(gachaEntry.ItemNumber)
|
||||
bf.WriteUint16(gachaEntry.ItemQuantity)
|
||||
if gachaType >= 4 { // If box
|
||||
bf.WriteUint16(1)
|
||||
@@ -197,7 +204,11 @@ func handleMsgMhfEnumerateShop(s *Session, p mhfpacket.MHFPacket) {
|
||||
|
||||
bf.WriteUint16(gachaEntry.FrontierPoints)
|
||||
bf.WriteUint8(gachaEntry.DailyLimit)
|
||||
bf.WriteUint8(0)
|
||||
if gachaEntry.EntryType < 10 {
|
||||
ps.Uint8(bf, gachaEntry.Name, true)
|
||||
} else {
|
||||
bf.WriteUint8(0)
|
||||
}
|
||||
bf.WriteBytes(temp.Data())
|
||||
}
|
||||
bf.Seek(4, 0)
|
||||
|
||||
@@ -47,6 +47,7 @@ type Server struct {
|
||||
acceptConns chan net.Conn
|
||||
deleteConns chan net.Conn
|
||||
sessions map[net.Conn]*Session
|
||||
objectIDs map[*Session]uint16
|
||||
listener net.Listener // Listener that is created when Server.Start is called.
|
||||
isShuttingDown bool
|
||||
|
||||
@@ -152,6 +153,7 @@ func NewServer(config *Config) *Server {
|
||||
acceptConns: make(chan net.Conn),
|
||||
deleteConns: make(chan net.Conn),
|
||||
sessions: make(map[net.Conn]*Session),
|
||||
objectIDs: make(map[*Session]uint16),
|
||||
stages: make(map[string]*Stage),
|
||||
userBinaryParts: make(map[userBinaryPartID][]byte),
|
||||
semaphore: make(map[string]*Semaphore),
|
||||
@@ -405,3 +407,8 @@ func (s *Server) NextSemaphoreID() uint32 {
|
||||
}
|
||||
return s.semaphoreIndex
|
||||
}
|
||||
|
||||
func (s *Server) Season() uint8 {
|
||||
sid := int64(((s.ID & 0xFF00) - 4096) / 256)
|
||||
return uint8(((TimeAdjusted().Unix() / 86400) + sid) % 3)
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ func getLangStrings(s *Server) map[string]string {
|
||||
strings["commandTeleportSuccess"] = "%d %dにテレポート"
|
||||
strings["commandPSNError"] = "PSN連携コマンドエラー 例:%s <psn id>"
|
||||
strings["commandPSNSuccess"] = "PSN「%s」が連携されています"
|
||||
strings["commandPSNExists"] = "PSNは既存のユーザに接続されています"
|
||||
|
||||
strings["commandRaviNoCommand"] = "ラヴィコマンドが指定されていません"
|
||||
strings["commandRaviStartSuccess"] = "大討伐を開始します"
|
||||
@@ -72,6 +73,7 @@ func getLangStrings(s *Server) map[string]string {
|
||||
strings["commandTeleportSuccess"] = "Teleporting to %d %d"
|
||||
strings["commandPSNError"] = "Error in command. Format: %s <psn id>"
|
||||
strings["commandPSNSuccess"] = "Connected PSN ID: %s"
|
||||
strings["commandPSNExists"] = "PSN ID is connected to another account!"
|
||||
|
||||
strings["commandRaviNoCommand"] = "No Raviente command specified!"
|
||||
strings["commandRaviStartSuccess"] = "The Great Slaying will begin in a moment"
|
||||
|
||||
@@ -34,6 +34,7 @@ type Session struct {
|
||||
clientContext *clientctx.ClientContext
|
||||
lastPacket time.Time
|
||||
|
||||
objectIndex uint16
|
||||
userEnteredStage bool // If the user has entered a stage before
|
||||
stageID string
|
||||
stage *Stage
|
||||
@@ -78,6 +79,7 @@ func NewSession(server *Server, conn net.Conn) *Session {
|
||||
sessionStart: TimeAdjusted().Unix(),
|
||||
stageMoveStack: stringstack.New(),
|
||||
}
|
||||
s.SetObjectID()
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -268,3 +270,29 @@ func (s *Session) logMessage(opcode uint16, data []byte, sender string, recipien
|
||||
fmt.Printf("Data [%d bytes]:\n(Too long!)\n\n", len(data))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) SetObjectID() {
|
||||
for i := uint16(1); i < 127; i++ {
|
||||
exists := false
|
||||
for _, j := range s.server.objectIDs {
|
||||
if i == j {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
s.server.objectIDs[s] = i
|
||||
return
|
||||
}
|
||||
}
|
||||
s.server.objectIDs[s] = 0
|
||||
}
|
||||
|
||||
func (s *Session) NextObjectID() uint32 {
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteUint16(s.server.objectIDs[s])
|
||||
s.objectIndex++
|
||||
bf.WriteUint16(s.objectIndex)
|
||||
bf.Seek(0, 0)
|
||||
return bf.ReadUint32()
|
||||
}
|
||||
|
||||
@@ -30,7 +30,7 @@ type Stage struct {
|
||||
|
||||
// Objects
|
||||
objects map[uint32]*Object
|
||||
objectIndex uint16
|
||||
objectIndex uint8
|
||||
|
||||
// Map of session -> charID.
|
||||
// These are clients that are CURRENTLY in the stage
|
||||
@@ -56,6 +56,7 @@ func NewStage(ID string) *Stage {
|
||||
clients: make(map[*Session]uint32),
|
||||
reservedClientSlots: make(map[uint32]bool),
|
||||
objects: make(map[uint32]*Object),
|
||||
objectIndex: 0,
|
||||
rawBinaryData: make(map[stageBinaryKey][]byte),
|
||||
maxPlayers: 4,
|
||||
}
|
||||
@@ -94,12 +95,3 @@ func (s *Stage) isCharInQuestByID(charID uint32) bool {
|
||||
func (s *Stage) isQuest() bool {
|
||||
return len(s.reservedClientSlots) > 0
|
||||
}
|
||||
|
||||
func (s *Stage) NextObjectID() uint32 {
|
||||
s.objectIndex++
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteUint16(127)
|
||||
bf.WriteUint16(s.objectIndex)
|
||||
bf.Seek(0, 0)
|
||||
return bf.ReadUint32()
|
||||
}
|
||||
|
||||
@@ -23,3 +23,7 @@ func TimeWeekStart() time.Time {
|
||||
func TimeWeekNext() time.Time {
|
||||
return TimeWeekStart().Add(time.Hour * 24 * 7)
|
||||
}
|
||||
|
||||
func TimeGameAbsolute() uint32 {
|
||||
return uint32((TimeAdjusted().Unix() - 2160) % 5760)
|
||||
}
|
||||
|
||||
@@ -1,87 +0,0 @@
|
||||
package timeserver
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var DoOnce_midnight = false
|
||||
var DoOnce_t2 = false
|
||||
var DoOnce_t = false
|
||||
var Fix_midnight = time.Time{}
|
||||
var Fix_t2 = time.Time{}
|
||||
var Fix_t = time.Time{}
|
||||
var Pfixtimer time.Duration
|
||||
var Pnewtime = 0
|
||||
var yearsFixed = -7
|
||||
|
||||
func PFadd_time() time.Duration {
|
||||
Pnewtime = Pnewtime + 24
|
||||
Pfixtimer = time.Duration(Pnewtime)
|
||||
return Pfixtimer
|
||||
}
|
||||
|
||||
func Time_static() time.Time {
|
||||
if !DoOnce_t {
|
||||
DoOnce_t = true
|
||||
// Force to 201x
|
||||
tFix1 := time.Now()
|
||||
tFix2 := tFix1.AddDate(yearsFixed, 0, 0)
|
||||
Fix_t = tFix2.In(time.FixedZone("UTC+1", 1*60*60))
|
||||
}
|
||||
return Fix_t
|
||||
}
|
||||
|
||||
func Tstatic_midnight() time.Time {
|
||||
if !DoOnce_midnight {
|
||||
DoOnce_midnight = true
|
||||
// Force to 201x
|
||||
tFix1 := time.Now()
|
||||
tFix2 := tFix1.AddDate(yearsFixed, 0, 0)
|
||||
var tFix = tFix2.In(time.FixedZone("UTC+1", 1*60*60))
|
||||
yearFix, monthFix, dayFix := tFix2.Date()
|
||||
Fix_midnight = time.Date(yearFix, monthFix, dayFix, 0, 0, 0, 0, tFix.Location()).Add(time.Hour)
|
||||
}
|
||||
return Fix_midnight
|
||||
}
|
||||
|
||||
func Time_midnight() time.Time {
|
||||
// Force to 201x
|
||||
t1 := time.Now()
|
||||
t2 := t1.AddDate(yearsFixed, 0, 0)
|
||||
var t = t2.In(time.FixedZone("UTC+1", 1*60*60))
|
||||
year, month, day := t2.Date()
|
||||
midnight := time.Date(year, month, day, 0, 0, 0, 0, t.Location()).Add(time.Hour)
|
||||
return midnight
|
||||
}
|
||||
|
||||
func TimeCurrent() time.Time {
|
||||
// Force to 201x
|
||||
t1 := time.Now()
|
||||
t2 := t1.AddDate(yearsFixed, 0, 0)
|
||||
var t = t2.In(time.FixedZone("UTC+1", 1*60*60))
|
||||
return t
|
||||
}
|
||||
|
||||
func Time_Current_Week_uint8() uint8 {
|
||||
beginningOfTheMonth := time.Date(TimeCurrent().Year(), TimeCurrent().Month(), 1, 1, 1, 1, 1, time.UTC)
|
||||
_, thisWeek := TimeCurrent().ISOWeek()
|
||||
_, beginningWeek := beginningOfTheMonth.ISOWeek()
|
||||
|
||||
return uint8(1 + thisWeek - beginningWeek)
|
||||
}
|
||||
|
||||
func Time_Current_Week_uint32() uint32 {
|
||||
beginningOfTheMonth := time.Date(TimeCurrent().Year(), TimeCurrent().Month(), 1, 1, 1, 1, 1, time.UTC)
|
||||
_, thisWeek := TimeCurrent().ISOWeek()
|
||||
_, beginningWeek := beginningOfTheMonth.ISOWeek()
|
||||
result := 1 + thisWeek - beginningWeek
|
||||
return uint32(result)
|
||||
}
|
||||
|
||||
func Detect_Day() bool {
|
||||
switch time.Now().Weekday() {
|
||||
case time.Wednesday:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -12,12 +12,6 @@ import (
|
||||
"erupe-ce/server/channelserver"
|
||||
)
|
||||
|
||||
// Server Entries
|
||||
var season uint8
|
||||
|
||||
// Server Channels
|
||||
var currentplayers uint16
|
||||
|
||||
func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
|
||||
serverInfos := config.Entrance.Entries
|
||||
bf := byteframe.NewByteFrame()
|
||||
@@ -34,11 +28,8 @@ func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
|
||||
continue
|
||||
}
|
||||
}
|
||||
sid := (4096 + serverIdx*256) + 16
|
||||
err := s.db.QueryRow("SELECT season FROM servers WHERE server_id=$1", sid).Scan(&season)
|
||||
if err != nil {
|
||||
season = 0
|
||||
}
|
||||
|
||||
sid := (4096 + serverIdx*256) * 6000
|
||||
if si.IP == "" {
|
||||
si.IP = config.Host
|
||||
}
|
||||
@@ -51,7 +42,7 @@ func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
|
||||
bf.WriteUint16(0x0000)
|
||||
bf.WriteUint16(uint16(len(si.Channels)))
|
||||
bf.WriteUint8(si.Type)
|
||||
bf.WriteUint8(season)
|
||||
bf.WriteUint8(uint8(((channelserver.TimeAdjusted().Unix() / 86400) + int64(serverIdx)) % 3))
|
||||
if s.erupeConfig.RealClientMode >= _config.G1 {
|
||||
bf.WriteUint8(si.Recommended)
|
||||
}
|
||||
@@ -81,18 +72,19 @@ func encodeServerInfo(config *_config.Config, s *Server, local bool) []byte {
|
||||
bf.WriteUint16(ci.Port)
|
||||
bf.WriteUint16(16 + uint16(channelIdx))
|
||||
bf.WriteUint16(ci.MaxPlayers)
|
||||
err := s.db.QueryRow("SELECT current_players FROM servers WHERE server_id=$1", sid).Scan(¤tplayers)
|
||||
if err != nil {
|
||||
currentplayers = 0
|
||||
}
|
||||
bf.WriteUint16(currentplayers)
|
||||
bf.WriteUint32(0)
|
||||
bf.WriteUint32(0)
|
||||
bf.WriteUint32(0)
|
||||
bf.WriteUint16(319) // Unk
|
||||
bf.WriteUint16(252) // Unk
|
||||
bf.WriteUint16(248) // Unk
|
||||
bf.WriteUint16(0x3039)
|
||||
var currentPlayers uint16
|
||||
s.db.QueryRow("SELECT current_players FROM servers WHERE server_id=$1", sid).Scan(¤tPlayers)
|
||||
bf.WriteUint16(currentPlayers)
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(0) // Unk
|
||||
bf.WriteUint16(319) // Unk
|
||||
bf.WriteUint16(252) // Unk
|
||||
bf.WriteUint16(248) // Unk
|
||||
bf.WriteUint16(12345) // Unk
|
||||
}
|
||||
}
|
||||
bf.WriteUint32(uint32(channelserver.TimeAdjusted().Unix()))
|
||||
|
||||
@@ -1,14 +1,18 @@
|
||||
package signserver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"erupe-ce/common/mhfcourse"
|
||||
"erupe-ce/common/token"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func (s *Server) newUserChara(uid int) error {
|
||||
func (s *Server) newUserChara(uid uint32) error {
|
||||
var numNewChars int
|
||||
err := s.db.QueryRow("SELECT COUNT(*) FROM characters WHERE user_id = $1 AND is_new_character = true", uid).Scan(&numNewChars)
|
||||
if err != nil {
|
||||
@@ -35,20 +39,22 @@ func (s *Server) newUserChara(uid int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) registerDBAccount(username string, password string) (int, error) {
|
||||
func (s *Server) registerDBAccount(username string, password string) (uint32, error) {
|
||||
var uid uint32
|
||||
s.logger.Info("Creating user", zap.String("User", username))
|
||||
|
||||
// Create salted hash of user password
|
||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var id int
|
||||
err = s.db.QueryRow("INSERT INTO users (username, password, return_expires) VALUES ($1, $2, $3) RETURNING id", username, string(passwordHash), time.Now().Add(time.Hour*24*30)).Scan(&id)
|
||||
err = s.db.QueryRow("INSERT INTO users (username, password, return_expires) VALUES ($1, $2, $3) RETURNING id", username, string(passwordHash), time.Now().Add(time.Hour*24*30)).Scan(&uid)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return id, nil
|
||||
return uid, nil
|
||||
}
|
||||
|
||||
type character struct {
|
||||
@@ -63,7 +69,7 @@ type character struct {
|
||||
LastLogin uint32 `db:"last_login"`
|
||||
}
|
||||
|
||||
func (s *Server) getCharactersForUser(uid int) ([]character, error) {
|
||||
func (s *Server) getCharactersForUser(uid uint32) ([]character, error) {
|
||||
characters := make([]character, 0)
|
||||
err := s.db.Select(&characters, "SELECT id, is_female, is_new_character, name, unk_desc_string, hrp, gr, weapon_type, last_login FROM characters WHERE user_id = $1 AND deleted = false ORDER BY id", uid)
|
||||
if err != nil {
|
||||
@@ -72,7 +78,7 @@ func (s *Server) getCharactersForUser(uid int) ([]character, error) {
|
||||
return characters, nil
|
||||
}
|
||||
|
||||
func (s *Server) getReturnExpiry(uid int) time.Time {
|
||||
func (s *Server) getReturnExpiry(uid uint32) time.Time {
|
||||
var returnExpiry, lastLogin time.Time
|
||||
s.db.Get(&lastLogin, "SELECT COALESCE(last_login, now()) FROM users WHERE id=$1", uid)
|
||||
if time.Now().Add((time.Hour * 24) * -90).After(lastLogin) {
|
||||
@@ -89,16 +95,18 @@ func (s *Server) getReturnExpiry(uid int) time.Time {
|
||||
return returnExpiry
|
||||
}
|
||||
|
||||
func (s *Server) getLastCID(uid int) uint32 {
|
||||
func (s *Server) getLastCID(uid uint32) uint32 {
|
||||
var lastPlayed uint32
|
||||
_ = s.db.QueryRow("SELECT last_character FROM users WHERE id=$1", uid).Scan(&lastPlayed)
|
||||
return lastPlayed
|
||||
}
|
||||
|
||||
func (s *Server) getUserRights(uid int) uint32 {
|
||||
rights := uint32(2)
|
||||
_ = s.db.QueryRow("SELECT rights FROM users WHERE id=$1", uid).Scan(&rights)
|
||||
_, rights = mhfcourse.GetCourseStruct(rights)
|
||||
func (s *Server) getUserRights(uid uint32) uint32 {
|
||||
var rights uint32
|
||||
if uid != 0 {
|
||||
_ = s.db.QueryRow("SELECT rights FROM users WHERE id=$1", uid).Scan(&rights)
|
||||
_, rights = mhfcourse.GetCourseStruct(rights)
|
||||
}
|
||||
return rights
|
||||
}
|
||||
|
||||
@@ -159,14 +167,12 @@ func (s *Server) getGuildmatesForCharacters(chars []character) []members {
|
||||
return guildmates
|
||||
}
|
||||
|
||||
func (s *Server) deleteCharacter(cid int, token string) error {
|
||||
var verify int
|
||||
err := s.db.QueryRow("SELECT count(*) FROM sign_sessions WHERE token = $1", token).Scan(&verify)
|
||||
if err != nil {
|
||||
return err // Invalid token
|
||||
func (s *Server) deleteCharacter(cid int, token string, tokenID uint32) error {
|
||||
if !s.validateToken(token, tokenID) {
|
||||
return errors.New("invalid token")
|
||||
}
|
||||
var isNew bool
|
||||
err = s.db.QueryRow("SELECT is_new_character FROM characters WHERE id = $1", cid).Scan(&isNew)
|
||||
err := s.db.QueryRow("SELECT is_new_character FROM characters WHERE id = $1", cid).Scan(&isNew)
|
||||
if isNew {
|
||||
_, err = s.db.Exec("DELETE FROM characters WHERE id = $1", cid)
|
||||
} else {
|
||||
@@ -179,7 +185,7 @@ func (s *Server) deleteCharacter(cid int, token string) error {
|
||||
}
|
||||
|
||||
// Unused
|
||||
func (s *Server) checkToken(uid int) (bool, error) {
|
||||
func (s *Server) checkToken(uid uint32) (bool, error) {
|
||||
var exists int
|
||||
err := s.db.QueryRow("SELECT count(*) FROM sign_sessions WHERE user_id = $1", uid).Scan(&exists)
|
||||
if err != nil {
|
||||
@@ -191,10 +197,55 @@ func (s *Server) checkToken(uid int) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (s *Server) registerToken(uid int, token string) error {
|
||||
_, err := s.db.Exec("INSERT INTO sign_sessions (user_id, token) VALUES ($1, $2)", uid, token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
func (s *Server) registerUidToken(uid uint32) (uint32, string, error) {
|
||||
token := token.Generate(16)
|
||||
var tid uint32
|
||||
err := s.db.QueryRow(`INSERT INTO sign_sessions (user_id, token) VALUES ($1, $2) RETURNING id`, uid, token).Scan(&tid)
|
||||
return tid, token, err
|
||||
}
|
||||
|
||||
func (s *Server) registerPsnToken(psn string) (uint32, string, error) {
|
||||
token := token.Generate(16)
|
||||
var tid uint32
|
||||
err := s.db.QueryRow(`INSERT INTO sign_sessions (psn_id, token) VALUES ($1, $2) RETURNING id`, psn, token).Scan(&tid)
|
||||
return tid, token, err
|
||||
}
|
||||
|
||||
func (s *Server) validateToken(token string, tokenID uint32) bool {
|
||||
query := `SELECT count(*) FROM sign_sessions WHERE token = $1`
|
||||
if tokenID > 0 {
|
||||
query += ` AND id = $2`
|
||||
}
|
||||
var exists int
|
||||
err := s.db.QueryRow(query, token, tokenID).Scan(&exists)
|
||||
if err != nil || exists == 0 {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) validateLogin(user string, pass string) (uint32, RespID) {
|
||||
var uid uint32
|
||||
var passDB string
|
||||
err := s.db.QueryRow(`SELECT id, password FROM users WHERE username = $1`, user).Scan(&uid, &passDB)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
s.logger.Info("User not found", zap.String("User", user))
|
||||
if s.erupeConfig.DevMode && s.erupeConfig.DevModeOptions.AutoCreateAccount {
|
||||
uid, err = s.registerDBAccount(user, pass)
|
||||
if err == nil {
|
||||
return uid, SIGN_SUCCESS
|
||||
} else {
|
||||
return 0, SIGN_EABORT
|
||||
}
|
||||
}
|
||||
return 0, SIGN_EAUTH
|
||||
}
|
||||
return 0, SIGN_EABORT
|
||||
} else {
|
||||
if bcrypt.CompareHashAndPassword([]byte(passDB), []byte(pass)) == nil {
|
||||
return uid, SIGN_SUCCESS
|
||||
}
|
||||
return 0, SIGN_EPASS
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"erupe-ce/common/byteframe"
|
||||
ps "erupe-ce/common/pascalstring"
|
||||
"erupe-ce/common/stringsupport"
|
||||
"erupe-ce/common/token"
|
||||
_config "erupe-ce/config"
|
||||
"erupe-ce/server/channelserver"
|
||||
"fmt"
|
||||
@@ -12,10 +11,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s *Session) makeSignResponse(uid int) []byte {
|
||||
func (s *Session) makeSignResponse(uid uint32) []byte {
|
||||
// Get the characters from the DB.
|
||||
chars, err := s.server.getCharactersForUser(uid)
|
||||
if len(chars) == 0 {
|
||||
if len(chars) == 0 && uid != 0 {
|
||||
err = s.server.newUserChara(uid)
|
||||
if err == nil {
|
||||
chars, err = s.server.getCharactersForUser(uid)
|
||||
@@ -25,10 +24,18 @@ func (s *Session) makeSignResponse(uid int) []byte {
|
||||
s.logger.Warn("Error getting characters from DB", zap.Error(err))
|
||||
}
|
||||
|
||||
sessToken := token.Generate(16)
|
||||
_ = s.server.registerToken(uid, sessToken)
|
||||
|
||||
bf := byteframe.NewByteFrame()
|
||||
var tokenID uint32
|
||||
var sessToken string
|
||||
if uid == 0 && s.psn != "" {
|
||||
tokenID, sessToken, err = s.server.registerPsnToken(s.psn)
|
||||
} else {
|
||||
tokenID, sessToken, err = s.server.registerUidToken(uid)
|
||||
}
|
||||
if err != nil {
|
||||
bf.WriteUint8(uint8(SIGN_EABORT))
|
||||
return bf.Data()
|
||||
}
|
||||
|
||||
bf.WriteUint8(uint8(SIGN_SUCCESS)) // resp_code
|
||||
if (s.server.erupeConfig.PatchServerManifest != "" && s.server.erupeConfig.PatchServerFile != "") || s.client == PS3 {
|
||||
@@ -38,7 +45,7 @@ func (s *Session) makeSignResponse(uid int) []byte {
|
||||
}
|
||||
bf.WriteUint8(1) // entrance server count
|
||||
bf.WriteUint8(uint8(len(chars)))
|
||||
bf.WriteUint32(0xFFFFFFFF) // login_token_number
|
||||
bf.WriteUint32(tokenID)
|
||||
bf.WriteBytes([]byte(sessToken))
|
||||
bf.WriteUint32(uint32(channelserver.TimeAdjusted().Unix()))
|
||||
if s.client == PS3 {
|
||||
|
||||
@@ -3,20 +3,21 @@ package signserver
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"erupe-ce/common/stringsupport"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"erupe-ce/common/byteframe"
|
||||
"erupe-ce/network"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type Client int
|
||||
type client int
|
||||
|
||||
const (
|
||||
PC100 Client = iota
|
||||
PC100 client = iota
|
||||
VITA
|
||||
PS3
|
||||
WIIU
|
||||
@@ -29,7 +30,8 @@ type Session struct {
|
||||
server *Server
|
||||
rawConn net.Conn
|
||||
cryptConn *network.CryptConn
|
||||
client Client
|
||||
client client
|
||||
psn string
|
||||
}
|
||||
|
||||
func (s *Session) work() {
|
||||
@@ -51,23 +53,25 @@ func (s *Session) work() {
|
||||
func (s *Session) handlePacket(pkt []byte) error {
|
||||
bf := byteframe.NewByteFrameFromBytes(pkt)
|
||||
reqType := string(bf.ReadNullTerminatedBytes())
|
||||
switch reqType {
|
||||
case "DLTSKEYSIGN:100", "DSGN:100":
|
||||
switch reqType[:len(reqType)-3] {
|
||||
case "DLTSKEYSIGN:", "DSGN:":
|
||||
s.handleDSGN(bf)
|
||||
case "PS3SGN:100":
|
||||
case "PS3SGN:":
|
||||
s.client = PS3
|
||||
s.handlePSSGN(bf)
|
||||
case "VITASGN:100":
|
||||
case "VITASGN:":
|
||||
s.client = VITA
|
||||
s.handlePSSGN(bf)
|
||||
case "WIIUSGN:100", "WIIUSGN:000":
|
||||
case "WIIUSGN:":
|
||||
s.client = WIIU
|
||||
s.handleWIIUSGN(bf)
|
||||
case "DELETE:100":
|
||||
loginTokenString := string(bf.ReadNullTerminatedBytes())
|
||||
case "VITACOGLNK:", "COGLNK:":
|
||||
s.handlePSNLink(bf)
|
||||
case "DELETE:":
|
||||
token := string(bf.ReadNullTerminatedBytes())
|
||||
characterID := int(bf.ReadUint32())
|
||||
_ = int(bf.ReadUint32()) // login_token_number
|
||||
err := s.server.deleteCharacter(characterID, loginTokenString)
|
||||
tokenID := bf.ReadUint32()
|
||||
err := s.server.deleteCharacter(characterID, token, tokenID)
|
||||
if err == nil {
|
||||
s.logger.Info("Deleted character", zap.Int("CharacterID", characterID))
|
||||
s.cryptConn.SendPacket([]byte{0x01}) // DEL_SUCCESS
|
||||
@@ -83,104 +87,117 @@ func (s *Session) handlePacket(pkt []byte) error {
|
||||
|
||||
func (s *Session) authenticate(username string, password string) {
|
||||
newCharaReq := false
|
||||
|
||||
if username[len(username)-1] == 43 { // '+'
|
||||
username = username[:len(username)-1]
|
||||
newCharaReq = true
|
||||
}
|
||||
|
||||
var id int
|
||||
var hash string
|
||||
bf := byteframe.NewByteFrame()
|
||||
|
||||
err := s.server.db.QueryRow("SELECT id, password FROM users WHERE username = $1", username).Scan(&id, &hash)
|
||||
switch {
|
||||
case err == sql.ErrNoRows:
|
||||
s.logger.Info("User not found", zap.String("Username", username))
|
||||
if s.server.erupeConfig.DevMode && s.server.erupeConfig.DevModeOptions.AutoCreateAccount {
|
||||
s.logger.Info("Creating user", zap.String("Username", username))
|
||||
id, err = s.server.registerDBAccount(username, password)
|
||||
if err == nil {
|
||||
bf.WriteBytes(s.makeSignResponse(id))
|
||||
}
|
||||
} else {
|
||||
bf.WriteUint8(uint8(SIGN_EAUTH))
|
||||
uid, resp := s.server.validateLogin(username, password)
|
||||
switch resp {
|
||||
case SIGN_SUCCESS:
|
||||
if newCharaReq {
|
||||
_ = s.server.newUserChara(uid)
|
||||
}
|
||||
case err != nil:
|
||||
bf.WriteUint8(uint8(SIGN_EABORT))
|
||||
s.logger.Error("Error getting user details", zap.Error(err))
|
||||
bf.WriteBytes(s.makeSignResponse(uid))
|
||||
default:
|
||||
if bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil || s.client == VITA || s.client == PS3 || s.client == WIIU {
|
||||
s.logger.Debug("Passwords match!")
|
||||
if newCharaReq {
|
||||
err = s.server.newUserChara(id)
|
||||
if err != nil {
|
||||
s.logger.Error("Error adding new character to user", zap.Error(err))
|
||||
bf.WriteUint8(uint8(SIGN_EABORT))
|
||||
break
|
||||
}
|
||||
}
|
||||
// TODO: Need to auto delete user tokens after inactivity
|
||||
// exists, err := s.server.checkToken(id)
|
||||
// if err != nil {
|
||||
// s.logger.Info("Error checking for live tokens", zap.Error(err))
|
||||
// serverRespBytes = makeSignInFailureResp(SIGN_EABORT)
|
||||
// break
|
||||
// }
|
||||
bf.WriteBytes(s.makeSignResponse(id))
|
||||
} else {
|
||||
s.logger.Warn("Incorrect password")
|
||||
bf.WriteUint8(uint8(SIGN_EPASS))
|
||||
}
|
||||
bf.WriteUint8(uint8(resp))
|
||||
}
|
||||
|
||||
if s.server.erupeConfig.DevMode && s.server.erupeConfig.DevModeOptions.LogOutboundMessages {
|
||||
fmt.Printf("\n[Server] -> [Client]\nData [%d bytes]:\n%s\n", len(bf.Data()), hex.Dump(bf.Data()))
|
||||
}
|
||||
|
||||
err = s.cryptConn.SendPacket(bf.Data())
|
||||
_ = s.cryptConn.SendPacket(bf.Data())
|
||||
}
|
||||
|
||||
func (s *Session) handleWIIUSGN(bf *byteframe.ByteFrame) {
|
||||
_ = bf.ReadBytes(1)
|
||||
wiiuKey := string(bf.ReadBytes(64))
|
||||
var reqUsername string
|
||||
err := s.server.db.QueryRow(`SELECT username FROM users WHERE wiiu_key = $1`, wiiuKey).Scan(&reqUsername)
|
||||
if err == sql.ErrNoRows {
|
||||
resp := byteframe.NewByteFrame()
|
||||
resp.WriteUint8(uint8(SIGN_ECOGLINK))
|
||||
s.cryptConn.SendPacket(resp.Data())
|
||||
var uid uint32
|
||||
err := s.server.db.QueryRow(`SELECT id FROM users WHERE wiiu_key = $1`, wiiuKey).Scan(&uid)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
s.logger.Info("Unlinked Wii U attempted to authenticate", zap.String("Key", wiiuKey))
|
||||
s.sendCode(SIGN_ECOGLINK)
|
||||
return
|
||||
}
|
||||
s.sendCode(SIGN_EABORT)
|
||||
return
|
||||
}
|
||||
s.authenticate(reqUsername, "")
|
||||
s.cryptConn.SendPacket(s.makeSignResponse(uid))
|
||||
}
|
||||
|
||||
func (s *Session) handlePSSGN(bf *byteframe.ByteFrame) {
|
||||
// Prevent reading malformed request
|
||||
if len(bf.DataFromCurrent()) < 128 {
|
||||
resp := byteframe.NewByteFrame()
|
||||
resp.WriteUint8(uint8(SIGN_EABORT))
|
||||
s.cryptConn.SendPacket(resp.Data())
|
||||
s.sendCode(SIGN_EABORT)
|
||||
return
|
||||
}
|
||||
_ = bf.ReadNullTerminatedBytes() // VITA = 0000000256, PS3 = 0000000255
|
||||
_ = bf.ReadBytes(2) // VITA = 1, PS3 = !
|
||||
_ = bf.ReadBytes(82)
|
||||
psnUser := string(bf.ReadNullTerminatedBytes())
|
||||
var reqUsername string
|
||||
err := s.server.db.QueryRow(`SELECT username FROM users WHERE psn_id = $1`, psnUser).Scan(&reqUsername)
|
||||
if err == sql.ErrNoRows {
|
||||
resp := byteframe.NewByteFrame()
|
||||
resp.WriteUint8(uint8(SIGN_ECOGLINK))
|
||||
s.cryptConn.SendPacket(resp.Data())
|
||||
s.psn = string(bf.ReadNullTerminatedBytes())
|
||||
var uid uint32
|
||||
err := s.server.db.QueryRow(`SELECT id FROM users WHERE psn_id = $1`, s.psn).Scan(&uid)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
s.cryptConn.SendPacket(s.makeSignResponse(0))
|
||||
return
|
||||
}
|
||||
s.sendCode(SIGN_EABORT)
|
||||
return
|
||||
}
|
||||
s.authenticate(reqUsername, "")
|
||||
s.cryptConn.SendPacket(s.makeSignResponse(uid))
|
||||
}
|
||||
|
||||
func (s *Session) handlePSNLink(bf *byteframe.ByteFrame) {
|
||||
_ = bf.ReadNullTerminatedBytes() // Client ID
|
||||
credentials := strings.Split(stringsupport.SJISToUTF8(bf.ReadNullTerminatedBytes()), "\n")
|
||||
token := string(bf.ReadNullTerminatedBytes())
|
||||
uid, resp := s.server.validateLogin(credentials[0], credentials[1])
|
||||
if resp == SIGN_SUCCESS && uid > 0 {
|
||||
var psn string
|
||||
err := s.server.db.QueryRow(`SELECT psn_id FROM sign_sessions WHERE token = $1`, token).Scan(&psn)
|
||||
if err != nil {
|
||||
s.sendCode(SIGN_ECOGLINK)
|
||||
return
|
||||
}
|
||||
|
||||
// Since we check for the psn_id, this will never run
|
||||
var exists int
|
||||
err = s.server.db.QueryRow(`SELECT count(*) FROM users WHERE psn_id = $1`, psn).Scan(&exists)
|
||||
if err != nil {
|
||||
s.sendCode(SIGN_ECOGLINK)
|
||||
return
|
||||
} else if exists > 0 {
|
||||
s.sendCode(SIGN_EPSI)
|
||||
return
|
||||
}
|
||||
|
||||
var currentPSN string
|
||||
err = s.server.db.QueryRow(`SELECT COALESCE(psn_id, '') FROM users WHERE username = $1`, credentials[0]).Scan(¤tPSN)
|
||||
if err != nil {
|
||||
s.sendCode(SIGN_ECOGLINK)
|
||||
return
|
||||
} else if currentPSN != "" {
|
||||
s.sendCode(SIGN_EMBID)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = s.server.db.Exec(`UPDATE users SET psn_id = $1 WHERE username = $2`, psn, credentials[0])
|
||||
if err == nil {
|
||||
s.sendCode(SIGN_SUCCESS)
|
||||
return
|
||||
}
|
||||
}
|
||||
s.sendCode(SIGN_ECOGLINK)
|
||||
}
|
||||
|
||||
func (s *Session) handleDSGN(bf *byteframe.ByteFrame) {
|
||||
reqUsername := string(bf.ReadNullTerminatedBytes())
|
||||
reqPassword := string(bf.ReadNullTerminatedBytes())
|
||||
user := stringsupport.SJISToUTF8(bf.ReadNullTerminatedBytes())
|
||||
pass := stringsupport.SJISToUTF8(bf.ReadNullTerminatedBytes())
|
||||
_ = string(bf.ReadNullTerminatedBytes()) // Unk
|
||||
s.authenticate(reqUsername, reqPassword)
|
||||
s.authenticate(user, pass)
|
||||
}
|
||||
|
||||
func (s *Session) sendCode(id RespID) {
|
||||
s.cryptConn.SendPacket([]byte{byte(id)})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user