From 4f17eb8bf684cf69737175f87c58a0229f61236b Mon Sep 17 00:00:00 2001 From: iuu <2167162990@qq.com> Date: Mon, 5 Aug 2024 12:06:31 +0800 Subject: [PATCH] init --- .idea/.gitignore | 8 +++++ .idea/flash.iml | 9 +++++ .idea/modules.xml | 8 +++++ config.json | 12 +++++++ core/crontab.go | 62 ++++++++++++++++++++++++++++++++++ core/http.go | 85 +++++++++++++++++++++++++++++++++++++++++++++++ core/json.go | 13 ++++++++ go.mod | 5 +++ go.sum | 2 ++ main.go | 72 +++++++++++++++++++++++++++++++++++++++ 10 files changed, 276 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/flash.iml create mode 100644 .idea/modules.xml create mode 100644 config.json create mode 100644 core/crontab.go create mode 100644 core/http.go create mode 100644 core/json.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/flash.iml b/.idea/flash.iml new file mode 100644 index 0000000..5e764c4 --- /dev/null +++ b/.idea/flash.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..edf8693 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/config.json b/config.json new file mode 100644 index 0000000..b35f8cc --- /dev/null +++ b/config.json @@ -0,0 +1,12 @@ +{ + "tokens": [ + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjdXN0b21QYXJhbWV0ZXIiOiJxd2VyMTIzNCIsImV4cCI6MTcyMjgzMDE0NSwidXNlcklkIjo4MDU4NDJ9.n3cXJCJNqEpHWoamv3nG-8rrnRPkD44TOqZamzEDFgU", + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjdXN0b21QYXJhbWV0ZXIiOiJxd2VyMTIzNCIsImV4cCI6MTcyMjgyOTkzOSwidXNlcklkIjoxMDU1NzgwfQ.CpMhGx0uWDOJe2xCeqQ_zDyxkelGTsIoUQs3J0aH0aw", + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjdXN0b21QYXJhbWV0ZXIiOiJxd2VyMTIzNCIsImV4cCI6MTcyMjgyOTc3MiwidXNlcklkIjo3OTg2NTJ9.9aTj_LiugdKptbErvGoeLYBvg8x9HZLl6fUWPrKFJ6o", + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjdXN0b21QYXJhbWV0ZXIiOiJxd2VyMTIzNCIsImV4cCI6MTcyMzQzNDg3MywidXNlcklkIjozNTk4NDl9.5J0mQzAXIDW6uWQ9gUdbEN26y9Tx9WpU30pAZrwrqqk" + ], + "activityId": 748226, + "totalCost": 23.5, + "groupId": 1251763 +} + diff --git a/core/crontab.go b/core/crontab.go new file mode 100644 index 0000000..8ece6db --- /dev/null +++ b/core/crontab.go @@ -0,0 +1,62 @@ +package core + +import ( + "fmt" + "github.com/robfig/cron" +) + +var CronInfo CronServiceInfo + +type CronServiceInfo struct { + Cron []string + Func []func() +} + +func (c *CronServiceInfo) Register(cron string, f func()) { + c.Cron = append(c.Cron, cron) + c.Func = append(c.Func, f) +} + +func CronRun() { + c := cron.New() + for k, v := range CronInfo.Cron { + err := c.AddFunc(v, wrap(CronInfo.Func[k])) + if err != nil { + fmt.Println("定时任务添加失败") + } + } + c.Start() +} + +func wrap(f func()) func() { + return func() { + //defer CronRecovery(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()) + f() + } +} + +func CronRegister(rule string, f func()) { + CronInfo.Register(rule, f) +} + +//func CronRecovery(name string){ +// if err := recover(); err != nil { +// var code int +// var msg interface{} +// if h, ok := err.(*Exception); ok { +// code = h.Code +// msg = h.Msg +// } else if _, ok = err.(error); ok { +// msg = "未知错误" +// code = UNKNOW_ERROR +// } else { +// msg = "服务器错误" +// code = SERVER_ERROR +// } +// Log.WithFields(map[string]interface{}{ +// "code":code, +// "model":"task", +// "func":name, +// }).Error(msg) +// } +//} diff --git a/core/http.go b/core/http.go new file mode 100644 index 0000000..cb7acc6 --- /dev/null +++ b/core/http.go @@ -0,0 +1,85 @@ +package core + +import ( + "bytes" + "compress/gzip" + "encoding/json" + "fmt" + "io" + "net/http" + "time" +) + +var ApiUrl = "https://www.shuote.top:8082/flash/activity/applyActivity" +var Headers = map[string]string{ + "Host": "www.shuote.top:8082", + "Connection": "keep-alive", + "Request-Channel": "MP-WEIXIN", + "X-Access-Token": "", + "Accept-Encoding": "gzip,compress,br,deflate", + "User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.49(0x18003137) NetType/WIFI Language/zh_CN", + "Referer": "https://servicewechat.com/wxbb50595cab69d719/403/page-frame.html", +} + +// PostStruct 结构 +type PostStruct struct { + PayAmount float64 `json:"payAmount"` + ActivityID int `json:"activityId"` + MaleCount int `json:"maleCount"` + FemaleCount int `json:"femaleCount"` + PayType int `json:"payType"` + GroupID int `json:"groupId"` + XbSex any `json:"xbSex"` + CardType int `json:"cardType"` +} + +func ApiRequest(url string, customHeaders map[string]string, postData *PostStruct) (string, error) { + if url == "" { + return "", fmt.Errorf("URL is empty") + } + jsonData, err := json.Marshal(postData) + if err != nil { + return "", err + } + client := &http.Client{ + Timeout: 30 * time.Second, + } + req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) + if err != nil { + return "", err + } + // 设置默认请求头 + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Content-Length", fmt.Sprintf("%d", len(jsonData))) + + // 如果有自定义请求头,则设置请求头 + for key, value := range customHeaders { + req.Header.Set(key, value) + } + + // 设置Accept-Encoding以接收压缩响应 + req.Header.Set("Accept-Encoding", "gzip") + + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + + var reader io.ReadCloser + switch resp.Header.Get("Content-Encoding") { + case "gzip": + reader, err = gzip.NewReader(resp.Body) + if err != nil { + return "", err + } + defer reader.Close() + default: + reader = resp.Body + } + responseBytes, err := io.ReadAll(reader) + if err != nil { + return "", err + } + return string(responseBytes), nil +} diff --git a/core/json.go b/core/json.go new file mode 100644 index 0000000..2ff7ca5 --- /dev/null +++ b/core/json.go @@ -0,0 +1,13 @@ +package core + +import ( + "encoding/json" +) + +func JsonToString(postData interface{}) string { + jsons, errs := json.Marshal(postData) //转换成JSON返回的是byte[] + if errs != nil { + return "" + } + return string(jsons) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..57b9c9a --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module flash + +go 1.22 + +require github.com/robfig/cron v1.2.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b5b4795 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ= +github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k= diff --git a/main.go b/main.go new file mode 100644 index 0000000..7baa23c --- /dev/null +++ b/main.go @@ -0,0 +1,72 @@ +package main + +import ( + "encoding/json" + "flash/core" + "fmt" + "math/rand" + "os" + "sync" + "time" +) + +// Conf 配置文件 +type Conf struct { + Tokens []string `json:"tokens"` + ActivityId int `json:"activityId"` + TotalCost float64 `json:"totalCost"` + GroupId int `json:"groupId"` +} + +var conf Conf + +var wg sync.WaitGroup + +//var queue = make(chan *app.PushData) +//var resultCh = make(chan *app.Result) + +func Task() { + for _, token := range conf.Tokens { + wg.Add(1) + go func(token string) { + defer wg.Done() + delay := time.Duration(rand.Intn(2)+5) * time.Second // 随机延迟2到3秒 + time.Sleep(delay) + postData := &core.PostStruct{ + PayAmount: conf.TotalCost, + ActivityID: conf.ActivityId, + MaleCount: 0, + FemaleCount: 0, + PayType: 3, + GroupID: conf.GroupId, + XbSex: nil, + CardType: 0, + } + core.Headers["X-Access-Token"] = token + fmt.Println(core.JsonToString(postData), token) + s, e := core.ApiRequest(core.ApiUrl, core.Headers, postData) + fmt.Println(s, e) + }(token) + } + wg.Wait() +} +func main() { + + p := "config.json" + f, err := os.ReadFile(p) + if err != nil { + fmt.Printf("载入配置文件出错, 错误: %v", err) + os.Exit(-1) + } + err = json.Unmarshal(f, &conf) + if nil != err { + fmt.Printf("解析配置文件内容出错, 错误: %v", err) + os.Exit(-1) + } + rand.New(rand.NewSource(time.Now().UnixNano())) + Task() + //core.CronRegister("0 12 * * * ?", Task) + core.CronRun() + select {} + +}