router.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package api
  2. import (
  3. "net/http"
  4. "os"
  5. "github.com/zu1k/proxypool/config"
  6. "github.com/gin-gonic/gin"
  7. _ "github.com/heroku/x/hmetrics/onload"
  8. "github.com/zu1k/proxypool/internal/cache"
  9. "github.com/zu1k/proxypool/pkg/provider"
  10. )
  11. var router *gin.Engine
  12. func setupRouter() {
  13. router = gin.Default()
  14. router.LoadHTMLGlob("assets/html/*")
  15. router.GET("/", func(c *gin.Context) {
  16. c.HTML(http.StatusOK, "index.html", gin.H{
  17. "domain": config.SourceConfig.Domain,
  18. "all_proxies_count": cache.AllProxiesCount,
  19. "ss_proxies_count": cache.SSProxiesCount,
  20. "ssr_proxies_count": cache.SSRProxiesCount,
  21. "vmess_proxies_count": cache.VmessProxiesCount,
  22. "trojan_proxies_count": cache.TrojanProxiesCount,
  23. "useful_proxies_count": cache.UsefullProxiesCount,
  24. "last_crawl_time": cache.LastCrawlTime,
  25. })
  26. })
  27. router.GET("/clash", func(c *gin.Context) {
  28. c.HTML(http.StatusOK, "clash.html", gin.H{
  29. "domain": config.SourceConfig.Domain,
  30. })
  31. })
  32. router.GET("/surge", func(c *gin.Context) {
  33. c.HTML(http.StatusOK, "surge.html", gin.H{
  34. "domain": config.SourceConfig.Domain,
  35. })
  36. })
  37. router.GET("/clash/config", func(c *gin.Context) {
  38. c.HTML(http.StatusOK, "clash-config.yaml", gin.H{
  39. "domain": config.SourceConfig.Domain,
  40. })
  41. })
  42. router.GET("/surge/config", func(c *gin.Context) {
  43. c.HTML(http.StatusOK, "surge.conf", gin.H{
  44. "domain": config.SourceConfig.Domain,
  45. })
  46. })
  47. router.GET("/clash/proxies", func(c *gin.Context) {
  48. proxyTypes := c.DefaultQuery("type", "")
  49. proxyCountry := c.DefaultQuery("c", "")
  50. text := ""
  51. if proxyTypes == "" && proxyCountry == "" {
  52. text = cache.GetString("clashproxies")
  53. if text == "" {
  54. proxies := cache.GetProxies("proxies")
  55. clash := provider.Clash{Proxies: proxies}
  56. text = clash.Provide()
  57. cache.SetString("clashproxies", text)
  58. }
  59. } else if proxyTypes == "all" {
  60. proxies := cache.GetProxies("allproxies")
  61. clash := provider.Clash{Proxies: proxies, Types: proxyTypes, Country: proxyCountry}
  62. text = clash.Provide()
  63. } else {
  64. proxies := cache.GetProxies("proxies")
  65. clash := provider.Clash{Proxies: proxies, Types: proxyTypes, Country: proxyCountry}
  66. text = clash.Provide()
  67. }
  68. c.String(200, text)
  69. })
  70. router.GET("/surge/proxies", func(c *gin.Context) {
  71. text := cache.GetString("surgeproxies")
  72. if text == "" {
  73. proxies := cache.GetProxies("proxies")
  74. surge := provider.Surge{Proxies: proxies}
  75. text = surge.Provide()
  76. cache.SetString("surgeproxies", text)
  77. }
  78. c.String(200, text)
  79. })
  80. }
  81. func Run() {
  82. setupRouter()
  83. port := os.Getenv("PORT")
  84. if port == "" {
  85. port = "8080"
  86. }
  87. router.Run(":" + port)
  88. }