From c800439fa7cb976794f215b0d8111205d2857525 Mon Sep 17 00:00:00 2001 From: sunxiaolong Date: Sat, 6 Jul 2024 19:46:48 +0800 Subject: [PATCH] =?UTF-8?q?refactorL=20=E9=80=82=E9=85=8D=E6=96=B0api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 ++- main.go | 41 +++++++++++++++++++---------------------- models/model.go | 2 ++ 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 757fee3..e0dc482 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/.idea \ No newline at end of file +/.idea +main.exe \ No newline at end of file diff --git a/main.go b/main.go index 1eeca74..44d1315 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "encoding/hex" "encoding/json" "fmt" - gonanoid "github.com/matoous/go-nanoid/v2" "io" "kami_scripts/models" "math" @@ -15,6 +14,8 @@ import ( "strconv" "sync" "time" + + gonanoid "github.com/matoous/go-nanoid/v2" ) // SortMap 对map的key值进行排序 @@ -28,29 +29,27 @@ func SortMap(m map[string]string) []string { } func GenRandomCardNo() string { - cardNo := "GCA" - //GCA3238734750243 - for i := 0; i < 13; i++ { - cardNo += strconv.Itoa(rand.Intn(10)) - } - return cardNo + return "" + // cardNo := "X" + // for i := 0; i < 15; i++ { + // cardNo += strconv.Itoa(rand.Intn(10)) + // } + // return cardNo } func GenRandomCardPass() string { - cardPass := "" - for i := 0; i < 16; i++ { - cardPass += string(rune(rand.Intn(26) + 65)) + cardPass := "X" + for i := 0; i < 15; i++ { + cardPass += strconv.Itoa(rand.Intn(10)) } return cardPass + // return cardPass } func genSign(data models.Data) string { appSecret := "ssssc9kit6bimggos5kk0c9g" - params := map[string]string{} - exValue, _ := json.Marshal(data.ExValue) - params["exValue"] = string(exValue) params["orderNo"] = data.OrderNo params["orderPeriod"] = data.OrderPeriod @@ -58,8 +57,8 @@ func genSign(data models.Data) string { params["notifyUrl"] = data.NotifyUrl params["timestamp"] = data.TimeStamp params["payKey"] = data.PayKey + params["ip"] = data.Ip params["productCode"] = data.ProductCode - strArr := SortMap(params) signStr := "" for i := 0; i < len(strArr); i++ { @@ -71,13 +70,12 @@ func genSign(data models.Data) string { } } signStr += appSecret - h := md5.New() h.Write([]byte(signStr)) return hex.EncodeToString(h.Sum(nil)) } -func randomRequest(count int, stop chan int, group *sync.WaitGroup) { +func randomRequest(count int, stop chan int, group *sync.WaitGroup, client *http.Client) { defer func() { time.Sleep(time.Second * 2) <-stop @@ -88,8 +86,6 @@ func randomRequest(count int, stop chan int, group *sync.WaitGroup) { }() fmt.Println("正在发送请求次数", count) id, _ := gonanoid.New() - - client := http.Client{} formData := models.Data{ ExValue: models.ExValue{ Data: GenRandomCardPass(), @@ -101,6 +97,7 @@ func randomRequest(count int, stop chan int, group *sync.WaitGroup) { OrderPeriod: strconv.Itoa(rand.Intn(24)), OrderPrice: "100", ProductCode: "8546", + Ip: "127.0.0.1", NotifyUrl: "https://baidu.com", PayKey: "kkkkc9kit6bimggos5kk0c90", TimeStamp: strconv.FormatInt(time.Now().Unix(), 10), @@ -108,7 +105,6 @@ func randomRequest(count int, stop chan int, group *sync.WaitGroup) { } formData.Sign = genSign(formData) res, _ := client.PostForm("http://121.37.253.228:12309/gateway/scan", formData.Url()) - result, _ := io.ReadAll(res.Body) fmt.Println(string(result)) } @@ -121,11 +117,12 @@ func randomRequest(count int, stop chan int, group *sync.WaitGroup) { func main() { group := sync.WaitGroup{} startTime := time.Now() - stop := make(chan int, 100) - for i := 1; i < int(math.Pow10(3)); i++ { + stop := make(chan int, 10) + client := http.Client{} + for i := 1; i < int(math.Pow10(2)); i++ { group.Add(1) stop <- 1 - go randomRequest(i, stop, &group) + go randomRequest(i, stop, &group, &client) } group.Wait() fmt.Println("总耗时", time.Since(startTime).Seconds()) diff --git a/models/model.go b/models/model.go index 14fdfdf..4e3f972 100644 --- a/models/model.go +++ b/models/model.go @@ -15,6 +15,7 @@ type Data struct { PayKey string `json:"PayKey"` TimeStamp string `json:"timestamp"` Sign string `json:"sign"` + Ip string `json:"ip"` } func (d *Data) Url() url.Values { @@ -27,6 +28,7 @@ func (d *Data) Url() url.Values { "productCode": {d.ProductCode}, "payKey": {d.PayKey}, "timestamp": {d.TimeStamp}, + "ip": {d.Ip}, "sign": {d.Sign}, } }