You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
282 lines
6.8 KiB
282 lines
6.8 KiB
package util |
|
|
|
import ( |
|
"bytes" |
|
"crypto/md5" |
|
"crypto/sha256" |
|
"encoding/hex" |
|
"encoding/json" |
|
"errors" |
|
"fmt" |
|
"io/ioutil" |
|
"net/http" |
|
"net/url" |
|
"reflect" |
|
"regexp" |
|
"strconv" |
|
"strings" |
|
"time" |
|
|
|
"github.com/gin-gonic/gin" |
|
"github.com/liangdas/mqant/log" |
|
) |
|
|
|
const ( |
|
key = "HXSQnFRhs1v1cEty" |
|
) |
|
|
|
// UnPackMD5 统一的解析方法 |
|
func UnPackMD5(body []byte) string { |
|
pkg := rawPack{} |
|
if err := json.Unmarshal(body, &pkg); err != nil { |
|
log.Error("umarshal msg fail %v", err) |
|
return "" |
|
} |
|
sign := pkg.Sign |
|
data := pkg.Data |
|
if CalculateMD5(data) != sign { |
|
log.Error("check sign fail") |
|
return "" |
|
} |
|
return data |
|
} |
|
|
|
type rawPack struct { |
|
Sign string |
|
Data string |
|
} |
|
|
|
// CalculateMD5 calculate md5 |
|
func CalculateMD5(data string) string { |
|
h := md5.New() |
|
h.Write([]byte(data)) |
|
return hex.EncodeToString(h.Sum(nil)) |
|
} |
|
|
|
func CalculateSHA256(input string) string { |
|
hasher := sha256.New() |
|
hasher.Write([]byte(input)) |
|
hashBytes := hasher.Sum(nil) |
|
|
|
// Convert the hash bytes to a hexadecimal string |
|
hashString := hex.EncodeToString(hashBytes) |
|
|
|
return hashString |
|
} |
|
|
|
// HttpUseProxy 使用代理请求 |
|
func HttpUseProxy(proxyURL, target string, ret interface{}) error { |
|
uri, err := url.Parse(proxyURL) |
|
if err != nil { |
|
log.Error("parse url error: ", err) |
|
return err |
|
} |
|
client := http.Client{ |
|
Transport: &http.Transport{ |
|
// 设置代理 |
|
Proxy: http.ProxyURL(uri), |
|
}, |
|
Timeout: 5 * time.Second, |
|
} |
|
log.Debug("reqURL:%v", target) |
|
resp, err := client.Get(target) |
|
if err != nil { |
|
log.Error("get err:%v", err) |
|
return err |
|
} |
|
// log.Debug("ret:%v", resp) |
|
if resp.StatusCode != http.StatusOK { |
|
log.Error("req fail err code:%v", resp.StatusCode) |
|
return errors.New("http req fail") |
|
} |
|
defer resp.Body.Close() |
|
data, err := ioutil.ReadAll(resp.Body) |
|
if err != nil { |
|
log.Error("read err %v", err) |
|
return err |
|
} |
|
if err := json.Unmarshal(data, ret); err != nil { |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
// HttpGet Get请求 |
|
func HttpGet(target string, ret interface{}) error { |
|
client := http.Client{ |
|
Timeout: 5 * time.Second, |
|
} |
|
resp, err := client.Get(target) |
|
if err != nil { |
|
log.Error("get err:%v", err) |
|
return err |
|
} |
|
if resp.StatusCode != http.StatusOK { |
|
log.Error("req fail err code:%v", resp.StatusCode) |
|
return errors.New("http req fail") |
|
} |
|
defer resp.Body.Close() |
|
data, err := ioutil.ReadAll(resp.Body) |
|
if err != nil { |
|
log.Error("read err %v", err) |
|
return err |
|
} |
|
log.Debug("resp:%v", string(data)) |
|
if err := json.Unmarshal(data, ret); err != nil { |
|
log.Error("Unmarshal err %v", err) |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
// HttpPost post请求 |
|
func HttpPost(target string, send, ret interface{}, header map[string]string) error { |
|
reqStr, _ := json.Marshal(send) |
|
log.Debug("Post to:%v,req:%v", target, string(reqStr)) |
|
req, err := http.NewRequest("POST", target, bytes.NewBuffer(reqStr)) |
|
if err != nil { |
|
log.Error("err:%v", err) |
|
return err |
|
} |
|
req.Header.Set("Content-Type", "application/json") |
|
for k, v := range header { |
|
req.Header.Set(k, v) |
|
} |
|
// req.Header.Set("Accept", "application/problem+json") |
|
// req.Header.Set("Authorization", "api-key "+config.GetConfig().Web.OrangePay.APISecret) |
|
// req.Header.Set("Authorization", "api-key "+OrangeAPISecret) |
|
|
|
client := &http.Client{ |
|
Timeout: 20 * time.Second, |
|
} |
|
log.Debug("req:%+v", req) |
|
resp, err := client.Do(req) |
|
if err != nil { |
|
log.Error("http post call err:%v", err) |
|
return err |
|
} |
|
// if resp.StatusCode != http.StatusOK { |
|
// log.Error("req fail err code:%v", resp.StatusCode) |
|
// return errors.New("http req fail") |
|
// } |
|
defer resp.Body.Close() |
|
body, _ := ioutil.ReadAll(resp.Body) |
|
log.Debug("response Body:%v", string(body)) |
|
if err := json.Unmarshal(body, ret); err != nil { |
|
log.Error("unmarshal fail err:%v", err) |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
// HttpPostForm post请求 |
|
func HttpPostForm(target string, send, ret interface{}, header map[string]string) error { |
|
log.Debug("send:%+v", send) |
|
uv := url.Values{} |
|
typ := reflect.TypeOf(send).Elem() |
|
val := reflect.ValueOf(send).Elem() |
|
for i := 0; i < typ.NumField(); i++ { |
|
if val.Field(i).IsZero() { |
|
continue |
|
} |
|
uv.Add(typ.Field(i).Tag.Get("json"), fmt.Sprintf("%v", val.Field(i).Interface())) |
|
} |
|
log.Debug("uv:%v", uv) |
|
// ul += fmt.Sprintf("%v=%v", "sign", util.CalculateMD5(oid+strconv.Itoa(apiReq.AdvChannel)+apiReq.ClientDate+apiReq.Money+values.ZYAppKey)) |
|
|
|
req, err := http.NewRequest("POST", target, strings.NewReader(uv.Encode())) |
|
if err != nil { |
|
log.Error("err:%v", err) |
|
return err |
|
} |
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
|
// req.Header.Set("Accept", "application/problem+json") |
|
// req.Header.Set("Authorization", "api-key "+config.GetConfig().Web.OrangePay.APISecret) |
|
// req.Header.Set("Authorization", "api-key "+OrangeAPISecret) |
|
|
|
client := &http.Client{ |
|
Timeout: 5 * time.Second, |
|
} |
|
log.Debug("req:%+v", req) |
|
resp, err := client.Do(req) |
|
if err != nil { |
|
log.Error("http post call err:%v", err) |
|
return err |
|
} |
|
// if resp.StatusCode != http.StatusOK { |
|
// log.Error("req fail err code:%v", resp.StatusCode) |
|
// return errors.New("http req fail") |
|
// } |
|
defer resp.Body.Close() |
|
body, _ := ioutil.ReadAll(resp.Body) |
|
log.Debug("response Body%v:", string(body)) |
|
if err := json.Unmarshal(body, ret); err != nil { |
|
log.Error("unmarshal fail err:%v", err) |
|
return err |
|
} |
|
return nil |
|
} |
|
|
|
func ParseFormReq(va url.Values, req interface{}) { |
|
typ := reflect.TypeOf(req).Elem() |
|
val := reflect.ValueOf(req).Elem() |
|
for i := 0; i < typ.NumField(); i++ { |
|
one := val.Field(i) |
|
value := va.Get(typ.Field(i).Tag.Get("json")) |
|
if value == "" { |
|
continue |
|
} |
|
if one.Kind() == reflect.Int || one.Kind() == reflect.Int64 { |
|
this, err := strconv.Atoi(value) |
|
if err != nil { |
|
log.Error("err:%v", err) |
|
} |
|
one.SetInt(int64(this)) |
|
} else if one.Kind() == reflect.Float32 || one.Kind() == reflect.Float64 { |
|
this, err := strconv.ParseFloat(value, 64) |
|
if err != nil { |
|
log.Error("err:%v", err) |
|
} |
|
one.SetFloat(this) |
|
} else if one.Kind() == reflect.Bool { |
|
this, err := strconv.ParseBool(value) |
|
if err != nil { |
|
log.Error("err:%v", err) |
|
} |
|
one.SetBool(this) |
|
} else { |
|
one.SetString(value) |
|
} |
|
} |
|
} |
|
|
|
// 将http的请求头替换为https |
|
func ReplaceHTTPWithHTTPS(text string) string { |
|
// 创建正则表达式,用于匹配 HTTP 链接 |
|
httpPattern := regexp.MustCompile(`http://[^\s]+`) |
|
|
|
// 将 HTTP 替换为 HTTPS |
|
httpsText := httpPattern.ReplaceAllStringFunc(text, func(match string) string { |
|
httpsURL := "https" + strings.TrimPrefix(match, "http") |
|
return httpsURL |
|
}) |
|
|
|
return httpsText |
|
} |
|
|
|
func CheckRequestBody(c *gin.Context) (bool, []byte) { |
|
// 读取请求体 |
|
bodyBytes, err := ioutil.ReadAll(c.Request.Body) |
|
if err != nil { |
|
return false, nil |
|
} |
|
|
|
// 恢复请求体的内容,以便后续处理 |
|
c.Request.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes)) |
|
|
|
// 检查请求体长度 |
|
if len(bodyBytes) == 0 { |
|
return false, nil |
|
} |
|
return true, bodyBytes |
|
}
|
|
|