generated from kedaya_haitao/template
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package getter
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
configv1 "gitea.timerzz.com/timerzz/proxy-detector/config"
|
|
"gitea.timerzz.com/timerzz/proxy-detector/log"
|
|
"gitea.timerzz.com/timerzz/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) {
|
|
urls := c.cfg.Urls()
|
|
for _, u := range urls {
|
|
fmt.Printf("fetching %s\n", u)
|
|
var clashConfig clash_config.RawConfig
|
|
resp, err := c.cli.R().Get(u)
|
|
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
|
|
}
|
|
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
|
|
}
|