Compare commits

...

6 Commits

Author SHA1 Message Date
zu1k
679c6fa0d4 fetch proxies from db every task & bump version 2020-09-06 09:17:50 +08:00
zu1k
864f8403ac use miner patch size 2020-09-05 22:30:05 +08:00
zu1k
f2275c7bdc manual gc every crawl 2020-09-05 22:16:06 +08:00
zu1k
5b5999834d add debug pprof 2020-09-05 21:08:58 +08:00
zu1k
30e46c6ace mod proxy model 2020-09-05 17:40:07 +08:00
zu1k
53e5dc4011 restore html when load 2020-09-05 12:23:53 +08:00
7 changed files with 64 additions and 21 deletions

View File

@@ -14,7 +14,7 @@ import (
"github.com/zu1k/proxypool/pkg/provider"
)
const version = "v0.3.6"
const version = "v0.3.7"
var router *gin.Engine
@@ -206,6 +206,7 @@ func Run() {
}
func loadTemplate() (t *template.Template, err error) {
_ = binhtml.RestoreAssets("", "assets/html")
t = template.New("")
for _, fileName := range binhtml.AssetNames() {
data := binhtml.MustAsset(fileName)

View File

@@ -5,9 +5,8 @@ import (
"sync"
"time"
"github.com/zu1k/proxypool/internal/database"
"github.com/zu1k/proxypool/internal/cache"
"github.com/zu1k/proxypool/internal/database"
"github.com/zu1k/proxypool/pkg/provider"
"github.com/zu1k/proxypool/pkg/proxy"
)
@@ -22,6 +21,7 @@ func CrawlGo() {
go g.Get2Chan(pc, wg)
}
proxies := cache.GetProxies("allproxies")
proxies = append(proxies, database.GetAllProxies()...)
go func() {
wg.Wait()
close(pc)

View File

@@ -1,6 +1,8 @@
package cron
import (
"runtime"
"github.com/jasonlvhit/gocron"
"github.com/zu1k/proxypool/internal/app"
)
@@ -13,4 +15,6 @@ func Cron() {
func crawlTask() {
_ = app.InitConfigAndGetters("")
app.CrawlGo()
app.Getters = nil
runtime.GC()
}

View File

@@ -8,6 +8,7 @@ import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
var DB *gorm.DB
@@ -20,7 +21,9 @@ func connect() (err error) {
if url := os.Getenv("DATABASE_URL"); url != "" {
dsn = url
}
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err == nil {
fmt.Println("DB connect success: ", DB.Name())
}

View File

@@ -1,15 +1,16 @@
package database
import (
"github.com/zu1k/proxypool/pkg/getter"
"github.com/zu1k/proxypool/pkg/proxy"
"gorm.io/gorm"
)
type Proxy struct {
gorm.Model
proxy.Base `gorm:"index"`
proxy.Base
Link string
Identifier string `gorm:"primaryKey"`
Identifier string `gorm:"unique"`
}
func InitTables() {
@@ -25,14 +26,43 @@ func InitTables() {
}
}
const roundSize = 100
func SaveProxyList(pl proxy.ProxyList) {
proxies := make([]Proxy, pl.Len())
for i, p := range pl {
proxies[i] = Proxy{
Base: *p.BaseInfo(),
Link: p.Link(),
Identifier: p.Identifier(),
if DB == nil {
return
}
size := pl.Len()
round := (size + roundSize - 1) / roundSize
for r := 0; r < round; r++ {
proxies := make([]Proxy, 0, roundSize)
for i, j := r*roundSize, (r+1)*roundSize-1; i < j && i < size; i++ {
p := pl[i]
proxies = append(proxies, Proxy{
Base: *p.BaseInfo(),
Link: p.Link(),
Identifier: p.Identifier(),
})
}
DB.Create(&proxies)
}
}
func GetAllProxies() (proxies proxy.ProxyList) {
proxies = make(proxy.ProxyList, 0)
if DB == nil {
return
}
proxiesDB := make([]Proxy, 0)
DB.Select("link").Find(&proxiesDB)
for _, proxyDB := range proxiesDB {
if proxiesDB != nil {
proxies = append(proxies, getter.String2Proxy(proxyDB.Link))
}
}
DB.Create(&proxies)
return
}

View File

@@ -3,19 +3,24 @@ package main
import (
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"github.com/zu1k/proxypool/internal/database"
"github.com/zu1k/proxypool/api"
"github.com/zu1k/proxypool/internal/app"
"github.com/zu1k/proxypool/internal/cron"
"github.com/zu1k/proxypool/internal/database"
"github.com/zu1k/proxypool/pkg/proxy"
)
var configFilePath = ""
func main() {
go func() {
http.ListenAndServe("0.0.0.0:6060", nil)
}()
flag.StringVar(&configFilePath, "c", "", "path to config file: config.yaml")
flag.Parse()

View File

@@ -1,13 +1,13 @@
package proxy
type Base struct {
Name string `yaml:"name" json:"name"`
Server string `yaml:"server" json:"server"`
Port int `yaml:"port" json:"port"`
Type string `yaml:"type" json:"type"`
Name string `yaml:"name" json:"name" gorm:"index"`
Server string `yaml:"server" json:"server" gorm:"index"`
Port int `yaml:"port" json:"port" gorm:"index"`
Type string `yaml:"type" json:"type" gorm:"index"`
UDP bool `yaml:"udp,omitempty" json:"udp,omitempty"`
Country string `yaml:"country,omitempty" json:"country,omitempty"`
Useable bool `yaml:"useable,omitempty" json:"useable,omitempty"`
Country string `yaml:"country,omitempty" json:"country,omitempty" gorm:"index"`
Useable bool `yaml:"useable,omitempty" json:"useable,omitempty" gorm:"index"`
}
func (b *Base) TypeName() string {