proxy-detector/pkg/getter/clash.go

61 lines
1.4 KiB
Go
Raw Normal View History

package getter
import (
"fmt"
"time"
configv1 "gitea.timerzz.com/kedaya_haitao/proxy-detector/config"
"gitea.timerzz.com/kedaya_haitao/proxy-detector/log"
"gitea.timerzz.com/kedaya_haitao/proxy-detector/pkg/proxy/structs"
"github.com/go-resty/resty/v2"
"github.com/metacubex/mihomo/adapter"
clash_config "github.com/metacubex/mihomo/config"
"gopkg.in/yaml.v3"
)
func init() {
Register("clash", NewClashGetter)
}
type Clash struct {
cfg configv1.CrawOption
cli *resty.Client
}
func (c *Clash) Get() (proxies []structs.Proxy) {
url := c.cfg.Url
if c.cfg.DateFormat {
url = time.Now().Format(url)
}
fmt.Printf("fetching %s\n", url)
var clashConfig clash_config.RawConfig
resp, err := c.cli.R().Get(url)
if err != nil {
log.Errorln("get clash config error: %s", err.Error())
return nil
}
err = yaml.Unmarshal(resp.Body(), &clashConfig)
if err != nil {
log.Errorln("decode clash config error: %s", err.Error())
return nil
}
proxies = make([]structs.Proxy, 0, len(clashConfig.Proxy))
for _, mapping := range clashConfig.Proxy {
proxy, _ := adapter.ParseProxy(mapping)
if proxy != nil {
proxies = append(proxies, structs.NewProxy(proxy, mapping))
}
}
return proxies
}
func NewClashGetter(options configv1.CrawOption) (getter Getter, err error) {
if options.Url != "" {
return &Clash{
cfg: options,
cli: resty.New().SetTimeout(time.Second * 30),
}, nil
}
return nil, ErrorUrlNotFound
}