diff --git a/engine/engine.go b/engine/engine.go index e72b2e610..0db17266d 100644 --- a/engine/engine.go +++ b/engine/engine.go @@ -80,16 +80,17 @@ func (e *Engine) DoEvent(ed *EventData) { fnCall := e.DoCase(ed) if nil != fnCall { in := inject.New() - a := ed.EventData - for _, i := range a { + for _, i := range ed.EventData { in.Map(i) } v, err := in.Invoke(fnCall) if nil != err { - log.Printf("DoEvent is error: %v %+v \n", err, ed.EventData) + log.Printf("DoEvent %s is error: %v %+v \n", ed.EventType, err, ed.EventData) } else if nil != v { - log.Printf("DoEvent result %v\n", v) + log.Printf("DoEvent result %s %v\n", ed.EventType, v) } + } else { + log.Printf("can case func %v\n", ed) } } } diff --git a/lib/util/db.go b/lib/util/db.go index 8dcd1da7d..0bf87a5da 100644 --- a/lib/util/db.go +++ b/lib/util/db.go @@ -3,6 +3,7 @@ package util import ( "gorm.io/driver/sqlite" "gorm.io/gorm" + "gorm.io/gorm/clause" "gorm.io/gorm/logger" "log" "os" @@ -52,7 +53,11 @@ func InitDb(dst ...interface{}) *gorm.DB { } log.Println("DbName ", szDf) xx01 := sqlite.Open("file:" + szDf + ".db?cache=shared&mode=rwc&_journal_mode=WAL&Synchronous=Off&temp_store=memory&mmap_size=30000000000") - db, err := gorm.Open(xx01, &gorm.Config{PrepareStmt: true, Logger: logger.Default.LogMode(logger.Silent)}) + db, err := gorm.Open(xx01, &gorm.Config{ + PrepareStmt: true, + //Logger: logger.Default.LogMode(logger.Silent), + Logger: logger.Default.LogMode(logger.Info), + }) if err == nil { // no error db1, _ := db.DB() if err := db1.Ping(); nil == err { @@ -84,7 +89,7 @@ func GetTableName[T any](mod T) string { // 指定id更新T类型mod数据 func Update[T any](mod *T, query string, args ...interface{}) int64 { var t1 *T = mod - xxxD := dbCC.Table(GetTableName(mod)).Model(t1) + xxxD := dbCC.Table(GetTableName(mod)).Model(&t1) xxxD.AutoMigrate(t1) rst := xxxD.Where(query, args...).Updates(mod) xxxD.Commit() @@ -96,10 +101,18 @@ func Update[T any](mod *T, query string, args ...interface{}) int64 { // 更新失败再插入新数据,确保只有一条数据 func UpInsert[T any](mod *T, query string, args ...interface{}) int64 { - if 1 >= Update[T](mod, query, args...) { - return Create[T](mod) + // 在冲突时,更新除主键以外的所有列 + if 1 > Update[T](mod, query, args...) { // && + if 1 > Create[T](mod) { + xx1 := dbCC.Clauses(clause.OnConflict(clause.OnConflict{ + Columns: []clause.Column{{Name: "addr"}}, // key colume + UpdateAll: true})).Create(mod) + return xx1.RowsAffected + } else { + return 1 + } } - return 0 + return 1 } // 通用,insert diff --git a/projectdiscovery/nuclei_Yaml/masscan/masscan.go b/projectdiscovery/nuclei_Yaml/masscan/masscan.go index 62aac5b2b..641216cd1 100644 --- a/projectdiscovery/nuclei_Yaml/masscan/masscan.go +++ b/projectdiscovery/nuclei_Yaml/masscan/masscan.go @@ -5,7 +5,6 @@ import ( "encoding/xml" _ "github.com/codegangsta/inject" "github.com/hktalent/scan4all/lib/util" - "gorm.io/gorm" "io" "log" "regexp" @@ -13,15 +12,15 @@ import ( // 地址 type Address struct { - Addr string `xml:"addr,attr" json:"addr"` - AddrType string `xml:"addrtype,attr" json:"addrType"` + Addr string `xml:"addr,attr" json:"addr" gorm:"primaryKey;"` + AddrType string `xml:"addrtype,attr" json:"addr_type"` } // 状态 type State struct { State string `xml:"state,attr" json:"state"` Reason string `xml:"reason,attr" json:"reason"` - ReasonTTL string `xml:"reason_ttl,attr" json:"reasonTTL"` + ReasonTTL string `xml:"reason_ttl,attr" json:"reason_ttl"` } // nmap 模式 @@ -37,25 +36,29 @@ type Nmaprun struct { // foreignKey should name the model-local key field that joins to the foreign entity. // references should name the foreign entity's primary or unique key. type Host struct { - gorm.Model - //StartTime string - //Endtime string `xml:"endtime,attr"` - Ip string `json:"-"` - Port string `json:"-"` - Address Address `json:",inline" xml:"address" gorm:"foreignKey:Ip;references:Addr"` - Ports Ports `json:"ports" xml:"ports>port" gorm:"foreignKey:Port;references:Portid"` - //LastScanTime int `json:"lastScanTime"` - //LastScanEndTime int `json:"lastScanEndTime"` + Address Address `json:"address" xml:"address" gorm:"embedded;"` + // association_autoupdate:true;association_autocreate:true;constraint:OnUpdate:CASCADE,OnDelete:SET NULL; + Ports []Ports `json:"ports" xml:"ports>port" gorm:"foreignKey:addr;References:addr;"` // association_autocreate:true; // many2many:Host_Ports;foreignKey:ID;References:ID; } +// `xml:",innerxml"` +//func (cm Host) MarshalXML(e *xml.Encoder, start xml.StartElement) error { +// if cm.Address != nil { +// err := e.EncodeToken(cm.comment) +// if err != nil { +// return err +// } +// } +// return e.Encode(cm.Member) +//} + // 端口信息 -type Ports []struct { - Protocol string `xml:"protocol,attr" json:"protocol"` - Portid string `xml:"portid,attr" json:"portid"` - State1 string `json:"-"` - State State `json:",inline" xml:"state" gorm:"foreignKey:State1;references:State"` - Name string `json:"-"` - Service Service `json:",inline" xml:"service" gorm:"foreignKey:Name;references:Name"` +type Ports struct { + Addr string `json:"addr" gorm:"unique_index:S_R"` + Protocol string `xml:"protocol,attr" json:"protocol" gorm:"unique_index:S_R"` + PortId string `xml:"portid,attr" json:"port_id" gorm:"unique_index:S_R"` + State State `json:"state" xml:"state" gorm:"embedded;"` + Service Service `json:"service" xml:"service" gorm:"embedded;"` } // 服务信息 @@ -124,9 +127,15 @@ func (m *Masscan) Run() error { return } for _, i := range x1 { - nR := util.UpInsert[Host](&i, "address=? and ports=?", i.Address, i.Ports) - if 1 >= nR { - log.Println("util.UpInsert fail") + //log.Printf("%+v\n", i) + if 0 < len(i.Ports) { + for _, x9 := range i.Ports { + x9.Addr = i.Address.Addr + } + nR := util.UpInsert[Host](&i, "addr=?", i.Address.Addr) + if 1 > nR { + log.Println("util.UpInsert fail \n", line) + } } } }, m.SystemPath, m.Args...) @@ -153,6 +162,7 @@ func (m *Masscan) ParseLine(s string) ([]Host, error) { if err == io.EOF || err != nil { break } + //host.Ip = host.Address.Addr hosts = append(hosts, host) } default: diff --git a/projectdiscovery/nuclei_Yaml/masscan/test/test.go b/projectdiscovery/nuclei_Yaml/masscan/test/test.go index f33ac1db0..ca86e7d93 100644 --- a/projectdiscovery/nuclei_Yaml/masscan/test/test.go +++ b/projectdiscovery/nuclei_Yaml/masscan/test/test.go @@ -6,7 +6,7 @@ import ( ) func main() { - m := &masscan.Host{} - util.InitModle(m) + //util.InitModle(masscan.Ports{}, masscan.Address{}, masscan.Service{}, masscan.State{}, masscan.Host{}) + util.InitModle(&masscan.Ports{}, &masscan.Host{}) masscan.ScanTarget("192.168.0.111") }