Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
package(default_visibility = ["//visibility:public"])
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/common/openplatform/encoding:all-srcs",
"//app/common/openplatform/geetest:all-srcs",
"//app/common/openplatform/random:all-srcs",
],
tags = ["automanaged"],
)

View File

@@ -0,0 +1,10 @@
# v1.0.0
1.添加随机数方法
2.判断随机数位数
3.增加审核人
# v1.0.1
1. 增加加密方法
# v1.0.2
1. 增加极验sdk

View File

@@ -0,0 +1,8 @@
# Owner
liuzhan
huangshancheng
# Author
# Reviewer
qiuliang

View File

@@ -0,0 +1,11 @@
# See the OWNERS docs at https://go.k8s.io/owners
approvers:
- huangshancheng
- liuzhan
labels:
- common
- library
- openplatform
reviewers:
- qiuliang

View File

@@ -0,0 +1,13 @@
# openplatform-common
# 项目简介
1.开放平台公共模块
# 编译环境
# 依赖包
# 编译执行

View File

@@ -0,0 +1,38 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["encoding_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = ["//vendor/github.com/smartystreets/goconvey/convey:go_default_library"],
)
go_library(
name = "go_default_library",
srcs = ["encoding.go"],
importpath = "go-common/app/common/openplatform/encoding",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,105 @@
package encoding
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
"errors"
)
//EncryptConfig 加密配置
type EncryptConfig struct {
Key string
IV string
}
//JSON JSON格式化增加对[]byte的支持
func JSON(o interface{}) string {
var v []byte
switch o1 := o.(type) {
case [][]byte:
o2 := make([]interface{}, len(o1))
for i := 0; i < len(o1); i++ {
o2[i] = string(o1[i])
}
v, _ = json.Marshal(o2)
case []interface{}:
for i := 0; i < len(o1); i++ {
if b, ok := o1[i].([]byte); ok {
o1[i] = string(b)
}
}
v, _ = json.Marshal(o1)
default:
v, _ = json.Marshal(o)
}
if len(v) > 0 {
return string(v)
}
return ""
}
//填充到BlockSize整数倍长度如果正好就是对的长度再多填充一个BlockSize长度
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)
if length == 0 {
return []byte{0}, nil
}
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
}
//Encrypt 加密
func Encrypt(text string, c *EncryptConfig) (string, error) {
if "" == text {
return "", nil
}
block, err := aes.NewCipher([]byte(c.Key))
if err != nil {
return "", err
}
msg := pad([]byte(text))
ciphertext := make([]byte, len(msg))
mode := cipher.NewCBCEncrypter(block, []byte(c.IV))
mode.CryptBlocks(ciphertext, msg)
finalMsg := base64.StdEncoding.EncodeToString(ciphertext)
return finalMsg, nil
}
//Decrypt 解密
func Decrypt(text string, c *EncryptConfig) (string, error) {
if "" == text {
return "", nil
}
block, err := aes.NewCipher([]byte(c.Key))
if err != nil {
return "", err
}
decodedMsg, err := base64.StdEncoding.DecodeString(text)
if err != nil {
return "", err
}
if (len(decodedMsg) % aes.BlockSize) != 0 {
return "", errors.New("blocksize must be multipe of decoded message length")
}
msg := decodedMsg
mode := cipher.NewCBCDecrypter(block, []byte(c.IV))
mode.CryptBlocks(msg, msg)
unpadMsg, err := unpad(msg)
if err != nil {
return "", err
}
return string(unpadMsg), nil
}

View File

@@ -0,0 +1,28 @@
package encoding
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
var c = &EncryptConfig{Key: "bilibili_key_GYl", IV: "biliBiliIv123456"}
const (
plat = "13291831554"
enc = "dWLVOucFkBBmmXmTm6so5A=="
)
func TestEncrypt(t *testing.T) {
convey.Convey("Encrypt", t, func() {
s, _ := Encrypt(plat, c)
convey.So(s, convey.ShouldEqual, enc)
})
}
func TestDecrypt(t *testing.T) {
convey.Convey("Decrypt", t, func() {
s, _ := Decrypt(enc, c)
convey.So(s, convey.ShouldEqual, plat)
})
}

