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,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
}