TODO: 测试Callback稳定性.

This commit is contained in:
eson 2020-11-19 19:05:32 +08:00
parent 5864a2645f
commit 119aed60f1
8 changed files with 84 additions and 66 deletions

View File

@ -17,6 +17,7 @@ function GetTask(sender) {
}
})
} else {
// ErrorTask(sender, "error response:" + response.text())
console.log("error response:",response.text())
}
}).catch(function (reason) {
@ -30,7 +31,7 @@ function FinishTask(sender, content) {
var formdata = new FormData();
formdata.append("taskid", task.data.taskid);
formdata.append("content", content);
console.log(task.data.taskid);
fetch(FinishTaskUrl, {method: "POST", body: formdata }).then(function (response) {
if (response.ok) {
response.json().then(function (value) {
@ -42,6 +43,8 @@ function FinishTask(sender, content) {
}
}).catch(function(error){
// TODO: 汇报错误
// ErrorTask(sender, error);
console.log(error)
GetTask(sender);
return;
});
@ -56,16 +59,13 @@ function ErrorTask(sender, error) {
}
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
// console.log(request);
sendResponse({});
// console.log(request, sender); // 利用sender tab id 返回正确的任务id
switch (request.type) {
case BackgroundMsgType.CONTENT:
// 重新获取任务
FinishTask(sender, request.content);
break;
case BackgroundMsgType.ERROR:
// 重新获取任务
ErrorTask(sender, request.error)
GetTask(sender);
@ -97,60 +97,4 @@ chrome.webRequest.onBeforeRequest.addListener(function (details) {
chrome.tabs.remove(details.tabId, function () { });
return { cancel: true };
}
}, { 'urls': ["<all_urls>"] }, ['blocking']);
// chrome.webRequest.onCompleted.addListener(
// function (details) {
// console.log(details);
// console.log(document);
// // return { responseHeaders: details.responseHeaders};
// },
// {
// urls: ["<all_urls>"],
// types: [
// "main_frame",
// "sub_frame",
// "stylesheet",
// "script",
// "image",
// "object",
// "xmlhttprequest",
// "other"
// ]
// },
// ["responseHeaders", "extraHeaders"]
// );
// fetch('https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser').then(r => r.text()).then(result => {
// // Result now contains the response text, do what you want...
// console.log(result);
// })
// chrome.webRequest.onBeforeRequest.addListener(function(data){
// // data contains request_body
// return {redirectUrl: "https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"};
// },{'urls':["<all_urls>"]},['blocking']);
}, { 'urls': ["<all_urls>"] }, ['blocking']);

View File

@ -5,8 +5,6 @@ var GetTaskUrl = `${Host}/task/get`;
var FinishTaskUrl = `${Host}/task/content`;
var ErrorTaskUrl = `${Host}/task/error`;
const TaskIDKey = "chrome_proxy_tid_key";
const BackgroundMsgType = {
NOTWANT: 'notwant',
CONTENT: 'content',

25
proxyserver/callback.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"fmt"
"log"
"github.com/474420502/requests"
)
// CallbackServer 处理回调客户端
func CallbackServer(task *Task) {
task.lock.Lock()
defer task.lock.Unlock()
if callback, ok := task.data["callback"]; ok {
cburl := callback.(string)
log.Println(cburl)
_, err := requests.NewSession().Post(cburl).SetBodyAuto(task.data, requests.TypeFormData).Execute()
if err != nil {
log.Println(err)
task.data["error"] = fmt.Sprintf("callback url error: %s", err)
} else {
task.data["status"] = "readied"
}
}
}

View File

@ -0,0 +1,31 @@
package main
import (
"log"
"testing"
"github.com/474420502/requests"
"github.com/gin-gonic/gin"
)
func TestCallback(t *testing.T) {
eg := gin.New()
eg.POST("/callback", func(c *gin.Context) {
if tid, ok := c.GetPostForm("taskid"); ok {
log.Println(tid)
}
})
data := make(map[string]string)
data["url"] = "https://playerduo.com/api/playerDuo-service-v2/rip113?lang=en&deviceType=browser"
data["callback"] = "http://localhost:4848/callback"
go func() {
resp, err := requests.NewSession().Post("http://localhost:7123/task/put").SetBodyAuto(data, requests.TypeFormData).Execute()
if err != nil {
log.Println(err)
} else {
log.Println(string(resp.Content()))
}
}()
eg.Run(":4848")
}

View File

@ -4,6 +4,7 @@ go 1.15
require (
github.com/474420502/focus v0.12.0
github.com/474420502/requests v1.10.0
github.com/bwmarrin/snowflake v0.3.0
github.com/davecgh/go-spew v1.1.1
github.com/gin-gonic/gin v1.6.3

View File

@ -1,6 +1,10 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/474420502/focus v0.12.0 h1:+icbmj7IEOefvTegHt5EpcHt6WFbe2miIrceUJx2Evo=
github.com/474420502/focus v0.12.0/go.mod h1:d0PMjtMxFz1a9HIhwyFPkWa+JF+0LgOrEUfd8iZka6s=
github.com/474420502/requests v1.9.1 h1:gCDmBCW1ym8zOvKKBGjEG9wafMY7imYk2u28+Oy1WEc=
github.com/474420502/requests v1.9.1/go.mod h1:x2T9l+e40R6kxxMvNm+YSZ9D6BHAXUDak4kQElIPJ9A=
github.com/474420502/requests v1.10.0 h1:Oz7+Nx+1iuJJUjbBH3cVmkDVosF2tyq5d72TMAoQH8I=
github.com/474420502/requests v1.10.0/go.mod h1:SB8/RIUVWF3AGotuq/mATUwAjXzzlV7iWaBNM3+c06c=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Pallinder/go-randomdata v1.1.0/go.mod h1:yHmJgulpD2Nfrm0cR9tI/+oAgRqCQQixsA8HyRZfV9Y=
github.com/bwmarrin/snowflake v0.3.0 h1:xm67bEhkKh6ij1790JB83OujPR5CzNe8QuQqAgISZN0=
@ -10,6 +14,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elazarl/goproxy v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/elazarl/goproxy/ext v0.0.0-20190711103511-473e67f1d7d2/go.mod h1:gNh8nYJoAm43RfaxurUnxr+N1PwuFV3ZMl/efxlIlY8=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
@ -66,10 +72,14 @@ github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3Rllmb
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-charset v0.0.0-20180617210344-2471d30d28b4/go.mod h1:qgYeAmZ5ZIpBWTGllZSQnw97Dj+woV0toclVaRGI8pc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/tidwall/gjson v1.3.2/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/ugorji/go v1.1.7 h1:/68gy2h+1mWMrwZFeD1kQialdSzAb432dtpeJ42ovdo=
github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw=
github.com/ugorji/go v1.2.0 h1:6eXlzYLLwZwXroJx9NyqbYcbv/d93twiOzQLDewE6qM=
@ -92,6 +102,8 @@ golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@ -102,6 +114,7 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42 h1:vEOn+mP2zCOVzKckCZy6YsCtDblrpj/w7B9nxGNELpg=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201118182958-a01c418693c7 h1:Z991aAXPjz0tLnj74pVXW3eWJ5lHMIBvbRfMq4M2jHA=
golang.org/x/sys v0.0.0-20201118182958-a01c418693c7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=

View File

@ -5,6 +5,6 @@ import "github.com/gin-gonic/gin"
var engine = gin.New()
func main() {
engine.Run(":7123")
}

View File

@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/http"
"runtime"
"time"
"github.com/bwmarrin/snowflake"
@ -67,12 +68,14 @@ func PutTask(c *gin.Context) {
u := c.PostForm("url")
if u != "" {
data := NewTask()
now := time.Now()
tid := snowNode.Generate().Base64()
data.Store("taskid", tid)
data.Store("url", u)
data.Store("ts", now.UnixNano())
if callback := c.PostForm("callback"); callback != "" {
data.Store("callback", callback)
}
taskQueue.Push(tid, data)
oplog.Write(data)
c.JSON(http.StatusOK, Response{Code: 200, Message: "ok", Data: data})
@ -85,6 +88,8 @@ func PutTask(c *gin.Context) {
// ContentTask 把一条任务放入队列
func ContentTask(c *gin.Context) {
pc, _, _, _ := runtime.Caller(0)
log.Println(runtime.FuncForPC(pc).Name())
var err error
tid, ok := c.GetPostForm("taskid")
@ -96,10 +101,11 @@ func ContentTask(c *gin.Context) {
if content, ok := c.GetPostForm("content"); ok {
task := iv.(*Task)
task.Store("content", content)
task.Store("is_read", "false")
task.Store("status", "ready")
readyQueue.Push(tid, task) // 进入回调发送队列.TODO: 内容持久化
c.JSON(200, Response{Code: 200, Data: task})
log.Println("start callback")
go CallbackServer(task)
return
}
}