Pārlūkot izejas kodu

Deleted InABox.Client.WebSocket and finished pulling out the WebSocket stuff serverside (i.e., deleting the commented out stuff)

Kenric Nugteren 2 gadi atpakaļ
vecāks
revīzija
f1b71ad21f

+ 0 - 1
InABox.Server/InABox.Server.csproj

@@ -15,7 +15,6 @@
     </ItemGroup>
 
     <ItemGroup>
-        <ProjectReference Include="..\..\3rdpartylibs\websocket-sharp-master\websocket-sharp-standard\websocket-sharp-standard.csproj" />
         <ProjectReference Include="..\InABox.Configuration\InABox.Configuration.csproj" />
         <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
         <ProjectReference Include="..\InABox.Database\InABox.Database.csproj" />

+ 1 - 26
InABox.Server/Rest/RestHandler.cs

@@ -16,12 +16,8 @@ namespace InABox.API
         private readonly List<string> endpoints;
         private readonly List<string> operations;
 
-        //private int? WebSocketPort;
-
-        public RestHandler(IHandler parent) //, int? webSocketPort)
+        public RestHandler(IHandler parent)
         {
-            // WebSocketPort = webSocketPort;
-
             Parent = parent;
 
             endpoints = new();
@@ -116,7 +112,6 @@ namespace InABox.API
                             {
                                 "validate" => new ValueTask<IResponse?>(Validate(request, data).Build()),
                                 "check_2fa" => new ValueTask<IResponse?>(Check2FA(request, data).Build()),
-                                //"notify" or "push" => new ValueTask<IResponse?>(GetPush(request, data).Build()),
                                 _ => HandleDatabaseRequest(request, data),
                             };
                         }
@@ -170,26 +165,6 @@ namespace InABox.API
             return SerializeResponse(request, data.ResponseFormat, data.BinarySerializationSettings, response);
         }
 
-        /// <summary>
-        /// Gets port for web socket
-        /// </summary>
-        /// <param name="request"></param>
-        /// <returns></returns>
-        // private IResponseBuilder GetPush(IRequest request, RequestData data)
-        // {
-        //     var requestObj = Deserialize<PushRequest>(request.Content, data.RequestFormat, data.BinarySerializationSettings, true);
-        //     if (!CredentialsCache.SessionExists(requestObj.Credentials.Session))
-        //     {
-        //         return request.Respond().Status(ResponseStatus.NotFound);
-        //     }
-        //     var response = new PushResponse
-        //     {
-        //         Status = StatusCode.OK,
-        //         SocketPort = WebSocketPort
-        //     };
-        //     return SerializeResponse(request, data.ResponseFormat, data.BinarySerializationSettings, response);
-        // }
-
         #region Authentication
 
         private IResponseBuilder Validate(IRequest request, RequestData data)

+ 1 - 8
InABox.Server/Rest/RestHandlerBuilder.cs

@@ -6,13 +6,6 @@ namespace InABox.API
     {
         private readonly List<IConcernBuilder> _Concerns = new();
 
-        //private int? WebSocketPort;
-
-        // public RestHandlerBuilder(int? webSocketPort)
-        // {
-        //     WebSocketPort = webSocketPort;
-        // }
-
         public RestHandlerBuilder Add(IConcernBuilder concern)
         {
             _Concerns.Add(concern);
@@ -21,7 +14,7 @@ namespace InABox.API
 
         public IHandler Build(IHandler parent)
         {
-            return Concerns.Chain(parent, _Concerns, p => new RestHandler(p)); //, WebSocketPort));
+            return Concerns.Chain(parent, _Concerns, p => new RestHandler(p));
         }
     }
 }

+ 0 - 33
InABox.Server/Rest/RestListener.cs

@@ -9,20 +9,17 @@ namespace InABox.API
     public static class RestListener
     {
         private static IServerHost? _host;
-        //private static RestPusher? _pusher;
 
         public static X509Certificate2? Certificate { get; private set; }
 
         public static void Start()
         {
             _host?.Start();
-            //_pusher?.Start();
         }
 
         public static void Stop()
         {
             _host?.Stop();
-            //_pusher?.Stop();
         }
 
         public static void Init(ushort port, X509Certificate2? cert)
@@ -38,35 +35,5 @@ namespace InABox.API
             else
                 _host?.Bind(IPAddress.Any, port);
         }
