From 0b9ab2eaec7d767a280f6c6ebc865fe3efde4da6 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Fri, 25 Jun 2021 11:05:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=88=97=E8=A1=A8=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E6=90=9C=E7=B4=A2=E7=AB=AF=E5=8F=A3=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/server_dao.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/internal/db/models/server_dao.go b/internal/db/models/server_dao.go index 0a1a6e8f..c7c97a12 100644 --- a/internal/db/models/server_dao.go +++ b/internal/db/models/server_dao.go @@ -18,6 +18,7 @@ import ( "github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/rands" "github.com/iwind/TeaGo/types" + "regexp" "strconv" "strings" "time" @@ -640,8 +641,14 @@ func (this *ServerDAO) CountAllEnabledServersMatch(tx *dbs.Tx, groupId int64, ke Param("groupId", numberutils.FormatInt64(groupId)) } if len(keyword) > 0 { - query.Where("(name LIKE :keyword OR serverNames LIKE :keyword)"). - Param("keyword", "%"+keyword+"%") + if regexp.MustCompile(`^\d+$`).MatchString(keyword) { + query.Where("(name LIKE :keyword OR serverNames LIKE :keyword OR JSON_CONTAINS(http, :portRange, '$.listen') OR JSON_CONTAINS(https, :portRange, '$.listen') OR JSON_CONTAINS(tcp, :portRange, '$.listen') OR JSON_CONTAINS(tls, :portRange, '$.listen'))"). + Param("portRange", maps.Map{"portRange": keyword}.AsJSON()). + Param("keyword", "%"+keyword+"%") + } else { + query.Where("(name LIKE :keyword OR serverNames LIKE :keyword)"). + Param("keyword", "%"+keyword+"%") + } } if userId > 0 { query.Attr("userId", userId) @@ -674,8 +681,13 @@ func (this *ServerDAO) ListEnabledServersMatch(tx *dbs.Tx, offset int64, size in Param("groupId", numberutils.FormatInt64(groupId)) } if len(keyword) > 0 { - query.Where("(name LIKE :keyword OR serverNames LIKE :keyword)"). - Param("keyword", "%"+keyword+"%") + if regexp.MustCompile(`^\d+$`).MatchString(keyword) { + query.Where("(name LIKE :keyword OR serverNames LIKE :keyword OR JSON_CONTAINS(http, :portRange, '$.listen') OR JSON_CONTAINS(https, :portRange, '$.listen') OR JSON_CONTAINS(tcp, :portRange, '$.listen') OR JSON_CONTAINS(tls, :portRange, '$.listen'))"). + Param("portRange", string(maps.Map{"portRange": keyword}.AsJSON())) + } else { + query.Where("(name LIKE :keyword OR serverNames LIKE :keyword)"). + Param("keyword", "%"+keyword+"%") + } } if userId > 0 { query.Attr("userId", userId)