-
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #82 from InvisibleManVPN/develop
InvisibleMan XRay version 1.4.4
- Loading branch information
Showing
41 changed files
with
1,715 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.