diff --git a/build.sh b/build.sh index c2dbab7..8822847 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ #!/bin/sh mkdir -p bin -CGO_ENABLED=0 GOOS=linux go build -o ./bin/server +CGO_ENABLED=0 GOOS=linux go build -o ./bin/server ./cmd echo 'Done' \ No newline at end of file diff --git a/cmd/login.go b/cmd/login.go new file mode 100644 index 0000000..d5d9203 --- /dev/null +++ b/cmd/login.go @@ -0,0 +1,42 @@ +package main + +import ( + "context" + "flag" + "log" + + "github.com/CPunch/gopenfusion/config" + "github.com/CPunch/gopenfusion/login" + "github.com/google/subcommands" +) + +type loginCommand struct { + port int +} + +func (s *loginCommand) Name() string { + return "login" +} + +func (s *loginCommand) Synopsis() string { + return "Starts login service" +} + +func (s *loginCommand) Usage() string { + return s.Name() + " - " + s.Synopsis() + ":\n" +} + +func (s *loginCommand) SetFlags(f *flag.FlagSet) { + f.IntVar(&s.port, "port", config.LOGIN_PORT, "Hosts the service on this port") +} + +func (s *loginCommand) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + loginServer, err := login.NewLoginServer(dbHndlr, redisHndlr, s.port) + if err != nil { + log.Panicf("failed to create shard server: %v", err) + } + + loginServer.Start() + + return subcommands.ExitSuccess +} diff --git a/cmd/main.go b/cmd/main.go new file mode 100644 index 0000000..cbd71b6 --- /dev/null +++ b/cmd/main.go @@ -0,0 +1,43 @@ +package main + +import ( + "context" + "flag" + "log" + "os" + + "github.com/CPunch/gopenfusion/config" + "github.com/CPunch/gopenfusion/core/db" + "github.com/CPunch/gopenfusion/core/redis" + + "github.com/google/subcommands" +) + +var dbHndlr *db.DBHandler +var redisHndlr *redis.RedisHandler + +func main() { + subcommands.Register(subcommands.HelpCommand(), "") + subcommands.Register(subcommands.FlagsCommand(), "") + subcommands.Register(subcommands.CommandsCommand(), "") + subcommands.Register(&shardCommand{}, "") + subcommands.Register(&loginCommand{}, "") + + var err error + dbHndlr, err = db.OpenPostgresDB(config.GetDBAddr()) + if err != nil { + log.Panicf("failed to open db: %v", err) + } + + if err = dbHndlr.Setup(); err != nil { + log.Panicf("failed to setup db: %v", err) + } + + redisHndlr, err = redis.OpenRedis(config.GetRedisAddr()) + if err != nil { + log.Panicf("failed to open redis: %v", err) + } + + flag.Parse() + os.Exit(int(subcommands.Execute(context.Background()))) +} diff --git a/cmd/shard.go b/cmd/shard.go new file mode 100644 index 0000000..fef22f0 --- /dev/null +++ b/cmd/shard.go @@ -0,0 +1,42 @@ +package main + +import ( + "context" + "flag" + "log" + + "github.com/CPunch/gopenfusion/config" + "github.com/CPunch/gopenfusion/shard" + "github.com/google/subcommands" +) + +type shardCommand struct { + port int +} + +func (s *shardCommand) Name() string { + return "shard" +} + +func (s *shardCommand) Synopsis() string { + return "Starts shard service" +} + +func (s *shardCommand) Usage() string { + return s.Name() + " - " + s.Synopsis() + ":\n" +} + +func (s *shardCommand) SetFlags(f *flag.FlagSet) { + f.IntVar(&s.port, "port", config.SHARD_PORT, "Hosts the service on this port") +} + +func (s *shardCommand) Execute(ctx context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { + shardServer, err := shard.NewShardServer(dbHndlr, redisHndlr, s.port) + if err != nil { + log.Panicf("failed to create shard server: %v", err) + } + + shardServer.Start() + + return subcommands.ExitSuccess +} diff --git a/compose.yaml b/compose.yaml index 8e20a2b..ad51767 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,19 +1,42 @@ services: - # redis: - # image: 'redislabs/redismod' - # ports: - # - '6379:6379' + postgresql: + image: 'postgres:15' + environment: + - POSTGRES_USER=gopenfusion + - POSTGRES_DB=gopenfusion + - POSTGRES_PASSWORD=gopenfusion + redis: + image: 'redis:7-alpine' login: restart: on-failure build: . hostname: login + command: login + environment: + - DB_ADDR=postgresql:5432 + - DB_USER=gopenfusion + - DB_PASS=gopenfusion + - DB_NAME=gopenfusion + - REDIS_ADDR=redis:6379 + ports: + - '23000:23000' + links: + - postgresql + - redis + shard0: + restart: on-failure + build: . + hostname: shard + command: shard + environment: + - DB_ADDR=postgresql:5432 + - DB_USER=gopenfusion + - DB_PASS=gopenfusion + - DB_NAME=gopenfusion + - REDIS_ADDR=redis:6379 ports: - '23001:23001' - - '23000:23000' - # shard0: - # restart: on-failure - # build: . - # hostname: shard0 - # ports: - # - '23001:23001' + links: + - postgresql + - redis diff --git a/config/config.go b/config/config.go index 9738de0..fd72a12 100644 --- a/config/config.go +++ b/config/config.go @@ -1,5 +1,21 @@ package config +import ( + "os" + "time" +) + +/* + Available environment variables: + REDIS_ADDR + REDIS_USER + REDIS_PASS + DB_ADDR + DB_USER + DB_PASS + ANNOUNCE_IP +*/ + const ( AEQUIP_COUNT = 9 AINVEN_COUNT = 50 @@ -15,9 +31,49 @@ var ( LOGIN_PORT = 23000 SHARD_PORT = 23001 - SHARD_IP = "127.0.0.1" + + LOGIN_TIMEOUT = time.Second * 30 ) +func getEnv(key string, fallback string) string { + if value, ok := os.LookupEnv(key); ok { + return value + } + + return fallback +} + func GetMaxHP(level int) int { return (925 + 75*(level)) } + +func GetRedisAddr() string { + return getEnv("REDIS_ADDR", "localhost:6379") +} + +func GetRedisCredentials() func() (string, string) { + return func() (string, string) { + return getEnv("REDIS_USER", ""), getEnv("REDIS_PASS", "") + } +} + +func GetDBName() string { + return getEnv("DB_NAME", "postgres") +} + +func GetDBAddr() string { + return getEnv("DB_ADDR", "localhost:5432") +} + +func GetDBUser() string { + return getEnv("DB_USER", "postgres") +} + +func GetDBPass() string { + return getEnv("DB_PASS", "") +} + +// needed for shard +func GetAnnounceIP() string { + return getEnv("ANNOUNCE_IP", "127.0.0.1") +} diff --git a/core/db/account.go b/core/db/account.go index 675d6ff..f1e2f31 100644 --- a/core/db/account.go +++ b/core/db/account.go @@ -2,24 +2,25 @@ package db import ( "fmt" + "log" "golang.org/x/crypto/bcrypt" "github.com/CPunch/gopenfusion/core/protocol" - "github.com/blockloop/scan" + "github.com/georgysavva/scany/v2/sqlscan" ) type Account struct { - AccountID int - Login string - Password string - Selected int - AccountLevel int - Created int - LastLogin int - BannedUntil int - BannedSince int - BanReason string + AccountID int `db:"accountid"` + Login string `db:"login"` + Password string `db:"password"` + Selected int `db:"selected"` + AccountLevel int `db:"accountlevel"` + Created int `db:"created"` + LastLogin int `db:"lastlogin"` + BannedUntil int `db:"banneduntil"` + BannedSince int `db:"bannedsince"` + BanReason string `db:"banreason"` } func (db *DBHandler) NewAccount(Login, Password string) (*Account, error) { @@ -28,13 +29,14 @@ func (db *DBHandler) NewAccount(Login, Password string) (*Account, error) { return nil, err } - row, err := db.Query("INSERT INTO Accounts (Login, Password, AccountLevel) VALUES(?, ?, ?) RETURNING *", Login, hash, protocol.CN_ACCOUNT_LEVEL__USER) + row, err := db.Query("INSERT INTO Accounts (Login, Password, AccountLevel) VALUES($1, $2, $3) RETURNING *", Login, hash, protocol.CN_ACCOUNT_LEVEL__USER) if err != nil { return nil, err } var account Account - if err := scan.Row(&account, row); err != nil { + row.Next() + if err := sqlscan.ScanRow(&account, row); err != nil { return nil, err } @@ -47,13 +49,15 @@ var ( ) func (db *DBHandler) TryLogin(Login, Password string) (*Account, error) { - row, err := db.Query("SELECT * FROM Accounts WHERE Login=?", Login) + row, err := db.Query("SELECT * FROM Accounts WHERE Login=$1", Login) if err != nil { return nil, err } var account Account - if err := scan.Row(&account, row); err != nil { + row.Next() + if err := sqlscan.ScanRow(&account, row); err != nil { + log.Printf("Error scanning row: %v", err) return nil, LoginErrorInvalidID } diff --git a/core/db/inventory.go b/core/db/inventory.go index a2e99d9..5376c67 100644 --- a/core/db/inventory.go +++ b/core/db/inventory.go @@ -7,17 +7,17 @@ import ( ) type Inventory struct { - PlayerID int - Slot int - ID int - Type int - Opt int - TimeLimit int + PlayerID int `db:"playerid"` + Slot int `db:"slot"` + ID int `db:"id"` + Type int `db:"type"` + Opt int `db:"opt"` + TimeLimit int `db:"timelimit"` } // start && end are both inclusive func (db *DBHandler) GetPlayerInventorySlots(PlayerID int, start int, end int) ([]protocol.SItemBase, error) { - rows, err := db.Query("SELECT Slot, Type, ID, Opt, TimeLimit FROM Inventory WHERE Slot BETWEEN ? AND ? AND PlayerID = ?", start, end, PlayerID) + rows, err := db.Query("SELECT Slot, Type, ID, Opt, TimeLimit FROM Inventory WHERE Slot BETWEEN $1 AND $2 AND PlayerID = $3", start, end, PlayerID) if err != nil { return nil, err } @@ -42,7 +42,7 @@ func (db *DBHandler) GetPlayerInventorySlots(PlayerID int, start int, end int) ( func (db *DBHandler) SetPlayerInventorySlots(PlayerID int, start int, items []protocol.SItemBase) error { return db.Transaction(func(tx *sql.Tx) error { // delete inventory slots - _, err := db.Exec("DELETE FROM Inventory WHERE Slot BETWEEN ? AND ? AND PlayerID = ?", start, start+len(items)-1, PlayerID) + _, err := db.Exec("DELETE FROM Inventory WHERE Slot BETWEEN $1 AND $2 AND PlayerID = $3", start, start+len(items)-1, PlayerID) if err != nil { return err } @@ -50,7 +50,7 @@ func (db *DBHandler) SetPlayerInventorySlots(PlayerID int, start int, items []pr // insert inventory for i, item := range items { if item.IID != 0 { - _, err := db.Exec("INSERT INTO Inventory (PlayerID, Slot, ID, Type, Opt, TimeLimit) VALUES (?, ?, ?, ?, ?, ?)", PlayerID, start+i, item.IID, item.IType, item.IOpt, item.ITimeLimit) + _, err := db.Exec("INSERT INTO Inventory (PlayerID, Slot, ID, Type, Opt, TimeLimit) VALUES ($1, $2, $3, $4, $5, $6)", PlayerID, start+i, item.IID, item.IType, item.IOpt, item.ITimeLimit) if err != nil { return err } diff --git a/core/db/migrations/new.sql b/core/db/migrations/new.sql index 96544f2..77b4f69 100644 --- a/core/db/migrations/new.sql +++ b/core/db/migrations/new.sql @@ -1,29 +1,31 @@ --- this file has been lifted from https://github.com/OpenFusionProject/OpenFusion/blob/master/sql/tables.sql +-- this file has been lifted from https://github.com/OpenFusionProject/OpenFusion/bytea/master/sql/tables.sql -- all credit to original contributors! +CREATE EXTENSION IF NOT EXISTS citext; + CREATE TABLE IF NOT EXISTS Accounts ( - AccountID INTEGER NOT NULL, - Login TEXT NOT NULL UNIQUE COLLATE NOCASE, + AccountID SERIAL NOT NULL, + Login CITEXT NOT NULL UNIQUE, Password TEXT NOT NULL, Selected INTEGER DEFAULT 1 NOT NULL, AccountLevel INTEGER NOT NULL, - Created INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL, - LastLogin INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL, + Created INTEGER DEFAULT (extract(EPOCH FROM current_time)) NOT NULL, + LastLogin INTEGER DEFAULT (extract(EPOCH FROM current_time)) NOT NULL, BannedUntil INTEGER DEFAULT 0 NOT NULL, BannedSince INTEGER DEFAULT 0 NOT NULL, BanReason TEXT DEFAULT '' NOT NULL, - PRIMARY KEY(AccountID AUTOINCREMENT) + PRIMARY KEY(AccountID) ); CREATE TABLE IF NOT EXISTS Players ( - PlayerID INTEGER NOT NULL, + PlayerID SERIAL NOT NULL, AccountID INTEGER NOT NULL, - FirstName TEXT NOT NULL COLLATE NOCASE, - LastName TEXT NOT NULL COLLATE NOCASE, + FirstName CITEXT NOT NULL, + LastName CITEXT NOT NULL, NameCheck INTEGER NOT NULL, Slot INTEGER NOT NULL, - Created INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL, - LastLogin INTEGER DEFAULT (strftime('%s', 'now')) NOT NULL, + Created INTEGER DEFAULT (extract(EPOCH FROM current_time)) NOT NULL, + LastLogin INTEGER DEFAULT (extract(EPOCH FROM current_time)) NOT NULL, Level INTEGER DEFAULT 1 NOT NULL, Nano1 INTEGER DEFAULT 0 NOT NULL, Nano2 INTEGER DEFAULT 0 NOT NULL, @@ -43,10 +45,10 @@ CREATE TABLE IF NOT EXISTS Players ( Mentor INTEGER DEFAULT 5 NOT NULL, CurrentMissionID INTEGER DEFAULT 0 NOT NULL, WarpLocationFlag INTEGER DEFAULT 0 NOT NULL, - SkywayLocationFlag BLOB NOT NULL, - FirstUseFlag BLOB NOT NULL, - Quests BLOB NOT NULL, - PRIMARY KEY(PlayerID AUTOINCREMENT), + SkywayLocationFlag bytea NOT NULL, + FirstUseFlag bytea NOT NULL, + Quests bytea NOT NULL, + PRIMARY KEY(PlayerID), FOREIGN KEY(AccountID) REFERENCES Accounts(AccountID) ON DELETE CASCADE, UNIQUE (AccountID, Slot), UNIQUE (FirstName, LastName) @@ -123,8 +125,8 @@ CREATE TABLE IF NOT EXISTS EmailData ( ReadFlag INTEGER NOT NULL, ItemFlag INTEGER NOT NULL, SenderID INTEGER NOT NULL, - SenderFirstName TEXT NOT NULL COLLATE NOCASE, - SenderLastName TEXT NOT NULL COLLATE NOCASE, + SenderFirstName CITEXT NOT NULL, + SenderLastName CITEXT NOT NULL, SubjectLine TEXT NOT NULL, MsgBody TEXT NOT NULL, Taros INTEGER NOT NULL, diff --git a/core/db/players.go b/core/db/players.go index 0f41d70..ab0885a 100644 --- a/core/db/players.go +++ b/core/db/players.go @@ -19,19 +19,19 @@ func (db *DBHandler) NewPlayer(AccountID int, FirstName, LastName string, slot i var PlayerID int if err := db.Transaction(func(tx *sql.Tx) error { // create player - row, err := tx.Query( - "INSERT INTO Players (AccountID, Slot, FirstName, LastName, XCoordinate, YCoordinate, ZCoordinate, Angle, HP, NameCheck, Quests, SkywayLocationFlag, FirstUseFlag) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING PlayerID", + rows, err := tx.Query( + "INSERT INTO Players (AccountID, Slot, FirstName, LastName, XCoordinate, YCoordinate, ZCoordinate, Angle, HP, NameCheck, Quests, SkywayLocationFlag, FirstUseFlag) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING PlayerID", AccountID, slot, FirstName, LastName, config.SPAWN_X, config.SPAWN_Y, config.SPAWN_Z, 0, config.GetMaxHP(1), nameCheck, QuestFlag, SkywayLocationFlag, FirstUseFlag) if err != nil { return err } - if err := scan.Row(&PlayerID, row); err != nil { + if err := scan.Row(&PlayerID, rows); err != nil { return err } // create appearance - if _, err := tx.Exec("INSERT INTO Appearances (PlayerID) VALUES (?)", PlayerID); err != nil { + if _, err := tx.Exec("INSERT INTO Appearances (PlayerID) VALUES ($1)", PlayerID); err != nil { return err } @@ -47,13 +47,13 @@ func (db *DBHandler) NewPlayer(AccountID int, FirstName, LastName string, slot i func (db *DBHandler) FinishPlayer(character *protocol.SP_CL2LS_REQ_CHAR_CREATE, AccountId int) error { return db.Transaction(func(tx *sql.Tx) error { // update AppearanceFlag - _, err := tx.Exec("UPDATE Players SET AppearanceFlag = 1 WHERE PlayerID = ? AND AccountID = ? AND AppearanceFlag = 0", character.PCStyle.IPC_UID, AccountId) + _, err := tx.Exec("UPDATE Players SET AppearanceFlag = 1 WHERE PlayerID = $1 AND AccountID = $2 AND AppearanceFlag = 0", character.PCStyle.IPC_UID, AccountId) if err != nil { return err } // update Appearance - _, err = tx.Exec("UPDATE Appearances SET Body = ?, EyeColor = ?, FaceStyle = ?, Gender = ?, HairColor = ?, HairStyle = ?, Height = ?, SkinColor = ? WHERE PlayerID = ?", + _, err = tx.Exec("UPDATE Appearances SET Body = $1, EyeColor = $2, FaceStyle = $3, Gender = $4, HairColor = $5, HairStyle = $6, Height = $7, SkinColor = $8 WHERE PlayerID = $9", character.PCStyle.IBody, character.PCStyle.IEyeColor, character.PCStyle.IFaceStyle, @@ -70,7 +70,7 @@ func (db *DBHandler) FinishPlayer(character *protocol.SP_CL2LS_REQ_CHAR_CREATE, // update Inventory items := [3]int16{character.SOn_Item.IEquipUBID, character.SOn_Item.IEquipLBID, character.SOn_Item.IEquipFootID} for i := 0; i < len(items); i++ { - _, err = tx.Exec("INSERT INTO Inventory (PlayerID, Slot, ID, Type, Opt) VALUES (?, ?, ?, ?, 1)", character.PCStyle.IPC_UID, i, items[i], i+1) + _, err = tx.Exec("INSERT INTO Inventory (PlayerID, Slot, ID, Type, Opt) VALUES ($1, $2, $3, $4, 1)", character.PCStyle.IPC_UID, i, items[i], i+1) if err != nil { return err } @@ -81,7 +81,7 @@ func (db *DBHandler) FinishPlayer(character *protocol.SP_CL2LS_REQ_CHAR_CREATE, } func (db *DBHandler) FinishTutorial(PlayerID, AccountID int) error { - _, err := db.Exec("UPDATE Players SET TutorialFlag = 1 WHERE PlayerID = ? AND AccountID = ? AND TutorialFlag = 0", PlayerID, AccountID) + _, err := db.Exec("UPDATE Players SET TutorialFlag = 1 WHERE PlayerID = $1 AND AccountID = $2 AND TutorialFlag = 0", PlayerID, AccountID) if err != nil { return err } @@ -92,16 +92,14 @@ func (db *DBHandler) FinishTutorial(PlayerID, AccountID int) error { // returns the deleted Slot number func (db *DBHandler) DeletePlayer(PlayerID, AccountID int) (int, error) { - row, err := db.Query("DELETE FROM Players WHERE AccountID = ? AND PlayerID = ? RETURNING Slot") + row, err := db.Query("DELETE FROM Players WHERE AccountID = $1 AND PlayerID = $2 RETURNING Slot") if err != nil { return -1, err } var slot int - for row.Next() { - if err := row.Scan(&slot); err != nil { - return -1, err - } + if err := row.Scan(&slot); err != nil { + return -1, err } return slot, nil @@ -165,7 +163,7 @@ func (db *DBHandler) readPlayer(rows *sql.Rows) (*core.Player, error) { } func (db *DBHandler) GetPlayer(PlayerID int) (*core.Player, error) { - rows, err := db.Query(QUERY_PLAYERS+"WHERE p.PlayerID = ?", PlayerID) + rows, err := db.Query(QUERY_PLAYERS+"WHERE p.PlayerID = $1", PlayerID) if err != nil { return nil, err } @@ -182,7 +180,7 @@ func (db *DBHandler) GetPlayer(PlayerID int) (*core.Player, error) { } func (db *DBHandler) GetPlayers(AccountID int) ([]core.Player, error) { - rows, err := db.Query(QUERY_PLAYERS+"WHERE p.AccountID = ?", AccountID) + rows, err := db.Query(QUERY_PLAYERS+"WHERE p.AccountID = $1", AccountID) if err != nil { return nil, err } diff --git a/core/db/schema.go b/core/db/schema.go index d54f198..03c7b1e 100644 --- a/core/db/schema.go +++ b/core/db/schema.go @@ -10,7 +10,8 @@ import ( _ "embed" "fmt" - _ "github.com/glebarez/go-sqlite" + "github.com/CPunch/gopenfusion/config" + _ "github.com/lib/pq" ) type DBHandler struct { @@ -20,10 +21,10 @@ type DBHandler struct { //go:embed migrations/new.sql var createDBQuery string -func OpenLiteDB(dbPath string) (*DBHandler, error) { - sqliteFmt := fmt.Sprintf("%s", dbPath) +func OpenPostgresDB(dbAddr string) (*DBHandler, error) { + fmt := fmt.Sprintf("postgresql://%s:%s@%s/%s?sslmode=disable", config.GetDBUser(), config.GetDBPass(), dbAddr, config.GetDBName()) - db, err := sql.Open("sqlite", sqliteFmt) + db, err := sql.Open("postgres", fmt) if err != nil { return nil, err } diff --git a/core/redis/redis.go b/core/redis/redis.go new file mode 100644 index 0000000..b876a22 --- /dev/null +++ b/core/redis/redis.go @@ -0,0 +1,112 @@ +package redis + +/* + used for state management between shard and login servers +*/ + +import ( + "context" + "encoding/json" + "fmt" + "strconv" + + "github.com/CPunch/gopenfusion/config" + + "github.com/redis/go-redis/v9" +) + +type RedisHandler struct { + client *redis.Client + ctx context.Context +} + +type LoginMetadata struct { + FEKey []byte `json:",omitempty"` + PlayerID int32 `json:",omitempty"` +} + +type ShardMetadata struct { + IP string + Port int +} + +const ( + SHARD_SET = "shards" +) + +func OpenRedis(addr string) (*RedisHandler, error) { + client := redis.NewClient(&redis.Options{ + Addr: addr, + CredentialsProvider: config.GetRedisCredentials(), + }) + + _, err := client.Ping(context.Background()).Result() + return &RedisHandler{client: client, ctx: context.Background()}, err +} + +func (r *RedisHandler) Close() error { + return r.client.Close() +} + +// we store login queues into redis with the name "loginMetadata_" +// set to expire after config.LOGIN_TIMEOUT duration. this way we can easily +// have a shared pool of active serial keys & player login data which any +// shard can pull from + +func makeLoginMetadataKey(serialKey int64) string { + return fmt.Sprintf("loginMetadata_%s", strconv.Itoa(int(serialKey))) +} + +func (r *RedisHandler) QueueLogin(serialKey int64, data LoginMetadata) error { + value, err := json.Marshal(data) + if err != nil { + return err + } + + // add to table + return r.client.Set(r.ctx, makeLoginMetadataKey(serialKey), value, config.LOGIN_TIMEOUT).Err() +} + +func (r *RedisHandler) GetLogin(serialKey int64) (LoginMetadata, error) { + value, err := r.client.Get(r.ctx, makeLoginMetadataKey(serialKey)).Result() + if err != nil { + return LoginMetadata{}, err + } + + var data LoginMetadata + if err := json.Unmarshal([]byte(value), &data); err != nil { + return LoginMetadata{}, err + } + + return data, nil +} + +func (r *RedisHandler) RemoveLogin(serialKey int64) error { + return r.client.Del(r.ctx, makeLoginMetadataKey(serialKey)).Err() +} + +func (r *RedisHandler) RegisterShard(shard ShardMetadata) error { + value, err := json.Marshal(shard) + if err != nil { + return err + } + + return r.client.SAdd(r.ctx, SHARD_SET, value).Err() +} + +func (r *RedisHandler) GetShards() []ShardMetadata { + shardData := r.client.SMembers(r.ctx, SHARD_SET).Val() + + // unmarshal all shards + shards := make([]ShardMetadata, 0, len(shardData)) + for _, data := range shardData { + var shard ShardMetadata + if err := json.Unmarshal([]byte(data), &shard); err != nil { + continue + } + + shards = append(shards, shard) + } + + return shards +} diff --git a/go.mod b/go.mod index 96a58f9..9bf2700 100644 --- a/go.mod +++ b/go.mod @@ -4,18 +4,15 @@ go 1.19 require ( github.com/blockloop/scan v1.3.0 - github.com/glebarez/go-sqlite v1.21.0 + github.com/georgysavva/scany/v2 v2.0.0 + github.com/google/subcommands v1.2.0 + github.com/lib/pq v1.10.9 + github.com/redis/go-redis/v9 v9.0.5 golang.org/x/crypto v0.7.0 ) require ( - github.com/dustin/go-humanize v1.0.1 // indirect - github.com/google/uuid v1.3.0 // indirect - github.com/mattn/go-isatty v0.0.17 // indirect - github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578 // indirect - golang.org/x/sys v0.6.0 // indirect - modernc.org/libc v1.22.2 // indirect - modernc.org/mathutil v1.5.0 // indirect - modernc.org/memory v1.5.0 // indirect - modernc.org/sqlite v1.20.4 // indirect + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/mattn/go-sqlite3 v1.14.15 // indirect ) diff --git a/go.sum b/go.sum index 2690e92..cf145a1 100644 --- a/go.sum +++ b/go.sum @@ -1,36 +1,39 @@ github.com/blockloop/scan v1.3.0 h1:p8xnajpGA3d/V6o23IBFdQ764+JnNJ+PQj+OwT+rkdg= github.com/blockloop/scan v1.3.0/go.mod h1:qd+3w68+o7m5Xhj9X5SlJH2rbFyK8w0WT47Rkuer010= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/bsm/ginkgo/v2 v2.7.0 h1:ItPMPH90RbmZJt5GtkcNvIRuGEdwlBItdNVoyzaNQao= +github.com/bsm/gomega v1.26.0 h1:LhQm+AFcgV2M0WyKroMASzAzCAJVpAxQXv4SaI9a69Y= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cockroachdb/cockroach-go/v2 v2.2.0 h1:/5znzg5n373N/3ESjHF5SMLxiW4RKB05Ql//KWfeTFs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= -github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= -github.com/glebarez/go-sqlite v1.21.0 h1:b8MHPtBagkSD2gntImZPsG3o3QEXgMDxguW/GLUonHQ= -github.com/glebarez/go-sqlite v1.21.0/go.mod h1:GodsA6yGSa3eKbvpr7dS+JaqazzVfMcjIXvx6KHhW/c= -github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ= -github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= -github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng= -github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/georgysavva/scany/v2 v2.0.0 h1:RGXqxDv4row7/FYoK8MRXAZXqoWF/NM+NP0q50k3DKU= +github.com/georgysavva/scany/v2 v2.0.0/go.mod h1:sigOdh+0qb/+aOs3TVhehVT10p8qJL7K/Zhyz8vWo38= +github.com/gofrs/flock v0.8.1 h1:+gYjHKf32LDeiEEFhQaotPbLuUXjY5ZqxKgXy7n59aw= +github.com/google/subcommands v1.2.0 h1:vWQspBTo2nEqTUFita5/KeEWlUL8kQObDFbub/EN9oE= +github.com/google/subcommands v1.2.0/go.mod h1:ZjhPrFU+Olkh9WazFPsl27BQ4UPiG37m3yTrtFlrHVk= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= +github.com/jackc/pgx/v5 v5.0.0 h1:3UdmB3yUeTnJtZ+nDv3Mxzd4GHHvHkl9XN3oboIbOrY= +github.com/jackc/puddle/v2 v2.0.0 h1:Kwk/AlLigcnZsDssc3Zun1dk1tAtQNPaBBxBHWn0Mjc= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/mattn/go-sqlite3 v1.10.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc= github.com/mattn/go-sqlite3 v1.14.15 h1:vfoHhTN1af61xCRSWzFIWzx2YskyMTwHLrExkBOjvxI= +github.com/mattn/go-sqlite3 v1.14.15/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= -github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578 h1:VstopitMQi3hZP0fzvnsLmzXZdQGc4bEcgu24cp+d4M= -github.com/remyoudompheng/bigfft v0.0.0-20230126093431-47fa9a501578/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/redis/go-redis/v9 v9.0.5 h1:CuQcn5HIEeK7BgElubPP8CGtE0KakrnbBSTLjathl5o= +github.com/redis/go-redis/v9 v9.0.5/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/objx v0.4.0 h1:M2gUjqZET1qApGOWNSnZ49BAIMX4F/1plDv3+l31EJ4= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk= golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= -golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -modernc.org/libc v1.22.2 h1:4U7v51GyhlWqQmwCHj28Rdq2Yzwk55ovjFrdPjs8Hb0= -modernc.org/libc v1.22.2/go.mod h1:uvQavJ1pZ0hIoC/jfqNoMLURIMhKzINIWypNM17puug= -modernc.org/mathutil v1.5.0 h1:rV0Ko/6SfM+8G+yKiyI830l3Wuz1zRutdslNoQ0kfiQ= -modernc.org/mathutil v1.5.0/go.mod h1:mZW8CKdRPY1v87qxC/wUdX5O1qDzXMP5TH3wjfpga6E= -modernc.org/memory v1.5.0 h1:N+/8c5rE6EqugZwHii4IFsaJ7MUhoWX07J5tC/iI5Ds= -modernc.org/memory v1.5.0/go.mod h1:PkUhL0Mugw21sHPeskwZW4D6VscE/GQJOnIpCnW6pSU= -modernc.org/sqlite v1.20.4 h1:J8+m2trkN+KKoE7jglyHYYYiaq5xmz2HoHJIiBlRzbE= -modernc.org/sqlite v1.20.4/go.mod h1:zKcGyrICaxNTMEHSr1HQ2GUraP0j+845GYw37+EyT6A= +golang.org/x/text v0.8.0 h1:57P1ETyNKtuIjB4SRd15iJxuhj8Gc416Y78H3qgMh68= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/login/login.go b/login/login.go index 87864aa..cbde08f 100644 --- a/login/login.go +++ b/login/login.go @@ -3,12 +3,14 @@ package login import ( "encoding/binary" "fmt" + "log" + "math/rand" "time" "github.com/CPunch/gopenfusion/config" "github.com/CPunch/gopenfusion/core/db" "github.com/CPunch/gopenfusion/core/protocol" - "github.com/CPunch/gopenfusion/shard" + "github.com/CPunch/gopenfusion/core/redis" ) const ( @@ -104,6 +106,7 @@ func (server *LoginServer) Login(peer *protocol.CNPeer, pkt protocol.Packet) err SendError(LOGIN_DATABASE_ERROR) return err } + log.Printf("Account (%d) %s has logged in", account.AccountID, account.Login) // truncate plrs if len(plrs) > 3 { @@ -218,11 +221,13 @@ func (server *LoginServer) CharacterCreate(peer *protocol.CNPeer, pkt protocol.P } if err := server.dbHndlr.FinishPlayer(&charPkt, peer.AccountID); err != nil { + log.Printf("Error finishing player: %v", err) return SendFail(peer) } plr, err := server.dbHndlr.GetPlayer(int(charPkt.PCStyle.IPC_UID)) if err != nil { + log.Printf("Error getting player: %v", err) return SendFail(peer) } @@ -252,8 +257,10 @@ func (server *LoginServer) ShardSelect(peer *protocol.CNPeer, pkt protocol.Packe var selection protocol.SP_CL2LS_REQ_CHAR_SELECT pkt.Decode(&selection) - if server.shard == nil { - return fmt.Errorf("LoginServer currently has no linked shard") + shards := server.redisHndlr.GetShards() + if len(shards) == 0 { + SendFail(peer) + return fmt.Errorf("LoginServer has found no linked shards!") } key, err := protocol.GenSerialKey() @@ -261,21 +268,36 @@ func (server *LoginServer) ShardSelect(peer *protocol.CNPeer, pkt protocol.Packe return err } - // TODO: verify peer->AccountID and selection->IPC_UID are valid!!!! + // TODO: better shard selection logic pls + // for now, pick random shard + shard := shards[rand.Intn(len(shards))] + // make sure the player is owned by the account + plr, err := server.dbHndlr.GetPlayer(int(selection.IPC_UID)) + if err != nil { + log.Printf("Error getting player: %v", err) + return SendFail(peer) + } + + if plr.AccountID != peer.AccountID { + log.Printf("HACK: player %d tried to join shard as player %d", peer.AccountID, plr.AccountID) + return SendFail(peer) + } + + // share the login attempt + server.redisHndlr.QueueLogin(key, redis.LoginMetadata{ + FEKey: peer.FE_key, + PlayerID: int32(selection.IPC_UID), + }) + + // craft response resp := protocol.SP_LS2CL_REP_SHARD_SELECT_SUCC{ - G_FE_ServerPort: int32(server.shard.GetPort()), + G_FE_ServerPort: int32(shard.Port), IEnterSerialKey: key, } // the rest of the bytes in G_FE_ServerIP will be zero'd, so there's no need to write the NULL byte - copy(resp.G_FE_ServerIP[:], []byte(config.SHARD_IP)) - - server.shard.QueueLogin(key, &shard.LoginMetadata{ - FEKey: peer.FE_key, - Timestamp: time.Now(), - PlayerID: int32(selection.IPC_UID), - }) + copy(resp.G_FE_ServerIP[:], []byte(shard.IP)) return peer.Send(protocol.P_LS2CL_REP_SHARD_SELECT_SUCC, resp) } diff --git a/login/loginServer.go b/login/loginServer.go index 0a4ca50..f2dd945 100644 --- a/login/loginServer.go +++ b/login/loginServer.go @@ -6,9 +6,10 @@ import ( "net" "sync" + "github.com/CPunch/gopenfusion/config" "github.com/CPunch/gopenfusion/core/db" "github.com/CPunch/gopenfusion/core/protocol" - "github.com/CPunch/gopenfusion/shard" + "github.com/CPunch/gopenfusion/core/redis" ) type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error @@ -19,23 +20,24 @@ type LoginServer struct { listener net.Listener port int dbHndlr *db.DBHandler + redisHndlr *redis.RedisHandler packetHandlers map[uint32]PacketHandler peers map[*protocol.CNPeer]bool peersLock sync.Mutex - shard *shard.ShardServer } -func NewLoginServer(dbHndlr *db.DBHandler, port int) (*LoginServer, error) { +func NewLoginServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*LoginServer, error) { listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) if err != nil { return nil, err } server := &LoginServer{ - listener: listener, - port: port, - dbHndlr: dbHndlr, - peers: make(map[*protocol.CNPeer]bool), + listener: listener, + port: port, + dbHndlr: dbHndlr, + redisHndlr: redisHndlr, + peers: make(map[*protocol.CNPeer]bool), } server.packetHandlers = map[uint32]PacketHandler{ @@ -59,7 +61,7 @@ func NewLoginServer(dbHndlr *db.DBHandler, port int) (*LoginServer, error) { } func (server *LoginServer) Start() { - log.Printf("Server hosted on 127.0.0.1:%d\n", server.port) + log.Printf("Login service hosted on %s:%d\n", config.GetAnnounceIP(), server.port) for { conn, err := server.listener.Accept() @@ -99,7 +101,3 @@ func (server *LoginServer) Connect(peer *protocol.CNPeer) { server.peers[peer] = true server.peersLock.Unlock() } - -func (server *LoginServer) AddShard(shard *shard.ShardServer) { - server.shard = shard -} diff --git a/main.go b/main.go deleted file mode 100644 index 49b5cd6..0000000 --- a/main.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "log" - - "github.com/CPunch/gopenfusion/config" - "github.com/CPunch/gopenfusion/core/db" - "github.com/CPunch/gopenfusion/login" - "github.com/CPunch/gopenfusion/shard" -) - -func main() { - dbHndlr, err := db.OpenLiteDB("test.db") - if err != nil { - log.Panicf("failed to open db: %v", err) - } - dbHndlr.Setup() - - loginServer, err := login.NewLoginServer(dbHndlr, config.LOGIN_PORT) - if err != nil { - log.Panicf("failed to create login server: %v", err) - } - - shardServer, err := shard.NewShardServer(dbHndlr, config.SHARD_PORT) - if err != nil { - log.Panicf("failed to create shard server: %v", err) - } - - loginServer.AddShard(shardServer) - go loginServer.Start() - - shardServer.Start() -} diff --git a/shard/join.go b/shard/join.go index 33a6bcb..4a2cd0b 100644 --- a/shard/join.go +++ b/shard/join.go @@ -2,6 +2,7 @@ package shard import ( "fmt" + "log" "time" "github.com/CPunch/gopenfusion/core" @@ -12,7 +13,7 @@ func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Pack var enter protocol.SP_CL2FE_REQ_PC_ENTER pkt.Decode(&enter) - loginData, err := server.CheckLogin(enter.IEnterSerialKey) + loginData, err := server.redisHndlr.GetLogin(enter.IEnterSerialKey) if err != nil { // the error codes for P_FE2CL_REP_PC_ENTER_FAIL aren't referenced in the client :( peer.Send(protocol.P_FE2CL_REP_PC_ENTER_FAIL, protocol.SP_FE2CL_REP_PC_ENTER_FAIL{}) @@ -48,6 +49,7 @@ func (server *ShardServer) RequestEnter(peer *protocol.CNPeer, pkt protocol.Pack peer.FE_key = loginData.FEKey peer.SetActiveKey(protocol.USE_FE) + log.Printf("Player %d (AccountID %d) entered\n", resp.IID, loginData.PlayerID) return peer.Send(protocol.P_FE2CL_REP_PC_ENTER_SUCC, resp) } diff --git a/shard/movement.go b/shard/movement.go new file mode 100644 index 0000000..ea03ee4 --- /dev/null +++ b/shard/movement.go @@ -0,0 +1 @@ +package shard diff --git a/shard/shardServer.go b/shard/shardServer.go index 21f4cfa..92c8d8c 100644 --- a/shard/shardServer.go +++ b/shard/shardServer.go @@ -5,19 +5,14 @@ import ( "log" "net" "sync" - "time" + "github.com/CPunch/gopenfusion/config" "github.com/CPunch/gopenfusion/core" "github.com/CPunch/gopenfusion/core/db" "github.com/CPunch/gopenfusion/core/protocol" + "github.com/CPunch/gopenfusion/core/redis" ) -type LoginMetadata struct { - FEKey []byte - Timestamp time.Time - PlayerID int32 -} - type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error func stubbedPacket(_ *protocol.CNPeer, _ protocol.Packet) error { /* stubbed */ return nil } @@ -26,13 +21,14 @@ type ShardServer struct { listener net.Listener port int dbHndlr *db.DBHandler + redisHndlr *redis.RedisHandler packetHandlers map[uint32]PacketHandler loginMetadataQueue sync.Map // [int64]*LoginMetadata w/ int64 = serialKey peersLock sync.Mutex peers sync.Map // [*protocol.CNPeer]core.Player } -func NewShardServer(dbHndlr *db.DBHandler, port int) (*ShardServer, error) { +func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) { listener, err := net.Listen("tcp", fmt.Sprintf(":%d", port)) if err != nil { return nil, err @@ -42,6 +38,7 @@ func NewShardServer(dbHndlr *db.DBHandler, port int) (*ShardServer, error) { listener: listener, port: port, dbHndlr: dbHndlr, + redisHndlr: redisHndlr, packetHandlers: make(map[uint32]PacketHandler), } @@ -50,6 +47,11 @@ func NewShardServer(dbHndlr *db.DBHandler, port int) (*ShardServer, error) { protocol.P_CL2FE_REQ_PC_LOADING_COMPLETE: server.LoadingComplete, } + redisHndlr.RegisterShard(redis.ShardMetadata{ + IP: config.GetAnnounceIP(), + Port: port, + }) + return server, nil } @@ -58,7 +60,7 @@ func (server *ShardServer) RegisterPacketHandler(typeID uint32, hndlr PacketHand } func (server *ShardServer) Start() { - log.Printf("Server hosted on 127.0.0.1:%d\n", server.port) + log.Printf("Shard service hosted on %s:%d\n", config.GetAnnounceIP(), server.port) for { conn, err := server.listener.Accept() @@ -151,21 +153,3 @@ func (server *ShardServer) RangePeers(f func(peer *protocol.CNPeer, player *core return f(peer, player) }) } - -func (server *ShardServer) QueueLogin(serialKey int64, meta *LoginMetadata) { - server.loginMetadataQueue.Store(serialKey, meta) -} - -func (server *ShardServer) CheckLogin(serialKey int64) (*LoginMetadata, error) { - value, ok := server.loginMetadataQueue.Load(serialKey) - if !ok { - return nil, fmt.Errorf("serialKey %x is not valid!", serialKey) - } - - meta, ok := value.(*LoginMetadata) - if !ok { // should never happen - panic(fmt.Errorf("ShardServer.loginMetadataQueue has an invalid value: loginMetadataQueue[%x] = %#v", serialKey, value)) - } - - return meta, nil -}