2025-04-02 03:44:23 +00:00
|
|
|
namespace ServiceLib.Handler.Fmt;
|
|
|
|
|
|
|
|
|
|
public class Hysteria2Fmt : BaseFmt
|
2024-06-04 01:48:04 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
public static ProfileItem? Resolve(string str, out string msg)
|
2024-06-04 01:48:04 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
msg = ResUI.ConfigurationFormatIncorrect;
|
|
|
|
|
ProfileItem item = new()
|
2024-06-04 01:48:04 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
ConfigType = EConfigType.Hysteria2
|
|
|
|
|
};
|
2024-06-04 01:48:04 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
var url = Utils.TryUri(str);
|
|
|
|
|
if (url == null)
|
2025-11-02 07:25:41 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
return null;
|
2025-11-02 07:25:41 +00:00
|
|
|
}
|
2024-06-04 01:48:04 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
item.Address = url.IdnHost;
|
|
|
|
|
item.Port = url.Port;
|
|
|
|
|
item.Remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
|
2026-01-21 03:58:29 +00:00
|
|
|
item.Password = Utils.UrlDecode(url.UserInfo);
|
2024-06-04 01:48:04 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
var query = Utils.ParseQueryString(url.Query);
|
2025-11-08 03:14:01 +00:00
|
|
|
ResolveUriQuery(query, ref item);
|
2026-01-17 08:22:26 +00:00
|
|
|
if (item.CertSha.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
item.CertSha = GetQueryDecoded(query, "pinSHA256");
|
|
|
|
|
}
|
2026-02-05 05:15:04 +00:00
|
|
|
item.SetProtocolExtra(item.GetProtocolExtra() with
|
2025-12-25 08:35:55 +00:00
|
|
|
{
|
2026-02-05 05:15:04 +00:00
|
|
|
Ports = GetQueryDecoded(query, "mport"),
|
|
|
|
|
SalamanderPass = GetQueryDecoded(query, "obfs-password"),
|
|
|
|
|
});
|
2024-06-04 01:48:04 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
return item;
|
|
|
|
|
}
|
2024-06-04 01:48:04 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public static string? ToUri(ProfileItem? item)
|
|
|
|
|
{
|
|
|
|
|
if (item == null)
|
2025-11-02 07:25:41 +00:00
|
|
|
{
|
2025-04-02 03:44:23 +00:00
|
|
|
return null;
|
2025-11-02 07:25:41 +00:00
|
|
|
}
|
|
|
|
|
|
2025-10-31 12:25:45 +00:00
|
|
|
var url = string.Empty;
|
2024-06-04 01:48:04 +00:00
|
|
|
|
2025-10-31 12:25:45 +00:00
|
|
|
var remark = string.Empty;
|
2025-04-02 03:44:23 +00:00
|
|
|
if (item.Remarks.IsNotEmpty())
|
|
|
|
|
{
|
|
|
|
|
remark = "#" + Utils.UrlEncode(item.Remarks);
|
2024-06-04 01:48:04 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
var dicQuery = new Dictionary<string, string>();
|
2025-11-08 03:14:01 +00:00
|
|
|
ToUriQueryLite(item, ref dicQuery);
|
2026-02-05 05:15:04 +00:00
|
|
|
var protocolExtraItem = item.GetProtocolExtra();
|
2025-11-09 07:17:08 +00:00
|
|
|
|
2026-02-05 05:15:04 +00:00
|
|
|
if (!protocolExtraItem.SalamanderPass.IsNullOrEmpty())
|
2025-04-02 03:44:23 +00:00
|
|
|
{
|
|
|
|
|
dicQuery.Add("obfs", "salamander");
|
2026-02-05 05:15:04 +00:00
|
|
|
dicQuery.Add("obfs-password", Utils.UrlEncode(protocolExtraItem.SalamanderPass));
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2026-02-05 05:15:04 +00:00
|
|
|
if (!protocolExtraItem.Ports.IsNullOrEmpty())
|
2025-04-02 03:44:23 +00:00
|
|
|
{
|
2026-02-05 05:15:04 +00:00
|
|
|
dicQuery.Add("mport", Utils.UrlEncode(protocolExtraItem.Ports.Replace(':', '-')));
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2026-01-17 08:22:26 +00:00
|
|
|
if (!item.CertSha.IsNullOrEmpty())
|
|
|
|
|
{
|
|
|
|
|
var sha = item.CertSha;
|
|
|
|
|
var idx = sha.IndexOf('~');
|
|
|
|
|
if (idx > 0)
|
|
|
|
|
{
|
|
|
|
|
sha = sha[..idx];
|
|
|
|
|
}
|
|
|
|
|
dicQuery.Add("pinSHA256", Utils.UrlEncode(sha));
|
|
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
|
2026-01-21 03:58:29 +00:00
|
|
|
return ToUri(EConfigType.Hysteria2, item.Address, item.Port, item.Password, dicQuery, remark);
|
2025-04-02 03:44:23 +00:00
|
|
|
}
|
2024-06-21 03:15:35 +00:00
|
|
|
|
2025-04-02 03:44:23 +00:00
|
|
|
public static ProfileItem? ResolveFull2(string strData, string? subRemarks)
|
|
|
|
|
{
|
|
|
|
|
if (Contains(strData, "server", "auth", "up", "down", "listen"))
|
|
|
|
|
{
|
|
|
|
|
var fileName = WriteAllText(strData);
|
|
|
|
|
|
|
|
|
|
var profileItem = new ProfileItem
|
|
|
|
|
{
|
|
|
|
|
CoreType = ECoreType.hysteria2,
|
|
|
|
|
Address = fileName,
|
|
|
|
|
Remarks = subRemarks ?? "hysteria2_custom"
|
|
|
|
|
};
|
|
|
|
|
return profileItem;
|
2024-06-21 03:15:35 +00:00
|
|
|
}
|
2025-04-02 03:44:23 +00:00
|
|
|
|
|
|
|
|
return null;
|
2024-06-04 01:48:04 +00:00
|
|
|
}
|
2025-01-30 09:10:05 +00:00
|
|
|
}
|