Skip to content

Commit

Permalink
Merge pull request #82 from InvisibleManVPN/develop
Browse files Browse the repository at this point in the history
InvisibleMan XRay version 1.4.4
  • Loading branch information
InvisibleManVPN authored Sep 27, 2023
2 parents c7ad4d9 + 66442a5 commit 11df566
Show file tree
Hide file tree
Showing 41 changed files with 1,715 additions and 325 deletions.
8 changes: 8 additions & 0 deletions InvisibleMan-XRay/Assets/Icons.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@
</VisualBrush.Visual>
</VisualBrush>

<VisualBrush x:Key="Icon.Refresh">
<VisualBrush.Visual>
<Canvas>
<Path Data="M463.5 224H472c13.3 0 24-10.7 24-24V72c0-9.7-5.8-18.5-14.8-22.2s-19.3-1.7-26.2 5.2L413.4 96.6c-87.6-86.5-228.7-86.2-315.8 1c-87.5 87.5-87.5 229.3 0 316.8s229.3 87.5 316.8 0c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0c-62.5 62.5-163.8 62.5-226.3 0s-62.5-163.8 0-226.3c62.2-62.2 162.7-62.5 225.3-1L327 183c-6.9 6.9-8.9 17.2-5.2 26.2s12.5 14.8 22.2 14.8H463.5z" Fill="#ffffff"/>
</Canvas>
</VisualBrush.Visual>
</VisualBrush>

<!-- Update status -->
<VisualBrush x:Key="Icon.Update.Check">
<VisualBrush.Visual>
Expand Down
18 changes: 12 additions & 6 deletions InvisibleMan-XRay/Factories/WindowFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,29 @@ private ServerWindow CreateServerWindow()

ServerWindow serverWindow = new ServerWindow();
serverWindow.Setup(
getCurrentConfigIndex: settingsHandler.UserSettings.GetCurrentConfigIndex,
getAllConfigs: configHandler.GetAllConfigs,
convertConfigLinkToV2Ray: templateHandler.ConverLinkToV2Ray,
getCurrentConfigPath: settingsHandler.UserSettings.GetCurrentConfigPath,
isCurrentPathEqualRootConfigPath: configHandler.IsCurrentPathEqualRootConfigPath,
getAllGeneralConfigs: configHandler.GetAllGeneralConfigs,
getAllSubscriptionConfigs: configHandler.GetAllSubscriptionConfigs,
getAllGroups: configHandler.GetAllGroups,
convertLinkToConfig: templateHandler.ConverLinkToConfig,
convertLinkToSubscription: templateHandler.ConvertLinkToSubscription,
loadConfig: core.LoadConfig,
testConnection: core.Test,
getLogPath: settingsHandler.UserSettings.GetLogPath,
onCopyConfig: configHandler.CopyConfig,
onCreateConfig: configHandler.CreateConfig,
onDeleteConfig: configHandler.LoadConfigFiles,
onCreateSubscription: configHandler.CreateSubscription,
onDeleteSubscription: configHandler.DeleteSubscription,
onDeleteConfig: configHandler.LoadFiles,
onUpdateConfig: UpdateConfig
);

return serverWindow;

