Skip to content

Commit

Permalink
transition to MWS
Browse files Browse the repository at this point in the history
  • Loading branch information
SlejmUr committed Oct 13, 2023
1 parent e685954 commit 751aa1d
Show file tree
Hide file tree
Showing 16 changed files with 269 additions and 114 deletions.
1 change: 0 additions & 1 deletion DS_Server/MiddleMan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ public override void OnWsConnecting(HttpRequest request)
request.SetHeader("Upgrade", "websocket");
request.SetHeader("Connection", "Upgrade");
request.SetHeader("Sec-WebSocket-Key", Convert.ToBase64String(WsNonce));
request.SetHeader("Sec-WebSocket-Protocol", "chat, superchat");
request.SetHeader("Sec-WebSocket-Version", "13");
request.SetHeader("MiddleMan", Id.ToString());
request.SetBody();
Expand Down
14 changes: 0 additions & 14 deletions PayCheckServerLib/HTTPAttribute.cs

This file was deleted.

3 changes: 2 additions & 1 deletion PayCheckServerLib/PayCheckServerLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="JWT" Version="10.0.3" />
<PackageReference Include="JWT" Version="10.1.1" />
<PackageReference Include="L" Version="5.0.0" />
<PackageReference Include="ModdableWebServer" Version="1.0.0" />
<PackageReference Include="NetCoreServer" Version="7.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
Expand Down
38 changes: 27 additions & 11 deletions PayCheckServerLib/Responses/BasicResponse.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using NetCoreServer;
using ModdableWebServer;
using ModdableWebServer.Attributes;
using ModdableWebServer.Helper;
using NetCoreServer;
using Newtonsoft.Json;
using PayCheckServerLib.Helpers;
using PayCheckServerLib.Jsons;
Expand All @@ -7,8 +10,17 @@ namespace PayCheckServerLib.Responses
{
public class BasicResponse
{
[HTTP("HEAD", "/generate_204")]
public static bool Generate204(HttpRequest request, ServerStruct serverStruct)
{
serverStruct.Response.MakeOkResponse(204);
ResponseSender.SendResponse(serverStruct);
return true;
}


[HTTP("GET", "/qosm/public/qos")]
public static bool QOSM_Public_QOS(HttpRequest request, PC3Server.PC3Session session)
public static bool QOSM_Public_QOS(HttpRequest request, ServerStruct serverStruct)
{
ResponseCreator response = new ResponseCreator();
response.SetHeader("Content-Type", "application/json");
Expand All @@ -31,16 +43,17 @@ public static bool QOSM_Public_QOS(HttpRequest request, PC3Server.PC3Session ses
}

response.SetBody(JsonConvert.SerializeObject(rsp, Formatting.Indented));
session.SendResponse(response.GetResponse());
serverStruct.Response = response.GetResponse();
ResponseSender.SendResponse(serverStruct);
return true;
}

[HTTP("GET", "/basic/v1/public/misc/time")]
public static bool Time(HttpRequest request, PC3Server.PC3Session session)
public static bool Time(HttpRequest request, ServerStruct serverStruct)
{
if (session.Headers.ContainsKey("cookie"))
if (serverStruct.Headers.ContainsKey("cookie"))
{
var tokens = TokenHelper.ReadFromHeader(session.Headers);
var tokens = TokenHelper.ReadFromHeader(serverStruct.Headers);
Debugger.PrintDebug($"{tokens.AccessToken.UserId}({tokens.AccessToken.Name}) Is still in the server!");
}

Expand All @@ -52,29 +65,32 @@ public static bool Time(HttpRequest request, PC3Server.PC3Session session)
CurrentTime = DateTime.UtcNow.ToString("o")
};
response.SetBody(JsonConvert.SerializeObject(rsp));
session.SendResponse(response.GetResponse());
serverStruct.Response = response.GetResponse();
ResponseSender.SendResponse(serverStruct);
return true;
}



[HTTP("GET", "/lobby/v1/messages")]
public static bool LobbyMessages(HttpRequest request, PC3Server.PC3Session session)
public static bool LobbyMessages(HttpRequest request, ServerStruct serverStruct)
{
ResponseCreator response = new ResponseCreator();
response.SetHeader("Content-Type", "application/json");
response.SetBody(File.ReadAllBytes("Files/messages.json"));
session.SendResponse(response.GetResponse());
serverStruct.Response = response.GetResponse();
ResponseSender.SendResponse(serverStruct);
return true;
}

[HTTP("GET", "/iam/v3/location/country")]
public static bool Country(HttpRequest request, PC3Server.PC3Session session)
public static bool Country(HttpRequest request, ServerStruct serverStruct)
{
ResponseCreator response = new ResponseCreator();
response.SetHeader("Content-Type", "application/json");
response.SetBody(File.ReadAllBytes("Files/Country.json"));
session.SendResponse(response.GetResponse());
serverStruct.Response = response.GetResponse();
ResponseSender.SendResponse(serverStruct);
return true;
}
}
Expand Down
3 changes: 3 additions & 0 deletions PayCheckServerLib/Responses/CloudSave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using PayCheckServerLib.Helpers;
using PayCheckServerLib.Jsons;
using PayCheckServerLib.Jsons.Basic;
using ModdableWebServer;
using ModdableWebServer.Attributes;
using ModdableWebServer.Helper;

