-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(lib): change network components
- Loading branch information
1 parent
20fd9bd
commit 32c5b3f
Showing
26 changed files
with
346 additions
and
531 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
src/Libraries/Core/src/Network/Http/Server/HttpBufferCache.cs
This file was deleted.
Oops, something went wrong.
80 changes: 28 additions & 52 deletions
80
src/Libraries/Core/src/Network/Http/Server/HttpConnection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
56
src/Libraries/Core/src/Network/Http/Server/HttpConnectionManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
src/Libraries/Core/src/Network/Http/Server/HttpRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.