Skip to content

Commit

Permalink
- support check cache
Browse files Browse the repository at this point in the history
-   optimize check?host= as concurrent, send to background
-   support load exists cert firstly when tls enabled
  • Loading branch information
LubyRuffy committed Oct 14, 2022
1 parent 123e6ab commit 9279157
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 20 deletions.
6 changes: 6 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v0.1.6 support cache

- support check cache
- optimize check?host= as concurrent, send to background
- support load exists cert firstly when tls enabled

## v0.1.5 only fetch user's proxies

- only fetch user's proxies
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
- 支持并发限制
- [x] 失败也要记录日志
- [x] 支持延迟参数(以服务器所在地为准,因为后续直接把服务器作为请求代理)
- 支持缓存,一天内不对同一个ip和端口进行多次检查请求
- [x] 支持缓存,一天内不对同一个ip和端口进行多次检查请求
- 代理服务器包含如下一些验证:
- [x] 是否支持http请求代理
- [x] 是否支持https请求代理,很多网站都是https网站,就不能用不支持CONNECT的http代理服务器
Expand All @@ -41,6 +41,8 @@
- [x] 支持转发时删除limit设置
- [x] 支持数据库存储
- [x] 支持sqlite
- [x] 支持tls模式https
- [x] 支持自生成证书,以及加载已经生成的证书

## 测试
- [ ] http代理服务器
Expand Down
17 changes: 14 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

