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,50 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = ["dao.go"],
importpath = "go-common/app/interface/main/creative/dao/geetest",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/geetest:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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 = ["dao_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/geetest:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)

View File

@@ -0,0 +1,137 @@
package geetest
import (
"context"
"crypto/tls"
"go-common/app/interface/main/creative/conf"
"go-common/app/interface/main/creative/model/geetest"
"go-common/library/ecode"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
_register = "/register.php"
_validate = "/validate.php"
)
// Dao is account dao.
type Dao struct {
c *conf.Config
// url
registerURI string
validateURI string
// http client
client *http.Client
clientx *httpx.Client
}
// New new a dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
registerURI: c.Host.Geetest + _register,
validateURI: c.Host.Geetest + _validate,
// http client
client: NewClient(c.HTTPClient),
clientx: httpx.NewClient(c.HTTPClient.Slow),
}
return
}
// PreProcess preprocessing the geetest and get to challenge
func (d *Dao) PreProcess(c context.Context, mid int64, ip, clientType string, newCaptcha int) (challenge string, err error) {
var (
req *http.Request
res *http.Response
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", d.c.Geetest.CaptchaID)
if req, err = http.NewRequest("GET", d.registerURI+"?"+params.Encode(), nil); err != nil {
log.Error("d.preprocess uri(%s) params(%s) error(%v)", d.registerURI, params.Encode(), err)
err = ecode.CreativeGeetestAPIErr
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if res, err = d.client.Do(req); err != nil {
log.Error("client.Do(%s) error(%v)", d.registerURI+"?"+params.Encode(), err)
err = ecode.CreativeGeetestAPIErr
return
}
defer res.Body.Close()
if res.StatusCode >= http.StatusInternalServerError {
log.Error("gtServerErr uri(%s) error(%v)", d.registerURI+"?"+params.Encode(), err)
err = ecode.CreativeGeetestAPIErr
return
}
if bs, err = ioutil.ReadAll(res.Body); err != nil {
log.Error("ioutil.ReadAll(%s) uri(%s) error(%v)", bs, d.registerURI+"?"+params.Encode(), err)
return
}
if len(bs) != 32 {
log.Error("d.preprocess len(%s) the length not equate 32byte", string(bs))
return
}
challenge = string(bs)
return
}
// Validate recheck the challenge code and get to seccode
func (d *Dao) Validate(c context.Context, challenge, seccode, clientType, ip, captchaID string, mid int64) (res *geetest.ValidateRes, err error) {
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))
req, err := http.NewRequest("POST", d.validateURI, strings.NewReader(params.Encode()))
if err != nil {
log.Error("http.NewRequest error(%v) | uri(%s) params(%s)", err, d.validateURI, params.Encode())
err = ecode.CreativeGeetestAPIErr
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
if err = d.clientx.Do(c, req, &res); err != nil {
log.Error("d.client.Do error(%v)", err)
err = ecode.CreativeGeetestAPIErr
return
}
return
}
// NewClient new a http client.
func NewClient(c *conf.HTTPClient) (client *http.Client) {
var (
transport *http.Transport
dialer *net.Dialer
)
dialer = &net.Dialer{
Timeout: time.Duration(c.Slow.Timeout),
KeepAlive: time.Duration(c.Slow.KeepAlive),
}
transport = &http.Transport{
DialContext: dialer.DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client = &http.Client{
Transport: transport,
}
return
}

View File

@@ -0,0 +1,108 @@
package geetest
import (
"context"
"encoding/json"
"flag"
"go-common/app/interface/main/creative/conf"
"go-common/app/interface/main/creative/model/geetest"
"os"
"strings"
"testing"
"github.com/smartystreets/goconvey/convey"
gock "gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative")
flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
flag.Set("tree_id", "2305")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/creative.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
d.clientx.SetTransport(gock.DefaultTransport)
d.client.Transport = gock.DefaultTransport
return r
}
func TestPreProcess(t *testing.T) {
var (
mid int64
ip, clientType, challenge string
newCaptcha int
c = context.TODO()
err error
)
convey.Convey("1", t, func(ctx convey.C) {
defer gock.OffAll()
httpMock("Do", d.registerURI).Reply(-502)
challenge, err = d.PreProcess(c, mid, ip, clientType, newCaptcha)
ctx.Convey("1", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(len(challenge), convey.ShouldEqual, 0)
})
})
convey.Convey("2", t, func(ctx convey.C) {
challenge, err = d.PreProcess(c, mid, ip, clientType, newCaptcha)
ctx.Convey("2", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(len(challenge), convey.ShouldEqual, 32)
})
})
}
func TestValidate(t *testing.T) {
var (
c = context.TODO()
err error
challenge, seccode, clientType, ip, captchaID string
mid int64
res = &geetest.ValidateRes{}
)
convey.Convey("1", t, func(ctx convey.C) {
defer gock.OffAll()
httpMock("POST", d.validateURI).Reply(-502)
res, err = d.Validate(c, challenge, seccode, clientType, ip, captchaID, mid)
ctx.Convey("1", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(res, convey.ShouldBeNil)
})
})
convey.Convey("2", t, func(ctx convey.C) {
defer gock.OffAll()
res = &geetest.ValidateRes{
Seccode: "ok",
}
js, _ := json.Marshal(res)
defer gock.OffAll()
httpMock("Post", d.validateURI).Reply(200).JSON(string(js))
res, err = d.Validate(c, challenge, seccode, clientType, ip, captchaID, mid)
ctx.Convey("2", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
}