From 05ff7341bb69be390b70f2b2e24b32e6059ca769 Mon Sep 17 00:00:00 2001 From: lazycat75246 <153997642+lazycat75246@users.noreply.github.com> Date: Tue, 12 Dec 2023 16:19:30 +0800 Subject: [PATCH] server filter add regex support --- v2rayN/v2rayN/Common/SqliteHelper.cs | 17 +++++++ v2rayN/v2rayN/Handler/LazyConfig.cs | 44 ++++++++++++++++++- .../v2rayN/ViewModels/MainWindowViewModel.cs | 2 +- 3 files changed, 61 insertions(+), 2 deletions(-) diff --git a/v2rayN/v2rayN/Common/SqliteHelper.cs b/v2rayN/v2rayN/Common/SqliteHelper.cs index 42ca84f5..f63c6099 100644 --- a/v2rayN/v2rayN/Common/SqliteHelper.cs +++ b/v2rayN/v2rayN/Common/SqliteHelper.cs @@ -1,5 +1,8 @@ using SQLite; +using SQLitePCL; using System.Collections; +using System.Drawing; +using System.Text.RegularExpressions; namespace v2rayN { @@ -17,6 +20,20 @@ namespace v2rayN _connstr = Utils.GetConfigPath(Global.ConfigDB); _db = new SQLiteConnection(_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() diff --git a/v2rayN/v2rayN/Handler/LazyConfig.cs b/v2rayN/v2rayN/Handler/LazyConfig.cs index 0e16af1e..30588f97 100644 --- a/v2rayN/v2rayN/Handler/LazyConfig.cs +++ b/v2rayN/v2rayN/Handler/LazyConfig.cs @@ -1,4 +1,4 @@ -using System.Runtime.Intrinsics.X86; +using v2rayN.Base; using v2rayN.Mode; namespace v2rayN.Handler @@ -73,6 +73,19 @@ namespace v2rayN.Handler return SqliteHelper.Instance.Table().FirstOrDefault(t => t.id == subid); } + public List SubItemIndexs(string filterreg) + { + if (Utils.IsNullOrEmpty(filterreg)) + { + return SqliteHelper.Instance.Table().ToList(); + } + else + { + var sql = String.Format("SELECT * FROM SubItem WHERE remarks REGEXP '{0}'",filterreg); + return SqliteHelper.Instance.Query(sql); + } + } + public List ProfileItems(string subid) { if (Utils.IsNullOrEmpty(subid)) @@ -120,6 +133,35 @@ namespace v2rayN.Handler return SqliteHelper.Instance.Query(sql).ToList(); } + public List 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(sql).ToList(); + } + public ProfileItem? GetProfileItem(string indexId) { if (Utils.IsNullOrEmpty(indexId)) diff --git a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs index c0122284..de6a2b4c 100644 --- a/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs +++ b/v2rayN/v2rayN/ViewModels/MainWindowViewModel.cs @@ -800,7 +800,7 @@ namespace v2rayN.ViewModels public void RefreshServers() { - List lstModel = LazyConfig.Instance.ProfileItems(_subId, _serverFilter); + List lstModel = LazyConfig.Instance.ProfileItemsReg(_subId, _serverFilter); ConfigHandler.SetDefaultServer(_config, lstModel);