var (
srv *http.Server // http服务器
Version = "v0.1.5"
Version = "v0.1.6"
Prefix = "/api"
authUserKey = "token" // 存在context中的token主键
authUserId = "userId" // 存在context中的token主键
Expand Down Expand Up @@ -224,6 +224,14 @@ func loadRestApi(router *gin.Engine) {
}
return q.Where(query, querySearch...)
}),
gorestful.WithDeleteFunc(func(id interface{}, res *gorestful.Resource, c *gin.Context) error {
err := models.GetDB().Unscoped().Where("proxy_id=? and user_id=?", id, userId(c)).Delete(&models.UserProxy{}).Error
if err != nil {
return fmt.Errorf("delete failed: %v", err)
}

return nil
}),
gorestful.WithUserStruct(func() interface{} {
return &models.Proxy{}
}),
Expand Down Expand Up @@ -294,8 +302,11 @@ func Start(addr string) error {

var err error
if viper.GetBool("tls") {
if err = httpscerts.Generate("cert.pem", "key.pem", ""); err != nil {
panic(err)
err = router.RunTLS(addr, "cert.pem", "key.pem")
if err != nil {
if err = httpscerts.Generate("cert.pem", "key.pem", ""); err != nil {
panic(err)
}
}
err = router.RunTLS(addr, "cert.pem", "key.pem")
} else {
Expand Down
35 changes: 27 additions & 8 deletions api/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/LubyRuffy/myip/ipdb"
"github.com/LubyRuffy/rproxy/models"
"github.com/gin-gonic/gin"
"github.com/patrickmn/go-cache"
"gorm.io/gorm/clause"
"h12.io/socks"
"io/ioutil"
Expand Down Expand Up @@ -75,6 +76,8 @@ var (
}
return false
}

globalCache = cache.New(1*time.Hour, 10*time.Minute) // 全局缓存
)

// TransportFunc takes an address to a proxy server and returns a fully
Expand Down Expand Up @@ -118,6 +121,14 @@ type proxyResult struct {

// checkProtocolHost 第一个返回参数是是否为代理,第二个返回参数是返回的header
func checkProtocolHost(protocol string, host string) *proxyResult {
// 确定最近没有进行测试
id := protocol + "://" + host
if _, found := globalCache.Get(id); found {
return nil
} else {
globalCache.Set(id, true, cache.DefaultExpiration)
}

var err error
defer func() {
errStr := ""
Expand Down Expand Up @@ -208,7 +219,7 @@ func checkHost(host string) *proxyResult {
"https",
//"socks4",
} {
if p := checkProtocolHost(protocol, host); p.Valid {
if p := checkProtocolHost(protocol, host); p != nil && p.Valid {
return p
}
}
Expand Down Expand Up @@ -405,13 +416,21 @@ func checkHandler(c *gin.Context) {
}
} else {
// ?host=1.1.1.1:80
if checkResult = checkHost(host); checkResult == nil || !checkResult.Valid {
c.JSON(200, map[string]interface{}{
"code": 500,
"message": fmt.Sprintf("not valid proxy of host: %s", host),
})
return
}

// 遍历协议时间比较长,都放到后台进行
go func() {
if checkResult = checkHost(host); checkResult == nil || !checkResult.Valid {
return
}
go fillProxyField(checkResult.Url, checkResult, userId(c))
}()

// 直接返回成功提示
c.JSON(200, map[string]interface{}{
"code": 200,
"data": "submit ok, process in the background",
})
return

}
proxyUrl = checkResult.Url
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/LubyRuffy/rproxy
go 1.18

require (
github.com/LubyRuffy/gorestful v0.0.0-20221013111114-b399fdf7b76e
github.com/LubyRuffy/gorestful v0.0.0-20221014061004-7e3441c69ae5
github.com/LubyRuffy/myip v0.0.0-20220808131427-cd1832923c09
github.com/elazarl/goproxy v0.0.0-20220529153421-8ea89ba92021
github.com/gin-contrib/pprof v1.4.0
Expand All @@ -13,6 +13,7 @@ require (
github.com/jinzhu/gorm v1.9.16
github.com/kabukky/httpscerts v0.0.0-20150320125433-617593d7dcb3
github.com/mitchellh/mapstructure v1.5.0
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.12.0
github.com/stretchr/testify v1.8.0
Expand Down Expand Up @@ -52,7 +53,7 @@ require (
github.com/ugorji/go/codec v1.2.7 // indirect
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a // indirect
golang.org/x/net v0.0.0-20221012135044-0b7e1fb9d458 // indirect
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 // indirect
golang.org/x/text v0.3.8 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/LubyRuffy/gorestful v0.0.0-20221013111114-b399fdf7b76e h1:ilFOnFFLDQUrUZWzCAA/rDCl2ZUQSE7OrjbY8dw1eAg=
github.com/LubyRuffy/gorestful v0.0.0-20221013111114-b399fdf7b76e/go.mod h1:4h7u9GJv8xLmhi97PtpyvT8wyfnSunC1alLXERK9CrU=
github.com/LubyRuffy/gorestful v0.0.0-20221014061004-7e3441c69ae5 h1:GNWzrYPtEsD5/qZyTt4MjqZGRF1ychnR1fkFhdl6P5k=
github.com/LubyRuffy/gorestful v0.0.0-20221014061004-7e3441c69ae5/go.mod h1:4h7u9GJv8xLmhi97PtpyvT8wyfnSunC1alLXERK9CrU=
github.com/LubyRuffy/myip v0.0.0-20220808131427-cd1832923c09 h1:JJP4NZjc+n9D1ShFjaW9RpmWhGm96IhKTslfWaG8rXA=
github.com/LubyRuffy/myip v0.0.0-20220808131427-cd1832923c09/go.mod h1:X3eM4eALpb5kG7q3ZWMBlUkuE8BYSjcI6T/Xor2y4so=
github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
Expand Down Expand Up @@ -223,6 +223,8 @@ github.com/oschwald/geoip2-golang v1.8.0 h1:KfjYB8ojCEn/QLqsDU0AzrJ3R5Qa9vFlx3z6
github.com/oschwald/geoip2-golang v1.8.0/go.mod h1:R7bRvYjOeaoenAp9sKRS8GX5bJWcZ0laWO5+DauEktw=
github.com/oschwald/maxminddb-golang v1.10.0 h1:Xp1u0ZhqkSuopaKmk1WwHtjF0H9Hd9181uj2MQ5Vndg=
github.com/oschwald/maxminddb-golang v1.10.0/go.mod h1:Y2ELenReaLAZ0b400URyGwvYxHV1dLIxBuyOsyYjHK0=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pelletier/go-toml/v2 v2.0.1/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo=
Expand Down Expand Up @@ -420,8 +422,8 @@ golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20211007075335-d3039528d8ac/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14 h1:k5II8e6QD8mITdi+okbbmR/cIyEbeXLBhy5Ha4nevyc=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43 h1:OK7RB6t2WQX54srQQYSXMW8dF5C6/8+oA/s5QBmmto4=
golang.org/x/sys v0.0.0-20221013171732-95e765b1cc43/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func main() {
log.Println("load config from file:", viper.ConfigFileUsed())
}

// 连接数据库
_, err := models.SetupDB(fmt.Sprintf("%s?cache=shared&_journal_mode=WAL&mode=rwc&_busy_timeout=9999999", viper.GetString("dbfile")))
// 连接数据库, cache=shared&_journal_mode=WAL&mode=rwc&_busy_timeout=9999999
_, err := models.SetupDB(fmt.Sprintf("%s?cache=shared&mode=rwc&_pragma=journal_mode(WAL)&_pragma=cache(shared)&_pragma=mode(rwc)&_pragma=busy_timeout(9999999)", viper.GetString("dbfile")))
if err != nil {
log.Println("connect db failed:", err)
}
Expand Down

0 comments on commit 9279157

Please sign in to comment.