fetcher.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package cron
  2. import (
  3. "log"
  4. "time"
  5. "github.com/710leo/urlooker/dataobj"
  6. "github.com/710leo/urlooker/modules/web/g"
  7. "github.com/710leo/urlooker/modules/web/model"
  8. "github.com/710leo/urlooker/modules/web/utils"
  9. )
  10. func GetDetectedItem() {
  11. t1 := time.NewTicker(time.Duration(60) * time.Second)
  12. for {
  13. err := getDetectedItem()
  14. if err != nil {
  15. time.Sleep(time.Second * 1)
  16. continue
  17. }
  18. <-t1.C
  19. }
  20. }
  21. func getDetectedItem() error {
  22. detectedItemMap := make(map[string][]*dataobj.DetectedItem)
  23. stras, err := model.GetAllStrategyByCron()
  24. if err != nil {
  25. log.Println("get strategies error:", err)
  26. return err
  27. }
  28. for _, s := range stras {
  29. detectedItem := newDetectedItem(s)
  30. idc := detectedItem.Idc
  31. if _, exists := detectedItemMap[idc]; exists {
  32. detectedItemMap[idc] = append(detectedItemMap[idc], &detectedItem)
  33. } else {
  34. detectedItemMap[idc] = []*dataobj.DetectedItem{&detectedItem}
  35. }
  36. }
  37. g.DetectedItemMap.Set(detectedItemMap)
  38. return nil
  39. }
  40. func newDetectedItem(s *model.Strategy) dataobj.DetectedItem {
  41. _, domain, _, _ := utils.ParseUrl(s.Url)
  42. idc := s.Idc
  43. if idc == "" {
  44. idc = g.Config.IDC[0]
  45. }
  46. detectedItem := dataobj.DetectedItem{
  47. Idc: idc,
  48. Target: s.Url,
  49. Creator: s.Creator,
  50. Sid: s.Id,
  51. Keywords: s.Keywords,
  52. Data: s.Data,
  53. Tag: s.Tag,
  54. Endpoint: s.Endpoint,
  55. ExpectCode: s.ExpectCode,
  56. Timeout: s.Timeout,
  57. Header: s.Header,
  58. PostData: s.PostData,
  59. Method: s.Method,
  60. Domain: domain,
  61. }
  62. return detectedItem
  63. }