mirror of
https://github.com/bolucat/Archive.git
synced 2025-09-26 20:21:35 +08:00
49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
namespace ServiceLib.Handler.Fmt
|
|
{
|
|
public class TrojanFmt : BaseFmt
|
|
{
|
|
public static ProfileItem? Resolve(string str, out string msg)
|
|
{
|
|
msg = ResUI.ConfigurationFormatIncorrect;
|
|
|
|
ProfileItem item = new()
|
|
{
|
|
configType = EConfigType.Trojan
|
|
};
|
|
|
|
Uri url = new(str);
|
|
|
|
item.address = url.IdnHost;
|
|
item.port = url.Port;
|
|
item.remarks = url.GetComponents(UriComponents.Fragment, UriFormat.Unescaped);
|
|
item.id = Utils.UrlDecode(url.UserInfo);
|
|
|
|
var query = Utils.ParseQueryString(url.Query);
|
|
ResolveStdTransport(query, ref item);
|
|
|
|
return item;
|
|
}
|
|
|
|
public static string? ToUri(ProfileItem? item)
|
|
{
|
|
if (item == null) return null;
|
|
string url = string.Empty;
|
|
|
|
string remark = string.Empty;
|
|
if (Utils.IsNotEmpty(item.remarks))
|
|
{
|
|
remark = "#" + Utils.UrlEncode(item.remarks);
|
|
}
|
|
var dicQuery = new Dictionary<string, string>();
|
|
GetStdTransport(item, null, ref dicQuery);
|
|
string query = "?" + string.Join("&", dicQuery.Select(x => x.Key + "=" + x.Value).ToArray());
|
|
|
|
url = string.Format("{0}@{1}:{2}",
|
|
item.id,
|
|
GetIpv6(item.address),
|
|
item.port);
|
|
url = $"{Global.ProtocolShares[EConfigType.Trojan]}{url}{query}{remark}";
|
|
return url;
|
|
}
|
|
}
|
|
} |