This commit is contained in:
zu1k
2020-08-23 00:04:31 +08:00
parent 03604fa900
commit dfa76a0255
8 changed files with 71 additions and 129 deletions

View File

@@ -20,7 +20,7 @@ func setupRouter() {
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"domain": config.SourceConfig.Domain,
"domain": config.Config.Domain,
"all_proxies_count": cache.AllProxiesCount,
"ss_proxies_count": cache.SSProxiesCount,
"ssr_proxies_count": cache.SSRProxiesCount,
@@ -33,25 +33,25 @@ func setupRouter() {
router.GET("/clash", func(c *gin.Context) {
c.HTML(http.StatusOK, "clash.html", gin.H{
"domain": config.SourceConfig.Domain,
"domain": config.Config.Domain,
})
})
router.GET("/surge", func(c *gin.Context) {
c.HTML(http.StatusOK, "surge.html", gin.H{
"domain": config.SourceConfig.Domain,
"domain": config.Config.Domain,
})
})
router.GET("/clash/config", func(c *gin.Context) {
c.HTML(http.StatusOK, "clash-config.yaml", gin.H{
"domain": config.SourceConfig.Domain,
"domain": config.Config.Domain,
})
})
router.GET("/surge/config", func(c *gin.Context) {
c.HTML(http.StatusOK, "surge.conf", gin.H{
"domain": config.SourceConfig.Domain,
"domain": config.Config.Domain,
})
})

View File

@@ -1,66 +1,76 @@
package config
import (
"fmt"
"errors"
"io/ioutil"
"os"
"strings"
"github.com/ghodss/yaml"
"github.com/zu1k/proxypool/pkg/tool"
)
var (
NeedFetch = true
Url = "source.yaml"
)
var configFilePath = "config.yaml"
type Source struct {
Type string `json:"type" yaml:"type"`
Options tool.Options `json:"options" yaml:"options"`
}
type Config struct {
type ConfigOptions struct {
Domain string `json:"domain" yaml:"domain"`
CFEmail string `json:"cf_email" yaml:"cf_email"`
CFKey string `json:"cf_key" yaml:"cf_key"`
Sources []Source `json:"sources" yaml:"sources"`
}
var SourceConfig = Config{}
// Config 配置
var Config ConfigOptions
func Parse(path string) (*Config, error) {
fileData, err := readFile(path)
if err != nil {
return nil, err
// Parse 解析配置文件,支持本地文件系统和网络链接
func Parse(path string) error {
if path == "" {
path = configFilePath
} else {
configFilePath = path
}
err = yaml.Unmarshal(fileData, &SourceConfig)
fileData, err := readConfigFile(path)
if err != nil {
return nil, err
return err
}
Config = ConfigOptions{}
err = yaml.Unmarshal(fileData, &Config)
if err != nil {
return err
}
// 部分配置环境变量优先
if domain := os.Getenv("DOMAIN"); domain != "" {
SourceConfig.Domain = domain
Config.Domain = domain
}
if cfEmail := os.Getenv("CF_API_EMAIL"); cfEmail != "" {
SourceConfig.CFEmail = cfEmail
Config.CFEmail = cfEmail
}
if cfKey := os.Getenv("CF_API_KEY"); cfKey != "" {
SourceConfig.CFKey = cfKey
Config.CFKey = cfKey
}
return &SourceConfig, nil
return nil
}
func readFile(path string) ([]byte, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, err
// 从本地文件或者http链接读取配置文件内容
func readConfigFile(path string) ([]byte, error) {
if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
resp, err := tool.GetHttpClient().Get(path)
if err != nil {
return nil, errors.New("config file http get fail")
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
} else {
if _, err := os.Stat(path); os.IsNotExist(err) {
return nil, err
}
return ioutil.ReadFile(path)
}
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, fmt.Errorf("Configuration file %s is empty", path)
}
return data, err
}

View File

@@ -11,18 +11,19 @@ import (
var Getters = make([]getter.Getter, 0)
func InitConfigAndGetters(path string) (err error) {
c, err := config.Parse(path)
err = config.Parse(path)
if err != nil {
return err
return
}
if c == nil {
if s := config.Config.Sources; len(s) == 0 {
return errors.New("no sources")
} else {
initGetters(s)
}
InitGetters(c.Sources)
return nil
return
}
func InitGetters(sources []config.Source) {
func initGetters(sources []config.Source) {
Getters = make([]getter.Getter, 0)
for _, source := range sources {
g, err := getter.NewGetter(source.Type, source.Options)

View File

@@ -1,27 +1,18 @@
package app
import (
"fmt"
"io/ioutil"
"log"
"os"
"sync"
"time"
"github.com/zu1k/proxypool/config"
"github.com/zu1k/proxypool/internal/cache"
"github.com/zu1k/proxypool/pkg/provider"
"github.com/zu1k/proxypool/pkg/proxy"
"github.com/zu1k/proxypool/pkg/tool"
"gopkg.in/yaml.v2"
)
var location, _ = time.LoadLocation("PRC")
func CrawlGo() {
if config.NeedFetch {
FetchNewConfigFileThenInit()
}
wg := &sync.WaitGroup{}
var pc = make(chan proxy.Proxy)
for _, g := range Getters {
@@ -61,31 +52,3 @@ func CrawlGo() {
cache.SetString("clashproxies", provider.Clash{Proxies: proxies}.Provide())
cache.SetString("surgeproxies", provider.Surge{Proxies: proxies}.Provide())
}
func FetchNewConfigFileThenInit() {
fmt.Println("fetch new config file...")
resp, err := tool.GetHttpClient().Get(config.Url)
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
err = yaml.Unmarshal(body, &config.SourceConfig)
if err != nil {
fmt.Errorf("Config file Error: %s\n", err.Error())
return
}
if domain := os.Getenv("DOMAIN"); domain != "" {
config.SourceConfig.Domain = domain
}
if cfEmail := os.Getenv("CF_API_EMAIL"); cfEmail != "" {
config.SourceConfig.CFEmail = cfEmail
}
if cfKey := os.Getenv("CF_API_KEY"); cfKey != "" {
config.SourceConfig.CFKey = cfKey
}
InitGetters(config.SourceConfig.Sources)
}

View File

@@ -9,13 +9,13 @@ import (
)
func test() {
api, err := cloudflare.New(config.SourceConfig.CFKey, config.SourceConfig.CFKey)
api, err := cloudflare.New(config.Config.CFKey, config.Config.CFKey)
if err != nil {
log.Fatal(err)
}
// Fetch the zone ID
id, err := api.ZoneIDByName(config.SourceConfig.Domain)
id, err := api.ZoneIDByName(config.Config.Domain)
if err != nil {
log.Fatal(err)
}

View File

@@ -6,6 +6,11 @@ import (
)
func Cron() {
_ = gocron.Every(15).Minutes().Do(app.CrawlGo)
_ = gocron.Every(15).Minutes().Do(crawlTask)
<-gocron.Start()
}
func crawlTask() {
_ = app.InitConfigAndGetters("")
app.CrawlGo()
}

59
main.go
View File

@@ -3,71 +3,34 @@ package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"strings"
"github.com/zu1k/proxypool/config"
"github.com/zu1k/proxypool/internal/cron"
_ "github.com/mkevac/debugcharts"
"github.com/zu1k/proxypool/api"
"github.com/zu1k/proxypool/internal/app"
"github.com/zu1k/proxypool/internal/cron"
"github.com/zu1k/proxypool/pkg/proxy"
)
var (
debugMode = false
configFilePath = ""
)
var configFilePath = ""
func main() {
flag.StringVar(&configFilePath, "c", "", "path to config file: source.yaml")
flag.BoolVar(&debugMode, "d", false, "debug mode")
flag.StringVar(&configFilePath, "c", "", "path to config file: config.yaml")
flag.Parse()
if debugMode {
go pprof()
if configFilePath == "" {
configFilePath = os.Getenv("CONFIG_FILE")
}
envConfigFilePath := os.Getenv("CONFIG_FILE")
if envConfigFilePath == "" {
envConfigFilePath = "source.yaml"
if configFilePath == "" {
configFilePath = "config.yaml"
}
if configFilePath != "" {
initConfigFile(configFilePath)
} else {
initConfigFile(envConfigFilePath)
err := app.InitConfigAndGetters(configFilePath)
if err != nil {
panic(err)
}
proxy.InitGeoIpDB()
go cron.Cron()
fmt.Println("Do the first crawl...")
go app.CrawlGo()
go cron.Cron()
api.Run()
}
func initConfigFile(path string) {
fmt.Println("Config file:", path)
if strings.HasPrefix(path, "http") {
config.Url = path
config.NeedFetch = true
} else {
err := app.InitConfigAndGetters(configFilePath)
if err != nil {
fmt.Errorf("Config file not found")
os.Exit(2)
}
}
}
func pprof() {
ip := "127.0.0.1:6060"
if err := http.ListenAndServe(ip, nil); err != nil {
fmt.Printf("start pprof failed on %s\n", ip)
}
}