更新domain逻辑
This commit is contained in:
parent
0cc9ecd602
commit
b919e2cd39
@ -80,13 +80,7 @@ func (c *Caller) GetLoginInfo(body []byte) (*LoginInfo, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
c.Client.domain = WechatDomain(path.Host)
|
||||||
domain, err := getDomainByHost(path.Host)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
c.Client.domain = domain
|
|
||||||
|
|
||||||
resp, err := c.Client.GetLoginInfo(path.String())
|
resp, err := c.Client.GetLoginInfo(path.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
uErr, ok := err.(*url.Error)
|
uErr, ok := err.(*url.Error)
|
||||||
|
46
client.go
46
client.go
@ -40,7 +40,7 @@ func (u UserAgentHook) AfterRequest(response *http.Response, err error) {}
|
|||||||
type Client struct {
|
type Client struct {
|
||||||
HttpHooks HttpHooks
|
HttpHooks HttpHooks
|
||||||
*http.Client
|
*http.Client
|
||||||
domain *WechatDomain
|
domain WechatDomain
|
||||||
mode mode
|
mode mode
|
||||||
mu sync.Mutex
|
mu sync.Mutex
|
||||||
cookies map[string][]*http.Cookie
|
cookies map[string][]*http.Cookie
|
||||||
@ -159,7 +159,7 @@ func (c *Client) GetLoginInfo(path string) (*http.Response, error) {
|
|||||||
|
|
||||||
// 请求获取初始化信息
|
// 请求获取初始化信息
|
||||||
func (c *Client) WebInit(request *BaseRequest) (*http.Response, error) {
|
func (c *Client) WebInit(request *BaseRequest) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxinit)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxinit)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("_", fmt.Sprintf("%d", time.Now().Unix()))
|
params.Add("_", fmt.Sprintf("%d", time.Now().Unix()))
|
||||||
path.RawQuery = params.Encode()
|
path.RawQuery = params.Encode()
|
||||||
@ -175,7 +175,7 @@ func (c *Client) WebInit(request *BaseRequest) (*http.Response, error) {
|
|||||||
|
|
||||||
// 通知手机已登录
|
// 通知手机已登录
|
||||||
func (c *Client) WebWxStatusNotify(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*http.Response, error) {
|
func (c *Client) WebWxStatusNotify(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxstatusnotify)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxstatusnotify)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("lang", "zh_CN")
|
params.Add("lang", "zh_CN")
|
||||||
params.Add("pass_ticket", info.PassTicket)
|
params.Add("pass_ticket", info.PassTicket)
|
||||||
@ -196,7 +196,7 @@ func (c *Client) WebWxStatusNotify(request *BaseRequest, response *WebInitRespon
|
|||||||
|
|
||||||
// 异步检查是否有新的消息返回
|
// 异步检查是否有新的消息返回
|
||||||
func (c *Client) SyncCheck(info *LoginInfo, response *WebInitResponse) (*http.Response, error) {
|
func (c *Client) SyncCheck(info *LoginInfo, response *WebInitResponse) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.SyncHost + synccheck)
|
path, _ := url.Parse(c.domain.SyncHost() + synccheck)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
||||||
params.Add("skey", info.SKey)
|
params.Add("skey", info.SKey)
|
||||||
@ -219,7 +219,7 @@ func (c *Client) SyncCheck(info *LoginInfo, response *WebInitResponse) (*http.Re
|
|||||||
|
|
||||||
// 获取联系人信息
|
// 获取联系人信息
|
||||||
func (c *Client) WebWxGetContact(info *LoginInfo) (*http.Response, error) {
|
func (c *Client) WebWxGetContact(info *LoginInfo) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxgetcontact)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxgetcontact)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
||||||
params.Add("skey", info.SKey)
|
params.Add("skey", info.SKey)
|
||||||
@ -231,7 +231,7 @@ func (c *Client) WebWxGetContact(info *LoginInfo) (*http.Response, error) {
|
|||||||
|
|
||||||
// 获取联系人详情
|
// 获取联系人详情
|
||||||
func (c *Client) WebWxBatchGetContact(members Members, request *BaseRequest) (*http.Response, error) {
|
func (c *Client) WebWxBatchGetContact(members Members, request *BaseRequest) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxbatchgetcontact)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxbatchgetcontact)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("type", "ex")
|
params.Add("type", "ex")
|
||||||
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
||||||
@ -250,7 +250,7 @@ func (c *Client) WebWxBatchGetContact(members Members, request *BaseRequest) (*h
|
|||||||
|
|
||||||
// 获取消息接口
|
// 获取消息接口
|
||||||
func (c *Client) WebWxSync(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*http.Response, error) {
|
func (c *Client) WebWxSync(request *BaseRequest, response *WebInitResponse, info *LoginInfo) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxsync)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxsync)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("sid", info.WxSid)
|
params.Add("sid", info.WxSid)
|
||||||
params.Add("skey", info.SKey)
|
params.Add("skey", info.SKey)
|
||||||
@ -284,7 +284,7 @@ func (c *Client) sendMessage(request *BaseRequest, url string, msg *SendMessage)
|
|||||||
// 发送文本消息
|
// 发送文本消息
|
||||||
func (c *Client) WebWxSendMsg(msg *SendMessage, info *LoginInfo, request *BaseRequest) (*http.Response, error) {
|
func (c *Client) WebWxSendMsg(msg *SendMessage, info *LoginInfo, request *BaseRequest) (*http.Response, error) {
|
||||||
msg.Type = TextMessage
|
msg.Type = TextMessage
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxsendmsg)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxsendmsg)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("lang", "zh_CN")
|
params.Add("lang", "zh_CN")
|
||||||
params.Add("pass_ticket", info.PassTicket)
|
params.Add("pass_ticket", info.PassTicket)
|
||||||
@ -294,7 +294,7 @@ func (c *Client) WebWxSendMsg(msg *SendMessage, info *LoginInfo, request *BaseRe
|
|||||||
|
|
||||||
// 获取用户的头像
|
// 获取用户的头像
|
||||||
func (c *Client) WebWxGetHeadImg(headImageUrl string) (*http.Response, error) {
|
func (c *Client) WebWxGetHeadImg(headImageUrl string) (*http.Response, error) {
|
||||||
path := c.domain.BaseHost + headImageUrl
|
path := c.domain.BaseHost() + headImageUrl
|
||||||
req, _ := http.NewRequest(http.MethodGet, path, nil)
|
req, _ := http.NewRequest(http.MethodGet, path, nil)
|
||||||
return c.Do(req)
|
return c.Do(req)
|
||||||
}
|
}
|
||||||
@ -329,7 +329,7 @@ func (c *Client) WebWxUploadMediaByChunk(file *os.File, request *BaseRequest, in
|
|||||||
// 获取文件的类型
|
// 获取文件的类型
|
||||||
mediaType := getMessageType(sate.Name())
|
mediaType := getMessageType(sate.Name())
|
||||||
|
|
||||||
path, _ := url.Parse(c.domain.FileHost + webwxuploadmedia)
|
path, _ := url.Parse(c.domain.FileHost() + webwxuploadmedia)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("f", "json")
|
params.Add("f", "json")
|
||||||
|
|
||||||
@ -451,7 +451,7 @@ func (c *Client) WebWxUploadMediaByChunk(file *os.File, request *BaseRequest, in
|
|||||||
// 发送的图片必须是已经成功上传的图片
|
// 发送的图片必须是已经成功上传的图片
|
||||||
func (c *Client) WebWxSendMsgImg(msg *SendMessage, request *BaseRequest, info *LoginInfo) (*http.Response, error) {
|
func (c *Client) WebWxSendMsgImg(msg *SendMessage, request *BaseRequest, info *LoginInfo) (*http.Response, error) {
|
||||||
msg.Type = ImageMessage
|
msg.Type = ImageMessage
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxsendmsgimg)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxsendmsgimg)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("fun", "async")
|
params.Add("fun", "async")
|
||||||
params.Add("f", "json")
|
params.Add("f", "json")
|
||||||
@ -464,7 +464,7 @@ func (c *Client) WebWxSendMsgImg(msg *SendMessage, request *BaseRequest, info *L
|
|||||||
// 发送文件信息
|
// 发送文件信息
|
||||||
func (c *Client) WebWxSendAppMsg(msg *SendMessage, request *BaseRequest) (*http.Response, error) {
|
func (c *Client) WebWxSendAppMsg(msg *SendMessage, request *BaseRequest) (*http.Response, error) {
|
||||||
msg.Type = AppMessage
|
msg.Type = AppMessage
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxsendappmsg)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxsendappmsg)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("fun", "async")
|
params.Add("fun", "async")
|
||||||
params.Add("f", "json")
|
params.Add("f", "json")
|
||||||
@ -474,7 +474,7 @@ func (c *Client) WebWxSendAppMsg(msg *SendMessage, request *BaseRequest) (*http.
|
|||||||
|
|
||||||
// 用户重命名接口
|
// 用户重命名接口
|
||||||
func (c *Client) WebWxOplog(request *BaseRequest, remarkName, userName string) (*http.Response, error) {
|
func (c *Client) WebWxOplog(request *BaseRequest, remarkName, userName string) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxoplog)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxoplog)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("lang", "zh_CN")
|
params.Add("lang", "zh_CN")
|
||||||
path.RawQuery = params.Encode()
|
path.RawQuery = params.Encode()
|
||||||
@ -493,7 +493,7 @@ func (c *Client) WebWxOplog(request *BaseRequest, remarkName, userName string) (
|
|||||||
// 添加用户为好友接口
|
// 添加用户为好友接口
|
||||||
func (c *Client) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyContent string) (*http.Response, error) {
|
func (c *Client) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyContent string) (*http.Response, error) {
|
||||||
loginInfo := storage.LoginInfo
|
loginInfo := storage.LoginInfo
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxverifyuser)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxverifyuser)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
params.Add("r", strconv.FormatInt(time.Now().Unix(), 10))
|
||||||
params.Add("lang", "zh_CN")
|
params.Add("lang", "zh_CN")
|
||||||
@ -520,7 +520,7 @@ func (c *Client) WebWxVerifyUser(storage *Storage, info RecommendInfo, verifyCon
|
|||||||
|
|
||||||
// 获取图片消息的图片响应
|
// 获取图片消息的图片响应
|
||||||
func (c *Client) WebWxGetMsgImg(msg *Message, info *LoginInfo) (*http.Response, error) {
|
func (c *Client) WebWxGetMsgImg(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxgetmsgimg)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxgetmsgimg)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("MsgID", msg.MsgId)
|
params.Add("MsgID", msg.MsgId)
|
||||||
params.Add("skey", info.SKey)
|
params.Add("skey", info.SKey)
|
||||||
@ -532,7 +532,7 @@ func (c *Client) WebWxGetMsgImg(msg *Message, info *LoginInfo) (*http.Response,
|
|||||||
|
|
||||||
// 获取语音消息的语音响应
|
// 获取语音消息的语音响应
|
||||||
func (c *Client) WebWxGetVoice(msg *Message, info *LoginInfo) (*http.Response, error) {
|
func (c *Client) WebWxGetVoice(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxgetvoice)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxgetvoice)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("msgid", msg.MsgId)
|
params.Add("msgid", msg.MsgId)
|
||||||
params.Add("skey", info.SKey)
|
params.Add("skey", info.SKey)
|
||||||
@ -543,7 +543,7 @@ func (c *Client) WebWxGetVoice(msg *Message, info *LoginInfo) (*http.Response, e
|
|||||||
|
|
||||||
// 获取视频消息的视频响应
|
// 获取视频消息的视频响应
|
||||||
func (c *Client) WebWxGetVideo(msg *Message, info *LoginInfo) (*http.Response, error) {
|
func (c *Client) WebWxGetVideo(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxgetvideo)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxgetvideo)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("msgid", msg.MsgId)
|
params.Add("msgid", msg.MsgId)
|
||||||
params.Add("skey", info.SKey)
|
params.Add("skey", info.SKey)
|
||||||
@ -554,7 +554,7 @@ func (c *Client) WebWxGetVideo(msg *Message, info *LoginInfo) (*http.Response, e
|
|||||||
|
|
||||||
// 获取文件消息的文件响应
|
// 获取文件消息的文件响应
|
||||||
func (c *Client) WebWxGetMedia(msg *Message, info *LoginInfo) (*http.Response, error) {
|
func (c *Client) WebWxGetMedia(msg *Message, info *LoginInfo) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.FileHost + webwxgetmedia)
|
path, _ := url.Parse(c.domain.FileHost() + webwxgetmedia)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("sender", msg.FromUserName)
|
params.Add("sender", msg.FromUserName)
|
||||||
params.Add("mediaid", msg.MediaId)
|
params.Add("mediaid", msg.MediaId)
|
||||||
@ -569,7 +569,7 @@ func (c *Client) WebWxGetMedia(msg *Message, info *LoginInfo) (*http.Response, e
|
|||||||
|
|
||||||
// 用户退出
|
// 用户退出
|
||||||
func (c *Client) Logout(info *LoginInfo) (*http.Response, error) {
|
func (c *Client) Logout(info *LoginInfo) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxlogout)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxlogout)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("redirect", "1")
|
params.Add("redirect", "1")
|
||||||
params.Add("type", "1")
|
params.Add("type", "1")
|
||||||
@ -581,7 +581,7 @@ func (c *Client) Logout(info *LoginInfo) (*http.Response, error) {
|
|||||||
|
|
||||||
// 添加用户进群聊
|
// 添加用户进群聊
|
||||||
func (c *Client) AddMemberIntoChatRoom(req *BaseRequest, info *LoginInfo, group *Group, friends ...*Friend) (*http.Response, error) {
|
func (c *Client) AddMemberIntoChatRoom(req *BaseRequest, info *LoginInfo, group *Group, friends ...*Friend) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxupdatechatroom)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxupdatechatroom)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("fun", "addmember")
|
params.Add("fun", "addmember")
|
||||||
params.Add("pass_ticket", info.PassTicket)
|
params.Add("pass_ticket", info.PassTicket)
|
||||||
@ -604,7 +604,7 @@ func (c *Client) AddMemberIntoChatRoom(req *BaseRequest, info *LoginInfo, group
|
|||||||
|
|
||||||
// 从群聊中移除用户
|
// 从群聊中移除用户
|
||||||
func (c *Client) RemoveMemberFromChatRoom(req *BaseRequest, info *LoginInfo, group *Group, friends ...*User) (*http.Response, error) {
|
func (c *Client) RemoveMemberFromChatRoom(req *BaseRequest, info *LoginInfo, group *Group, friends ...*User) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxupdatechatroom)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxupdatechatroom)
|
||||||
params := url.Values{}
|
params := url.Values{}
|
||||||
params.Add("fun", "delmember")
|
params.Add("fun", "delmember")
|
||||||
params.Add("lang", "zh_CN")
|
params.Add("lang", "zh_CN")
|
||||||
@ -633,14 +633,14 @@ func (c *Client) WebWxRevokeMsg(msg *SentMessage, request *BaseRequest) (*http.R
|
|||||||
"ToUserName": msg.ToUserName,
|
"ToUserName": msg.ToUserName,
|
||||||
}
|
}
|
||||||
buffer, _ := ToBuffer(content)
|
buffer, _ := ToBuffer(content)
|
||||||
req, _ := http.NewRequest(http.MethodPost, c.domain.BaseHost+webwxrevokemsg, buffer)
|
req, _ := http.NewRequest(http.MethodPost, c.domain.BaseHost()+webwxrevokemsg, buffer)
|
||||||
req.Header.Set("Content-Type", jsonContentType)
|
req.Header.Set("Content-Type", jsonContentType)
|
||||||
return c.Do(req)
|
return c.Do(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 校验上传文件
|
// 校验上传文件
|
||||||
func (c *Client) webWxCheckUpload(stat os.FileInfo, request *BaseRequest, fileMd5, fromUserName, toUserName string) (*http.Response, error) {
|
func (c *Client) webWxCheckUpload(stat os.FileInfo, request *BaseRequest, fileMd5, fromUserName, toUserName string) (*http.Response, error) {
|
||||||
path, _ := url.Parse(c.domain.BaseHost + webwxcheckupload)
|
path, _ := url.Parse(c.domain.BaseHost() + webwxcheckupload)
|
||||||
content := map[string]interface{}{
|
content := map[string]interface{}{
|
||||||
"BaseRequest": request,
|
"BaseRequest": request,
|
||||||
"FileMd5": fileMd5,
|
"FileMd5": fileMd5,
|
||||||
|
@ -18,7 +18,7 @@ type HotReloadStorageItem struct {
|
|||||||
Cookies map[string][]*http.Cookie
|
Cookies map[string][]*http.Cookie
|
||||||
BaseRequest *BaseRequest
|
BaseRequest *BaseRequest
|
||||||
LoginInfo *LoginInfo
|
LoginInfo *LoginInfo
|
||||||
WechatDomain *WechatDomain
|
WechatDomain WechatDomain
|
||||||
}
|
}
|
||||||
|
|
||||||
// 热登陆存储接口
|
// 热登陆存储接口
|
||||||
|
24
url.go
24
url.go
@ -1,7 +1,5 @@
|
|||||||
package openwechat
|
package openwechat
|
||||||
|
|
||||||
import "errors"
|
|
||||||
|
|
||||||
// mode 类型限制
|
// mode 类型限制
|
||||||
type mode string
|
type mode string
|
||||||
|
|
||||||
@ -39,22 +37,16 @@ const (
|
|||||||
qrcode = "https://login.weixin.qq.com/qrcode/"
|
qrcode = "https://login.weixin.qq.com/qrcode/"
|
||||||
)
|
)
|
||||||
|
|
||||||
var domainMap = map[string]*WechatDomain{
|
type WechatDomain string
|
||||||
"wx.qq.com": {"https://wx.qq.com", "https://file.wx.qq.com", "https://webpush.wx.qq.com"},
|
|
||||||
"wx2.qq.com": {"https://wx2.qq.com", "https://file.wx2.qq.com", "https://webpush.wx2.qq.com"},
|
func (w WechatDomain) BaseHost() string {
|
||||||
"wx8.qq.com": {"https://wx8.qq.com", "https://file.wx8.qq.com", "https://webpush.wx8.qq.com"},
|
return "https://" + string(w)
|
||||||
"web2.wechat.com": {"https://web2.wechat.com", "https://file.web2.wechat.com", "https://webpush.web2.wechat.com"},
|
|
||||||
"wechat.com": {"https://wechat.com", "https://file.web.wechat.com", "https://webpush.web.wechat.com"},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getDomainByHost(host string) (*WechatDomain, error) {
|
func (w WechatDomain) FileHost() string {
|
||||||
value, exist := domainMap[host]
|
return "https://file." + string(w)
|
||||||
if !exist {
|
|
||||||
return nil, errors.New("invalid host")
|
|
||||||
}
|
|
||||||
return value, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WechatDomain struct {
|
func (w WechatDomain) SyncHost() string {
|
||||||
BaseHost, FileHost, SyncHost string
|
return "https://webpush." + string(w)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user