Skip to content

Commit

Permalink
Merge pull request #123 from InvisibleManVPN/develop
Browse files Browse the repository at this point in the history
InvisibleMan XRay version 2.3.2
  • Loading branch information
InvisibleManVPN authored Sep 7, 2024
2 parents ba1639b + a9344b9 commit a575165
Show file tree
Hide file tree
Showing 18 changed files with 400 additions and 28 deletions.
13 changes: 12 additions & 1 deletion InvisibleMan-XRay/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ protected override void OnStartup(StartupEventArgs e)
InitializeAppManager();
InitializeWindowFactory();
InitializeMainWindow();
HandlePipes();

void InitializeAppManager()
{
appManager = new AppManager();
appManager = new AppManager(e.Args);
appManager.Initialize();
}

Expand All @@ -32,6 +33,16 @@ void InitializeMainWindow()
MainWindow mainWindow = windowFactory.CreateMainWindow();
mainWindow.Show();
}

void HandlePipes()
{
if (IsThereAnyArg())
PipeManager.SignalThisApp(e.Args);

PipeManager.ListenForPipes();
}

bool IsThereAnyArg() => e.Args.Length != 0;
}
}
}
8 changes: 8 additions & 0 deletions InvisibleMan-XRay/Assets/Icons.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@
</VisualBrush.Visual>
</VisualBrush>

<VisualBrush x:Key="Icon.Share">
<VisualBrush.Visual>
<Canvas>
<Path Data="M339.588,314.529c-14.215,0-27.456,4.133-38.621,11.239l-112.682-78.67c1.809-6.315,2.798-12.976,2.798-19.871c0-6.896-0.989-13.557-2.798-19.871l109.64-76.547c11.764,8.356,26.133,13.286,41.662,13.286c39.79,0,72.047-32.257,72.047-72.047C411.634,32.258,379.378,0,339.588,0c-39.79,0-72.047,32.257-72.047,72.047c0,5.255,0.578,10.373,1.646,15.308l-112.424,78.491c-10.974-6.759-23.892-10.666-37.727-10.666c-39.79,0-72.047,32.257-72.047,72.047s32.256,72.047,72.047,72.047c13.834,0,26.753-3.907,37.727-10.666l113.292,79.097c-1.629,6.017-2.514,12.34-2.514,18.872c0,39.79,32.257,72.047,72.047,72.047c39.79,0,72.047-32.257,72.047-72.047C411.635,346.787,379.378,314.529,339.588,314.529z" Fill="#ffffff"/>
</Canvas>
</VisualBrush.Visual>
</VisualBrush>

<!-- Update status -->
<VisualBrush x:Key="Icon.Update.Check">
<VisualBrush.Visual>
Expand Down
2 changes: 1 addition & 1 deletion InvisibleMan-XRay/Factories/WindowFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ string GetXRayCoreVersion()
}
}

