Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Binary file not shown.
22 changes: 18 additions & 4 deletions InvisibleMan-XRay/Core/InvisibleManXRayCore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using InvisibleManXRay.Handlers;

namespace InvisibleManXRay.Core
{
Expand Down Expand Up @@ -27,6 +28,7 @@ public class InvisibleManXRayCore
private Func<IProxy> getProxy;
private Func<ITunnel> getTunnel;
private Action<string> onFailLoadingConfig;
private CoreEnabledOrDisabledModeObserver coreEnabledOrDisabledModeObserver;

private LocalizationService LocalizationService => ServiceLocator.Get<LocalizationService>();
private AnalyticsService AnalyticsService => ServiceLocator.Get<AnalyticsService>();
Expand All @@ -46,7 +48,8 @@ public void Setup(
Func<string> getDns,
Func<IProxy> getProxy,
Func<ITunnel> getTunnel,
Action<string> onFailLoadingConfig)
Action<string> onFailLoadingConfig,
CoreEnabledOrDisabledModeObserver coreEnabledOrDisabledModeObserver)
{
this.getConfig = getConfig;
this.getMode = getMode;
Expand All @@ -63,6 +66,7 @@ public void Setup(
this.getProxy = getProxy;
this.getTunnel = getTunnel;
this.onFailLoadingConfig = onFailLoadingConfig;
this.coreEnabledOrDisabledModeObserver = coreEnabledOrDisabledModeObserver;
}

public Status LoadConfig()
Expand Down Expand Up @@ -95,17 +99,26 @@ public Status LoadConfig(string path)
public Status EnableMode()
{
Mode mode = getMode.Invoke();


Status status;
if (mode == Mode.PROXY)
return EnableProxy();
status = EnableProxy();
else
return EnableTunnel();
status = EnableTunnel();

if (status.Code == Code.SUCCESS)
{
coreEnabledOrDisabledModeObserver.Notify(ProxyEnabledOrDisabledState.ENABLED);
}

return status;
}

public void DisableMode()
{
DisableProxy();
DisableTunnel();
coreEnabledOrDisabledModeObserver.Notify(ProxyEnabledOrDisabledState.DISABLED);
}

public void Run(string config)
Expand Down Expand Up @@ -139,6 +152,7 @@ public void Cancel()
{
CancelProxy();
CancelTunnel();
coreEnabledOrDisabledModeObserver.Notify(ProxyEnabledOrDisabledState.DISABLED);
}

public int Test(string config)
Expand Down
33 changes: 33 additions & 0 deletions InvisibleMan-XRay/Handlers/CoreEnabledOrDisabledModeObserver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;

namespace InvisibleManXRay.Handlers;

public enum ProxyEnabledOrDisabledState
{
ENABLED,
DISABLED
}

public class CoreEnabledOrDisabledModeObserver : Handler
{
private readonly List<Action<ProxyEnabledOrDisabledState>> listeners = new();

public void Subscribe(Action<ProxyEnabledOrDisabledState> listener)
{
listeners.Add(listener);
}

public void Unsubscribe(Action<ProxyEnabledOrDisabledState> listener)
{
listeners.Remove(listener);
}

public void Notify(ProxyEnabledOrDisabledState state)
{
foreach (var listener in listeners)
{
listener(state);
}
}
}
36 changes: 27 additions & 9 deletions InvisibleMan-XRay/Handlers/NotifyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Reflection;

namespace InvisibleManXRay.Handlers
{
Expand All @@ -13,7 +14,7 @@ namespace InvisibleManXRay.Handlers

public class NotifyHandler : Handler
{
private NotifyIcon notifyIcon;
private NotifyIcon? notifyIcon;

private Func<Mode> getMode;
private Action onOpenClick;
Expand All @@ -35,7 +36,8 @@ public void Setup(
Action onAboutClick,
Action onCloseClick,
Action onProxyModeClick,
Action onTunnelModeClick
Action onTunnelModeClick,
CoreEnabledOrDisabledModeObserver coreEnabledOrDisabledModeObserver
)
{
this.getMode = getMode;
Expand All @@ -45,6 +47,7 @@ Action onTunnelModeClick
this.onCloseClick = onCloseClick;
this.onProxyModeClick = onProxyModeClick;
this.onTunnelModeClick = onTunnelModeClick;
coreEnabledOrDisabledModeObserver.Subscribe(HandleProxyEnabledOrDisabled);
}

public void CheckModeItem(Mode mode)
Expand All @@ -60,20 +63,35 @@ public void InitializeNotifyIcon()
notifyIcon.Dispose();

notifyIcon = new NotifyIcon();
notifyIcon.Icon = GetNotifyIcon();
notifyIcon.Icon = GetNotifyIconFromResources(forEnabled: false);
notifyIcon.Visible = true;

HandleNotifyIconClick();
AddMenuStrip();

bool IsNotifyIconAlreadyExists() => notifyIcon != null;
}

Icon GetNotifyIcon()
{
return Icon.ExtractAssociatedIcon(
System.Environment.GetCommandLineArgs().First()
);
}
public void HandleProxyEnabledOrDisabled(ProxyEnabledOrDisabledState state)
{
if (notifyIcon is null)
return;

var icon = GetNotifyIconFromResources(forEnabled: state == ProxyEnabledOrDisabledState.ENABLED);
notifyIcon.Icon = icon;
}

private Icon GetNotifyIconFromResources(bool forEnabled)
{
const string rootNamespace = "InvisibleManXRay";
const string basePath = $"Assets.NotifyIcons";
var fileName = forEnabled ? "IconEnabled.ico" : "IconDisabled.ico";
var resourceName = $"{rootNamespace}.{basePath}.{fileName}";

var assembly = Assembly.GetExecutingAssembly();
using var stream = assembly.GetManifestResourceStream(resourceName) ?? throw new InvalidOperationException("Something went wrong with resources or with assembly or with root namespace");
var icon = new Icon(stream);
return icon;
}

private void HandleNotifyIconClick()
Expand Down
6 changes: 6 additions & 0 deletions InvisibleMan-XRay/InvisibleMan-XRay.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,10 @@
</Content>
</ItemGroup>

<!-- icons for notify icon-->
<ItemGroup>
<EmbeddedResource Include="Assets\NotifyIcons\IconDisabled.ico" />
<EmbeddedResource Include="Assets\NotifyIcons\IconEnabled.ico" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion InvisibleMan-XRay/Managers/Initializers/CoreInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void Setup(HandlersManager handlersManager)
getDns: settingsHandler.UserSettings.GetDns,
getProxy: proxyHandler.GetProxy,
getTunnel: tunnelHandler.GetTunnel,
onFailLoadingConfig: configHandler.RemoveConfigFromList
onFailLoadingConfig: configHandler.RemoveConfigFromList,
coreEnabledOrDisabledModeObserver: handlersManager.GetHandler<CoreEnabledOrDisabledModeObserver>()
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void Register()
HandlersManager.AddHandler(new DeepLinkHandler());
HandlersManager.AddHandler(new LinkHandler());
HandlersManager.AddHandler(new LocalizationHandler());
HandlersManager.AddHandler(new CoreEnabledOrDisabledModeObserver());
}

public void Setup(
Expand Down Expand Up @@ -96,7 +97,8 @@ void SetupNotifyHandler()
onAboutClick: OpenAboutWindow,
onCloseClick: CloseApplication,
onProxyModeClick: () => { OnModeClick(Mode.PROXY); },
onTunnelModeClick: () => { OnModeClick(Mode.TUN); }
onTunnelModeClick: () => { OnModeClick(Mode.TUN); },
coreEnabledOrDisabledModeObserver: HandlersManager.GetHandler<CoreEnabledOrDisabledModeObserver>()
);

void CloseApplication()
Expand Down