search/service/probe-service.go

79 lines
1.7 KiB
Go

package service
import (
"fmt"
"math/rand"
"time"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"myschools.me/suguo/search/exceptionless"
"myschools.me/suguo/search/model"
"myschools.me/suguo/search/mysql"
)
// 探针
func Probe() {
for {
time.Sleep(10 * time.Second)
db, err := mysql.New()
if err != nil {
exceptionless.SubmitAppError("Probe", "mysql.New", nil, err)
continue
}
var domains []*model.Domain
if err := db.Where("updated_at<?", time.Now().Add(-3*time.Minute)).Find(&domains).Error; err != nil {
if err != gorm.ErrRecordNotFound {
exceptionless.SubmitAppError("Probe", "db.Find", nil, err)
continue
}
}
for _, d := range domains {
go probeRequest(d)
}
}
}
func probeRequest(domain *model.Domain) {
db, _ := mysql.New()
for i := 0; i < 100; i++ {
val := randSeq()
url := fmt.Sprintf(`%s.%s`, val, domain.Root)
err := siteAccess(&url)
if err != nil {
if len(val) < 4 {
if err.Error() == fmt.Sprintf("dial tcp: lookup www.%s.%s: no such host", val, domain.Root) {
if err := db.Clauses(clause.OnConflict{UpdateAll: true}).Create(&model.SiteObj{
Root: domain.Root,
Domain: val,
}).Error; err != nil {
exceptionless.SubmitAppError("probeRequest", "db.Create", nil, err)
}
}
}
}
domain.UpdatedAt = time.Now()
if err := db.Updates(domain).Error; err != nil {
exceptionless.SubmitAppError("probeRequest", "db.Updates", nil, err)
continue
}
}
}
var letters = []rune("0123456789abcdefghijklmnopqrstuvwxyz_")
func randSeq() string {
t := time.Now().UnixNano()
rand.Seed(t)
n := 2 + rand.Intn(10)
b := make([]rune, n)
r := rand.New(rand.NewSource(t))
for i := range b {
b[i] = letters[r.Intn(37)]
}
return string(b)
}