优化自定义页面设置,页面URL不再支持填写本地文件

This commit is contained in:
GoEdgeLab
2023-07-07 11:48:48 +08:00
parent 175607fbb4
commit de9c6fa515
2 changed files with 123 additions and 8 deletions

View File

@@ -3,8 +3,11 @@ package services
import (
"context"
"encoding/json"
"errors"
"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/iwind/TeaGo/types"
)
@@ -22,6 +25,34 @@ func (this *HTTPPageService) CreateHTTPPage(ctx context.Context, req *pb.CreateH
var tx = this.NullTx()
// validate
const maxURLLength = 512
const maxBodyLength = 32 * 1024
switch req.BodyType {
case shared.BodyTypeURL:
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 shared.BodyTypeHTML:
if len(req.Body) > maxBodyLength {
return nil, errors.New("'body' too long")
}
if len(req.Url) > maxURLLength { // we keep short url for user experience
req.Url = ""
}
default:
return nil, errors.New("invalid 'bodyType': " + req.BodyType)
}
pageId, err := models.SharedHTTPPageDAO.CreatePage(tx, userId, req.StatusList, req.BodyType, req.Url, req.Body, types.Int(req.NewStatus))
if err != nil {
return nil, err
@@ -46,6 +77,34 @@ func (this *HTTPPageService) UpdateHTTPPage(ctx context.Context, req *pb.UpdateH
}
}
// validate
const maxURLLength = 512
const maxBodyLength = 32 * 1024
switch req.BodyType {
case shared.BodyTypeURL:
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 shared.BodyTypeHTML:
if len(req.Body) > maxBodyLength {
return nil, errors.New("'body' too long")
}
if len(req.Url) > maxURLLength { // we keep short url for user experience
req.Url = ""
}
default:
return nil, errors.New("invalid 'bodyType': " + req.BodyType)
}
err = models.SharedHTTPPageDAO.UpdatePage(tx, req.HttpPageId, req.StatusList, req.BodyType, req.Url, req.Body, types.Int(req.NewStatus))
if err != nil {
return nil, err