public override async Task <bool> OnPreRequest() { //Authenticate session session = await Program.conn.AuthenticateServerSessionTokenAsync(e.Request.Query["session_token"]); //Check if auth failed if (session == null) { await WriteString("Server authentication failed!", "text/plain", 401); return(false); } //Get server server = await Program.conn.GetServerByIdAsync(session.server_id); if (server == null) { await WriteString("Server authentication failed!", "text/plain", 401); return(false); } return(true); }
public static async Task OnHttpRequest(Microsoft.AspNetCore.Http.HttpContext e) { //Authenticate DbServer server = await Program.ForceAuthServer(e); if (server == null) { return; } //Authenticate state DbSyncSavedState state = await Program.ForceAuthSessionState(e); if (state == null) { return; } //Decode Dictionary <string, TimeAnalyticsRequestItem> request = Program.DecodeStreamAsJson <Dictionary <string, TimeAnalyticsRequestItem> >(e.Request.Body); //Add each analytics option List <WriteModel <DbModTimeAnalyticsObject> > actions = new List <WriteModel <DbModTimeAnalyticsObject> >(); foreach (var r in request.Values) { //Create DbModTimeAnalyticsObject o = new DbModTimeAnalyticsObject { action = r.action, client_session = state._id, client_version = state.mod_version, key = r.key, server_id = server._id, time = new DateTime(1970, 1, 1, 0, 0, 0).AddSeconds(r.time), payload = new DbModTimeAnalyticsObject.DbModTimeAnalyticsObject_Payload { duration = r.durationms, extras = r.extras, length = r.resultlength } }; //Add actions.Add(new InsertOneModel <DbModTimeAnalyticsObject>(o)); } //Apply if (actions.Count > 0) { await Program.conn.system_analytics_time.BulkWriteAsync(actions); } }
public override async Task OnRequest() { //Check to see if this is a valid ARK server if (!e.Request.Query.ContainsKey("client_version")) { e.Response.StatusCode = 400; await Program.WriteStringToStream(e.Response.Body, "Required information was not sent, this is likely not a valid ARK server.\r\n\r\nIt's likely that you're looking into how things here work. More information: https://github.com/deltawebmap/Docs/blob/master/basics.md \r\n\r\n(C) DeltaWebMap 2020, RomanPort 2020"); return; } //Get version int clientVersion = int.Parse(e.Request.Query["client_version"]); //Read the request data var request = await ReadPOSTContentChecked <RequestPayload>(); if (request == null) { return; } //Attempt to authenticate. It's OK of this fails. DbServer server = await Program.conn.AuthenticateServerTokenAsync(request.token); ObjectId stateId = ObjectId.GenerateNewId(); if (server == null) { //Create server = await CreateNewServer(request, clientVersion, stateId); } else { //Update info await server.GetUpdateBuilder(conn) .UpdateLastSyncState(stateId) .UpdateLastSyncConnectedTime(DateTime.UtcNow) .UpdateLastSyncPingedTime(DateTime.UtcNow) .UpdateLastSyncClientVersion(clientVersion) .Apply(); } //Generate a state token string stateToken = SecureStringTool.GenerateSecureString(56); while (!await SecureStringTool.CheckStringUniquenessAsync <DbSyncSavedState>(stateToken, Program.conn.system_sync_states)) { stateToken = SecureStringTool.GenerateSecureString(56); } //Create a state DbSyncSavedState state = new DbSyncSavedState { mod_version = clientVersion, server_id = server._id, system_version = Program.VERSION_MAJOR, time = DateTime.UtcNow, token = stateToken, mod_enviornment = e.Request.Query["client_env"], _id = stateId }; await Program.conn.system_sync_states.InsertOneAsync(state); //Create a response ResponsePayload response = new ResponsePayload { refresh_token = server.token, session_token = state.token, is_claimed = false, server_id = server.id, content_server_host = server.game_content_server_hostname }; //Write response await Program.WriteStringToStream(e.Response.Body, "D002" + Newtonsoft.Json.JsonConvert.SerializeObject(response)); }