2020-09-15 14:44:11 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2020-09-21 19:51:56 +08:00
|
|
|
"encoding/json"
|
2020-09-15 14:44:11 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
2021-01-10 17:34:35 +08:00
|
|
|
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
2020-09-15 14:44:11 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
2021-01-10 17:34:35 +08:00
|
|
|
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
|
|
|
|
|
"github.com/iwind/TeaGo/dbs"
|
2020-09-15 14:44:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type HTTPWebService struct {
|
2020-11-24 15:02:44 +08:00
|
|
|
BaseService
|
2020-09-15 14:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// CreateHTTPWeb 创建Web配置
|
2020-09-15 14:44:11 +08:00
|
|
|
func (this *HTTPWebService) CreateHTTPWeb(ctx context.Context, req *pb.CreateHTTPWebRequest) (*pb.CreateHTTPWebResponse, error) {
|
|
|
|
|
// 校验请求
|
2020-12-18 21:18:53 +08:00
|
|
|
adminId, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-15 14:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
|
|
|
|
webId, err := models.SharedHTTPWebDAO.CreateWeb(tx, adminId, userId, req.RootJSON)
|
2020-09-15 14:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
return &pb.CreateHTTPWebResponse{HttpWebId: webId}, nil
|
2020-09-15 14:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// FindEnabledHTTPWeb 查找Web配置
|
2020-09-15 14:44:11 +08:00
|
|
|
func (this *HTTPWebService) FindEnabledHTTPWeb(ctx context.Context, req *pb.FindEnabledHTTPWebRequest) (*pb.FindEnabledHTTPWebResponse, error) {
|
|
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-15 14:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
web, err := models.SharedHTTPWebDAO.FindEnabledHTTPWeb(tx, req.HttpWebId)
|
2020-09-15 14:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if web == nil {
|
2021-11-24 12:00:38 +08:00
|
|
|
return &pb.FindEnabledHTTPWebResponse{HttpWeb: nil}, nil
|
2020-09-15 14:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result := &pb.HTTPWeb{}
|
|
|
|
|
result.Id = int64(web.Id)
|
2020-09-16 09:09:21 +08:00
|
|
|
result.IsOn = web.IsOn == 1
|
2021-11-24 12:00:38 +08:00
|
|
|
return &pb.FindEnabledHTTPWebResponse{HttpWeb: result}, nil
|
2020-09-15 14:44:11 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// FindEnabledHTTPWebConfig 查找Web配置
|
2020-09-21 19:51:56 +08:00
|
|
|
func (this *HTTPWebService) FindEnabledHTTPWebConfig(ctx context.Context, req *pb.FindEnabledHTTPWebConfigRequest) (*pb.FindEnabledHTTPWebConfigResponse, error) {
|
|
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-21 19:51:56 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
config, err := models.SharedHTTPWebDAO.ComposeWebConfig(tx, req.HttpWebId, nil)
|
2020-09-21 19:51:56 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
configJSON, err := json.Marshal(config)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-11-24 12:00:38 +08:00
|
|
|
return &pb.FindEnabledHTTPWebConfigResponse{HttpWebJSON: configJSON}, nil
|
2020-09-21 19:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWeb 修改Web配置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWeb(ctx context.Context, req *pb.UpdateHTTPWebRequest) (*pb.RPCSuccess, error) {
|
2020-09-15 14:44:11 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-15 14:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWeb(tx, req.HttpWebId, req.RootJSON)
|
2020-09-15 14:44:11 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 17:36:47 +08:00
|
|
|
return this.Success()
|
2020-09-15 14:44:11 +08:00
|
|
|
}
|
2020-09-16 09:09:21 +08:00
|
|
|
|
2021-09-29 19:32:25 +08:00
|
|
|
// UpdateHTTPWebCompression 修改压缩配置
|
|
|
|
|
func (this *HTTPWebService) UpdateHTTPWebCompression(ctx context.Context, req *pb.UpdateHTTPWebCompressionRequest) (*pb.RPCSuccess, error) {
|
2020-09-16 09:09:21 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-16 09:09:21 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebCompression(tx, req.HttpWebId, req.CompressionJSON)
|
2020-09-16 09:09:21 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 17:36:47 +08:00
|
|
|
return this.Success()
|
2020-09-16 09:09:21 +08:00
|
|
|
}
|
2020-09-16 20:29:18 +08:00
|
|
|
|
2021-10-01 16:25:16 +08:00
|
|
|
// UpdateHTTPWebWebP 修改WebP配置
|
|
|
|
|
func (this *HTTPWebService) UpdateHTTPWebWebP(ctx context.Context, req *pb.UpdateHTTPWebWebPRequest) (*pb.RPCSuccess, error) {
|
|
|
|
|
// 校验请求
|
|
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if userId > 0 {
|
|
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-10-01 16:25:16 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebWebP(tx, req.HttpWebId, req.WebpJSON)
|
2021-10-01 16:25:16 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.Success()
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 11:40:29 +08:00
|
|
|
// UpdateHTTPWebRemoteAddr 更改RemoteAddr配置
|
|
|
|
|
func (this *HTTPWebService) UpdateHTTPWebRemoteAddr(ctx context.Context, req *pb.UpdateHTTPWebRemoteAddrRequest) (*pb.RPCSuccess, error) {
|
|
|
|
|
// 校验请求
|
|
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if userId > 0 {
|
|
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-10-06 11:40:29 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tx = this.NullTx()
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebRemoteAddr(tx, req.HttpWebId, req.RemoteAddrJSON)
|
2021-10-06 11:40:29 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.Success()
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebCharset 修改字符集配置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebCharset(ctx context.Context, req *pb.UpdateHTTPWebCharsetRequest) (*pb.RPCSuccess, error) {
|
2020-09-16 20:29:18 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-16 20:29:18 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebCharset(tx, req.HttpWebId, req.CharsetJSON)
|
2020-09-16 20:29:18 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 17:36:47 +08:00
|
|
|
return this.Success()
|
2020-09-16 20:29:18 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebRequestHeader 更改请求Header策略
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebRequestHeader(ctx context.Context, req *pb.UpdateHTTPWebRequestHeaderRequest) (*pb.RPCSuccess, error) {
|
2020-09-16 20:29:18 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-16 20:29:18 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebRequestHeaderPolicy(tx, req.HttpWebId, req.HeaderJSON)
|
2020-09-16 20:29:18 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 17:36:47 +08:00
|
|
|
return this.Success()
|
2020-09-16 20:29:18 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebResponseHeader 更改响应Header策略
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebResponseHeader(ctx context.Context, req *pb.UpdateHTTPWebResponseHeaderRequest) (*pb.RPCSuccess, error) {
|
2020-09-16 20:29:18 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-16 20:29:18 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebResponseHeaderPolicy(tx, req.HttpWebId, req.HeaderJSON)
|
2020-09-16 20:29:18 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 17:36:47 +08:00
|
|
|
return this.Success()
|
2020-09-17 10:16:00 +08:00
|
|
|
}
|
2020-09-16 20:29:18 +08:00
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebShutdown 更改Shutdown
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebShutdown(ctx context.Context, req *pb.UpdateHTTPWebShutdownRequest) (*pb.RPCSuccess, error) {
|
2020-09-17 10:16:00 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-17 10:16:00 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebShutdown(tx, req.HttpWebId, req.ShutdownJSON)
|
2020-09-17 10:16:00 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-17 10:16:00 +08:00
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebPages 更改Pages
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebPages(ctx context.Context, req *pb.UpdateHTTPWebPagesRequest) (*pb.RPCSuccess, error) {
|
2020-09-17 10:16:00 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-17 10:16:00 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebPages(tx, req.HttpWebId, req.PagesJSON)
|
2020-09-17 10:16:00 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-16 20:29:18 +08:00
|
|
|
}
|
2020-09-20 11:56:30 +08:00
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebAccessLog 更改访问日志配置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebAccessLog(ctx context.Context, req *pb.UpdateHTTPWebAccessLogRequest) (*pb.RPCSuccess, error) {
|
2020-09-20 11:56:30 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-20 11:56:30 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebAccessLogConfig(tx, req.HttpWebId, req.AccessLogJSON)
|
2020-09-20 11:56:30 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-20 11:56:30 +08:00
|
|
|
}
|
2020-09-20 14:48:41 +08:00
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebStat 更改统计配置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebStat(ctx context.Context, req *pb.UpdateHTTPWebStatRequest) (*pb.RPCSuccess, error) {
|
2020-09-20 14:48:41 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-20 14:48:41 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebStat(tx, req.HttpWebId, req.StatJSON)
|
2020-09-20 14:48:41 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-20 14:48:41 +08:00
|
|
|
}
|
2020-09-20 16:28:07 +08:00
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebCache 更改缓存配置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebCache(ctx context.Context, req *pb.UpdateHTTPWebCacheRequest) (*pb.RPCSuccess, error) {
|
2020-09-20 16:28:07 +08:00
|
|
|
// 校验请求
|
2020-12-18 21:18:53 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-20 16:28:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 21:18:53 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-18 21:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebCache(tx, req.HttpWebId, req.CacheJSON)
|
2020-09-20 16:28:07 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-20 16:28:07 +08:00
|
|
|
}
|
2020-09-20 20:12:47 +08:00
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebFirewall 更改防火墙设置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebFirewall(ctx context.Context, req *pb.UpdateHTTPWebFirewallRequest) (*pb.RPCSuccess, error) {
|
2020-09-20 20:12:47 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-20 20:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebFirewall(tx, req.HttpWebId, req.FirewallJSON)
|
2020-09-20 20:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-21 19:51:56 +08:00
|
|
|
}
|
|
|
|
|
|
2021-07-13 14:30:30 +08:00
|
|
|
// UpdateHTTPWebLocations 更改路由规则设置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebLocations(ctx context.Context, req *pb.UpdateHTTPWebLocationsRequest) (*pb.RPCSuccess, error) {
|
2020-09-21 19:51:56 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-21 19:51:56 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查用户权限
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebLocations(tx, req.HttpWebId, req.LocationsJSON)
|
2020-09-21 19:51:56 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-21 19:51:56 +08:00
|
|
|
}
|
2020-09-23 18:43:42 +08:00
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebRedirectToHTTPS 更改跳转到HTTPS设置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebRedirectToHTTPS(ctx context.Context, req *pb.UpdateHTTPWebRedirectToHTTPSRequest) (*pb.RPCSuccess, error) {
|
2020-09-23 18:43:42 +08:00
|
|
|
// 校验请求
|
2021-01-10 17:34:35 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-23 18:43:42 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-10 17:34:35 +08:00
|
|
|
// 检查权限
|
|
|
|
|
if userId > 0 {
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebRedirectToHTTPS(tx, req.HttpWebId, req.RedirectToHTTPSJSON)
|
2020-09-23 18:43:42 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-23 18:43:42 +08:00
|
|
|
}
|
2020-09-26 19:54:15 +08:00
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebWebsocket 更改Websocket设置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebWebsocket(ctx context.Context, req *pb.UpdateHTTPWebWebsocketRequest) (*pb.RPCSuccess, error) {
|
2020-09-26 19:54:15 +08:00
|
|
|
// 校验请求
|
2021-01-10 17:34:35 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-26 19:54:15 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-10 17:34:35 +08:00
|
|
|
if userId > 0 {
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebsocket(tx, req.HttpWebId, req.WebsocketJSON)
|
2020-09-26 19:54:15 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-26 19:54:15 +08:00
|
|
|
}
|
2020-09-28 16:25:39 +08:00
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebFastcgi 更改Fastcgi设置
|
|
|
|
|
func (this *HTTPWebService) UpdateHTTPWebFastcgi(ctx context.Context, req *pb.UpdateHTTPWebFastcgiRequest) (*pb.RPCSuccess, error) {
|
|
|
|
|
// 校验请求
|
|
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if userId > 0 {
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-05-10 21:13:32 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebFastcgi(tx, req.HttpWebId, req.FastcgiJSON)
|
2021-05-10 21:13:32 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return this.Success()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateHTTPWebRewriteRules 更改重写规则设置
|
2020-11-13 18:22:22 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebRewriteRules(ctx context.Context, req *pb.UpdateHTTPWebRewriteRulesRequest) (*pb.RPCSuccess, error) {
|
2020-09-28 16:25:39 +08:00
|
|
|
// 校验请求
|
2020-12-23 09:52:50 +08:00
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
2020-09-28 16:25:39 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 09:52:50 +08:00
|
|
|
if userId > 0 {
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-12-23 09:52:50 +08:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 23:31:30 +08:00
|
|
|
tx := this.NullTx()
|
|
|
|
|
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebRewriteRules(tx, req.HttpWebId, req.RewriteRulesJSON)
|
2020-09-28 16:25:39 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2020-11-24 15:02:44 +08:00
|
|
|
return this.Success()
|
2020-09-28 16:25:39 +08:00
|
|
|
}
|
2021-01-10 17:34:35 +08:00
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// UpdateHTTPWebHostRedirects 更改主机跳转设置
|
2021-01-10 17:34:35 +08:00
|
|
|
func (this *HTTPWebService) UpdateHTTPWebHostRedirects(ctx context.Context, req *pb.UpdateHTTPWebHostRedirectsRequest) (*pb.RPCSuccess, error) {
|
|
|
|
|
// 校验请求
|
|
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if userId > 0 {
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hostRedirects := []*serverconfigs.HTTPHostRedirectConfig{}
|
|
|
|
|
if len(req.HostRedirectsJSON) == 0 {
|
|
|
|
|
return nil, errors.New("'hostRedirectsJSON' should not be empty")
|
|
|
|
|
}
|
|
|
|
|
err = json.Unmarshal(req.HostRedirectsJSON, &hostRedirects)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 校验
|
|
|
|
|
for _, redirect := range hostRedirects {
|
|
|
|
|
err := redirect.Init()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tx *dbs.Tx
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebHostRedirects(tx, req.HttpWebId, hostRedirects)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return this.Success()
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-10 21:13:32 +08:00
|
|
|
// FindHTTPWebHostRedirects 查找主机跳转设置
|
2021-01-10 17:34:35 +08:00
|
|
|
func (this *HTTPWebService) FindHTTPWebHostRedirects(ctx context.Context, req *pb.FindHTTPWebHostRedirectsRequest) (*pb.FindHTTPWebHostRedirectsResponse, error) {
|
|
|
|
|
// 校验请求
|
|
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if userId > 0 {
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tx *dbs.Tx
|
2021-11-24 12:00:38 +08:00
|
|
|
redirectsJSON, err := models.SharedHTTPWebDAO.FindWebHostRedirects(tx, req.HttpWebId)
|
2021-01-10 17:34:35 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return &pb.FindHTTPWebHostRedirectsResponse{HostRedirectsJSON: redirectsJSON}, nil
|
|
|
|
|
}
|
2021-06-17 21:17:53 +08:00
|
|
|
|
|
|
|
|
// UpdateHTTPWebAuth 更改认证设置
|
|
|
|
|
func (this *HTTPWebService) UpdateHTTPWebAuth(ctx context.Context, req *pb.UpdateHTTPWebAuthRequest) (*pb.RPCSuccess, error) {
|
|
|
|
|
// 校验请求
|
|
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if userId > 0 {
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(nil, userId, req.HttpWebId)
|
2021-06-17 21:17:53 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tx *dbs.Tx
|
2021-11-24 12:00:38 +08:00
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebAuth(tx, req.HttpWebId, req.AuthJSON)
|
2021-06-17 21:17:53 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return this.Success()
|
|
|
|
|
}
|
2021-11-24 12:00:38 +08:00
|
|
|
|
|
|
|
|
// UpdateHTTPWebCommon 更改通用设置
|
|
|
|
|
func (this *HTTPWebService) UpdateHTTPWebCommon(ctx context.Context, req *pb.UpdateHTTPWebCommonRequest) (*pb.RPCSuccess, error) {
|
|
|
|
|
// 校验请求
|
|
|
|
|
_, userId, err := this.ValidateAdminAndUser(ctx, 0, 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tx = this.NullTx()
|
|
|
|
|
|
|
|
|
|
if userId > 0 {
|
|
|
|
|
err = models.SharedHTTPWebDAO.CheckUserWeb(tx, userId, req.HttpWebId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = models.SharedHTTPWebDAO.UpdateWebCommon(tx, req.HttpWebId, req.MergeSlashes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this.Success()
|
|
|
|
|
}
|