2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-26 21:13:13 +00:00
This commit is contained in:
2019-08-07 05:18:27 +03:00
parent 2caae61910
commit 8d06fd05ad
74 changed files with 2302 additions and 1336 deletions

View File

@ -26,7 +26,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Data;
using Esiur.Net.Packets;
using Esiur.Resource;

View File

@ -26,7 +26,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Data;
using System.Runtime.InteropServices;
using Esiur.Net.Packets;

View File

@ -27,7 +27,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Resource;
namespace Esiur.Net.DataLink

View File

@ -32,7 +32,7 @@ using System.Net;
using System.Collections;
using System.Collections.Generic;
using Esiur.Data;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Resource;
namespace Esiur.Net.HTTP

View File

@ -34,7 +34,7 @@ using System.Collections.Generic;
using Esiur.Net.Sockets;
using Esiur.Data;
using Esiur.Misc;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Net.Packets;
using System.Security.Cryptography.X509Certificates;
using Esiur.Resource;

View File

@ -33,7 +33,7 @@ using System.Collections;
using System.Collections.Generic;
using Esiur.Data;
using Esiur.Misc;
using Esiur.Engine;
using Esiur.Core;
namespace Esiur.Net.HTTP
{

View File

@ -30,7 +30,7 @@ using System.Threading.Tasks;
using Esiur.Resource;
using Esiur.Net.IIP;
using Esiur.Net.Sockets;
using Esiur.Engine;
using Esiur.Core;
namespace Esiur.Net.HTTP
{

View File

@ -31,7 +31,7 @@ using System.Security.Cryptography;
using Esiur.Net.Sockets;
using Esiur.Data;
using Esiur.Misc;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Net.Packets;
using Esiur.Resource;
using Esiur.Security.Authority;
@ -62,6 +62,8 @@ namespace Esiur.Net.IIP
Session session;
AsyncReply<bool> openReply;
byte[] localPassword;
byte[] localNonce, remoteNonce;
@ -109,8 +111,11 @@ namespace Esiur.Net.IIP
/// Send data to the other end as parameters
/// </summary>
/// <param name="values">Values will be converted to bytes then sent.</param>
internal void SendParams(params object[] values)
internal SendList SendParams(IAsyncReply<object[]> reply = null)//params object[] values)
{
return new SendList(this, reply);
/*
var data = BinaryList.ToBytes(values);
if (ready)
@ -149,6 +154,7 @@ namespace Esiur.Net.IIP
// Get calling method name
//Console.WriteLine("TX " + hostType + " " + ar.Length + " " + stackTrace.GetFrame(1).GetMethod().ToString());
*/
}
/// <summary>
@ -199,13 +205,28 @@ namespace Esiur.Net.IIP
if (socket.State == SocketState.Established)
{
SendParams((byte)0x60, (byte)dmn.Length, dmn, localNonce, (byte)un.Length, un);
SendParams()
.AddUInt8(0x60)
.AddUInt8((byte)dmn.Length)
.AddUInt8Array(dmn)
.AddUInt8Array(localNonce)
.AddUInt8((byte)un.Length)
.AddUInt8Array(un)
.Done();//, dmn, localNonce, (byte)un.Length, un);
}
else
{
socket.OnConnect += () =>
{ // declare (Credentials -> No Auth, No Enctypt)
SendParams((byte)0x60, (byte)dmn.Length, dmn, localNonce, (byte)un.Length, un);
//SendParams((byte)0x60, (byte)dmn.Length, dmn, localNonce, (byte)un.Length, un);
SendParams()
.AddUInt8(0x60)
.AddUInt8((byte)dmn.Length)
.AddUInt8Array(dmn)
.AddUInt8Array(localNonce)
.AddUInt8((byte)un.Length)
.AddUInt8Array(un)
.Done();
};
}
}
@ -569,12 +590,17 @@ namespace Esiur.Net.IIP
session.RemoteAuthentication.Username = authPacket.RemoteUsername;
remoteNonce = authPacket.RemoteNonce;
session.RemoteAuthentication.Domain = authPacket.Domain;
SendParams((byte)0xa0, localNonce);
SendParams()
.AddUInt8(0xa0)
.AddUInt8Array(localNonce)
.Done();
//SendParams((byte)0xa0, localNonce);
}
else
{
//Console.WriteLine("User not found");
SendParams((byte)0xc0, (byte)1, (ushort)14, DC.ToBytes("User not found"));
//SendParams((byte)0xc0, (byte)1, (ushort)14, DC.ToBytes("User not found"));
SendParams().AddUInt8(0xc0).AddUInt8(1).AddUInt16(14).AddString("User not found").Done();
}
});
@ -592,21 +618,29 @@ namespace Esiur.Net.IIP
if (pw != null)
{
var hashFunc = SHA256.Create();
var hash = hashFunc.ComputeHash(BinaryList.ToBytes(pw, remoteNonce, localNonce));
//var hash = hashFunc.ComputeHash(BinaryList.ToBytes(pw, remoteNonce, localNonce));
var hash = hashFunc.ComputeHash((new BinaryList())
.AddUInt8Array(pw)
.AddUInt8Array( remoteNonce)
.AddUInt8Array(localNonce)
.ToArray());
if (hash.SequenceEqual(remoteHash))
{
// send our hash
var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localNonce, remoteNonce, pw));
// send our hash
//var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localNonce, remoteNonce, pw));
//SendParams((byte)0, localHash);
SendParams((byte)0, localHash);
var localHash = hashFunc.ComputeHash((new BinaryList()).AddUInt8Array(localNonce).AddUInt8Array(remoteNonce).AddUInt8Array(pw).ToArray());
SendParams().AddUInt8(0).AddUInt8Array(localHash).Done();
readyToEstablish = true;
}
else
{
Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:DENIED");
//Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:DENIED");
//Console.WriteLine("Incorrect password");
SendParams((byte)0xc0, (byte)1, (ushort)5, DC.ToBytes("Error"));
//SendParams((byte)0xc0, (byte)1, (ushort)5, DC.ToBytes("Error"));
SendParams().AddUInt8(0xc0).AddUInt8(1).AddUInt16(5).AddString("Error").Done();
}
}
});
@ -618,12 +652,18 @@ namespace Esiur.Net.IIP
var r = new Random();
session.Id = new byte[32];
r.NextBytes(session.Id);
SendParams((byte)0x28, session.Id);
//SendParams((byte)0x28, session.Id);
SendParams()
.AddUInt8(0x28)
.AddUInt8Array(session.Id)
.Done();
ready = true;
openReply?.Trigger(true);
OnReady?.Invoke(this);
Server.Membership.Login(session);
Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:AUTH");
//Global.Log("auth", LogType.Warning, "U:" + RemoteUsername + " IP:" + Socket.RemoteEndPoint.Address.ToString() + " S:AUTH");
}
}
@ -637,9 +677,19 @@ namespace Esiur.Net.IIP
// send our hash
var hashFunc = SHA256.Create();
var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localPassword, localNonce, remoteNonce));
//var localHash = hashFunc.ComputeHash(BinaryList.ToBytes(localPassword, localNonce, remoteNonce));
var localHash = hashFunc.ComputeHash(new BinaryList()
.AddUInt8Array(localPassword)
.AddUInt8Array(localNonce)
.AddUInt8Array(remoteNonce)
.ToArray());
SendParams((byte)0, localHash);
SendParams()
.AddUInt8(0)
.AddUInt8Array(localHash)
.Done();
//SendParams((byte)0, localHash);
}
else if (authPacket.Command == IIPAuthPacket.IIPAuthPacketCommand.Action)
{
@ -647,16 +697,33 @@ namespace Esiur.Net.IIP
{
// check if the server knows my password
var hashFunc = SHA256.Create();
var remoteHash = hashFunc.ComputeHash(BinaryList.ToBytes(remoteNonce, localNonce, localPassword));
//var remoteHash = hashFunc.ComputeHash(BinaryList.ToBytes(remoteNonce, localNonce, localPassword));
var remoteHash = hashFunc.ComputeHash(new BinaryList()
.AddUInt8Array(remoteNonce)
.AddUInt8Array(localNonce)
.AddUInt8Array(localPassword)
.ToArray());
if (remoteHash.SequenceEqual(authPacket.Hash))
{
// send establish request
SendParams((byte)0x20, (ushort)0);
//SendParams((byte)0x20, (ushort)0);
SendParams()
.AddUInt8(0x20)
.AddUInt16(0)
.Done();
}
else
{
SendParams((byte)0xc0, 1, (ushort)5, DC.ToBytes("Error"));
SendParams()
.AddUInt8(0xc0)
.AddUInt8(1)
.AddUInt16(5)
.AddString("Error")
.Done();
//SendParams((byte)0xc0, 1, (ushort)5, DC.ToBytes("Error"));
}
}
else if (authPacket.Action == IIPAuthPacket.IIPAuthPacketAction.ConnectionEstablished)
@ -664,12 +731,14 @@ namespace Esiur.Net.IIP
session.Id = authPacket.SessionId;
ready = true;
openReply?.Trigger(true);
OnReady?.Invoke(this);
}
}
else if (authPacket.Command == IIPAuthPacket.IIPAuthPacketCommand.Error)
{
openReply?.TriggerError(new AsyncException(ErrorType.Management, authPacket.ErrorCode, authPacket.ErrorMessage));
OnError?.Invoke(this, authPacket.ErrorCode, authPacket.ErrorMessage);
Close();
}
@ -710,6 +779,40 @@ namespace Esiur.Net.IIP
/// <returns></returns>
public AsyncReply<bool> Trigger(ResourceTrigger trigger)
{
if (trigger == ResourceTrigger.Open)
{
if (Instance.Attributes.ContainsKey("username")
&& Instance.Attributes.ContainsKey("password"))
{
var hostname = String.Join("://", Instance.Name.Split(new string[] { "://" }, StringSplitOptions.None).Skip(1)).Split('/')[0];
// assign domain from hostname if not provided
var address = hostname.Split(':')[0];
var port = ushort.Parse(hostname.Split(':')[1]);
var username = Instance.Attributes["username"].ToString();
var domain = Instance.Attributes.ContainsKey("domain") ? Instance.Attributes["domain"].ToString() : address;
session = new Session(new ClientAuthentication()
, new HostAuthentication());
session.LocalAuthentication.Domain = domain;
session.LocalAuthentication.Username = username;
localPassword = DC.ToBytes(Instance.Attributes["password"].ToString());
openReply = new AsyncReply<bool>();
var sock = new TCPSocket();
sock.Connect(domain, port).Then((x)=> {
Assign(sock);
//rt.trigger(true);
}).Error((x) => openReply.TriggerError(x));
return openReply;
}
}
return new AsyncReply<bool>();
}
@ -720,7 +823,9 @@ namespace Esiur.Net.IIP
/// <returns></returns>
public bool Put(IResource resource)
{
resources.Add(Convert.ToUInt32(resource.Instance.Name), (DistributedResource)resource);
if (Codec.IsLocalResource(resource, this))
resources.Add((resource as DistributedResource).Id, (DistributedResource)resource);
// else ... send it to the peer
return true;
}

