imitater/config.go

215 lines
4.3 KiB
Go
Raw Normal View History

2018-12-07 10:21:23 +00:00
package imitater
2018-11-26 01:30:02 +00:00
import (
2018-11-26 10:27:28 +00:00
"errors"
2018-11-26 01:30:02 +00:00
"io/ioutil"
2018-12-06 06:44:10 +00:00
"net/http"
2018-11-26 01:30:02 +00:00
"os"
2018-11-26 10:27:28 +00:00
"reflect"
2018-11-26 01:30:02 +00:00
"strings"
2018-12-07 11:09:38 +00:00
"474420502.top/eson/structure/circular_linked"
2018-11-26 01:30:02 +00:00
yaml "gopkg.in/yaml.v2"
)
2018-11-26 10:27:28 +00:00
// YamlCurls 为了自定义序列化函数
type YamlCurls []string
// UnmarshalYAML YamlCurls反序列化函数
func (curls *YamlCurls) UnmarshalYAML(unmarshal func(interface{}) error) error {
var buf interface{}
err := unmarshal(&buf)
if err != nil {
return nil
}
switch tbuf := buf.(type) {
case string:
2018-12-21 10:23:03 +00:00
if tbuf != "" {
for _, curlinfo := range parseCurl(tbuf) {
*curls = append(*curls, curlinfo)
}
2018-12-07 10:21:23 +00:00
}
2018-11-26 10:27:28 +00:00
case []interface{}:
for _, ifa := range tbuf {
2018-12-21 10:23:03 +00:00
curlstr := ifa.(string)
if curlstr != "" {
for _, curlinfo := range parseCurl(curlstr) {
*curls = append(*curls, curlinfo)
}
2018-12-07 10:21:23 +00:00
}
2018-12-21 10:23:03 +00:00
2018-11-26 10:27:28 +00:00
}
default:
return errors.New("read curls is error, " + reflect.TypeOf(buf).String())
}
return nil
}
2018-12-06 10:34:10 +00:00
// MarshalYAML YamlCurls序列化函数
2018-11-26 10:27:28 +00:00
func (curls *YamlCurls) MarshalYAML() (interface{}, error) {
content := "["
for _, curl := range []string(*curls) {
content += "\"" + curl + "\"" + ", "
}
content = strings.TrimRight(content, ", ")
content += "]"
return content, nil
}
2018-12-06 10:34:10 +00:00
// YamlProxies 为了自定义序列化函数
2018-12-07 11:09:38 +00:00
type YamlProxies clinked.CircularLinked
2018-12-06 06:44:10 +00:00
2018-12-06 10:34:10 +00:00
// UnmarshalYAML YamlProxies反序列化函数
2018-12-06 06:44:10 +00:00
func (proxies *YamlProxies) UnmarshalYAML(unmarshal func(interface{}) error) error {
var buf interface{}
err := unmarshal(&buf)
if err != nil {
return nil
}
switch tbuf := buf.(type) {
case string:
2018-12-07 11:09:38 +00:00
p := (*clinked.CircularLinked)(proxies)
p.Append(tbuf)
2018-12-06 06:44:10 +00:00
case []interface{}:
2018-12-07 11:09:38 +00:00
p := (*clinked.CircularLinked)(proxies)
2018-12-06 06:44:10 +00:00
for _, ifa := range tbuf {
2018-12-07 11:09:38 +00:00
p.Append(ifa.(string))
2018-12-06 06:44:10 +00:00
}
default:
return errors.New("read curls is error, " + reflect.TypeOf(buf).String())
}
return nil
}
2018-12-06 10:34:10 +00:00
// MarshalYAML YamlProxies 序列化函数
2018-12-06 06:44:10 +00:00
func (proxies *YamlProxies) MarshalYAML() (interface{}, error) {
content := "["
2018-12-07 11:09:38 +00:00
p := (*clinked.CircularLinked)(proxies)
for _, cnode := range p.GetLoopValues() {
content += "\"" + cnode.GetValue().(string) + "\"" + ", "
2018-12-06 06:44:10 +00:00
}
content = strings.TrimRight(content, ", ")
content += "]"
return content, nil
}
2018-12-10 11:22:57 +00:00
// ADInfo ad 的一些属性,基础信息等
type ADInfo struct {
Priority int `yaml:"priority"`
2018-11-26 01:30:02 +00:00
Device string `yaml:"device"`
Platform string `yaml:"platform"`
AreaCC string `yaml:"area_cc"`
Channel int `yaml:"channel"`
Media int `yaml:"media"`
SpiderID int `yaml:"spider_id"`
CatchAccountID int `yaml:"catch_account_id"`
}
2018-12-10 11:22:57 +00:00
// Config 任务加载的默认配置
type Config struct {
// Session int `yaml:"session"`
Mode int `yaml:"mode"`
Proxies *YamlProxies `yaml:"proxies"`
Retry int `yaml:"retry"`
Curls YamlCurls `yaml:"curls"`
Crontab string `yaml:"crontab"`
ITask string `yaml:"task"`
ADInfo `yaml:",inline"`
}
2018-11-26 01:30:02 +00:00
// newDefaultConfig create a default config
func newDefaultConfig() *Config {
conf := &Config{
// Session: 1,
2018-12-10 11:22:57 +00:00
Mode: 0,
Retry: 0,
Crontab: "",
ADInfo: ADInfo{
Device: "",
Platform: "",
AreaCC: "",
Channel: -1,
Media: -1,
SpiderID: -1,
CatchAccountID: -1,
},
2018-11-26 01:30:02 +00:00
}
return conf
}
// NewConfig 加载并返回Config
func NewConfig(p string) *Config {
f, err := os.Open(p)
defer f.Close()
if err != nil {
panic(err)
}
conf := newDefaultConfig()
err = yaml.NewDecoder(f).Decode(conf)
2018-12-06 06:44:10 +00:00
2018-11-26 01:30:02 +00:00
if err != nil {
panic(err)
}
2018-11-26 10:27:28 +00:00
return conf
}
2018-12-07 10:21:23 +00:00
func parseCurl(curl string) []string {
var result []string
2018-12-06 06:44:10 +00:00
switch curl[0] {
case '@':
2018-11-26 10:27:28 +00:00
curlfile, err := os.Open(curl[1:])
defer curlfile.Close()
if err != nil {
panic(err)
2018-11-26 01:30:02 +00:00
}
2018-12-07 10:21:23 +00:00
2018-11-26 10:27:28 +00:00
curldata, err := ioutil.ReadAll(curlfile)
2018-12-07 10:21:23 +00:00
for _, curlinfo := range strings.Split(string(curldata), "\n") {
2018-12-21 11:02:18 +00:00
curlstr := strings.Trim(curlinfo, "\r\n ")
if len(curlstr) >= 4 {
result = append(result, curlstr)
}
2018-12-07 10:21:23 +00:00
}
2018-12-06 06:44:10 +00:00
case '#':
resp, err := http.Get(curl[1:])
if err != nil {
panic(err)
}
curldata, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
2018-12-07 10:21:23 +00:00
for _, curlinfo := range strings.Split(string(curldata), "\n") {
2018-12-21 11:02:18 +00:00
curlstr := strings.Trim(curlinfo, "\r\n ")
if len(curlstr) >= 4 {
result = append(result, curlstr)
}
2018-12-07 10:21:23 +00:00
}
2018-12-09 14:25:48 +00:00
default:
2018-12-21 11:02:18 +00:00
curlstr := strings.Trim(curl, "\r\n ")
if len(curlstr) >= 4 {
result = append(result, curlstr)
}
2018-11-26 01:30:02 +00:00
}
2018-12-07 10:21:23 +00:00
return result
2018-11-26 01:30:02 +00:00
}