void UpdateConfig(int index)
void UpdateConfig(string path)
{
settingsHandler.UpdateCurrentConfigIndex(index);
settingsHandler.UpdateCurrentConfigPath(path);
mainWindow.UpdateUI();
mainWindow.TryRerun();
}
Expand Down
137 changes: 60 additions & 77 deletions InvisibleMan-XRay/Handlers/ConfigHandler.cs
Original file line number Diff line number Diff line change
@@ -1,120 +1,103 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace InvisibleManXRay.Handlers
{
using Configs;
using Models;
using Values;
using Utilities;

public class ConfigHandler : Handler
{
private Dictionary<string, Config> configs;
private Func<int> getCurrentConfigIndex;
private GeneralConfig generalConfig;
private SubscriptionConfig subscriptionConfig;

private Func<string> getCurrentConfigPath;

public ConfigHandler()
{
this.configs = new Dictionary<string, Config>();
LoadConfigFiles();
this.generalConfig = new GeneralConfig();
this.subscriptionConfig = new SubscriptionConfig();
}

public void Setup(Func<int> getCurrentConfigIndex)
public void Setup(Func<string> getCurrentConfigPath)
{
this.getCurrentConfigIndex = getCurrentConfigIndex;
this.getCurrentConfigPath = getCurrentConfigPath;
subscriptionConfig.Setup(getCurrentConfigPath);
}

public void LoadConfigFiles()
public void LoadFiles(GroupType group, string path)
{
configs.Clear();
BaseConfig config = group == GroupType.GENERAL ? generalConfig : subscriptionConfig;
config.LoadFiles(path);
}

DirectoryInfo directoryInfo = new DirectoryInfo(Directory.CONFIGS);
if (!directoryInfo.Exists)
return;
public void CreateConfig(string remark, string data) => generalConfig.CreateConfig(remark, data);

FileInfo[] files = directoryInfo.GetFiles().OrderBy(file => file.CreationTime).ToArray();
foreach(FileInfo file in files)
{
AddConfigToList(CreateConfig(file.FullName));
}
}
public void CreateSubscription(string remark, string url, string data) => subscriptionConfig.CreateSubscription(remark, url, data);

public void CopyConfig(string path)
{
string destinationPath = $"{Directory.CONFIGS}/{GetFileName(path)}";
CopyToConfigsDirectory();
SetFileTime(destinationPath);
AddConfigToList(CreateConfig(destinationPath));
public void DeleteSubscription(Subscription subscription) => subscriptionConfig.DeleteSubscription(subscription);

void CopyToConfigsDirectory()
{
System.IO.Directory.CreateDirectory(Directory.CONFIGS);
File.Copy(path, destinationPath, true);
}
}
public void CopyConfig(string path) => generalConfig.CopyConfig(path);

public void CreateConfig(string remark, string data)
{
string destinationPath = $"{Directory.CONFIGS}/{remark}.json";
SaveToConfigsDirectory();
SetFileTime(destinationPath);
AddConfigToList(CreateConfig(destinationPath));
public Config GetCurrentConfig() => CreateConfigModel(getCurrentConfigPath.Invoke());

void SaveToConfigsDirectory()
{
System.IO.Directory.CreateDirectory(Directory.CONFIGS);
File.WriteAllText(destinationPath, data);
}
}
public void RemoveConfigFromList(string path) => GetCurrentBaseConfig().RemoveConfigFromList(path);

public Config GetCurrentConfig()
public List<Config> GetAllGeneralConfigs()
{
int currentConfigIndex = getCurrentConfigIndex.Invoke();

try
{
return configs.ElementAt(currentConfigIndex).Value;
}
catch
{
return null;
}
generalConfig.LoadFiles();
return generalConfig.GetAllConfigs();
}

public List<Config> GetAllConfigs() => configs.Select(config => config.Value).ToList();

public void RemoveConfigFromList(string path)
public List<Config> GetAllSubscriptionConfigs(string path)
{
if (configs.ContainsKey(path))
configs.Remove(path);
subscriptionConfig.LoadFiles(path);
return subscriptionConfig.GetAllConfigs();
}

private void AddConfigToList(Config config)
public List<Subscription> GetAllGroups()
{
if (configs.ContainsKey(config.Path))
configs[config.Path] = config;
else
configs.Add(config.Path, config);
subscriptionConfig.LoadGroups();
return subscriptionConfig.GetAllGroups();
}

private Config CreateConfig(string path)
public Config CreateConfigModel(string path)
{
return new Config(
path: $"{Directory.CONFIGS}/{GetFileName(path)}",
name: GetFileName(path),
type: ConfigType.FILE,
updateTime: GetFileUpdateTime(path)
);
if (string.IsNullOrEmpty(path) || !FileUtility.IsFileExists(path))
return null;

return GetCurrentBaseConfig().CreateConfigModel(path);
}

private void SetFileTime(string path)
public bool IsCurrentPathEqualRootConfigPath()
{
File.SetCreationTime(path, DateTime.Now);
File.SetLastWriteTime(path, DateTime.Now);
}
return GetCurrentConfigDirectory() == GetRootConfigDirectory();

string GetCurrentConfigDirectory()
{
string path = getCurrentConfigPath.Invoke();
string directory = FileUtility.GetDirectory(path);

private string GetFileName(string path) => System.IO.Path.GetFileName(path);
if (string.IsNullOrEmpty(path) || !FileUtility.IsFileExists(path))
directory = Directory.CONFIGS;

return FileUtility.GetFullPath(directory);
}

string GetRootConfigDirectory()
{
return FileUtility.GetFullPath(Directory.CONFIGS);
}
}

