mirror of
https://github.com/Mezeporta/Erupe.git
synced 2025-12-16 08:55:31 +01:00
Initial chat prototype
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
package channelserver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
"github.com/Andoryuuta/Erupe/config"
|
||||
"github.com/Andoryuuta/Erupe/network/mhfpacket"
|
||||
"github.com/Andoryuuta/byteframe"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Config struct allows configuring the server.
|
||||
type Config struct {
|
||||
Logger *zap.Logger
|
||||
DB *sql.DB
|
||||
DB *sqlx.DB
|
||||
ErupeConfig *config.Config
|
||||
}
|
||||
|
||||
@@ -21,7 +23,7 @@ type Config struct {
|
||||
type Server struct {
|
||||
sync.Mutex
|
||||
logger *zap.Logger
|
||||
db *sql.DB
|
||||
db *sqlx.DB
|
||||
erupeConfig *config.Config
|
||||
acceptConns chan net.Conn
|
||||
deleteConns chan net.Conn
|
||||
@@ -29,6 +31,9 @@ type Server struct {
|
||||
listener net.Listener // Listener that is created when Server.Start is called.
|
||||
|
||||
isShuttingDown bool
|
||||
|
||||
gameObjectLock sync.Mutex
|
||||
gameObjectCount uint32
|
||||
}
|
||||
|
||||
// NewServer creates a new Server type.
|
||||
@@ -117,3 +122,22 @@ func (s *Server) manageSessions() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// BroadcastMHF queues a MHFPacket to be sent to all sessions.
|
||||
func (s *Server) BroadcastMHF(pkt mhfpacket.MHFPacket, ignoredSession *Session) {
|
||||
// Make the header
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteUint16(uint16(pkt.Opcode()))
|
||||
|
||||
// Build the packet onto the byteframe.
|
||||
pkt.Build(bf)
|
||||
|
||||
// Broadcast the data.
|
||||
for _, session := range s.sessions {
|
||||
if session == ignoredSession {
|
||||
continue
|
||||
}
|
||||
// Enqueue in a non-blocking way that drops the packet if the connections send buffer channel is full.
|
||||
session.QueueSendNonBlocking(bf.Data())
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,7 +3,6 @@ package channelserver
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"sync"
|
||||
|
||||
@@ -16,48 +15,104 @@ import (
|
||||
// Session holds state for the channel server connection.
|
||||
type Session struct {
|
||||
sync.Mutex
|
||||
logger *zap.Logger
|
||||
server *Server
|
||||
rawConn net.Conn
|
||||
cryptConn *network.CryptConn
|
||||
logger *zap.Logger
|
||||
server *Server
|
||||
rawConn net.Conn
|
||||
cryptConn *network.CryptConn
|
||||
sendPackets chan []byte
|
||||
|
||||
stageID string
|
||||
charID uint32
|
||||
}
|
||||
|
||||
// NewSession creates a new Session type.
|
||||
func NewSession(server *Server, conn net.Conn) *Session {
|
||||
s := &Session{
|
||||
logger: server.logger,
|
||||
server: server,
|
||||
rawConn: conn,
|
||||
cryptConn: network.NewCryptConn(conn),
|
||||
logger: server.logger,
|
||||
server: server,
|
||||
rawConn: conn,
|
||||
cryptConn: network.NewCryptConn(conn),
|
||||
sendPackets: make(chan []byte, 20),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Start starts the session packet read&handle loop.
|
||||
// Start starts the session packet send and recv loop(s).
|
||||
func (s *Session) Start() {
|
||||
go func() {
|
||||
s.logger.Info("Channel server got connection!")
|
||||
s.logger.Info("Channel server got connection!", zap.String("remoteaddr", s.rawConn.RemoteAddr().String()))
|
||||
|
||||
// Unlike the sign and entrance server,
|
||||
// the client DOES NOT initalize the channel connection with 8 NULL bytes.
|
||||
|
||||
for {
|
||||
pkt, err := s.cryptConn.ReadPacket()
|
||||
if err != nil {
|
||||
s.logger.Warn("Error on channel server readpacket", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
s.handlePacketGroup(pkt)
|
||||
|
||||
}
|
||||
go s.sendLoop()
|
||||
s.recvLoop()
|
||||
}()
|
||||
}
|
||||
|
||||
var loadDataCount int
|
||||
var getPaperDataCount int
|
||||
// QueueSend queues a packet (raw []byte) to be sent.
|
||||
func (s *Session) QueueSend(data []byte) {
|
||||
s.sendPackets <- data
|
||||
}
|
||||
|
||||
// QueueSendNonBlocking queues a packet (raw []byte) to be sent, dropping the packet entirely if the queue is full.
|
||||
func (s *Session) QueueSendNonBlocking(data []byte) {
|
||||
select {
|
||||
case s.sendPackets <- data:
|
||||
// Enqueued properly.
|
||||
default:
|
||||
// Couldn't enqueue, likely something wrong with the connection.
|
||||
s.logger.Warn("Dropped packet for session because of full send buffer, something is probably wrong")
|
||||
}
|
||||
}
|
||||
|
||||
// QueueSendMHF queues a MHFPacket to be sent.
|
||||
func (s *Session) QueueSendMHF(pkt mhfpacket.MHFPacket) {
|
||||
// Make the header
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteUint16(uint16(pkt.Opcode()))
|
||||
|
||||
// Build the packet onto the byteframe.
|
||||
pkt.Build(bf)
|
||||
|
||||
// Queue it.
|
||||
s.QueueSend(bf.Data())
|
||||
}
|
||||
|
||||
// QueueAck is a helper function to queue an MSG_SYS_ACK with the given ack handle and data.
|
||||
func (s *Session) QueueAck(ackHandle uint32, data []byte) {
|
||||
bf := byteframe.NewByteFrame()
|
||||
bf.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bf.WriteUint32(ackHandle)
|
||||
bf.WriteBytes(data)
|
||||
s.QueueSend(bf.Data())
|
||||
}
|
||||
|
||||
func (s *Session) sendLoop() {
|
||||
for {
|
||||
// TODO(Andoryuuta): Test making this into a buffered channel and grouping the packet together before sending.
|
||||
rawPacket := <-s.sendPackets
|
||||
if rawPacket == nil {
|
||||
s.logger.Debug("Got nil from s.SendPackets, exiting send loop")
|
||||
return
|
||||
}
|
||||
s.cryptConn.SendPacket(rawPacket)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) recvLoop() {
|
||||
for {
|
||||
pkt, err := s.cryptConn.ReadPacket()
|
||||
if err != nil {
|
||||
s.logger.Warn("Error on ReadPacket, exiting recv loop", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
s.handlePacketGroup(pkt)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Session) handlePacketGroup(pktGroup []byte) {
|
||||
// This shouldn't be needed, but it's better to recover and let the connection die than to panic the server.
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("Recovered from panic.")
|
||||
@@ -67,370 +122,30 @@ func (s *Session) handlePacketGroup(pktGroup []byte) {
|
||||
bf := byteframe.NewByteFrameFromBytes(pktGroup)
|
||||
opcode := network.PacketID(bf.ReadUint16())
|
||||
|
||||
if opcode != network.MSG_SYS_END {
|
||||
// Print any (non-common spam) packet opcodes and data.
|
||||
if opcode != network.MSG_SYS_END &&
|
||||
opcode != network.MSG_SYS_PING &&
|
||||
opcode != network.MSG_SYS_NOP &&
|
||||
opcode != network.MSG_SYS_TIME &&
|
||||
opcode != network.MSG_SYS_EXTEND_THRESHOLD {
|
||||
fmt.Printf("Opcode: %s\n", opcode)
|
||||
fmt.Printf("Data:\n%s\n", hex.Dump(pktGroup))
|
||||
}
|
||||
|
||||
switch opcode {
|
||||
case network.MSG_MHF_ENUMERATE_EVENT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_ENUMERATE_QUEST:
|
||||
fallthrough
|
||||
case network.MSG_MHF_ENUMERATE_RANKING:
|
||||
fallthrough
|
||||
case network.MSG_MHF_READ_MERCENARY_W:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_ETC_POINTS:
|
||||
fallthrough
|
||||
case network.MSG_MHF_READ_GUILDCARD:
|
||||
fallthrough
|
||||
case network.MSG_MHF_READ_BEAT_LEVEL:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_EARTH_STATUS:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_EARTH_VALUE:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_WEEKLY_SCHEDULE:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LIST_MEMBER:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_PLATE_DATA:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_PLATE_BOX:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_FAVORITE_QUEST:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_PARTNER:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_TOWER_INFO:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_OTOMO_AIROU:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_DECO_MYSET:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_HUNTER_NAVI:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_UD_SCHEDULE:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_UD_INFO:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_UD_MONSTER_POINT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_RAND_FROM_TABLE:
|
||||
fallthrough
|
||||
case network.MSG_MHF_ACQUIRE_MONTHLY_REWARD:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_PLATE_MYSET:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_RENGOKU_DATA:
|
||||
fallthrough
|
||||
case network.MSG_MHF_ENUMERATE_SHOP:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_SCENARIO_DATA:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_BOOST_TIME_LIMIT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_BOOST_RIGHT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_REWARD_SONG:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_GACHA_POINT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_KOURYOU_POINT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_ENHANCED_MINIDATA:
|
||||
|
||||
ackHandle := bf.ReadUint32()
|
||||
|
||||
data, err := ioutil.ReadFile(fmt.Sprintf("bin_resp/%s_resp.bin", opcode.String()))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteBytes(data)
|
||||
s.cryptConn.SendPacket(bfw.Data())
|
||||
|
||||
case network.MSG_MHF_INFO_FESTA:
|
||||
ackHandle := bf.ReadUint32()
|
||||
_ = bf.ReadUint32()
|
||||
|
||||
data, err := ioutil.ReadFile(fmt.Sprintf("bin_resp/%s_resp.bin", opcode.String()))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteBytes(data)
|
||||
s.cryptConn.SendPacket(bfw.Data())
|
||||
|
||||
case network.MSG_MHF_LOADDATA:
|
||||
ackHandle := bf.ReadUint32()
|
||||
|
||||
data, err := ioutil.ReadFile(fmt.Sprintf("bin_resp/%s_resp%d.bin", opcode.String(), loadDataCount))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteBytes(data)
|
||||
s.cryptConn.SendPacket(bfw.Data())
|
||||
|
||||
loadDataCount++
|
||||
if loadDataCount > 1 {
|
||||
loadDataCount = 0
|
||||
}
|
||||
case network.MSG_MHF_GET_PAPER_DATA:
|
||||
ackHandle := bf.ReadUint32()
|
||||
|
||||
data, err := ioutil.ReadFile(fmt.Sprintf("bin_resp/%s_resp%d.bin", opcode.String(), getPaperDataCount))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteBytes(data)
|
||||
s.cryptConn.SendPacket(bfw.Data())
|
||||
|
||||
getPaperDataCount++
|
||||
if getPaperDataCount > 7 {
|
||||
getPaperDataCount = 0
|
||||
}
|
||||
default:
|
||||
// Get the packet parser and handler for this opcode.
|
||||
mhfPkt := mhfpacket.FromOpcode(opcode)
|
||||
if mhfPkt == nil {
|
||||
fmt.Println("Got opcode which we don't know how to parse, can't parse anymore for this group")
|
||||
return
|
||||
}
|
||||
|
||||
// Parse and handle the packet
|
||||
mhfPkt.Parse(bf)
|
||||
handlerTable[opcode](s, mhfPkt)
|
||||
break
|
||||
// Get the packet parser and handler for this opcode.
|
||||
mhfPkt := mhfpacket.FromOpcode(opcode)
|
||||
if mhfPkt == nil {
|
||||
fmt.Println("Got opcode which we don't know how to parse, can't parse anymore for this group")
|
||||
return
|
||||
}
|
||||
|
||||
// Parse and handle the packet
|
||||
mhfPkt.Parse(bf)
|
||||
handlerTable[opcode](s, mhfPkt)
|
||||
|
||||
// If there is more data on the stream that the .Parse method didn't read, then read another packet off it.
|
||||
remainingData := bf.DataFromCurrent()
|
||||
if len(remainingData) >= 2 && (opcode == network.MSG_SYS_TIME || opcode == network.MSG_MHF_INFO_FESTA || opcode == network.MSG_SYS_EXTEND_THRESHOLD) {
|
||||
if len(remainingData) >= 2 {
|
||||
s.handlePacketGroup(remainingData)
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func handlePacket(cc *network.CryptConn, pkt []byte) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
fmt.Println("Recovered from panic.")
|
||||
}
|
||||
}()
|
||||
|
||||
bf := byteframe.NewByteFrameFromBytes(pkt)
|
||||
opcode := network.PacketID(bf.ReadUint16())
|
||||
|
||||
if opcode == network.MSG_SYS_EXTEND_THRESHOLD {
|
||||
opcode = network.PacketID(bf.ReadUint16())
|
||||
}
|
||||
|
||||
fmt.Printf("Opcode: %s\n", opcode)
|
||||
switch opcode {
|
||||
case network.MSG_SYS_PING:
|
||||
ackHandle := bf.ReadUint32()
|
||||
_ = bf.ReadUint16()
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteUint32(0)
|
||||
bfw.WriteUint32(0)
|
||||
cc.SendPacket(bfw.Data())
|
||||
case network.MSG_SYS_TIME:
|
||||
_ = bf.ReadUint8()
|
||||
timestamp := bf.ReadUint32() // unix timestamp, e.g. 1577105879
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_TIME))
|
||||
bfw.WriteUint8(0)
|
||||
bfw.WriteUint32(timestamp)
|
||||
cc.SendPacket(bfw.Data())
|
||||
case network.MSG_SYS_LOGIN:
|
||||
ackHandle := bf.ReadUint32()
|
||||
charID0 := bf.ReadUint32()
|
||||
loginTokenNumber := bf.ReadUint32()
|
||||
hardcodedZero0 := bf.ReadUint16()
|
||||
requestVersion := bf.ReadUint16()
|
||||
charID1 := bf.ReadUint32()
|
||||
hardcodedZero1 := bf.ReadUint16()
|
||||
loginTokenLength := bf.ReadUint16() // hardcoded to 0x11
|
||||
loginTokenString := bf.ReadBytes(17)
|
||||
|
||||
_ = ackHandle
|
||||
_ = charID0
|
||||
_ = loginTokenNumber
|
||||
_ = hardcodedZero0
|
||||
_ = requestVersion
|
||||
_ = charID1
|
||||
_ = hardcodedZero1
|
||||
_ = loginTokenLength
|
||||
_ = loginTokenString
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteUint64(0x000000005E00B9C2) // Timestamp?
|
||||
cc.SendPacket(bfw.Data())
|
||||
|
||||
case network.MSG_MHF_ENUMERATE_EVENT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_ENUMERATE_QUEST:
|
||||
fallthrough
|
||||
case network.MSG_MHF_ENUMERATE_RANKING:
|
||||
fallthrough
|
||||
case network.MSG_MHF_READ_MERCENARY_W:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_ETC_POINTS:
|
||||
fallthrough
|
||||
case network.MSG_MHF_READ_GUILDCARD:
|
||||
fallthrough
|
||||
case network.MSG_MHF_READ_BEAT_LEVEL:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_EARTH_STATUS:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_EARTH_VALUE:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_WEEKLY_SCHEDULE:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LIST_MEMBER:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_PLATE_DATA:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_PLATE_BOX:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_FAVORITE_QUEST:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_PARTNER:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_TOWER_INFO:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_OTOMO_AIROU:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_DECO_MYSET:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_HUNTER_NAVI:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_UD_SCHEDULE:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_UD_INFO:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_UD_MONSTER_POINT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_RAND_FROM_TABLE:
|
||||
fallthrough
|
||||
case network.MSG_MHF_ACQUIRE_MONTHLY_REWARD:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_RENGOKU_RANKING_RANK:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_PLATE_MYSET:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_RENGOKU_DATA:
|
||||
fallthrough
|
||||
case network.MSG_MHF_ENUMERATE_SHOP:
|
||||
fallthrough
|
||||
case network.MSG_MHF_LOAD_SCENARIO_DATA:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_BOOST_TIME_LIMIT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_BOOST_RIGHT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_REWARD_SONG:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_GACHA_POINT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_KOURYOU_POINT:
|
||||
fallthrough
|
||||
case network.MSG_MHF_GET_ENHANCED_MINIDATA:
|
||||
|
||||
ackHandle := bf.ReadUint32()
|
||||
|
||||
data, err := ioutil.ReadFile(fmt.Sprintf("bin_resp/%s_resp.bin", opcode.String()))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteBytes(data)
|
||||
cc.SendPacket(bfw.Data())
|
||||
|
||||
case network.MSG_MHF_INFO_FESTA:
|
||||
ackHandle := bf.ReadUint32()
|
||||
_ = bf.ReadUint32()
|
||||
|
||||
data, err := ioutil.ReadFile(fmt.Sprintf("bin_resp/%s_resp.bin", opcode.String()))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteBytes(data)
|
||||
cc.SendPacket(bfw.Data())
|
||||
|
||||
case network.MSG_MHF_LOADDATA:
|
||||
ackHandle := bf.ReadUint32()
|
||||
|
||||
data, err := ioutil.ReadFile(fmt.Sprintf("bin_resp/%s_resp%d.bin", opcode.String(), loadDataCount))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteBytes(data)
|
||||
cc.SendPacket(bfw.Data())
|
||||
|
||||
loadDataCount++
|
||||
if loadDataCount > 1 {
|
||||
loadDataCount = 0
|
||||
}
|
||||
case network.MSG_MHF_GET_PAPER_DATA:
|
||||
ackHandle := bf.ReadUint32()
|
||||
|
||||
data, err := ioutil.ReadFile(fmt.Sprintf("bin_resp/%s_resp%d.bin", opcode.String(), getPaperDataCount))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
bfw := byteframe.NewByteFrame()
|
||||
bfw.WriteUint16(uint16(network.MSG_SYS_ACK))
|
||||
bfw.WriteUint32(ackHandle)
|
||||
bfw.WriteBytes(data)
|
||||
cc.SendPacket(bfw.Data())
|
||||
|
||||
getPaperDataCount++
|
||||
if getPaperDataCount > 7 {
|
||||
getPaperDataCount = 0
|
||||
}
|
||||
default:
|
||||
fmt.Printf("Data:\n%s\n", hex.Dump(pkt))
|
||||
break
|
||||
}
|
||||
|
||||
remainingData := bf.DataFromCurrent()
|
||||
if len(remainingData) >= 2 && (opcode == network.MSG_SYS_TIME || opcode == network.MSG_MHF_INFO_FESTA) {
|
||||
handlePacket(cc, remainingData)
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package entranceserver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -10,6 +9,7 @@ import (
|
||||
|
||||
"github.com/Andoryuuta/Erupe/config"
|
||||
"github.com/Andoryuuta/Erupe/network"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
@@ -18,7 +18,7 @@ type Server struct {
|
||||
sync.Mutex
|
||||
logger *zap.Logger
|
||||
erupeConfig *config.Config
|
||||
db *sql.DB
|
||||
db *sqlx.DB
|
||||
listener net.Listener
|
||||
isShuttingDown bool
|
||||
}
|
||||
@@ -26,7 +26,7 @@ type Server struct {
|
||||
// Config struct allows configuring the server.
|
||||
type Config struct {
|
||||
Logger *zap.Logger
|
||||
DB *sql.DB
|
||||
DB *sqlx.DB
|
||||
ErupeConfig *config.Config
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@ package entranceserver
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
|
||||
"github.com/Andoryuuta/Erupe/config"
|
||||
@@ -32,6 +35,7 @@ func encodeServerInfo(serverInfos []config.EntranceServerInfo) []byte {
|
||||
bf.WriteUint32(si.AllowedClientFlags)
|
||||
|
||||
for channelIdx, ci := range si.Channels {
|
||||
fmt.Println("Channel idx", channelIdx)
|
||||
bf.WriteUint16(ci.Port)
|
||||
bf.WriteUint16(16 + uint16(channelIdx))
|
||||
bf.WriteUint16(ci.MaxPlayers)
|
||||
@@ -71,6 +75,7 @@ func makeHeader(data []byte, respType string, entryCount uint16, key byte) []byt
|
||||
}
|
||||
|
||||
func makeResp(servers []config.EntranceServerInfo) []byte {
|
||||
fmt.Printf("%+v\n", servers)
|
||||
rawServerData := encodeServerInfo(servers)
|
||||
|
||||
bf := byteframe.NewByteFrame()
|
||||
@@ -81,6 +86,11 @@ func makeResp(servers []config.EntranceServerInfo) []byte {
|
||||
// If so, how does it work without the entrance server connection being authenticated?
|
||||
bf.WriteBytes(makeHeader([]byte{}, "USR", 0, 0x00))
|
||||
|
||||
err := ioutil.WriteFile("go_entrance_resp.bin", bf.Data(), 0644)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return bf.Data()
|
||||
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@ package launcherserver
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -12,13 +11,14 @@ import (
|
||||
"github.com/Andoryuuta/Erupe/config"
|
||||
"github.com/gorilla/handlers"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Config struct allows configuring the server.
|
||||
type Config struct {
|
||||
Logger *zap.Logger
|
||||
DB *sql.DB
|
||||
DB *sqlx.DB
|
||||
ErupeConfig *config.Config
|
||||
UseOriginalLauncherFiles bool
|
||||
}
|
||||
@@ -28,7 +28,7 @@ type Server struct {
|
||||
sync.Mutex
|
||||
logger *zap.Logger
|
||||
erupeConfig *config.Config
|
||||
db *sql.DB
|
||||
db *sqlx.DB
|
||||
httpServer *http.Server
|
||||
useOriginalLauncherFiles bool
|
||||
isShuttingDown bool
|
||||
|
||||
@@ -13,7 +13,7 @@ func serverList(s *Server, w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w,
|
||||
`<?xml version="1.0"?><server_groups><group idx='0' nam='Erupe' ip='%s' port="%d"/></server_groups>`,
|
||||
s.erupeConfig.HostIP,
|
||||
s.erupeConfig.Entrance.Port,
|
||||
s.erupeConfig.Sign.Port,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
56
server/signserver/dbutils.go
Normal file
56
server/signserver/dbutils.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package signserver
|
||||
|
||||
import "time"
|
||||
|
||||
func (s *Server) registerDBAccount(username string, password string) error {
|
||||
_, err := s.db.Exec("INSERT INTO users (username, password) VALUES ($1, $2)", username, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var id int
|
||||
err = s.db.QueryRow("SELECT id FROM users WHERE username = $1", username).Scan(&id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create a base new character.
|
||||
_, err = s.db.Exec(`
|
||||
INSERT INTO characters (
|
||||
user_id, is_female, is_new_character, small_gr_level, gr_override_mode, name, unk_desc_string,
|
||||
gr_override_level, gr_override_unk0, gr_override_unk1, exp, weapon, last_login)
|
||||
VALUES($1, False, True, 0, True, '', '', 0, 0, 0, 0, 0, $2)`,
|
||||
id,
|
||||
uint32(time.Now().Unix()),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type character struct {
|
||||
ID uint32 `db:"id"`
|
||||
IsFemale bool `db:"is_female"`
|
||||
IsNewCharacter bool `db:"is_new_character"`
|
||||
SmallGRLevel uint8 `db:"small_gr_level"`
|
||||
GROverrideMode bool `db:"gr_override_mode"`
|
||||
Name string `db:"name"`
|
||||
UnkDescString string `db:"unk_desc_string"`
|
||||
GROverrideLevel uint16 `db:"gr_override_level"`
|
||||
GROverrideUnk0 uint8 `db:"gr_override_unk0"`
|
||||
GROverrideUnk1 uint8 `db:"gr_override_unk1"`
|
||||
Exp uint16 `db:"exp"`
|
||||
Weapon uint16 `db:"weapon"`
|
||||
LastLogin uint32 `db:"last_login"`
|
||||
}
|
||||
|
||||
func (s *Server) getCharactersForUser(uid int) ([]character, error) {
|
||||
characters := []character{}
|
||||
err := s.db.Select(&characters, "SELECT id, is_female, is_new_character, small_gr_level, gr_override_mode, name, unk_desc_string, gr_override_level, gr_override_unk0, gr_override_unk1, exp, weapon, last_login FROM characters WHERE user_id = $1", uid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return characters, nil
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
package signserver
|
||||
|
||||
import "github.com/Andoryuuta/byteframe"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/Andoryuuta/byteframe"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
func paddedString(x string, size uint) []byte {
|
||||
out := make([]byte, size)
|
||||
@@ -27,91 +32,45 @@ func makeSignInFailureResp(respID RespID) []byte {
|
||||
return bf.Data()
|
||||
}
|
||||
|
||||
func (session *Session) makeSignInResp(username string) []byte {
|
||||
bf := byteframe.NewByteFrame()
|
||||
func (s *Session) makeSignInResp(uid int) []byte {
|
||||
// Get the characters from the DB.
|
||||
chars, err := s.server.getCharactersForUser(uid)
|
||||
if err != nil {
|
||||
s.logger.Warn("Error getting characters from DB", zap.Error(err))
|
||||
}
|
||||
|
||||
// delete me:
|
||||
//bf.WriteUint8(8)
|
||||
//return bf.Data()
|
||||
bf := byteframe.NewByteFrame()
|
||||
|
||||
bf.WriteUint8(1) // resp_code
|
||||
bf.WriteUint8(0) // file/patch server count
|
||||
bf.WriteUint8(4) // entrance server count
|
||||
bf.WriteUint8(1) // character count
|
||||
bf.WriteUint8(uint8(len(chars))) // character count
|
||||
bf.WriteUint32(0xFFFFFFFF) // login_token_number
|
||||
bf.WriteBytes(paddedString("logintokenstrng", 16)) // login_token (16 byte padded string)
|
||||
bf.WriteUint32(1576761190)
|
||||
|
||||
// file patch server PascalStrings here
|
||||
|
||||
// Array(this.entrance_server_count, PascalString(Byte, "utf8")),
|
||||
uint8PascalString(bf, "localhost:53310")
|
||||
uint8PascalString(bf, fmt.Sprintf("%s:%d", s.server.erupeConfig.HostIP, s.server.erupeConfig.Entrance.Port))
|
||||
uint8PascalString(bf, "")
|
||||
uint8PascalString(bf, "")
|
||||
uint8PascalString(bf, "mhf-n.capcom.com.tw")
|
||||
|
||||
///////////////////////////
|
||||
// Characters:
|
||||
|
||||
/*
|
||||
tab = '123456789ABCDEFGHJKLMNPQRTUVWXYZ'
|
||||
def make_uid_str(cid):
|
||||
out = ''
|
||||
for i in range(6):
|
||||
v = (cid>>5*i)
|
||||
out += tab[v&0x1f]
|
||||
return out
|
||||
|
||||
def make_cid_int(uid):
|
||||
v = 0
|
||||
for c in uid[::-1]:
|
||||
idx = tab.find(c)
|
||||
if idx == -1:
|
||||
raise Exception("not in tab")
|
||||
v |= idx
|
||||
v = v<<5
|
||||
return v>>5
|
||||
*/
|
||||
bf.WriteUint32(469153291) // character ID 469153291
|
||||
bf.WriteUint16(999) // Exp, HR[x] is split by 0, 1, 30, 50, 99, 299, 998, 999
|
||||
|
||||
//44.204
|
||||
|
||||
/*
|
||||
0=大劍/Big sword
|
||||
1=重弩/Heavy crossbow
|
||||
2=大錘/Sledgehammer
|
||||
3=長槍/Spear
|
||||
4=單手劍/One-handed sword
|
||||
5=輕弩/Light crossbow
|
||||
6=雙劍/Double sword
|
||||
7=太刀/Tadao
|
||||
8=狩獵笛/Hunting flute
|
||||
9=銃槍/Shotgun
|
||||
10=弓/bow
|
||||
11=穿龍棍/Wear a dragon stick
|
||||
12=斬擊斧F/Chopping Axe F
|
||||
13=---
|
||||
default=不明/unknown
|
||||
*/
|
||||
bf.WriteUint16(7) // Weapon, 0-13.
|
||||
|
||||
bf.WriteUint32(1576761172) // Last login date, unix timestamp in seconds.
|
||||
bf.WriteUint8(1) // Sex, 0=male, 1=female.
|
||||
bf.WriteUint8(0) // Is new character, 1 replaces character name with ?????.
|
||||
grMode := uint8(0)
|
||||
bf.WriteUint8(1) // GR level if grMode == 0
|
||||
bf.WriteUint8(grMode) // GR mode.
|
||||
bf.WriteBytes(paddedString(username, 16)) // Character name
|
||||
bf.WriteBytes(paddedString("0", 32)) // unk str
|
||||
if grMode == 1 {
|
||||
bf.WriteUint16(55) // GR level override.
|
||||
bf.WriteUint8(0) // unk
|
||||
bf.WriteUint8(0) // unk
|
||||
for _, char := range chars {
|
||||
bf.WriteUint32(char.ID) // character ID 469153291
|
||||
bf.WriteUint16(char.Exp) // Exp, HR[x] is split by 0, 1, 30, 50, 99, 299, 998, 999
|
||||
bf.WriteUint16(char.Weapon) // Weapon, 0-13.
|
||||
bf.WriteUint32(char.LastLogin) // Last login date, unix timestamp in seconds.
|
||||
bf.WriteBool(char.IsFemale) // Sex, 0=male, 1=female.
|
||||
bf.WriteBool(char.IsNewCharacter) // Is new character, 1 replaces character name with ?????.
|
||||
bf.WriteUint8(char.SmallGRLevel) // GR level if grMode == 0
|
||||
bf.WriteBool(char.GROverrideMode) // GR mode.
|
||||
bf.WriteBytes(paddedString(char.Name, 16)) // Character name
|
||||
bf.WriteBytes(paddedString(char.UnkDescString, 32)) // unk str
|
||||
if char.GROverrideMode {
|
||||
bf.WriteUint16(char.GROverrideLevel) // GR level override.
|
||||
bf.WriteUint8(char.GROverrideUnk0) // unk
|
||||
bf.WriteUint8(char.GROverrideUnk1) // unk
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
|
||||
bf.WriteUint8(0) // friends_list_count
|
||||
bf.WriteUint8(0) // guild_members_count
|
||||
bf.WriteUint8(0) // notice_count
|
||||
|
||||
@@ -95,6 +95,25 @@ func (s *Session) handleDSGNRequest(bf *byteframe.ByteFrame) error {
|
||||
case err == sql.ErrNoRows:
|
||||
s.logger.Info("Account not found", zap.String("reqUsername", reqUsername))
|
||||
serverRespBytes = makeSignInFailureResp(SIGN_EAUTH)
|
||||
|
||||
// HACK(Andoryuuta): Create a new account if it doesn't exit.
|
||||
s.logger.Info("Creating account", zap.String("reqUsername", reqUsername), zap.String("reqPassword", reqPassword))
|
||||
err = s.server.registerDBAccount(reqUsername, reqPassword)
|
||||
if err != nil {
|
||||
s.logger.Info("Error on creating new account", zap.Error(err))
|
||||
serverRespBytes = makeSignInFailureResp(SIGN_EABORT)
|
||||
break
|
||||
}
|
||||
|
||||
var id int
|
||||
err = s.server.db.QueryRow("SELECT id FROM users WHERE username = $1", reqUsername).Scan(&id)
|
||||
if err != nil {
|
||||
s.logger.Info("Error on querying account id", zap.Error(err))
|
||||
serverRespBytes = makeSignInFailureResp(SIGN_EABORT)
|
||||
break
|
||||
}
|
||||
|
||||
serverRespBytes = s.makeSignInResp(id)
|
||||
break
|
||||
case err != nil:
|
||||
serverRespBytes = makeSignInFailureResp(SIGN_EABORT)
|
||||
@@ -103,7 +122,7 @@ func (s *Session) handleDSGNRequest(bf *byteframe.ByteFrame) error {
|
||||
default:
|
||||
if reqPassword == password {
|
||||
s.logger.Info("Passwords match!")
|
||||
serverRespBytes = s.makeSignInResp(reqUsername)
|
||||
serverRespBytes = s.makeSignInResp(id)
|
||||
} else {
|
||||
s.logger.Info("Passwords don't match!")
|
||||
serverRespBytes = makeSignInFailureResp(SIGN_EPASS)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package signserver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
@@ -9,13 +8,14 @@ import (
|
||||
|
||||
"github.com/Andoryuuta/Erupe/config"
|
||||
"github.com/Andoryuuta/Erupe/network"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Config struct allows configuring the server.
|
||||
type Config struct {
|
||||
Logger *zap.Logger
|
||||
DB *sql.DB
|
||||
DB *sqlx.DB
|
||||
ErupeConfig *config.Config
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ type Server struct {
|
||||
erupeConfig *config.Config
|
||||
sid int
|
||||
sessions map[int]*Session
|
||||
db *sql.DB
|
||||
db *sqlx.DB
|
||||
listener net.Listener
|
||||
isShuttingDown bool
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user