feat 修改代理配置
This commit is contained in:
parent
2cc7b5cfb7
commit
f3d77b5816
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user