private ServerWindow CreateServerWindow()
public ServerWindow CreateServerWindow()
{
ConfigHandler configHandler = handlersManager.GetHandler<ConfigHandler>();
TemplateHandler templateHandler = handlersManager.GetHandler<TemplateHandler>();
Expand Down
67 changes: 67 additions & 0 deletions InvisibleMan-XRay/Handlers/DeepLinkHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;

namespace InvisibleManXRay.Handlers
{
using DeepLinks;
using Values;

public class DeepLinkHandler : Handler
{
private IDeepLink deepLink;

private Action<string> onConfigLinkFetched;
private Action<string> onSubscriptionLinkFetched;

public DeepLinkHandler()
{
InitializeDeepLink();
}

public void Setup(
ref Action<string> onReceiveArg,
Action<string> onConfigLinkFetched,
Action<string> onSubscriptionLinkFetched
)
{
onReceiveArg = TryFetchLink;
this.onConfigLinkFetched = onConfigLinkFetched;
this.onSubscriptionLinkFetched = onSubscriptionLinkFetched;
}

private void InitializeDeepLink()
{
this.deepLink = GetDeepLink();
deepLink.Register();
}

private IDeepLink GetDeepLink()
{
WindowsDeepLink windowsDeepLink = new WindowsDeepLink();
return windowsDeepLink;
}

private void TryFetchLink(string arg)
{
if (IsValidConfigLink())
onConfigLinkFetched.Invoke(GetConfigLink());
else if (IsValidSubscriptionLink())
onSubscriptionLinkFetched.Invoke(GetSubscriptionLink());

bool IsValidConfigLink()
{
return arg.StartsWith(DeepLink.CONFIG)
&& !string.IsNullOrEmpty(GetConfigLink());
}

bool IsValidSubscriptionLink()
{
return arg.StartsWith(DeepLink.SUBSCRIPTION)
&& !string.IsNullOrEmpty(GetSubscriptionLink());
}

string GetConfigLink() => arg.Replace(DeepLink.CONFIG, "").Trim();

string GetSubscriptionLink() => arg.Replace(DeepLink.SUBSCRIPTION, "").Trim();
}
}
}
7 changes: 7 additions & 0 deletions InvisibleMan-XRay/Handlers/DeepLinks/IDeepLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace InvisibleManXRay.Handlers.DeepLinks
{
public interface IDeepLink
{
void Register();
}
}
42 changes: 42 additions & 0 deletions InvisibleMan-XRay/Handlers/DeepLinks/WindowsDeepLink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Windows;
using Microsoft.Win32;

namespace InvisibleManXRay.Handlers.DeepLinks
{
using Values;

public class WindowsDeepLink : IDeepLink
{
private const string URI_SCHEME = $@"Software\Classes\{DeepLink.SCHEME}";
private const string SHELL_OPEN_COMMAND = @"shell\open\command";
private const string URL_PROTOCOL = "URL Protocol";

private string AppName => Application.ResourceAssembly.GetName().Name;
private string AppPath => Environment.ProcessPath;

public void Register()
{
try
{
RegistryKey registry = GetApplicationClassRegistery();

registry.SetValue("", $"URL:{AppName}");
registry.SetValue(URL_PROTOCOL, "");
registry.CreateSubKey(SHELL_OPEN_COMMAND).SetValue(
name: "",
value: $"\"{AppPath}\" \"%1\""
);
}
catch
{
}
}

private RegistryKey GetApplicationClassRegistery()
{
RegistryKey registry = Registry.CurrentUser.CreateSubKey(URI_SCHEME);
return registry;
}
}
}
5 changes: 3 additions & 2 deletions InvisibleMan-XRay/InvisibleMan-XRay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<PackageId>Invisible Man XRay</PackageId>
<Company>Invisible Man</Company>
<Copyright>Copyright (C) 2024 Invisible Man</Copyright>
<Version>2.1.2.0</Version>
<AssemblyVersion>2.1.2.0</AssemblyVersion>
<Version>2.3.2.0</Version>
<AssemblyVersion>2.3.2.0</AssemblyVersion>
<Nullable>enable</Nullable>
<NoWarn>0108;8600;8601;8602;8603;8604;8618;8625;8629;8762</NoWarn>
<UseWPF>true</UseWPF>
Expand All @@ -25,6 +25,7 @@
<ItemGroup>
<PackageReference Include="NetCore5.0.Microsoft.Expression.Drawing" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="QRCoder.Xaml" Version="1.6.0" />
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
<PackageReference Include="System.Management" Version="7.0.0" />
<PackageReference Include="System.Windows.Forms" />
Expand Down
14 changes: 13 additions & 1 deletion InvisibleMan-XRay/Managers/AppManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ public class AppManager

public WindowFactory WindowFactory => factoriesInitializer.WindowFactory;

private string[] args;
private static Mutex mutex;
private const string APP_GUID = "{7I6N0VI4-S9I1-43bl-A0eM-72A47N6EDH8M}";

public AppManager(string[] args)
{
this.args = args;
}

public void Initialize()
{
AvoidRunningMultipleInstances();
Expand All @@ -42,9 +48,15 @@ private void AvoidRunningMultipleInstances()

if(!isCreatedNew)
{
MessageBox.Show(Message.APP_ALREADY_RUNNING);
if (IsThereAnyArg())
PipeManager.SignalOpenedApp(args);
else
MessageBox.Show(Message.APP_ALREADY_RUNNING);

Environment.Exit(0);
}

bool IsThereAnyArg() => args.Length != 0;
}

private void SetApplicationCurrentDirectory()
Expand Down
79 changes: 58 additions & 21 deletions InvisibleMan-XRay/Managers/Initializers/HandlersInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void Register()
HandlersManager.AddHandler(new VersionHandler());
HandlersManager.AddHandler(new UpdateHandler());
HandlersManager.AddHandler(new BroadcastHandler());
HandlersManager.AddHandler(new DeepLinkHandler());
HandlersManager.AddHandler(new LinkHandler());
}

