Skip to content

Commit

Permalink
check invite collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePinkUnicorn6 committed Dec 5, 2023
1 parent 094ba1b commit c12ee53
Showing 1 changed file with 54 additions and 35 deletions.
89 changes: 54 additions & 35 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
using System;
using System.IO;
using System.Net;
using System.Web;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using System.Text.Json.Serialization;
using System.Data.SQLite;
using System.Security.Cryptography;
using System.Collections;
using System.ComponentModel.Design;
using System.Data.Entity.Infrastructure.Design;
using System.Buffers;
using System.Net.Cache;
using System.Text.Json.Nodes;

class MessageServer
{
Expand Down Expand Up @@ -65,6 +54,7 @@ static void Main(string[] args)
case "/api/guild/createInvite": apiCreateInvite(context); break; //Get
case "/api/guild/listInvites": apiListInvites(context); break; //Get
case "/api/guild/join": apiJoinGuildFromCode(context); break; //Post
case "/api/guild/requestKeys": apiRequestKeys(context); break; //Get
case "/api/account/create": apiCreateUser(context); break; //Post
case "/api/account/login": apiLogin(context); break; //Post
case "/api/account/userID": apiReturnUserIDFromToken(context); break; //Get
Expand Down Expand Up @@ -175,23 +165,27 @@ FOREIGN KEY('GuildID') REFERENCES 'tblGuilds'('GuildID')
}
static void sendResponse(HttpListenerContext context, string type, int code, string? responseMessage = null) // Sends data in response to a call from a client
{
if (string.IsNullOrEmpty(responseMessage))
try
{
context.Response.Headers.Clear();
context.Response.StatusCode = code;
context.Response.Close();
}
else
{
byte[] responseBytes = Encoding.UTF8.GetBytes(responseMessage);
context.Response.ContentLength64 = responseBytes.Length;
context.Response.ContentType = type;
context.Response.StatusCode = code;
Stream outputStream = context.Response.OutputStream;
outputStream.Write(responseBytes, 0, responseBytes.Length);
outputStream.Close();
if (string.IsNullOrEmpty(responseMessage))
{
context.Response.Headers.Clear();
context.Response.StatusCode = code;
context.Response.Close();
}
else
{
byte[] responseBytes = Encoding.UTF8.GetBytes(responseMessage);
context.Response.ContentLength64 = responseBytes.Length;
context.Response.ContentType = type;
context.Response.StatusCode = code;
Stream outputStream = context.Response.OutputStream;
outputStream.Write(responseBytes, 0, responseBytes.Length);
outputStream.Close();
}
Console.WriteLine("Sent response: " + responseMessage);
}
Console.WriteLine("Sent response: " + responseMessage);
catch(Exception ex) { log("ERROR", "Failed to send response", ex); }
}
static void log(string type, string desc, Exception ex = null)

Check warning on line 190 in Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
Expand All @@ -205,33 +199,38 @@ static void log(string type, string desc, Exception ex = null)
case "ERROR":
{
Console.WriteLine("[!ERROR] " + ex.GetType() + " exeption, see logs for details");
File.AppendAllText(logPath, time + " [!ERROR] " + desc + ex.ToString() + "\n\n");
File.AppendAllText(logPath, time + " [!ERROR] " + desc + ex.ToString() + "\n\n");
}break;
case "WARNING":
{
Console.WriteLine("[WARNING] " + desc);
File.AppendAllText(logPath, time + " [WARNING] " + desc + ex.ToString() + "\n\n");
}break;
case "INFO":
{
File.AppendAllText(logPath, time + " [INFO] " + desc + "\n");
File.AppendAllText(logPath, time + " [INFO] " + desc + "\n");
}break;
case "DEBUG":
{
File.AppendAllText(logPath, time + " [DEBUG] " + desc + "\n");
File.AppendAllText(logPath, time + " [DEBUG] " + desc + "\n");
}break;
}
}
static dynamic parsePost(HttpListenerContext context)
{
string jsonBody;
dynamic jsonBodyObject;
var request = context.Request;
if (request.HttpMethod == "POST" && request.ContentType != null && request.ContentType.Contains(typeJson))
{
using (var body = request.InputStream)
using (var reader = new StreamReader(body, request.ContentEncoding))
{
jsonBody = reader.ReadToEnd();

}
jsonBodyObject = JsonConvert.DeserializeObject<dynamic>(jsonBody);
}
else { jsonBody = null; }
dynamic jsonBodyObject = JsonConvert.DeserializeObject<dynamic>(jsonBody);
else { jsonBodyObject = null; }
return jsonBodyObject;
}
static void apiGetMessages(HttpListenerContext context) // Fetches message data from db if user has permission, and returns it as a json array.
Expand Down Expand Up @@ -577,7 +576,7 @@ FROM tblUsers
}
static void apiCreateUser(HttpListenerContext context)
{
//Checks if user exists before adding them to the database. Will respond with an error if the user allready exists.
// Checks if user exists before adding them to the database. Will respond with an error if the user allready exists.
string responseMessage;
int code;
string? userName;
Expand All @@ -597,7 +596,7 @@ static void apiCreateUser(HttpListenerContext context)
publicKey = jsonBodyObject.publicKey;
passHash = jsonBodyObject.passHash;
}
if (string.IsNullOrEmpty(userName) | string.IsNullOrEmpty(publicKey) | string.IsNullOrEmpty(passHash))
if (string.IsNullOrEmpty(userName) /*| string.IsNullOrEmpty(publicKey)*/ | string.IsNullOrEmpty(passHash))
{
var responseJson = new { error = "Missing a required parameter", errcode = "MISSING_PARAMETER"};
responseMessage = JsonConvert.SerializeObject(responseJson);
Expand Down Expand Up @@ -1090,6 +1089,22 @@ static void apiCreateInvite(HttpListenerContext context)
using (var cmd = new SQLiteCommand(con))
{
con.Open();
bool collision = false;
do
{
cmd.CommandText = @"SELECT EXISTS(
SELECT 1
FROM tblInvites
WHERE Code = @Code
)";
cmd.Parameters.AddWithValue("Code", inviteCode);
collision = (Int64)cmd.ExecuteScalar() > 0;
if (collision = true)
{
log("WARNING", "Duplicate invite generated");
}
} while (collision = true); // If an invite that allready exists is generated create a warning and regenerate it.

cmd.CommandText = @"INSERT INTO tblInvites (Code, GuildID)
VALUES (@Code, @GuildID);";
cmd.Parameters.AddWithValue("Code", inviteCode);
Expand Down Expand Up @@ -1223,6 +1238,10 @@ FROM tblInvites
}
}
sendResponse(context, typeJson, code, responseMessage);
}
static void apiRequestKeys(HttpListenerContext context)
{

}
static string createToken(string userID)// Generates a token that the client can then use to authenticate with
{
Expand Down

0 comments on commit c12ee53

Please sign in to comment.