From f15d114708f95636724e4fc920ebcdd6dfe4b6c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Fri, 10 Nov 2023 16:36:09 +0800 Subject: [PATCH] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E2=80=9C=E8=B7=B3=E8=BD=ACURL=E2=80=9D?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/models/http_page_dao.go | 7 ++--- internal/rpc/services/service_http_page.go | 32 ++++++++++++++++++---- internal/rpc/services/service_http_web.go | 16 +++++++++-- 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/internal/db/models/http_page_dao.go b/internal/db/models/http_page_dao.go index a36f91a9..93447932 100644 --- a/internal/db/models/http_page_dao.go +++ b/internal/db/models/http_page_dao.go @@ -5,7 +5,6 @@ import ( "errors" "github.com/TeaOSLab/EdgeAPI/internal/utils" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" - "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" _ "github.com/go-sql-driver/mysql" "github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/dbs" @@ -77,7 +76,7 @@ func (this *HTTPPageDAO) FindEnabledHTTPPage(tx *dbs.Tx, id int64) (*HTTPPage, e } // CreatePage 创建Page -func (this *HTTPPageDAO) CreatePage(tx *dbs.Tx, userId int64, statusList []string, bodyType shared.BodyType, url string, body string, newStatus int) (pageId int64, err error) { +func (this *HTTPPageDAO) CreatePage(tx *dbs.Tx, userId int64, statusList []string, bodyType serverconfigs.HTTPPageBodyType, url string, body string, newStatus int) (pageId int64, err error) { var op = NewHTTPPageOperator() op.UserId = userId op.IsOn = true @@ -103,7 +102,7 @@ func (this *HTTPPageDAO) CreatePage(tx *dbs.Tx, userId int64, statusList []strin } // UpdatePage 修改Page -func (this *HTTPPageDAO) UpdatePage(tx *dbs.Tx, pageId int64, statusList []string, bodyType shared.BodyType, url string, body string, newStatus int) error { +func (this *HTTPPageDAO) UpdatePage(tx *dbs.Tx, pageId int64, statusList []string, bodyType serverconfigs.HTTPPageBodyType, url string, body string, newStatus int) error { if pageId <= 0 { return errors.New("invalid pageId") } @@ -188,7 +187,7 @@ func (this *HTTPPageDAO) ComposePageConfig(tx *dbs.Tx, pageId int64, cacheMap *u config.BodyType = page.BodyType if len(page.BodyType) == 0 { - page.BodyType = shared.BodyTypeURL + page.BodyType = serverconfigs.HTTPPageBodyTypeURL } if len(page.StatusList) > 0 { diff --git a/internal/rpc/services/service_http_page.go b/internal/rpc/services/service_http_page.go index fe7473cb..3cbd09f7 100644 --- a/internal/rpc/services/service_http_page.go +++ b/internal/rpc/services/service_http_page.go @@ -7,7 +7,7 @@ import ( "github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" - "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" + "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/iwind/TeaGo/types" ) @@ -30,7 +30,7 @@ func (this *HTTPPageService) CreateHTTPPage(ctx context.Context, req *pb.CreateH const maxBodyLength = 32 * 1024 switch req.BodyType { - case shared.BodyTypeURL: + case serverconfigs.HTTPPageBodyTypeURL: if len(req.Url) > maxURLLength { return nil, errors.New("'url' too long") } @@ -41,7 +41,18 @@ func (this *HTTPPageService) CreateHTTPPage(ctx context.Context, req *pb.CreateH if len(req.Body) > maxBodyLength { // we keep short body for user experience req.Body = "" } - case shared.BodyTypeHTML: + case serverconfigs.HTTPPageBodyTypeRedirectURL: + if len(req.Url) > maxURLLength { + return nil, errors.New("'url' too long") + } + if !regexputils.HTTPProtocol.MatchString(req.Url) { + return nil, errors.New("invalid 'url' format") + } + + if len(req.Body) > maxBodyLength { // we keep short body for user experience + req.Body = "" + } + case serverconfigs.HTTPPageBodyTypeHTML: if len(req.Body) > maxBodyLength { return nil, errors.New("'body' too long") } @@ -82,7 +93,7 @@ func (this *HTTPPageService) UpdateHTTPPage(ctx context.Context, req *pb.UpdateH const maxBodyLength = 32 * 1024 switch req.BodyType { - case shared.BodyTypeURL: + case serverconfigs.HTTPPageBodyTypeURL: if len(req.Url) > maxURLLength { return nil, errors.New("'url' too long") } @@ -93,7 +104,18 @@ func (this *HTTPPageService) UpdateHTTPPage(ctx context.Context, req *pb.UpdateH if len(req.Body) > maxBodyLength { // we keep short body for user experience req.Body = "" } - case shared.BodyTypeHTML: + case serverconfigs.HTTPPageBodyTypeRedirectURL: + if len(req.Url) > maxURLLength { + return nil, errors.New("'url' too long") + } + if !regexputils.HTTPProtocol.MatchString(req.Url) { + return nil, errors.New("invalid 'url' format") + } + + if len(req.Body) > maxBodyLength { // we keep short body for user experience + req.Body = "" + } + case serverconfigs.HTTPPageBodyTypeHTML: if len(req.Body) > maxBodyLength { return nil, errors.New("'body' too long") } diff --git a/internal/rpc/services/service_http_web.go b/internal/rpc/services/service_http_web.go index 7e01adfc..c2900f2e 100644 --- a/internal/rpc/services/service_http_web.go +++ b/internal/rpc/services/service_http_web.go @@ -8,7 +8,6 @@ import ( "github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" - "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs/shared" "github.com/iwind/TeaGo/dbs" ) @@ -365,7 +364,7 @@ func (this *HTTPWebService) UpdateHTTPWebShutdown(ctx context.Context, req *pb.U } switch shutdownConfig.BodyType { - case shared.BodyTypeURL: + case serverconfigs.HTTPPageBodyTypeURL: if len(shutdownConfig.URL) > maxURLLength { return nil, errors.New("'url' too long") } @@ -376,7 +375,18 @@ func (this *HTTPWebService) UpdateHTTPWebShutdown(ctx context.Context, req *pb.U if len(shutdownConfig.Body) > maxBodyLength { // we keep short body for user experience shutdownConfig.Body = "" } - case shared.BodyTypeHTML: + case serverconfigs.HTTPPageBodyTypeRedirectURL: + if len(shutdownConfig.URL) > maxURLLength { + return nil, errors.New("'url' too long") + } + if shutdownConfig.IsOn /** validate when it's on **/ && !regexputils.HTTPProtocol.MatchString(shutdownConfig.URL) { + return nil, errors.New("invalid 'url' format") + } + + if len(shutdownConfig.Body) > maxBodyLength { // we keep short body for user experience + shutdownConfig.Body = "" + } + case serverconfigs.HTTPPageBodyTypeHTML: if len(shutdownConfig.Body) > maxBodyLength { return nil, errors.New("'body' too long") }