File diff suppressed because it is too large Load Diff

View File

@ -33,7 +33,7 @@ using Esiur.Misc;
using Esiur.Data;
using System.Dynamic;
using System.Security.Cryptography;
using Esiur.Engine;
using Esiur.Core;
using System.Runtime.CompilerServices;
using System.Reflection.Emit;
using System.Linq;
@ -229,7 +229,7 @@ namespace Esiur.Net.IIP
internal void _EmitEventByIndex(byte index, object[] args)
{
var et = Instance.Template.GetEventTemplate(index);
var et = Instance.Template.GetEventTemplateByIndex(index);
events[index]?.Invoke(this, args);
Instance.EmitResourceEvent(null, null, et.Name, args);
}
@ -261,7 +261,7 @@ namespace Esiur.Net.IIP
public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result)
{
var ft = Instance.Template.GetFunctionTemplate(binder.Name);
var ft = Instance.Template.GetFunctionTemplateByName(binder.Name);
var reply = new AsyncReply<object>();
@ -325,7 +325,7 @@ namespace Esiur.Net.IIP
if (!isAttached)
return false;
var pt = Instance.Template.GetPropertyTemplate(binder.Name);
var pt = Instance.Template.GetPropertyTemplateByName(binder.Name);
if (pt != null)
{
@ -334,7 +334,7 @@ namespace Esiur.Net.IIP
}
else
{
var et = Instance.Template.GetEventTemplate(binder.Name);
var et = Instance.Template.GetEventTemplateByName(binder.Name);
if (et == null)
return false;
@ -347,7 +347,7 @@ namespace Esiur.Net.IIP
internal void _UpdatePropertyByIndex(byte index, object value)
{
var pt = Instance.Template.GetPropertyTemplate(index);
var pt = Instance.Template.GetPropertyTemplateByIndex(index);
properties[index] = value;
Instance.EmitModification(pt, value);
}
@ -366,12 +366,18 @@ namespace Esiur.Net.IIP
var reply = new AsyncReply<object>();
var parameters = Codec.Compose(value, connection);
connection.SendRequest(Packets.IIPPacket.IIPPacketAction.SetProperty, instanceId, index, parameters).Then((res) =>
{
// not really needed, server will always send property modified, this only happens if the programmer forgot to emit in property setter
properties[index] = value;
reply.Trigger(null);
});
connection.SendRequest(Packets.IIPPacket.IIPPacketAction.SetProperty)
.AddUInt32(instanceId)
.AddUInt8(index)
.AddUInt8Array(parameters)
.Done()
.Then((res) =>
{
// not really needed, server will always send property modified,
// this only happens if the programmer forgot to emit in property setter
properties[index] = value;
reply.Trigger(null);
});
return reply;
}
@ -384,7 +390,7 @@ namespace Esiur.Net.IIP
if (!isAttached)
return false;
var pt = Instance.Template.GetPropertyTemplate(binder.Name);
var pt = Instance.Template.GetPropertyTemplateByName(binder.Name);
if (pt != null)
{
@ -393,7 +399,7 @@ namespace Esiur.Net.IIP
}
else
{
var et = Instance.Template.GetEventTemplate(binder.Name);
var et = Instance.Template.GetEventTemplateByName(binder.Name);
if (et == null)
return false;

View File

@ -30,7 +30,7 @@ using Esiur.Net.Sockets;
using Esiur.Misc;
using System.Threading;
using Esiur.Data;
using Esiur.Engine;
using Esiur.Core;
using System.Net;
using Esiur.Resource;
using Esiur.Security.Membership;
@ -128,7 +128,7 @@ namespace Esiur.Net.IIP
protected override void ClientConnected(DistributedConnection sender)
{
//Console.WriteLine("DistributedConnection Client Connected");
Console.WriteLine("DistributedConnection Client Connected");
}

View File

@ -31,7 +31,7 @@ using System.Net;
using System.Collections;
using System.Collections.Generic;
using Esiur.Misc;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Data;
using Esiur.Net.Sockets;
using Esiur.Resource;

View File

@ -27,7 +27,7 @@ using System.Threading;
using System.Collections.Generic;
using Esiur.Data;
using Esiur.Misc;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Net.Sockets;
using Esiur.Resource;
using System.Threading.Tasks;

View File

@ -33,7 +33,7 @@ using System.Collections;
using System.Collections.Generic;
using Esiur.Data;
using Esiur.Misc;
using Esiur.Engine;
using Esiur.Core;
namespace Esiur.Net
{

View File

@ -23,7 +23,7 @@ SOFTWARE.
*/
using Esiur.Data;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Misc;
using Esiur.Net.Packets;
using System;

26
Esiur/Net/SendList.cs Normal file
View File

@ -0,0 +1,26 @@
using Esiur.Core;
using Esiur.Data;
using System;
using System.Collections.Generic;
using System.Text;
namespace Esiur.Net
{
public class SendList : BinaryList
{
NetworkConnection connection;
IAsyncReply<object[]> reply;
public SendList(NetworkConnection connection, IAsyncReply<object[]> reply)
{
this.reply = reply;
this.connection = connection;
}
public override IAsyncReply<object[]> Done()
{
connection.Send(this.ToArray());
return reply;
}
}
}

View File

@ -34,7 +34,7 @@ using Esiur.Data;
using Esiur.Misc;
using System.Collections.Concurrent;
using Esiur.Resource;
using Esiur.Engine;
using Esiur.Core;
namespace Esiur.Net.Sockets
{
@ -53,7 +53,7 @@ namespace Esiur.Net.Sockets
void Send(byte[] message);
void Send(byte[] message, int offset, int size);
void Close();
bool Connect(string hostname, ushort port);
AsyncReply<bool> Connect(string hostname, ushort port);
bool Begin();
//ISocket Accept();
AsyncReply<ISocket> Accept();

View File

@ -29,7 +29,7 @@ using System.Text;
using System.Net.Sockets;
using System.Net;
using Esiur.Misc;
using Esiur.Engine;
using Esiur.Core;
using System.Threading;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
@ -77,20 +77,29 @@ namespace Esiur.Net.Sockets
}
}
public bool Connect(string hostname, ushort port)
public AsyncReply<bool> Connect(string hostname, ushort port)
{
var rt = new AsyncReply<bool>();
try
{
this.hostname = hostname;
server = false;
state = SocketState.Connecting;
sock.ConnectAsync(hostname, port).ContinueWith(Connected);
return true;
sock.ConnectAsync(hostname, port).ContinueWith((x) =>
{
if (x.IsFaulted)
rt.TriggerError(x.Exception);
else
rt.Trigger(true);
Connected(x);
});
}
catch
catch (Exception ex)
{
return false;
rt.TriggerError(ex);
}
return rt;
}
private void DataSent(Task task)

View File

@ -29,7 +29,7 @@ using System.Text;
using System.Net.Sockets;
using System.Net;
using Esiur.Misc;
using Esiur.Engine;
using Esiur.Core;
using System.Threading;
using Esiur.Resource;
using System.Threading.Tasks;
@ -62,13 +62,6 @@ namespace Esiur.Net.Sockets
SocketAsyncEventArgs socketArgs = new SocketAsyncEventArgs();
private void Connected(Task t)
{
state = SocketState.Established;
OnConnect?.Invoke();
Begin();
}
public bool Begin()
{
if (began)
@ -86,18 +79,34 @@ namespace Esiur.Net.Sockets
return true;
}
public bool Connect(string hostname, ushort port)
public AsyncReply<bool> Connect(string hostname, ushort port)
{
var rt = new AsyncReply<bool>();
try
{
state = SocketState.Connecting;
sock.ConnectAsync(hostname, port).ContinueWith(Connected);
return true;
sock.ConnectAsync(hostname, port).ContinueWith((x) =>
{
if (x.IsFaulted)
rt.TriggerError(x.Exception);
else
{
state = SocketState.Established;
OnConnect?.Invoke();
Begin();
rt.Trigger(true);
}
});
}
catch
catch (Exception ex)
{
return false;
rt.TriggerError(ex);
}
return rt;
}
@ -117,7 +126,7 @@ namespace Esiur.Net.Sockets
}
//if (receiveNetworkBuffer.Protected)
// Console.WriteLine();
// Console.WriteLine();
//lock (receiveNetworkBuffer.SyncLock)
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)task.Result);
@ -128,7 +137,7 @@ namespace Esiur.Net.Sockets
if (state == SocketState.Established)
{
sock.ReceiveAsync(receiveBufferSegment, SocketFlags.None).ContinueWith(DataReceived);
}
}
@ -149,7 +158,7 @@ namespace Esiur.Net.Sockets
try
{
// SocketError err;
if (state == SocketState.Closed || state == SocketState.Terminated)
return;
@ -162,7 +171,7 @@ namespace Esiur.Net.Sockets
//if (receiveNetworkBuffer.Protected)
// Console.WriteLine();
//lock (receiveNetworkBuffer.SyncLock)
receiveNetworkBuffer.Write(receiveBuffer, 0, (uint)e.BytesTransferred);

View File

@ -31,7 +31,7 @@ using System.Net;
using Esiur.Net.Packets;
using Esiur.Misc;
using System.IO;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Resource;
using Esiur.Data;
@ -236,7 +236,7 @@ namespace Esiur.Net.Sockets
sock.Close();
}
public bool Connect(string hostname, ushort port)
public AsyncReply<bool> Connect(string hostname, ushort port)
{
throw new NotImplementedException();
}

View File

@ -29,7 +29,7 @@ using System.Text;
using System.Collections;
using Esiur.Data;
using Esiur.Net.Sockets;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Resource;
namespace Esiur.Net.TCP

View File

@ -30,7 +30,7 @@ using Esiur.Net.Sockets;
using Esiur.Misc;
using System.Threading;
using Esiur.Data;
using Esiur.Engine;
using Esiur.Core;
using System.Net;
using Esiur.Resource;

View File

@ -29,7 +29,7 @@ using System.Text;
using System.Collections;
using System.Net;
using Esiur.Data;
using Esiur.Engine;
using Esiur.Core;
using Esiur.Resource;
namespace Esiur.Net.UDP