2024-12-10 10:38:35 +08:00
|
|
|
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) {
|
2024-12-10 11:30:39 +08:00
|
|
|
var urls = make([]string, 0)
|
2024-12-10 10:38:35 +08:00
|
|
|
url := c.cfg.Url
|
|
|
|
if c.cfg.DateFormat {
|
2024-12-10 11:30:39 +08:00
|
|
|
now := time.Now()
|
|
|
|
urls = append(urls, now.Format(url))
|
|
|
|
now = now.AddDate(0, 0, -1)
|
|
|
|
urls = append(urls, now.Format(url))
|
|
|
|
} else {
|
|
|
|
urls = append(urls, url)
|
2024-12-10 10:38:35 +08:00
|
|
|
}
|
2024-12-10 11:30:39 +08:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
2024-12-10 10:38:35 +08:00
|
|
|
}
|
|
|
|
}
|
2024-12-10 11:30:39 +08:00
|
|
|
|
2024-12-10 10:38:35 +08:00
|
|
|
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
|
|
|
|
}
|