proxy-detector/main.go

69 lines
1.9 KiB
Go

package main
import (
"context"
"flag"
"os"
"gitea.timerzz.com/timerzz/proxy-detector/config"
"gitea.timerzz.com/timerzz/proxy-detector/log"
"gitea.timerzz.com/timerzz/proxy-detector/pkg/getter"
"gitea.timerzz.com/timerzz/proxy-detector/pkg/proxy"
"gitea.timerzz.com/timerzz/proxy-detector/pkg/proxy/structs"
"gitea.timerzz.com/timerzz/proxy-detector/pkg/worker"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/cors"
"github.com/gofiber/fiber/v3/middleware/recover"
"github.com/metacubex/mihomo/component/process"
clash_config "github.com/metacubex/mihomo/config"
"gopkg.in/yaml.v3"
)
var debugMode = false
func main() {
ctx := context.Background()
var configFilePath = ""
flag.StringVar(&configFilePath, "c", "", "path to config file: config.yaml")
flag.BoolVar(&debugMode, "d", false, "debug output")
flag.Parse()
log.SetLevel(log.INFO)
if debugMode {
log.SetLevel(log.DEBUG)
log.Debugln("=======Debug Mode=======")
}
if configFilePath == "" {
configFilePath = os.Getenv("CONFIG_FILE")
}
if configFilePath == "" {
configFilePath = "./config/config.yaml"
}
config.LoadConfig(configFilePath)
worker.Init(config.Config.Goroutines)
getters, err := getter.CreateGetters(config.Config.SourceFiles)
if err != nil {
log.Errorln("create getters failed: %s", err.Error())
os.Exit(-1)
}
proxy.CrawlProxies(ctx, getters)
go proxy.CronCrawl(ctx, getters, config.Config.CrawlInterval)
go proxy.CronHealthCheck(ctx, config.Config.HealthCheckInterval)
r := fiber.New()
r.Use(cors.New(), recover.New())
r.Get("/clash/config", func(ctx fiber.Ctx) error {
mappings := structs.ProxiesList.Mapping()
rawConfig := clash_config.RawConfig{Proxy: mappings, FindProcessMode: process.FindProcessAlways}
return yaml.NewEncoder(ctx.Response().BodyWriter()).Encode(&rawConfig)
})
_ = r.Listen(":"+config.Config.Port, fiber.ListenConfig{
EnablePrintRoutes: true,
GracefulContext: ctx,
})
}