From f3d77b58168321498a08f2e4534f3be771373e6d Mon Sep 17 00:00:00 2001 From: timerzz Date: Wed, 4 Dec 2024 19:30:22 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=90=86?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/proxy/option.go | 23 ++++++++++++++++++- pkg/proxy/proxy.go | 55 +++++++++++++++++++++++++++++++++------------ 2 files changed, 63 insertions(+), 15 deletions(-) diff --git a/pkg/proxy/option.go b/pkg/proxy/option.go index efd14c6..662dc3c 100644 --- a/pkg/proxy/option.go +++ b/pkg/proxy/option.go @@ -1,8 +1,29 @@ package proxy -import "time" +import ( + "os" + "time" + + "gopkg.in/yaml.v3" +) + +const ( + ProxyConfigEnv = "PROXY_CONFIG_PATH" + DefaultProxyConfigPath = "/cfg/proxy.yaml" +) type Option struct { Subscribes []string `yaml:"subscribes"` + Clash []string `yaml:"clash"` Interval time.Duration `yaml:"interval"` } + +func LoadProxyConfig(path string) (*Option, error) { + f, err := os.Open(path) + if err != nil { + return nil, err + } + defer f.Close() + var opt Option + return &opt, yaml.NewDecoder(f).Decode(&opt) +} diff --git a/pkg/proxy/proxy.go b/pkg/proxy/proxy.go index d01c10f..13c1e9d 100644 --- a/pkg/proxy/proxy.go +++ b/pkg/proxy/proxy.go @@ -3,26 +3,39 @@ package proxy import ( "context" "fmt" + "log/slog" + "math/rand" + "os" + "sync" + "time" + "github.com/golang/glog" "github.com/timerzz/proxypool/pkg/getter" "github.com/timerzz/proxypool/pkg/proxy" "github.com/timerzz/proxypool/pkg/tool" - "log/slog" - "math/rand" - "sync" - "time" ) type ProxyPool struct { - m sync.Mutex - proxies proxy.ProxyList - subscribes []string //订阅url - updated time.Time + m sync.Mutex + proxies proxy.ProxyList + cfg *Option + updated time.Time } -func NewProxyPool(subscribes []string) *ProxyPool { +func InitDefaultProxyPool() (*ProxyPool, error) { + path := os.Getenv(ProxyConfigEnv) + if path == "" { + path = DefaultProxyConfigPath + } + cfg, err := LoadProxyConfig(path) + if err != nil { + return nil, fmt.Errorf("获取代理池配置失败:%v", err) + } + return NewProxyPool(cfg), nil +} +func NewProxyPool(cfg *Option) *ProxyPool { var p = &ProxyPool{} - p.subscribes = subscribes + p.cfg = cfg p.Update() return p } @@ -34,15 +47,29 @@ func (p *ProxyPool) Status() (proxy.ProxyList, time.Time) { // Update 更新代理池 func (p *ProxyPool) Update() { var list = make(proxy.ProxyList, 0, len(p.proxies)) - for _, url := range p.subscribes { - subscribeGetter, err := getter.NewSubscribe(tool.Options{"url": url}) + var getters = make([]getter.Getter, 0, len(p.cfg.Clash)+len(p.cfg.Subscribes)) + for _, url := range p.cfg.Subscribes { + gtr, err := getter.NewSubscribe(tool.Options{"url": url}) if err != nil { slog.Warn(fmt.Sprintf("创建Subscribe Getter失败:%v", err)) continue } - list = list.UniqAppendProxyList(subscribeGetter.Get()) + getters = append(getters, gtr) } - glog.Infof("代理源共 %d 个: %v", len(p.subscribes), p.subscribes) + for _, url := range p.cfg.Clash { + gtr, err := getter.NewClashGetter(tool.Options{"url": url}) + if err != nil { + slog.Warn(fmt.Sprintf("创建Clash Getter失败:%v", err)) + continue + } + getters = append(getters, gtr) + } + + for _, gtr := range getters { + list = list.UniqAppendProxyList(gtr.Get()) + } + + glog.Infof("代理源共 %d 个: %v", len(p.cfg.Subscribes), p.cfg.Subscribes) glog.Infof("获取代理共 %d 个", len(list)) p.m.Lock() p.proxies = list