139 lines
3.2 KiB
Go
139 lines
3.2 KiB
Go
package plumcrypto
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sandc/pkg/bhttp"
|
|
"strings"
|
|
)
|
|
|
|
// PlumResData plum加解密返回数据
|
|
type PlumResData struct {
|
|
Data PlumData `json:"data,omitempty"`
|
|
Success bool `json:"success,omitempty"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// PlumData plum加解密返回数据data
|
|
type PlumData struct {
|
|
Key string `json:"key,omitempty"`
|
|
Val string `json:"val,omitempty"`
|
|
}
|
|
|
|
// PlumReq plum请求bdoy参数
|
|
type PlumReq struct {
|
|
Pkg string `json:"pkg"`
|
|
FirebaseAppId string `json:"firebase_app_id"`
|
|
Key string `json:"key"`
|
|
Val string `json:"val"`
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
// PlumCrypto plum加解密
|
|
type PlumCrypto struct {
|
|
encryptUrl string
|
|
decryptUrl string
|
|
token string
|
|
}
|
|
|
|
// supportParams 支持的plum参数强制
|
|
const supportParams string = ",_ads_config,_pop_config,"
|
|
|
|
// NewPlumCrypto
|
|
func NewPlumCrypto(plumCryptUrl, token string) *PlumCrypto {
|
|
encryptUrl := fmt.Sprintf("%s/encrypt", plumCryptUrl)
|
|
decryptUrl := fmt.Sprintf("%s/decrypt", plumCryptUrl)
|
|
return &PlumCrypto{encryptUrl: encryptUrl, decryptUrl: decryptUrl, token: token}
|
|
}
|
|
|
|
// Encrypt 对plum数据进行加密
|
|
func (a *PlumCrypto) Encrypt(appId, packageName, key, value string) (*PlumData, error) {
|
|
if !strings.Contains(supportParams, fmt.Sprintf(",%s,", key)) {
|
|
return nil, fmt.Errorf("不支持的参数: %s", key)
|
|
}
|
|
x, err := bhttp.NewBhttpClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
x.SetHeader("Content-Type", "application/json")
|
|
req := PlumReq{
|
|
Pkg: packageName,
|
|
FirebaseAppId: appId,
|
|
Key: key,
|
|
Val: value,
|
|
Token: a.token,
|
|
}
|
|
reqBody, _ := json.Marshal(req)
|
|
x.SetBody(reqBody)
|
|
fmt.Println("reqBody: ", string(reqBody))
|
|
if a.encryptUrl == "" {
|
|
return nil, fmt.Errorf("加密url丢失")
|
|
}
|
|
fmt.Println("a.encryptUrl: ", a.encryptUrl)
|
|
res, err := x.DoPost(a.encryptUrl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var info PlumResData
|
|
err = json.Unmarshal(res, &info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fmt.Println(string(res))
|
|
|
|
if !info.Success {
|
|
return nil, fmt.Errorf(info.Error)
|
|
}
|
|
|
|
if info.Data.Val == "" {
|
|
return nil, fmt.Errorf("plum加密失败: %s, 请输入有效的Json数据", key)
|
|
}
|
|
|
|
return &info.Data, nil
|
|
}
|
|
|
|
// Decrypt 对WJ数据进行加密
|
|
func (a *PlumCrypto) Decrypt(appId, packageName, key, value string) (*PlumData, error) {
|
|
x, err := bhttp.NewBhttpClient()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
x.SetHeader("Content-Type", "application/json")
|
|
req := PlumReq{
|
|
Pkg: packageName,
|
|
FirebaseAppId: appId,
|
|
Key: key,
|
|
Val: value,
|
|
Token: a.token,
|
|
}
|
|
reqBody, _ := json.Marshal(req)
|
|
fmt.Println("reqBody: ", string(reqBody))
|
|
x.SetBody(reqBody)
|
|
if a.decryptUrl == "" {
|
|
return nil, fmt.Errorf("解密url丢失")
|
|
}
|
|
|
|
res, err := x.DoPost(a.decryptUrl)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var info PlumResData
|
|
err = json.Unmarshal(res, &info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if !info.Success {
|
|
return nil, fmt.Errorf(info.Error)
|
|
}
|
|
|
|
if !strings.Contains(supportParams, fmt.Sprintf(",%s,", info.Data.Key)) {
|
|
return nil, fmt.Errorf("不支持的参数: %s", info.Data.Key)
|
|
}
|
|
|
|
return &info.Data, nil
|
|
}
|