namespace PayCheckServerLib.Responses
{
Expand Down
3 changes: 3 additions & 0 deletions PayCheckServerLib/Responses/GameSessions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using PayCheckServerLib.Helpers;
using PayCheckServerLib.Jsons.GS;
using PayCheckServerLib.WSController;
using ModdableWebServer;
using ModdableWebServer.Attributes;
using ModdableWebServer.Helper;

namespace PayCheckServerLib.Responses
{
Expand Down
3 changes: 3 additions & 0 deletions PayCheckServerLib/Responses/IAM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
using PayCheckServerLib.Jsons;
using PayCheckServerLib.Jsons.Basic;
using System.Web;
using ModdableWebServer;
using ModdableWebServer.Attributes;
using ModdableWebServer.Helper;

namespace PayCheckServerLib.Responses
{
Expand Down
3 changes: 3 additions & 0 deletions PayCheckServerLib/Responses/MatchTickets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using Newtonsoft.Json;
using PayCheckServerLib.Helpers;
using PayCheckServerLib.WSController;
using ModdableWebServer;
using ModdableWebServer.Attributes;
using ModdableWebServer.Helper;

namespace PayCheckServerLib.Responses
{
Expand Down
7 changes: 5 additions & 2 deletions PayCheckServerLib/Responses/Parties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
using PayCheckServerLib.Jsons.PartyStuff;
using PayCheckServerLib.Jsons.WSS;
using PayCheckServerLib.WSController;
using ModdableWebServer;
using ModdableWebServer.Attributes;
using ModdableWebServer.Helper;

namespace PayCheckServerLib.Responses
{
Expand Down Expand Up @@ -54,7 +57,7 @@ public static bool Party(HttpRequest request, PC3Server.PC3Session session)
}
};

ChatControl.SendToChat(JsonConvert.SerializeObject(topic), session.GetWSChat(token.UserId, token.Namespace));
ChatControl.SendToChat(JsonConvert.SerializeObject(topic), ChatControl.GetChatUser(token.UserId, token.Namespace));

return true;
}
Expand Down Expand Up @@ -162,7 +165,7 @@ public static bool LeaveParties(HttpRequest request, PC3Server.PC3Session sessio
UserId = token.UserId
}
};
ChatControl.SendToChat(JsonConvert.SerializeObject(topic), session.GetWSChat(token.UserId, token.Namespace));
ChatControl.SendToChat(JsonConvert.SerializeObject(topic), ChatControl.GetChatUser(token.UserId, token.Namespace));
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions PayCheckServerLib/Responses/Social.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using PayCheckServerLib.Helpers;
using PayCheckServerLib.Jsons;
using PayCheckServerLib.Jsons.Basic;
using ModdableWebServer;
using ModdableWebServer.Attributes;
using ModdableWebServer.Helper;

namespace PayCheckServerLib.Responses
{
Expand Down
69 changes: 61 additions & 8 deletions PayCheckServerLib/ServerManager.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using PayCheckServerLib.Helpers;
using ModdableWebServer.Helper;
using ModdableWebServer.Servers;
using NetCoreServer;
using PayCheckServerLib.Helpers;
using System.Diagnostics;
using System.Reflection;

namespace PayCheckServerLib
{
public class ServerManager
{
static GSTATICServer.GSServer STATICServer;
/// <summary>
/// Use this to check if update finished (Either Cancelled or Success)
/// </summary>
Expand All @@ -26,24 +30,73 @@ public static void Pre()
UpdateFinished?.Invoke(null, true);

}

static WSS_Server? server = null;
static HTTP_Server? gserver = null;
public static void Start()
{
if (ConfigHelper.ServerConfig.Hosting.WSS)
PC3Server.Start(ConfigHelper.ServerConfig.Hosting.IP, 443);
{
var context = CertHelper.GetContext( System.Security.Authentication.SslProtocols.Tls12 , "cert.pfx", "cert");
server = new(context, ConfigHelper.ServerConfig.Hosting.IP, 443);
server.ReceivedFailed += ReceivedFailed;
server.Started += Server_Started;
server.Stopped += Server_Stopped;
server.HTTP_AttributeToMethods.Merge(Assembly.GetExecutingAssembly());
server.WS_AttributeToMethods.Merge(Assembly.GetExecutingAssembly());
server.WSError += WSError;
server.Start();
}
if (ConfigHelper.ServerConfig.Hosting.Gstatic)
{
STATICServer = new GSTATICServer.GSServer(ConfigHelper.ServerConfig.Hosting.IP, 80);
STATICServer.Start();
gserver = new(ConfigHelper.ServerConfig.Hosting.IP, 80);
gserver.Start();
}
}

public static void Stop()
{
if (ConfigHelper.ServerConfig.Hosting.WSS)
PC3Server.Stop();
server?.Stop();
if (ConfigHelper.ServerConfig.Hosting.Gstatic)
STATICServer.Stop();
gserver?.Stop();
server = null;
gserver = null;
}

#region Actions
private static void Server_Stopped(object? sender, EventArgs e)
{
Console.WriteLine("HTTPS Server stopped!");
}

private static void Server_Started(object? sender, (string address, int port) e)
{
Console.WriteLine("HTTPS Server started!");
}

private static void ReceivedFailed(object? sender, HttpRequest request)
{
File.AppendAllText("REQUESTED.txt", request.Url + "\n" + request.Method + "\n" + request.Body + "\n");
Debugger.logger.Debug(request.Url + "\n" + request);
Console.WriteLine("something isnt good");
}
private static void WSError(object? sender, string error)
{
Console.WriteLine("WSError! " + error);
Debugger.PrintDebug($"Server reported error: {error}");
StackTrace st = new StackTrace(true);
for (int i = 0; i < st.FrameCount; i++)
{
var sf = st.GetFrame(i);
if (sf == null)
continue;
Debugger.PrintDebug("");
Debugger.PrintDebug($"Method: " + sf.GetMethod());
Debugger.PrintDebug($"File: " + sf.GetFileName());
Debugger.PrintDebug($"Line Number: " + sf.GetFileLineNumber());
Debugger.PrintDebug("");
}
}
#endregion
}
}
41 changes: 0 additions & 41 deletions PayCheckServerLib/Servers/GSTATICServer.cs

This file was deleted.

9 changes: 5 additions & 4 deletions PayCheckServerLib/Servers/PC3Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public static void Start(string IP, int Port)
{
HttpServerThingy.Clear();
var context = new SslContext(SslProtocols.Tls12, GetCert());
/*
server = new PC3WSSServer(context, IP, Port);
Console.WriteLine("[HTTPS] Server Started on https://" + IP + ":" + Port);
server.Start();
Expand All @@ -40,7 +41,7 @@ public static void Start(string IP, int Port)
continue;
Debugger.PrintDebug(method.Name + $" ({httpAttr.url}) ({httpAttr.method}) is added as an URL", "HTTPServer");
HttpServerThingy.Add((httpAttr.url, httpAttr.method), method);
}
}*/
}

public static void Stop()
Expand Down Expand Up @@ -228,13 +229,13 @@ public override void OnWsReceived(byte[] buffer, long offset, long size)
switch (WS_ID)
{
case WSEnum.Lobby:
LobbyControl.Control(buffer, offset, size, this);
//LobbyControl.Control(buffer, offset, size, this);
return;
case WSEnum.Chat:
ChatControl.Control(buffer, offset, size, this);
//ChatControl.Control(buffer, offset, size, this);
return;
case WSEnum.MiddleMan:
MiddleManControl.Control(buffer, offset, size, this);
//MiddleManControl.Control(buffer, offset, size, this);
return;
case WSEnum.IDK:
default:
Expand Down
Loading

0 comments on commit 751aa1d

Please sign in to comment.