上传API接口校验扫描

This commit is contained in:
liwei1dao 2022-07-13 16:50:35 +08:00
parent 1a96cde1c8
commit 5036f6b2b0
10 changed files with 42 additions and 22 deletions

View File

@ -67,26 +67,37 @@ func (this *MCompGate) suitableMethods() {
typ := reflect.TypeOf(this.comp)
for m := 0; m < typ.NumMethod(); m++ {
method := typ.Method(m)
mname := method.Name
if mname == "Start" ||
mname == "Init" ||
mname == "Destroy" ||
strings.HasSuffix(mname, "Check") {
continue
}
this.reflectionRouteHandle(typ, method)
}
}
//反射注册路由处理函数
func (this *MCompGate) reflectionRouteHandle(typ reflect.Type, method reflect.Method) {
func (this *MCompGate) reflectionRouteHandle(typ reflect.Type, method reflect.Method) (ret bool) {
mtype := method.Type
mname := method.Name
if method.PkgPath != "" {
log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname)
return
}
if mtype.NumIn() != 3 {
log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname)
return
}
sessionType := mtype.In(1)
if !sessionType.Implements(typeOfSession) {
log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname)
return
}
agrType := mtype.In(2)
if !agrType.Implements(typeOfMessage) {
log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname)
return
}
// if agrType.Kind() != reflect.Ptr {
@ -97,14 +108,17 @@ func (this *MCompGate) reflectionRouteHandle(typ reflect.Type, method reflect.Me
// }
if mtype.NumOut() != 2 {
log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname)
return
}
returnCodeType := mtype.Out(0)
if returnCodeType != typeOfErrorCode {
log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname)
return
}
returnDataType := mtype.Out(1)
if !returnDataType.Implements(typeOfMessage) {
log.Panicf("反射注册用户处理函数错误 [%s-%s] Api接口格式错误", this.module.GetType(), mname)
return
}
//寻找校验函数
@ -119,6 +133,7 @@ func (this *MCompGate) reflectionRouteHandle(typ reflect.Type, method reflect.Me
log.Panicf("反射注册用户处理函数错误 [%s-%s]校验函数格式异常:%v", this.module.GetType(), mname, err)
return
}
return true
}
//反射注册路由校验函数

View File

