Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f1729f129a | ||
|
|
f61302d25f | ||
|
|
b9d931da68 | ||
|
|
2623502cc8 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
debug.test
|
||||||
23
callback.go
Normal file
23
callback.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package cwclient
|
||||||
|
|
||||||
|
import "encoding/json"
|
||||||
|
|
||||||
|
// Callback 发送代理连接获取内容后的回调函数
|
||||||
|
type Callback struct {
|
||||||
|
label string
|
||||||
|
hash string
|
||||||
|
Do func(cxt *CallbackContext)
|
||||||
|
}
|
||||||
|
|
||||||
|
// CallbackContext Callback上下文
|
||||||
|
type CallbackContext struct {
|
||||||
|
TaskID string
|
||||||
|
Content string
|
||||||
|
Error error
|
||||||
|
Carry interface{} // 传递的参数.
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContentJSON 返回 json反序列化的对象
|
||||||
|
func (cxt *CallbackContext) ContentJSON(obj interface{}) error {
|
||||||
|
return json.Unmarshal([]byte(cxt.Content), obj)
|
||||||
|
}
|
||||||
65
client.go
65
client.go
@@ -9,6 +9,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -20,21 +21,6 @@ func init() {
|
|||||||
log.SetFlags(log.Llongfile | log.LstdFlags)
|
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(cxt *CallbackContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Client 客户端
|
// Client 客户端
|
||||||
type Client struct {
|
type Client struct {
|
||||||
chromeProxyAddr string
|
chromeProxyAddr string
|
||||||
@@ -54,12 +40,36 @@ type Client struct {
|
|||||||
type Label struct {
|
type Label struct {
|
||||||
label string
|
label string
|
||||||
|
|
||||||
conditionJS string
|
conditionJS string
|
||||||
conditionlock sync.Mutex
|
retry string
|
||||||
|
waitcapture string
|
||||||
|
configlock sync.Mutex
|
||||||
|
|
||||||
cli *Client
|
cli *Client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetWaitime Get return waitime int
|
||||||
|
func (l *Label) GetWaitime() int {
|
||||||
|
r, _ := strconv.Atoi(l.waitcapture)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetWaitime Set waitime int
|
||||||
|
func (l *Label) SetWaitime(waitime int) {
|
||||||
|
l.waitcapture = strconv.Itoa(waitime)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetRetry Get return retry int
|
||||||
|
func (l *Label) GetRetry() int {
|
||||||
|
r, _ := strconv.Atoi(l.retry)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetRetry Set retry int
|
||||||
|
func (l *Label) SetRetry(retry int) {
|
||||||
|
l.retry = strconv.Itoa(retry)
|
||||||
|
}
|
||||||
|
|
||||||
// GetHash 根据label获取hash路径
|
// GetHash 根据label获取hash路径
|
||||||
func (l *Label) GetHash(label string) string {
|
func (l *Label) GetHash(label string) string {
|
||||||
if cb, ok := l.cli.register.Load(label); ok {
|
if cb, ok := l.cli.register.Load(label); ok {
|
||||||
@@ -83,8 +93,8 @@ func (l *Label) Open(urlstr string, carray interface{}) (bodyRes string, ok bool
|
|||||||
|
|
||||||
// SetContentCondition 设置识别到的内容条件. js代码. 必须是一个函数. 命名可以随意. 返回bool
|
// SetContentCondition 设置识别到的内容条件. js代码. 必须是一个函数. 命名可以随意. 返回bool
|
||||||
func (l *Label) SetContentCondition(jsScript string) {
|
func (l *Label) SetContentCondition(jsScript string) {
|
||||||
l.conditionlock.Lock()
|
l.configlock.Lock()
|
||||||
defer l.conditionlock.Unlock()
|
defer l.configlock.Unlock()
|
||||||
l.conditionJS = jsScript
|
l.conditionJS = jsScript
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,8 +108,8 @@ func (l *Label) SetContentConditionFromFile(jsScriptFile string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
l.conditionlock.Lock()
|
l.configlock.Lock()
|
||||||
defer l.conditionlock.Unlock()
|
defer l.configlock.Unlock()
|
||||||
// log.Println(string(data))
|
// log.Println(string(data))
|
||||||
l.conditionJS = string(data)
|
l.conditionJS = string(data)
|
||||||
}
|
}
|
||||||
@@ -132,7 +142,7 @@ func (cli *Client) Register(label string, callback func(cxt *CallbackContext)) *
|
|||||||
}
|
}
|
||||||
cli.register.Store(label, cb)
|
cli.register.Store(label, cb)
|
||||||
cli.register.Store(cb.hash, cb)
|
cli.register.Store(cb.hash, cb)
|
||||||
l := &Label{label: label, cli: cli}
|
l := &Label{label: label, cli: cli, waitcapture: "6000", retry: "1"}
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -158,7 +168,10 @@ func (cli *Client) Connect() {
|
|||||||
}
|
}
|
||||||
cli.port = fmt.Sprintf("%d", listener.Addr().(*net.TCPAddr).Port)
|
cli.port = fmt.Sprintf("%d", listener.Addr().(*net.TCPAddr).Port)
|
||||||
|
|
||||||
router := gin.Default()
|
router := gin.New()
|
||||||
|
router.Use(gin.Recovery())
|
||||||
|
gin.SetMode(gin.ReleaseMode)
|
||||||
|
|
||||||
router.POST("/:label", func(c *gin.Context) {
|
router.POST("/:label", func(c *gin.Context) {
|
||||||
label := c.Param("label")
|
label := c.Param("label")
|
||||||
if f, ok := cli.register.Load(label); ok {
|
if f, ok := cli.register.Load(label); ok {
|
||||||
@@ -239,9 +252,11 @@ func (cli *Client) open(label *Label, urlstr string, carray interface{}) (bodyRe
|
|||||||
}
|
}
|
||||||
|
|
||||||
func() {
|
func() {
|
||||||
label.conditionlock.Lock()
|
label.configlock.Lock()
|
||||||
defer label.conditionlock.Unlock()
|
defer label.configlock.Unlock()
|
||||||
data["content_condition"] = []string{label.conditionJS}
|
data["content_condition"] = []string{label.conditionJS}
|
||||||
|
data["waitcapture"] = []string{label.waitcapture}
|
||||||
|
data["retry"] = []string{label.retry}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
resp, err := http.DefaultClient.PostForm(cli.chromeProxyAddr+"/task/put", data)
|
resp, err := http.DefaultClient.PostForm(cli.chromeProxyAddr+"/task/put", data)
|
||||||
|
|||||||
@@ -5,8 +5,30 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestSelectChan(t *testing.T) {
|
||||||
|
var a chan string = make(chan string)
|
||||||
|
var b chan string = make(chan string)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
time.Sleep(time.Second * 2)
|
||||||
|
a <- "1"
|
||||||
|
time.Sleep(time.Second * 2)
|
||||||
|
b <- "2"
|
||||||
|
}()
|
||||||
|
|
||||||
|
select {
|
||||||
|
case s := <-a:
|
||||||
|
log.Println("a", s)
|
||||||
|
case s := <-b:
|
||||||
|
log.Println("b", s)
|
||||||
|
case <-time.After(time.Second * 5):
|
||||||
|
log.Println("timeout 5s")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestPort(t *testing.T) {
|
func TestPort(t *testing.T) {
|
||||||
cli := New("http://localhost:7123")
|
cli := New("http://localhost:7123")
|
||||||
|
|
||||||
@@ -20,7 +42,7 @@ func TestPort(t *testing.T) {
|
|||||||
|
|
||||||
ltest.SetContentConditionFromFile("example.js")
|
ltest.SetContentConditionFromFile("example.js")
|
||||||
cli.Connect()
|
cli.Connect()
|
||||||
// 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/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/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/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/yanngu?lang=en&deviceType=browser"))
|
||||||
|
|||||||
Reference in New Issue
Block a user