123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package warning
- import (
- "bytes"
- "crypto/tls"
- "encoding/json"
- "gd_crontab/common.in/config"
- "gd_crontab/errors"
- "github.com/tidwall/gjson"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- )
- type HttpRequestWithHeadCommon struct {
- Url string
- Method string
- Head map[string]string
- TimeOut time.Duration
- Query map[string]string
- Body []byte
- }
- func joinGetRequestArgsCommon(data map[string]string) string {
- var path string
- if data != nil {
- path += "?"
- for k, v := range data {
- path += k + "=" + v + "&"
- }
- path = strings.TrimRight(path, "&")
- return path
- }
- return path
- }
- func (h HttpRequestWithHeadCommon) Request() ([]byte, error) {
- if len(h.Query) > 0 {
- h.Url = h.Url + joinGetRequestArgsCommon(h.Query)
- }
- request, err := http.NewRequest(h.Method, h.Url, bytes.NewReader(h.Body))
- for k, v := range h.Head {
- request.Header.Add(k, v)
- }
- //跳过证书验证
- tr := &http.Transport{
- TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
- }
- client := http.Client{
- Transport: tr,
- Timeout: h.TimeOut,
- }
- resp, err := client.Do(request)
- if err != nil {
- return nil, errors.VendorError
- }
- defer resp.Body.Close()
- if resp.StatusCode != http.StatusOK {
- return nil, errors.VendorError
- }
- contents, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return nil, errors.VendorError
- }
- return contents, nil
- }
- func RobotMsg(content string) (err error) {
- body := map[string]interface{}{
- "msgtype": "text",
- "text": map[string]interface{}{"content": content},
- }
- bytes, _ := json.Marshal(body)
- h := HttpRequestWithHeadCommon{
- Method: "POST",
- Url: config.Conf.ThirdPart.WarnWebhook,
- Body: bytes,
- TimeOut: 10 * time.Second,
- }
- bytes, err = h.Request()
- if err != nil {
- return errors.VendorError
- }
- errcode := gjson.GetBytes(bytes, "errcode").Int()
- //errmsg := gjson.GetBytes(bytes, "errmsg").String()
- if errcode != 0 {
- return errors.VendorError
- }
- return nil
- }
|