View File

@@ -0,0 +1,19 @@
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/common/openplatform/geetest/conf:all-srcs",
"//app/common/openplatform/geetest/dao:all-srcs",
"//app/common/openplatform/geetest/model:all-srcs",
"//app/common/openplatform/geetest/service:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,28 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["conf.go"],
importpath = "go-common/app/common/openplatform/geetest/conf",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,48 @@
package conf
import (
"encoding/json"
"io/ioutil"
)
// Conf info.
var (
Conf *Config
)
// Config struct.
type Config struct {
// httpClinet
HTTPClient *HTTPClient
// host
Host *Host
// Secret
Secret *Secret
}
// HTTPClient conf.
type HTTPClient struct {
Dial int64
KeepAlive int64
}
// Host conf.
type Host struct {
Geetest string
}
// Secret of Geetest
type Secret struct {
CaptchaID string
PrivateKey string
}
// Init conf.
func Init() (err error) {
bs, err := ioutil.ReadFile("config.json")
if err != nil {
return
}
err = json.Unmarshal(bs, &Conf)
return
}

View File

@@ -0,0 +1,16 @@
{
"host":
{
"geetest":"http://api.geetest.com"
},
"httpclient":
{
"dial":1,
"keepAlive":1
},
"secret":
{
"captchaId":"",
"privateKey":""
}
}

View File

@@ -0,0 +1,29 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["geetest.go"],
importpath = "go-common/app/common/openplatform/geetest/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = ["//app/common/openplatform/geetest/model: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"],
)

View File

@@ -0,0 +1,133 @@
package dao
import (
"context"
"crypto/tls"
"encoding/json"
"errors"
"go-common/app/common/openplatform/geetest/model"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
_register = "http://api.geetest.com/register.php"
_validate = "http://api.geetest.com/validate.php"
)
// PreProcess preprocessing the geetest and get to challenge
func PreProcess(c context.Context, mid int64, ip, clientType string, newCaptcha int, captchaID string) (challenge string, err error) {
var (
bs []byte
params url.Values
)
params = url.Values{}
params.Set("user_id", strconv.FormatInt(mid, 10))
params.Set("new_captcha", strconv.Itoa(newCaptcha))
params.Set("client_type", clientType)
params.Set("ip_address", ip)
params.Set("gt", captchaID)
if bs, err = Get(c, _register, params); err != nil {
return
}
if len(bs) != 32 {
return
}
challenge = string(bs)
return
}
// Validate recheck the challenge code and get to seccode
func Validate(c context.Context, challenge, seccode, clientType, ip, captchaID string, mid int64) (res *model.ValidateRes, err error) {
var (
bs []byte
params url.Values
)
params = url.Values{}
params.Set("seccode", seccode)
params.Set("challenge", challenge)
params.Set("captchaid", captchaID)
params.Set("client_type", clientType)
params.Set("ip_address", ip)
params.Set("json_format", "1")
params.Set("sdk", "golang_3.0.0")
params.Set("user_id", strconv.FormatInt(mid, 10))
params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
if bs, err = Post(c, _validate, params); err != nil {
return
}
if err = json.Unmarshal(bs, &res); err != nil {
return
}
return
}
// NewRequest new a http request.
func NewRequest(method, uri string, params url.Values) (req *http.Request, err error) {
if method == "GET" {
req, err = http.NewRequest(method, uri+"?"+params.Encode(), nil)
} else {
req, err = http.NewRequest(method, uri, strings.NewReader(params.Encode()))
}
if err != nil {
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return
}
// Do handler request
func Do(c context.Context, req *http.Request) (body []byte, err error) {
var res *http.Response
dialer := &net.Dialer{
Timeout: time.Duration(1 * int64(time.Second)),
KeepAlive: time.Duration(1 * int64(time.Second)),
}
transport := &http.Transport{
DialContext: dialer.DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{
Transport: transport,
}
req = req.WithContext(c)
if res, err = client.Do(req); err != nil {
return
}
defer res.Body.Close()
if res.StatusCode >= http.StatusInternalServerError {
err = errors.New("http status code 5xx")
return
}
if body, err = ioutil.ReadAll(res.Body); err != nil {
return
}
return
}
// Get client.Get send GET request
func Get(c context.Context, uri string, params url.Values) (body []byte, err error) {
req, err := NewRequest("GET", uri, params)
if err != nil {
return
}
body, err = Do(c, req)
return
}
// Post client.Get send POST request
func Post(c context.Context, uri string, params url.Values) (body []byte, err error) {
req, err := NewRequest("POST", uri, params)
if err != nil {
return
}
body, err = Do(c, req)
return
}

View File

@@ -0,0 +1,28 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["geetest.go"],
importpath = "go-common/app/common/openplatform/geetest/model",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,14 @@
package model
//ProcessRes 拉起返回值
type ProcessRes struct {
Success int `json:"success"`
CaptchaID string `json:"gt"`
Challenge string `json:"challenge"`
NewCaptcha int `json:"new_captcha"`
}
//ValidateRes 验证返回值
type ValidateRes struct {
Seccode string `json:"seccode"`
}

View File

@@ -0,0 +1,32 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["geetest.go"],
importpath = "go-common/app/common/openplatform/geetest/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/common/openplatform/geetest/dao:go_default_library",
"//app/common/openplatform/geetest/model: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"],
)

