mirror of
https://github.com/esiur/esiur-dotnet.git
synced 2026-04-03 20:08:21 +00:00
WIP
This commit is contained in:
@@ -37,22 +37,22 @@ using Esiur.Misc;
|
||||
using System.Security.Cryptography;
|
||||
using Esiur.Core;
|
||||
using Esiur.Net.Packets.WebSocket;
|
||||
using Esiur.Net.Packets.HTTP;
|
||||
using Esiur.Net.Packets.Http;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
public class HTTPConnection : NetworkConnection
|
||||
namespace Esiur.Net.Http;
|
||||
public class HttpConnection : NetworkConnection
|
||||
{
|
||||
|
||||
|
||||
|
||||
public bool WSMode { get; internal set; }
|
||||
public HTTPServer Server { get; internal set; }
|
||||
public HttpServer Server { get; internal set; }
|
||||
|
||||
public WebsocketPacket WSRequest { get; set; }
|
||||
public HTTPRequestPacket Request { get; set; }
|
||||
public HTTPResponsePacket Response { get; } = new HTTPResponsePacket();
|
||||
public HttpRequestPacket Request { get; set; }
|
||||
public HttpResponsePacket Response { get; } = new HttpResponsePacket();
|
||||
|
||||
HTTPSession session;
|
||||
HttpSession session;
|
||||
|
||||
public KeyList<string, object> Variables { get; } = new KeyList<string, object>();
|
||||
|
||||
@@ -80,7 +80,7 @@ public class HTTPConnection : NetworkConnection
|
||||
}
|
||||
else
|
||||
{
|
||||
var rp = new HTTPRequestPacket();
|
||||
var rp = new HttpRequestPacket();
|
||||
var pSize = rp.Parse(data, 0, (uint)data.Length);
|
||||
if (pSize > 0)
|
||||
{
|
||||
@@ -115,7 +115,7 @@ public class HTTPConnection : NetworkConnection
|
||||
return ok;
|
||||
}
|
||||
|
||||
public static bool Upgrade(HTTPRequestPacket request, HTTPResponsePacket response)
|
||||
public static bool Upgrade(HttpRequestPacket request, HttpResponsePacket response)
|
||||
{
|
||||
if (IsWebsocketRequest(request))
|
||||
{
|
||||
@@ -132,7 +132,7 @@ public class HTTPConnection : NetworkConnection
|
||||
response.Headers["Sec-WebSocket-Protocol"] = request.Headers["Sec-WebSocket-Protocol"];
|
||||
|
||||
|
||||
response.Number = HTTPResponseCode.Switching;
|
||||
response.Number = HttpResponseCode.Switching;
|
||||
response.Text = "Switching Protocols";
|
||||
|
||||
return true;
|
||||
@@ -141,7 +141,7 @@ public class HTTPConnection : NetworkConnection
|
||||
return false;
|
||||
}
|
||||
|
||||
public HTTPServer Parent
|
||||
public HttpServer Parent
|
||||
{
|
||||
get
|
||||
{
|
||||
@@ -173,7 +173,7 @@ public class HTTPConnection : NetworkConnection
|
||||
Send();
|
||||
}
|
||||
|
||||
public void Send(HTTPComposeOption Options = HTTPComposeOption.AllCalculateLength)
|
||||
public void Send(HttpComposeOption Options = HttpComposeOption.AllCalculateLength)
|
||||
{
|
||||
if (Response.Handled)
|
||||
return;
|
||||
@@ -211,7 +211,7 @@ public class HTTPConnection : NetworkConnection
|
||||
// Create a new one
|
||||
session = Server.CreateSession(Global.GenerateCode(12), 60 * 20);
|
||||
|
||||
HTTPCookie cookie = new HTTPCookie("SID", session.Id);
|
||||
HttpCookie cookie = new HttpCookie("SID", session.Id);
|
||||
cookie.Expires = DateTime.MaxValue;
|
||||
cookie.Path = "/";
|
||||
cookie.HttpOnly = true;
|
||||
@@ -226,7 +226,7 @@ public class HTTPConnection : NetworkConnection
|
||||
return IsWebsocketRequest(this.Request);
|
||||
}
|
||||
|
||||
public static bool IsWebsocketRequest(HTTPRequestPacket request)
|
||||
public static bool IsWebsocketRequest(HttpRequestPacket request)
|
||||
{
|
||||
if (request.Headers.ContainsKey("connection")
|
||||
&& request.Headers["connection"].ToLower().Contains("upgrade")
|
||||
@@ -254,7 +254,7 @@ public class HTTPConnection : NetworkConnection
|
||||
|
||||
if (BL == 0)
|
||||
{
|
||||
if (Request.Method == HTTPMethod.UNKNOWN)
|
||||
if (Request.Method == Packets.Http.HttpMethod.UNKNOWN)
|
||||
{
|
||||
Close();
|
||||
return;
|
||||
@@ -313,7 +313,7 @@ public class HTTPConnection : NetworkConnection
|
||||
{
|
||||
if (!Server.Execute(this))
|
||||
{
|
||||
Response.Number = HTTPResponseCode.InternalServerError;
|
||||
Response.Number = HttpResponseCode.InternalServerError;
|
||||
Send("Bad Request");
|
||||
Close();
|
||||
}
|
||||
@@ -362,7 +362,7 @@ public class HTTPConnection : NetworkConnection
|
||||
|
||||
if (!File.Exists(filename))
|
||||
{
|
||||
Response.Number = HTTPResponseCode.NotFound;
|
||||
Response.Number = HttpResponseCode.NotFound;
|
||||
Send("File Not Found");
|
||||
return true;
|
||||
}
|
||||
@@ -376,10 +376,10 @@ public class HTTPConnection : NetworkConnection
|
||||
var ims = DateTime.Parse(Request.Headers["if-modified-since"]);
|
||||
if ((fileEditTime - ims).TotalSeconds < 2)
|
||||
{
|
||||
Response.Number = HTTPResponseCode.NotModified;
|
||||
Response.Number = HttpResponseCode.NotModified;
|
||||
Response.Headers.Clear();
|
||||
//Response.Text = "Not Modified";
|
||||
Send(HTTPComposeOption.SpecifiedHeadersOnly);
|
||||
Send(HttpComposeOption.SpecifiedHeadersOnly);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -391,12 +391,12 @@ public class HTTPConnection : NetworkConnection
|
||||
|
||||
|
||||
|
||||
Response.Number = HTTPResponseCode.OK;
|
||||
Response.Number = HttpResponseCode.OK;
|
||||
// Fri, 30 Oct 2007 14:19:41 GMT
|
||||
Response.Headers["Last-Modified"] = fileEditTime.ToString("ddd, dd MMM yyyy HH:mm:ss");
|
||||
FileInfo fi = new FileInfo(filename);
|
||||
Response.Headers["Content-Length"] = fi.Length.ToString();
|
||||
Send(HTTPComposeOption.SpecifiedHeadersOnly);
|
||||
Send(HttpComposeOption.SpecifiedHeadersOnly);
|
||||
|
||||
//var fd = File.ReadAllBytes(filename);
|
||||
|
||||
|
||||
@@ -35,9 +35,9 @@ using Esiur.Data;
|
||||
using Esiur.Core;
|
||||
using Esiur.Resource;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
namespace Esiur.Net.Http;
|
||||
|
||||
public abstract class HTTPFilter : IResource
|
||||
public abstract class HttpFilter : IResource
|
||||
{
|
||||
public Instance Instance
|
||||
{
|
||||
@@ -60,14 +60,14 @@ public abstract class HTTPFilter : IResource
|
||||
}
|
||||
*/
|
||||
|
||||
public abstract AsyncReply<bool> Execute(HTTPConnection sender);
|
||||
public abstract AsyncReply<bool> Execute(HttpConnection sender);
|
||||
|
||||
public virtual void ClientConnected(HTTPConnection HTTP)
|
||||
public virtual void ClientConnected(HttpConnection HTTP)
|
||||
{
|
||||
//return false;
|
||||
}
|
||||
|
||||
public virtual void ClientDisconnected(HTTPConnection HTTP)
|
||||
public virtual void ClientDisconnected(HttpConnection HTTP)
|
||||
{
|
||||
//return false;
|
||||
}
|
||||
|
||||
@@ -40,25 +40,25 @@ using Esiur.Resource;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using Esiur.Net.Packets.HTTP;
|
||||
using Esiur.Net.Packets.Http;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
namespace Esiur.Net.Http;
|
||||
public class HttpServer : NetworkServer<HttpConnection>, IResource
|
||||
{
|
||||
Dictionary<string, HTTPSession> sessions = new Dictionary<string, HTTPSession>();
|
||||
HTTPFilter[] filters = new HTTPFilter[0];
|
||||
Dictionary<string, HttpSession> sessions = new Dictionary<string, HttpSession>();
|
||||
HttpFilter[] filters = new HttpFilter[0];
|
||||
|
||||
Dictionary<HTTPMethod, List<RouteInfo>> routes = new()
|
||||
Dictionary<Packets.Http.HttpMethod, List<RouteInfo>> routes = new()
|
||||
{
|
||||
[HTTPMethod.GET] = new List<RouteInfo>(),
|
||||
[HTTPMethod.POST] = new List<RouteInfo>(),
|
||||
[HTTPMethod.HEAD] = new List<RouteInfo>(),
|
||||
[HTTPMethod.OPTIONS] = new List<RouteInfo>(),
|
||||
[HTTPMethod.UNKNOWN] = new List<RouteInfo>(),
|
||||
[HTTPMethod.DELETE] = new List<RouteInfo>(),
|
||||
[HTTPMethod.TRACE] = new List<RouteInfo>(),
|
||||
[HTTPMethod.CONNECT] = new List<RouteInfo>(),
|
||||
[HTTPMethod.PUT] = new List<RouteInfo>()
|
||||
[Packets.Http.HttpMethod.GET] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.POST] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.HEAD] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.OPTIONS] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.UNKNOWN] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.DELETE] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.TRACE] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.CONNECT] = new List<RouteInfo>(),
|
||||
[Packets.Http.HttpMethod.PUT] = new List<RouteInfo>()
|
||||
};
|
||||
|
||||
//List<RouteInfo> GetRoutes = new List<RouteInfo>();
|
||||
@@ -86,7 +86,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
|
||||
var last = ps.LastOrDefault();
|
||||
|
||||
if (last != null && last.ParameterType == typeof(HTTPConnection))
|
||||
if (last != null && last.ParameterType == typeof(HttpConnection))
|
||||
{
|
||||
|
||||
SenderIndex = ps.Length - 1;
|
||||
@@ -101,7 +101,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
}
|
||||
|
||||
|
||||
public bool Invoke(HTTPConnection sender)
|
||||
public bool Invoke(HttpConnection sender)
|
||||
{
|
||||
var match = Pattern.Match(sender.Request.URL);
|
||||
|
||||
@@ -176,9 +176,9 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
}
|
||||
|
||||
|
||||
public HTTPSession CreateSession(string id, int timeout)
|
||||
public HttpSession CreateSession(string id, int timeout)
|
||||
{
|
||||
var s = new HTTPSession();
|
||||
var s = new HttpSession();
|
||||
|
||||
s.Set(id, timeout);
|
||||
|
||||
@@ -214,7 +214,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
return Cookie;
|
||||
}
|
||||
|
||||
protected override void ClientDisconnected(HTTPConnection connection)
|
||||
protected override void ClientDisconnected(HttpConnection connection)
|
||||
{
|
||||
foreach (var filter in filters)
|
||||
filter.ClientDisconnected(connection);
|
||||
@@ -222,7 +222,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
|
||||
|
||||
|
||||
internal bool Execute(HTTPConnection sender)
|
||||
internal bool Execute(HttpConnection sender)
|
||||
{
|
||||
if (!sender.WSMode)
|
||||
foreach (var route in routes[sender.Request.Method])
|
||||
@@ -243,14 +243,14 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
public void MapGet(string pattern, Delegate handler)
|
||||
{
|
||||
var regex = Global.GetRouteRegex(pattern);
|
||||
var list = routes[HTTPMethod.GET];
|
||||
var list = routes[Packets.Http.HttpMethod.GET];
|
||||
list.Add(new RouteInfo(handler, regex));
|
||||
}
|
||||
|
||||
public void MapPost(string pattern, Delegate handler)
|
||||
{
|
||||
var regex = Global.GetRouteRegex(pattern);
|
||||
var list = routes[HTTPMethod.POST];
|
||||
var list = routes[Packets.Http.HttpMethod.POST];
|
||||
list.Add(new RouteInfo(handler, regex));
|
||||
}
|
||||
|
||||
@@ -329,7 +329,7 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
}
|
||||
else if (trigger == ResourceTrigger.SystemInitialized)
|
||||
{
|
||||
filters = await Instance.Children<HTTPFilter>();
|
||||
filters = await Instance.Children<HttpFilter>();
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -337,19 +337,19 @@ public class HTTPServer : NetworkServer<HTTPConnection>, IResource
|
||||
}
|
||||
|
||||
|
||||
public override void Add(HTTPConnection connection)
|
||||
public override void Add(HttpConnection connection)
|
||||
{
|
||||
connection.Server = this;
|
||||
base.Add(connection);
|
||||
}
|
||||
|
||||
public override void Remove(HTTPConnection connection)
|
||||
public override void Remove(HttpConnection connection)
|
||||
{
|
||||
connection.Server = null;
|
||||
base.Remove(connection);
|
||||
}
|
||||
|
||||
protected override void ClientConnected(HTTPConnection connection)
|
||||
protected override void ClientConnected(HttpConnection connection)
|
||||
{
|
||||
//if (filters.Length == 0 && routes.)
|
||||
//{
|
||||
|
||||
@@ -35,11 +35,11 @@ using Esiur.Data;
|
||||
using Esiur.Misc;
|
||||
using Esiur.Core;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
public class HTTPSession : IDestructible //<T> where T : TClient
|
||||
namespace Esiur.Net.Http;
|
||||
public class HttpSession : IDestructible //<T> where T : TClient
|
||||
{
|
||||
public delegate void SessionModifiedEvent(HTTPSession session, string key, object oldValue, object newValue);
|
||||
public delegate void SessionEndedEvent(HTTPSession session);
|
||||
public delegate void SessionModifiedEvent(HttpSession session, string key, object oldValue, object newValue);
|
||||
public delegate void SessionEndedEvent(HttpSession session);
|
||||
|
||||
private string id;
|
||||
private Timer timer;
|
||||
@@ -58,7 +58,7 @@ public class HTTPSession : IDestructible //<T> where T : TClient
|
||||
get { return variables; }
|
||||
}
|
||||
|
||||
public HTTPSession()
|
||||
public HttpSession()
|
||||
{
|
||||
variables = new KeyList<string, object>();
|
||||
variables.OnModified += new KeyList<string, object>.Modified(VariablesModified);
|
||||
|
||||
@@ -6,13 +6,13 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
public class EPoHTTP : HTTPFilter
|
||||
namespace Esiur.Net.Http;
|
||||
public class EpOverHttp : HttpFilter
|
||||
{
|
||||
[Attribute]
|
||||
EntryPoint EntryPoint { get; set; }
|
||||
|
||||
public override AsyncReply<bool> Execute(HTTPConnection sender)
|
||||
public override AsyncReply<bool> Execute(HttpConnection sender)
|
||||
{
|
||||
if (sender.Request.URL != "EP")
|
||||
return new AsyncReply<bool>(false);
|
||||
@@ -32,8 +32,8 @@ using Esiur.Net.Sockets;
|
||||
using Esiur.Core;
|
||||
using Esiur.Protocol;
|
||||
|
||||
namespace Esiur.Net.HTTP;
|
||||
public class EPoWS : HTTPFilter
|
||||
namespace Esiur.Net.Http;
|
||||
public class EpOvwerWebsocket : HttpFilter
|
||||
{
|
||||
[Attribute]
|
||||
public EpServer Server
|
||||
@@ -42,7 +42,7 @@ public class EPoWS : HTTPFilter
|
||||
set;
|
||||
}
|
||||
|
||||
public override AsyncReply<bool> Execute(HTTPConnection sender)
|
||||
public override AsyncReply<bool> Execute(HttpConnection sender)
|
||||
{
|
||||
|
||||
if (sender.IsWebsocketRequest())
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017 Ahmed Kh. Zamil
|
||||
Copyright (c) 2017-2026 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -37,23 +37,11 @@ namespace Esiur.Net.Packets;
|
||||
|
||||
public class EpAuthPacket : Packet
|
||||
{
|
||||
|
||||
public EpAuthPacketCommand Command
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public EpAuthPacketInitialize Initialization
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public EpAuthPacketAcknowledge Acknowledgement
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public EpAuthPacketAction Action
|
||||
{
|
||||
@@ -67,18 +55,32 @@ public class EpAuthPacket : Packet
|
||||
set;
|
||||
}
|
||||
|
||||
public AuthenticationMethod LocalMethod
|
||||
public EpAuthPacketAcknowledgement Acknowledgement
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public AuthenticationMethod RemoteMethod
|
||||
|
||||
public EpAuthPacketAuthMode AuthMode
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public EpAuthPacketEncryptionMode EncryptionMode
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
//public AuthenticationMethod AuthenticationMethod
|
||||
//{
|
||||
// get;
|
||||
// set;
|
||||
//}
|
||||
|
||||
|
||||
public byte ErrorCode
|
||||
{
|
||||
get;
|
||||
@@ -91,52 +93,14 @@ public class EpAuthPacket : Packet
|
||||
set;
|
||||
}
|
||||
|
||||
|
||||
public EpAuthPacketPublicKeyAlgorithm PublicKeyAlgorithm
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public EpAuthPacketHashAlgorithm HashAlgorithm
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
|
||||
public byte[] Certificate
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public byte[] Challenge
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public byte[] AsymetricEncryptionKey
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
|
||||
public byte[] SessionId
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public byte[] AccountId
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public ParsedTDU? DataType
|
||||
public ParsedTdu? Tdu
|
||||
{
|
||||
get;
|
||||
set;
|
||||
@@ -177,284 +141,46 @@ public class EpAuthPacket : Packet
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Command = (EpAuthPacketCommand)(data[offset] >> 6);
|
||||
var hasTdu = (data[offset] & 0x20) != 0;
|
||||
|
||||
if (Command == EpAuthPacketCommand.Initialize)
|
||||
{
|
||||
LocalMethod = (AuthenticationMethod)(data[offset] >> 4 & 0x3);
|
||||
RemoteMethod = (AuthenticationMethod)(data[offset] >> 2 & 0x3);
|
||||
|
||||
Initialization = (EpAuthPacketInitialize)(data[offset++] & 0xFC); // remove last two reserved LSBs
|
||||
|
||||
if (NotEnough(offset, ends, 1))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
DataType = ParsedTDU.Parse(data, offset, ends);
|
||||
|
||||
if (DataType.Value.Class == TDUClass.Invalid)
|
||||
return -(int)DataType.Value.TotalLength;
|
||||
|
||||
|
||||
offset += (uint)DataType.Value.TotalLength;
|
||||
|
||||
AuthMode = (EpAuthPacketAuthMode)(data[offset] >> 3 & 0x7);
|
||||
EncryptionMode = (EpAuthPacketEncryptionMode)(data[offset++] & 0x7);
|
||||
}
|
||||
else if (Command == EpAuthPacketCommand.Acknowledge)
|
||||
{
|
||||
|
||||
LocalMethod = (AuthenticationMethod)(data[offset] >> 4 & 0x3);
|
||||
RemoteMethod = (AuthenticationMethod)(data[offset] >> 2 & 0x3);
|
||||
|
||||
Acknowledgement = (EpAuthPacketAcknowledge)(data[offset++] & 0xFC); // remove last two reserved LSBs
|
||||
|
||||
if (NotEnough(offset, ends, 1))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
DataType = ParsedTDU.Parse(data, offset, ends);
|
||||
|
||||
if (DataType.Value.Class == TDUClass.Invalid)
|
||||
return -(int)DataType.Value.TotalLength;
|
||||
|
||||
|
||||
offset += (uint)DataType.Value.TotalLength;
|
||||
// remove last two reserved LSBs
|
||||
Acknowledgement = (EpAuthPacketAcknowledgement)(data[offset++]);// & 0xFC);
|
||||
}
|
||||
else if (Command == EpAuthPacketCommand.Action)
|
||||
{
|
||||
Action = (EpAuthPacketAction)data[offset++]; // (EPAuthPacketAction)(data[offset++] & 0x3f);
|
||||
|
||||
if (Action == EpAuthPacketAction.AuthenticateHash
|
||||
|| Action == EpAuthPacketAction.AuthenticatePublicHash
|
||||
|| Action == EpAuthPacketAction.AuthenticatePrivateHash
|
||||
|| Action == EpAuthPacketAction.AuthenticatePublicPrivateHash)
|
||||
{
|
||||
if (NotEnough(offset, ends, 3))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
HashAlgorithm = (EpAuthPacketHashAlgorithm)data[offset++];
|
||||
|
||||
var hashLength = data.GetUInt16(offset, Endian.Little);
|
||||
offset += 2;
|
||||
|
||||
|
||||
if (NotEnough(offset, ends, hashLength))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Challenge = data.Clip(offset, hashLength);
|
||||
offset += hashLength;
|
||||
|
||||
}
|
||||
else if (Action == EpAuthPacketAction.AuthenticatePrivateHashCert
|
||||
|| Action == EpAuthPacketAction.AuthenticatePublicPrivateHashCert)
|
||||
{
|
||||
if (NotEnough(offset, ends, 3))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
HashAlgorithm = (EpAuthPacketHashAlgorithm)data[offset++];
|
||||
|
||||
var hashLength = data.GetUInt16(offset, Endian.Little);
|
||||
offset += 2;
|
||||
|
||||
|
||||
if (NotEnough(offset, ends, hashLength))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Challenge = data.Clip(offset, hashLength);
|
||||
offset += hashLength;
|
||||
|
||||
if (NotEnough(offset, ends, 2))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
var certLength = data.GetUInt16(offset, Endian.Little);
|
||||
offset += 2;
|
||||
|
||||
if (NotEnough(offset, ends, certLength))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Certificate = data.Clip(offset, certLength);
|
||||
|
||||
offset += certLength;
|
||||
}
|
||||
else if (Action == EpAuthPacketAction.IAuthPlain)
|
||||
{
|
||||
if (NotEnough(offset, ends, 5))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Reference = data.GetUInt32(offset, Endian.Little);
|
||||
offset += 4;
|
||||
|
||||
DataType = ParsedTDU.Parse(data, offset, ends);
|
||||
|
||||
if (DataType.Value.Class == TDUClass.Invalid)
|
||||
return -(int)DataType.Value.TotalLength;
|
||||
|
||||
offset += (uint)DataType.Value.TotalLength;
|
||||
|
||||
}
|
||||
else if (Action == EpAuthPacketAction.IAuthHashed)
|
||||
{
|
||||
if (NotEnough(offset, ends, 7))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Reference = data.GetUInt32(offset, Endian.Little);
|
||||
offset += 4;
|
||||
|
||||
HashAlgorithm = (EpAuthPacketHashAlgorithm)data[offset++];
|
||||
|
||||
var cl = data.GetUInt16(offset, Endian.Little);
|
||||
offset += 2;
|
||||
|
||||
if (NotEnough(offset, ends, cl))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Challenge = data.Clip(offset, cl);
|
||||
|
||||
offset += cl;
|
||||
|
||||
}
|
||||
else if (Action == EpAuthPacketAction.IAuthEncrypted)
|
||||
{
|
||||
if (NotEnough(offset, ends, 7))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Reference = data.GetUInt32(offset, Endian.Little);
|
||||
offset += 4;
|
||||
|
||||
PublicKeyAlgorithm = (EpAuthPacketPublicKeyAlgorithm)data[offset++];
|
||||
|
||||
var cl = data.GetUInt16(offset, Endian.Little);
|
||||
offset += 2;
|
||||
|
||||
if (NotEnough(offset, ends, cl))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Challenge = data.Clip(offset, cl);
|
||||
|
||||
offset += cl;
|
||||
|
||||
}
|
||||
else if (Action == EpAuthPacketAction.EstablishNewSession)
|
||||
{
|
||||
// Nothing here
|
||||
}
|
||||
else if (Action == EpAuthPacketAction.EstablishResumeSession)
|
||||
{
|
||||
if (NotEnough(offset, ends, 1))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
var sessionLength = data[offset++];
|
||||
|
||||
if (NotEnough(offset, ends, sessionLength))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
SessionId = data.Clip(offset, sessionLength);
|
||||
|
||||
offset += sessionLength;
|
||||
}
|
||||
|
||||
else if (Action == EpAuthPacketAction.EncryptKeyExchange)
|
||||
{
|
||||
if (NotEnough(offset, ends, 2))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
var keyLength = data.GetUInt16(offset, Endian.Little);
|
||||
|
||||
offset += 2;
|
||||
|
||||
if (NotEnough(offset, ends, keyLength))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
AsymetricEncryptionKey = data.Clip(offset, keyLength);
|
||||
|
||||
offset += keyLength;
|
||||
}
|
||||
|
||||
else if (Action == EpAuthPacketAction.RegisterEndToEndKey
|
||||
|| Action == EpAuthPacketAction.RegisterHomomorphic)
|
||||
{
|
||||
if (NotEnough(offset, ends, 3))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
PublicKeyAlgorithm = (EpAuthPacketPublicKeyAlgorithm)data[offset++];
|
||||
|
||||
var keyLength = data.GetUInt16(offset, Endian.Little);
|
||||
|
||||
offset += 2;
|
||||
|
||||
if (NotEnough(offset, ends, keyLength))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
AsymetricEncryptionKey = data.Clip(offset, keyLength);
|
||||
|
||||
offset += keyLength;
|
||||
|
||||
}
|
||||
// remove last two reserved LSBs
|
||||
Action = (EpAuthPacketAction)(data[offset++]);// & 0xFC);
|
||||
}
|
||||
else if (Command == EpAuthPacketCommand.Event)
|
||||
{
|
||||
|
||||
Event = (EpAuthPacketEvent)data[offset++];
|
||||
|
||||
if (Event == EpAuthPacketEvent.ErrorTerminate
|
||||
|| Event == EpAuthPacketEvent.ErrorMustEncrypt
|
||||
|| Event == EpAuthPacketEvent.ErrorRetry)
|
||||
{
|
||||
if (NotEnough(offset, ends, 3))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
ErrorCode = data[offset++];
|
||||
var msgLength = data.GetUInt16(offset, Endian.Little);
|
||||
offset += 2;
|
||||
|
||||
if (NotEnough(offset, ends, msgLength))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
|
||||
Message = data.GetString(offset, msgLength);
|
||||
|
||||
offset += msgLength;
|
||||
}
|
||||
else if (Event == EpAuthPacketEvent.IndicationEstablished)
|
||||
{
|
||||
if (NotEnough(offset, ends, 2))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
var sessionLength = data[offset++];
|
||||
|
||||
if (NotEnough(offset, ends, sessionLength))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
SessionId = data.Clip(offset, sessionLength);
|
||||
|
||||
offset += sessionLength;
|
||||
|
||||
if (NotEnough(offset, ends, 1))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
var accountLength = data[offset++];
|
||||
|
||||
if (NotEnough(offset, ends, accountLength))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
AccountId = data.Clip(offset, accountLength);
|
||||
|
||||
offset += accountLength;
|
||||
}
|
||||
|
||||
else if (Event == EpAuthPacketEvent.IAuthPlain
|
||||
|| Event == EpAuthPacketEvent.IAuthHashed
|
||||
|| Event == EpAuthPacketEvent.IAuthEncrypted)
|
||||
{
|
||||
if (NotEnough(offset, ends, 1))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
DataType = ParsedTDU.Parse(data, offset, ends);
|
||||
|
||||
if (DataType.Value.Class == TDUClass.Invalid)
|
||||
return -(int)DataType.Value.TotalLength;
|
||||
|
||||
offset += (uint)DataType.Value.TotalLength;
|
||||
|
||||
}
|
||||
// remove last two reserved LSBs
|
||||
Event = (EpAuthPacketEvent)(data[offset++]);// & 0xFC);
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1; // invalid command
|
||||
}
|
||||
|
||||
if (hasTdu)
|
||||
{
|
||||
if (NotEnough(offset, ends, 1))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
Tdu = ParsedTdu.Parse(data, offset, ends);
|
||||
|
||||
if (Tdu.Value.Class == TduClass.Invalid)
|
||||
return -(int)Tdu.Value.TotalLength;
|
||||
|
||||
offset += (uint)Tdu.Value.TotalLength;
|
||||
|
||||
}
|
||||
|
||||
return offset - oOffset;
|
||||
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets
|
||||
{
|
||||
public enum EpAuthPacketAcknowledge : byte
|
||||
{
|
||||
NoAuthNoAuth = 0x40, // 0b01000000,
|
||||
NoAuthCredentials = 0x44, // 0b01000100,
|
||||
NoAuthToken = 0x48, //0b01001000,
|
||||
NoAuthCertificate = 0x4c, //0b01001100,
|
||||
CredentialsNoAuth = 0x50, //0b01010000,
|
||||
CredentialsCredentials = 0x54, //0b01010100,
|
||||
CredentialsToken = 0x58, //0b01011000,
|
||||
CredentialsCertificate = 0x5c, //0b01011100,
|
||||
TokenNoAuth = 0x60, //0b01100000,
|
||||
TokenCredentials = 0x64, //0b01100100,
|
||||
TokenToken = 0x68, //0b01101000,
|
||||
TokenCertificate = 0x6c, //0b01101100,
|
||||
CertificateNoAuth = 0x70, //0b01110000,
|
||||
CertificateCredentials = 0x74, //0b01110100,
|
||||
CertificateToken = 0x78, //0b01111000,
|
||||
CertificateCertificate = 0x7c, // 0b01111100,
|
||||
}
|
||||
}
|
||||
18
Esiur/Net/Packets/EpAuthPacketAcknowledgement.cs
Normal file
18
Esiur/Net/Packets/EpAuthPacketAcknowledgement.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets
|
||||
{
|
||||
public enum EpAuthPacketAcknowledgement : byte
|
||||
{
|
||||
Denied = 0x40, // no reason, terminate connection
|
||||
NotSupported = 0x41, // auth not supported, terminate connection
|
||||
TrySupported = 0x42, // auth not supported, but other auth methods in the reply are supported. connection is still open
|
||||
Retry = 0x43, // try another auth method, connection is still open
|
||||
ProceedToHandshake = 0x44, // auth method accepted, proceed to handshake, connection is still open
|
||||
ProceedToFinalHandshake = 0x45, // auth method accepted, proceed to final handshake, connection is still open
|
||||
ProceedToEstablishSession = 0x46, // auth method accepted, proceed to establish session, connection is still open
|
||||
SessionEstablished = 0x47, // session established, session Id provided, switch to session mode, connection is still open
|
||||
}
|
||||
}
|
||||
@@ -6,26 +6,29 @@ namespace Esiur.Net.Packets
|
||||
{
|
||||
public enum EpAuthPacketAction : byte
|
||||
{
|
||||
AuthenticateHash = 0x80,
|
||||
AuthenticatePublicHash = 0x81,
|
||||
AuthenticatePrivateHash = 0x82,
|
||||
AuthenticatePublicPrivateHash = 0x83,
|
||||
Handshake = 0x80,
|
||||
FinalHandshake = 0x81,
|
||||
|
||||
AuthenticatePrivateHashCert = 0x88,
|
||||
AuthenticatePublicPrivateHashCert = 0x89,
|
||||
//AuthenticateHash = 0x80,
|
||||
//AuthenticatePublicHash = 0x81,
|
||||
//AuthenticatePrivateHash = 0x82,
|
||||
//AuthenticatePublicPrivateHash = 0x83,
|
||||
|
||||
IAuthPlain = 0x90,
|
||||
IAuthHashed = 0x91,
|
||||
IAuthEncrypted = 0x92,
|
||||
//AuthenticatePrivateHashCert = 0x88,
|
||||
//AuthenticatePublicPrivateHashCert = 0x89,
|
||||
|
||||
//IAuthPlain = 0x90,
|
||||
//IAuthHashed = 0x91,
|
||||
//IAuthEncrypted = 0x92,
|
||||
|
||||
|
||||
EstablishNewSession = 0x98,
|
||||
EstablishResumeSession = 0x99,
|
||||
//EstablishNewSession = 0x98,
|
||||
//EstablishResumeSession = 0x99,
|
||||
|
||||
EncryptKeyExchange = 0xA0,
|
||||
//EncryptKeyExchange = 0xA0,
|
||||
|
||||
RegisterEndToEndKey = 0xA8,
|
||||
RegisterHomomorphic = 0xA9,
|
||||
//RegisterEndToEndKey = 0xA8,
|
||||
//RegisterHomomorphic = 0xA9,
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
32
Esiur/Net/Packets/EpAuthPacketAuthMode.cs
Normal file
32
Esiur/Net/Packets/EpAuthPacketAuthMode.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets
|
||||
{
|
||||
public enum EpAuthPacketAuthMode : byte
|
||||
{
|
||||
NoAuh = 0x0,
|
||||
InitializerIdentity = 0x1,
|
||||
ResponderIdentity = 0x2,
|
||||
DualIdentity = 0x3,
|
||||
|
||||
|
||||
//NoAuthNoAuth = 0x0, //0b00000000,
|
||||
//NoAuthCredentials = 0x4, //0b00000100,
|
||||
//NoAuthToken = 0x8, //0b00001000,
|
||||
//NoAuthCertificate = 0xC, //0b00001100,
|
||||
//CredentialsNoAuth = 0x10, //0b00010000,
|
||||
//CredentialsCredentials = 0x14, //0b00010100,
|
||||
//CredentialsToken = 0x18, //0b00011000,
|
||||
//CredentialsCertificate = 0x1c, //0b00011100,
|
||||
//TokenNoAuth = 0x20, //0b00100000,
|
||||
//TokenCredentials = 0x24, //0b00100100,
|
||||
//TokenToken = 0x28, //0b00101000,
|
||||
//TokenCertificate = 0x2c, //0b00101100,
|
||||
//CertificateNoAuth = 0x30, //0b00110000,
|
||||
//CertificateCredentials = 0x34,// 0b00110100,
|
||||
//CertificateToken = 0x38, //0b00111000,
|
||||
//CertificateCertificate = 0x3c, //0b00111100,
|
||||
}
|
||||
}
|
||||
@@ -11,5 +11,4 @@ namespace Esiur.Net.Packets
|
||||
Action = 0x2,
|
||||
Event = 0x3,
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
13
Esiur/Net/Packets/EpAuthPacketEncryptionMode.cs
Normal file
13
Esiur/Net/Packets/EpAuthPacketEncryptionMode.cs
Normal file
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets
|
||||
{
|
||||
public enum EpAuthPacketEncryptionMode
|
||||
{
|
||||
NoEncryption,
|
||||
EncryptWithSessionKey,
|
||||
EncryptWithSessionKeyAndAddress,
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets
|
||||
{
|
||||
public enum EpAuthPacketHashAlgorithm
|
||||
{
|
||||
SHA256,
|
||||
SHA3,
|
||||
}
|
||||
}
|
||||
@@ -6,24 +6,19 @@ namespace Esiur.Net.Packets
|
||||
{
|
||||
public enum EpAuthPacketHeader
|
||||
{
|
||||
Version = 0,
|
||||
Domain = 1,
|
||||
SupportedAuthentications = 2,
|
||||
SupportedHashAlgorithms = 3,
|
||||
SupportedCiphers = 4,
|
||||
SupportedCompression = 5,
|
||||
SupportedPersonalAuth = 6,
|
||||
Nonce = 7,
|
||||
Username = 8,
|
||||
TokenIndex = 9,
|
||||
CertificateId = 10,
|
||||
CachedCertificates = 11,
|
||||
CipherType = 12,
|
||||
CipherKey = 13,
|
||||
SoftwareIdentity = 14,
|
||||
Referrer = 15,
|
||||
Time = 16,
|
||||
Certificate = 17,
|
||||
IPAddress = 18,
|
||||
Version,
|
||||
Domain,
|
||||
SupportedAuthentications ,
|
||||
SupportedHashAlgorithms,
|
||||
SupportedCiphers,
|
||||
SupportedCompression,
|
||||
SupportedMultiFactorAuthentications,
|
||||
CipherType,
|
||||
CipherKey,
|
||||
SoftwareIdentity,
|
||||
Referrer,
|
||||
Time,
|
||||
IPAddress,
|
||||
AuthenticationData,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets
|
||||
{
|
||||
public enum EpAuthPacketInitialize
|
||||
{
|
||||
NoAuthNoAuth = 0x0, //0b00000000,
|
||||
NoAuthCredentials = 0x4, //0b00000100,
|
||||
NoAuthToken = 0x8, //0b00001000,
|
||||
NoAuthCertificate = 0xC, //0b00001100,
|
||||
CredentialsNoAuth = 0x10, //0b00010000,
|
||||
CredentialsCredentials = 0x14, //0b00010100,
|
||||
CredentialsToken = 0x18, //0b00011000,
|
||||
CredentialsCertificate = 0x1c, //0b00011100,
|
||||
TokenNoAuth = 0x20, //0b00100000,
|
||||
TokenCredentials = 0x24, //0b00100100,
|
||||
TokenToken = 0x28, //0b00101000,
|
||||
TokenCertificate = 0x2c, //0b00101100,
|
||||
CertificateNoAuth = 0x30, //0b00110000,
|
||||
CertificateCredentials = 0x34,// 0b00110100,
|
||||
CertificateToken = 0x38, //0b00111000,
|
||||
CertificateCertificate = 0x3c, //0b00111100,
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets
|
||||
{
|
||||
public enum EpAuthPacketPublicKeyAlgorithm
|
||||
{
|
||||
RSA = 0,
|
||||
CKKS = 1,
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
|
||||
Copyright (c) 2017-2025 Ahmed Kh. Zamil
|
||||
Copyright (c) 2017-2026 Ahmed Kh. Zamil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -44,7 +44,7 @@ class EpPacket : Packet
|
||||
|
||||
public byte Extension { get; set; }
|
||||
|
||||
public ParsedTDU? DataType { get; set; }
|
||||
public ParsedTdu? Tdu { get; set; }
|
||||
|
||||
|
||||
private uint dataLengthNeeded;
|
||||
@@ -124,16 +124,16 @@ class EpPacket : Packet
|
||||
if (NotEnough(offset, ends, 1))
|
||||
return -dataLengthNeeded;
|
||||
|
||||
DataType = ParsedTDU.Parse(data, offset, ends);
|
||||
Tdu = ParsedTdu.Parse(data, offset, ends);
|
||||
|
||||
if (DataType.Value.Class == TDUClass.Invalid)
|
||||
return -(int)DataType.Value.TotalLength;
|
||||
if (Tdu.Value.Class == TduClass.Invalid)
|
||||
return -(int)Tdu.Value.TotalLength;
|
||||
|
||||
offset += (uint)DataType.Value.TotalLength;
|
||||
offset += (uint)Tdu.Value.TotalLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
DataType = null;
|
||||
Tdu = null;
|
||||
}
|
||||
|
||||
return offset - originalOffset;
|
||||
|
||||
@@ -10,9 +10,9 @@ struct EpPacketAttachInfo
|
||||
public string Link;
|
||||
public ulong Age;
|
||||
public byte[] Content;
|
||||
public UUID TypeId;
|
||||
public Uuid TypeId;
|
||||
|
||||
public EpPacketAttachInfo(UUID typeId, ulong age, string link, byte[] content)
|
||||
public EpPacketAttachInfo(Uuid typeId, ulong age, string link, byte[] content)
|
||||
{
|
||||
TypeId = typeId;
|
||||
Age = age;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets.HTTP
|
||||
namespace Esiur.Net.Packets.Http
|
||||
{
|
||||
public enum HTTPComposeOption : int
|
||||
public enum HttpComposeOption : int
|
||||
{
|
||||
AllCalculateLength,
|
||||
AllDontCalculateLength,
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets.HTTP
|
||||
namespace Esiur.Net.Packets.Http
|
||||
{
|
||||
public struct HTTPCookie
|
||||
public struct HttpCookie
|
||||
{
|
||||
public string Name;
|
||||
public string Value;
|
||||
@@ -13,7 +13,7 @@ namespace Esiur.Net.Packets.HTTP
|
||||
public bool HttpOnly;
|
||||
public string Domain;
|
||||
|
||||
public HTTPCookie(string name, string value)
|
||||
public HttpCookie(string name, string value)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
@@ -23,7 +23,7 @@ namespace Esiur.Net.Packets.HTTP
|
||||
Domain = null;
|
||||
}
|
||||
|
||||
public HTTPCookie(string name, string value, DateTime expires)
|
||||
public HttpCookie(string name, string value, DateTime expires)
|
||||
{
|
||||
Name = name;
|
||||
Value = value;
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets.HTTP
|
||||
namespace Esiur.Net.Packets.Http
|
||||
{
|
||||
public enum HTTPMethod : byte
|
||||
public enum HttpMethod : byte
|
||||
{
|
||||
GET,
|
||||
POST,
|
||||
|
||||
@@ -32,13 +32,13 @@ using Esiur.Data;
|
||||
using System.Net;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Esiur.Net.Packets.HTTP;
|
||||
public class HTTPRequestPacket : Packet
|
||||
namespace Esiur.Net.Packets.Http;
|
||||
public class HttpRequestPacket : Packet
|
||||
{
|
||||
|
||||
|
||||
public StringKeyList Query;
|
||||
public HTTPMethod Method;
|
||||
public HttpMethod Method;
|
||||
public StringKeyList Headers;
|
||||
|
||||
public bool WSMode;
|
||||
@@ -52,28 +52,28 @@ public class HTTPRequestPacket : Packet
|
||||
public byte[] Message;
|
||||
|
||||
|
||||
private HTTPMethod GetMethod(string method)
|
||||
private HttpMethod GetMethod(string method)
|
||||
{
|
||||
switch (method.ToLower())
|
||||
{
|
||||
case "get":
|
||||
return HTTPMethod.GET;
|
||||
return HttpMethod.GET;
|
||||
case "post":
|
||||
return HTTPMethod.POST;
|
||||
return HttpMethod.POST;
|
||||
case "head":
|
||||
return HTTPMethod.HEAD;
|
||||
return HttpMethod.HEAD;
|
||||
case "put":
|
||||
return HTTPMethod.PUT;
|
||||
return HttpMethod.PUT;
|
||||
case "delete":
|
||||
return HTTPMethod.DELETE;
|
||||
return HttpMethod.DELETE;
|
||||
case "options":
|
||||
return HTTPMethod.OPTIONS;
|
||||
return HttpMethod.OPTIONS;
|
||||
case "trace":
|
||||
return HTTPMethod.TRACE;
|
||||
return HttpMethod.TRACE;
|
||||
case "connect":
|
||||
return HTTPMethod.CONNECT;
|
||||
return HttpMethod.CONNECT;
|
||||
default:
|
||||
return HTTPMethod.UNKNOWN;
|
||||
return HttpMethod.UNKNOWN;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,7 +219,7 @@ public class HTTPRequestPacket : Packet
|
||||
}
|
||||
|
||||
// Post Content-Length
|
||||
if (Method == HTTPMethod.POST)
|
||||
if (Method == HttpMethod.POST)
|
||||
{
|
||||
try
|
||||
{
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Packets.HTTP
|
||||
namespace Esiur.Net.Packets.Http
|
||||
{
|
||||
public enum HTTPResponseCode : int
|
||||
public enum HttpResponseCode : int
|
||||
{
|
||||
Switching = 101,
|
||||
OK = 200,
|
||||
|
||||
@@ -28,18 +28,18 @@ using System.Text;
|
||||
using Esiur.Misc;
|
||||
using Esiur.Data;
|
||||
|
||||
namespace Esiur.Net.Packets.HTTP;
|
||||
public class HTTPResponsePacket : Packet
|
||||
namespace Esiur.Net.Packets.Http;
|
||||
public class HttpResponsePacket : Packet
|
||||
{
|
||||
|
||||
public StringKeyList Headers { get; } = new StringKeyList(true);
|
||||
public string Version { get; set; } = "HTTP/1.1";
|
||||
|
||||
public byte[] Message;
|
||||
public HTTPResponseCode Number { get; set; } = HTTPResponseCode.OK;
|
||||
public HttpResponseCode Number { get; set; } = HttpResponseCode.OK;
|
||||
public string Text;
|
||||
|
||||
public List<HTTPCookie> Cookies { get; } = new List<HTTPCookie>();
|
||||
public List<HttpCookie> Cookies { get; } = new List<HttpCookie>();
|
||||
public bool Handled;
|
||||
|
||||
public override string ToString()
|
||||
@@ -51,11 +51,11 @@ public class HTTPResponsePacket : Packet
|
||||
+ "\n\tMessage: " + (Message != null ? Message.Length.ToString() : "NULL");
|
||||
}
|
||||
|
||||
private string MakeHeader(HTTPComposeOption options)
|
||||
private string MakeHeader(HttpComposeOption options)
|
||||
{
|
||||
string header = $"{Version} {(int)Number} {Text}\r\nServer: Esiur {Global.Version}\r\nDate: {DateTime.Now.ToUniversalTime().ToString("r")}\r\n";
|
||||
|
||||
if (options == HTTPComposeOption.AllCalculateLength)
|
||||
if (options == HttpComposeOption.AllCalculateLength)
|
||||
Headers["Content-Length"] = Message?.Length.ToString() ?? "0";
|
||||
|
||||
foreach (var kv in Headers)
|
||||
@@ -75,16 +75,16 @@ public class HTTPResponsePacket : Packet
|
||||
}
|
||||
|
||||
|
||||
public bool Compose(HTTPComposeOption options)
|
||||
public bool Compose(HttpComposeOption options)
|
||||
{
|
||||
List<byte> msg = new List<byte>();
|
||||
|
||||
if (options != HTTPComposeOption.DataOnly)
|
||||
if (options != HttpComposeOption.DataOnly)
|
||||
{
|
||||
msg.AddRange(Encoding.UTF8.GetBytes(MakeHeader(options)));
|
||||
}
|
||||
|
||||
if (options != HTTPComposeOption.SpecifiedHeadersOnly)
|
||||
if (options != HttpComposeOption.SpecifiedHeadersOnly)
|
||||
{
|
||||
if (Message != null)
|
||||
msg.AddRange(Message);
|
||||
@@ -97,7 +97,7 @@ public class HTTPResponsePacket : Packet
|
||||
|
||||
public override bool Compose()
|
||||
{
|
||||
return Compose(HTTPComposeOption.AllDontCalculateLength);
|
||||
return Compose(HttpComposeOption.AllDontCalculateLength);
|
||||
}
|
||||
|
||||
public override long Parse(byte[] data, uint offset, uint ends)
|
||||
@@ -129,7 +129,7 @@ public class HTTPResponsePacket : Packet
|
||||
if (sMethod.Length == 3)
|
||||
{
|
||||
Version = sMethod[0].Trim();
|
||||
Number = (HTTPResponseCode)Convert.ToInt32(sMethod[1].Trim());
|
||||
Number = (HttpResponseCode)Convert.ToInt32(sMethod[1].Trim());
|
||||
Text = sMethod[2];
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ public class HTTPResponsePacket : Packet
|
||||
if (cookie.Length >= 1)
|
||||
{
|
||||
string[] splitCookie = cookie[0].Split('=');
|
||||
HTTPCookie c = new HTTPCookie(splitCookie[0], splitCookie[1]);
|
||||
HttpCookie c = new HttpCookie(splitCookie[0], splitCookie[1]);
|
||||
|
||||
for (int j = 1; j < cookie.Length; j++)
|
||||
{
|
||||
|
||||
249
Esiur/Net/Ppap/DeterministicGenerator.cs
Normal file
249
Esiur/Net/Ppap/DeterministicGenerator.cs
Normal file
@@ -0,0 +1,249 @@
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Prng;
|
||||
using Org.BouncyCastle.Security;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Ppap
|
||||
{
|
||||
public class DeterministicGenerator
|
||||
: SecureRandom
|
||||
{
|
||||
private static long counter = DateTime.UtcNow.Ticks;
|
||||
|
||||
public byte[] value;
|
||||
|
||||
private static long NextCounterValue()
|
||||
{
|
||||
return Interlocked.Increment(ref counter);
|
||||
}
|
||||
|
||||
private static readonly SecureRandom MasterRandom = new SecureRandom(new CryptoApiRandomGenerator());
|
||||
internal static readonly SecureRandom ArbitraryRandom = new SecureRandom(new VmpcRandomGenerator(), 16);
|
||||
|
||||
private static DigestRandomGenerator CreatePrng(string digestName, bool autoSeed)
|
||||
{
|
||||
IDigest digest = DigestUtilities.GetDigest(digestName);
|
||||
if (digest == null)
|
||||
return null;
|
||||
DigestRandomGenerator prng = new DigestRandomGenerator(digest);
|
||||
if (autoSeed)
|
||||
{
|
||||
AutoSeed(prng, 2 * digest.GetDigestSize());
|
||||
}
|
||||
return prng;
|
||||
}
|
||||
|
||||
public static new byte[] GetNextBytes(SecureRandom secureRandom, int length)
|
||||
{
|
||||
byte[] result = new byte[length];
|
||||
secureRandom.NextBytes(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static new SecureRandom GetInstance(string algorithm)
|
||||
{
|
||||
return GetInstance(algorithm, true);
|
||||
}
|
||||
|
||||
public static new SecureRandom GetInstance(string algorithm, bool autoSeed)
|
||||
{
|
||||
if (algorithm == null)
|
||||
throw new ArgumentNullException(nameof(algorithm));
|
||||
|
||||
if (algorithm.EndsWith("PRNG", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string digestName = algorithm.Substring(0, algorithm.Length - "PRNG".Length);
|
||||
|
||||
DigestRandomGenerator prng = CreatePrng(digestName, autoSeed);
|
||||
if (prng != null)
|
||||
return new SecureRandom(prng);
|
||||
}
|
||||
|
||||
throw new ArgumentException("Unrecognised PRNG algorithm: " + algorithm, "algorithm");
|
||||
}
|
||||
|
||||
protected new readonly IRandomGenerator generator;
|
||||
|
||||
public DeterministicGenerator(byte[] value)
|
||||
: this(CreatePrng("SHA256", true))
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public DeterministicGenerator(IRandomGenerator generator)
|
||||
|
||||
{
|
||||
this.generator = generator;
|
||||
}
|
||||
|
||||
public DeterministicGenerator(IRandomGenerator generator, int autoSeedLengthInBytes)
|
||||
|
||||
{
|
||||
AutoSeed(generator, autoSeedLengthInBytes);
|
||||
|
||||
this.generator = generator;
|
||||
}
|
||||
|
||||
public override byte[] GenerateSeed(int length)
|
||||
{
|
||||
return GetNextBytes(MasterRandom, length);
|
||||
}
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
public override void GenerateSeed(Span<byte> seed)
|
||||
{
|
||||
MasterRandom.NextBytes(seed);
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void SetSeed(byte[] seed)
|
||||
{
|
||||
generator.AddSeedMaterial(seed);
|
||||
}
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
public override void SetSeed(Span<byte> seed)
|
||||
{
|
||||
generator.AddSeedMaterial(seed);
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void SetSeed(long seed)
|
||||
{
|
||||
generator.AddSeedMaterial(seed);
|
||||
}
|
||||
|
||||
public override int Next()
|
||||
{
|
||||
return NextInt() & int.MaxValue;
|
||||
}
|
||||
|
||||
public override int Next(int maxValue)
|
||||
{
|
||||
if (maxValue < 2)
|
||||
{
|
||||
if (maxValue < 0)
|
||||
throw new ArgumentOutOfRangeException("maxValue", "cannot be negative");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bits;
|
||||
|
||||
// Test whether maxValue is a power of 2
|
||||
if ((maxValue & (maxValue - 1)) == 0)
|
||||
{
|
||||
bits = NextInt() & int.MaxValue;
|
||||
return (int)(((long)bits * maxValue) >> 31);
|
||||
}
|
||||
|
||||
int result;
|
||||
do
|
||||
{
|
||||
bits = NextInt() & int.MaxValue;
|
||||
result = bits % maxValue;
|
||||
}
|
||||
while (bits - result + (maxValue - 1) < 0); // Ignore results near overflow
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override int Next(int minValue, int maxValue)
|
||||
{
|
||||
if (maxValue <= minValue)
|
||||
{
|
||||
if (maxValue == minValue)
|
||||
return minValue;
|
||||
|
||||
throw new ArgumentException("maxValue cannot be less than minValue");
|
||||
}
|
||||
|
||||
int diff = maxValue - minValue;
|
||||
if (diff > 0)
|
||||
return minValue + Next(diff);
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
int i = NextInt();
|
||||
|
||||
if (i >= minValue && i < maxValue)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
public override void NextBytes(byte[] buf)
|
||||
{
|
||||
Buffer.BlockCopy(value, 0, buf, 0, buf.Length);
|
||||
}
|
||||
|
||||
public override void NextBytes(byte[] buf, int off, int len)
|
||||
{
|
||||
Buffer.BlockCopy(value, 0, buf, off, len);
|
||||
}
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
public override void NextBytes(Span<byte> buffer)
|
||||
{
|
||||
if (generator != null)
|
||||
{
|
||||
generator.NextBytes(buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] tmp = new byte[buffer.Length];
|
||||
NextBytes(tmp);
|
||||
tmp.CopyTo(buffer);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private static readonly double DoubleScale = 1.0 / Convert.ToDouble(1L << 53);
|
||||
|
||||
public override double NextDouble()
|
||||
{
|
||||
ulong x = (ulong)NextLong() >> 11;
|
||||
|
||||
return Convert.ToDouble(x) * DoubleScale;
|
||||
}
|
||||
|
||||
public override int NextInt()
|
||||
{
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
Span<byte> bytes = stackalloc byte[4];
|
||||
#else
|
||||
byte[] bytes = new byte[4];
|
||||
#endif
|
||||
NextBytes(bytes);
|
||||
return (int)0;
|
||||
}
|
||||
|
||||
public override long NextLong()
|
||||
{
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
Span<byte> bytes = stackalloc byte[8];
|
||||
#else
|
||||
byte[] bytes = new byte[8];
|
||||
#endif
|
||||
NextBytes(bytes);
|
||||
return (long)0;
|
||||
}
|
||||
|
||||
private static void AutoSeed(IRandomGenerator generator, int seedLength)
|
||||
{
|
||||
generator.AddSeedMaterial(NextCounterValue());
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
Span<byte> seed = seedLength <= 128
|
||||
? stackalloc byte[seedLength]
|
||||
: new byte[seedLength];
|
||||
#else
|
||||
byte[] seed = new byte[seedLength];
|
||||
#endif
|
||||
MasterRandom.NextBytes(seed);
|
||||
generator.AddSeedMaterial(seed);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
61
Esiur/Net/Ppap/KeyGenerator.cs
Normal file
61
Esiur/Net/Ppap/KeyGenerator.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
using Org.BouncyCastle.Crypto.Generators;
|
||||
using Org.BouncyCastle.Crypto.Parameters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Ppap
|
||||
{
|
||||
internal class KeyGenerator
|
||||
{
|
||||
public static (byte[], byte[]) Gen(MLKemParameters parameters = null)
|
||||
{
|
||||
var random = new RandomGenerator();
|
||||
var keyGenParameters = new MLKemKeyGenerationParameters(random, parameters ?? MLKemParameters.ml_kem_768);
|
||||
|
||||
var kyberKeyPairGenerator = new MLKemKeyPairGenerator();
|
||||
kyberKeyPairGenerator.Init(keyGenParameters);
|
||||
|
||||
var keys = kyberKeyPairGenerator.GenerateKeyPair();
|
||||
return ((keys.Private as MLKemPrivateKeyParameters).GetEncoded(),
|
||||
(keys.Public as MLKemPublicKeyParameters).GetEncoded());
|
||||
}
|
||||
|
||||
|
||||
public static (byte[], byte[]) GenS(byte[] username, byte[] password, byte[] registrationNonce, Argon2Parameters argon2parameters = null, MLKemParameters mlkemParameters = null)
|
||||
{
|
||||
|
||||
var secret = new byte[username.Length + password.Length + registrationNonce.Length];
|
||||
Buffer.BlockCopy(username, 0, secret, 0, username.Length);
|
||||
Buffer.BlockCopy(password, 0, secret, username.Length, password.Length);
|
||||
Buffer.BlockCopy(registrationNonce, 0, secret, username.Length + password.Length, registrationNonce.Length);
|
||||
|
||||
var output = new byte[64];
|
||||
//Argon2id.DeriveKey(output, secret, registrationNonce, ArgonIterations, ArgonMemory * 1024);
|
||||
|
||||
var argon2 = new Argon2BytesGenerator();
|
||||
var argon2params = argon2parameters ?? new Argon2Parameters.Builder(Argon2Parameters.Argon2id)
|
||||
.WithSalt(registrationNonce)
|
||||
.WithMemoryAsKB(1024 * 10)
|
||||
.WithIterations(3)
|
||||
.WithParallelism(1)
|
||||
.Build();
|
||||
|
||||
argon2.Init(argon2params);
|
||||
var output2 = new byte[64];
|
||||
argon2.GenerateBytes(secret, output);
|
||||
|
||||
|
||||
var random = new DeterministicGenerator(output);
|
||||
var keyGenParameters = new MLKemKeyGenerationParameters(random, mlkemParameters ?? MLKemParameters.ml_kem_768);
|
||||
|
||||
var kyberKeyPairGenerator = new MLKemKeyPairGenerator();
|
||||
kyberKeyPairGenerator.Init(keyGenParameters);
|
||||
|
||||
var keys = kyberKeyPairGenerator.GenerateKeyPair();
|
||||
return ((keys.Private as MLKemPrivateKeyParameters).GetEncoded(),
|
||||
(keys.Public as MLKemPublicKeyParameters).GetEncoded());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
247
Esiur/Net/Ppap/RandomGenerator.cs
Normal file
247
Esiur/Net/Ppap/RandomGenerator.cs
Normal file
@@ -0,0 +1,247 @@
|
||||
using Org.BouncyCastle.Crypto;
|
||||
using Org.BouncyCastle.Crypto.Prng;
|
||||
using Org.BouncyCastle.Security;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Esiur.Net.Ppap
|
||||
{
|
||||
public class RandomGenerator
|
||||
: SecureRandom
|
||||
{
|
||||
private static long counter = DateTime.UtcNow.Ticks;
|
||||
|
||||
private static long NextCounterValue()
|
||||
{
|
||||
return Interlocked.Increment(ref counter);
|
||||
}
|
||||
|
||||
private static readonly SecureRandom MasterRandom = new SecureRandom(new CryptoApiRandomGenerator());
|
||||
internal static readonly SecureRandom ArbitraryRandom = new SecureRandom(new VmpcRandomGenerator(), 16);
|
||||
|
||||
private static DigestRandomGenerator CreatePrng(string digestName, bool autoSeed)
|
||||
{
|
||||
IDigest digest = DigestUtilities.GetDigest(digestName);
|
||||
if (digest == null)
|
||||
return null;
|
||||
DigestRandomGenerator prng = new DigestRandomGenerator(digest);
|
||||
if (autoSeed)
|
||||
{
|
||||
AutoSeed(prng, 2 * digest.GetDigestSize());
|
||||
}
|
||||
return prng;
|
||||
}
|
||||
|
||||
public static new byte[] GetNextBytes(SecureRandom secureRandom, int length)
|
||||
{
|
||||
byte[] result = new byte[length];
|
||||
secureRandom.NextBytes(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static new SecureRandom GetInstance(string algorithm)
|
||||
{
|
||||
return GetInstance(algorithm, true);
|
||||
}
|
||||
|
||||
public static new SecureRandom GetInstance(string algorithm, bool autoSeed)
|
||||
{
|
||||
if (algorithm == null)
|
||||
throw new ArgumentNullException(nameof(algorithm));
|
||||
|
||||
if (algorithm.EndsWith("PRNG", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
string digestName = algorithm.Substring(0, algorithm.Length - "PRNG".Length);
|
||||
|
||||
DigestRandomGenerator prng = CreatePrng(digestName, autoSeed);
|
||||
if (prng != null)
|
||||
return new SecureRandom(prng);
|
||||
}
|
||||
|
||||
throw new ArgumentException("Unrecognised PRNG algorithm: " + algorithm, "algorithm");
|
||||
}
|
||||
|
||||
protected new readonly IRandomGenerator generator;
|
||||
|
||||
public RandomGenerator()
|
||||
: this(CreatePrng("SHA256", true))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public RandomGenerator(IRandomGenerator generator)
|
||||
|
||||
{
|
||||
this.generator = generator;
|
||||
}
|
||||
|
||||
public RandomGenerator(IRandomGenerator generator, int autoSeedLengthInBytes)
|
||||
|
||||
{
|
||||
AutoSeed(generator, autoSeedLengthInBytes);
|
||||
|
||||
this.generator = generator;
|
||||
}
|
||||
|
||||
public override byte[] GenerateSeed(int length)
|
||||
{
|
||||
return GetNextBytes(MasterRandom, length);
|
||||
}
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
public override void GenerateSeed(Span<byte> seed)
|
||||
{
|
||||
MasterRandom.NextBytes(seed);
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void SetSeed(byte[] seed)
|
||||
{
|
||||
generator.AddSeedMaterial(seed);
|
||||
}
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
public override void SetSeed(Span<byte> seed)
|
||||
{
|
||||
generator.AddSeedMaterial(seed);
|
||||
}
|
||||
#endif
|
||||
|
||||
public override void SetSeed(long seed)
|
||||
{
|
||||
generator.AddSeedMaterial(seed);
|
||||
}
|
||||
|
||||
public override int Next()
|
||||
{
|
||||
return NextInt() & int.MaxValue;
|
||||
}
|
||||
|
||||
public override int Next(int maxValue)
|
||||
{
|
||||
if (maxValue < 2)
|
||||
{
|
||||
if (maxValue < 0)
|
||||
throw new ArgumentOutOfRangeException("maxValue", "cannot be negative");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int bits;
|
||||
|
||||
// Test whether maxValue is a power of 2
|
||||
if ((maxValue & (maxValue - 1)) == 0)
|
||||
{
|
||||
bits = NextInt() & int.MaxValue;
|
||||
return (int)(((long)bits * maxValue) >> 31);
|
||||
}
|
||||
|
||||
int result;
|
||||
do
|
||||
{
|
||||
bits = NextInt() & int.MaxValue;
|
||||
result = bits % maxValue;
|
||||
}
|
||||
while (bits - result + (maxValue - 1) < 0); // Ignore results near overflow
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public override int Next(int minValue, int maxValue)
|
||||
{
|
||||
if (maxValue <= minValue)
|
||||
{
|
||||
if (maxValue == minValue)
|
||||
return minValue;
|
||||
|
||||
throw new ArgumentException("maxValue cannot be less than minValue");
|
||||
}
|
||||
|
||||
int diff = maxValue - minValue;
|
||||
if (diff > 0)
|
||||
return minValue + Next(diff);
|
||||
|
||||
for (; ; )
|
||||
{
|
||||
int i = NextInt();
|
||||
|
||||
if (i >= minValue && i < maxValue)
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
public override void NextBytes(byte[] buf)
|
||||
{
|
||||
generator.NextBytes(buf);
|
||||
}
|
||||
|
||||
public override void NextBytes(byte[] buf, int off, int len)
|
||||
{
|
||||
generator.NextBytes(buf, off, len);
|
||||
}
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
public override void NextBytes(Span<byte> buffer)
|
||||
{
|
||||
if (generator != null)
|
||||
{
|
||||
generator.NextBytes(buffer);
|
||||
}
|
||||
else
|
||||
{
|
||||
byte[] tmp = new byte[buffer.Length];
|
||||
NextBytes(tmp);
|
||||
tmp.CopyTo(buffer);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
private static readonly double DoubleScale = 1.0 / Convert.ToDouble(1L << 53);
|
||||
|
||||
public override double NextDouble()
|
||||
{
|
||||
ulong x = (ulong)NextLong() >> 11;
|
||||
|
||||
return Convert.ToDouble(x) * DoubleScale;
|
||||
}
|
||||
|
||||
public override int NextInt()
|
||||
{
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
Span<byte> bytes = stackalloc byte[4];
|
||||
#else
|
||||
byte[] bytes = new byte[4];
|
||||
#endif
|
||||
NextBytes(bytes);
|
||||
return (int)0;
|
||||
}
|
||||
|
||||
public override long NextLong()
|
||||
{
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
Span<byte> bytes = stackalloc byte[8];
|
||||
#else
|
||||
byte[] bytes = new byte[8];
|
||||
#endif
|
||||
NextBytes(bytes);
|
||||
return (long)0;
|
||||
}
|
||||
|
||||
private static void AutoSeed(IRandomGenerator generator, int seedLength)
|
||||
{
|
||||
generator.AddSeedMaterial(NextCounterValue());
|
||||
|
||||
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
|
||||
Span<byte> seed = seedLength <= 128
|
||||
? stackalloc byte[seedLength]
|
||||
: new byte[seedLength];
|
||||
#else
|
||||
byte[] seed = new byte[seedLength];
|
||||
#endif
|
||||
MasterRandom.NextBytes(seed);
|
||||
generator.AddSeedMaterial(seed);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -32,13 +32,13 @@ using System.Collections;
|
||||
using Esiur.Misc;
|
||||
using Esiur.Data;
|
||||
|
||||
namespace Esiur.Net.TCP;
|
||||
public class TCPConnection : NetworkConnection
|
||||
namespace Esiur.Net.Tcp;
|
||||
public class TcpConnection : NetworkConnection
|
||||
{
|
||||
|
||||
private KeyList<string, object> variables = new KeyList<string, object>();
|
||||
|
||||
public TCPServer Server { get; internal set; }
|
||||
public TcpServer Server { get; internal set; }
|
||||
|
||||
public KeyList<string, object> Variables
|
||||
{
|
||||
|
||||
@@ -32,9 +32,9 @@ using Esiur.Net.Sockets;
|
||||
using Esiur.Core;
|
||||
using Esiur.Resource;
|
||||
|
||||
namespace Esiur.Net.TCP;
|
||||
namespace Esiur.Net.Tcp;
|
||||
|
||||
public abstract class TCPFilter : IResource
|
||||
public abstract class TcpFilter : IResource
|
||||
{
|
||||
|
||||
public Instance Instance
|
||||
@@ -48,17 +48,17 @@ public abstract class TCPFilter : IResource
|
||||
|
||||
public abstract AsyncReply<bool> Trigger(ResourceTrigger trigger);
|
||||
|
||||
public virtual bool Connected(TCPConnection sender)
|
||||
public virtual bool Connected(TcpConnection sender)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public virtual bool Disconnected(TCPConnection sender)
|
||||
public virtual bool Disconnected(TcpConnection sender)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public abstract bool Execute(byte[] msg, NetworkBuffer data, TCPConnection sender);
|
||||
public abstract bool Execute(byte[] msg, NetworkBuffer data, TcpConnection sender);
|
||||
|
||||
public void Destroy()
|
||||
{
|
||||
|
||||
@@ -34,8 +34,8 @@ using Esiur.Core;
|
||||
using System.Net;
|
||||
using Esiur.Resource;
|
||||
|
||||
namespace Esiur.Net.TCP;
|
||||
public class TCPServer : NetworkServer<TCPConnection>, IResource
|
||||
namespace Esiur.Net.Tcp;
|
||||
public class TcpServer : NetworkServer<TcpConnection>, IResource
|
||||
{
|
||||
|
||||
[Attribute]
|
||||
@@ -64,7 +64,7 @@ public class TCPServer : NetworkServer<TCPConnection>, IResource
|
||||
//}
|
||||
public Instance Instance { get; set; }
|
||||
|
||||
TCPFilter[] filters = null;
|
||||
TcpFilter[] filters = null;
|
||||
|
||||
public AsyncReply<bool> Trigger(ResourceTrigger trigger)
|
||||
{
|
||||
@@ -93,7 +93,7 @@ public class TCPServer : NetworkServer<TCPConnection>, IResource
|
||||
}
|
||||
else if (trigger == ResourceTrigger.SystemInitialized)
|
||||
{
|
||||
Instance.Children<TCPFilter>().Then(x => filters = x);
|
||||
Instance.Children<TcpFilter>().Then(x => filters = x);
|
||||
}
|
||||
|
||||
return new AsyncReply<bool>(true);
|
||||
@@ -103,7 +103,7 @@ public class TCPServer : NetworkServer<TCPConnection>, IResource
|
||||
|
||||
|
||||
|
||||
internal bool Execute(TCPConnection sender, NetworkBuffer data)
|
||||
internal bool Execute(TcpConnection sender, NetworkBuffer data)
|
||||
{
|
||||
|
||||
if (filters == null)
|
||||
@@ -120,12 +120,12 @@ public class TCPServer : NetworkServer<TCPConnection>, IResource
|
||||
return false;
|
||||
}
|
||||
|
||||
private void SessionModified(TCPConnection session, string key, object newValue)
|
||||
private void SessionModified(TcpConnection session, string key, object newValue)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
protected override void ClientDisconnected(TCPConnection connection)
|
||||
protected override void ClientDisconnected(TcpConnection connection)
|
||||
{
|
||||
if (filters == null)
|
||||
return;
|
||||
@@ -136,19 +136,19 @@ public class TCPServer : NetworkServer<TCPConnection>, IResource
|
||||
}
|
||||
}
|
||||
|
||||
public override void Add(TCPConnection connection)
|
||||
public override void Add(TcpConnection connection)
|
||||
{
|
||||
connection.Server = this;
|
||||
base.Add(connection);
|
||||
}
|
||||
|
||||
public override void Remove(TCPConnection connection)
|
||||
public override void Remove(TcpConnection connection)
|
||||
{
|
||||
connection.Server = null;
|
||||
base.Remove(connection);
|
||||
}
|
||||
|
||||
protected override void ClientConnected(TCPConnection connection)
|
||||
protected override void ClientConnected(TcpConnection connection)
|
||||
{
|
||||
if (filters == null)
|
||||
return;
|
||||
|
||||
@@ -28,8 +28,8 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Esiur.Net.TCP;
|
||||
public class TCPSession : NetworkSession
|
||||
namespace Esiur.Net.Tcp;
|
||||
public class TcpSession : NetworkSession
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ using Esiur.Core;
|
||||
using Esiur.Resource;
|
||||
|
||||
namespace Esiur.Net.UDP;
|
||||
public abstract class UDPFilter : IResource
|
||||
public abstract class UdpFilter : IResource
|
||||
{
|
||||
|
||||
|
||||
|
||||
@@ -40,11 +40,11 @@ namespace Esiur.Net.UDP;
|
||||
public EndPoint SenderPoint;
|
||||
public
|
||||
}*/
|
||||
public class UDPServer : IResource
|
||||
public class UdpServer : IResource
|
||||
{
|
||||
Thread receiver;
|
||||
UdpClient udp;
|
||||
UDPFilter[] filters = new UDPFilter[0];
|
||||
UdpFilter[] filters = new UdpFilter[0];
|
||||
|
||||
public event DestroyedEvent OnDestroy;
|
||||
|
||||
@@ -80,7 +80,7 @@ public class UDPServer : IResource
|
||||
|
||||
foreach (var child in filters)
|
||||
{
|
||||
var f = child as UDPFilter;
|
||||
var f = child as UdpFilter;
|
||||
|
||||
try
|
||||
{
|
||||
@@ -195,7 +195,7 @@ public class UDPServer : IResource
|
||||
}
|
||||
else if (trigger == ResourceTrigger.SystemInitialized)
|
||||
{
|
||||
filters = await Instance.Children<UDPFilter>();
|
||||
filters = await Instance.Children<UdpFilter>();
|
||||
}
|
||||
|
||||
return true;
|
||||
|
||||
Reference in New Issue
Block a user