| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package api
- import (
- "html/template"
- "net/http"
- "os"
- "github.com/gin-gonic/gin"
- _ "github.com/heroku/x/hmetrics/onload"
- "github.com/zu1k/proxypool/config"
- binhtml "github.com/zu1k/proxypool/internal/bindata/html"
- "github.com/zu1k/proxypool/internal/cache"
- "github.com/zu1k/proxypool/pkg/provider"
- )
- const version = "v0.3.2"
- var router *gin.Engine
- func setupRouter() {
- gin.SetMode(gin.ReleaseMode)
- router = gin.New()
- router.Use(gin.Logger(), gin.Recovery())
- temp, err := loadTemplate()
- if err != nil {
- panic(err)
- }
- router.SetHTMLTemplate(temp)
- router.GET("/", func(c *gin.Context) {
- c.HTML(http.StatusOK, "assets/html/index.html", gin.H{
- "domain": config.Config.Domain,
- "getters_count": cache.GettersCount,
- "all_proxies_count": cache.AllProxiesCount,
- "ss_proxies_count": cache.SSProxiesCount,
- "ssr_proxies_count": cache.SSRProxiesCount,
- "vmess_proxies_count": cache.VmessProxiesCount,
- "trojan_proxies_count": cache.TrojanProxiesCount,
- "useful_proxies_count": cache.UsefullProxiesCount,
- "last_crawl_time": cache.LastCrawlTime,
- "version": version,
- })
- })
- router.GET("/clash", func(c *gin.Context) {
- c.HTML(http.StatusOK, "assets/html/clash.html", gin.H{
- "domain": config.Config.Domain,
- })
- })
- router.GET("/surge", func(c *gin.Context) {
- c.HTML(http.StatusOK, "assets/html/surge.html", gin.H{
- "domain": config.Config.Domain,
- })
- })
- router.GET("/clash/config", func(c *gin.Context) {
- c.HTML(http.StatusOK, "assets/html/clash-config.yaml", gin.H{
- "domain": config.Config.Domain,
- })
- })
- router.GET("/surge/config", func(c *gin.Context) {
- c.HTML(http.StatusOK, "assets/html/surge.conf", gin.H{
- "domain": config.Config.Domain,
- })
- })
- router.GET("/clash/proxies", func(c *gin.Context) {
- proxyTypes := c.DefaultQuery("type", "")
- proxyCountry := c.DefaultQuery("c", "")
- text := ""
- if proxyTypes == "" && proxyCountry == "" {
- text = cache.GetString("clashproxies")
- if text == "" {
- proxies := cache.GetProxies("proxies")
- clash := provider.Clash{Proxies: proxies}
- text = clash.Provide()
- cache.SetString("clashproxies", text)
- }
- } else if proxyTypes == "all" {
- proxies := cache.GetProxies("allproxies")
- clash := provider.Clash{Proxies: proxies, Types: proxyTypes, Country: proxyCountry}
- text = clash.Provide()
- } else {
- proxies := cache.GetProxies("proxies")
- clash := provider.Clash{Proxies: proxies, Types: proxyTypes, Country: proxyCountry}
- text = clash.Provide()
- }
- c.String(200, text)
- })
- router.GET("/surge/proxies", func(c *gin.Context) {
- text := cache.GetString("surgeproxies")
- if text == "" {
- proxies := cache.GetProxies("proxies")
- surge := provider.Surge{Proxies: proxies}
- text = surge.Provide()
- cache.SetString("surgeproxies", text)
- }
- c.String(200, text)
- })
- }
- func Run() {
- setupRouter()
- port := os.Getenv("PORT")
- if port == "" {
- port = "8080"
- }
- router.Run(":" + port)
- }
- func loadTemplate() (t *template.Template, err error) {
- t = template.New("")
- for _, fileName := range binhtml.AssetNames() {
- data := binhtml.MustAsset(fileName)
- t, err = t.New(fileName).Parse(string(data))
- if err != nil {
- return nil, err
- }
- }
- return t, nil
- }
|