View File

@@ -0,0 +1,53 @@
package service
import (
"context"
"crypto/md5"
"encoding/hex"
"go-common/app/common/openplatform/geetest/dao"
"go-common/app/common/openplatform/geetest/model"
"math/rand"
"strconv"
)
// PreProcess preprocessing the geetest and get to challenge
func PreProcess(c context.Context, mid int64, newCaptcha int, ip, clientType, captchaID, privateKey string) (res *model.ProcessRes, err error) {
var pre string
res = &model.ProcessRes{}
res.CaptchaID = captchaID
res.NewCaptcha = newCaptcha
if pre, err = dao.PreProcess(c, mid, ip, clientType, newCaptcha, captchaID); err != nil || pre == "" {
randOne := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
randTwo := md5.Sum([]byte(strconv.Itoa(rand.Intn(100))))
challenge := hex.EncodeToString(randOne[:]) + hex.EncodeToString(randTwo[:])[0:2]
res.Challenge = challenge
return
}
res.Success = 1
slice := md5.Sum([]byte(pre + privateKey))
res.Challenge = hex.EncodeToString(slice[:])
return
}
// Validate recheck the challenge code and get to seccode
func Validate(c context.Context, challenge, validate, seccode, clientType, ip, captchaID, privateKey string, success int, mid int64) (stat bool) {
if len(validate) != 32 {
return
}
if success != 1 {
slice := md5.Sum([]byte(challenge))
stat = hex.EncodeToString(slice[:]) == validate
return
}
slice := md5.Sum([]byte(privateKey + "geetest" + challenge))
if hex.EncodeToString(slice[:]) != validate {
return
}
res, err := dao.Validate(c, challenge, seccode, clientType, ip, captchaID, mid)
if err != nil {
return
}
slice = md5.Sum([]byte(seccode))
stat = hex.EncodeToString(slice[:]) == res.Seccode
return
}

View File

@@ -0,0 +1,28 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = ["uniqid.go"],
importpath = "go-common/app/common/openplatform/random",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,35 @@
package random
import (
"math"
"math/rand"
"time"
)
var (
rnd *rand.Rand
ch chan int64
)
func init() {
rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
ch = make(chan int64, 1000)
go randomBase(ch)
}
func randomBase(c chan int64) {
for {
c <- rnd.Int63()
}
}
//Uniqid 随机数length是需要返回的长度只支持10~19位
func Uniqid(length int) int64 {
if length < 10 || length > 19 {
return 0
}
prefix := (time.Now().UnixNano() / 100000000) & 0x3fffffff
cut := int64(math.Pow10(length - 9))
suffix := <-ch % cut
return prefix*cut + suffix
}

