2024-08-19 10:15:54 +00:00
|
|
|
|
namespace ServiceLib.Handler.Fmt
|
2024-06-04 01:48:04 +00:00
|
|
|
|
{
|
2024-08-19 10:15:54 +00:00
|
|
|
|
public class TuicFmt : BaseFmt
|
2024-06-04 01:48:04 +00:00
|
|
|
|
{
|
|
|
|
|
|
public static ProfileItem? Resolve(string str, out string msg)
|
|
|
|
|
|
{
|
|
|
|
|
|
msg = ResUI.ConfigurationFormatIncorrect;
|
|
|
|
|
|
|
|
|
|
|
|
ProfileItem item = new()
|
|
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
ConfigType = EConfigType.TUIC
|
2024-06-04 01:48:04 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
2024-10-24 09:09:07 +00:00
|
|
|
|
var url = Utils.TryUri(str);
|
|
|
|
|
|
if (url == null) return null;
|
2024-06-04 01:48:04 +00:00
|
|
|
|
|
2024-10-23 09:00:33 +00:00
|
|
|
|
item.Address = url.IdnHost;
|
|
|
|
|
|
item.Port = url.Port;
|
|
|
|
|
|
item.Remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
|
2024-10-15 03:02:17 +00:00
|
|
|
|
var rawUserInfo = Utils.UrlDecode(url.UserInfo);
|
|
|
|
|
|
var userInfoParts = rawUserInfo.Split(new[] { ':' }, 2);
|
2024-06-04 01:48:04 +00:00
|
|
|
|
if (userInfoParts.Length == 2)
|
|
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
item.Id = userInfoParts[0];
|
|
|
|
|
|
item.Security = userInfoParts[1];
|
2024-06-04 01:48:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var query = Utils.ParseQueryString(url.Query);
|
|
|
|
|
|
ResolveStdTransport(query, ref item);
|
2024-10-23 09:00:33 +00:00
|
|
|
|
item.HeaderType = query["congestion_control"] ?? "";
|
2024-06-04 01:48:04 +00:00
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string? ToUri(ProfileItem? item)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (item == null) return null;
|
|
|
|
|
|
string url = string.Empty;
|
|
|
|
|
|
|
|
|
|
|
|
string remark = string.Empty;
|
2024-10-23 09:00:33 +00:00
|
|
|
|
if (Utils.IsNotEmpty(item.Remarks))
|
2024-06-04 01:48:04 +00:00
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
remark = "#" + Utils.UrlEncode(item.Remarks);
|
2024-06-04 01:48:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
var dicQuery = new Dictionary<string, string>();
|
2024-10-23 09:00:33 +00:00
|
|
|
|
if (Utils.IsNotEmpty(item.Sni))
|
2024-06-04 01:48:04 +00:00
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
dicQuery.Add("sni", item.Sni);
|
2024-06-04 01:48:04 +00:00
|
|
|
|
}
|
2024-10-23 09:00:33 +00:00
|
|
|
|
if (Utils.IsNotEmpty(item.Alpn))
|
2024-06-04 01:48:04 +00:00
|
|
|
|
{
|
2024-10-23 09:00:33 +00:00
|
|
|
|
dicQuery.Add("alpn", Utils.UrlEncode(item.Alpn));
|
2024-06-04 01:48:04 +00:00
|
|
|
|
}
|
2024-10-23 09:00:33 +00:00
|
|
|
|
dicQuery.Add("congestion_control", item.HeaderType);
|
2024-06-04 01:48:04 +00:00
|
|
|
|
|
2024-10-23 09:00:33 +00:00
|
|
|
|
return ToUri(EConfigType.TUIC, item.Address, item.Port, $"{item.Id}:{item.Security}", dicQuery, remark);
|
2024-06-04 01:48:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|