Skip to content

Commit

Permalink
refactor(lib): change network components
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojiuwo1993 committed Nov 2, 2023
1 parent 20fd9bd commit 32c5b3f
Show file tree
Hide file tree
Showing 26 changed files with 346 additions and 531 deletions.
7 changes: 2 additions & 5 deletions src/Libraries/Core/src/Abstraction/BaseClass/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using UniSpy.Server.Core.Encryption;
using UniSpy.Server.Core.Logging;
using UniSpy.Server.Core.Extension;
using System.Threading.Tasks;

namespace UniSpy.Server.Core.Abstraction.BaseClass
{
Expand Down Expand Up @@ -44,8 +43,6 @@ protected virtual void EventBinding()
break;
case NetworkConnectionType.Http:
((IHttpConnection)Connection).OnReceive += OnReceived;
((IHttpConnection)Connection).OnConnect += OnConnected;
((IHttpConnection)Connection).OnDisconnect += OnDisconnected;
break;
case NetworkConnectionType.Test:
this.LogVerbose("Using unit-test mock connection.");
Expand All @@ -57,7 +54,8 @@ protected virtual void EventBinding()
/// <summary>
/// Only work for tcp
/// </summary>
protected virtual void OnConnected() => ClientManagerBase.AddClient(this);
// protected virtual void OnConnected() => ClientManagerBase.AddClient(this);
protected virtual void OnConnected() { }

/// <summary>
/// Only work for tcp
Expand Down Expand Up @@ -139,7 +137,6 @@ public void Dispose()
/// <summary>
/// Sending IResponse to client(ciphertext or plaintext)
/// </summary>
/// <param name="response"></param>
public void Send(IResponse response)
{
byte[] buffer = null;
Expand Down
6 changes: 3 additions & 3 deletions src/Libraries/Core/src/Abstraction/Interface/IConnection.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Net;
using UniSpy.Server.Core.Events;

Expand Down Expand Up @@ -29,7 +30,6 @@ public interface IConnection
/// </summary>
public interface IUdpConnection : IConnection
{
// public TimeSpan ConnectionExistedTime { get; }
void Send(IPEndPoint endPoint, byte[] response);
void Send(IPEndPoint endPoint, string response);
}
Expand All @@ -42,11 +42,11 @@ public interface ITcpConnection : IConnection
event OnDisconnectedEventHandler OnDisconnect;
void Disconnect();
}
public interface IHttpConnection : ITcpConnection
public interface IHttpConnection : IConnection
{
}

public interface IConnectionManager
public interface IConnectionManager : IDisposable
{
event OnConnectingEventHandler OnInitialization;
void Start();
Expand Down
9 changes: 3 additions & 6 deletions src/Libraries/Core/src/Abstraction/Interface/IHttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
using System;

namespace UniSpy.Server.Core.Abstraction.Interface
{
public interface IHttpRequest
{
byte[] BodyBytes { get; }
string Body { get; }
long Cookies { get; }
long Headers { get; }
string Protocol { get; }
string Url { get; }
Uri Url { get; }
string Method { get; }
bool KeepAlive { get; }
}
}
17 changes: 0 additions & 17 deletions src/Libraries/Core/src/Misc/BufferCacheBase.cs

This file was deleted.

42 changes: 0 additions & 42 deletions src/Libraries/Core/src/Network/Http/Server/HttpBufferCache.cs

This file was deleted.

80 changes: 28 additions & 52 deletions src/Libraries/Core/src/Network/Http/Server/HttpConnection.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,38 @@
using System.Linq;
using System.Net;
using System.Text;
using UniSpy.Server.Core.Abstraction.Interface;
using UniSpy.Server.Core.Encryption;
using UniSpy.Server.Core.Events;

namespace UniSpy.Server.Core.Network.Http.Server
{
public class HttpConnection : NetCoreServer.HttpSession, IHttpConnection
{
public IPEndPoint RemoteIPEndPoint { get; private set; }
public NetworkConnectionType ConnectionType => NetworkConnectionType.Http;
public IConnectionManager Manager => (IConnectionManager)Server;
namespace UniSpy.Server.Core.Network.Http.Server;

public event OnConnectedEventHandler OnConnect;
public event OnDisconnectedEventHandler OnDisconnect;
public event OnReceivedEventHandler OnReceive;
private HttpBufferCache _bufferCache = new HttpBufferCache();
public HttpConnection(HttpConnectionManager server) : base(server)
{
}
protected override void OnConnecting()
{
if (RemoteIPEndPoint is null)
{
RemoteIPEndPoint = (IPEndPoint)Socket.RemoteEndPoint;
}
base.OnConnecting();
}
protected override void OnConnected() => OnConnect();
protected override void OnDisconnected() => OnDisconnect();
protected override void OnReceived(byte[] buffer, long offset, long size)
{
var req = UniSpyEncoding.GetString(buffer.Take((int)size).ToArray());
string compeleteBuffer;
if (_bufferCache.ProcessBuffer(req, out compeleteBuffer))
{
var completeBytes = UniSpyEncoding.GetBytes(compeleteBuffer);
base.OnReceived(completeBytes, offset, completeBytes.Length);
}
}
protected override void OnReceivedRequest(NetCoreServer.HttpRequest request) => OnReceive(new HttpRequest(request));
void IConnection.Send(string response)
{
// Response.MakeOkResponse();
Response.SetBegin(200);
Response.SetBody(response);
base.SendResponse();
}
public class HttpConnection : IHttpConnection
{
public IConnectionManager Manager { get; private set; }
public HttpListenerContext Context { get; private set; }
public IPEndPoint RemoteIPEndPoint { get; private set; }

void IConnection.Send(byte[] response)
{
// Response.MakeOkResponse();
Response.SetBegin(200);
Response.SetBody(response);
base.SendResponse();
}
public NetworkConnectionType ConnectionType { get; } = NetworkConnectionType.Http;
public event OnReceivedEventHandler OnReceive;
public HttpConnection(HttpListenerContext context, IConnectionManager manager)
{
Manager = manager;
Context = context;
RemoteIPEndPoint = context.Request.RemoteEndPoint;
}
public void OnReceived(IHttpRequest request)
{
OnReceive(request);
}

void ITcpConnection.Disconnect() => Disconnect();
public void Send(string response)
{
Send(Encoding.UTF8.GetBytes(response));
}

public void Send(byte[] response)
{
Context.Response.StatusCode = (int)HttpStatusCode.OK;
Context.Response.ContentType = "application/xml?";
Context.Response.OutputStream.Write(response);
}
}
56 changes: 41 additions & 15 deletions src/Libraries/Core/src/Network/Http/Server/HttpConnectionManager.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,51 @@
using System;
using System.Net;
using NetCoreServer;
using System.Threading.Tasks;
using UniSpy.Server.Core.Abstraction.Interface;
using UniSpy.Server.Core.Events;
using UniSpy.Server.Core.Logging;

namespace UniSpy.Server.Core.Network.Http.Server
namespace UniSpy.Server.Core.Network.Http.Server;

public class HttpConnectionManager : IConnectionManager, IDisposable
{
public class HttpConnectionManager : NetCoreServer.HttpServer, IConnectionManager
public event OnConnectingEventHandler OnInitialization;
public HttpListener Listener { get; private set; }
public HttpConnectionManager(IPEndPoint endPoint)
{
public event OnConnectingEventHandler OnInitialization;
public HttpConnectionManager(IPEndPoint endpoint) : base(endpoint)
{
}

protected override NetCoreServer.TcpSession CreateSession() => new UniSpy.Server.Core.Network.Http.Server.HttpConnection(this);
protected override void OnConnecting(TcpSession connection)
Listener = new HttpListener();
Listener.Prefixes.Add($"http://localhost:{endPoint.Port}/");
}
public void Start()
{
Listener.Start();
Task.Run(() =>
{
OnInitialization((HttpConnection)connection);
base.OnConnecting(connection);
}

public new void Start() => base.Start();
while (true)
{
try
{
var context = Listener.GetContext();
var raw = context.Request;
var request = new HttpRequest(raw);
var conn = new HttpConnection(context, this);
OnConnecting(conn);
conn.OnReceived(request);
}
catch (Exception ex)
{
LogWriter.LogError(ex);
}
}
});
}
public void OnConnecting(IHttpConnection connection)
{
OnInitialization((IConnection)connection);
}

public void Dispose()
{
Listener.Stop();
}
}
51 changes: 16 additions & 35 deletions src/Libraries/Core/src/Network/Http/Server/HttpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,45 +1,26 @@
using System;
using System.IO;
using System.Net;
using System.Text;
using UniSpy.Server.Core.Abstraction.Interface;

namespace UniSpy.Server.Core.Network.Http.Server
namespace UniSpy.Server.Core.Network.Http.Server;
public class HttpRequest : IHttpRequest
{
public class HttpRequest : IHttpRequest
public string Body { get; private set; }
public Uri Url { get; private set; }
public string Method { get; private set; }
public HttpListenerRequest RawRequest { get; private set; }
public HttpRequest(HttpListenerRequest rawRequest)
{
public Uri Uri => new Uri(Url);

public byte[] BodyBytes { get; private set; }

public string Body { get; private set; }

public long Cookies { get; private set; }

public long Headers { get; private set; }

public string Protocol { get; private set; }

public string Url { get; private set; }

public string Method { get; private set; }

public bool KeepAlive { get; private set; }


public HttpRequest(NetCoreServer.HttpRequest request)
RawRequest = rawRequest;
Method = RawRequest.HttpMethod;
Url = RawRequest.Url;
using (var stream = rawRequest.InputStream)
{
Url = request.Url;
BodyBytes = request.BodyBytes;
Body = request.Body;
Cookies = request.Cookies;
Headers = request.Headers;
Protocol = request.Protocol;
Method = request.Method;
KeepAlive = false;

for (var m = 0; m < request.Headers; m++)
using (var reader = new StreamReader(stream, rawRequest.ContentEncoding))
{
var k = request.Header(m);
if (k.Item1 == "Connection" && k.Item2.ToLower() == "keep-alive")
KeepAlive = true;
Body = reader.ReadToEnd();
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Libraries/Core/src/Network/RemoteObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ public class RemoteTcpConnectionManager : IConnectionManager
{
#pragma warning disable CS0067
public event OnConnectingEventHandler OnInitialization;
public void Start() => throw new UniSpy.Exception("Remote tcp connection do not have this method.");
public RemoteTcpConnectionManager() { }
public void Start() { }
public void Dispose() { }
}

public class RemoteTcpConnection : ITcpConnection
Expand Down
Loading

0 comments on commit 32c5b3f

Please sign in to comment.