View File

@@ -0,0 +1,11 @@
#!/usr/bin/env bash
#替换当前目录下文件中Id,Url这种不合规的字符
replacements="Id Url Sku Uid"
for i in $replacements; do
r=`echo $i|tr 'a-z' 'A-Z'`
find . -name "*.go"|xargs sed -ibak -E 's/'$i'(s?)([[:>:]]|[A-Z_])/'$r'\1\2/g'
done
find . -name "*.gobak"|xargs rm

View File

@@ -0,0 +1,107 @@
#!/usr/bin/env bash
#自动配置环境变量并使用测试配置运行go run或test加参数 -t可以项目任一目录执行都可以自动找到并运行对应的cmd/main.go
export DEPLOY_ENV=uat
export ZONE=sh001
export APP_ID
export APP_ROOT
grpcPort=9000
httpPort=8000
perfPort=2000
portOffset=0
getApp(){
declare -a dirs
n=0
wd=$PWD
depts=4
while true;do
cur=`basename $PWD`
if [ "$cur" == "" ] || [ "$cur" == "/" ]; then
break
fi
dirs[n]=$cur
if [ "${dirs[n]}" == "go-common" ] && [ "${dirs[n-1]}" == "app" ]; then
if [ $n -lt $depts ]; then
break
fi
for((i=n-1;i>=n-depts+1;i--));do
cd "${dirs[i]}"
done
export APP_ID=${dirs[n-depts]}
export APP_ROOT=$PWD/$APP_ID
portOffset=`ls -l|grep ^d|awk '{if($NF=="'$APP_ID'")print FNR}'`
cd $wd
return 0
fi
let n=n+1
cd ..
done
echo "must be run in go-common app directory" >&2
exit 1
}
getApp
let grpcPort=grpcPort+portOffset
let httpPort=httpPort+portOffset
let perfPort=perfPort+portOffset
export GRPC="tcp://0.0.0.0:"$grpcPort"/?timeout=1s&idle_timeout=60s"
export HTTP="tcp://0.0.0.0:"$httpPort"/?timeout=1s"
export HTTP_PERF="tcp://0.0.0.0:$perfPort"
conf=`find $APP_ROOT/cmd -name "*.toml"`
if [ ! -f "$conf" ]; then
echo "toml file not exist"
exit 1
fi
logdir=`sed -n '/^\[log\]/,$p' $conf|grep -m1 'dir *='|awk -F'=' '{print $2}'|cut -d'"' -f2`
if [ -n "$logdir" ] && [ ! -d "$logdir" ];then
mkdir -p "$logdir"
fi
while getopts t o; do
case "$o" in
t) test=1;;
esac
done
shift $((OPTIND-1))
target=$1
#运行main.go
if [ -z "$test" ]; then
if [ -z "$target" ]; then
target=$APP_ROOT/cmd/main.go
fi
echo "running "$APP_ID" on grpc port:"$grpcPort" and http port:"$httpPort
go run "$target" -conf "$conf"
exit 0
fi
methods=""
files=""
for i in $@;do
if echo $i|grep '^[a-zA-Z0-9_]\+$'>/dev/null; then
methods=$methods$i" "
else
files=$files$i" "
fi
done
if [ -n "$methods" ]; then
for m in $methods; do
go test -v -run $m -conf "$conf"
done
fi
if [ -n "$files" ];then
for f in $files; do
base=`echo $f|sed 's/_test\.go//'`
if [ "$base" != "$f" ] && ! echo ' '$files' '|grep ' '$base'\.go '>/dev/null; then
files=$files$base".go "
fi
done
go test -v $files -conf "$conf"
fi