1
This commit is contained in:
236
pkg/httpclient/httpclient.go
Normal file
236
pkg/httpclient/httpclient.go
Normal file
@ -0,0 +1,236 @@
|
||||
package httpclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.uber.org/zap"
|
||||
|
||||
log "github.com/go-admin-team/go-admin-core/logger"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
var (
|
||||
client = &fasthttp.Client{MaxConnsPerHost: 1000000, TLSConfig: &tls.Config{InsecureSkipVerify: true}, ReadTimeout: time.Second * 60, MaxIdemponentCallAttempts: 0}
|
||||
clientForm = &fasthttp.Client{MaxConnsPerHost: 1000000, TLSConfig: &tls.Config{InsecureSkipVerify: true}, ReadTimeout: time.Second * 60, MaxIdemponentCallAttempts: 0}
|
||||
)
|
||||
|
||||
// JumioPostBasicAuth
|
||||
func JumioPostBasicAuth(uri, username, password string, data url.Values) ([]byte, error) {
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 10,
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", uri, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req.SetBasicAuth(username, password)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
body, _ := ioutil.ReadAll(response.Body)
|
||||
return body, nil
|
||||
}
|
||||
|
||||
// JumioPostOauth
|
||||
func JumioPostOauth(uri, token string, data []byte) ([]byte, error) {
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 10,
|
||||
}
|
||||
req, err := http.NewRequest("POST", uri, bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", token))
|
||||
req.Header.Set("User-Agent", "myapp-v1.0.0")
|
||||
req.Header.Set("Accept", "*/*")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
body, _ := ioutil.ReadAll(response.Body)
|
||||
return body, nil
|
||||
|
||||
}
|
||||
|
||||
// JumioHttp
|
||||
func JumioHttp(method, uri, accessToken string, data url.Values) ([]byte, error) {
|
||||
client := &http.Client{
|
||||
Timeout: time.Second * 10,
|
||||
}
|
||||
req, err := http.NewRequest(method, uri, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", accessToken))
|
||||
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
body, _ := ioutil.ReadAll(response.Body)
|
||||
xx := string(body)
|
||||
fmt.Println(xx)
|
||||
return body, nil
|
||||
}
|
||||
|
||||
// FastPostByte do application/json POST request via fasthttp
|
||||
func FastPostByte(url string, body []byte) ([]byte, error) {
|
||||
req := fasthttp.AcquireRequest()
|
||||
resp := fasthttp.AcquireResponse()
|
||||
defer func() {
|
||||
fasthttp.ReleaseResponse(resp)
|
||||
fasthttp.ReleaseRequest(req)
|
||||
}()
|
||||
req.SetRequestURI(url)
|
||||
//req.Header.SetContentType("application/x-www-form-urlencoded")
|
||||
req.Header.SetContentType("application/json; charset=utf-8")
|
||||
req.Header.Add("Accept", "application/json")
|
||||
// if w.Authentication && len(w.JwtToken) > 0 {
|
||||
// req.Header.Set("Authorization", "Bearer "+w.JwtToken)
|
||||
// }
|
||||
|
||||
req.Header.SetMethod("POST")
|
||||
req.SetBody(body)
|
||||
|
||||
// if !loghelper.LevelIsError() {
|
||||
// loghelper.InfoLog("FastPostByte:", string(body))
|
||||
// }
|
||||
err := client.Do(req, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
recbody := resp.Body()
|
||||
arr := make([]byte, len(recbody))
|
||||
copy(arr, recbody)
|
||||
return arr, nil
|
||||
}
|
||||
|
||||
// FastGet Fast Get
|
||||
//
|
||||
// @params header 格式 []string{
|
||||
// "key:value",
|
||||
// "key:value",
|
||||
// }
|
||||
//
|
||||
// @return "中国,深圳"
|
||||
func FastGet(url string, header ...string) ([]byte, error) {
|
||||
req := fasthttp.AcquireRequest()
|
||||
resp := fasthttp.AcquireResponse()
|
||||
defer func() {
|
||||
fasthttp.ReleaseResponse(resp)
|
||||
fasthttp.ReleaseRequest(req)
|
||||
}()
|
||||
req.SetRequestURI(url)
|
||||
|
||||
// if w.Authentication && len(w.JwtToken) > 0 {
|
||||
// req.Header.Set("Authorization", "Bearer "+w.JwtToken)
|
||||
// }
|
||||
|
||||
for _, h := range header {
|
||||
kv := strings.Split(h, ":")
|
||||
req.Header.Set(kv[0], kv[1])
|
||||
}
|
||||
|
||||
// define webapi client request Method
|
||||
req.Header.SetMethod("GET")
|
||||
// DO GET request
|
||||
err := client.Do(req, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
body := resp.Body()
|
||||
// arr := make([]byte, len(body))
|
||||
// copy(arr, body)
|
||||
return body, nil
|
||||
}
|
||||
|
||||
// fastFormPost do POST request via fasthttp
|
||||
func fastFormPost(url string, parmlist map[string]string) (*fasthttp.Response, error) {
|
||||
req := fasthttp.AcquireRequest()
|
||||
resp := fasthttp.AcquireResponse()
|
||||
defer func() {
|
||||
fasthttp.ReleaseResponse(resp)
|
||||
fasthttp.ReleaseRequest(req)
|
||||
}()
|
||||
req.SetRequestURI(url)
|
||||
// if w.Authentication && len(w.JwtToken) > 0 {
|
||||
// req.Header.Set("Authorization", "Bearer "+w.JwtToken)
|
||||
// }
|
||||
req.Header.SetMethod("POST")
|
||||
args := req.PostArgs()
|
||||
for k, v := range parmlist {
|
||||
args.Set(k, v)
|
||||
}
|
||||
err := clientForm.Do(req, resp) // fasthttp.DoTimeout(req, resp, timeOut)
|
||||
if err != nil {
|
||||
log.Error("post request error", zap.Error(err))
|
||||
return nil, err
|
||||
}
|
||||
out := fasthttp.AcquireResponse()
|
||||
resp.CopyTo(out)
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// PostForm do POST request via fasthttp
|
||||
func PostForm(url string, parmlist map[string]string) ([]byte, error) {
|
||||
resp, err := fastFormPost(url, parmlist)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
arr := make([]byte, len(resp.Body()))
|
||||
copy(arr, resp.Body())
|
||||
// defer fasthttp.ReleaseResponse(resp)
|
||||
// return resp.Body()
|
||||
return arr, nil
|
||||
}
|
||||
|
||||
// ClientIP 获取真实的IP 1.1.1.1, 2.2.2.2, 3.3.3.3
|
||||
func ClientIP(ctx *fasthttp.RequestCtx) string {
|
||||
clientIP := string(ctx.Request.Header.Peek("X-Forwarded-For"))
|
||||
if index := strings.IndexByte(clientIP, ','); index >= 0 {
|
||||
clientIP = clientIP[0:index]
|
||||
// 获取最开始的一个 即 1.1.1.1
|
||||
}
|
||||
clientIP = strings.TrimSpace(clientIP)
|
||||
if len(clientIP) > 0 {
|
||||
return clientIP
|
||||
}
|
||||
clientIP = strings.TrimSpace(string(ctx.Request.Header.Peek("X-Real-Ip")))
|
||||
if len(clientIP) > 0 {
|
||||
return clientIP
|
||||
}
|
||||
return ctx.RemoteIP().String()
|
||||
}
|
||||
|
||||
// MergeQuery appends additional query values to an existing URL.
|
||||
func MergeQuery(u url.URL, q url.Values) url.URL {
|
||||
uv := u.Query()
|
||||
for k, vs := range q {
|
||||
for _, v := range vs {
|
||||
uv.Add(k, v)
|
||||
}
|
||||
}
|
||||
u.RawQuery = uv.Encode()
|
||||
return u
|
||||
}
|
||||
192
pkg/httpclient/httputils.go
Normal file
192
pkg/httpclient/httputils.go
Normal file
@ -0,0 +1,192 @@
|
||||
package httpclient
|
||||
|
||||
// http request 工具函数
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
"github.com/valyala/fasthttp"
|
||||
"github.com/valyala/fasthttp/fasthttpproxy"
|
||||
)
|
||||
|
||||
var (
|
||||
fastHttpClient = &fasthttp.Client{
|
||||
Name: "http",
|
||||
MaxConnsPerHost: 10000,
|
||||
MaxIdleConnDuration: 30 * time.Second,
|
||||
ReadTimeout: 50 * time.Second,
|
||||
WriteTimeout: 50 * time.Second,
|
||||
TLSConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
}
|
||||
socksDialer fasthttp.DialFunc
|
||||
proxyUrl = ""
|
||||
proxyScheme = "socks5"
|
||||
)
|
||||
|
||||
func NewHttpRequestWithFasthttp(reqMethod, reqUrl, postData string, headers map[string]string) ([]byte, error) {
|
||||
// loghelper.InfoLog("use fasthttp client")
|
||||
if len(proxyUrl) > 0 {
|
||||
|
||||
if socksDialer == nil {
|
||||
socksDialer = fasthttpproxy.FasthttpSocksDialer(strings.TrimPrefix(proxyUrl, proxyScheme+"://"))
|
||||
fastHttpClient.Dial = socksDialer
|
||||
}
|
||||
}
|
||||
req := fasthttp.AcquireRequest()
|
||||
resp := fasthttp.AcquireResponse()
|
||||
defer func() {
|
||||
fasthttp.ReleaseRequest(req)
|
||||
fasthttp.ReleaseResponse(resp)
|
||||
}()
|
||||
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
req.Header.SetMethod(reqMethod)
|
||||
req.SetRequestURI(reqUrl)
|
||||
req.SetBodyString(postData)
|
||||
|
||||
err := fastHttpClient.Do(req, resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if resp.StatusCode() != 200 {
|
||||
return nil, errors.New(fmt.Sprintf("HttpStatusCode:%d ,Desc:%s", resp.StatusCode(), string(resp.Body())))
|
||||
}
|
||||
return resp.Body(), nil
|
||||
}
|
||||
|
||||
func HttpGet(reqUrl string) (map[string]interface{}, error) {
|
||||
respData, err := NewHttpRequestWithFasthttp("GET", reqUrl, "", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var bodyDataMap map[string]interface{}
|
||||
err = sonic.Unmarshal(respData, &bodyDataMap)
|
||||
if err != nil {
|
||||
log.Println(string(respData))
|
||||
return nil, err
|
||||
}
|
||||
return bodyDataMap, nil
|
||||
}
|
||||
|
||||
func HttpGet2(reqUrl string, headers map[string]string) (map[string]interface{}, error) {
|
||||
if headers == nil {
|
||||
headers = map[string]string{}
|
||||
}
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
respData, err := NewHttpRequestWithFasthttp("GET", reqUrl, "", headers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var bodyDataMap map[string]interface{}
|
||||
err = sonic.Unmarshal(respData, &bodyDataMap)
|
||||
if err != nil {
|
||||
log.Println("respData", string(respData))
|
||||
return nil, err
|
||||
}
|
||||
return bodyDataMap, nil
|
||||
}
|
||||
|
||||
func HttpGet3(reqUrl string, headers map[string]string) ([]interface{}, error) {
|
||||
if headers == nil {
|
||||
headers = map[string]string{}
|
||||
}
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
respData, err := NewHttpRequestWithFasthttp("GET", reqUrl, "", headers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// println(string(respData))
|
||||
var bodyDataMap []interface{}
|
||||
err = sonic.Unmarshal(respData, &bodyDataMap)
|
||||
if err != nil {
|
||||
log.Println("respData", string(respData))
|
||||
return nil, err
|
||||
}
|
||||
return bodyDataMap, nil
|
||||
}
|
||||
|
||||
func HttpGet4(reqUrl string, headers map[string]string, result interface{}) error {
|
||||
if headers == nil {
|
||||
headers = map[string]string{}
|
||||
}
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
respData, err := NewHttpRequestWithFasthttp("GET", reqUrl, "", headers)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = sonic.Unmarshal(respData, result)
|
||||
if err != nil {
|
||||
log.Printf("HttpGet4 - jsonhelper.Unmarshal failed : %v, resp %s", err, string(respData))
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func HttpGet5(reqUrl string, headers map[string]string) ([]byte, error) {
|
||||
if headers == nil {
|
||||
headers = map[string]string{}
|
||||
}
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
respData, err := NewHttpRequestWithFasthttp("GET", reqUrl, "", headers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return respData, nil
|
||||
}
|
||||
|
||||
func HttpPostForm(reqUrl string, postData url.Values) ([]byte, error) {
|
||||
headers := map[string]string{
|
||||
"Content-Type": "application/x-www-form-urlencoded"}
|
||||
return NewHttpRequestWithFasthttp("POST", reqUrl, postData.Encode(), headers)
|
||||
}
|
||||
|
||||
func HttpPostForm2(reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
|
||||
if headers == nil {
|
||||
headers = map[string]string{}
|
||||
}
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
return NewHttpRequestWithFasthttp("POST", reqUrl, postData.Encode(), headers)
|
||||
}
|
||||
|
||||
func HttpPostForm3(reqUrl string, postData string, headers map[string]string) ([]byte, error) {
|
||||
return NewHttpRequestWithFasthttp("POST", reqUrl, postData, headers)
|
||||
}
|
||||
|
||||
func HttpPostForm4(reqUrl string, postData map[string]string, headers map[string]string) ([]byte, error) {
|
||||
if headers == nil {
|
||||
headers = map[string]string{}
|
||||
}
|
||||
headers["Content-Type"] = "application/json"
|
||||
data, _ := sonic.Marshal(postData)
|
||||
return NewHttpRequestWithFasthttp("POST", reqUrl, string(data), headers)
|
||||
}
|
||||
|
||||
func HttpDeleteForm(reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
|
||||
if headers == nil {
|
||||
headers = map[string]string{}
|
||||
}
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
return NewHttpRequestWithFasthttp("DELETE", reqUrl, postData.Encode(), headers)
|
||||
}
|
||||
|
||||
func HttpPut(reqUrl string, postData url.Values, headers map[string]string) ([]byte, error) {
|
||||
if headers == nil {
|
||||
headers = map[string]string{}
|
||||
}
|
||||
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
||||
return NewHttpRequestWithFasthttp("PUT", reqUrl, postData.Encode(), headers)
|
||||
}
|
||||
Reference in New Issue
Block a user