search/service/probe-service.go

65 lines
1.3 KiB
Go
Raw Normal View History

2022-10-17 05:59:10 +00:00
package service
import (
2022-10-17 07:06:30 +00:00
"fmt"
"math/rand"
2022-10-17 05:59:10 +00:00
"time"
"gorm.io/gorm"
"myschools.me/suguo/search/exceptionless"
"myschools.me/suguo/search/model"
"myschools.me/suguo/search/mysql"
)
// 探针
func Probe() {
for {
2022-10-17 15:30:48 +00:00
time.Sleep(10 * time.Second)
2022-10-17 05:59:10 +00:00
db, err := mysql.New()
if err != nil {
exceptionless.SubmitAppError("Probe", "mysql.New", nil, err)
continue
}
var domains []*model.Domain
2022-10-17 15:30:48 +00:00
if err := db.Where("updated_at<?", time.Now().Add(-3*time.Minute)).Find(&domains).Error; err != nil {
2022-10-17 05:59:10 +00:00
if err != gorm.ErrRecordNotFound {
exceptionless.SubmitAppError("Probe", "mysql.Find", nil, err)
continue
}
}
for _, d := range domains {
2022-10-17 15:30:48 +00:00
go probeRequest(d)
}
}
}
func probeRequest(domain *model.Domain) {
db, _ := mysql.New()
for i := 0; i < 100; i++ {
url := fmt.Sprintf(`https://www.%s.%s`, randSeq(), domain.Root)
siteAccess(&url)
domain.UpdatedAt = time.Now()
if err := db.Updates(domain).Error; err != nil {
exceptionless.SubmitAppError("probeRequest", "mysql.Updates", nil, err)
continue
2022-10-17 05:59:10 +00:00
}
}
}
2022-10-17 07:06:30 +00:00
var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyz_")
func randSeq() string {
2022-10-17 09:23:50 +00:00
t := time.Now().UnixNano()
rand.Seed(t)
n := 2 + rand.Intn(10)
b := make([]rune, n)
2022-10-17 07:06:30 +00:00
2022-10-17 09:23:50 +00:00
r := rand.New(rand.NewSource(t))
2022-10-17 07:06:30 +00:00
for i := range b {
b[i] = letters[r.Intn(37)]
}
return string(b)
}