From 6905eb33f8945e8e18f7128f2e726659f08447e3 Mon Sep 17 00:00:00 2001 From: DHR60 Date: Fri, 1 Aug 2025 11:29:29 +0800 Subject: [PATCH] Fix hy2 custom config --- .../ServiceLib/Handler/CoreConfigHandler.cs | 2 + .../Minimal/CoreConfigHy2Service.cs | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs b/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs index 780e6f2b..ae68ce80 100644 --- a/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/CoreConfigHandler.cs @@ -207,6 +207,8 @@ public class CoreConfigHandler return new CoreConfigClashService(AppHandler.Instance.Config); case ECoreType.sing_box: return new CoreConfigSingboxService(AppHandler.Instance.Config); + case ECoreType.hysteria2: + return new CoreConfigHy2Service(AppHandler.Instance.Config); default: // CoreConfigServiceMinimalBase return new CoreConfigV2rayService(AppHandler.Instance.Config); diff --git a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigHy2Service.cs b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigHy2Service.cs index fe269a5b..9f899969 100644 --- a/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigHy2Service.cs +++ b/v2rayN/ServiceLib/Services/CoreConfig/Minimal/CoreConfigHy2Service.cs @@ -99,4 +99,73 @@ public class CoreConfigHy2Service(Config config) : CoreConfigServiceMinimalBase( return await Task.FromResult(ret); } } + + public override async Task GenerateClientCustomConfig(ProfileItem node, string? fileName) + { + var ret = new RetResult(); + try + { + if (node == null || fileName is null) + { + ret.Msg = ResUI.CheckServerSettings; + return ret; + } + + if (File.Exists(fileName)) + { + File.SetAttributes(fileName, FileAttributes.Normal); //If the file has a read-only attribute, direct deletion will fail + File.Delete(fileName); + } + + string addressFileName = node.Address; + if (!File.Exists(addressFileName)) + { + addressFileName = Utils.GetConfigPath(addressFileName); + } + if (!File.Exists(addressFileName)) + { + ret.Msg = ResUI.FailedGenDefaultConfiguration; + return ret; + } + // Try deserializing the file to check if it is a valid JSON or YAML file + var fileContent = File.ReadAllText(addressFileName); + var jsonContent = JsonUtils.Deserialize(fileContent); + if (jsonContent != null) + { + File.Copy(addressFileName, fileName); + } + else + { + // If it's YAML, convert to JSON and write it + var yamlContent = YamlUtils.FromYaml>(fileContent); + if (yamlContent != null) + { + File.WriteAllText(fileName, JsonUtils.Serialize(yamlContent, true)); + } + else + { + ret.Msg = ResUI.FailedReadConfiguration + "2"; + return ret; + } + } + 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. + + //check again + if (!File.Exists(fileName)) + { + ret.Msg = ResUI.FailedGenDefaultConfiguration; + return ret; + } + + ret.Msg = string.Format(ResUI.SuccessfulConfiguration, ""); + ret.Success = true; + return await Task.FromResult(ret); + } + catch (Exception ex) + { + Logging.SaveLog(_tag, ex); + ret.Msg = ResUI.FailedGenDefaultConfiguration; + return ret; + } + } }