Skip to content

Commit

Permalink
Update to Ver.5.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
若凡 祝 committed Jul 14, 2020
1 parent 0b146fa commit a4e44c0
Show file tree
Hide file tree
Showing 51 changed files with 1,225 additions and 361 deletions.
11 changes: 7 additions & 4 deletions CommunicateService/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace CommunicateService
public sealed class Service : IBackgroundTask
{
private BackgroundTaskDeferral Deferral;
private static readonly List<AppServiceConnection> ClientConnections = new List<AppServiceConnection>();
private static readonly Dictionary<AppServiceConnection, string> ClientConnections = new Dictionary<AppServiceConnection, string>();
private static AppServiceConnection ServerConnection;
private static readonly object Locker = new object();

Expand Down Expand Up @@ -49,7 +49,8 @@ public async void Run(IBackgroundTaskInstance taskInstance)
{
lock (Locker)
{
ClientConnections.Add(IncomeConnection);
string Guid = Convert.ToString(Response.Message["Guid"]);
ClientConnections.Add(IncomeConnection, Guid);
}

break;
Expand Down Expand Up @@ -107,11 +108,13 @@ private void TaskInstance_Canceled(IBackgroundTaskInstance sender, BackgroundTas
lock (Locker)
{
AppServiceConnection DisConnection = (sender.TriggerDetails as AppServiceTriggerDetails).AppServiceConnection;

DisConnection.RequestReceived -= Connection_RequestReceived;

if (ClientConnections.Contains(DisConnection))
if (ClientConnections.ContainsKey(DisConnection))
{
ServerConnection.SendMessageAsync(new ValueSet { { "ExcuteType", "Excute_RequestClosePipe" }, { "Guid", ClientConnections[DisConnection] } }).AsTask().ConfigureAwait(false).GetAwaiter().GetResult();

ClientConnections.Remove(DisConnection);

DisConnection.Dispose();
Expand Down
2 changes: 1 addition & 1 deletion FullTrustProcess/FullTrustProcess.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
<Version>4.7.1</Version>
</PackageReference>
<PackageReference Include="Vanara.Windows.Shell">
<Version>3.2.11</Version>
<Version>3.2.12</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down
175 changes: 165 additions & 10 deletions FullTrustProcess/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,24 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Reflection;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel.AppService;
using Windows.Foundation.Collections;
using Windows.System;

namespace FullTrustProcess
{
class Program
{
private static AppServiceConnection Connection;

private static readonly Dictionary<string, NamedPipeServerStream> PipeServers = new Dictionary<string, NamedPipeServerStream>();

private static readonly HashSet<string> SpecialStringMap = new HashSet<string>()
{
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "WindowsPowerShell\\v1.0\\powershell.exe"),
Expand All @@ -26,14 +30,16 @@ class Program

private readonly static ManualResetEvent ExitLocker = new ManualResetEvent(false);

private static readonly object Locker = new object();

[STAThread]
static async Task Main(string[] args)
{
try
{
using (Mutex LaunchLocker = new Mutex(true, "RX_Explorer_FullTrustProcess", out bool IsNotExist))
{
if(!IsNotExist)
if (!IsNotExist)
{
return;
}
Expand All @@ -53,7 +59,7 @@ static async Task Main(string[] args)
ExitLocker.WaitOne();
}
}
catch(Exception e)
catch (Exception e)
{
Debug.WriteLine($"FullTrustProcess出现异常,错误信息{e.Message}");
}
Expand All @@ -62,18 +68,62 @@ static async Task Main(string[] args)
Connection?.Dispose();
ExitLocker?.Dispose();

PipeServers.Values.ToList().ForEach((Item) =>
{
Item.Disconnect();
Item.Dispose();
});

PipeServers.Clear();

Environment.Exit(0);
}
}

private static void InitializeNewNamedPipe(string ID)
{
NamedPipeServerStream NewPipeServer = new NamedPipeServerStream($@"Explorer_And_FullTrustProcess_NamedPipe-{ID}", PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.None, 2048, 2048, null, HandleInheritability.None, PipeAccessRights.ChangePermissions);
PipeSecurity Security = NewPipeServer.GetAccessControl();
PipeAccessRule ClientRule = new PipeAccessRule(new SecurityIdentifier("S-1-15-2-1"), PipeAccessRights.ReadWrite | PipeAccessRights.CreateNewInstance, AccessControlType.Allow);
PipeAccessRule OwnerRule = new PipeAccessRule(WindowsIdentity.GetCurrent().Owner, PipeAccessRights.FullControl, AccessControlType.Allow);
Security.AddAccessRule(ClientRule);
Security.AddAccessRule(OwnerRule);
NewPipeServer.SetAccessControl(Security);

PipeServers.Add(ID, NewPipeServer);
}

private async static void Connection_RequestReceived(AppServiceConnection sender, AppServiceRequestReceivedEventArgs args)
{
var Deferral = args.GetDeferral();
AppServiceDeferral Deferral = args.GetDeferral();

try
{
switch (args.Request.Message["ExcuteType"])
{
case "Excute_RequestClosePipe":
{
string Guid = Convert.ToString(args.Request.Message["Guid"]);

if (PipeServers.ContainsKey(Guid))
{
PipeServers[Guid].Disconnect();
PipeServers[Guid].Dispose();
PipeServers.Remove(Guid);
}
break;
}
case "Excute_RequestCreateNewPipe":
{
string Guid = Convert.ToString(args.Request.Message["Guid"]);

if (!PipeServers.ContainsKey(Guid))
{
InitializeNewNamedPipe(Guid);
}

break;
}
case "Identity":
{
await args.Request.SendResponseAsync(new ValueSet { { "Identity", "FullTrustProcess" } });
Expand Down Expand Up @@ -226,12 +276,29 @@ private async static void Connection_RequestReceived(AppServiceConnection sender

string SourcePathJson = Convert.ToString(args.Request.Message["SourcePath"]);
string DestinationPath = Convert.ToString(args.Request.Message["DestinationPath"]);
string Guid = Convert.ToString(args.Request.Message["Guid"]);

List<KeyValuePair<string, string>> SourcePathList = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(SourcePathJson);

if (SourcePathList.All((Item) => Directory.Exists(Item.Key) || File.Exists(Item.Key)))
{
if (StorageItemController.Copy(SourcePathList, DestinationPath))
if (StorageItemController.Copy(SourcePathList, DestinationPath, (s, e) =>
{
lock (Locker)
{
NamedPipeServerStream Server = PipeServers[Guid];

if (!Server.IsConnected)
{
Server.WaitForConnection();
}

using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true))
{
Writer.WriteLine(e.ProgressPercentage);
}
}
}))
{
Value.Add("Success", string.Empty);
}
Expand All @@ -245,6 +312,24 @@ private async static void Connection_RequestReceived(AppServiceConnection sender
Value.Add("Error_NotFound", "SourcePath is not a file or directory");
}

if (!Value.ContainsKey("Success"))
{
lock (Locker)
{
NamedPipeServerStream Server = PipeServers[Guid];

if (!Server.IsConnected)
{
Server.WaitForConnection();
}

using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true))
{
Writer.WriteLine("Error_Stop_Signal");
}
}
}

await args.Request.SendResponseAsync(Value);
break;
}
Expand All @@ -254,6 +339,7 @@ private async static void Connection_RequestReceived(AppServiceConnection sender

string SourcePathJson = Convert.ToString(args.Request.Message["SourcePath"]);
string DestinationPath = Convert.ToString(args.Request.Message["DestinationPath"]);
string Guid = Convert.ToString(args.Request.Message["Guid"]);

List<KeyValuePair<string, string>> SourcePathList = JsonConvert.DeserializeObject<List<KeyValuePair<string, string>>>(SourcePathJson);

Expand All @@ -265,7 +351,23 @@ private async static void Connection_RequestReceived(AppServiceConnection sender
}
else
{
if (StorageItemController.Move(SourcePathList, DestinationPath))
if (StorageItemController.Move(SourcePathList, DestinationPath, (s, e) =>
{
lock (Locker)
{
NamedPipeServerStream Server = PipeServers[Guid];

if (!Server.IsConnected)
{
Server.WaitForConnection();
}

using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true))
{
Writer.WriteLine(e.ProgressPercentage);
}
}
}))
{
Value.Add("Success", string.Empty);
}
Expand All @@ -280,6 +382,24 @@ private async static void Connection_RequestReceived(AppServiceConnection sender
Value.Add("Error_NotFound", "SourcePath is not a file or directory");
}

if(!Value.ContainsKey("Success"))
{
lock (Locker)
{
NamedPipeServerStream Server = PipeServers[Guid];

if (!Server.IsConnected)
{
Server.WaitForConnection();
}

using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true))
{
Writer.WriteLine("Error_Stop_Signal");
}
}
}

await args.Request.SendResponseAsync(Value);
break;
}
Expand All @@ -288,6 +408,7 @@ private async static void Connection_RequestReceived(AppServiceConnection sender
ValueSet Value = new ValueSet();

string ExcutePathJson = Convert.ToString(args.Request.Message["ExcutePath"]);
string Guid = Convert.ToString(args.Request.Message["Guid"]);
bool PermanentDelete = Convert.ToBoolean(args.Request.Message["PermanentDelete"]);

List<string> ExcutePathList = JsonConvert.DeserializeObject<List<string>>(ExcutePathJson);
Expand All @@ -308,7 +429,23 @@ private async static void Connection_RequestReceived(AppServiceConnection sender
Attributes = FileAttributes.Normal & FileAttributes.Directory
});

if (StorageItemController.Delete(ExcutePathList, PermanentDelete))
if (StorageItemController.Delete(ExcutePathList, PermanentDelete, (s, e) =>
{
lock (Locker)
{
NamedPipeServerStream Server = PipeServers[Guid];

if (!Server.IsConnected)
{
Server.WaitForConnection();
}

using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true))
{
Writer.WriteLine(e.ProgressPercentage);
}
}
}))
{
Value.Add("Success", string.Empty);
}
Expand All @@ -328,6 +465,24 @@ private async static void Connection_RequestReceived(AppServiceConnection sender
Value.Add("Error_Failure", "The specified file or folder could not be deleted");
}

if (!Value.ContainsKey("Success"))
{
lock (Locker)
{
NamedPipeServerStream Server = PipeServers[Guid];

if (!Server.IsConnected)
{
Server.WaitForConnection();
}

using (StreamWriter Writer = new StreamWriter(Server, new UTF8Encoding(false), 1024, true))
{
Writer.WriteLine("Error_Stop_Signal");
}
}
}

await args.Request.SendResponseAsync(Value);
break;
}
Expand Down Expand Up @@ -412,13 +567,13 @@ private async static void Connection_RequestReceived(AppServiceConnection sender
case "Excute_Test_Connection":
{
await args.Request.SendResponseAsync(new ValueSet { { "Excute_Test_Connection", string.Empty } });

break;
}
case "Excute_Exit":
{
ExitLocker.Set();

break;
}
}
Expand Down
Loading

0 comments on commit a4e44c0

Please sign in to comment.