Expand All @@ -40,6 +41,7 @@ WindowFactory windowFactory
SetupConfigHandler();
SetupUpdateHandler();
SetupNotifyHandler();
SetupDeepLinkHandler();

void SetupProcessHandler()
{
Expand Down Expand Up @@ -95,27 +97,6 @@ void SetupNotifyHandler()
onTunnelModeClick: () => { OnModeClick(Mode.TUN); }
);

bool IsAnotherWindowOpened() => Application.Current.Windows.Count > 1;

bool IsMainWindow(Window window) => window == Application.Current.MainWindow;

void ShowMainWindow() => Application.Current.MainWindow.Show();

void CloseOtherWindows()
{
foreach (Window window in Application.Current.Windows)
{
if (!IsMainWindow(window))
window.Close();
}
}

void OpenApplication()
{
ShowMainWindow();
Application.Current.MainWindow.WindowState = WindowState.Normal;
}

void CloseApplication()
{
core.DisableMode();
Expand Down Expand Up @@ -154,6 +135,62 @@ void OnModeClick(Mode mode)
mainWindow.TryDisableModeAndRerun();
}
}

void SetupDeepLinkHandler()
{
HandlersManager.GetHandler<DeepLinkHandler>().Setup(
onReceiveArg: ref PipeManager.OnReceiveArg,
onConfigLinkFetched: PrepareToImportConfigLink,
onSubscriptionLinkFetched: PrepareToImportSubscriptionLink
);

void PrepareToImportConfigLink(string link)
{
ServerWindow serverWindow = GetServerWindow();
serverWindow.OpenImportConfigWithLinkSection(link);
serverWindow.ShowDialog();
}

void PrepareToImportSubscriptionLink(string link)
{
ServerWindow serverWindow = GetServerWindow();
serverWindow.OpenImportSubscriptionWithLinkSection(link);
serverWindow.ShowDialog();
}

ServerWindow GetServerWindow()
{
OpenApplication();
if (IsAnotherWindowOpened())
CloseOtherWindows();

ServerWindow serverWindow = windowFactory.CreateServerWindow();
serverWindow.Owner = Application.Current.MainWindow;

return serverWindow;
}
}
}

private void OpenApplication()
{
ShowMainWindow();
Application.Current.MainWindow.WindowState = WindowState.Normal;
}

private void CloseOtherWindows()
{
foreach (Window window in Application.Current.Windows)
{
if (!IsMainWindow(window))
window.Close();
}
}

private void ShowMainWindow() => Application.Current.MainWindow.Show();

private bool IsAnotherWindowOpened() => Application.Current.Windows.Count > 1;

private bool IsMainWindow(Window window) => window == Application.Current.MainWindow;
}
}
51 changes: 51 additions & 0 deletions InvisibleMan-XRay/Managers/PipeManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.IO;
using System.IO.Pipes;
using System.Windows;
using System.Threading.Tasks;

namespace InvisibleManXRay.Managers
{
public static class PipeManager
{
private const string PIPE_NAME = "InvisibleManXRayPipe";

public static Action<string> OnReceiveArg = delegate{};

public static void ListenForPipes()
{
Task.Run(() => {
while(true)
{
NamedPipeServerStream pipeServer = new NamedPipeServerStream(PIPE_NAME);
pipeServer.WaitForConnection();
StreamReader reader = new StreamReader(pipeServer);
string message = reader.ReadToEnd();
Application.Current.Dispatcher.BeginInvoke(new Action(delegate {
OnReceiveArg.Invoke(message);
}));
pipeServer.Close();
pipeServer.Dispose();
}
});
}

public static void SignalOpenedApp(string[] args)
{
NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", PIPE_NAME);
pipeClient.Connect();

StreamWriter writer = new StreamWriter(pipeClient);
writer.WriteLine(args[0]);
writer.Flush();
writer.Close();
}

public static void SignalThisApp(string[] args)
{
OnReceiveArg.Invoke(args[0]);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace InvisibleManXRay.Services.Analytics.Configuration
{
public class ShareButtonClickedEvent : ConfigurationEvent
{

}
}
Loading

0 comments on commit a575165

Please sign in to comment.