diff --git a/mROA.Codegen/MethodRepo.cstmpl b/mROA.Codegen/MethodRepo.cstmpl index b1c4a93..3860e42 100644 --- a/mROA.Codegen/MethodRepo.cstmpl +++ b/mROA.Codegen/MethodRepo.cstmpl @@ -17,6 +17,7 @@ namespace mROA.Codegen new mROA.Implementation.AsyncMethodInvoker { IsVoid = , + IsTrusted = , ReturnType = typeof(), ParameterTypes = new Type[] { }, SuitableType = typeof(), @@ -26,6 +27,7 @@ namespace mROA.Codegen new mROA.Implementation.MethodInvoker { IsVoid = , + IsTrusted = , ReturnType = typeof(), ParameterTypes = new Type[] { }, SuitableType = typeof(), diff --git a/mROA.Codegen/mROASourceGenerator.cs b/mROA.Codegen/mROASourceGenerator.cs index 9f05e44..b2b9f26 100644 --- a/mROA.Codegen/mROASourceGenerator.cs +++ b/mROA.Codegen/mROASourceGenerator.cs @@ -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(); } diff --git a/mROA/Abstract/IMethodInvoker.cs b/mROA/Abstract/IMethodInvoker.cs index dcb04db..f151cae 100644 --- a/mROA/Abstract/IMethodInvoker.cs +++ b/mROA/Abstract/IMethodInvoker.cs @@ -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; } diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index f184d20..fee9bbb 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -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(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 }; } } diff --git a/mROA/Implementation/Backend/UdpGateway.cs b/mROA/Implementation/Backend/UdpGateway.cs index 86806cf..68be81a 100644 --- a/mROA/Implementation/Backend/UdpGateway.cs +++ b/mROA/Implementation/Backend/UdpGateway.cs @@ -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); } diff --git a/mROA/Implementation/UdpUntrustedInteraction.cs b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs similarity index 92% rename from mROA/Implementation/UdpUntrustedInteraction.cs rename to mROA/Implementation/Frontend/UdpUntrustedInteraction.cs index e66b526..6dafb47 100644 --- a/mROA/Implementation/UdpUntrustedInteraction.cs +++ b/mROA/Implementation/Frontend/UdpUntrustedInteraction.cs @@ -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"); diff --git a/mROA/Implementation/MethodInvoker.cs b/mROA/Implementation/MethodInvoker.cs index 59efb7d..ff713db 100644 --- a/mROA/Implementation/MethodInvoker.cs +++ b/mROA/Implementation/MethodInvoker.cs @@ -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 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; }