auto fetch new config & add subs

This commit is contained in:
zu1k
2020-08-13 08:18:52 +08:00
parent f0cc329db2
commit 8a2181f822
4 changed files with 49 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ package app
import (
"fmt"
"os"
"github.com/zu1k/proxypool/config"
"github.com/zu1k/proxypool/getter"
@@ -9,7 +10,21 @@ import (
var Getters = make([]getter.Getter, 0)
func InitConfigAndGetters(path string) {
c, err := config.Parse(path)
if err != nil {
fmt.Println("Error: ", err.Error())
os.Exit(1)
}
if c == nil {
fmt.Println("Error: no sources")
os.Exit(2)
}
InitGetters(c.Sources)
}
func InitGetters(sources []config.Source) {
Getters = make([]getter.Getter, 0)
for _, source := range sources {
g := getter.NewGetter(source.Type, source.Options)
if g != nil {

View File

@@ -1,17 +1,25 @@
package app
import (
"io/ioutil"
"log"
"math/rand"
"os"
"strconv"
"sync"
"github.com/zu1k/proxypool/app/cache"
"github.com/zu1k/proxypool/provider"
"github.com/zu1k/proxypool/proxy"
"github.com/zu1k/proxypool/tool"
)
var NeedFetchNewConfigFile = false
func Crawl() {
if NeedFetchNewConfigFile {
FetchNewConfigFileThenInit()
}
proxies := make([]proxy.Proxy, 0)
for _, g := range Getters {
proxies = append(proxies, g.Get()...)
@@ -29,6 +37,9 @@ func Crawl() {
}
func CrawlGo() {
if NeedFetchNewConfigFile {
FetchNewConfigFileThenInit()
}
wg := &sync.WaitGroup{}
var pc = make(chan proxy.Proxy)
for _, g := range Getters {
@@ -55,3 +66,20 @@ func CrawlGo() {
cache.SetProxies(proxies)
cache.SetString("clashproxies", provider.Clash{Proxies: proxies}.Provide())
}
func FetchNewConfigFileThenInit() {
resp, err := tool.GetHttpClient().Get("https://raw.githubusercontent.com/zu1k/proxypool/master/source.yaml")
if err != nil {
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = ioutil.WriteFile("source.yaml", body, os.ModePerm)
if err != nil {
return
}
InitConfigAndGetters("source.yaml")
}

View File

@@ -25,7 +25,7 @@ func Parse(path string) (*Config, error) {
if err != nil {
return nil, err
}
SourceConfig = Config{}
err = yaml.Unmarshal(fileData, &SourceConfig)
if err != nil {
return nil, err

18
main.go
View File

@@ -3,27 +3,19 @@ package main
import (
"flag"
"fmt"
"os"
"github.com/zu1k/proxypool/api"
"github.com/zu1k/proxypool/app"
"github.com/zu1k/proxypool/config"
)
func main() {
filePath := flag.String("c", "source.yaml", "path to config file: source.yaml")
filePath := flag.String("c", "", "path to config file: source.yaml")
flag.Parse()
c, err := config.Parse(*filePath)
if err != nil {
fmt.Println("Error: ", err.Error())
os.Exit(1)
if *filePath == "" {
app.NeedFetchNewConfigFile = true
} else {
app.InitConfigAndGetters(*filePath)
}
if c == nil {
fmt.Println("Error: no sources")
os.Exit(2)
}
app.InitGetters(c.Sources)
go app.Cron()
fmt.Println("Do the first crawl...")
app.CrawlGo()