Files
open-im-server/internal/push/getui/push.go
T

326 lines
9.0 KiB
Go
Raw Normal View History

2022-04-05 10:27:34 +08:00
package getui
2022-04-08 15:40:07 +08:00
import (
2022-06-02 19:14:11 +08:00
"Open_IM/internal/push"
2022-04-08 15:40:07 +08:00
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/log"
"Open_IM/pkg/utils"
"bytes"
2022-04-08 17:48:16 +08:00
"crypto/sha256"
2022-04-12 11:12:36 +08:00
"errors"
2022-04-08 18:29:54 +08:00
//"crypto/sha512"
2022-04-08 16:15:22 +08:00
"encoding/hex"
2022-04-08 15:40:07 +08:00
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"time"
)
var (
GetuiClient *Getui
2022-04-12 11:12:36 +08:00
TokenExpireError = errors.New("token expire")
2022-04-08 15:40:07 +08:00
)
2022-04-08 17:00:28 +08:00
const (
2022-12-21 16:46:16 +08:00
PushURL = "/push/single/alias"
AuthURL = "/auth"
TaskURL = "/push/list/message"
BatchPushURL = "/push/list/alias"
2022-04-08 17:00:28 +08:00
)
2022-04-08 15:40:07 +08:00
func init() {
GetuiClient = newGetuiClient()
}
type Getui struct{}
type GetuiCommonResp struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
type AuthReq struct {
Sign string `json:"sign"`
Timestamp string `json:"timestamp"`
Appkey string `json:"appkey"`
}
type AuthResp struct {
ExpireTime string `json:"expire_time"`
Token string `json:"token"`
}
2022-12-21 16:46:16 +08:00
type TaskResp struct {
TaskID string `json:"taskID"`
}
2022-12-22 11:10:19 +08:00
type Settings struct {
TTL *int64 `json:"ttl"`
}
type Audience struct {
Alias []string `json:"alias"`
}
type PushMessage struct {
Notification *Notification `json:"notification,omitempty"`
Transmission *string `json:"transmission,omitempty"`
}
type PushChannel struct {
Ios *Ios `json:"ios"`
Android *Android `json:"android"`
}
2022-04-08 15:40:07 +08:00
type PushReq struct {
2022-12-22 11:10:19 +08:00
RequestID *string `json:"request_id"`
Settings *Settings `json:"settings"`
Audience *Audience `json:"audience"`
PushMessage *PushMessage `json:"push_message"`
PushChannel *PushChannel `json:"push_channel"`
IsAsync *bool `json:"is_async"`
Taskid *string `json:"taskid"`
2022-04-13 15:21:53 +08:00
}
type Ios struct {
2022-12-22 17:17:28 +08:00
NotiType *string `json:"type"`
AutoBadge *string `json:"auto_badge"`
Aps struct {
2022-04-13 15:21:53 +08:00
Sound string `json:"sound"`
Alert Alert `json:"alert"`
} `json:"aps"`
}
type Alert struct {
Title string `json:"title"`
Body string `json:"body"`
}
type Android struct {
Ups struct {
Notification Notification `json:"notification"`
2022-08-10 19:31:57 +08:00
Options Options `json:"options"`
2022-04-13 15:21:53 +08:00
} `json:"ups"`
2022-04-08 15:40:07 +08:00
}
type Notification struct {
2022-09-07 18:20:41 +08:00
Title string `json:"title"`
Body string `json:"body"`
ChannelID string `json:"channelID"`
ChannelName string `json:"ChannelName"`
ClickType string `json:"click_type"`
2022-04-08 15:40:07 +08:00
}
2022-08-10 19:31:57 +08:00
type Options struct {
HW struct {
DefaultSound bool `json:"/message/android/notification/default_sound"`
ChannelID string `json:"/message/android/notification/channel_id"`
Sound string `json:"/message/android/notification/sound"`
Importance string `json:"/message/android/notification/importance"`
} `json:"HW"`
XM struct {
ChannelID string `json:"/extra.channel_id"`
2022-08-23 19:08:20 +08:00
} `json:"XM"`
VV struct {
Classification int `json:"/classification"`
} `json:"VV"`
2022-08-10 19:31:57 +08:00
}
2022-04-08 15:40:07 +08:00
type PushResp struct {
}
func newGetuiClient() *Getui {
return &Getui{}
}
2022-09-01 21:05:16 +08:00
func (g *Getui) Push(userIDList []string, title, detailContent, operationID string, opts push.PushOpts) (resp string, err error) {
2022-04-08 15:40:07 +08:00
token, err := db.DB.GetGetuiToken()
2022-12-21 17:39:48 +08:00
log.NewDebug(operationID, utils.GetSelfFuncName(), "token", token, userIDList)
2022-04-08 15:40:07 +08:00
if err != nil {
2022-12-21 16:46:16 +08:00
log.NewError(operationID, utils.GetSelfFuncName(), "GetGetuiToken failed", err.Error())
2022-04-08 15:40:07 +08:00
}
if token == "" || err != nil {
2022-04-11 14:41:09 +08:00
token, err = g.getTokenAndSave2Redis(operationID)
2022-04-08 15:40:07 +08:00
if err != nil {
2022-04-11 14:41:09 +08:00
log.NewError(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis failed", err.Error())
return "", utils.Wrap(err, "")
2022-04-08 15:40:07 +08:00
}
}
2022-12-21 16:46:16 +08:00
2022-12-22 11:10:19 +08:00
pushReq := PushReq{PushMessage: &PushMessage{Notification: &Notification{
2022-09-16 17:23:28 +08:00
Title: title,
Body: detailContent,
2022-09-07 18:20:41 +08:00
ClickType: "startapp",
ChannelID: config.Config.Push.Getui.ChannelID,
ChannelName: config.Config.Push.Getui.ChannelName,
2022-12-22 11:10:19 +08:00
}}}
2022-12-22 17:17:28 +08:00
pushReq.setPushChannel(title, detailContent)
2022-12-22 11:10:19 +08:00
pushResp := PushResp{}
2022-12-21 16:46:16 +08:00
if len(userIDList) > 1 {
2022-12-21 18:58:36 +08:00
taskID, err := g.GetTaskID(operationID, token, pushReq)
2022-12-21 16:46:16 +08:00
if err != nil {
2022-12-21 18:58:36 +08:00
return "", utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
2022-12-21 16:46:16 +08:00
}
2022-12-22 17:17:28 +08:00
pushReq = PushReq{Audience: &Audience{Alias: userIDList}}
2022-12-22 17:35:09 +08:00
var IsAsync = true
2022-12-21 18:03:39 +08:00
pushReq.IsAsync = &IsAsync
pushReq.Taskid = &taskID
2022-12-21 16:46:16 +08:00
err = g.request(BatchPushURL, pushReq, token, &pushResp, operationID)
} else {
2022-12-21 18:03:39 +08:00
reqID := utils.OperationIDGenerator()
2022-12-21 19:56:25 +08:00
pushReq.RequestID = &reqID
2022-12-22 11:10:19 +08:00
pushReq.Audience = &Audience{Alias: []string{userIDList[0]}}
2022-12-21 16:46:16 +08:00
err = g.request(PushURL, pushReq, token, &pushResp, operationID)
2022-08-10 19:31:57 +08:00
}
2022-04-12 11:12:36 +08:00
switch err {
case TokenExpireError:
2022-04-13 16:25:33 +08:00
token, err = g.getTokenAndSave2Redis(operationID)
2022-04-12 11:12:36 +08:00
if err != nil {
log.NewError(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis failed, ", err.Error())
2022-04-13 16:25:33 +08:00
} else {
log.NewInfo(operationID, utils.GetSelfFuncName(), "getTokenAndSave2Redis: ", token)
2022-04-12 11:12:36 +08:00
}
}
2022-04-08 15:40:07 +08:00
if err != nil {
2022-04-08 16:40:39 +08:00
return "", utils.Wrap(err, "push failed")
2022-04-08 15:40:07 +08:00
}
respBytes, err := json.Marshal(pushResp)
2022-04-11 14:41:09 +08:00
return string(respBytes), utils.Wrap(err, "")
2022-04-05 10:27:34 +08:00
}
2022-04-08 16:15:22 +08:00
func (g *Getui) Auth(operationID string, timeStamp int64) (token string, expireTime int64, err error) {
2022-04-08 18:27:20 +08:00
log.NewInfo(operationID, utils.GetSelfFuncName(), config.Config.Push.Getui.AppKey, timeStamp, config.Config.Push.Getui.MasterSecret)
2022-04-08 17:48:16 +08:00
h := sha256.New()
2022-04-08 16:15:22 +08:00
h.Write([]byte(config.Config.Push.Getui.AppKey + strconv.Itoa(int(timeStamp)) + config.Config.Push.Getui.MasterSecret))
sum := h.Sum(nil)
sign := hex.EncodeToString(sum)
log.NewInfo(operationID, utils.GetSelfFuncName(), "sha256 result", sign)
2022-04-08 15:40:07 +08:00
reqAuth := AuthReq{
Sign: sign,
Timestamp: strconv.Itoa(int(timeStamp)),
2022-04-08 16:15:22 +08:00
Appkey: config.Config.Push.Getui.AppKey,
2022-04-08 15:40:07 +08:00
}
respAuth := AuthResp{}
2022-04-08 17:00:28 +08:00
err = g.request(AuthURL, reqAuth, "", &respAuth, operationID)
2022-04-08 16:40:39 +08:00
if err != nil {
return "", 0, err
}
log.NewInfo(operationID, utils.GetSelfFuncName(), "result: ", respAuth)
2022-04-08 15:40:07 +08:00
expire, err := strconv.Atoi(respAuth.ExpireTime)
return respAuth.Token, int64(expire), err
2022-04-05 10:27:34 +08:00
}
2022-12-21 16:46:16 +08:00
func (g *Getui) GetTaskID(operationID, token string, pushReq PushReq) (string, error) {
respTask := TaskResp{}
2022-12-22 11:10:19 +08:00
ttl := int64(1000 * 60 * 5)
pushReq.Settings = &Settings{TTL: &ttl}
2022-12-21 16:46:16 +08:00
err := g.request(TaskURL, pushReq, token, &respTask, operationID)
if err != nil {
return "", utils.Wrap(err, "")
}
return respTask.TaskID, nil
}
2022-04-08 17:12:11 +08:00
func (g *Getui) request(url string, content interface{}, token string, returnStruct interface{}, operationID string) error {
2022-04-08 15:40:07 +08:00
con, err := json.Marshal(content)
if err != nil {
return err
}
client := &http.Client{}
2022-08-08 16:46:43 +08:00
log.Debug(operationID, utils.GetSelfFuncName(), "json:", string(con), "token:", token)
2022-04-08 17:12:11 +08:00
req, err := http.NewRequest("POST", config.Config.Push.Getui.PushUrl+url, bytes.NewBuffer(con))
2022-04-08 15:40:07 +08:00
if err != nil {
return err
}
2022-04-08 16:40:39 +08:00
if token != "" {
2022-04-11 10:35:33 +08:00
req.Header.Set("token", token)
2022-04-08 16:40:39 +08:00
}
2022-04-08 15:40:07 +08:00
req.Header.Set("content-type", "application/json")
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
result, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
2022-08-08 16:46:43 +08:00
log.NewDebug(operationID, "getui", utils.GetSelfFuncName(), "resp, ", string(result))
2022-04-08 19:18:00 +08:00
commonResp := GetuiCommonResp{}
commonResp.Data = returnStruct
2022-04-08 19:31:13 +08:00
if err := json.Unmarshal(result, &commonResp); err != nil {
2022-04-08 15:40:07 +08:00
return err
}
2022-04-12 11:12:36 +08:00
if commonResp.Code == 10001 {
return TokenExpireError
}
2022-04-08 15:40:07 +08:00
return nil
2022-04-05 10:27:34 +08:00
}
2022-04-11 14:41:09 +08:00
2022-12-22 15:42:16 +08:00
func (pushReq *PushReq) setPushChannel(title string, body string) {
pushReq.PushChannel = &PushChannel{}
2022-12-26 10:25:42 +08:00
// autoBadge := "+1"
pushReq.PushChannel.Ios = &Ios{}
2022-12-22 17:17:28 +08:00
notify := "notify"
pushReq.PushChannel.Ios.NotiType = &notify
2022-12-22 15:42:16 +08:00
pushReq.PushChannel.Ios.Aps.Sound = "default"
pushReq.PushChannel.Ios.Aps.Alert = Alert{
Title: title,
Body: body,
}
pushReq.PushChannel.Android = &Android{}
pushReq.PushChannel.Android.Ups.Notification = Notification{
Title: title,
Body: body,
ClickType: "startapp",
}
pushReq.PushChannel.Android.Ups.Options = Options{
HW: struct {
DefaultSound bool `json:"/message/android/notification/default_sound"`
ChannelID string `json:"/message/android/notification/channel_id"`
Sound string `json:"/message/android/notification/sound"`
Importance string `json:"/message/android/notification/importance"`
}{ChannelID: "RingRing4", Sound: "/raw/ring001", Importance: "NORMAL"},
XM: struct {
ChannelID string `json:"/extra.channel_id"`
}{ChannelID: "high_system"},
VV: struct {
Classification int "json:\"/classification\""
}{
Classification: 1,
},
}
}
2022-04-11 14:41:09 +08:00
func (g *Getui) getTokenAndSave2Redis(operationID string) (token string, err error) {
token, expireTime, err := g.Auth(operationID, time.Now().UnixNano()/1e6)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
log.NewDebug(operationID, "getui", utils.GetSelfFuncName(), token, expireTime, err)
err = db.DB.SetGetuiToken(token, 60*60*23)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
return token, nil
}
2022-12-21 16:46:16 +08:00
func (g *Getui) GetTaskIDAndSave2Redis(operationID, token string, pushReq PushReq) (taskID string, err error) {
2022-12-21 18:03:39 +08:00
ttl := int64(1000 * 60 * 60 * 24)
2022-12-22 11:10:19 +08:00
pushReq.Settings = &Settings{TTL: &ttl}
2022-12-21 16:46:16 +08:00
taskID, err = g.GetTaskID(operationID, token, pushReq)
if err != nil {
return "", utils.Wrap(err, "GetTaskIDAndSave2Redis failed")
}
err = db.DB.SetGetuiTaskID(taskID, 60*60*23)
if err != nil {
return "", utils.Wrap(err, "Auth failed")
}
return token, nil
}