server filter add regex support

This commit is contained in:
lazycat75246 2023-12-12 16:19:30 +08:00
parent 8a3eb41314
commit 05ff7341bb
3 changed files with 61 additions and 2 deletions

View file

@ -1,5 +1,8 @@
using SQLite; using SQLite;
using SQLitePCL;
using System.Collections; using System.Collections;
using System.Drawing;
using System.Text.RegularExpressions;
namespace v2rayN namespace v2rayN
{ {
@ -17,6 +20,20 @@ namespace v2rayN
_connstr = Utils.GetConfigPath(Global.ConfigDB); _connstr = Utils.GetConfigPath(Global.ConfigDB);
_db = new SQLiteConnection(_connstr, false); _db = new SQLiteConnection(_connstr, false);
_dbAsync = new SQLiteAsyncConnection(_connstr, false); _dbAsync = new SQLiteAsyncConnection(_connstr, false);
SQLitePCL.raw.sqlite3_create_function(_db.Handle, "REGEXP", 2, null, MatchRegex);
SQLitePCL.raw.sqlite3_create_function(_dbAsync.GetConnection().Handle, "REGEXP", 2, null, MatchRegex);
}
private void MatchRegex(sqlite3_context ctx, object user_data, sqlite3_value[] args)
{
lock (objLock)
{
bool isMatched = System.Text.RegularExpressions.Regex.IsMatch(SQLitePCL.raw.sqlite3_value_text(args[1]).utf8_to_string(), SQLitePCL.raw.sqlite3_value_text(args[0]).utf8_to_string(), RegexOptions.IgnoreCase);
if (isMatched)
SQLitePCL.raw.sqlite3_result_int(ctx, 1);
else
SQLitePCL.raw.sqlite3_result_int(ctx, 0);
}
} }
public CreateTableResult CreateTable<T>() public CreateTableResult CreateTable<T>()

View file

@ -1,4 +1,4 @@
using System.Runtime.Intrinsics.X86; using v2rayN.Base;
using v2rayN.Mode; using v2rayN.Mode;
namespace v2rayN.Handler namespace v2rayN.Handler
@ -73,6 +73,19 @@ namespace v2rayN.Handler
return SqliteHelper.Instance.Table<SubItem>().FirstOrDefault(t => t.id == subid); return SqliteHelper.Instance.Table<SubItem>().FirstOrDefault(t => t.id == subid);
} }
public List<SubItem> SubItemIndexs(string filterreg)
{
if (Utils.IsNullOrEmpty(filterreg))
{
return SqliteHelper.Instance.Table<SubItem>().ToList();
}
else
{
var sql = String.Format("SELECT * FROM SubItem WHERE remarks REGEXP '{0}'",filterreg);
return SqliteHelper.Instance.Query<SubItem>(sql);
}
}
public List<ProfileItem> ProfileItems(string subid) public List<ProfileItem> ProfileItems(string subid)
{ {
if (Utils.IsNullOrEmpty(subid)) if (Utils.IsNullOrEmpty(subid))
@ -120,6 +133,35 @@ namespace v2rayN.Handler
return SqliteHelper.Instance.Query<ProfileItemModel>(sql).ToList(); return SqliteHelper.Instance.Query<ProfileItemModel>(sql).ToList();
} }
public List<ProfileItemModel> ProfileItemsReg(string subidfilter, string profilefilter)
{
var sql = @$"SELECT * FROM ProfileItem";
if (!Utils.IsNullOrEmpty(profilefilter))
{
if (profilefilter[0] != '^' && profilefilter[0] != '.')
profilefilter = @".*" + profilefilter;
if (profilefilter[profilefilter.Length - 1] != '$' && profilefilter[profilefilter.Length - 1] != '*')
profilefilter = profilefilter + @".*";
sql += String.Format(" WHERE remarks REGEXP '{0}'", profilefilter);
}
if (!Utils.IsNullOrEmpty(subidfilter))
{
//if (subidfilter[0] != '^' && subidfilter[0] != '.')
// subidfilter = @".*" + subidfilter;
//if (subidfilter[subidfilter.Length-1] != '$' && subidfilter[subidfilter.Length - 1] != '*')
// subidfilter =subidfilter+@".*";
if (!Utils.IsNullOrEmpty(profilefilter))
sql += String.Format(" AND subid REGEXP '{0}'", subidfilter);
else
sql += String.Format(" WHERE subid REGEXP '{0}'", subidfilter);
}
return SqliteHelper.Instance.Query<ProfileItemModel>(sql).ToList();
}
public ProfileItem? GetProfileItem(string indexId) public ProfileItem? GetProfileItem(string indexId)
{ {
if (Utils.IsNullOrEmpty(indexId)) if (Utils.IsNullOrEmpty(indexId))

View file

@ -800,7 +800,7 @@ namespace v2rayN.ViewModels
public void RefreshServers() public void RefreshServers()
{ {
List<ProfileItemModel> lstModel = LazyConfig.Instance.ProfileItems(_subId, _serverFilter); List<ProfileItemModel> lstModel = LazyConfig.Instance.ProfileItemsReg(_subId, _serverFilter);
ConfigHandler.SetDefaultServer(_config, lstModel); ConfigHandler.SetDefaultServer(_config, lstModel);