generated from kedaya_haitao/template
71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package getter
|
|
|
|
import (
|
|
"os"
|
|
"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"
|
|
"gitea.timerzz.com/timerzz/proxy-detector/pkg/templates"
|
|
"github.com/go-resty/resty/v2"
|
|
"github.com/metacubex/mihomo/adapter"
|
|
clash_config "github.com/metacubex/mihomo/config"
|
|
"github.com/nitezs/sub2clash/api/handler"
|
|
"github.com/nitezs/sub2clash/config"
|
|
"github.com/nitezs/sub2clash/logger"
|
|
"github.com/nitezs/sub2clash/model"
|
|
"github.com/nitezs/sub2clash/validator"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func init() {
|
|
Register("subscribe", NewSubscribeGetter)
|
|
_ = config.LoadConfig()
|
|
logger.InitLogger("Warn")
|
|
_ = os.MkdirAll("./subs", 0755)
|
|
}
|
|
|
|
type Subscribe struct {
|
|
cfg configv1.CrawOption
|
|
cli *resty.Client
|
|
}
|
|
|
|
func (c *Subscribe) Get() (proxies []structs.Proxy) {
|
|
urls := c.cfg.Urls()
|
|
vlr := validator.SubValidator{Subs: urls, Refresh: true}
|
|
sub, err := handler.BuildSub(model.ClashMeta, vlr, templates.DefaultTemplate)
|
|
if err != nil {
|
|
log.Errorln("get subscribe config error: %s", err.Error())
|
|
return nil
|
|
}
|
|
var bytes []byte
|
|
if bytes, err = yaml.Marshal(sub); err != nil {
|
|
log.Errorln("marshal subscribe config error: %s", err.Error())
|
|
return nil
|
|
}
|
|
|
|
var clashConfig clash_config.RawConfig
|
|
if err = yaml.Unmarshal(bytes, &clashConfig); err != nil {
|
|
log.Errorln("unmarshal subscribe 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 NewSubscribeGetter(options configv1.CrawOption) (getter Getter, err error) {
|
|
if options.Url != "" {
|
|
return &Subscribe{
|
|
cfg: options,
|
|
cli: resty.New().SetTimeout(time.Second * 30),
|
|
}, nil
|
|
}
|
|
return nil, ErrorUrlNotFound
|
|
}
|