diff --git a/.gitignore b/.gitignore index 04df6fb..94f146e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,17 +12,13 @@ *.out # custom -build .env *.key go.work go.work.sum -# config file -secure.config - # built server -mario-kart-7-secure +build # logs log diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8e68d74 --- /dev/null +++ b/Makefile @@ -0,0 +1,60 @@ +# TODO - Assumes a UNIX-like OS + +RED := $(shell tput setaf 1) +BLUE := $(shell tput setaf 4) +CYAN := $(shell tput setaf 14) +ORANGE := $(shell tput setaf 202) +YELLOW := $(shell tput setaf 214) +RESET := $(shell tput sgr0) + +ifeq ($(shell which go),) +# TODO - Read contents from .git folder instead? +$(error "$(RED)go command not found. Install go to continue $(BLUE)https://go.dev/doc/install$(RESET)") +endif + +ifneq ($(wildcard .git),) +# * .git folder exists, build server build string from repo info +ifeq ($(shell which git),) +# TODO - Read contents from .git folder instead? +$(error "$(RED)git command not found. Install git to continue $(ORANGE)https://git-scm.com/downloads$(RESET)") +endif +$(info "$(CYAN)Building server build string from repository info$(RESET)") +# * Build server build string from repo info +BRANCH := $(shell git rev-parse --abbrev-ref HEAD) +REMOTE_ORIGIN := $(shell git config --get remote.origin.url) + +# * Handle multiple origin URL formats +HTTPS_PREFIX_CHECK := $(shell echo $(REMOTE_ORIGIN) | head -c 8) +HTTP_PREFIX_CHECK := $(shell echo $(REMOTE_ORIGIN) | head -c 7) +GIT@_PREFIX_CHECK := $(shell echo $(REMOTE_ORIGIN) | head -c 4) + +ifeq ($(HTTPS_PREFIX_CHECK), https://) +REMOTE_PATH := $(shell echo $(REMOTE_ORIGIN) | cut -d/ -f4-) +else ifeq ($(HTTP_PREFIX_CHECK), http://) +REMOTE_PATH := $(shell echo $(REMOTE_ORIGIN) | cut -d/ -f4-) +else ifeq ($(GIT@_PREFIX_CHECK), git@) +REMOTE_PATH := $(shell echo $(REMOTE_ORIGIN) | cut -d: -f2-) +else +REMOTE_PATH := $(shell echo $(REMOTE_ORIGIN) | cut -d/ -f2-) +endif + +HASH := $(shell git rev-parse --short HEAD) +SERVER_BUILD := $(BRANCH):$(REMOTE_PATH)@$(HASH) + +else +# * .git folder not present, assume downloaded from zip file and just use folder name +$(info "$(CYAN)git repository not found. Building server build string from folder name$(RESET)") +SERVER_BUILD := mario-kart-7 +endif + +# * Final build string +DATE_TIME := $(shell date --iso=seconds) +BUILD_STRING := $(SERVER_BUILD), $(DATE_TIME) + +default: +ifeq ($(wildcard .env),) + $(warning "$(YELLOW).env file not found, environment variables may not be populated correctly$(RESET)") +endif + go get -u + go mod tidy + go build -ldflags "-X 'main.serverBuildString=$(BUILD_STRING)'" -o ./build/mario-kart-7 diff --git a/README.md b/README.md index d05ecb5..40ca591 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,56 @@ -# Mario Kart 7 - Secure -### Pretendo Mario Kart 7 secure server +# Mario Kart 7 (3DS) replacement server +Includes both the authentication and secure servers -## About -This server handles all of MK7's online functionality (incomplete). +## Compiling + +### Setup +Install [Go](https://go.dev/doc/install) and [git](https://git-scm.com/downloads), then clone and enter the repository + +```bash +$ git clone https://github.com/PretendoNetwork/mario-kart-7 +$ cd mario-kart-7 +``` + +### Compiling using `go` +To compile using Go, `go get` the required modules and then `go build` to your desired location. You may also want to tidy the go modules, though this is optional + +```bash +$ go get -u +$ go mod tidy +$ go build -o build/mario-kart-7 +``` + +The server is now built to `build/mario-kart-7` + +When compiling with only Go, the authentication servers build string is not automatically set. This should not cause any issues with gameplay, but it means that the server build will not be visible in any packet dumps or logs a title may produce + +To compile the servers with the authentication server build string, add `-ldflags "-X 'main.serverBuildString=BUILD_STRING_HERE'"` to the build command, or use `make` to compile the server + +### Compiling using `make` +Compiling using `make` will read the local `.git` directory to create a dynamic authentication server build string, based on your repositories remote origin and current commit + +Install `make` either through your systems package manager or the [official download](https://www.gnu.org/software/make/). We provide a `default` rule which compiles [using `go`](#compiling-using-go) + +To build using `go` + +```bash +$ make +``` + +The server is now built to `build/mario-kart-7` + +## Configuration +All configuration options are handled via environment variables + +`.env` files are supported + +| Name | Description | Required | +|-------------------------------------|------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------| +| `PN_MK7_POSTGRES_URI` | Fully qualified URI to your Postgres server (Example `postgres://username:password@localhost/mk7?sslmode=disable`) | Yes | +| `PN_MK7_KERBEROS_PASSWORD` | Password used as part of the internal server data in Kerberos tickets | No (Default password `password` will be used) | +| `PN_MK7_AUTHENTICATION_SERVER_PORT` | Port for the authentication server | Yes | +| `PN_MK7_SECURE_SERVER_HOST` | Host name for the secure server (should point to the same address as the authentication server) | Yes | +| `PN_MK7_SECURE_SERVER_PORT` | Port for the secure server | Yes | +| `PN_MK7_ACCOUNT_GRPC_HOST` | Host name for your account server gRPC service | Yes | +| `PN_MK7_ACCOUNT_GRPC_PORT` | Port for your account server gRPC service | Yes | +| `PN_MK7_ACCOUNT_GRPC_API_KEY` | API key for your account server gRPC service | No (Assumed to be an open gRPC API) | diff --git a/database/add_new_user.go b/database/add_new_user.go deleted file mode 100644 index 624b9dc..0000000 --- a/database/add_new_user.go +++ /dev/null @@ -1,14 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" -) - -func AddNewUser(pid uint32) { - _, err := usersCollection.InsertOne(context.TODO(), bson.D{{"pid", pid}, {"rating", bson.A{1000, 1000, 1000}}, {"username", GetUsernameFromPID(pid)}, {"status", "allowed"}}) - if err != nil { - panic(err) - } -} diff --git a/database/add_player_session.go b/database/add_player_session.go deleted file mode 100644 index 0d04dba..0000000 --- a/database/add_player_session.go +++ /dev/null @@ -1,14 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" -) - -func AddPlayerSession(rvcid uint32, urls []string, ip string, port string) { - _, err := sessionsCollection.InsertOne(context.TODO(), bson.D{{"rvcid", rvcid}, {"urls", urls}, {"ip", ip}, {"port", port}}) - if err != nil { - panic(err) - } -} diff --git a/database/add_player_to_room.go b/database/add_player_to_room.go deleted file mode 100644 index ba3c218..0000000 --- a/database/add_player_to_room.go +++ /dev/null @@ -1,49 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func AddPlayerToRoom(gid uint32, pid uint32, addplayercount uint32) { - var result bson.M - - err := roomsCollection.FindOne(context.TODO(), bson.D{{"gid", gid}}, options.FindOne()).Decode(&result) - if err != nil { - //panic(err) - return - } - - oldPlayerList := result["players"].(bson.A) - newPlayerList := make([]int64, 12) - for i := 0; i < 12; i++ { - if oldPlayerList[i].(int64) == int64(pid) || oldPlayerList[i].(int64) == -1*int64(pid) { - newPlayerList[i] = 0 - } else { - newPlayerList[i] = oldPlayerList[i].(int64) - } - } - unassignedPlayers := addplayercount - needToAddGuest := (unassignedPlayers > 1) - for i := 0; i < 12; i++ { - if newPlayerList[i] == 0 && unassignedPlayers > 0 { - if unassignedPlayers == 1 && needToAddGuest { - newPlayerList[i] = -1 * int64(pid) - needToAddGuest = false - } else { - newPlayerList[i] = int64(pid) - } - unassignedPlayers-- - } - } - - newplayercount := result["player_count"].(int64) + int64(addplayercount) - - _, err = roomsCollection.UpdateOne(context.TODO(), bson.D{{"gid", gid}}, bson.D{{"$set", bson.D{{"players", newPlayerList}, {"player_count", newplayercount}}}}) - if err != nil { - //panic(err) - return - } -} diff --git a/database/community_exists.go b/database/community_exists.go deleted file mode 100644 index 9e066dd..0000000 --- a/database/community_exists.go +++ /dev/null @@ -1,26 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func CommunityExists(gid uint32) bool { - var result bson.M - - err := communitiesCollection.FindOne(context.TODO(), bson.D{{"gid", gid}}, options.FindOne()).Decode(&result) - if err != nil { - if err == mongo.ErrNoDocuments { - return false - } else { - panic(err) - } - } else { - return true - } - - return false -} diff --git a/database/connect_all.go b/database/connect_all.go deleted file mode 100644 index 0791bde..0000000 --- a/database/connect_all.go +++ /dev/null @@ -1,5 +0,0 @@ -package database - -func ConnectAll() { - connectMongo() -} diff --git a/database/connect_mongo.go b/database/connect_mongo.go deleted file mode 100644 index cf39e87..0000000 --- a/database/connect_mongo.go +++ /dev/null @@ -1,53 +0,0 @@ -package database - -import ( - "context" - "time" - - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -var nexMongoClient *mongo.Client -var accountMongoClient *mongo.Client -var nexMongoContext context.Context -var accountMongoContext context.Context -var accountDatabase *mongo.Database -var splatoonDatabase *mongo.Database -var pnidCollection *mongo.Collection -var nexAccountsCollection *mongo.Collection -var regionsCollection *mongo.Collection -var usersCollection *mongo.Collection -var sessionsCollection *mongo.Collection -var roomsCollection *mongo.Collection -var communitiesCollection *mongo.Collection - -func connectMongo() { - if globals.Config.DatabaseUseAuth { - nexMongoClient, _ = mongo.NewClient(options.Client().ApplyURI("mongodb://" + globals.Config.DatabaseUsername + ":" + globals.Config.DatabasePassword + "@" + globals.Config.NEXDatabaseIP + ":" + globals.Config.NEXDatabasePort + "/")) - accountMongoClient, _ = mongo.NewClient(options.Client().ApplyURI("mongodb://" + globals.Config.DatabaseUsername + ":" + globals.Config.DatabasePassword + "@" + globals.Config.AccountDatabaseIP + ":" + globals.Config.AccountDatabasePort + "/")) - } else { - nexMongoClient, _ = mongo.NewClient(options.Client().ApplyURI("mongodb://" + globals.Config.NEXDatabaseIP + ":" + globals.Config.NEXDatabasePort + "/")) - accountMongoClient, _ = mongo.NewClient(options.Client().ApplyURI("mongodb://" + globals.Config.AccountDatabaseIP + ":" + globals.Config.AccountDatabasePort + "/")) - } - nexMongoContext, _ = context.WithTimeout(context.Background(), 10*time.Second) - _ = nexMongoClient.Connect(nexMongoContext) - accountMongoContext, _ = context.WithTimeout(context.Background(), 10*time.Second) - _ = accountMongoClient.Connect(accountMongoContext) - - accountDatabase = accountMongoClient.Database(globals.Config.AccountDatabase) - pnidCollection = accountDatabase.Collection(globals.Config.PNIDCollection) - nexAccountsCollection = accountDatabase.Collection(globals.Config.NexAccountsCollection) - - splatoonDatabase = nexMongoClient.Database(globals.Config.SplatoonDatabase) - regionsCollection = splatoonDatabase.Collection(globals.Config.RegionsCollection) - usersCollection = splatoonDatabase.Collection(globals.Config.UsersCollection) - sessionsCollection = splatoonDatabase.Collection(globals.Config.SessionsCollection) - roomsCollection = splatoonDatabase.Collection(globals.Config.RoomsCollection) - communitiesCollection = splatoonDatabase.Collection(globals.Config.CommunitiesCollection) - - sessionsCollection.DeleteMany(context.TODO(), bson.D{}) - roomsCollection.DeleteMany(context.TODO(), bson.D{}) -} diff --git a/database/connect_postgres.go b/database/connect_postgres.go new file mode 100644 index 0000000..40b6add --- /dev/null +++ b/database/connect_postgres.go @@ -0,0 +1,25 @@ +package database + +import ( + "database/sql" + "os" + + _ "github.com/lib/pq" + + "github.com/PretendoNetwork/mario-kart-7/globals" +) + +var Postgres *sql.DB + +func ConnectPostgres() { + var err error + + Postgres, err = sql.Open("postgres", os.Getenv("PN_MK7_POSTGRES_URI")) + if err != nil { + globals.Logger.Critical(err.Error()) + } + + globals.Logger.Success("Connected to Postgres!") + + initPostgres() +} diff --git a/database/delete_player_session.go b/database/delete_player_session.go deleted file mode 100644 index 096608e..0000000 --- a/database/delete_player_session.go +++ /dev/null @@ -1,14 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" -) - -func DeletePlayerSession(rvcid uint32) { - _, err := sessionsCollection.DeleteOne(context.TODO(), bson.D{{"rvcid", rvcid}}) - if err != nil { - panic(err) - } -} diff --git a/database/destroy_room.go b/database/destroy_room.go deleted file mode 100644 index f327cbf..0000000 --- a/database/destroy_room.go +++ /dev/null @@ -1,15 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" -) - -func DestroyRoom(gid uint32) { - _, err := roomsCollection.DeleteOne(context.TODO(), bson.D{{"gid", gid}}) - if err != nil { - //panic(err) - return - } -} diff --git a/database/does_session_exist.go b/database/does_session_exist.go deleted file mode 100644 index 5b11430..0000000 --- a/database/does_session_exist.go +++ /dev/null @@ -1,24 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func DoesSessionExist(rvcid uint32) bool { - var result bson.M - - err := sessionsCollection.FindOne(context.TODO(), bson.D{{"rvcid", rvcid}}, options.FindOne()).Decode(&result) - if err != nil { - if err == mongo.ErrNoDocuments { - return false - } else { - panic(err) - } - } else { - return true - } -} diff --git a/database/does_user_exist.go b/database/does_user_exist.go deleted file mode 100644 index b0703cb..0000000 --- a/database/does_user_exist.go +++ /dev/null @@ -1,24 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func DoesUserExist(pid uint32) bool { - var result bson.M - - err := usersCollection.FindOne(context.TODO(), bson.D{{"pid", pid}}, options.FindOne()).Decode(&result) - if err != nil { - if err == mongo.ErrNoDocuments { - return false - } else { - panic(err) - } - } else { - return true - } -} diff --git a/database/find_communities_with_participant.go b/database/find_communities_with_participant.go deleted file mode 100644 index 6843188..0000000 --- a/database/find_communities_with_participant.go +++ /dev/null @@ -1,34 +0,0 @@ -package database - -import ( - "context" - "fmt" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func FindCommunitiesWithParticipant(pid uint32) []uint32 { - arr := []uint32{pid} - gatheringIDs := []uint32{} - - cur, err := communitiesCollection.Find(context.TODO(), bson.M{"participants": bson.M{"$in": arr}}, options.Find()) - if err != nil { - return nil - } - - for cur.Next(context.TODO()) { - fmt.Println("test") - //Create a value into which the single document can be decoded - var result bson.M - err := cur.Decode(&result) - if err != nil { - return nil - } - - gatheringIDs = append(gatheringIDs, (uint32)(result["gid"].(int64))) - - } - - return gatheringIDs -} diff --git a/database/find_room.go b/database/find_room.go deleted file mode 100644 index 176485d..0000000 --- a/database/find_room.go +++ /dev/null @@ -1,34 +0,0 @@ -package database - -import ( - "context" - "math" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func FindRoom(gamemode uint32, public bool, region uint32, gameconfig uint32, vacantcount uint32, update uint32, communitygid uint32) uint32 { - var result bson.M - maxplayersinroom := 12 - vacantcount - filter := bson.D{ - {"gamemode", gamemode}, - {"public", public}, - {"region", region}, - {"gameconfig", gameconfig}, - {"player_count", bson.D{{"$lte", maxplayersinroom}}}, - {"update", update}, - {"community_gid", communitygid}} - - err := roomsCollection.FindOne(context.TODO(), filter, options.FindOne()).Decode(&result) - if err != nil { - if err == mongo.ErrNoDocuments { - return math.MaxUint32 - } else { - panic(err) - } - } else { - return uint32(result["gid"].(int64)) - } -} diff --git a/database/get_community_info.go b/database/get_community_info.go deleted file mode 100644 index c9c0994..0000000 --- a/database/get_community_info.go +++ /dev/null @@ -1,28 +0,0 @@ -package database - -import ( - "context" - "encoding/base64" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func GetCommunityInfo(gid uint32) (uint32, uint32, string, []uint32, []byte, uint64, uint64, uint32, uint32) { - var result bson.M - - err := communitiesCollection.FindOne(context.TODO(), bson.D{{"gid", gid}}, options.FindOne()).Decode(&result) - if err != nil { - panic(err) - } - attribs := make([]uint32, len(result["attribs"].(bson.A))) - for index, attrib := range result["attribs"].(bson.A) { - if val, ok := attrib.(uint32); ok { - attribs[index] = val - } - } - - application_buffer, _ := base64.StdEncoding.DecodeString(result["application_buffer"].(string)) - - return uint32(result["host"].(int64)), uint32(result["type"].(int64)), result["password"].(string), attribs, application_buffer, uint64(result["start_date"].(int64)), uint64(result["end_date"].(int64)), uint32(result["sessions"].(int32)), uint32(len(result["participants"].(bson.A))) -} diff --git a/database/get_player_session_address.go b/database/get_player_session_address.go deleted file mode 100644 index d21ca25..0000000 --- a/database/get_player_session_address.go +++ /dev/null @@ -1,20 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func GetPlayerSessionAddress(rvcid uint32) string { - var result bson.M - - err := sessionsCollection.FindOne(context.TODO(), bson.D{{"rvcid", rvcid}}, options.FindOne()).Decode(&result) - if err != nil { - panic(rvcid) - //panic(err) - } - - return result["ip"].(string) + ":" + result["port"].(string) -} diff --git a/database/get_player_urls.go b/database/get_player_urls.go deleted file mode 100644 index a5ec8cf..0000000 --- a/database/get_player_urls.go +++ /dev/null @@ -1,27 +0,0 @@ -package database - -import ( - "context" - "fmt" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func GetPlayerURLs(rvcid uint32) []string { - var result bson.M - - err := sessionsCollection.FindOne(context.TODO(), bson.D{{"rvcid", rvcid}}, options.FindOne()).Decode(&result) - if err != nil { - panic(err) - } - - oldurlArray := result["urls"].(bson.A) - newurlArray := make([]string, len(oldurlArray)) - for i := 0; i < len(oldurlArray); i++ { - fmt.Println(oldurlArray[i].(string)) - newurlArray[i] = oldurlArray[i].(string) - } - - return newurlArray -} diff --git a/database/get_room_info.go b/database/get_room_info.go deleted file mode 100644 index c827ef4..0000000 --- a/database/get_room_info.go +++ /dev/null @@ -1,19 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func GetRoomInfo(gid uint32) (uint32, uint32, uint32, uint32, uint32) { - var result bson.M - - err := roomsCollection.FindOne(context.TODO(), bson.D{{"gid", gid}}, options.FindOne()).Decode(&result) - if err != nil { - panic(err) - } - - return uint32(result["host"].(int64)), uint32(result["gamemode"].(int64)), uint32(result["region"].(int64)), uint32(result["gameconfig"].(int64)), uint32(result["update"].(int64)) -} diff --git a/database/get_room_players.go b/database/get_room_players.go deleted file mode 100644 index ec5f682..0000000 --- a/database/get_room_players.go +++ /dev/null @@ -1,28 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func GetRoomPlayers(gid uint32) []uint32 { - var result bson.M - - err := roomsCollection.FindOne(context.TODO(), bson.D{{"gid", gid}}, options.FindOne()).Decode(&result) - if err != nil { - panic(err) - } - - dbPlayerList := result["players"].(bson.A) - pidList := make([]uint32, 0) - - for i := 0; i < 12; i++ { - if (uint32)(dbPlayerList[i].(int64)) != 0 { - pidList = append(pidList, (uint32)(dbPlayerList[i].(int64))) - } - } - - return pidList -} diff --git a/database/get_unique_id_by_owner_pid.go b/database/get_unique_id_by_owner_pid.go new file mode 100644 index 0000000..8250723 --- /dev/null +++ b/database/get_unique_id_by_owner_pid.go @@ -0,0 +1,11 @@ +package database + +func GetUniqueIDByOwnerPID(pid uint32) (uint32, error) { + var uniqueID uint32 + err := Postgres.QueryRow(`SELECT unique_id FROM common_data WHERE owner_pid=$1`, pid).Scan(&uniqueID) + if err != nil { + return 0, err + } + + return uniqueID, nil +} diff --git a/database/get_username_from_pid.go b/database/get_username_from_pid.go deleted file mode 100644 index 2b56fdc..0000000 --- a/database/get_username_from_pid.go +++ /dev/null @@ -1,25 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func GetUsernameFromPID(pid uint32) string { - var result bson.M - - err := pnidCollection.FindOne(context.TODO(), bson.D{{Key: "pid", Value: pid}}, options.FindOne()).Decode(&result) - - if err != nil { - if err == mongo.ErrNoDocuments { - return "" - } - - panic(err) - } - - return result["username"].(string) -} diff --git a/database/init_postgres.go b/database/init_postgres.go new file mode 100644 index 0000000..86b1ca8 --- /dev/null +++ b/database/init_postgres.go @@ -0,0 +1,19 @@ +package database + +import "github.com/PretendoNetwork/mario-kart-7/globals" + +func initPostgres() { + var err error + + _, err = Postgres.Exec(`CREATE TABLE IF NOT EXISTS common_data ( + unique_id serial PRIMARY KEY, + owner_pid integer, + common_data bytea + )`) + if err != nil { + globals.Logger.Critical(err.Error()) + return + } + + globals.Logger.Success("Postgres tables created") +} diff --git a/database/insert_common_data_by_owner_pid.go b/database/insert_common_data_by_owner_pid.go new file mode 100644 index 0000000..38da88a --- /dev/null +++ b/database/insert_common_data_by_owner_pid.go @@ -0,0 +1,11 @@ +package database + +func InsertCommonDataByOwnerPID(pid uint32) (uint32, error) { + var uniqueID uint32 + err := Postgres.QueryRow(`INSERT INTO common_data (owner_pid) VALUES ($1) RETURNING unique_id`, pid).Scan(&uniqueID) + if err != nil { + return 0, err + } + + return uniqueID, nil +} diff --git a/database/is_allowed_game_mode_and_region.go b/database/is_allowed_game_mode_and_region.go deleted file mode 100644 index 068bca1..0000000 --- a/database/is_allowed_game_mode_and_region.go +++ /dev/null @@ -1,30 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func IsAllowedGameModeAndRegion(gamemode uint32, region uint32) bool { - var result bson.M - - err := regionsCollection.FindOne(context.TODO(), bson.D{{Key: "id", Value: region}}, options.FindOne()).Decode(&result) - - if err != nil { - if err == mongo.ErrNoDocuments { - return false - } - } - - if len(result["allowed_gamemodes"].(bson.A)) <= int(gamemode) { - return false - } - if result["allowed_gamemodes"].(bson.A)[gamemode].(bool) { - return true - } else { - return false - } -} diff --git a/database/new_community.go b/database/new_community.go deleted file mode 100644 index 2980a31..0000000 --- a/database/new_community.go +++ /dev/null @@ -1,49 +0,0 @@ -package database - -import ( - "context" - "encoding/base64" - "math/rand" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func NewCommunity(host uint32, communityType uint32, password string, attribs []uint32, applicationBuffer []byte, participationStartDate uint64, participationEndDate uint64) uint32 { - var gatheringId uint32 - var result bson.M - - for true { - gatheringId = rand.Uint32() % 500000 - err := communitiesCollection.FindOne(context.TODO(), bson.D{{"gid", gatheringId}}, options.FindOne()).Decode(&result) - if err != nil { - if err == mongo.ErrNoDocuments { - break - } else { - panic(err) - } - } else { - continue - } - } - participants := make([]int64, 1) - participants[0] = int64(host) - gatheringDoc := bson.D{ - {"gid", gatheringId}, - {"host", host}, - {"type", communityType}, - {"password", password}, - {"attribs", attribs}, - {"application_buffer", base64.StdEncoding.EncodeToString(applicationBuffer)}, - {"start_date", participationStartDate}, - {"end_date", participationEndDate}, - {"sessions", 0}, - {"participants", participants}} - _, err := communitiesCollection.InsertOne(context.TODO(), gatheringDoc) - if err != nil { - panic(err) - } - - return gatheringId -} diff --git a/database/new_room.go b/database/new_room.go deleted file mode 100644 index c4d0200..0000000 --- a/database/new_room.go +++ /dev/null @@ -1,47 +0,0 @@ -package database - -import ( - "context" - "math/rand" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func NewRoom(host uint32, gamemode uint32, public bool, region uint32, gameconfig uint32, playercount uint32, update uint32, communitygid uint32) uint32 { - var gatheringId uint32 - var result bson.M - - for true { - gatheringId = rand.Uint32() % 500000 - err := roomsCollection.FindOne(context.TODO(), bson.D{{"gid", gatheringId}}, options.FindOne()).Decode(&result) - if err != nil { - if err == mongo.ErrNoDocuments { - break - } else { - panic(err) - } - } else { - continue - } - } - players := make([]int64, 12) - gatheringDoc := bson.D{ - {"gid", gatheringId}, - {"host", host}, - {"gamemode", gamemode}, - {"players", players}, - {"public", public}, - {"region", region}, - {"gameconfig", gameconfig}, - {"player_count", int64(0)}, - {"update", update}, - {"community_gid", communitygid}} - _, err := roomsCollection.InsertOne(context.TODO(), gatheringDoc) - if err != nil { - panic(err) - } - - return gatheringId -} diff --git a/database/remove_player.go b/database/remove_player.go deleted file mode 100644 index 6d2afd1..0000000 --- a/database/remove_player.go +++ /dev/null @@ -1,39 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func RemovePlayer(pid uint32) { - var result bson.M - arr := []uint32{pid} - - err := roomsCollection.FindOne(context.TODO(), bson.M{"players": bson.M{"$in": arr}}, options.FindOne()).Decode(&result) - if err != nil { - return - } - - oldPlayerList := result["players"].(bson.A) - newPlayerList := make([]int64, 12) - newplayercount := result["player_count"].(int64) - for i := 0; i < 12; i++ { - newPlayerList[i] = oldPlayerList[i].(int64) - if newPlayerList[i] == int64(pid) || newPlayerList[i] == -1*int64(pid) { - newPlayerList[i] = 0 - newplayercount-- - } - } - - _, err = roomsCollection.UpdateOne(context.TODO(), bson.D{{"gid", result["gid"]}}, bson.D{{"$set", bson.D{{"players", newPlayerList}, {"player_count", newplayercount}}}}) - if err != nil { - return - //panic(err) - } - - if newplayercount <= 0 { - DestroyRoom((uint32)(result["gid"].(int64))) - } -} diff --git a/database/remove_player_from_room.go b/database/remove_player_from_room.go deleted file mode 100644 index a9ae8f7..0000000 --- a/database/remove_player_from_room.go +++ /dev/null @@ -1,39 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func RemovePlayerFromRoom(gid uint32, pid uint32) { - var result bson.M - - err := roomsCollection.FindOne(context.TODO(), bson.D{{"gid", gid}}, options.FindOne()).Decode(&result) - if err != nil { - //panic(err) - return - } - - oldPlayerList := result["players"].(bson.A) - newPlayerList := make([]int64, 12) - newplayercount := result["player_count"].(int64) - for i := 0; i < 12; i++ { - newPlayerList[i] = oldPlayerList[i].(int64) - if newPlayerList[i] == int64(pid) || newPlayerList[i] == -1*int64(pid) { - newPlayerList[i] = 0 - newplayercount-- - } - } - - _, err = roomsCollection.UpdateOne(context.TODO(), bson.D{{"gid", gid}}, bson.D{{"$set", bson.D{{"players", newPlayerList}, {"player_count", newplayercount}}}}) - if err != nil { - //panic(err) - return - } - - if newplayercount <= 0 { - DestroyRoom(gid) - } -} diff --git a/database/update_player_session_all.go b/database/update_player_session_all.go deleted file mode 100644 index b6c917b..0000000 --- a/database/update_player_session_all.go +++ /dev/null @@ -1,14 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" -) - -func UpdatePlayerSessionAll(rvcid uint32, urls []string, ip string, port string) { - _, err := sessionsCollection.UpdateOne(context.TODO(), bson.D{{"rvcid", rvcid}}, bson.D{{"$set", bson.D{{"rvcid", rvcid}, {"urls", urls}, {"ip", ip}, {"port", port}}}}) - if err != nil { - panic(err) - } -} diff --git a/database/update_player_session_url.go b/database/update_player_session_url.go deleted file mode 100644 index 7f999a1..0000000 --- a/database/update_player_session_url.go +++ /dev/null @@ -1,32 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo/options" -) - -func UpdatePlayerSessionURL(rvcid uint32, oldurl string, newurl string) { - var result bson.M - - err := sessionsCollection.FindOne(context.TODO(), bson.D{{"rvcid", rvcid}}, options.FindOne()).Decode(&result) - if err != nil { - panic(err) - } - - oldurlArray := result["urls"].(bson.A) - newurlArray := make([]string, len(oldurlArray)) - for i := 0; i < len(oldurlArray); i++ { - if oldurlArray[i].(string) == oldurl { - newurlArray[i] = newurl - } else { - newurlArray[i] = oldurlArray[i].(string) - } - } - - _, err = sessionsCollection.UpdateOne(context.TODO(), bson.D{{"rvcid", rvcid}}, bson.D{{"$set", bson.D{{"urls", newurlArray}}}}) - if err != nil { - panic(err) - } -} diff --git a/database/update_room_host.go b/database/update_room_host.go deleted file mode 100644 index 7691191..0000000 --- a/database/update_room_host.go +++ /dev/null @@ -1,15 +0,0 @@ -package database - -import ( - "context" - - "go.mongodb.org/mongo-driver/bson" -) - -func UpdateRoomHost(gid uint32, newownerpid uint32) { - _, err := roomsCollection.UpdateOne(context.TODO(), bson.D{{"gid", gid}}, bson.D{{"$set", bson.D{{"host", int64(newownerpid)}}}}) - if err != nil { - //panic(err) - return - } -} diff --git a/database/update_user_rating.go b/database/update_user_rating.go deleted file mode 100644 index 5a4c418..0000000 --- a/database/update_user_rating.go +++ /dev/null @@ -1,15 +0,0 @@ -package database - -import ( - "context" - "strconv" - - "go.mongodb.org/mongo-driver/bson" -) - -func UpdateUserRating(pid uint32, gamemode uint32, rating uint32) { - _, err := usersCollection.UpdateOne(context.TODO(), bson.D{{"pid", pid}}, bson.D{{"$set", bson.D{{"rating." + strconv.FormatUint(uint64(gamemode), 10), rating}}}}) - if err != nil { - panic(err) - } -} diff --git a/example_secure.config b/example_secure.config deleted file mode 100644 index cefe6d8..0000000 --- a/example_secure.config +++ /dev/null @@ -1,21 +0,0 @@ -PrudpVersion=1 -SignatureVersion=1 -ServerPort=60003 -KerberosKeySize=32 -AccessKey=6f599f81 -ServerName=Pretendo MK7 Secure -NexVersion=30500 -AccountDatabaseIP=8.8.8.8 -AccountDatabasePort=1000 -NEXDatabaseIP=8.8.8.8 -NEXDatabasePort=1000 -DatabaseUseAuth=false -AccountDatabase=pretendo -PNIDCollection=pnids -NexAccountsCollection=nexaccounts -SplatoonDatabase=mk7 -RoomsCollection=rooms -SessionsCollection=sessions -UsersCollection=users -RegionsCollection=regions -TournamentsCollection=tourneys \ No newline at end of file diff --git a/globals/globals.go b/globals/globals.go index a246c66..61546bd 100644 --- a/globals/globals.go +++ b/globals/globals.go @@ -1,22 +1,17 @@ package globals import ( - "github.com/PretendoNetwork/mario-kart-7-secure/types" + pb "github.com/PretendoNetwork/grpc-go/account" "github.com/PretendoNetwork/nex-go" "github.com/PretendoNetwork/plogger-go" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" ) -var Logger = plogger.NewLogger() -var NEXServer *nex.Server -var Config *types.ServerConfig - -var RegionList = []string{"Worldwide", "Japan", "United States", "Europe", "Korea", "China", "Taiwan"} -var GameModes = []string{"Turf War", "Unk1", "Unk2", "Private Battle", "Unk4"} -var CCList = []string{"Unk", "200cc", "50cc", "100cc", "150cc", "Mirror", "BattleCC"} -var ItemModes = []string{"Unk1", "Unk2", "Unk3", "Unk4", "Unk5", "Normal", "Unk7", "All Items", "Shells Only", "Bananas Only", "Mushrooms Only", "Bob-ombs Only", "No Items", "No Items or Coins", "Frantic"} -var VehicleModes = []string{"All Vehicles", "Karts Only", "Bikes Only"} -var ControllerModes = []string{"Unk", "Tilt Only", "All Controls"} -var DLCModes = []string{"No DLC", "DLC Pack 1 Only", "DLC Pack 2 Only", "Both DLC Packs"} - -var GlobalGIDstring = "" -var GlobalHostString = "" +var Logger *plogger.Logger +var KerberosPassword = "password" // * Default password +var AuthenticationServer *nex.Server +var SecureServer *nex.Server +var GRPCAccountClientConnection *grpc.ClientConn +var GRPCAccountClient pb.AccountClient +var GRPCAccountCommonMetadata metadata.MD diff --git a/globals/password_from_pid.go b/globals/password_from_pid.go new file mode 100644 index 0000000..f59823c --- /dev/null +++ b/globals/password_from_pid.go @@ -0,0 +1,22 @@ +package globals + +import ( + "context" + + pb "github.com/PretendoNetwork/grpc-go/account" + "github.com/PretendoNetwork/nex-go" + "github.com/PretendoNetwork/nex-protocols-go/globals" + "google.golang.org/grpc/metadata" +) + +func PasswordFromPID(pid uint32) (string, uint32) { + ctx := metadata.NewOutgoingContext(context.Background(), GRPCAccountCommonMetadata) + + response, err := GRPCAccountClient.GetNEXPassword(ctx, &pb.GetNEXPasswordRequest{Pid: pid}) + if err != nil { + globals.Logger.Error(err.Error()) + return "", nex.Errors.RendezVous.InvalidUsername + } + + return response.Password, 0 +} diff --git a/go.mod b/go.mod index 56f5f5e..68e6c29 100644 --- a/go.mod +++ b/go.mod @@ -1,32 +1,31 @@ -module github.com/PretendoNetwork/mario-kart-7-secure +module github.com/PretendoNetwork/mario-kart-7 go 1.19 require ( - github.com/PretendoNetwork/nex-go v1.0.16 - github.com/PretendoNetwork/nex-protocols-common-go v1.0.17 - github.com/PretendoNetwork/nex-protocols-go v1.0.23 - github.com/PretendoNetwork/plogger-go v1.0.2 - go.mongodb.org/mongo-driver v1.11.4 + github.com/PretendoNetwork/grpc-go v1.0.2 + github.com/PretendoNetwork/nex-go v1.0.41 + github.com/PretendoNetwork/nex-protocols-common-go v1.0.28 + github.com/PretendoNetwork/nex-protocols-go v1.0.53 + github.com/PretendoNetwork/plogger-go v1.0.4 + github.com/joho/godotenv v1.5.1 + github.com/lib/pq v1.10.9 + google.golang.org/grpc v1.58.2 ) require ( github.com/fatih/color v1.15.0 // indirect - github.com/golang/snappy v0.0.4 // indirect - github.com/jwalton/go-supportscolor v1.1.0 // indirect - github.com/klauspost/compress v1.16.4 // indirect + github.com/golang/protobuf v1.5.3 // indirect + github.com/jwalton/go-supportscolor v1.2.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect - github.com/mattn/go-isatty v0.0.18 // indirect - github.com/montanaflynn/stats v0.7.0 // indirect - github.com/pkg/errors v0.9.1 // indirect + github.com/mattn/go-isatty v0.0.19 // indirect github.com/superwhiskers/crunch/v3 v3.5.7 // indirect - github.com/xdg-go/pbkdf2 v1.0.0 // indirect - github.com/xdg-go/scram v1.1.2 // indirect - github.com/xdg-go/stringprep v1.0.4 // indirect - github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect - golang.org/x/crypto v0.8.0 // indirect - golang.org/x/sync v0.1.0 // indirect - golang.org/x/sys v0.7.0 // indirect - golang.org/x/term v0.7.0 // indirect - golang.org/x/text v0.9.0 // indirect + golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect + golang.org/x/mod v0.13.0 // indirect + golang.org/x/net v0.15.0 // indirect + golang.org/x/sys v0.13.0 // indirect + golang.org/x/term v0.13.0 // indirect + golang.org/x/text v0.13.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 // indirect + google.golang.org/protobuf v1.31.0 // indirect ) diff --git a/go.sum b/go.sum index 6223d60..5d2f54e 100644 --- a/go.sum +++ b/go.sum @@ -1,113 +1,56 @@ -github.com/PretendoNetwork/nex-go v1.0.16 h1:g2lJW8G+WOS6/8qsgQ0+jLlzemjJ60tQtLqloLTjElo= -github.com/PretendoNetwork/nex-go v1.0.16/go.mod h1:Bx2ONeSefnbJyE0IDIwGopxrjRrnszOV/uQv74Cx+m0= -github.com/PretendoNetwork/nex-protocols-common-go v1.0.17 h1:w0n+RJAHANgX/iVnL5qRiyPJeDHcwr8w6DzDeFqxkqk= -github.com/PretendoNetwork/nex-protocols-common-go v1.0.17/go.mod h1:5Y3jRIbvDxasj0CIvd024za+t9LMbD0OZkBwyKICijg= -github.com/PretendoNetwork/nex-protocols-go v1.0.23 h1:aWUuzsPuNN0hrt2NegDcxrV5Uq0/8kpkIN+p/nCQRp4= -github.com/PretendoNetwork/nex-protocols-go v1.0.23/go.mod h1:Bt7hI7hJ+8r6UU5LSs+3xx4xzsvMVeEUkAW8Cg3lGjQ= -github.com/PretendoNetwork/plogger-go v1.0.2 h1:vWKEnEmJJzYwqLxLyiSsAvCrZV6qnnu/a0GQOjIfzY0= -github.com/PretendoNetwork/plogger-go v1.0.2/go.mod h1:7kD6M4vPq1JL4LTuPg6kuB1OvUBOwQOtAvTaUwMbwvU= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/PretendoNetwork/grpc-go v1.0.2 h1:9TvKmX7dCOANyoHEra1MMYqS1N/RGav66TRG4SHInvo= +github.com/PretendoNetwork/grpc-go v1.0.2/go.mod h1:XZjEsij9lL7HJBNkH6JPbBIkUSq/1rjflvjGdv+DAj0= +github.com/PretendoNetwork/nex-go v1.0.41 h1:TcBb1Bpe2KAB+AXaGX1K9NVQBRtZJIoy4yCvRde2xbI= +github.com/PretendoNetwork/nex-go v1.0.41/go.mod h1:QwHEa165DeVd0xIuthrgc3j6NWXT8lyPSG6t5kC/Shk= +github.com/PretendoNetwork/nex-protocols-common-go v1.0.28 h1:ErkWga7Uzn4WoDU8Q/Sy+geHy0fF0G/5wFnL5i6hngI= +github.com/PretendoNetwork/nex-protocols-common-go v1.0.28/go.mod h1:4jYhLg+Cb2qhJHyyA+f2OwCrmc98zuTO3JPWK22mIKw= +github.com/PretendoNetwork/nex-protocols-go v1.0.53 h1:o56sr8D2Mx//pGEFF/pzDTwOWusI0aQVehoyRYqSqVo= +github.com/PretendoNetwork/nex-protocols-go v1.0.53/go.mod h1:9r4KbNELVZj01TY8p+FGYDb9+e4mHLiWKo5NL1fPqD8= +github.com/PretendoNetwork/plogger-go v1.0.4 h1:PF7xHw9eDRHH+RsAP9tmAE7fG0N0p6H4iPwHKnsoXwc= +github.com/PretendoNetwork/plogger-go v1.0.4/go.mod h1:7kD6M4vPq1JL4LTuPg6kuB1OvUBOwQOtAvTaUwMbwvU= github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= -github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= -github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ= -github.com/jwalton/go-supportscolor v1.1.0 h1:HsXFJdMPjRUAx8cIW6g30hVSFYaxh9yRQwEWgkAR7lQ= -github.com/jwalton/go-supportscolor v1.1.0/go.mod h1:hFVUAZV2cWg+WFFC4v8pT2X/S2qUUBYMioBD9AINXGs= -github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= -github.com/klauspost/compress v1.16.4 h1:91KN02FnsOYhuunwU4ssRe8lc2JosWmizWa91B5v1PU= -github.com/klauspost/compress v1.16.4/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg= +github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +github.com/jwalton/go-supportscolor v1.2.0 h1:g6Ha4u7Vm3LIsQ5wmeBpS4gazu0UP1DRDE8y6bre4H8= +github.com/jwalton/go-supportscolor v1.2.0/go.mod h1:hFVUAZV2cWg+WFFC4v8pT2X/S2qUUBYMioBD9AINXGs= +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-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= -github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= -github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= -github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= -github.com/montanaflynn/stats v0.7.0 h1:r3y12KyNxj/Sb/iOE46ws+3mS1+MZca1wlHQFPsY/JU= -github.com/montanaflynn/stats v0.7.0/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= -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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= -github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA= +github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/superwhiskers/crunch/v3 v3.5.7 h1:N9RLxaR65C36i26BUIpzPXGy2f6pQ7wisu2bawbKNqg= github.com/superwhiskers/crunch/v3 v3.5.7/go.mod h1:4ub2EKgF1MAhTjoOCTU4b9uLMsAweHEa89aRrfAypXA= -github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= -github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= -github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= -github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI= -github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g= -github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= -github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= -github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8= -github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= -github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= -github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= -github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= -github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= -github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.11.4 h1:4ayjakA013OdpGyL2K3ZqylTac/rMjrJOMZ1EHizXas= -go.mongodb.org/mongo-driver v1.11.4/go.mod h1:PTSz5yu21bkT/wXpkS7WR5f0ddqw5quethTUn9WM+2g= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ= -golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= -golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= +golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= +golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY= +golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.15.0 h1:ugBLEUaxABaB5AJqW9enI0ACdci2RUd4eP51NTBvuJ8= +golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210220050731-9a76102bfb43/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= -golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE= +golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= -golang.org/x/term v0.7.0 h1:BEvjmm5fURWqcfbSKTdpkDXYBrUS1c0m8agp14W48vQ= -golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE= -golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/term v0.13.0 h1:bb+I9cTfFazGW51MZqBVmZy7+JEJMouUHTUSKVQLBek= +golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U= +golang.org/x/text v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= +golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97 h1:6GQBEOdGkX6MMTLT9V+TjtIRZCw9VPD5Z+yHY9wMgS0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20231002182017-d307bd883b97/go.mod h1:v7nGkzlmW8P3n/bKmWBn2WpBjpOEx8Q6gMueudAmKfY= +google.golang.org/grpc v1.58.2 h1:SXUpjxeVF3FKrTYQI4f4KvbGD5u2xccdYdurwowix5I= +google.golang.org/grpc v1.58.2/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSsCJk0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= diff --git a/init.go b/init.go index 5c2dc10..0812e34 100644 --- a/init.go +++ b/init.go @@ -1,14 +1,114 @@ package main import ( - "github.com/PretendoNetwork/mario-kart-7-secure/database" - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - "github.com/PretendoNetwork/mario-kart-7-secure/utility" + "fmt" + "os" + "strconv" + "strings" + + pb "github.com/PretendoNetwork/grpc-go/account" + "github.com/PretendoNetwork/mario-kart-7/globals" + "github.com/PretendoNetwork/mario-kart-7/database" + "github.com/PretendoNetwork/plogger-go" + "github.com/joho/godotenv" + "google.golang.org/grpc" + "google.golang.org/grpc/credentials/insecure" + "google.golang.org/grpc/metadata" ) func init() { + globals.Logger = plogger.NewLogger() + + var err error + + err = godotenv.Load() + if err != nil { + globals.Logger.Warning("Error loading .env file") + } + + postgresURI := os.Getenv("PN_MK7_POSTGRES_URI") + kerberosPassword := os.Getenv("PN_MK7_KERBEROS_PASSWORD") + authenticationServerPort := os.Getenv("PN_MK7_AUTHENTICATION_SERVER_PORT") + secureServerHost := os.Getenv("PN_MK7_SECURE_SERVER_HOST") + secureServerPort := os.Getenv("PN_MK7_SECURE_SERVER_PORT") + accountGRPCHost := os.Getenv("PN_MK7_ACCOUNT_GRPC_HOST") + accountGRPCPort := os.Getenv("PN_MK7_ACCOUNT_GRPC_PORT") + accountGRPCAPIKey := os.Getenv("PN_MK7_ACCOUNT_GRPC_API_KEY") + + if strings.TrimSpace(postgresURI) == "" { + globals.Logger.Error("PN_MK7_POSTGRES_URI environment variable not set") + os.Exit(0) + } + + if strings.TrimSpace(kerberosPassword) == "" { + globals.Logger.Warningf("PN_MK7_KERBEROS_PASSWORD environment variable not set. Using default password: %q", globals.KerberosPassword) + } else { + globals.KerberosPassword = kerberosPassword + } + + if strings.TrimSpace(authenticationServerPort) == "" { + globals.Logger.Error("PN_MK7_AUTHENTICATION_SERVER_PORT environment variable not set") + os.Exit(0) + } + + if port, err := strconv.Atoi(authenticationServerPort); err != nil { + globals.Logger.Errorf("PN_MK7_AUTHENTICATION_SERVER_PORT is not a valid port. Expected 0-65535, got %s", authenticationServerPort) + os.Exit(0) + } else if port < 0 || port > 65535 { + globals.Logger.Errorf("PN_MK7_AUTHENTICATION_SERVER_PORT is not a valid port. Expected 0-65535, got %s", authenticationServerPort) + os.Exit(0) + } + + if strings.TrimSpace(secureServerHost) == "" { + globals.Logger.Error("PN_MK7_SECURE_SERVER_HOST environment variable not set") + os.Exit(0) + } + + if strings.TrimSpace(secureServerPort) == "" { + globals.Logger.Error("PN_MK7_SECURE_SERVER_PORT environment variable not set") + os.Exit(0) + } + + if port, err := strconv.Atoi(secureServerPort); err != nil { + globals.Logger.Errorf("PN_MK7_SECURE_SERVER_PORT is not a valid port. Expected 0-65535, got %s", secureServerPort) + os.Exit(0) + } else if port < 0 || port > 65535 { + globals.Logger.Errorf("PN_MK7_SECURE_SERVER_PORT is not a valid port. Expected 0-65535, got %s", secureServerPort) + os.Exit(0) + } + + if strings.TrimSpace(accountGRPCHost) == "" { + globals.Logger.Error("PN_MK7_ACCOUNT_GRPC_HOST environment variable not set") + os.Exit(0) + } + + if strings.TrimSpace(accountGRPCPort) == "" { + globals.Logger.Error("PN_MK7_ACCOUNT_GRPC_PORT environment variable not set") + os.Exit(0) + } + + if port, err := strconv.Atoi(accountGRPCPort); err != nil { + globals.Logger.Errorf("PN_MK7_ACCOUNT_GRPC_PORT is not a valid port. Expected 0-65535, got %s", accountGRPCPort) + os.Exit(0) + } else if port < 0 || port > 65535 { + globals.Logger.Errorf("PN_MK7_ACCOUNT_GRPC_PORT is not a valid port. Expected 0-65535, got %s", accountGRPCPort) + os.Exit(0) + } + + if strings.TrimSpace(accountGRPCAPIKey) == "" { + globals.Logger.Warning("Insecure gRPC server detected. PN_MK7_ACCOUNT_GRPC_API_KEY environment variable not set") + } + + globals.GRPCAccountClientConnection, err = grpc.Dial(fmt.Sprintf("%s:%s", accountGRPCHost, accountGRPCPort), grpc.WithTransportCredentials(insecure.NewCredentials())) + if err != nil { + globals.Logger.Criticalf("Failed to connect to account gRPC server: %v", err) + os.Exit(0) + } - globals.Config, _ = utility.ImportConfigFromFile("secure.config") + globals.GRPCAccountClient = pb.NewAccountClient(globals.GRPCAccountClientConnection) + globals.GRPCAccountCommonMetadata = metadata.Pairs( + "X-API-Key", accountGRPCAPIKey, + ) - database.ConnectAll() + database.ConnectPostgres() } diff --git a/main.go b/main.go index 50d916f..d544784 100644 --- a/main.go +++ b/main.go @@ -3,16 +3,17 @@ package main import ( "sync" - "github.com/PretendoNetwork/mario-kart-7-secure/nex" + "github.com/PretendoNetwork/mario-kart-7/nex" ) var wg sync.WaitGroup func main() { - wg.Add(1) + wg.Add(2) // TODO - Add gRPC server - go nex.StartNEXServer() + go nex.StartAuthenticationServer() + go nex.StartSecureServer() wg.Wait() } diff --git a/nex/authentication.go b/nex/authentication.go new file mode 100644 index 0000000..b0524f3 --- /dev/null +++ b/nex/authentication.go @@ -0,0 +1,33 @@ +package nex + +import ( + "fmt" + "os" + + nex "github.com/PretendoNetwork/nex-go" + "github.com/PretendoNetwork/mario-kart-7/globals" +) + +var serverBuildString string + +func StartAuthenticationServer() { + globals.AuthenticationServer = nex.NewServer() + globals.AuthenticationServer.SetPRUDPVersion(0) + globals.AuthenticationServer.SetDefaultNEXVersion(nex.NewNEXVersion(2, 4, 3)) + + globals.AuthenticationServer.SetKerberosPassword(globals.KerberosPassword) + globals.AuthenticationServer.SetAccessKey("6181dff1") + + globals.AuthenticationServer.On("Data", func(packet *nex.PacketV0) { + request := packet.RMCRequest() + + fmt.Println("=== MK7 - Auth ===") + fmt.Printf("Protocol ID: %#v\n", request.ProtocolID()) + fmt.Printf("Method ID: %#v\n", request.MethodID()) + fmt.Println("==================") + }) + + registerCommonAuthenticationServerProtocols() + + globals.AuthenticationServer.Listen(fmt.Sprintf(":%s", os.Getenv("PN_MK7_AUTHENTICATION_SERVER_PORT"))) +} diff --git a/nex/match-making-ext/end_participation.go b/nex/match-making-ext/end_participation.go deleted file mode 100644 index 2af16ef..0000000 --- a/nex/match-making-ext/end_participation.go +++ /dev/null @@ -1,36 +0,0 @@ -package nex_match_making_ext - -import ( - "github.com/PretendoNetwork/mario-kart-7-secure/database" - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - match_making_ext "github.com/PretendoNetwork/nex-protocols-go/match-making-ext" -) - -func EndParticipation(err error, client *nex.Client, callID uint32, idGathering uint32, strMessage string) { - database.RemovePlayerFromRoom(idGathering, client.PID()) - - rmcResponseStream := nex.NewStreamOut(globals.NEXServer) - - rmcResponseStream.WriteBool(true) // %retval% - - rmcResponseBody := rmcResponseStream.Bytes() - - rmcResponse := nex.NewRMCResponse(match_making_ext.ProtocolID, callID) - rmcResponse.SetSuccess(match_making_ext.MethodEndParticipation, rmcResponseBody) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/matchmake-extension/auto_matchmake_postpone.go b/nex/matchmake-extension/auto_matchmake_postpone.go deleted file mode 100644 index b642155..0000000 --- a/nex/matchmake-extension/auto_matchmake_postpone.go +++ /dev/null @@ -1,144 +0,0 @@ -package nex_matchmake_extension - -import ( - "fmt" - "math" - "strconv" - - "github.com/PretendoNetwork/mario-kart-7-secure/database" - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - match_making "github.com/PretendoNetwork/nex-protocols-go/match-making" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" - "github.com/PretendoNetwork/nex-protocols-go/notifications" -) - -func AutoMatchmake_Postpone(err error, client *nex.Client, callID uint32, matchmakeSession *match_making.MatchmakeSession, message string) { - - gameConfig := matchmakeSession.Attributes[2] - fmt.Println(strconv.FormatUint(uint64(gameConfig), 2)) - fmt.Println("===== MATCHMAKE SESSION SEARCH =====") - fmt.Println("COMMUNITY GID: " + strconv.Itoa((int)(matchmakeSession.Attributes[0]))) - fmt.Println("UPDATE: " + strconv.Itoa((int)(matchmakeSession.Attributes[2]))) - fmt.Println("REGION: " + strconv.Itoa((int)(matchmakeSession.Attributes[3]))) - //fmt.Println("REGION: " + regionList[matchmakeSession.Attributes[3]]) - fmt.Println("GAME MODE: " + strconv.Itoa((int)(matchmakeSession.GameMode))) - //fmt.Println("GAME MODE: " + gameModes[matchmakeSession.GameMode]) - //fmt.Println("CC: " + ccList[gameConfig%0b111]) - gameConfig = gameConfig >> 12 - //fmt.Println("DLC MODE: " + dlcModes[matchmakeSession.Attributes[5]&0xF]) - //fmt.Println("ITEM MODE: " + itemModes[gameConfig%0b1111]) - gameConfig = gameConfig >> 8 - //fmt.Println("VEHICLE MODE: " + vehicleModes[gameConfig%0b11]) - gameConfig = gameConfig >> 4 - //fmt.Println("CONTROLLER MODE: " + controllerModes[gameConfig%0b11]) - //fmt.Println("HAVE GUEST PLAYER: " + strconv.FormatBool(false)) - - fmt.Println("0: " + strconv.Itoa((int)(matchmakeSession.Attributes[0]))) - fmt.Println("2: " + strconv.Itoa((int)(matchmakeSession.Attributes[2]))) - fmt.Println("3: " + strconv.Itoa((int)(matchmakeSession.Attributes[3]))) - fmt.Println("5: " + strconv.Itoa((int)(matchmakeSession.Attributes[5]))) - - gid := database.FindRoom(matchmakeSession.GameMode, true, matchmakeSession.Attributes[3], matchmakeSession.Attributes[2], uint32(1), matchmakeSession.Attributes[5]&0xF, matchmakeSession.Attributes[0]) - if gid == math.MaxUint32 { - gid = database.NewRoom(client.PID(), matchmakeSession.GameMode, true, matchmakeSession.Attributes[3], matchmakeSession.Attributes[2], uint32(1), matchmakeSession.Attributes[5]&0xF, matchmakeSession.Attributes[0]) - } - - fmt.Println("GATHERING ID: " + strconv.Itoa((int)(gid))) - - database.AddPlayerToRoom(gid, client.PID(), uint32(1)) - - hostpid, gamemode, _, _, update := database.GetRoomInfo(gid) - - matchmakeSession.Gathering.ID = gid - matchmakeSession.Gathering.OwnerPID = hostpid - matchmakeSession.Gathering.HostPID = hostpid - matchmakeSession.GameMode = gamemode - matchmakeSession.Attributes[2] = update - - rmcResponseStream := nex.NewStreamOut(globals.NEXServer) - rmcResponseStream.WriteString("MatchmakeSession") - lengthStream := nex.NewStreamOut(globals.NEXServer) - lengthStream.WriteStructure(matchmakeSession.Gathering) - lengthStream.WriteStructure(matchmakeSession) - matchmakeSessionLength := uint32(len(lengthStream.Bytes())) - rmcResponseStream.WriteUInt32LE(matchmakeSessionLength + 4) - rmcResponseStream.WriteUInt32LE(matchmakeSessionLength) - rmcResponseStream.WriteStructure(matchmakeSession.Gathering) - rmcResponseStream.WriteStructure(matchmakeSession) - - rmcResponseBody := rmcResponseStream.Bytes() - - rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID) - rmcResponse.SetSuccess(matchmake_extension.MethodAutoMatchmake_Postpone, rmcResponseBody) - - var globalStation nex.StationURL - fmt.Println(globals.NEXServer.FindClientFromPID(hostpid).ConnectionID()) - stationUrlStringsOrig := database.GetPlayerURLs(globals.NEXServer.FindClientFromPID(hostpid).ConnectionID()) - globalStation.FromString(stationUrlStringsOrig[1]) - address := client.Address().IP.String() - port := strconv.Itoa(client.Address().Port) - - globalStation.SetAddress(address) - globalStation.SetPort(port) - - globalStationURL := globalStation.EncodeToString() - database.UpdatePlayerSessionURL(client.ConnectionID(), stationUrlStringsOrig[1], globalStationURL) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) - - rmcMessage := nex.NewRMCRequest() - rmcMessage.SetProtocolID(notifications.ProtocolID) - rmcMessage.SetCallID(0xffff0000 + callID) - rmcMessage.SetMethodID(notifications.MethodProcessNotificationEvent) - - oEvent := notifications.NewNotificationEvent() - oEvent.PIDSource = hostpid - oEvent.Type = 3001 // New participant - oEvent.Param1 = gid - oEvent.Param2 = client.PID() - - stream := nex.NewStreamOut(globals.NEXServer) - oEventBytes := oEvent.Bytes(stream) - rmcMessage.SetParameters(oEventBytes) - rmcMessageBytes := rmcMessage.Bytes() - - targetClient := globals.NEXServer.FindClientFromPID(uint32(hostpid)) - - messagePacket, _ := nex.NewPacketV0(targetClient, nil) - messagePacket.SetVersion(1) - messagePacket.SetSource(0xA1) - messagePacket.SetDestination(0xAF) - messagePacket.SetType(nex.DataPacket) - messagePacket.SetPayload(rmcMessageBytes) - - messagePacket.AddFlag(nex.FlagNeedsAck) - messagePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(messagePacket) - - messagePacket, _ = nex.NewPacketV0(client, nil) - messagePacket.SetVersion(1) - messagePacket.SetSource(0xA1) - messagePacket.SetDestination(0xAF) - messagePacket.SetType(nex.DataPacket) - messagePacket.SetPayload(rmcMessageBytes) - - messagePacket.AddFlag(nex.FlagNeedsAck) - messagePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(messagePacket) -} diff --git a/nex/matchmake-extension/close_participation.go b/nex/matchmake-extension/close_participation.go deleted file mode 100644 index 0792c29..0000000 --- a/nex/matchmake-extension/close_participation.go +++ /dev/null @@ -1,29 +0,0 @@ -package nex_matchmake_extension - -import ( - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" -) - -func CloseParticipation(err error, client *nex.Client, callID uint32, gatheringId uint32) { - //destroyRoom(gatheringId) - - rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID) - rmcResponse.SetSuccess(matchmake_extension.MethodCloseParticipation, nil) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/matchmake-extension/common/cleanup_search_matchmake_session.go b/nex/matchmake-extension/common/cleanup_search_matchmake_session.go new file mode 100644 index 0000000..76d6ce1 --- /dev/null +++ b/nex/matchmake-extension/common/cleanup_search_matchmake_session.go @@ -0,0 +1,10 @@ +package nex_matchmake_extension_common + +import ( + matchmaking_types "github.com/PretendoNetwork/nex-protocols-go/match-making/types" +) + +func CleanupSearchMatchmakeSession(matchmakeSession *matchmaking_types.MatchmakeSession) { + // Cleanup VR + matchmakeSession.Attributes[1] = 0 +} diff --git a/nex/matchmake-extension/create_community.go b/nex/matchmake-extension/create_community.go deleted file mode 100644 index e8a1022..0000000 --- a/nex/matchmake-extension/create_community.go +++ /dev/null @@ -1,39 +0,0 @@ -package nex_matchmake_extension - -import ( - "encoding/hex" - "fmt" - - "github.com/PretendoNetwork/mario-kart-7-secure/database" - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - match_making "github.com/PretendoNetwork/nex-protocols-go/match-making" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" -) - -func CreateCommunity(err error, client *nex.Client, callID uint32, community *match_making.PersistentGathering, strMessage string) { - rmcResponseStream := nex.NewStreamOut(globals.NEXServer) - gid := database.NewCommunity(client.PID(), community.M_CommunityType, community.M_Password, community.M_Attribs, community.M_ApplicationBuffer, community.M_ParticipationStartDate.Value(), community.M_ParticipationEndDate.Value()) - - rmcResponseStream.WriteUInt32LE(gid) - rmcResponseBody := rmcResponseStream.Bytes() - fmt.Println(hex.EncodeToString(rmcResponseBody)) - - rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID) - rmcResponse.SetSuccess(matchmake_extension.MethodCreateCommunity, rmcResponseBody) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/matchmake-extension/find_community_by_gathering_id.go b/nex/matchmake-extension/find_community_by_gathering_id.go deleted file mode 100644 index aba2e85..0000000 --- a/nex/matchmake-extension/find_community_by_gathering_id.go +++ /dev/null @@ -1,67 +0,0 @@ -package nex_matchmake_extension - -import ( - "encoding/hex" - "fmt" - - "github.com/PretendoNetwork/mario-kart-7-secure/database" - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - match_making "github.com/PretendoNetwork/nex-protocols-go/match-making" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" -) - -func FindCommunityByGatheringID(err error, client *nex.Client, callID uint32, lstGid []uint32) { - rmcResponseStream := nex.NewStreamOut(globals.NEXServer) - lstCommunity := make([]*match_making.PersistentGathering, 0) - for _, gid := range lstGid { - fmt.Println("gid1", gid) - if database.CommunityExists(gid) { - fmt.Println("gid2", gid) - host, communityType, password, attribs, application_buffer, start_date, end_date, sessions, participationCount := database.GetCommunityInfo(gid) - persistentGathering := match_making.NewPersistentGathering() - persistentGathering.Gathering.ID = gid - persistentGathering.Gathering.OwnerPID = host - persistentGathering.Gathering.HostPID = host - persistentGathering.Gathering.MinimumParticipants = 1 - persistentGathering.Gathering.MaximumParticipants = 8 - persistentGathering.M_CommunityType = communityType - persistentGathering.M_Password = password - persistentGathering.M_Attribs = attribs - persistentGathering.M_ApplicationBuffer = application_buffer - persistentGathering.M_ParticipationStartDate = nex.NewDateTime(start_date) - persistentGathering.M_ParticipationEndDate = nex.NewDateTime(end_date) - persistentGathering.M_ApplicationBuffer = application_buffer - persistentGathering.M_MatchmakeSessionCount = sessions - persistentGathering.M_ParticipationCount = participationCount - lstCommunity = append(lstCommunity, persistentGathering) - } - } - - rmcResponseStream.WriteUInt32LE(uint32(len(lstCommunity))) - fmt.Println("len", len(lstCommunity)) - for _, persistentGathering := range lstCommunity { - rmcResponseStream.WriteStructure(persistentGathering) - } - - rmcResponseBody := rmcResponseStream.Bytes() - fmt.Println(hex.EncodeToString(rmcResponseBody)) - - rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID) - rmcResponse.SetSuccess(matchmake_extension.MethodFindCommunityByGatheringID, rmcResponseBody) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/matchmake-extension/find_community_by_participant.go b/nex/matchmake-extension/find_community_by_participant.go deleted file mode 100644 index 9f0d7c4..0000000 --- a/nex/matchmake-extension/find_community_by_participant.go +++ /dev/null @@ -1,67 +0,0 @@ -package nex_matchmake_extension - -import ( - "encoding/hex" - "fmt" - - "github.com/PretendoNetwork/mario-kart-7-secure/database" - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - match_making "github.com/PretendoNetwork/nex-protocols-go/match-making" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" -) - -func FindCommunityByParticipant(err error, client *nex.Client, callID uint32, pid uint32, resultRange *nex.ResultRange) { - rmcResponseStream := nex.NewStreamOut(globals.NEXServer) - lstCommunity := make([]*match_making.PersistentGathering, 0) - lstGid := database.FindCommunitiesWithParticipant(pid) - for _, gid := range lstGid { - fmt.Println("gid1", gid) - if database.CommunityExists(gid) { - fmt.Println("gid2", gid) - host, communityType, password, attribs, application_buffer, start_date, end_date, sessions, participationCount := database.GetCommunityInfo(gid) - persistentGathering := match_making.NewPersistentGathering() - persistentGathering.Gathering.ID = gid - persistentGathering.Gathering.OwnerPID = host - persistentGathering.Gathering.HostPID = host - persistentGathering.Gathering.MinimumParticipants = 1 - persistentGathering.Gathering.MaximumParticipants = 8 - persistentGathering.M_CommunityType = communityType - persistentGathering.M_Password = password - persistentGathering.M_Attribs = attribs - persistentGathering.M_ApplicationBuffer = application_buffer - persistentGathering.M_ParticipationStartDate = nex.NewDateTime(start_date) - persistentGathering.M_ParticipationEndDate = nex.NewDateTime(end_date) - persistentGathering.M_MatchmakeSessionCount = sessions - persistentGathering.M_ParticipationCount = participationCount - lstCommunity = append(lstCommunity, persistentGathering) - } - } - - rmcResponseStream.WriteUInt32LE(uint32(len(lstCommunity))) - fmt.Println("len", len(lstCommunity)) - for _, persistentGathering := range lstCommunity { - rmcResponseStream.WriteStructure(persistentGathering) - } - - rmcResponseBody := rmcResponseStream.Bytes() - fmt.Println(hex.EncodeToString(rmcResponseBody)) - - rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID) - rmcResponse.SetSuccess(matchmake_extension.MethodFindCommunityByParticipant, rmcResponseBody) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/matchmake-extension/find_official_community.go b/nex/matchmake-extension/find_official_community.go deleted file mode 100644 index cadffd4..0000000 --- a/nex/matchmake-extension/find_official_community.go +++ /dev/null @@ -1,36 +0,0 @@ -package nex_matchmake_extension - -import ( - "encoding/hex" - "fmt" - - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" -) - -func FindOfficialCommunity(err error, client *nex.Client, callID uint32, isAvailableOnly bool, resultRange *nex.ResultRange) { - rmcResponseStream := nex.NewStreamOut(globals.NEXServer) - rmcResponseStream.WriteUInt32LE(0) - - rmcResponseBody := rmcResponseStream.Bytes() - fmt.Println(hex.EncodeToString(rmcResponseBody)) - - rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID) - rmcResponse.SetSuccess(matchmake_extension.MethodFindOfficialCommunity, rmcResponseBody) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/matchmake-extension/get_playing_session.go b/nex/matchmake-extension/get_playing_session.go deleted file mode 100644 index 7a5f349..0000000 --- a/nex/matchmake-extension/get_playing_session.go +++ /dev/null @@ -1,36 +0,0 @@ -package nex_matchmake_extension - -import ( - "encoding/hex" - "fmt" - - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" -) - -func GetPlayingSession(err error, client *nex.Client, callID uint32, listPID []uint32) { - rmcResponseStream := nex.NewStreamOut(globals.NEXServer) - rmcResponseStream.WriteUInt32LE(0) - - rmcResponseBody := rmcResponseStream.Bytes() - fmt.Println(hex.EncodeToString(rmcResponseBody)) - - rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID) - rmcResponse.SetSuccess(matchmake_extension.MethodGetPlayingSession, rmcResponseBody) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/matchmake-extension/get_simple_community.go b/nex/matchmake-extension/get_simple_community.go deleted file mode 100644 index 681229c..0000000 --- a/nex/matchmake-extension/get_simple_community.go +++ /dev/null @@ -1,53 +0,0 @@ -package nex_matchmake_extension - -import ( - "encoding/hex" - "fmt" - - "github.com/PretendoNetwork/mario-kart-7-secure/database" - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - match_making "github.com/PretendoNetwork/nex-protocols-go/match-making" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" -) - -func GetSimpleCommunity(err error, client *nex.Client, callID uint32, gatheringIdList []uint32) { - rmcResponseStream := nex.NewStreamOut(globals.NEXServer) - lstSimpleCommunityList := make([]*match_making.SimpleCommunity, 0) - for _, gid := range gatheringIdList { - fmt.Println("gid1", gid) - if database.CommunityExists(gid) { - fmt.Println("gid2", gid) - _, _, _, _, _, _, _, sessions, _ := database.GetCommunityInfo(gid) - simpleCommunity := match_making.NewSimpleCommunity() - simpleCommunity.M_GatheringID = gid - simpleCommunity.M_MatchmakeSessionCount = sessions - lstSimpleCommunityList = append(lstSimpleCommunityList, simpleCommunity) - } - } - - rmcResponseStream.WriteUInt32LE(uint32(len(lstSimpleCommunityList))) - for _, simpleCommunity := range lstSimpleCommunityList { - rmcResponseStream.WriteStructure(simpleCommunity) - } - rmcResponseBody := rmcResponseStream.Bytes() - fmt.Println(hex.EncodeToString(rmcResponseBody)) - - rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID) - rmcResponse.SetSuccess(matchmake_extension.MethodGetSimpleCommunity, rmcResponseBody) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/matchmake-extension/update_progress_score.go b/nex/matchmake-extension/update_progress_score.go deleted file mode 100644 index 289fc77..0000000 --- a/nex/matchmake-extension/update_progress_score.go +++ /dev/null @@ -1,28 +0,0 @@ -package nex_matchmake_extension - -import ( - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" -) - -func UpdateProgressScore(err error, client *nex.Client, callID uint32, gid uint32, progressScore uint8) { - //destroyRoom(gatheringId) - rmcResponse := nex.NewRMCResponse(matchmake_extension.ProtocolID, callID) - rmcResponse.SetSuccess(matchmake_extension.MethodUpdateProgressScore, nil) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/ranking/upload_common_data.go b/nex/ranking/upload_common_data.go deleted file mode 100644 index df36de1..0000000 --- a/nex/ranking/upload_common_data.go +++ /dev/null @@ -1,27 +0,0 @@ -package nex_ranking - -import ( - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - "github.com/PretendoNetwork/nex-protocols-go/ranking" -) - -func UploadCommonData(err error, client *nex.Client, callID uint32, commonData []byte, uniqueID uint64) { - rmcResponse := nex.NewRMCResponse(ranking.ProtocolID, callID) - rmcResponse.SetSuccess(ranking.MethodUploadCommonData, nil) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/register_common_authentication_server_protocols.go b/nex/register_common_authentication_server_protocols.go new file mode 100644 index 0000000..103f0bd --- /dev/null +++ b/nex/register_common_authentication_server_protocols.go @@ -0,0 +1,31 @@ +package nex + +import ( + "os" + "strconv" + + nex "github.com/PretendoNetwork/nex-go" + ticket_granting "github.com/PretendoNetwork/nex-protocols-common-go/ticket-granting" + "github.com/PretendoNetwork/mario-kart-7/globals" +) + +func registerCommonAuthenticationServerProtocols() { + ticketGrantingProtocol := ticket_granting.NewCommonTicketGrantingProtocol(globals.AuthenticationServer) + + port, _ := strconv.Atoi(os.Getenv("PN_MK7_SECURE_SERVER_PORT")) + + secureStationURL := nex.NewStationURL("") + secureStationURL.SetScheme("prudps") + secureStationURL.SetAddress(os.Getenv("PN_MK7_SECURE_SERVER_HOST")) + secureStationURL.SetPort(uint32(port)) + secureStationURL.SetCID(1) + secureStationURL.SetPID(2) + secureStationURL.SetSID(1) + secureStationURL.SetStream(10) + secureStationURL.SetType(2) + + ticketGrantingProtocol.SetSecureStationURL(secureStationURL) + ticketGrantingProtocol.SetBuildName(serverBuildString) + + globals.AuthenticationServer.SetPasswordFromPIDFunction(globals.PasswordFromPID) +} diff --git a/nex/register_common_protocols.go b/nex/register_common_protocols.go deleted file mode 100644 index bfa238f..0000000 --- a/nex/register_common_protocols.go +++ /dev/null @@ -1,30 +0,0 @@ -package nex - -import ( - "github.com/PretendoNetwork/mario-kart-7-secure/database" - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nexmatchmaking "github.com/PretendoNetwork/nex-protocols-common-go/matchmaking" - nexnattraversal "github.com/PretendoNetwork/nex-protocols-common-go/nat-traversal" - nexsecure "github.com/PretendoNetwork/nex-protocols-common-go/secure-connection" -) - -func registerCommonProtocols() { - secureConnectionCommonProtocol := nexsecure.NewCommonSecureConnectionProtocol(globals.NEXServer) - secureConnectionCommonProtocol.AddConnection(database.AddPlayerSession) - secureConnectionCommonProtocol.UpdateConnection(database.UpdatePlayerSessionAll) - secureConnectionCommonProtocol.DoesConnectionExist(database.DoesSessionExist) - secureConnectionCommonProtocol.ReplaceConnectionUrl(database.UpdatePlayerSessionURL) - - natTraversalCommonProtocol := nexnattraversal.InitNatTraversalProtocol(globals.NEXServer) - nexnattraversal.GetConnectionUrls(database.GetPlayerURLs) - nexnattraversal.ReplaceConnectionUrl(database.UpdatePlayerSessionURL) - _ = natTraversalCommonProtocol - - matchMakingCommonProtocol := nexmatchmaking.InitMatchmakingProtocol(globals.NEXServer) - nexmatchmaking.GetConnectionUrls(database.GetPlayerURLs) - nexmatchmaking.UpdateRoomHost(database.UpdateRoomHost) - nexmatchmaking.DestroyRoom(database.DestroyRoom) - nexmatchmaking.GetRoomInfo(database.GetRoomInfo) - nexmatchmaking.GetRoomPlayers(database.GetRoomPlayers) - _ = matchMakingCommonProtocol -} diff --git a/nex/register_common_secure_server_protocols.go b/nex/register_common_secure_server_protocols.go new file mode 100644 index 0000000..bfc761f --- /dev/null +++ b/nex/register_common_secure_server_protocols.go @@ -0,0 +1,26 @@ +package nex + +import ( + "github.com/PretendoNetwork/mario-kart-7/globals" + matchmake_extension "github.com/PretendoNetwork/nex-protocols-common-go/matchmake-extension" + matchmaking "github.com/PretendoNetwork/nex-protocols-common-go/matchmaking" + match_making_ext "github.com/PretendoNetwork/nex-protocols-common-go/matchmaking-ext" + nattraversal "github.com/PretendoNetwork/nex-protocols-common-go/nat-traversal" + secure "github.com/PretendoNetwork/nex-protocols-common-go/secure-connection" + + nex_matchmake_extension_common "github.com/PretendoNetwork/mario-kart-7/nex/matchmake-extension/common" +) + +func registerCommonSecureServerProtocols() { + secure.NewCommonSecureConnectionProtocol(globals.SecureServer) + + nattraversal.NewCommonNATTraversalProtocol(globals.SecureServer) + + matchmaking.NewCommonMatchMakingProtocol(globals.SecureServer) + + match_making_ext.NewCommonMatchMakingExtProtocol(globals.SecureServer) + + matchmakeExtensionProtocol := matchmake_extension.NewCommonMatchmakeExtensionProtocol(globals.SecureServer) + + matchmakeExtensionProtocol.CleanupSearchMatchmakeSession(nex_matchmake_extension_common.CleanupSearchMatchmakeSession) +} diff --git a/nex/register_nex_protocols.go b/nex/register_nex_protocols.go deleted file mode 100644 index 0ca0b07..0000000 --- a/nex/register_nex_protocols.go +++ /dev/null @@ -1,41 +0,0 @@ -package nex - -import ( - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - match_making_ext "github.com/PretendoNetwork/nex-protocols-go/match-making-ext" - matchmake_extension "github.com/PretendoNetwork/nex-protocols-go/matchmake-extension" - "github.com/PretendoNetwork/nex-protocols-go/ranking" - "github.com/PretendoNetwork/nex-protocols-go/utility" - - nex_match_making_ext "github.com/PretendoNetwork/mario-kart-7-secure/nex/match-making-ext" - nex_matchmake_extension "github.com/PretendoNetwork/mario-kart-7-secure/nex/matchmake-extension" - nex_ranking "github.com/PretendoNetwork/mario-kart-7-secure/nex/ranking" - nex_utility "github.com/PretendoNetwork/mario-kart-7-secure/nex/utility" -) - -func registerNEXProtocols() { - matchmakeExtensionProtocol := matchmake_extension.NewMatchmakeExtensionProtocol(globals.NEXServer) - - matchmakeExtensionProtocol.CloseParticipation(nex_matchmake_extension.CloseParticipation) - matchmakeExtensionProtocol.AutoMatchmake_Postpone(nex_matchmake_extension.AutoMatchmake_Postpone) - matchmakeExtensionProtocol.GetPlayingSession(nex_matchmake_extension.GetPlayingSession) - matchmakeExtensionProtocol.CreateCommunity(nex_matchmake_extension.CreateCommunity) - matchmakeExtensionProtocol.FindCommunityByGatheringID(nex_matchmake_extension.FindCommunityByGatheringID) - matchmakeExtensionProtocol.FindOfficialCommunity(nex_matchmake_extension.FindOfficialCommunity) - matchmakeExtensionProtocol.FindCommunityByParticipant(nex_matchmake_extension.FindCommunityByParticipant) - matchmakeExtensionProtocol.GetSimpleCommunity(nex_matchmake_extension.GetSimpleCommunity) - matchmakeExtensionProtocol.UpdateProgressScore(nex_matchmake_extension.UpdateProgressScore) - - matchMakingExtProtocol := match_making_ext.NewMatchMakingExtProtocol(globals.NEXServer) - - matchMakingExtProtocol.EndParticipation(nex_match_making_ext.EndParticipation) - - rankingProtocol := ranking.NewRankingProtocol(globals.NEXServer) - - rankingProtocol.UploadCommonData(nex_ranking.UploadCommonData) - - utilityProtocol := utility.NewUtilityProtocol(globals.NEXServer) - - utilityProtocol.GetAssociatedNexUniqueIDWithMyPrincipalID(nex_utility.GetAssociatedNexUniqueIDWithMyPrincipalID) - utilityProtocol.AssociateNexUniqueIDsWithMyPrincipalID(nex_utility.AssociateNexUniqueIDsWithMyPrincipalID) -} diff --git a/nex/register_secure_server_nex_protocols.go b/nex/register_secure_server_nex_protocols.go new file mode 100644 index 0000000..499f763 --- /dev/null +++ b/nex/register_secure_server_nex_protocols.go @@ -0,0 +1,22 @@ +package nex + +import ( + "github.com/PretendoNetwork/mario-kart-7/globals" + datastore "github.com/PretendoNetwork/nex-protocols-go/datastore" + ranking "github.com/PretendoNetwork/nex-protocols-go/ranking" + storage_manager "github.com/PretendoNetwork/nex-protocols-go/storage-manager" + + nex_storage_manager "github.com/PretendoNetwork/mario-kart-7/nex/storage-manager" +) + +func registerSecureServerNEXProtocols() { + _ = datastore.NewProtocol(globals.SecureServer) + + // TODO - Add legacy ranking protocol! + _ = ranking.NewProtocol(globals.SecureServer) + + storageManagerProtocol := storage_manager.NewProtocol(globals.SecureServer) + + storageManagerProtocol.AcquireCardID(nex_storage_manager.AcquireCardID) + storageManagerProtocol.ActivateWithCardID(nex_storage_manager.ActivateWithCardID) +} diff --git a/nex/secure.go b/nex/secure.go new file mode 100644 index 0000000..8e84ade --- /dev/null +++ b/nex/secure.go @@ -0,0 +1,32 @@ +package nex + +import ( + "fmt" + "os" + + "github.com/PretendoNetwork/mario-kart-7/globals" + nex "github.com/PretendoNetwork/nex-go" +) + +func StartSecureServer() { + globals.SecureServer = nex.NewServer() + globals.SecureServer.SetPRUDPVersion(0) + globals.SecureServer.SetDefaultNEXVersion(nex.NewNEXVersion(2, 4, 3)) + + globals.SecureServer.SetAccessKey("6181dff1") + globals.SecureServer.SetKerberosPassword(globals.KerberosPassword) + + globals.SecureServer.On("Data", func(packet *nex.PacketV0) { + request := packet.RMCRequest() + + fmt.Println("=== MK7 - Secure ===") + fmt.Printf("Protocol ID: %#v\n", request.ProtocolID()) + fmt.Printf("Method ID: %#v\n", request.MethodID()) + fmt.Println("====================") + }) + + registerCommonSecureServerProtocols() + registerSecureServerNEXProtocols() + + globals.SecureServer.Listen(fmt.Sprintf(":%s", os.Getenv("PN_MK7_SECURE_SERVER_PORT"))) +} diff --git a/nex/server.go b/nex/server.go deleted file mode 100644 index 053affa..0000000 --- a/nex/server.go +++ /dev/null @@ -1,52 +0,0 @@ -package nex - -import ( - "fmt" - - "github.com/PretendoNetwork/mario-kart-7-secure/database" - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" -) - -func StartNEXServer() { - globals.NEXServer = nex.NewServer() - globals.NEXServer.SetPRUDPVersion(0) - globals.NEXServer.SetDefaultNEXVersion(&nex.NEXVersion{ - Major: 3, - Minor: 4, - Patch: 17, - }) - - // We don't know the extact Matchmaking version, we only know is below 3.0 - globals.NEXServer.SetMatchMakingProtocolVersion(&nex.NEXVersion{ - Major: 2, - Minor: 0, - Patch: 0, - }) - globals.NEXServer.SetKerberosKeySize(32) - globals.NEXServer.SetAccessKey(globals.Config.AccessKey) - globals.NEXServer.SetPingTimeout(999) - globals.NEXServer.SetKerberosPassword("test") - - globals.NEXServer.On("Data", func(packet *nex.PacketV0) { - request := packet.RMCRequest() - - fmt.Println("==MK7 - Secure==") - fmt.Printf("Protocol ID: %#v\n", request.ProtocolID()) - fmt.Printf("Method ID: %#v\n", request.MethodID()) - fmt.Printf("Method ID: %#v\n", globals.NEXServer.NEXVersion()) - fmt.Println("=================") - }) - - globals.NEXServer.On("Kick", func(packet *nex.PacketV0) { - pid := packet.Sender().PID() - database.RemovePlayer(pid) - - fmt.Println("Leaving") - }) - - registerCommonProtocols() - registerNEXProtocols() - - globals.NEXServer.Listen(":" + globals.Config.ServerPort) -} diff --git a/nex/storage-manager/acquire_card_id.go b/nex/storage-manager/acquire_card_id.go new file mode 100644 index 0000000..87c11b7 --- /dev/null +++ b/nex/storage-manager/acquire_card_id.go @@ -0,0 +1,44 @@ +package nex_storage_manager + +import ( + "math/rand" + + "github.com/PretendoNetwork/mario-kart-7/globals" + nex "github.com/PretendoNetwork/nex-go" + storage_manager "github.com/PretendoNetwork/nex-protocols-go/storage-manager" +) + +func AcquireCardID(err error, client *nex.Client, callID uint32) uint32 { + if err != nil { + globals.Logger.Error(err.Error()) + return nex.Errors.Core.Unknown + } + + cardID := rand.Uint64() + + rmcResponseStream := nex.NewStreamOut(globals.SecureServer) + + rmcResponseStream.WriteUInt64LE(cardID) + + rmcResponseBody := rmcResponseStream.Bytes() + + rmcResponse := nex.NewRMCResponse(storage_manager.ProtocolID, callID) + rmcResponse.SetSuccess(storage_manager.MethodAcquireCardID, rmcResponseBody) + + rmcResponseBytes := rmcResponse.Bytes() + + responsePacket, _ := nex.NewPacketV0(client, nil) + + responsePacket.SetVersion(0) + responsePacket.SetSource(0xA1) + responsePacket.SetDestination(0xAF) + responsePacket.SetType(nex.DataPacket) + responsePacket.SetPayload(rmcResponseBytes) + + responsePacket.AddFlag(nex.FlagNeedsAck) + responsePacket.AddFlag(nex.FlagReliable) + + globals.SecureServer.Send(responsePacket) + + return 0 +} diff --git a/nex/storage-manager/activate_with_card_id.go b/nex/storage-manager/activate_with_card_id.go new file mode 100644 index 0000000..34da8a6 --- /dev/null +++ b/nex/storage-manager/activate_with_card_id.go @@ -0,0 +1,67 @@ +package nex_storage_manager + +import ( + "database/sql" + + "github.com/PretendoNetwork/mario-kart-7/database" + "github.com/PretendoNetwork/mario-kart-7/globals" + nex "github.com/PretendoNetwork/nex-go" + storage_manager "github.com/PretendoNetwork/nex-protocols-go/storage-manager" +) + +func ActivateWithCardID(err error, client *nex.Client, callID uint32, unknown uint8, cardID uint64) uint32 { + if err != nil { + globals.Logger.Error(err.Error()) + return nex.Errors.Core.InvalidArgument + } + + // * It's not guaranteed that the client will call AcquireCardID, + // * because that method is only called the first time the client + // * goes online, and it may have used official servers previously. + // * + // * To workaround this, we ignore the card ID stuff and get the + // * unique ID using the PID + var firstTime bool + uniqueID, err := database.GetUniqueIDByOwnerPID(client.PID()) + if err != nil && err != sql.ErrNoRows { + globals.Logger.Critical(err.Error()) + return nex.Errors.Core.Unknown + } + + if err == sql.ErrNoRows { + uniqueID, err = database.InsertCommonDataByOwnerPID(client.PID()) + if err != nil { + globals.Logger.Critical(err.Error()) + return nex.Errors.Core.Unknown + } + + firstTime = true + } + + rmcResponseStream := nex.NewStreamOut(globals.SecureServer) + + rmcResponseStream.WriteUInt32LE(uniqueID) + rmcResponseStream.WriteBool(firstTime) + + rmcResponseBody := rmcResponseStream.Bytes() + + rmcResponse := nex.NewRMCResponse(storage_manager.ProtocolID, callID) + rmcResponse.SetSuccess(storage_manager.MethodActivateWithCardID, rmcResponseBody) + + rmcResponseBytes := rmcResponse.Bytes() + + responsePacket, _ := nex.NewPacketV0(client, nil) + + responsePacket.SetVersion(0) + responsePacket.SetSource(0xA1) + responsePacket.SetDestination(0xAF) + responsePacket.SetType(nex.DataPacket) + responsePacket.SetPayload(rmcResponseBytes) + + responsePacket.AddFlag(nex.FlagNeedsAck) + responsePacket.AddFlag(nex.FlagReliable) + + globals.SecureServer.Send(responsePacket) + + return 0 +} diff --git a/nex/utility/associate_nex_unique_id_with_my_principal_id.go b/nex/utility/associate_nex_unique_id_with_my_principal_id.go deleted file mode 100644 index 158e396..0000000 --- a/nex/utility/associate_nex_unique_id_with_my_principal_id.go +++ /dev/null @@ -1,27 +0,0 @@ -package nex_utility - -import ( - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - "github.com/PretendoNetwork/nex-protocols-go/utility" -) - -func AssociateNexUniqueIDsWithMyPrincipalID(err error, client *nex.Client, callID uint32, uniqueIDInfo []*utility.UniqueIDInfo) { - rmcResponse := nex.NewRMCResponse(utility.ProtocolID, callID) - rmcResponse.SetSuccess(utility.MethodAssociateNexUniqueIDsWithMyPrincipalID, nil) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/nex/utility/get_associated_nex_unique_id_with_my_principal_id.go b/nex/utility/get_associated_nex_unique_id_with_my_principal_id.go deleted file mode 100644 index a124c99..0000000 --- a/nex/utility/get_associated_nex_unique_id_with_my_principal_id.go +++ /dev/null @@ -1,36 +0,0 @@ -package nex_utility - -import ( - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - nex "github.com/PretendoNetwork/nex-go" - "github.com/PretendoNetwork/nex-protocols-go/utility" -) - -func GetAssociatedNexUniqueIDWithMyPrincipalID(err error, client *nex.Client, callID uint32) { - // TODO - This method has a different behavior on MK7 compared to the docs. - // Find out what are the request and response contents - rmcResponseStream := nex.NewStreamOut(globals.NEXServer) - - rmcResponseStream.WriteUInt64LE(0) - rmcResponseStream.WriteUInt64LE(0) - - rmcResponseBody := rmcResponseStream.Bytes() - - rmcResponse := nex.NewRMCResponse(utility.ProtocolID, callID) - rmcResponse.SetSuccess(utility.MethodGetAssociatedNexUniqueIDWithMyPrincipalID, rmcResponseBody) - - rmcResponseBytes := rmcResponse.Bytes() - - responsePacket, _ := nex.NewPacketV0(client, nil) - - responsePacket.SetVersion(0) - responsePacket.SetSource(0xA1) - responsePacket.SetDestination(0xAF) - responsePacket.SetType(nex.DataPacket) - responsePacket.SetPayload(rmcResponseBytes) - - responsePacket.AddFlag(nex.FlagNeedsAck) - responsePacket.AddFlag(nex.FlagReliable) - - globals.NEXServer.Send(responsePacket) -} diff --git a/types/config.go b/types/config.go deleted file mode 100644 index 2e46b5f..0000000 --- a/types/config.go +++ /dev/null @@ -1,28 +0,0 @@ -package types - -type ServerConfig struct { - ServerName string - ServerPort string - PrudpVersion int - SignatureVersion int - KerberosKeySize int - AccessKey string - NexVersion int - NEXDatabaseIP string - NEXDatabasePort string - AccountDatabaseIP string - AccountDatabasePort string - DatabaseUseAuth bool - DatabaseUsername string - DatabasePassword string - AccountDatabase string - PNIDCollection string - NexAccountsCollection string - SplatoonDatabase string - RoomsCollection string - SessionsCollection string - UsersCollection string - RegionsCollection string - CommunitiesCollection string - SubscriptionsCollection string -} diff --git a/utility/import_config_from_file.go b/utility/import_config_from_file.go deleted file mode 100644 index 7110315..0000000 --- a/utility/import_config_from_file.go +++ /dev/null @@ -1,117 +0,0 @@ -package utility - -import ( - "io/ioutil" - "strconv" - "strings" - - "github.com/PretendoNetwork/mario-kart-7-secure/globals" - "github.com/PretendoNetwork/mario-kart-7-secure/types" -) - -func ImportConfigFromFile(path string) (*types.ServerConfig, error) { - data, err := ioutil.ReadFile(path) - if err != nil { - return nil, err - } - indexes := strings.Split(string(data), "\n") - config := &types.ServerConfig{ - ServerName: "server", - KerberosKeySize: 32, - } - for i := 0; i < len(indexes); i++ { - index := strings.Split(indexes[i], "=") - if len(index) != 2 { - continue - } - switch index[0] { - case "ServerName": - globals.Config.ServerName = index[1] - break - case "ServerPort": - globals.Config.ServerPort = index[1] - break - case "PrudpVersion": - globals.Config.PrudpVersion, err = strconv.Atoi(index[1]) - if err != nil { - return nil, err - } - break - case "SignatureVersion": - globals.Config.SignatureVersion, err = strconv.Atoi(index[1]) - if err != nil { - return nil, err - } - break - case "KerberosKeySize": - globals.Config.KerberosKeySize, err = strconv.Atoi(index[1]) - if err != nil { - return nil, err - } - break - case "AccessKey": - globals.Config.AccessKey = index[1] - break - case "NexVersion": - globals.Config.NexVersion, err = strconv.Atoi(index[1]) - if err != nil { - return nil, err - } - break - case "AccountDatabaseIP": - globals.Config.AccountDatabaseIP = index[1] - break - case "AccountDatabasePort": - globals.Config.AccountDatabasePort = index[1] - break - case "NEXDatabaseIP": - globals.Config.NEXDatabaseIP = index[1] - break - case "NEXDatabasePort": - globals.Config.NEXDatabasePort = index[1] - break - case "DatabaseUseAuth": - globals.Config.DatabaseUseAuth = (index[1] == "true") - break - case "DatabaseUsername": - globals.Config.DatabaseUsername = index[1] - break - case "DatabasePassword": - globals.Config.DatabasePassword = index[1] - break - case "AccountDatabase": - globals.Config.AccountDatabase = index[1] - break - case "PNIDCollection": - globals.Config.PNIDCollection = index[1] - break - case "NexAccountsCollection": - globals.Config.NexAccountsCollection = index[1] - break - case "SplatoonDatabase": - globals.Config.SplatoonDatabase = index[1] - break - case "RoomsCollection": - globals.Config.RoomsCollection = index[1] - break - case "SessionsCollection": - globals.Config.SessionsCollection = index[1] - break - case "UsersCollection": - globals.Config.UsersCollection = index[1] - break - case "RegionsCollection": - globals.Config.RegionsCollection = index[1] - break - case "CommunitiesCollection": - globals.Config.CommunitiesCollection = index[1] - break - case "SubscriptionsCollection": - globals.Config.SubscriptionsCollection = index[1] - break - default: - break - } - } - return config, nil -}