TODO: 当遇到cloudflare页面时候错误.
This commit is contained in:
parent
cc8f912c82
commit
3275ec2609
45
carray.go
Normal file
45
carray.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package cwclient
|
||||
|
||||
import "time"
|
||||
|
||||
// Carray 每次url请求都会携带一个值. 可以为nil
|
||||
type Carray struct {
|
||||
data interface{} // 携带的数据. 每次请求唯一.
|
||||
hash string // 每次请求唯一hash. 标识.
|
||||
expire time.Time // 过期时间
|
||||
}
|
||||
|
||||
// GetExpire Get return expire time.Time
|
||||
func (undefined *Carray) GetExpire() time.Time {
|
||||
return undefined.expire
|
||||
}
|
||||
|
||||
// SetExpire Set expire time.Time
|
||||
func (undefined *Carray) SetExpire(expire time.Time) {
|
||||
undefined.expire = expire
|
||||
}
|
||||
|
||||
// GetHash Get return hash string
|
||||
func (undefined *Carray) GetHash() string {
|
||||
return undefined.hash
|
||||
}
|
||||
|
||||
// SetHash Set hash string
|
||||
func (undefined *Carray) SetHash(hash string) {
|
||||
undefined.hash = hash
|
||||
}
|
||||
|
||||
// GetData Get return data interface{}
|
||||
func (undefined *Carray) GetData() interface{} {
|
||||
return undefined.data
|
||||
}
|
||||
|
||||
// SetData Set data interface{}
|
||||
func (undefined *Carray) SetData(data interface{}) {
|
||||
undefined.data = data
|
||||
}
|
||||
|
||||
// Timeup 判断是否过期,超时
|
||||
func (undefined *Carray) Timeup() bool {
|
||||
return time.Now().Before(undefined.expire)
|
||||
}
|
48
client.go
48
client.go
|
@ -10,6 +10,7 @@ import (
|
|||
"net/url"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
@ -19,18 +20,27 @@ func init() {
|
|||
log.SetFlags(log.Llongfile | log.LstdFlags)
|
||||
}
|
||||
|
||||
// CallbackContext Callback上下文
|
||||
type CallbackContext struct {
|
||||
TaskID string
|
||||
Content string
|
||||
Error error
|
||||
Carry interface{} // 传递的参数.
|
||||
}
|
||||
|
||||
// Callback 发送代理连接获取内容后的回调函数
|
||||
type Callback struct {
|
||||
label string
|
||||
hash string
|
||||
Do func(tid, content string, err error)
|
||||
Do func(cxt *CallbackContext)
|
||||
}
|
||||
|
||||
// Client 客户端
|
||||
type Client struct {
|
||||
chromeProxyAddr string
|
||||
|
||||
register sync.Map
|
||||
carrayCache sync.Map
|
||||
register sync.Map
|
||||
|
||||
host string
|
||||
port string
|
||||
|
@ -66,9 +76,9 @@ func (l *Label) GetLabel(hash string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
// Open 缓存了Label值. 每次调用少了label传参
|
||||
func (l *Label) Open(urlstr string) (bodyRes string, ok bool) {
|
||||
return l.cli.open(l, urlstr)
|
||||
// Open 缓存了Label值. 每次调用少了label传参. carray每次都会给一次请求回调传入参数.
|
||||
func (l *Label) Open(urlstr string, carray interface{}) (bodyRes string, ok bool) {
|
||||
return l.cli.open(l, urlstr, carray)
|
||||
}
|
||||
|
||||
// SetContentCondition 设置识别到的内容条件. js代码. 必须是一个函数. 命名可以随意. 返回bool
|
||||
|
@ -115,7 +125,7 @@ func (cli *Client) SetHost(host string) {
|
|||
}
|
||||
|
||||
// Register 注册基础信息
|
||||
func (cli *Client) Register(label string, callback func(tid, content string, err error)) *Label {
|
||||
func (cli *Client) Register(label string, callback func(cxt *CallbackContext)) *Label {
|
||||
cb := Callback{Do: callback, hash: uuid.New().String()}
|
||||
if _, ok := cli.register.Load(label); ok {
|
||||
log.Panic("label: ", label, " is exists")
|
||||
|
@ -156,11 +166,25 @@ func (cli *Client) Connect() {
|
|||
if tid, ok := c.GetPostForm("taskid"); ok {
|
||||
content := c.PostForm("content")
|
||||
errorStr := c.PostForm("error")
|
||||
carrayhash := c.PostForm("carrayhash")
|
||||
|
||||
var carray interface{}
|
||||
if icarray, ok := cli.carrayCache.Load(carrayhash); ok {
|
||||
carray = icarray.(*Carray).data
|
||||
}
|
||||
|
||||
var err error = nil
|
||||
if errorStr != "" {
|
||||
err = fmt.Errorf(errorStr)
|
||||
}
|
||||
callback.Do(tid, content, err)
|
||||
|
||||
cxt := &CallbackContext{
|
||||
TaskID: tid,
|
||||
Content: content,
|
||||
Error: err,
|
||||
Carry: carray,
|
||||
}
|
||||
callback.Do(cxt)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -193,7 +217,7 @@ func (cli *Client) Disconnect() {
|
|||
}
|
||||
|
||||
// open 请求完url后 调用不同label注册的回调函数. bodyRes 请求后服务器返回的基础信息. 如果不需要debug一般不需要使用.
|
||||
func (cli *Client) open(label *Label, urlstr string) (bodyRes string, ok bool) {
|
||||
func (cli *Client) open(label *Label, urlstr string, carray interface{}) (bodyRes string, ok bool) {
|
||||
// urlstr = "https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"
|
||||
|
||||
if cli.server == nil {
|
||||
|
@ -201,11 +225,19 @@ func (cli *Client) open(label *Label, urlstr string) (bodyRes string, ok bool) {
|
|||
}
|
||||
|
||||
if callback, ok := cli.register.Load(label.label); ok {
|
||||
|
||||
data := url.Values{}
|
||||
data["url"] = []string{urlstr}
|
||||
data["callback"] = []string{cli.host + ":" + cli.port + "/" + callback.(Callback).hash}
|
||||
data["label"] = []string{label.label}
|
||||
|
||||
if carray != nil {
|
||||
carrayhash := uuid.New().String()
|
||||
c := &Carray{hash: carrayhash, data: carray, expire: time.Now().Add(time.Minute * 2)}
|
||||
label.cli.carrayCache.Store(carrayhash, c)
|
||||
data["carrayhash"] = []string{carrayhash}
|
||||
}
|
||||
|
||||
func() {
|
||||
label.conditionlock.Lock()
|
||||
defer label.conditionlock.Unlock()
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cwclient
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
@ -9,32 +10,26 @@ import (
|
|||
func TestPort(t *testing.T) {
|
||||
cli := New("http://localhost:7123")
|
||||
|
||||
ltest := cli.Register("test", func(tid, content string, err error) {
|
||||
if err != nil {
|
||||
log.Println("error:", err)
|
||||
ltest := cli.Register("test", func(cxt *CallbackContext) {
|
||||
if cxt.Error != nil {
|
||||
log.Println("error:", cxt.Error)
|
||||
} else {
|
||||
log.Println(tid, content)
|
||||
log.Println(cxt.TaskID, cxt.Content, cxt.Carry)
|
||||
}
|
||||
|
||||
})
|
||||
ltest.SetContentConditionFromFile("example.js")
|
||||
cli.Connect()
|
||||
log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"))
|
||||
log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/yanngu?lang=en&deviceType=browser"))
|
||||
log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"))
|
||||
log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/yanngu?lang=en&deviceType=browser"))
|
||||
log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser", nil))
|
||||
log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/yanngu?lang=en&deviceType=browser", "213"))
|
||||
// log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"))
|
||||
// log.Println(ltest.Open("https://playerduo.com/api/playerDuo-service-v2/yanngu?lang=en&deviceType=browser"))
|
||||
http.ListenAndServe(":42311", nil)
|
||||
}
|
||||
|
||||
func TestChrome(t *testing.T) {
|
||||
|
||||
{
|
||||
defer func() {
|
||||
log.Println(123)
|
||||
}()
|
||||
log.Println(456)
|
||||
}
|
||||
|
||||
log.Println(321)
|
||||
|
||||
var a = make(map[string]interface{})
|
||||
a["as"] = 123
|
||||
data, _ := json.Marshal(a["as"])
|
||||
log.Println(string(data), a["as"])
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user