-        
-        // /// <summary>
-        // /// Clears certificate and host information, and stops the listener.
-        // /// </summary>
-        // public static void Clear()
-        // {
-        //     _host?.Stop();
-        //     _host = null;
-        //
-        //     //_pusher?.Stop();
-        //     //_pusher = null;
-        //
-        //     Certificate = null;
-        // }
-
-        /// <summary>
-        /// Initialise rest listener, and set up web socket port if non-zero.
-        /// </summary>
-        /// <param name="webSocketPort">The web-socket port to use, or 0 for no websocket.</param>
-        // public static void Init() //int webSocketPort)
-        // {
-        //     if(webSocketPort != 0)
-        //     {
-        //         _pusher = new RestPusher(webSocketPort);
-        //         PushManager.AddPusher(_pusher);
-        //     }
-        //
-        //     _host = Host.Create();
-        //     _host.Handler(new RestHandlerBuilder(_pusher?.Port)).Defaults().Backlog(1024);
-        // }
     }
 }

+ 0 - 57
InABox.Server/Rest/RestPusher.cs

@@ -1,57 +0,0 @@
-namespace InABox.API
-{
-
-    // class RestPusher : IPusher
-    // {
-    //     private WebSocketServer SocketServer;
-    //
-    //     public int Port => SocketServer.Port;
-    //
-    //     public RestPusher(int port)
-    //     {
-    //         SocketServer = new WebSocketServer(port);
-    //         SocketServer.Poll += SocketServer_Poll;
-    //     }
-    //
-    //     private void SocketServer_Poll(PushState.Session session)
-    //     {
-    //         PushManager.Poll(session.SessionID);
-    //     }
-    //
-    //     public void Start()
-    //     {
-    //         SocketServer.Start();
-    //     }
-    //
-    //     public void Stop()
-    //     {
-    //         SocketServer.Stop();
-    //     }
-    //
-    //     public void PushToAll<TPush>(TPush push) where TPush : BaseObject
-    //     {
-    //         SocketServer.Push(push);
-    //     }
-    //
-    //     public void PushToSession(Guid session, Type TPush, BaseObject push)
-    //     {
-    //         SocketServer.Push(session, TPush, push);
-    //     }
-    //
-    //     public void PushToSession<TPush>(Guid session, TPush push) where TPush : BaseObject
-    //     {
-    //         SocketServer.Push(session, push);
-    //     }
-    //
-    //     public IEnumerable<Guid> GetUserSessions(Guid userID)
-    //     {
-    //         return CredentialsCache.GetUserSessions(userID);
-    //     }
-    //
-    //     public IEnumerable<Guid> GetSessions(Platform platform)
-    //     {
-    //         return SocketServer.GetSessions(platform);
-    //     }
-    // }
-    
-}

+ 0 - 1
inabox.client.rest/InABox.Client.Rest/InABox.Client.Rest.csproj

@@ -7,7 +7,6 @@
     </PropertyGroup>
 
     <ItemGroup>
-      <ProjectReference Include="..\..\inabox.client.websocket\InABox.Client.WebSocket.csproj" />
       <ProjectReference Include="..\..\InABox.Core\InABox.Core.csproj" />
     </ItemGroup>
 

+ 0 - 17
inabox.client.rest/InABox.Client.Rest/RestClient.cs

@@ -7,7 +7,6 @@ using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Threading.Tasks;
-using InABox.WebSocket.Shared;
 
 
 namespace InABox.Clients
