chat-backend/utils/helper.go

85 lines
1.6 KiB
Go

package utils
import (
"crypto/md5"
"fmt"
"reflect"
"sort"
"strings"
)
// GetAddress
// 获取监听地址
//
// @return []string
func GetListenAddress() []string {
address := Conf.Get("init", "addr")
addressArr := strings.Split(address, "://")
if addressArr[0] == "https" {
return []string{addressArr[0], addressArr[1]}
}
var addr string
if len(addressArr) > 1 {
addr = addressArr[1]
} else {
addr = addressArr[0]
}
return []string{"http", addr}
}
// GetProxy
// 获取代理服务器
//
// @return string
func GetProxyServer() string {
proxy := Conf.Get("proxy", "addr")
if proxy == "" {
return ""
}
proxyArr := strings.Split(proxy, "://")
if proxyArr[0] != "http" && proxyArr[0] != "https" {
return "http://" + proxyArr[0]
}
return proxy
}
// Md5
//
// @param text
// @return string
func Md5(text string) string {
hash := md5.New()
hash.Write([]byte(text))
byteData := hash.Sum(nil)
return fmt.Sprintf("%x", byteData)
}
// MakeSign
// 生成签名
//
// @param obj
// @param privateKey
// @return string
func MakeSign(obj interface{}, privateKey string) string {
valueOfObj := reflect.ValueOf(obj)
keysCount := valueOfObj.NumField()
keys := make([]string, keysCount)
for i := 0; i < keysCount; i++ {
keys[i] = valueOfObj.Type().Field(i).Name
}
sort.Strings(keys)
str := ""
currentKey := ""
var currentValue reflect.Value
for j := 0; j < keysCount; j++ {
currentKey = strings.ToLower(keys[j])
currentValue = valueOfObj.FieldByName(keys[j])
str = fmt.Sprintf("%s%s=%v&", str, currentKey, currentValue)
}
return Md5(str + "key=" + privateKey)
}