@ -15,7 +15,7 @@ func (this *apiComp) DelCheck(session comm.IUserSession, req *pb.FriendDelReq) (
}
//删除好友
func (this *apiComp) Del(session comm.IUserSession, chk map[string]interface{}, req *pb.FriendDelReq) (code pb.ErrorCode, data proto.Message) {
func (this *apiComp) Del(session comm.IUserSession, req *pb.FriendDelReq) (code pb.ErrorCode, data proto.Message) {
if code = this.DelCheck(session, req); code != pb.ErrorCode_Success {
return
}

View File

@ -147,7 +147,7 @@ func (this *Agent) decodeUserData(msg *pb.UserMessage) error {
}
//只有login的时候才需要设置Data
if msg.MainType == "user" && msg.SubType == "login" {
if msg.MainType == string(comm.ModuleUser) && msg.SubType == "login" {
serverId := jsonRet.Get("serverId").Int()
account := jsonRet.Get("account").String()
req := &pb.UserLoginReq{
@ -160,7 +160,7 @@ func (this *Agent) decodeUserData(msg *pb.UserMessage) error {
}
msg.Data = ad
} else {
if this.UserId() == "" {
if msg.MainType != string(comm.ModuleNotify) && this.UserId() == "" {
return fmt.Errorf("no login")
}
}

View File

@ -12,13 +12,13 @@ API
type apiComp struct {
modules.MCompGate
service core.IService
module *Notification
module *Notify
}
//组件初始化接口
func (this *apiComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, options core.IModuleOptions) (err error) {
this.MCompGate.Init(service, module, comp, options)
this.module = module.(*Notification)
this.module = module.(*Notify)
this.service = service
return
}

View File

@ -4,17 +4,17 @@ import (
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/proto"
)
//参数校验
func (this *apiComp) GetlistCheck(session comm.IUserSession, req *pb.NotifyGetListReq) (code pb.ErrorCode) {
func (this *apiComp) GetListCheck(session comm.IUserSession, req *pb.NotifyGetListReq) (code pb.ErrorCode) {
return
}
///获取系统公告
func (this *apiComp) Getlist(session comm.IUserSession, req *pb.NotifyGetListReq) (code pb.ErrorCode, data proto.Message) {
func (this *apiComp) GetList(session comm.IUserSession, req *pb.NotifyGetListReq) (code pb.ErrorCode, data proto.Message) {
var (
err error
userexpand *pb.DBUserExpand

View File

@ -16,13 +16,13 @@ import (
///论坛 数据组件
type modelNotifyComp struct {
modules.MCompModel
module *Notification
module *Notify
}
//组件初始化接口
func (this *modelNotifyComp) Init(service core.IService, module core.IModule, comp core.IModuleComp, opt core.IModuleOptions) (err error) {
this.MCompModel.Init(service, module, comp, opt)
this.module = module.(*Notification)
this.module = module.(*Notify)
this.TableName = "notify"
//创建uid索引
this.DB.CreateIndex(core.SqlTable(this.TableName), mongo.IndexModel{

View File

@ -13,29 +13,29 @@ import (
开发:李伟
*/
func NewModule() core.IModule {
m := new(Notification)
m := new(Notify)
return m
}
type Notification struct {
type Notify struct {
modules.ModuleBase
api_comp *apiComp
modelNotify *modelNotifyComp
}
//模块名
func (this *Notification) GetType() core.M_Modules {
return comm.ModuleEquipment
func (this *Notify) GetType() core.M_Modules {
return comm.ModuleNotify
}
//模块初始化接口 注册用户创建角色事件
func (this *Notification) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
func (this *Notify) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
err = this.ModuleBase.Init(service, module, options)
return
}
//装备组件
func (this *Notification) OnInstallComp() {
func (this *Notify) OnInstallComp() {
this.ModuleBase.OnInstallComp()
this.api_comp = this.RegisterComp(new(apiComp)).(*apiComp)
this.modelNotify = this.RegisterComp(new(modelNotifyComp)).(*modelNotifyComp)

View File

@ -1,4 +1,4 @@
package notify
package notify_test
import (
"fmt"
@ -7,6 +7,7 @@ import (
"go_dreamfactory/lego/base/rpcx"
"go_dreamfactory/lego/core"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/modules/notify"
"go_dreamfactory/services"
"go_dreamfactory/sys/cache"
"go_dreamfactory/sys/configure"
@ -49,7 +50,7 @@ func (this *TestService) InitSys() {
var service core.IService
var s_gateComp comm.ISC_GateRouteComp = services.NewGateRouteComp()
var module = new(Notification)
var module = new(notify.Notify)
//测试环境下初始化db和cache 系统
func TestMain(m *testing.M) {

View File

@ -4,14 +4,16 @@ import (
"go_dreamfactory/comm"
"go_dreamfactory/lego/sys/log"
"go_dreamfactory/pb"
"google.golang.org/protobuf/proto"
)
func (this *apiComp) LogoutCheck(session comm.IUserSession, req *pb.UserLoginReq) (result map[string]interface{}, code comm.ErrorCode) {
func (this *apiComp) LogoutCheck(session comm.IUserSession, req *pb.UserLoginReq) (code pb.ErrorCode) {
return
}
//注销
func (this *apiComp) Logout(session comm.IUserSession, result map[string]interface{}, rsp *pb.UserLoginReq) (code pb.ErrorCode) {
func (this *apiComp) Logout(session comm.IUserSession, rsp *pb.UserLoginReq) (code pb.ErrorCode, data proto.Message) {
log.Debugf("User - Logout: session:%v rsp:%v", session.ToString(), rsp)
return

View File

@ -8,6 +8,7 @@ import (
"go_dreamfactory/modules/hero"
"go_dreamfactory/modules/items"
"go_dreamfactory/modules/mail"
"go_dreamfactory/modules/notify"
"go_dreamfactory/modules/shop"
"go_dreamfactory/modules/story"
"go_dreamfactory/modules/task"
@ -50,6 +51,7 @@ func main() {
task.NewModule(),
story.NewModule(),
shop.NewModule(),
notify.NewModule(),
)
}