136 lines
3.4 KiB
Go
136 lines
3.4 KiB
Go
|
|
package mmtls
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"io/ioutil"
|
||
|
|
"net"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"xiawan/wx/clientsdk/baseutils"
|
||
|
|
)
|
||
|
|
|
||
|
|
// MMHTTPPost mmtls方式发送数据包
|
||
|
|
func MMHTTPPost(mmInfo *MMInfo, data []byte) ([]byte, error) {
|
||
|
|
requestURL := "http://" + mmInfo.ShortHost + mmInfo.ShortURL
|
||
|
|
request, err := http.NewRequest("POST", requestURL, bytes.NewReader(data))
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
request.Header.Add("UserAgent", "MicroMessenger Client")
|
||
|
|
request.Header.Add("Accept", "*/*")
|
||
|
|
request.Header.Add("Cache-Control", "no-cache")
|
||
|
|
request.Header.Add("Connection", "close")
|
||
|
|
request.Header.Add("content-type", "application/octet-stream")
|
||
|
|
request.Header.Add("Upgrade", "mmtls")
|
||
|
|
request.Header.Add("Host", mmInfo.ShortHost)
|
||
|
|
//fmt.Println(mmInfo.ShortHost)//szshort.weixin.qq.com
|
||
|
|
|
||
|
|
// 发送请求
|
||
|
|
httpTransport := &http.Transport{
|
||
|
|
Dial: func(network, addr string) (net.Conn, error) {
|
||
|
|
conn, err := net.DialTimeout(network, addr, time.Second*15) //设置建立连接超时
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
conn.SetDeadline(time.Now().Add(time.Second * 15)) //设置发送接受数据超时
|
||
|
|
return conn, nil
|
||
|
|
},
|
||
|
|
ResponseHeaderTimeout: time.Second * 15,
|
||
|
|
MaxIdleConnsPerHost: -1, //禁用连接池缓存
|
||
|
|
DisableKeepAlives: true, //禁用客户端连接缓存到连接池
|
||
|
|
}
|
||
|
|
|
||
|
|
// 如果有代理
|
||
|
|
if mmInfo.Dialer != nil {
|
||
|
|
httpTransport.Dial = mmInfo.Dialer.Dial
|
||
|
|
}
|
||
|
|
|
||
|
|
client := &http.Client{Transport: httpTransport}
|
||
|
|
resp, err := client.Do(request)
|
||
|
|
if err != nil {
|
||
|
|
baseutils.PrintLog(err.Error())
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 接收响应
|
||
|
|
defer resp.Body.Close()
|
||
|
|
body, err := ioutil.ReadAll(resp.Body)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 返回响应数据
|
||
|
|
return body, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
// MMHTTPPostData mmtls短链接方式发送请求数据包
|
||
|
|
func MMHTTPPostData(mmInfo *MMInfo, url string, data []byte) ([]byte, error) {
|
||
|
|
// 创建HttpHandler
|
||
|
|
httpHandler := &HTTPHandler{}
|
||
|
|
httpHandler.URL = url
|
||
|
|
httpHandler.Host = mmInfo.ShortHost
|
||
|
|
httpHandler.MMPkg = data
|
||
|
|
|
||
|
|
// 创建发送请求项列表
|
||
|
|
sendItems, err := CreateSendPackItems(mmInfo, httpHandler)
|
||
|
|
if err != nil {
|
||
|
|
return []byte{}, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// MMTLS-加密要发送的数据
|
||
|
|
packData, err := MMHTTPPackData(mmInfo, sendItems)
|
||
|
|
if err != nil {
|
||
|
|
return []byte{}, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 发送数据
|
||
|
|
respData, err := MMHTTPPost(mmInfo, packData)
|
||
|
|
// 解包响应数据
|
||
|
|
decodeData, err := MMDecodeResponseData(mmInfo, sendItems, respData)
|
||
|
|
if err != nil {
|
||
|
|
baseutils.PrintLog(err.Error())
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return decodeData, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
*
|
||
|
|
纯Http请求
|
||
|
|
*/
|
||
|
|
func HTTPPost(mmInfo *MMInfo, cgi string, data []byte) ([]byte, error) {
|
||
|
|
requestURL := "http://" + mmInfo.ShortURL + cgi
|
||
|
|
request, err := http.NewRequest("POST", requestURL, bytes.NewReader(data))
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
request.Header.Add("UserAgent", "MicroMessenger Client")
|
||
|
|
request.Header.Add("Accept", "*/*")
|
||
|
|
request.Header.Add("Cache-Control", "no-cache")
|
||
|
|
request.Header.Add("Connection", "Keep-Alive")
|
||
|
|
request.Header.Add("content-type", "application/octet-stream")
|
||
|
|
// 发送请求
|
||
|
|
httpTransport := &http.Transport{}
|
||
|
|
// 如果有代理
|
||
|
|
if mmInfo.Dialer != nil {
|
||
|
|
httpTransport.Dial = mmInfo.Dialer.Dial
|
||
|
|
}
|
||
|
|
client := &http.Client{Transport: httpTransport}
|
||
|
|
resp, err := client.Do(request)
|
||
|
|
if err != nil {
|
||
|
|
baseutils.PrintLog(err.Error())
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
|
||
|
|
// 接收响应
|
||
|
|
defer resp.Body.Close()
|
||
|
|
body, err := ioutil.ReadAll(resp.Body)
|
||
|
|
if err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
// 返回响应数据
|
||
|
|
return body, nil
|
||
|
|
}
|