Adding checks for subscription url

pull/5940/head
2dust 2024-10-24 17:09:07 +08:00
parent 3fafc6de93
commit 34f5c0f910
11 changed files with 69 additions and 34 deletions

View File

@ -427,6 +427,30 @@ namespace ServiceLib.Common
return false;
}
public static Uri? TryUri(string url)
{
try
{
return new Uri(url);
}
catch (UriFormatException)
{
return null;
}
}
public static bool IsPrivateNetwork(string ip)
{
if (IPAddress.TryParse(ip, out var address))
{
var ipBytes = address.GetAddressBytes();
if (ipBytes[0] == 10) return true;
if (ipBytes[0] == 172 && ipBytes[1] >= 16 && ipBytes[1] <= 31) return true;
if (ipBytes[0] == 192 && ipBytes[1] == 168) return true;
}
return false;
}
#endregion 数据检查
#region 测速
@ -632,7 +656,7 @@ namespace ServiceLib.Common
}
return Path.Combine(startupPath, fileName);
}
public static string GetExePath()
{
return Environment.ProcessPath ?? Process.GetCurrentProcess().MainModule?.FileName ?? string.Empty;

View File

@ -1335,17 +1335,17 @@ namespace ServiceLib.Handler
Url = url
};
try
var uri = Utils.TryUri(url);
if (uri == null) return -1;
//Do not allow http protocol
if (url.StartsWith(Global.HttpProtocol) && !Utils.IsPrivateNetwork(uri.IdnHost))
{
var uri = new Uri(url);
var queryVars = Utils.ParseQueryString(uri.Query);
subItem.Remarks = queryVars["remarks"] ?? "import_sub";
}
catch (UriFormatException)
{
return 0;
return -1;
}
var queryVars = Utils.ParseQueryString(uri.Query);
subItem.Remarks = queryVars["remarks"] ?? "import_sub";
return await AddSubItem(config, subItem);
}

View File

@ -10,7 +10,8 @@
ConfigType = EConfigType.Hysteria2
};
Uri url = new(str);
var url = Utils.TryUri(str);
if (url == null) return null;
item.Address = url.IdnHost;
item.Port = url.Port;

View File

@ -81,15 +81,9 @@ namespace ServiceLib.Handler.Fmt
private static ProfileItem? ResolveSip002(string result)
{
Uri parsedUrl;
try
{
parsedUrl = new Uri(result);
}
catch (UriFormatException)
{
return null;
}
var parsedUrl = Utils.TryUri(result);
if (parsedUrl == null) return null;
ProfileItem item = new()
{
Remarks = parsedUrl.GetComponents(UriComponents.Fragment, UriFormat.Unescaped),

View File

@ -93,15 +93,9 @@
private static ProfileItem? ResolveSocksNew(string result)
{
Uri parsedUrl;
try
{
parsedUrl = new Uri(result);
}
catch (UriFormatException)
{
return null;
}
var parsedUrl = Utils.TryUri(result);
if (parsedUrl == null) return null;
ProfileItem item = new()
{
Remarks = parsedUrl.GetComponents(UriComponents.Fragment, UriFormat.Unescaped),

View File

@ -10,8 +10,9 @@
{
ConfigType = EConfigType.Trojan
};
Uri url = new(str);
var url = Utils.TryUri(str);
if (url == null) return null;
item.Address = url.IdnHost;
item.Port = url.Port;

View File

@ -11,7 +11,8 @@
ConfigType = EConfigType.TUIC
};
Uri url = new(str);
var url = Utils.TryUri(str);
if (url == null) return null;
item.Address = url.IdnHost;
item.Port = url.Port;

View File

@ -12,7 +12,8 @@
Security = Global.None
};
Uri url = new(str);
var url = Utils.TryUri(str);
if (url == null) return null;
item.Address = url.IdnHost;
item.Port = url.Port;

View File

@ -105,7 +105,8 @@
Security = "auto"
};
Uri url = new(str);
var url = Utils.TryUri(str);
if (url == null) return null;
item.Address = url.IdnHost;
item.Port = url.Port;

View File

@ -11,7 +11,8 @@
ConfigType = EConfigType.WireGuard
};
Uri url = new(str);
var url = Utils.TryUri(str);
if (url == null) return null;
item.Address = url.IdnHost;
item.Port = url.Port;

View File

@ -33,6 +33,23 @@ namespace ServiceLib.ViewModels
return;
}
var url = SelectedSource.Url;
if (url.IsNotEmpty())
{
var uri = Utils.TryUri(url);
if (uri == null)
{
NoticeHandler.Instance.Enqueue(ResUI.LvUrl);
return;
}
//Do not allow http protocol
if (url.StartsWith(Global.HttpProtocol) && !Utils.IsPrivateNetwork(uri.IdnHost))
{
NoticeHandler.Instance.Enqueue(ResUI.LvUrl);
return;
}
}
if (await ConfigHandler.AddSubItem(_config, SelectedSource) == 0)
{
NoticeHandler.Instance.Enqueue(ResUI.OperationSuccess);