add: hash路径

This commit is contained in:
eson 2020-11-20 19:15:13 +08:00
parent f5720a36f7
commit ad5fcb4e53
3 changed files with 42 additions and 11 deletions

View File

@ -11,10 +11,15 @@ import (
"sync"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
// Callback 发送代理连接获取内容后的回调函数
type Callback func(tid, content string)
type Callback struct {
label string
hash string
Do func(tid, content string)
}
// Client 客户端
type Client struct {
@ -36,9 +41,25 @@ type Label struct {
cli *Client
}
// GetHash 根据label获取hash路径
func (l *Label) GetHash(label string) string {
if cb, ok := l.cli.register.Load(label); ok {
return cb.(Callback).hash
}
return ""
}
// GetLabel 根据hash获取路径label
func (l *Label) GetLabel(hash string) string {
if cb, ok := l.cli.register.Load(hash); ok {
return cb.(Callback).label
}
return ""
}
// Open 缓存了Label值. 每次调用少了label传参
func (l *Label) Open(urlstr string) (bodyRes string, ok bool) {
return l.cli.open(l.label, urlstr)
return l.cli.Open(l.label, urlstr)
}
// GetPort Get return port string. default random.
@ -62,15 +83,23 @@ func (cli *Client) SetHost(host string) {
}
// Register 注册基础信息
func (cli *Client) Register(label string, callback Callback) *Label {
cli.register.Store(label, callback)
func (cli *Client) Register(label string, callback func(tid, content string)) *Label {
cb := Callback{Do: callback, hash: uuid.New().String()}
if _, ok := cli.register.Load(label); ok {
log.Panic("label: ", label, " is exists")
}
cli.register.Store(label, cb)
cli.register.Store(cb.hash, cb)
l := &Label{label: label, cli: cli}
return l
}
// UnRegister 卸载注册基础信息
func (cli *Client) UnRegister(label string) {
cli.register.Delete(label)
if cb, ok := cli.register.Load(label); ok {
cli.register.Delete(label)
cli.register.Delete(cb.(Callback).hash)
}
}
// Connect 连接初始化回调端口
@ -94,7 +123,7 @@ func (cli *Client) Connect() {
callback := f.(Callback)
if tid, ok := c.GetPostForm("taskid"); ok {
content := c.PostForm("content")
callback(tid, content)
callback.Do(tid, content)
}
}
})
@ -126,19 +155,18 @@ func (cli *Client) Disconnect() {
}
}
// open 请求完url后 调用不同label注册的回调函数
func (cli *Client) open(label, urlstr string) (bodyRes string, ok bool) {
// Open 请求完url后 调用不同label注册的回调函数
func (cli *Client) Open(label, urlstr string) (bodyRes string, ok bool) {
// urlstr = "https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"
if cli.server == nil {
panic("client is not connect. Client.Connect() ? ")
}
if _, ok := cli.register.Load(label); ok {
if callback, ok := cli.register.Load(label); ok {
data := url.Values{}
data["url"] = []string{urlstr}
data["callback"] = []string{cli.host + ":" + cli.port + "/" + label}
data["callback"] = []string{cli.host + ":" + cli.port + "/" + callback.(Callback).hash}
resp, err := http.DefaultClient.PostForm(cli.chromeProxyAddr+"/task/put", data)
if err != nil {
panic(err)

1
go.mod
View File

@ -4,5 +4,6 @@ go 1.15
require (
github.com/gin-gonic/gin v1.6.3
github.com/google/uuid v1.1.2
github.com/tebeka/selenium v0.9.9
)

2
go.sum
View File

@ -42,6 +42,8 @@ github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=