feat 修改代理配置
This commit is contained in:
parent
2cc7b5cfb7
commit
f3d77b5816
@ -1,8 +1,29 @@
|
|||||||
package proxy
|
package proxy
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
ProxyConfigEnv = "PROXY_CONFIG_PATH"
|
||||||
|
DefaultProxyConfigPath = "/cfg/proxy.yaml"
|
||||||
|
)
|
||||||
|
|
||||||
type Option struct {
|
type Option struct {
|
||||||
Subscribes []string `yaml:"subscribes"`
|
Subscribes []string `yaml:"subscribes"`
|
||||||
|
Clash []string `yaml:"clash"`
|
||||||
Interval time.Duration `yaml:"interval"`
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
"github.com/timerzz/proxypool/pkg/getter"
|
"github.com/timerzz/proxypool/pkg/getter"
|
||||||
"github.com/timerzz/proxypool/pkg/proxy"
|
"github.com/timerzz/proxypool/pkg/proxy"
|
||||||
"github.com/timerzz/proxypool/pkg/tool"
|
"github.com/timerzz/proxypool/pkg/tool"
|
||||||
"log/slog"
|
|
||||||
"math/rand"
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type ProxyPool struct {
|
type ProxyPool struct {
|
||||||
m sync.Mutex
|
m sync.Mutex
|
||||||
proxies proxy.ProxyList
|
proxies proxy.ProxyList
|
||||||
subscribes []string //订阅url
|
cfg *Option
|
||||||
updated time.Time
|
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{}
|
var p = &ProxyPool{}
|
||||||
p.subscribes = subscribes
|
p.cfg = cfg
|
||||||
p.Update()
|
p.Update()
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
@ -34,15 +47,29 @@ func (p *ProxyPool) Status() (proxy.ProxyList, time.Time) {
|
|||||||
// Update 更新代理池
|
// Update 更新代理池
|
||||||
func (p *ProxyPool) Update() {
|
func (p *ProxyPool) Update() {
|
||||||
var list = make(proxy.ProxyList, 0, len(p.proxies))
|
var list = make(proxy.ProxyList, 0, len(p.proxies))
|
||||||
for _, url := range p.subscribes {
|
var getters = make([]getter.Getter, 0, len(p.cfg.Clash)+len(p.cfg.Subscribes))
|
||||||
subscribeGetter, err := getter.NewSubscribe(tool.Options{"url": url})
|
for _, url := range p.cfg.Subscribes {
|
||||||
|
gtr, err := getter.NewSubscribe(tool.Options{"url": url})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Warn(fmt.Sprintf("创建Subscribe Getter失败:%v", err))
|
slog.Warn(fmt.Sprintf("创建Subscribe Getter失败:%v", err))
|
||||||
continue
|
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))
|
glog.Infof("获取代理共 %d 个", len(list))
|
||||||
p.m.Lock()
|
p.m.Lock()
|
||||||
p.proxies = list
|
p.proxies = list
|
||||||
|
Loading…
Reference in New Issue
Block a user