Untrusted channel exception solved

This commit is contained in:
2025-05-03 23:09:32 +03:00
parent 8010677a8b
commit eede4d28c4
7 changed files with 55 additions and 12 deletions
+2
View File
@@ -17,6 +17,7 @@ namespace mROA.Codegen
new mROA.Implementation.AsyncMethodInvoker
{
IsVoid = <!L isVoid>,
IsTrusted = <!L isTrusted>,
ReturnType = typeof(<!L returnType>),
ParameterTypes = new Type[] { <!L parametersType> },
SuitableType = typeof(<!L suitableType>),
@@ -26,6 +27,7 @@ namespace mROA.Codegen
new mROA.Implementation.MethodInvoker
{
IsVoid = <!L isVoid>,
IsTrusted = <!L isTrusted>,
ReturnType = typeof(<!L returnType>),
ParameterTypes = new Type[] { <!L parametersType> },
SuitableType = typeof(<!L suitableType>),
+11
View File
@@ -380,6 +380,7 @@ namespace mROA.Codegen
invokerTemplate.AddDefine("parametersType", parameterTypes);
invokerTemplate.AddDefine("suitableType", baseInterace.ToUnityString());
invokerTemplate.AddDefine("funcInvoking", funcInvoking);
invokerTemplate.AddDefine("isTrusted", (!isUntrusted).ToString().ToLower());
backend = invokerTemplate.Compile();
}
else
@@ -390,6 +391,7 @@ namespace mROA.Codegen
invokerTemplate.AddDefine("parametersType", parameterTypes);
invokerTemplate.AddDefine("suitableType", baseInterace.ToUnityString());
invokerTemplate.AddDefine("funcInvoking", funcInvoking);
invokerTemplate.AddDefine("isTrusted", (!isUntrusted).ToString().ToLower());
backend = invokerTemplate.Compile();
}
@@ -474,6 +476,7 @@ namespace mROA.Codegen
invokerTemplate.AddDefine("parametersType", parameterTypes);
invokerTemplate.AddDefine("suitableType", baseInterface.ToUnityString());
invokerTemplate.AddDefine("funcInvoking", funcInvoking);
invokerTemplate.AddDefine("isTrusted", "true");
var backend = invokerTemplate.Compile();
_methodRepoTemplate.Insert("invoker", backend);
@@ -507,6 +510,8 @@ namespace mROA.Codegen
invokerTemplate.AddDefine("suitableType", baseInterace.ToUnityString());
invokerTemplate.AddDefine("funcInvoking",
$"(i as {method.ContainingType.ToUnityString()})[{parameterInserts}]");
invokerTemplate.AddDefine("isTrusted", "true");
backend = invokerTemplate.Compile();
}
else
@@ -517,6 +522,8 @@ namespace mROA.Codegen
invokerTemplate.AddDefine("suitableType", baseInterace.ToUnityString());
invokerTemplate.AddDefine("funcInvoking",
$"(i as {method.ContainingType.ToUnityString()}).{(method.AssociatedSymbol as IPropertySymbol)!.Name}");
invokerTemplate.AddDefine("isTrusted", "true");
backend = invokerTemplate.Compile();
}
@@ -547,6 +554,8 @@ namespace mROA.Codegen
invokerTemplate.AddDefine("suitableType", baseInterace.ToUnityString());
invokerTemplate.AddDefine("funcInvoking",
$"(i as {method.ContainingType.ToUnityString()})[{parameterInserts}] = {valueInsert}");
invokerTemplate.AddDefine("isTrusted", "true");
backend = invokerTemplate.Compile();
}
else
@@ -560,6 +569,8 @@ namespace mROA.Codegen
invokerTemplate.AddDefine("suitableType", baseInterace.ToUnityString());
invokerTemplate.AddDefine("funcInvoking",
$"(i as {method.ContainingType.ToUnityString()}).{(method.AssociatedSymbol as IPropertySymbol)!.Name} = {Caster((method.AssociatedSymbol as IPropertySymbol)!.Type, "parameters[0]")}");
invokerTemplate.AddDefine("isTrusted", "true");
backend = invokerTemplate.Compile();
}
+1
View File
@@ -5,6 +5,7 @@ namespace mROA.Abstract
public interface IMethodInvoker
{
bool IsVoid { get; }
bool IsTrusted { get; }
Type[] ParameterTypes { get; }
Type? ReturnType { get; }
Type SuitableType { get; }
@@ -95,7 +95,8 @@ namespace mROA.Implementation.Backend
}
}
private static object GetContext(ICallRequest command, IContextRepository contextRepository, IMethodInvoker invoker)
private static object GetContext(ICallRequest command, IContextRepository contextRepository,
IMethodInvoker invoker)
{
var context = command.ObjectId.ContextId != -1
? contextRepository.GetObject<object>(command.ObjectId)
@@ -147,7 +148,12 @@ namespace mROA.Implementation.Backend
{
var finalResult = invoker.Invoke(instance, parameter, new object[] { executionContext });
if (invoker.IsVoid)
if (!invoker.IsTrusted)
{
return new AsyncCommandExecution();
}
if (invoker.IsVoid )
{
return new FinalCommandExecution
{
@@ -163,10 +169,15 @@ namespace mROA.Implementation.Backend
}
catch (Exception e)
{
return new ExceptionCommandExecution
if (invoker.IsTrusted)
return new ExceptionCommandExecution
{
Id = command.Id,
Exception = e.ToString()
};
return new AsyncCommandExecution
{
Id = command.Id,
Exception = e.ToString()
Id = command.Id
};
}
}
@@ -198,7 +209,9 @@ namespace mROA.Implementation.Backend
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
multiClientOwnershipRepository?.RegisterOwnership(representationModule.Id);
representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution, payload);
if (invoker.IsTrusted)
representationModule.PostCallMessage(command.Id, EMessageType.FinishedCommandExecution,
payload);
multiClientOwnershipRepository?.FreeOwnership();
});
@@ -209,10 +222,15 @@ namespace mROA.Implementation.Backend
}
catch (Exception e)
{
return new ExceptionCommandExecution
if (invoker.IsTrusted)
return new ExceptionCommandExecution
{
Id = command.Id,
Exception = e.ToString()
};
return new AsyncCommandExecution
{
Id = command.Id,
Exception = e.ToString()
Id = command.Id
};
}
}
+7 -2
View File
@@ -6,6 +6,7 @@ using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using mROA.Abstract;
using static mROA.Implementation.EMessageType;
namespace mROA.Implementation.Backend
{
@@ -56,12 +57,12 @@ namespace mROA.Implementation.Backend
int channelId;
switch (parsed.MessageType)
{
case EMessageType.UntrustedConnect:
case UntrustedConnect:
channelId = BitConverter.ToInt32(parsed.Data);
_reservedPorts[incoming.RemoteEndPoint] = channelId;
_ = UntrustedSend(_hub.GetInteraction(channelId), incoming.RemoteEndPoint);
break;
default:
default:
channelId = _reservedPorts[incoming.RemoteEndPoint];
var interaction = _hub.GetInteraction(channelId);
await interaction.ReceiveChanel.Writer.WriteAsync(parsed, token);
@@ -82,6 +83,10 @@ namespace mROA.Implementation.Backend
{
await foreach (var post in interaction.UntrustedPostChanel.ReadAllAsync())
{
if (post.MessageType is not (CallRequest or EMessageType.CancelRequest
or EventRequest))
continue;
var parsed = _serializationToolkit.Serialize(post);
await _client.SendAsync(parsed, parsed.Length, endpoint);
}
@@ -5,7 +5,7 @@ using System.Threading;
using System.Threading.Tasks;
using mROA.Abstract;
namespace mROA.Implementation
namespace mROA.Implementation.Frontend
{
public class UdpUntrustedInteraction : IUntrustedInteractionModule
{
@@ -55,6 +55,10 @@ namespace mROA.Implementation
await foreach (var post in _channelInteractionModule.UntrustedPostChanel.ReadAllAsync(token))
{
if (post.MessageType is not (EMessageType.CallRequest or EMessageType.CancelRequest
or EMessageType.EventRequest))
continue;
var serialized = _serializationToolkit.Serialize(post);
#if TRACE
Console.WriteLine("Untrusted write start");
+2
View File
@@ -6,6 +6,7 @@ namespace mROA.Implementation
public class MethodInvoker : IMethodInvoker
{
public bool IsVoid { get; set; }
public bool IsTrusted { get; set; } = true;
public Type[] ParameterTypes { get; set; } = Type.EmptyTypes;
public Type? ReturnType { get; set; }
public Func<object, object?[]?, object[], object?> Invoking { get; set; } = (_, _, _) => null;
@@ -32,6 +33,7 @@ namespace mROA.Implementation
public class AsyncMethodInvoker : IMethodInvoker
{
public bool IsVoid { get; set; }
public bool IsTrusted { get; set; } = true;
public Type[] ParameterTypes { get; set; } = Type.EmptyTypes;
public Type? ReturnType { get; set; }
public Type SuitableType { get; set; }