Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"aes.go",
"service.go",
"user_act_log.go",
],
importpath = "go-common/app/admin/main/passport/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/passport/conf:go_default_library",
"//app/admin/main/passport/dao:go_default_library",
"//app/admin/main/passport/model:go_default_library",
"//library/database/elastic:go_default_library",
"//library/log:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = ["user_act_log_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/passport/conf:go_default_library",
"//app/admin/main/passport/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,63 @@
package service
import (
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"errors"
)
//func pad(src []byte) []byte {
// padding := aes.BlockSize - len(src)%aes.BlockSize
// padText := bytes.Repeat([]byte{byte(padding)}, padding)
// return append(src, padText...)
//}
func unpad(src []byte) ([]byte, error) {
length := len(src)
unpadding := int(src[length-1])
if unpadding > length {
return nil, errors.New("unpad error. This could happen when incorrect encryption key is used")
}
return src[:(length - unpadding)], nil
}
//func (s *Service) encrypt(text string) (string, error) {
// msg := pad([]byte(text))
// cipherText := make([]byte, aes.BlockSize+len(msg))
// iv := cipherText[:aes.BlockSize]
// if _, err := io.ReadFull(rand.Reader, iv); err != nil {
// return "", err
// }
//
// cfb := cipher.NewCFBEncrypter(s.AESBlock, iv)
// cfb.XORKeyStream(cipherText[aes.BlockSize:], []byte(msg))
// finalMsg := base64.URLEncoding.EncodeToString(cipherText)
// return finalMsg, nil
//}
func (s *Service) decrypt(text string) (string, error) {
decodedMsg, err := base64.URLEncoding.DecodeString(text)
if err != nil {
return "", err
}
if (len(decodedMsg) % aes.BlockSize) != 0 {
return "", errors.New("blocksize must be multipe of decoded message length")
}
iv := decodedMsg[:aes.BlockSize]
msg := decodedMsg[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(s.AESBlock, iv)
cfb.XORKeyStream(msg, msg)
unpadMsg, err := unpad(msg)
if err != nil {
return "", err
}
return string(unpadMsg), nil
}

View File

@@ -0,0 +1,39 @@
package service
import (
"context"
"crypto/aes"
"crypto/cipher"
"go-common/app/admin/main/passport/conf"
"go-common/app/admin/main/passport/dao"
)
// Service struct
type Service struct {
c *conf.Config
dao *dao.Dao
AESBlock cipher.Block
hashSalt []byte
}
// New init
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
dao: dao.New(c),
hashSalt: []byte(c.Encode.Salt),
}
s.AESBlock, _ = aes.NewCipher([]byte(c.Encode.AesKey))
return s
}
// Ping Service
func (s *Service) Ping(c context.Context) (err error) {
return s.dao.Ping(c)
}
// Close Service
func (s *Service) Close() {
s.dao.Close()
}

View File

@@ -0,0 +1,107 @@
package service
import (
"context"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"strconv"
"time"
"go-common/app/admin/main/passport/model"
"go-common/library/database/elastic"
"go-common/library/log"
)
type userLogsExtra struct {
EncryptTel string `json:"tel"`
EncryptEmail string `json:"email"`
}
// UserBindLog User bind log
func (s *Service) UserBindLog(c context.Context, userActLogReq *model.UserBindLogReq) (res *model.UserBindLogRes, err error) {
e := s.dao.EsCli
nowYear := time.Now().Year()
index1 := "log_user_action_54_" + strconv.Itoa(nowYear)
index2 := "log_user_action_54_" + strconv.Itoa(nowYear-1)
r := e.NewRequest("log_user_action").Fields("mid", "str_0", "extra_data", "ctime").Index(index1, index2)
r.Order("ctime", elastic.OrderDesc).Order("mid", elastic.OrderDesc).Pn(userActLogReq.Page).Ps(userActLogReq.Size)
if userActLogReq.Mid != 0 {
r.WhereEq("mid", userActLogReq.Mid)
}
if userActLogReq.Query != "" {
hash := sha1.New()
hash.Write([]byte(userActLogReq.Query))
telHash := base64.StdEncoding.EncodeToString(hash.Sum(s.hashSalt))
r.WhereEq("str_0", telHash)
}
if userActLogReq.Action != "" {
r.WhereEq("action", userActLogReq.Action)
}
if userActLogReq.From != 0 && userActLogReq.To != 0 {
ftm := time.Unix(userActLogReq.From, 0)
sf := ftm.Format("2006-01-02 15:04:05")
ttm := time.Unix(userActLogReq.To, 0)
tf := ttm.Format("2006-01-02 15:04:05")
r.WhereRange("ctime", sf, tf, elastic.RangeScopeLoRo)
}
esres := new(model.EsRes)
if err = r.Scan(context.Background(), &esres); err != nil {
log.Error("userActLogs search error(%v)", err)
}
var users = make([]*model.UserBindLog, 0)
for _, value := range esres.Result {
var email, tel string
//var model.UserBindLog
userLogExtra := userLogsExtra{}
err = json.Unmarshal([]byte(value.ExtraData), &userLogExtra)
if err != nil {
log.Error("cannot convert json(%s) to struct,err(%+v) ", value.ExtraData, err)
continue
}
if userLogExtra.EncryptEmail != "" {
email, err = s.decrypt(userLogExtra.EncryptEmail)
if err != nil {
log.Error("EncryptEmail decode err(%v)", err)
continue
}
}
if userLogExtra.EncryptTel != "" {
tel, err = s.decrypt(userLogExtra.EncryptTel)
if err != nil {
log.Error("EncryptTel decode err(%v)", err)
continue
}
}
ulr := model.UserBindLog{Mid: value.Mid, EMail: email, Phone: tel, Time: value.CTime}
users = append(users, &ulr)
}
res = &model.UserBindLogRes{Page: esres.Page, Result: users}
return
}
// DecryptBindLog decrypt bind log
func (s *Service) DecryptBindLog(c context.Context, reqParams *model.DecryptBindLogParam) (res map[string]string, err error) {
if len(reqParams.EncryptText) == 0 {
return make(map[string]string), nil
}
res = make(map[string]string, len(reqParams.EncryptText))
for _, v := range reqParams.EncryptText {
var tel string
if tel, err = s.decrypt(v); err != nil {
return
}
res[v] = tel
}
return
}

View File

@@ -0,0 +1,26 @@
package service
import (
"context"
"testing"
"go-common/app/admin/main/passport/conf"
"go-common/app/admin/main/passport/model"
"github.com/smartystreets/goconvey/convey"
)
func TestService_DecryptBindLog(t *testing.T) {
config := &conf.Config{
Encode: &conf.Encode{
AesKey: "0123456789abcdef",
Salt: "",
},
}
s := New(config)
convey.Convey("", t, func() {
res, err := s.DecryptBindLog(context.Background(), &model.DecryptBindLogParam{EncryptText: []string{"IsjRu7dmHBY8l7bGf6O3rgDegFvh3cVTgWkf2Bn87Oc="}})
convey.So(err, convey.ShouldBeNil)
convey.So(res["IsjRu7dmHBY8l7bGf6O3rgDegFvh3cVTgWkf2Bn87Oc="], convey.ShouldEqual, "19921218988")
})
}