From ef521aea15f83e1e65346b73c1d1f2eae191b21b Mon Sep 17 00:00:00 2001 From: Ash Logan Date: Fri, 6 Dec 2024 16:02:56 +1100 Subject: [PATCH] feat: Save pia connection reports to database I would have liked to save the gathering info too but I couldn't work out how to extract that from MatchmakingManager you can kinda reverse-engineer it from the tracking logs anyway --- init.go | 10 ++++++++++ ...register_common_secure_server_protocols.go | 19 ++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/init.go b/init.go index 1361ac5..845ac14 100644 --- a/init.go +++ b/init.go @@ -110,4 +110,14 @@ func init() { globals.Logger.Critical(err.Error()) } globals.Logger.Success("Connected to Postgres!") + + _, err = globals.Postgres.Exec(`CREATE TABLE IF NOT EXISTS reports ( + id bigserial PRIMARY KEY, + ts timestamp, + report_id integer, + report bytea + )`) + if err != nil { + globals.Logger.Error(err.Error()) + } } diff --git a/nex/register_common_secure_server_protocols.go b/nex/register_common_secure_server_protocols.go index 9c29c49..f3b71f9 100644 --- a/nex/register_common_secure_server_protocols.go +++ b/nex/register_common_secure_server_protocols.go @@ -19,8 +19,21 @@ import ( "github.com/PretendoNetwork/splatoon/globals" ) -func CreateReportDBRecord(_ *types.PID, _ *types.PrimitiveU32, _ *types.QBuffer) error { - return nil +// that's right boys it's a FACTORY METHOD IN GO LET'S GOOOO +func fCreateReportDBRecord() func(*types.PID, *types.PrimitiveU32, *types.QBuffer) error { + createReportStmt, err := globals.Postgres.Prepare(`INSERT INTO reports (ts, report_id, report) VALUES (NOW(), $1, $2)`) + if err != nil { + globals.Logger.Error(err.Error()) + } + + return func(pid *types.PID, id *types.PrimitiveU32, report *types.QBuffer) error { + if createReportStmt == nil { + return nil // Just drop the report in case of DB error - no need to freak out the client with a reported err + } + + _, err := createReportStmt.Exec(id.Value, report.Value) + return err // consider not reporting this error to the client + } } func stubGetPlayingSession(err error, packet nex.PacketInterface, callID uint32, _ *types.List[*types.PID]) (*nex.RMCMessage, *nex.Error) { @@ -70,7 +83,7 @@ func registerCommonSecureServerProtocols() { globals.SecureEndpoint.RegisterServiceProtocol(secureProtocol) commonSecureProtocol := commonsecure.NewCommonProtocol(secureProtocol) - commonSecureProtocol.CreateReportDBRecord = CreateReportDBRecord + commonSecureProtocol.CreateReportDBRecord = fCreateReportDBRecord() natTraversalProtocol := nattraversal.NewProtocol() globals.SecureEndpoint.RegisterServiceProtocol(natTraversalProtocol)