From 1c8166959e344c4e7413c332cb643b015097a638 Mon Sep 17 00:00:00 2001 From: Sketch <75850871+SketchMaster2001@users.noreply.github.com> Date: Thu, 26 Oct 2023 11:52:06 -0400 Subject: [PATCH] Go-ify GPCM --- gpcm/login.go | 28 ++++++++++++++-------------- gpcm/main.go | 22 +++++++++++----------- gpcm/profile.go | 32 ++++++++++++++++---------------- 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/gpcm/login.go b/gpcm/login.go index f263036..7d2b27c 100644 --- a/gpcm/login.go +++ b/gpcm/login.go @@ -17,7 +17,7 @@ func generateResponse(gpcmChallenge, nasChallenge, authToken, clientChallenge st hasher := md5.New() hasher.Write([]byte(nasChallenge)) str := hex.EncodeToString(hasher.Sum(nil)) - str += " " + str += strings.Repeat(" ", 48) str += authToken str += clientChallenge str += gpcmChallenge @@ -32,8 +32,8 @@ func generateProof(gpcmChallenge, nasChallenge, authToken, clientChallenge strin return generateResponse(clientChallenge, nasChallenge, authToken, gpcmChallenge) } -func Login(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand, challenge string) (string, bool) { - if session.LoggedIn { +func (g *GameSpySession) Login(pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand, challenge string) (string, bool) { + if g.LoggedIn { log.Fatalf("Attempt to login twice") } @@ -54,27 +54,27 @@ func Login(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, com // TODO: Return an error log.Fatalf("GPCM login error") } - session.User = user + g.User = user // Check to see if a session is already open with this profile ID // TODO: Test this mutex.Lock() - _, exists := sessions[session.User.ProfileId] + _, exists := sessions[g.User.ProfileId] if exists { mutex.Unlock() // TODO: Return an error - log.Fatalf("Session with profile ID %u is already open", session.User.ProfileId) + log.Fatalf("Session with profile ID %d is already open", g.User.ProfileId) } - sessions[session.User.ProfileId] = session + sessions[g.User.ProfileId] = g mutex.Unlock() loginTicket := strings.Replace(base64.StdEncoding.EncodeToString([]byte(common.RandomString(16))), "=", "_", -1) // Now initiate the session - _ = database.CreateSession(pool, ctx, session.User.ProfileId, loginTicket) + _ = database.CreateSession(pool, ctx, g.User.ProfileId, loginTicket) - session.LoggedIn = true - session.ModuleName += ":" + strconv.FormatInt(int64(session.User.ProfileId), 10) - session.ModuleName += "/" + common.CalcFriendCodeString(session.User.ProfileId, "RMCJ") + g.LoggedIn = true + g.ModuleName += ":" + strconv.FormatInt(int64(g.User.ProfileId), 10) + g.ModuleName += "/" + common.CalcFriendCodeString(g.User.ProfileId, "RMCJ") return common.CreateGameSpyMessage(common.GameSpyCommand{ Command: "lc", @@ -82,9 +82,9 @@ func Login(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, com OtherValues: map[string]string{ "sesskey": "199714190", "proof": proof, - "userid": strconv.FormatInt(session.User.UserId, 10), - "profileid": strconv.FormatInt(int64(session.User.ProfileId), 10), - "uniquenick": session.User.UniqueNick, + "userid": strconv.FormatInt(g.User.UserId, 10), + "profileid": strconv.FormatInt(int64(g.User.ProfileId), 10), + "uniquenick": g.User.UniqueNick, "lt": loginTicket, "id": command.OtherValues["id"], }, diff --git a/gpcm/main.go b/gpcm/main.go index ec53e6e..457a236 100644 --- a/gpcm/main.go +++ b/gpcm/main.go @@ -70,13 +70,13 @@ func StartServer() { } } -func closeSession(session *GameSpySession) { +func (g *GameSpySession) closeSession() { mutex.Lock() defer mutex.Unlock() - session.Conn.Close() - if session.LoggedIn { - delete(sessions, session.User.ProfileId) + g.Conn.Close() + if g.LoggedIn { + delete(sessions, g.User.ProfileId) } } @@ -90,7 +90,7 @@ func handleRequest(conn net.Conn) { Status: "", } - defer closeSession(&session) + defer session.closeSession() // Set session ID and challenge challenge := common.RandomString(10) @@ -140,7 +140,7 @@ func handleRequest(conn net.Conn) { return } - payload, _ := Login(&session, pool, ctx, command, challenge) + payload, _ := session.Login(pool, ctx, command, challenge) conn.Write([]byte(payload)) } } @@ -157,19 +157,19 @@ func handleRequest(conn net.Conn) { return case "updatepro": - updateProfile(&session, pool, ctx, command) + session.updateProfile(pool, ctx, command) break case "status": - setStatus(&session, pool, ctx, command) + session.setStatus(pool, ctx, command) break case "addbuddy": - addFriend(&session, pool, ctx, command) + session.addFriend(pool, ctx, command) break case "delbuddy": - removeFriend(&session, pool, ctx, command) + session.removeFriend(pool, ctx, command) break } } @@ -181,7 +181,7 @@ func handleRequest(conn net.Conn) { break case "getprofile": - payload := getProfile(&session, pool, ctx, command) + payload := session.getProfile(pool, ctx, command) conn.Write([]byte(payload)) break } diff --git a/gpcm/profile.go b/gpcm/profile.go index 319617f..0387300 100644 --- a/gpcm/profile.go +++ b/gpcm/profile.go @@ -11,7 +11,7 @@ import ( "wwfc/logging" ) -func getProfile(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) string { +func (g *GameSpySession) getProfile(pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) string { strProfileId := command.OtherValues["profileid"] profileId, err := strconv.ParseInt(strProfileId, 10, 32) if err != nil { @@ -45,7 +45,7 @@ func getProfile(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context }) } -func updateProfile(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) { +func (g *GameSpySession) updateProfile(pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) { var firstName string var lastName string if v, ok := command.OtherValues["firstname"]; ok { @@ -56,39 +56,39 @@ func updateProfile(session *GameSpySession, pool *pgxpool.Pool, ctx context.Cont lastName = v } - database.UpdateUser(pool, ctx, firstName, lastName, session.User.UserId) + database.UpdateUser(pool, ctx, firstName, lastName, g.User.UserId) } -func setStatus(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) { +func (g *GameSpySession) setStatus(pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) { status := command.CommandValue statstring, ok := command.OtherValues["statstring"] if !ok { - logging.Notice(session.ModuleName, "Missing statstring") + logging.Notice(g.ModuleName, "Missing statstring") statstring = "" } else { if statstring == "" { - logging.Notice(session.ModuleName, "statstring: (empty)") + logging.Notice(g.ModuleName, "statstring: (empty)") } else { - logging.Notice(session.ModuleName, "statstring:", aurora.Cyan(statstring)) + logging.Notice(g.ModuleName, "statstring:", aurora.Cyan(statstring)) } } locstring, ok := command.OtherValues["locstring"] if !ok { - logging.Notice(session.ModuleName, "Missing locstring") + logging.Notice(g.ModuleName, "Missing locstring") locstring = "" } else { if locstring == "" { - logging.Notice(session.ModuleName, "locstring: (empty)") + logging.Notice(g.ModuleName, "locstring: (empty)") } else { - logging.Notice(session.ModuleName, "locstring:", aurora.Cyan(locstring)) + logging.Notice(g.ModuleName, "locstring:", aurora.Cyan(locstring)) } } // Get the IP address for the status msg var rawIP int - for i, s := range strings.Split(strings.Split(session.Conn.RemoteAddr().String(), ":")[0], ".") { + for i, s := range strings.Split(strings.Split(g.Conn.RemoteAddr().String(), ":")[0], ".") { val, err := strconv.Atoi(s) if err != nil { panic(err) @@ -104,17 +104,17 @@ func setStatus(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, Command: "bm", CommandValue: "100", OtherValues: map[string]string{ - "f": strconv.FormatUint(uint64(session.User.ProfileId), 10), + "f": strconv.FormatUint(uint64(g.User.ProfileId), 10), "msg": "|s|" + status + "|ss|" + statstring + "|ls|" + locstring + "|ip|" + ip + "|p|0|qm|0", }, }) mutex.Lock() - session.Status = friendStatus + g.Status = friendStatus mutex.Unlock() } -func addFriend(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) { +func (g *GameSpySession) addFriend(pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) { strProfileId := command.OtherValues["newprofileid"] profileId, err := strconv.ParseUint(strProfileId, 10, 32) if err != nil { @@ -122,10 +122,10 @@ func addFriend(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, } fc := common.CalcFriendCodeString(uint32(profileId), "RMCJ") - logging.Notice(session.ModuleName, "Add friend:", aurora.Cyan(strProfileId), aurora.Cyan(fc)) + logging.Notice(g.ModuleName, "Add friend:", aurora.Cyan(strProfileId), aurora.Cyan(fc)) // TODO } -func removeFriend(session *GameSpySession, pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) { +func (g *GameSpySession) removeFriend(pool *pgxpool.Pool, ctx context.Context, command common.GameSpyCommand) { // TODO }