@@ -136,22 +135,6 @@ namespace InABox.Clients
             if (response != null)
                 if (response.Status.Equals(StatusCode.OK))
                 {
-                    if(response.Session != Guid.Empty)
-                    {
-                        var pushRequest = new PushRequest();
-                        PrepareRequest(pushRequest);
-
-                        // Session is required so that the server can exclude any requests from bad actors
-                        pushRequest.Credentials.Session = response.Session;
-                        var pushResponse = SendRequest<PushRequest, PushResponse>(pushRequest, "push", SerializationFormat.Binary, SerializationFormat.Binary, false);
-                        if(pushResponse != null && pushResponse.Status.Equals(StatusCode.OK))
-                        {
-                            if (pushResponse.SocketPort.HasValue)
-                            {
-                                SocketClientCache.StartWebSocket(_server, pushResponse.SocketPort.Value, response.Session);
-                            }
-                        }
-                    }
                     PasswordCache.Password = password;
                     return new ValidationData(
                         response.ValidationStatus,

+ 0 - 22
inabox.client.rest/InABox.Client.Rest/SocketClientCache.cs

@@ -1,22 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using InABox.Client.WebSocket;
-
-namespace InABox.Clients
-{
-    public static class SocketClientCache
-    {
-        private static Dictionary<string, WebSocketClient> Clients = new Dictionary<string, WebSocketClient>();
-
-        public static void StartWebSocket(string url, int port, Guid session)
-        {
-            var host = url.Split(new[] { "://" }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault();
-
-            Uri uri = new Uri($"http://{host}");
-            var key = $"{uri.Host}:{uri.Port}${session}";
-            if (!Clients.ContainsKey(key))
-                Clients[key] = new WebSocketClient(uri.Host, port, session);
-        }
-    }
-}

+ 0 - 14
inabox.client.websocket/InABox.Client.WebSocket.csproj

@@ -1,14 +0,0 @@
-<Project Sdk="Microsoft.NET.Sdk">
-
-  <PropertyGroup>
-    <TargetFramework>netstandard2.1</TargetFramework>
-    <Nullable>enable</Nullable>
-  </PropertyGroup>
-
-  <ItemGroup>
-    <ProjectReference Include="..\..\3rdpartylibs\websocket-sharp-master\websocket-sharp-standard\websocket-sharp-standard.csproj" />
-    <ProjectReference Include="..\InABox.Core\InABox.Core.csproj" />
-    <ProjectReference Include="..\inabox.websocket.shared\InABox.WebSocket.Shared.csproj" />
-  </ItemGroup>
-
-</Project>

+ 0 - 85
inabox.client.websocket/WebSocketClient.cs

@@ -1,85 +0,0 @@
-using System;
-using System.Threading.Tasks;
-using WebSocketSharp;
-using Socket = WebSocketSharp.WebSocket;
-using Logger = InABox.Core.Logger;
-using InABox.Core;
-using InABox.Clients;
-using InABox.WebSocket.Shared;
-
-namespace InABox.Client.WebSocket
-{
-    public class WebSocketClient : IDisposable
-    {
-        private Socket Socket;
-        private Guid Session;
-        private bool Closed = false;
-
-        public WebSocketClient(string url, int port, Guid session)
-        {
-            Session = session;
-            Uri uri = new Uri("http://" + url);
-            Socket = new Socket($"ws://{uri.Host}:{port}/push");
-            Socket.OnOpen += Socket_OnOpen;
-            Socket.OnError += Socket_OnError;
-            Socket.OnClose += Socket_OnClose;
-            Socket.OnMessage += Socket_OnMessage;
-            Socket.Connect();
-
-            // Time to wait before disconnect - the default meant that the client disconnected during debugging, since the ping would fail
-            Socket.WaitTime = TimeSpan.FromMinutes(10); 
-        }
-
-        private void Socket_OnMessage(object? sender, MessageEventArgs e)
-        {
-            var message = SocketMessage.ReadMessage(e.RawData);
-            if(message is PushMessage pushMessage)
-            {
-                var pushType = CoreUtils.GetEntity(pushMessage.EntityType);
-                var pushObject = Serialization.Deserialize(pushType, pushMessage.EntityData);
-                ClientFactory.PushHandlers.Push(pushType, pushObject);
-            }
-            else if(message is InitialMessage)
-            {
-            }
-        }
-
-        private void Socket_OnOpen(object? sender, EventArgs e)
-        {
-            Logger.Send(LogType.Information, "", "WebSocket connected to server");
-
-            var initial = new InitialMessage(Session, ClientFactory.Platform);
-            Socket.Send(initial.WriteToBytes());
-        }
-
-        private void Socket_OnClose(object? sender, CloseEventArgs e)
-        {
-            Logger.Send(LogType.Information, "", "WebSocket disconnected from server");
-            if (!Closed)
-            {
-                Task.Run(() =>
-                {
-                    if (!Socket.IsAlive)
-                    {
-                        Task.Delay(30_000).Wait(); // Try to reconnect after 30 seconds
-                        Socket.Connect();
-                    }
-                });
-            }
-        }
-
-        private void Socket_OnError(object? sender, ErrorEventArgs e)
-        {
-            Logger.Send(LogType.Error, "", $"WebSocket Error: {e.Message}");
-        }
-
-        public void Dispose()
-        {
-            Closed = true;
-            if (Socket.IsAlive)
-            {
-                Socket.Close();
-            }
-        }
-    }
-}