From 42035022c6d1744c8d5f53f3920788898be1f98e Mon Sep 17 00:00:00 2001 From: zu1k Date: Sun, 23 Aug 2020 17:14:04 +0800 Subject: [PATCH] splite source from config --- assets/html/index.html | 4 ++-- config/config.go | 17 ++++++----------- config/config.yaml | 33 +++------------------------------ config/source.go | 8 ++++++++ config/source.yaml | 30 ++++++++++++++++++++++++++++++ internal/app/getter.go | 19 ++++++++++++++++--- 6 files changed, 65 insertions(+), 46 deletions(-) create mode 100644 config/source.go create mode 100644 config/source.yaml diff --git a/assets/html/index.html b/assets/html/index.html index d9dd8c7..c8e3f2b 100644 --- a/assets/html/index.html +++ b/assets/html/index.html @@ -115,7 +115,7 @@
可用节点数量:{{ .useful_proxies_count }}
最后更新时间:{{ .last_crawl_time }}
Clash配置文件:https://{{ .domain }}/clash/config 一键导入
Clash proxy-provider(Shadowrocket添加订阅方式可用):https://{{ .domain }}/clash/proxies @@ -123,7 +123,7 @@
筛选代理类型:https://{{ .domain }}/clash/proxies?type=ss,ssr,vmess
筛选国家:https://{{ .domain }}/clash/proxies?c=HK,TW,US
Surge配置文件:https://{{ .domain }}/surge/config 一键导入
Surge proxy list:https://{{ .domain }}/surge/proxies diff --git a/config/config.go b/config/config.go index 7d484ef..d5840b7 100644 --- a/config/config.go +++ b/config/config.go @@ -12,16 +12,11 @@ import ( var configFilePath = "config.yaml" -type Source struct { - Type string `json:"type" yaml:"type"` - Options tool.Options `json:"options" yaml:"options"` -} - type ConfigOptions struct { - Domain string `json:"domain" yaml:"domain"` - CFEmail string `json:"cf_email" yaml:"cf_email"` - CFKey string `json:"cf_key" yaml:"cf_key"` - Sources []Source `json:"sources" yaml:"sources"` + Domain string `json:"domain" yaml:"domain"` + CFEmail string `json:"cf_email" yaml:"cf_email"` + CFKey string `json:"cf_key" yaml:"cf_key"` + SourceFiles []string `json:"source-files" yaml:"source-files"` } // Config 配置 @@ -34,7 +29,7 @@ func Parse(path string) error { } else { configFilePath = path } - fileData, err := readConfigFile(path) + fileData, err := ReadFile(path) if err != nil { return err } @@ -59,7 +54,7 @@ func Parse(path string) error { } // 从本地文件或者http链接读取配置文件内容 -func readConfigFile(path string) ([]byte, error) { +func ReadFile(path string) ([]byte, error) { if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") { resp, err := tool.GetHttpClient().Get(path) if err != nil { diff --git a/config/config.yaml b/config/config.yaml index ff88f42..2568344 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,33 +1,6 @@ -domain: proxy.tgbot.co +domain: example.com cf_email: "" cf_key: "" sources: - # 模糊抓取订阅链接 - - type: webfuzzsub - options: - url: https://raw.githubusercontent.com/du5/free/master/sub.list - - # 订阅链接 - - type: subscribe - options: - url: https://raw.githubusercontent.com/ssrsub/ssr/master/v2ray - - # 网页模糊抓取 - - type: webfuzz - options: - url: https://merlinblog.xyz/wiki/freess.html - - # tg频道抓取 - - type: tgchannel - options: - channel: ssrList - num: 200 - - # 翻墙党论坛抓取 - - type: web-fanqiangdang-rss - options: - url: https://fanqiangdang.com/forum.php?mod=rss&fid=50&auth=0 - - # 某个网站抓取 - - type: web-freessrxyz - options: + - ./source.yaml + - https://example.com/source.yaml \ No newline at end of file diff --git a/config/source.go b/config/source.go new file mode 100644 index 0000000..5718d9a --- /dev/null +++ b/config/source.go @@ -0,0 +1,8 @@ +package config + +import "github.com/zu1k/proxypool/pkg/tool" + +type Source struct { + Type string `json:"type" yaml:"type"` + Options tool.Options `json:"options" yaml:"options"` +} diff --git a/config/source.yaml b/config/source.yaml new file mode 100644 index 0000000..6c44bf8 --- /dev/null +++ b/config/source.yaml @@ -0,0 +1,30 @@ +sources: + # 模糊抓取订阅链接 + - type: webfuzzsub + options: + url: https://raw.githubusercontent.com/du5/free/master/sub.list + + # 订阅链接 + - type: subscribe + options: + url: https://raw.githubusercontent.com/ssrsub/ssr/master/v2ray + + # 网页模糊抓取 + - type: webfuzz + options: + url: https://merlinblog.xyz/wiki/freess.html + + # tg频道抓取 + - type: tgchannel + options: + channel: ssrList + num: 200 + + # 翻墙党论坛抓取 + - type: web-fanqiangdang-rss + options: + url: https://fanqiangdang.com/forum.php?mod=rss&fid=50&auth=0 + + # 某个网站抓取 + - type: web-freessrxyz + options: diff --git a/internal/app/getter.go b/internal/app/getter.go index 8ff1038..803a5c4 100644 --- a/internal/app/getter.go +++ b/internal/app/getter.go @@ -4,6 +4,8 @@ import ( "errors" "fmt" + "github.com/ghodss/yaml" + "github.com/zu1k/proxypool/config" "github.com/zu1k/proxypool/pkg/getter" ) @@ -15,7 +17,7 @@ func InitConfigAndGetters(path string) (err error) { if err != nil { return } - if s := config.Config.Sources; len(s) == 0 { + if s := config.Config.SourceFiles; len(s) == 0 { return errors.New("no sources") } else { initGetters(s) @@ -23,9 +25,20 @@ func InitConfigAndGetters(path string) (err error) { return } -func initGetters(sources []config.Source) { +func initGetters(sourceFiles []string) { Getters = make([]getter.Getter, 0) - for _, source := range sources { + for _, path := range sourceFiles { + source := config.Source{} + data, err := config.ReadFile(path) + if err != nil { + fmt.Errorf("Init SourceFile Error: %s\n", err.Error()) + continue + } + err = yaml.Unmarshal(data, &source) + if err != nil { + fmt.Errorf("Init SourceFile Error: %s\n", err.Error()) + continue + } g, err := getter.NewGetter(source.Type, source.Options) if err == nil && g != nil { Getters = append(Getters, g)