v2rayN/v2rayN/ServiceLib/Handler/CoreConfig/CoreConfigHandler.cs

171 lines
6.5 KiB
C#
Raw Normal View History

2024-08-19 10:15:54 +00:00
namespace ServiceLib.Handler.CoreConfig
2019-10-11 06:15:20 +00:00
{
/// <summary>
2023-01-01 11:42:01 +00:00
/// Core configuration file processing class
2019-10-11 06:15:20 +00:00
/// </summary>
2024-08-19 10:15:54 +00:00
public class CoreConfigHandler
2019-10-11 06:15:20 +00:00
{
2023-02-19 04:18:08 +00:00
public static int GenerateClientConfig(ProfileItem node, string? fileName, out string msg, out string content)
2019-10-11 06:15:20 +00:00
{
content = string.Empty;
2019-10-11 06:15:20 +00:00
try
{
2022-03-13 02:41:04 +00:00
if (node == null)
2019-10-11 06:15:20 +00:00
{
2022-03-31 11:53:58 +00:00
msg = ResUI.CheckServerSettings;
2019-10-11 06:15:20 +00:00
return -1;
}
2024-08-15 13:03:00 +00:00
var config = LazyConfig.Instance.Config;
2019-10-11 06:15:20 +00:00
2022-03-31 11:53:58 +00:00
msg = ResUI.InitialConfiguration;
2022-03-18 23:53:48 +00:00
if (node.configType == EConfigType.Custom)
2019-10-11 06:15:20 +00:00
{
if (node.coreType is ECoreType.clash or ECoreType.clash_meta or ECoreType.mihomo)
{
var configGenClash = new CoreConfigClash(config);
2024-07-14 09:16:07 +00:00
return configGenClash.GenerateClientCustomConfig(node, fileName, out msg);
}
if (node.coreType is ECoreType.sing_box)
{
var configGenSingbox = new CoreConfigSingbox(config);
return configGenSingbox.GenerateClientCustomConfig(node, fileName, out msg);
}
else
{
return GenerateClientCustomConfig(node, fileName, out msg);
}
2019-10-11 06:15:20 +00:00
}
else if (LazyConfig.Instance.GetCoreType(node, node.configType) == ECoreType.sing_box)
2019-10-11 06:15:20 +00:00
{
2023-05-04 03:44:51 +00:00
var configGenSingbox = new CoreConfigSingbox(config);
2023-04-23 12:21:52 +00:00
if (configGenSingbox.GenerateClientConfigContent(node, out SingboxConfig? singboxConfig, out msg) != 0)
{
return -1;
}
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(fileName))
{
2024-03-26 06:26:03 +00:00
content = JsonUtils.Serialize(singboxConfig);
2019-10-11 06:15:20 +00:00
}
else
{
2024-03-26 06:26:03 +00:00
JsonUtils.ToFile(singboxConfig, fileName, false);
2019-10-11 06:15:20 +00:00
}
}
else
{
2023-05-04 03:44:51 +00:00
var coreConfigV2ray = new CoreConfigV2ray(config);
2023-04-23 12:21:52 +00:00
if (coreConfigV2ray.GenerateClientConfigContent(node, out V2rayConfig? v2rayConfig, out msg) != 0)
2019-10-11 06:15:20 +00:00
{
2023-04-23 12:21:52 +00:00
return -1;
2019-10-11 06:15:20 +00:00
}
2024-03-26 06:26:03 +00:00
if (Utils.IsNullOrEmpty(fileName))
2023-01-01 11:42:01 +00:00
{
2024-03-26 06:26:03 +00:00
content = JsonUtils.Serialize(v2rayConfig);
2023-01-01 11:42:01 +00:00
}
else
{
2024-03-26 06:26:03 +00:00
JsonUtils.ToFile(v2rayConfig, fileName, false);
2022-04-14 01:56:11 +00:00
}
}
2019-10-11 06:15:20 +00:00
}
2022-07-02 03:11:23 +00:00
catch (Exception ex)
2019-10-11 06:15:20 +00:00
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog("GenerateClientConfig", ex);
2023-04-23 12:21:52 +00:00
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
2019-10-11 06:15:20 +00:00
}
return 0;
}
2023-04-23 12:21:52 +00:00
private static int GenerateClientCustomConfig(ProfileItem node, string? fileName, out string msg)
2019-10-11 06:15:20 +00:00
{
try
{
2023-04-23 12:21:52 +00:00
if (node == null || fileName is null)
2019-10-11 06:15:20 +00:00
{
2023-04-23 12:21:52 +00:00
msg = ResUI.CheckServerSettings;
return -1;
2019-10-11 06:15:20 +00:00
}
2023-04-14 12:49:36 +00:00
2023-04-23 12:21:52 +00:00
if (File.Exists(fileName))
2023-01-01 11:42:01 +00:00
{
File.SetAttributes(fileName, FileAttributes.Normal); //If the file has a read-only attribute, direct deletion will fail
2023-04-23 12:21:52 +00:00
File.Delete(fileName);
2023-01-01 11:42:01 +00:00
}
2020-12-31 12:15:01 +00:00
2023-04-23 12:21:52 +00:00
string addressFileName = node.address;
if (!File.Exists(addressFileName))
2019-10-11 06:15:20 +00:00
{
2024-03-26 06:26:03 +00:00
addressFileName = Utils.GetConfigPath(addressFileName);
2020-12-30 07:55:37 +00:00
}
2023-04-23 12:21:52 +00:00
if (!File.Exists(addressFileName))
2020-12-30 07:55:37 +00:00
{
2023-04-23 12:21:52 +00:00
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
2019-10-11 06:15:20 +00:00
}
2023-04-23 12:21:52 +00:00
File.Copy(addressFileName, fileName);
File.SetAttributes(fileName, FileAttributes.Normal); //Copy will keep the attributes of addressFileName, so we need to add write permissions to fileName just in case of addressFileName is a read-only file.
2019-10-11 06:15:20 +00:00
2023-04-23 12:21:52 +00:00
//check again
if (!File.Exists(fileName))
2019-10-11 06:15:20 +00:00
{
2023-04-23 12:21:52 +00:00
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
2019-10-11 06:15:20 +00:00
}
2023-04-23 12:21:52 +00:00
msg = string.Format(ResUI.SuccessfulConfiguration, "");
2019-10-11 06:15:20 +00:00
}
2022-07-02 03:11:23 +00:00
catch (Exception ex)
2019-10-11 06:15:20 +00:00
{
2024-01-10 02:43:48 +00:00
Logging.SaveLog("GenerateClientCustomConfig", ex);
2023-04-23 12:21:52 +00:00
msg = ResUI.FailedGenDefaultConfiguration;
return -1;
2019-10-11 06:15:20 +00:00
}
return 0;
}
public static int GenerateClientSpeedtestConfig(Config config, string fileName, List<ServerTestItem> selecteds, ECoreType coreType, out string msg)
2019-10-11 06:15:20 +00:00
{
if (coreType == ECoreType.sing_box)
{
2024-06-04 01:48:04 +00:00
if (new CoreConfigSingbox(config).GenerateClientSpeedtestConfig(selecteds, out SingboxConfig? singboxConfig, out msg) != 0)
{
return -1;
}
2024-03-26 06:26:03 +00:00
JsonUtils.ToFile(singboxConfig, fileName, false);
}
else
{
2024-06-04 01:48:04 +00:00
if (new CoreConfigV2ray(config).GenerateClientSpeedtestConfig(selecteds, out V2rayConfig? v2rayConfig, out msg) != 0)
{
return -1;
}
2024-03-26 06:26:03 +00:00
JsonUtils.ToFile(v2rayConfig, fileName, false);
}
return 0;
2019-10-11 06:15:20 +00:00
}
2024-07-21 03:26:10 +00:00
public static int GenerateClientMultipleLoadConfig(Config config, string fileName, List<ProfileItem> selecteds, ECoreType coreType, out string msg)
{
2024-07-21 03:26:10 +00:00
msg = ResUI.CheckServerSettings;
if (coreType == ECoreType.sing_box)
{
2024-07-21 03:26:10 +00:00
if (new CoreConfigSingbox(config).GenerateClientMultipleLoadConfig(selecteds, out SingboxConfig? singboxConfig, out msg) != 0)
{
return -1;
}
JsonUtils.ToFile(singboxConfig, fileName, false);
}
else if (coreType == ECoreType.Xray)
{
if (new CoreConfigV2ray(config).GenerateClientMultipleLoadConfig(selecteds, out V2rayConfig? v2rayConfig, out msg) != 0)
{
return -1;
}
JsonUtils.ToFile(v2rayConfig, fileName, false);
}
return 0;
}
2019-10-11 06:15:20 +00:00
}
2023-11-28 08:07:53 +00:00
}