private string GetFileUpdateTime(string path) => System.IO.File.GetLastWriteTime(path).ToShortDateString();
private BaseConfig GetCurrentBaseConfig()
{
if (IsCurrentPathEqualRootConfigPath())
return generalConfig;
else
return subscriptionConfig;
}
}
}
49 changes: 49 additions & 0 deletions InvisibleMan-XRay/Handlers/Configs/BaseConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Linq;
using System.Collections.Generic;

namespace InvisibleManXRay.Handlers.Configs
{
using Models;
using Utilities;

public abstract class BaseConfig
{
protected Dictionary<string, Config> configs;

public BaseConfig()
{
this.configs = new Dictionary<string, Config>();
}

public List<Config> GetAllConfigs()
{
return configs.Select(config => config.Value).ToList();
}

public void AddConfigToList(Config config)
{
if (configs.ContainsKey(config.Path))
configs[config.Path] = config;
else
configs.Add(config.Path, config);
}

public void RemoveConfigFromList(string path)
{
if (configs.ContainsKey(path))
configs.Remove(path);
}

protected void SetFileTime(string path) => FileUtility.SetFileTimeToNow(path);

protected string GetFileName(string path) => FileUtility.GetFileName(path);

protected string GetDirectory(string path) => FileUtility.GetDirectory(path);

protected string GetFileUpdateTime(string path) => FileUtility.GetFileUpdateTime(path);

public abstract void LoadFiles(string path);

public abstract Config CreateConfigModel(string path);
}
}
70 changes: 70 additions & 0 deletions InvisibleMan-XRay/Handlers/Configs/GeneralConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System.IO;
using System.Linq;

namespace InvisibleManXRay.Handlers.Configs
{
using Models;
using Values;

public class GeneralConfig : BaseConfig
{
public GeneralConfig() : base()
{
LoadFiles();
}

public void CreateConfig(string remark, string data)
{
string destinationPath = $"{Directory.CONFIGS}/{remark}.json";
SaveToDirectory();
SetFileTime(destinationPath);
AddConfigToList(CreateConfigModel(destinationPath));

void SaveToDirectory()
{
System.IO.Directory.CreateDirectory(Directory.CONFIGS);
File.WriteAllText(destinationPath, data);
}
}

public void CopyConfig(string path)
{
string destinationPath = $"{Directory.CONFIGS}/{GetFileName(path)}";
CopyToConfigsDirectory();
SetFileTime(destinationPath);
AddConfigToList(CreateConfigModel(destinationPath));

void CopyToConfigsDirectory()
{
System.IO.Directory.CreateDirectory(Directory.CONFIGS);
File.Copy(path, destinationPath, true);
}
}

public override void LoadFiles(string path = null)
{
configs.Clear();

DirectoryInfo directoryInfo = new DirectoryInfo(Directory.CONFIGS);
if (!directoryInfo.Exists)
return;

FileInfo[] files = directoryInfo.GetFiles().OrderBy(file => file.CreationTime).ToArray();
foreach(FileInfo file in files)
{
AddConfigToList(CreateConfigModel(file.FullName));
}
}

public override Config CreateConfigModel(string path)
{
return new Config(
path: $"{Directory.CONFIGS}/{GetFileName(path)}",
name: GetFileName(path),
type: ConfigType.FILE,
group: GroupType.GENERAL,
updateTime: GetFileUpdateTime(path)
);
}
}
}
Loading

0 comments on commit 11df566

Please sign in to comment.