2018-12-07 10:21:23 +00:00
|
|
|
package imitater
|
2018-12-07 04:18:29 +00:00
|
|
|
|
|
|
|
import (
|
2018-12-07 10:21:23 +00:00
|
|
|
"fmt"
|
2018-12-07 04:18:29 +00:00
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
2018-12-07 11:09:38 +00:00
|
|
|
"474420502.top/eson/structure/circular_linked"
|
|
|
|
|
2018-12-07 04:18:29 +00:00
|
|
|
"474420502.top/eson/curl2info"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ITask 继承这个接口的类
|
|
|
|
type ITask interface {
|
2018-12-07 10:21:23 +00:00
|
|
|
Execute(data interface{}) ITask
|
2018-12-18 06:38:59 +00:00
|
|
|
Init()
|
2018-12-07 04:18:29 +00:00
|
|
|
|
|
|
|
SetCrontab(cron string)
|
|
|
|
SetLastStatus(status bool)
|
|
|
|
|
2018-12-07 10:21:23 +00:00
|
|
|
SetName(name string)
|
|
|
|
GetName() string
|
|
|
|
|
2018-12-07 04:18:29 +00:00
|
|
|
SetCurl(Curl *curl2info.CURL)
|
2018-12-07 10:21:23 +00:00
|
|
|
GetCurl() *curl2info.CURL
|
2018-12-07 04:18:29 +00:00
|
|
|
|
2018-12-07 11:09:38 +00:00
|
|
|
GetProxies() *clinked.CircularLinked
|
|
|
|
AddProxies(proxy string)
|
2018-12-07 04:18:29 +00:00
|
|
|
|
2018-12-18 07:33:57 +00:00
|
|
|
SetADInfo(adinfo ADInfo)
|
|
|
|
GetADInfo() *ADInfo
|
|
|
|
|
2018-12-07 04:18:29 +00:00
|
|
|
TimeUp() bool
|
|
|
|
NextTime() time.Time
|
|
|
|
}
|
|
|
|
|
|
|
|
var register = make(map[string]reflect.Type)
|
|
|
|
|
|
|
|
func init() {
|
2018-12-07 10:21:23 +00:00
|
|
|
log.SetFlags(log.Llongfile)
|
2018-12-07 04:18:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register 注册 类型 ITask为样本的类型
|
2018-12-07 10:21:23 +00:00
|
|
|
func Register(name string, itask ITask) {
|
|
|
|
register[name] = reflect.TypeOf(itask).Elem()
|
2018-12-07 04:18:29 +00:00
|
|
|
}
|
|
|
|
|
2018-12-07 10:21:23 +00:00
|
|
|
func makeRegisterType(name string) ITask {
|
|
|
|
result := reflect.New(register[name]).Interface().(ITask)
|
|
|
|
result.SetName(name)
|
2018-12-07 04:18:29 +00:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Person 以人为单位
|
|
|
|
type Person struct {
|
|
|
|
Tasks []ITask
|
|
|
|
Conf *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPerson 创建一个人实例
|
|
|
|
func NewPerson() *Person {
|
|
|
|
person := &Person{}
|
|
|
|
|
|
|
|
// person.Conf = NewConfig(conf)
|
|
|
|
// person.Tasks = SplitTasks(person.Conf)
|
|
|
|
|
|
|
|
return person
|
|
|
|
}
|
|
|
|
|
2018-12-07 11:09:38 +00:00
|
|
|
// Config 加载配置
|
|
|
|
func (person *Person) Config(conf string) {
|
2018-12-07 04:18:29 +00:00
|
|
|
person.Conf = NewConfig(conf)
|
|
|
|
person.Tasks = splitTasks(person.Conf)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPersonWithConfig 创建一个person
|
|
|
|
func NewPersonWithConfig(conf string) *Person {
|
|
|
|
person := NewPerson()
|
2018-12-07 11:09:38 +00:00
|
|
|
person.Config(conf)
|
2018-12-07 04:18:29 +00:00
|
|
|
return person
|
|
|
|
}
|
|
|
|
|
|
|
|
// SplitTasks 拆开出需求的任务
|
|
|
|
func splitTasks(conf *Config) []ITask {
|
|
|
|
var tasks []ITask
|
2018-12-07 11:09:38 +00:00
|
|
|
proxies := (*clinked.CircularLinked)(conf.Proxies)
|
2018-12-07 04:18:29 +00:00
|
|
|
for _, scurl := range conf.Curls {
|
|
|
|
curl, err := curl2info.ParseRawCURL(scurl)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2018-12-07 10:21:23 +00:00
|
|
|
if curl.ITask == "" {
|
|
|
|
curl.ITask = conf.ITask
|
|
|
|
}
|
|
|
|
task := makeRegisterType(curl.ITask)
|
2018-12-07 04:18:29 +00:00
|
|
|
switch conf.Mode {
|
|
|
|
case 0:
|
2018-12-07 10:21:23 +00:00
|
|
|
initTask(conf, task, curl)
|
2018-12-18 07:33:57 +00:00
|
|
|
// 初始化代理
|
2018-12-09 14:25:48 +00:00
|
|
|
if proxies != nil {
|
|
|
|
for _, cnode := range proxies.GetLoopValues() {
|
|
|
|
proxy := cnode.GetValue().(string)
|
|
|
|
task.AddProxies(proxy)
|
|
|
|
}
|
2018-12-07 11:09:38 +00:00
|
|
|
}
|
2018-12-09 14:25:48 +00:00
|
|
|
|
2018-12-07 11:09:38 +00:00
|
|
|
tasks = append(tasks, task)
|
2018-12-07 04:18:29 +00:00
|
|
|
case 1:
|
2018-12-09 14:25:48 +00:00
|
|
|
if proxies != nil {
|
|
|
|
for _, cnode := range proxies.GetLoopValues() {
|
|
|
|
proxy := cnode.GetValue().(string)
|
|
|
|
|
|
|
|
ncurl, err := curl2info.ParseRawCURL(scurl)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if curl.ITask == "" {
|
|
|
|
curl.ITask = conf.ITask
|
|
|
|
}
|
|
|
|
ptask := makeRegisterType(ncurl.ITask).(ITask)
|
|
|
|
initTask(conf, ptask, ncurl)
|
|
|
|
|
|
|
|
ptask.AddProxies(proxy)
|
|
|
|
tasks = append(tasks, ptask)
|
2018-12-07 04:18:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tasks
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute 人的执行所有任务
|
|
|
|
func (person *Person) Execute() {
|
2018-12-07 10:21:23 +00:00
|
|
|
|
|
|
|
taskLen := len(person.Tasks)
|
|
|
|
|
|
|
|
result := make(chan string, 1)
|
2018-12-07 04:18:29 +00:00
|
|
|
for _, task := range person.Tasks {
|
2018-12-09 14:25:48 +00:00
|
|
|
|
2018-12-07 10:21:23 +00:00
|
|
|
go ExecuteOnPlan(task, result)
|
2018-12-09 14:25:48 +00:00
|
|
|
|
2018-12-07 10:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for t := range result {
|
|
|
|
log.Println(t)
|
|
|
|
taskLen--
|
|
|
|
if taskLen <= 0 {
|
|
|
|
close(result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// engine := gin.Default()
|
|
|
|
// engine.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
// initTask 生成一个新任务
|
|
|
|
func initTask(conf *Config, task ITask, curl *curl2info.CURL) {
|
2018-12-18 06:38:59 +00:00
|
|
|
|
2018-12-07 10:21:23 +00:00
|
|
|
if curl.Crontab != "" {
|
|
|
|
task.SetCrontab(curl.Crontab)
|
|
|
|
} else {
|
|
|
|
task.SetCrontab(conf.Crontab)
|
|
|
|
}
|
|
|
|
|
2018-12-18 07:33:57 +00:00
|
|
|
task.SetADInfo(conf.ADInfo)
|
2018-12-07 10:21:23 +00:00
|
|
|
task.SetCurl(curl)
|
2018-12-18 07:33:57 +00:00
|
|
|
|
|
|
|
task.Init()
|
2018-12-07 10:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExecuteOnPlan 按照计划执行任务并返回结果
|
|
|
|
func ExecuteOnPlan(task ITask, result chan string) {
|
|
|
|
data := make(map[string]interface{})
|
|
|
|
var rtask ITask
|
|
|
|
taskname := task.GetName()
|
|
|
|
urlname := task.GetCurl().Name
|
|
|
|
|
|
|
|
for {
|
2018-12-09 13:39:44 +00:00
|
|
|
interval := time.Second * 2
|
2018-12-07 10:21:23 +00:00
|
|
|
if task.TimeUp() {
|
|
|
|
rtask = task.Execute(data) // 事件 在这里变化
|
2018-12-09 13:39:44 +00:00
|
|
|
ntime := task.NextTime()
|
|
|
|
interval = ntime.Sub(time.Now())
|
2018-12-07 10:21:23 +00:00
|
|
|
if rtask == nil {
|
|
|
|
// log.Println("rtask is nil")
|
|
|
|
result <- fmt.Sprintf("rtask is nil, the first task = %s, name = %s", taskname, urlname)
|
|
|
|
return
|
|
|
|
}
|
2018-12-09 13:39:44 +00:00
|
|
|
task = rtask
|
|
|
|
|
2018-12-07 10:21:23 +00:00
|
|
|
}
|
|
|
|
time.Sleep(interval)
|
2018-12-07 04:18:29 +00:00
|
|
|
}
|
|
|
|
}
|