mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 07:50:25 +08:00 
			
		
		
		
	增加请求脚本审核机制
This commit is contained in:
		@@ -1299,6 +1299,61 @@ func (this *HTTPWebDAO) UpdateWebRequestScripts(tx *dbs.Tx, webId int64, config
 | 
				
			|||||||
	return this.NotifyUpdate(tx, webId)
 | 
						return this.NotifyUpdate(tx, webId)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UpdateWebRequestScriptsAsPassed 设置请求脚本为审核通过
 | 
				
			||||||
 | 
					func (this *HTTPWebDAO) UpdateWebRequestScriptsAsPassed(tx *dbs.Tx, webId int64, codeMD5 string) error {
 | 
				
			||||||
 | 
						if webId <= 0 || len(codeMD5) == 0 {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						configString, err := this.Query(tx).
 | 
				
			||||||
 | 
							Pk(webId).
 | 
				
			||||||
 | 
							Result("requestScripts").
 | 
				
			||||||
 | 
							FindStringCol("")
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var config = &serverconfigs.HTTPRequestScriptsConfig{}
 | 
				
			||||||
 | 
						if len(configString) == 0 {
 | 
				
			||||||
 | 
							return nil
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						err = json.Unmarshal([]byte(configString), config)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						var found bool
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, group := range config.AllGroups() {
 | 
				
			||||||
 | 
							for _, script := range group.Scripts {
 | 
				
			||||||
 | 
								if script.AuditingCodeMD5 == codeMD5 {
 | 
				
			||||||
 | 
									script.Code = script.AuditingCode
 | 
				
			||||||
 | 
									script.AuditingCode = ""
 | 
				
			||||||
 | 
									script.AuditingCodeMD5 = ""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
									found = true
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if found {
 | 
				
			||||||
 | 
							configJSON, err := json.Marshal(config)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							err = this.Query(tx).
 | 
				
			||||||
 | 
								Pk(webId).
 | 
				
			||||||
 | 
								Set("requestScripts", configJSON).
 | 
				
			||||||
 | 
								UpdateQuickly()
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return this.NotifyUpdate(tx, webId)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// FindWebRequestScripts 查找服务的脚本设置
 | 
					// FindWebRequestScripts 查找服务的脚本设置
 | 
				
			||||||
func (this *HTTPWebDAO) FindWebRequestScripts(tx *dbs.Tx, webId int64) (*serverconfigs.HTTPRequestScriptsConfig, error) {
 | 
					func (this *HTTPWebDAO) FindWebRequestScripts(tx *dbs.Tx, webId int64) (*serverconfigs.HTTPRequestScriptsConfig, error) {
 | 
				
			||||||
	configString, err := this.Query(tx).
 | 
						configString, err := this.Query(tx).
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										33
									
								
								internal/db/models/user_script_dao.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								internal/db/models/user_script_dao.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						_ "github.com/go-sql-driver/mysql"
 | 
				
			||||||
 | 
						"github.com/iwind/TeaGo/Tea"
 | 
				
			||||||
 | 
						"github.com/iwind/TeaGo/dbs"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						UserScriptStateEnabled  = 1 // 已启用
 | 
				
			||||||
 | 
						UserScriptStateDisabled = 0 // 已禁用
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type UserScriptDAO dbs.DAO
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func NewUserScriptDAO() *UserScriptDAO {
 | 
				
			||||||
 | 
						return dbs.NewDAO(&UserScriptDAO{
 | 
				
			||||||
 | 
							DAOObject: dbs.DAOObject{
 | 
				
			||||||
 | 
								DB:     Tea.Env,
 | 
				
			||||||
 | 
								Table:  "edgeUserScripts",
 | 
				
			||||||
 | 
								Model:  new(UserScript),
 | 
				
			||||||
 | 
								PkName: "id",
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}).(*UserScriptDAO)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var SharedUserScriptDAO *UserScriptDAO
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						dbs.OnReady(func() {
 | 
				
			||||||
 | 
							SharedUserScriptDAO = NewUserScriptDAO()
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										6
									
								
								internal/db/models/user_script_dao_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								internal/db/models/user_script_dao_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,6 @@
 | 
				
			|||||||
 | 
					package models_test
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						_ "github.com/go-sql-driver/mysql"
 | 
				
			||||||
 | 
						_ "github.com/iwind/TeaGo/bootstrap"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
							
								
								
									
										56
									
								
								internal/db/models/user_script_model.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										56
									
								
								internal/db/models/user_script_model.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,56 @@
 | 
				
			|||||||
 | 
					package models
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "github.com/iwind/TeaGo/dbs"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const (
 | 
				
			||||||
 | 
						UserScriptField_Id             dbs.FieldName = "id"             // ID
 | 
				
			||||||
 | 
						UserScriptField_UserId         dbs.FieldName = "userId"         // 用户ID
 | 
				
			||||||
 | 
						UserScriptField_AdminId        dbs.FieldName = "adminId"        // 操作管理员
 | 
				
			||||||
 | 
						UserScriptField_Code           dbs.FieldName = "code"           // 代码
 | 
				
			||||||
 | 
						UserScriptField_CodeMD5        dbs.FieldName = "codeMD5"        // 代码MD5
 | 
				
			||||||
 | 
						UserScriptField_CreatedAt      dbs.FieldName = "createdAt"      // 创建时间
 | 
				
			||||||
 | 
						UserScriptField_IsRejected     dbs.FieldName = "isRejected"     // 是否已驳回
 | 
				
			||||||
 | 
						UserScriptField_RejectedAt     dbs.FieldName = "rejectedAt"     // 驳回时间
 | 
				
			||||||
 | 
						UserScriptField_RejectedReason dbs.FieldName = "rejectedReason" // 驳回原因
 | 
				
			||||||
 | 
						UserScriptField_IsPassed       dbs.FieldName = "isPassed"       // 是否通过审核
 | 
				
			||||||
 | 
						UserScriptField_PassedAt       dbs.FieldName = "passedAt"       // 通过时间
 | 
				
			||||||
 | 
						UserScriptField_State          dbs.FieldName = "state"          // 状态
 | 
				
			||||||
 | 
						UserScriptField_WebIds         dbs.FieldName = "webIds"         // WebId列表
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UserScript 用户脚本审核
 | 
				
			||||||
 | 
					type UserScript struct {
 | 
				
			||||||
 | 
						Id             uint64   `field:"id"`             // ID
 | 
				
			||||||
 | 
						UserId         uint64   `field:"userId"`         // 用户ID
 | 
				
			||||||
 | 
						AdminId        uint64   `field:"adminId"`        // 操作管理员
 | 
				
			||||||
 | 
						Code           string   `field:"code"`           // 代码
 | 
				
			||||||
 | 
						CodeMD5        string   `field:"codeMD5"`        // 代码MD5
 | 
				
			||||||
 | 
						CreatedAt      uint64   `field:"createdAt"`      // 创建时间
 | 
				
			||||||
 | 
						IsRejected     bool     `field:"isRejected"`     // 是否已驳回
 | 
				
			||||||
 | 
						RejectedAt     uint64   `field:"rejectedAt"`     // 驳回时间
 | 
				
			||||||
 | 
						RejectedReason string   `field:"rejectedReason"` // 驳回原因
 | 
				
			||||||
 | 
						IsPassed       bool     `field:"isPassed"`       // 是否通过审核
 | 
				
			||||||
 | 
						PassedAt       uint64   `field:"passedAt"`       // 通过时间
 | 
				
			||||||
 | 
						State          uint8    `field:"state"`          // 状态
 | 
				
			||||||
 | 
						WebIds         dbs.JSON `field:"webIds"`         // WebId列表
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type UserScriptOperator struct {
 | 
				
			||||||
 | 
						Id             any // ID
 | 
				
			||||||
 | 
						UserId         any // 用户ID
 | 
				
			||||||
 | 
						AdminId        any // 操作管理员
 | 
				
			||||||
 | 
						Code           any // 代码
 | 
				
			||||||
 | 
						CodeMD5        any // 代码MD5
 | 
				
			||||||
 | 
						CreatedAt      any // 创建时间
 | 
				
			||||||
 | 
						IsRejected     any // 是否已驳回
 | 
				
			||||||
 | 
						RejectedAt     any // 驳回时间
 | 
				
			||||||
 | 
						RejectedReason any // 驳回原因
 | 
				
			||||||
 | 
						IsPassed       any // 是否通过审核
 | 
				
			||||||
 | 
						PassedAt       any // 通过时间
 | 
				
			||||||
 | 
						State          any // 状态
 | 
				
			||||||
 | 
						WebIds         any // WebId列表
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func NewUserScriptOperator() *UserScriptOperator {
 | 
				
			||||||
 | 
						return &UserScriptOperator{}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										1
									
								
								internal/db/models/user_script_model_ext.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								internal/db/models/user_script_model_ext.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					package models
 | 
				
			||||||
@@ -898,28 +898,6 @@ func (this *HTTPWebService) FindHTTPWebRequestLimit(ctx context.Context, req *pb
 | 
				
			|||||||
	return &pb.FindHTTPWebRequestLimitResponse{RequestLimitJSON: configJSON}, nil
 | 
						return &pb.FindHTTPWebRequestLimitResponse{RequestLimitJSON: configJSON}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// UpdateHTTPWebRequestScripts 修改请求脚本
 | 
					 | 
				
			||||||
func (this *HTTPWebService) UpdateHTTPWebRequestScripts(ctx context.Context, req *pb.UpdateHTTPWebRequestScriptsRequest) (*pb.RPCSuccess, error) {
 | 
					 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	var tx = this.NullTx()
 | 
					 | 
				
			||||||
	var config = &serverconfigs.HTTPRequestScriptsConfig{}
 | 
					 | 
				
			||||||
	err = json.Unmarshal(req.RequestScriptsJSON, config)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	err = models.SharedHTTPWebDAO.UpdateWebRequestScripts(tx, req.HttpWebId, config)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		return nil, err
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return this.Success()
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// FindHTTPWebRequestScripts 查找请求脚本
 | 
					// FindHTTPWebRequestScripts 查找请求脚本
 | 
				
			||||||
func (this *HTTPWebService) FindHTTPWebRequestScripts(ctx context.Context, req *pb.FindHTTPWebRequestScriptsRequest) (*pb.FindHTTPWebRequestScriptsResponse, error) {
 | 
					func (this *HTTPWebService) FindHTTPWebRequestScripts(ctx context.Context, req *pb.FindHTTPWebRequestScriptsRequest) (*pb.FindHTTPWebRequestScriptsResponse, error) {
 | 
				
			||||||
	_, err := this.ValidateAdmin(ctx)
 | 
						_, err := this.ValidateAdmin(ctx)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -27,3 +27,8 @@ func (this *HTTPWebService) UpdateHTTPWebCC(ctx context.Context, req *pb.UpdateH
 | 
				
			|||||||
func (this *HTTPWebService) FindHTTPWebCC(ctx context.Context, req *pb.FindHTTPWebCCRequest) (*pb.FindHTTPWebCCResponse, error) {
 | 
					func (this *HTTPWebService) FindHTTPWebCC(ctx context.Context, req *pb.FindHTTPWebCCRequest) (*pb.FindHTTPWebCCResponse, error) {
 | 
				
			||||||
	return nil, this.NotImplementedYet()
 | 
						return nil, this.NotImplementedYet()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// UpdateHTTPWebRequestScripts 修改请求脚本
 | 
				
			||||||
 | 
					func (this *HTTPWebService) UpdateHTTPWebRequestScripts(ctx context.Context, req *pb.UpdateHTTPWebRequestScriptsRequest) (*pb.RPCSuccess, error) {
 | 
				
			||||||
 | 
						return nil, this.NotImplementedYet()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -245810,6 +245810,81 @@
 | 
				
			|||||||
      ],
 | 
					      ],
 | 
				
			||||||
      "records": []
 | 
					      "records": []
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					      "name": "edgeUserScripts",
 | 
				
			||||||
 | 
					      "engine": "InnoDB",
 | 
				
			||||||
 | 
					      "charset": "utf8mb4_general_ci",
 | 
				
			||||||
 | 
					      "definition": "CREATE TABLE `edgeUserScripts` (\n  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',\n  `userId` bigint(11) unsigned DEFAULT '0' COMMENT '用户ID',\n  `adminId` bigint(11) unsigned DEFAULT '0' COMMENT '操作管理员',\n  `code` text COMMENT '代码',\n  `codeMD5` varchar(32) DEFAULT NULL COMMENT '代码MD5',\n  `createdAt` bigint(11) unsigned DEFAULT '0' COMMENT '创建时间',\n  `isRejected` tinyint(1) unsigned DEFAULT '0' COMMENT '是否已驳回',\n  `rejectedAt` bigint(11) unsigned DEFAULT '0' COMMENT '驳回时间',\n  `rejectedReason` varchar(255) DEFAULT NULL COMMENT '驳回原因',\n  `isPassed` tinyint(1) unsigned DEFAULT '0' COMMENT '是否通过审核',\n  `passedAt` bigint(11) unsigned DEFAULT '0' COMMENT '通过时间',\n  `state` tinyint(1) unsigned DEFAULT '1' COMMENT '状态',\n  `webIds` json DEFAULT NULL COMMENT 'WebId列表',\n  PRIMARY KEY (`id`),\n  KEY `userId` (`userId`),\n  KEY `codeMD5` (`codeMD5`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户脚本审核'",
 | 
				
			||||||
 | 
					      "fields": [
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "id",
 | 
				
			||||||
 | 
					          "definition": "bigint(20) unsigned auto_increment COMMENT 'ID'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "userId",
 | 
				
			||||||
 | 
					          "definition": "bigint(11) unsigned DEFAULT '0' COMMENT '用户ID'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "adminId",
 | 
				
			||||||
 | 
					          "definition": "bigint(11) unsigned DEFAULT '0' COMMENT '操作管理员'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "code",
 | 
				
			||||||
 | 
					          "definition": "text COMMENT '代码'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "codeMD5",
 | 
				
			||||||
 | 
					          "definition": "varchar(32) COMMENT '代码MD5'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "createdAt",
 | 
				
			||||||
 | 
					          "definition": "bigint(11) unsigned DEFAULT '0' COMMENT '创建时间'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "isRejected",
 | 
				
			||||||
 | 
					          "definition": "tinyint(1) unsigned DEFAULT '0' COMMENT '是否已驳回'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "rejectedAt",
 | 
				
			||||||
 | 
					          "definition": "bigint(11) unsigned DEFAULT '0' COMMENT '驳回时间'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "rejectedReason",
 | 
				
			||||||
 | 
					          "definition": "varchar(255) COMMENT '驳回原因'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "isPassed",
 | 
				
			||||||
 | 
					          "definition": "tinyint(1) unsigned DEFAULT '0' COMMENT '是否通过审核'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "passedAt",
 | 
				
			||||||
 | 
					          "definition": "bigint(11) unsigned DEFAULT '0' COMMENT '通过时间'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "state",
 | 
				
			||||||
 | 
					          "definition": "tinyint(1) unsigned DEFAULT '1' COMMENT '状态'"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "webIds",
 | 
				
			||||||
 | 
					          "definition": "json COMMENT 'WebId列表'"
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      ],
 | 
				
			||||||
 | 
					      "indexes": [
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "PRIMARY",
 | 
				
			||||||
 | 
					          "definition": "UNIQUE KEY `PRIMARY` (`id`) USING BTREE"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "userId",
 | 
				
			||||||
 | 
					          "definition": "KEY `userId` (`userId`) USING BTREE"
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          "name": "codeMD5",
 | 
				
			||||||
 | 
					          "definition": "KEY `codeMD5` (`codeMD5`) USING BTREE"
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      ],
 | 
				
			||||||
 | 
					      "records": []
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
      "name": "edgeUserTicketCategories",
 | 
					      "name": "edgeUserTicketCategories",
 | 
				
			||||||
      "engine": "InnoDB",
 | 
					      "engine": "InnoDB",
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user