Compare commits

...

7 Commits

Author SHA1 Message Date
peze fc13b6ebee fix the big number unmarshal error 2021-05-19 09:42:26 +08:00
wb-wzc505509 6355725204 modify Convert 2021-01-26 17:34:04 +08:00
wb-wzc505509 b6aa048c4a add ToInt and ToInt32 2021-01-18 09:45:18 +08:00
wb-wzc505509 5da4c33a57 rm port 2021-01-06 14:42:54 +08:00
wb-wzc505509 2a316b984d support ignore tag 2021-01-05 09:21:42 +08:00
wb-wzc505509 fa780870a7 fix pattern error 2020-11-30 13:12:50 +08:00
wb-wzc505509 9144ca7f27 support ssl 2020-10-19 13:36:46 +08:00
5 changed files with 1351 additions and 33 deletions
+2
View File
@@ -4,5 +4,7 @@ go 1.14
require (
github.com/alibabacloud-go/debug v0.0.0-20190504072949-9472017b5c68
github.com/json-iterator/go v1.1.10
github.com/modern-go/reflect2 v1.0.1
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b
)
+333
View File
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+53 -25
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"crypto/tls"
"crypto/x509"
"encoding/base64"
"encoding/json"
"errors"
@@ -85,6 +86,9 @@ type RuntimeObject struct {
HttpsProxy *string `json:"httpsProxy" xml:"httpsProxy"`
NoProxy *string `json:"noProxy" xml:"noProxy"`
MaxIdleConns *int `json:"maxIdleConns" xml:"maxIdleConns"`
Key *string `json:"key" xml:"key"`
Cert *string `json:"cert" xml:"cert"`
CA *string `json:"ca" xml:"ca"`
Socks5Proxy *string `json:"socks5Proxy" xml:"socks5Proxy"`
Socks5NetWork *string `json:"socks5NetWork" xml:"socks5NetWork"`
Listener utils.ProgressListener `json:"listener" xml:"listener"`
@@ -123,6 +127,9 @@ func NewRuntimeObject(runtime map[string]interface{}) *RuntimeObject {
MaxIdleConns: TransInterfaceToInt(runtime["maxIdleConns"]),
Socks5Proxy: TransInterfaceToString(runtime["socks5Proxy"]),
Socks5NetWork: TransInterfaceToString(runtime["socks5NetWork"]),
Key: TransInterfaceToString(runtime["key"]),
Cert: TransInterfaceToString(runtime["cert"]),
CA: TransInterfaceToString(runtime["ca"]),
}
if runtime["listener"] != nil {
runtimeObject.Listener = runtime["listener"].(utils.ProgressListener)
@@ -202,7 +209,9 @@ func (err *CastError) Error() string {
// Convert is use convert map[string]interface object to struct
func Convert(in interface{}, out interface{}) error {
byt, _ := json.Marshal(in)
err := json.Unmarshal(byt, out)
decoder := jsonParser.NewDecoder(bytes.NewReader(byt))
decoder.UseNumber();
err := decoder.Decode(&out)
return err
}
@@ -264,20 +273,9 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
request.Protocol = String(strings.ToLower(StringValue(request.Protocol)))
}
if StringValue(request.Protocol) == "http" {
request.Port = Int(80)
} else if StringValue(request.Protocol) == "https" {
request.Port = Int(443)
}
requestURL := ""
request.Domain = request.Headers["host"]
matched, _ := regexp.MatchString(":", StringValue(request.Domain))
if matched {
requestURL = fmt.Sprintf("%s://%s%s", StringValue(request.Protocol), StringValue(request.Domain), StringValue(request.Pathname))
} else {
requestURL = fmt.Sprintf("%s://%s:%d%s", StringValue(request.Protocol), StringValue(request.Domain), IntValue(request.Port), StringValue(request.Pathname))
}
requestURL = fmt.Sprintf("%s://%s%s", StringValue(request.Protocol), StringValue(request.Domain), StringValue(request.Pathname))
queryParams := request.Query
// sort QueryParams by key
q := url.Values{}
@@ -367,8 +365,29 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
if err != nil {
return nil, err
}
trans.TLSClientConfig = &tls.Config{
InsecureSkipVerify: BoolValue(runtime.IgnoreSSL),
if strings.ToLower(*req.Protocol) == "https" &&
runtime.Key != nil && runtime.Cert != nil {
cert, err := tls.X509KeyPair([]byte(StringValue(runtime.Cert)), []byte(StringValue(runtime.Key)))
if err != nil {
return nil, err
}
trans.TLSClientConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: BoolValue(runtime.IgnoreSSL),
}
if runtime.CA != nil {
clientCertPool := x509.NewCertPool()
ok := clientCertPool.AppendCertsFromPEM([]byte(StringValue(runtime.CA)))
if !ok {
return nil, errors.New("Failed to parse root certificate")
}
trans.TLSClientConfig.RootCAs = clientCertPool
}
} else {
trans.TLSClientConfig = &tls.Config{
InsecureSkipVerify: BoolValue(runtime.IgnoreSSL),
}
}
if httpProxy != nil {
trans.Proxy = http.ProxyURL(httpProxy)
@@ -397,7 +416,7 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
&net.Dialer{
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Millisecond,
DualStack: true,
LocalAddr: getLocalAddr(StringValue(runtime.LocalAddr), IntValue(req.Port)),
LocalAddr: getLocalAddr(StringValue(runtime.LocalAddr)),
})
if err != nil {
return nil, err
@@ -405,7 +424,7 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
trans.Dial = dialer.Dial
}
} else {
trans.DialContext = setDialContext(runtime, IntValue(req.Port))
trans.DialContext = setDialContext(runtime)
}
return trans, nil
}
@@ -493,22 +512,20 @@ func getSocks5Proxy(runtime *RuntimeObject) (proxy *url.URL, err error) {
return proxy, err
}
func getLocalAddr(localAddr string, port int) (addr *net.TCPAddr) {
func getLocalAddr(localAddr string) (addr *net.TCPAddr) {
if localAddr != "" {
addr = &net.TCPAddr{
Port: port,
IP: []byte(localAddr),
IP: []byte(localAddr),
}
}
return addr
}
func setDialContext(runtime *RuntimeObject, port int) func(cxt context.Context, net, addr string) (c net.Conn, err error) {
func setDialContext(runtime *RuntimeObject) func(cxt context.Context, net, addr string) (c net.Conn, err error) {
return func(ctx context.Context, network, address string) (net.Conn, error) {
if runtime.LocalAddr != nil && StringValue(runtime.LocalAddr) != "" {
netAddr := &net.TCPAddr{
Port: port,
IP: []byte(StringValue(runtime.LocalAddr)),
IP: []byte(StringValue(runtime.LocalAddr)),
}
return (&net.Dialer{
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Second,
@@ -698,7 +715,9 @@ func structToMap(dataValue reflect.Value) map[string]interface{} {
if !fieldValue.IsValid() || fieldValue.IsNil() {
continue
}
if field.Type.Kind().String() == "struct" {
if field.Type.String() == "io.Reader" || field.Type.String() == "io.Writer" {
continue
} else if field.Type.Kind().String() == "struct" {
out[name] = structToMap(fieldValue)
} else if field.Type.Kind().String() == "ptr" &&
field.Type.Elem().Kind().String() == "struct" {
@@ -931,7 +950,8 @@ func checkRequire(field reflect.StructField, valueField reflect.Value) error {
func checkPattern(field reflect.StructField, valueField reflect.Value, tag string) error {
if valueField.IsValid() && valueField.String() != "" {
value := valueField.String()
if match, _ := regexp.MatchString(tag, value); !match { // Determines whether the parameter value satisfies the regular expression or not, and throws an error
r, _ := regexp.Compile("^" + tag + "$")
if match := r.MatchString(value); !match { // Determines whether the parameter value satisfies the regular expression or not, and throws an error
return errors.New(value + " is not matched " + tag)
}
}
@@ -1081,3 +1101,11 @@ func Prettify(i interface{}) string {
resp, _ := json.MarshalIndent(i, "", " ")
return string(resp)
}
func ToInt(a *int32) *int {
return Int(int(Int32Value(a)))
}
func ToInt32(a *int) *int32 {
return Int32(int32(IntValue(a)))
}
+83 -8
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
"io"
"io/ioutil"
"net/http"
"os"
@@ -50,14 +51,14 @@ type validateTest struct {
Num2 *int `json:"num2,omitempty" maximum:"6"`
Name1 *string `json:"name1,omitempty" maxLength:"4"`
Name2 *string `json:"name2,omitempty" minLength:"2"`
Str *string `json:"str,omitempty" pattern:"^[a-d]*$" maxLength:"4"`
Str *string `json:"str,omitempty" pattern:"[a-d]*" maxLength:"4"`
MaxLength *errMaxLength `json:"MaxLength,omitempty"`
MinLength *errMinLength `json:"MinLength,omitempty"`
Maximum *errMaximum `json:"Maximum,omitempty"`
Minimum *errMinimum `json:"Minimum,omitempty"`
MaxItems *errMaxItems `json:"MaxItems,omitempty"`
MinItems *errMinItems `json:"MinItems,omitempty"`
List []*string `json:"list,omitempty" pattern:"^[a-d]*$" minItems:"2" maxItems:"3" maxLength:"4"`
List []*string `json:"list,omitempty" pattern:"[a-d]*" minItems:"2" maxItems:"3" maxLength:"4"`
}
type errMaxLength struct {
@@ -138,14 +139,16 @@ func TestConvert(t *testing.T) {
utils.AssertEqual(t, "test", string(out.Body))
}
func TestConvertType(t *testing.T) {
func TestConvertType(t *testing.T){
in := map[string]interface{}{
"key": 123,
"body": []byte("test"),
}
out := new(test)
err := Convert(in, &out)
utils.AssertNotNil(t, err)
utils.AssertEqual(t, "json: cannot unmarshal number into Go struct field test.key of type string", err.Error())
utils.AssertNil(t, err)
utils.AssertEqual(t, "123", out.Key)
utils.AssertEqual(t, "test", string(out.Body))
}
func TestRuntimeObject(t *testing.T) {
@@ -240,6 +243,8 @@ type Test struct {
List []string `json:"List,omitempty"`
CastList []CastError `json:"CastList,omitempty"`
CastListPtr []*CastError `json:"CastListPtr,omitempty"`
Reader io.Reader
Inter interface{}
}
func TestToMap(t *testing.T) {
@@ -273,9 +278,12 @@ func TestToMap(t *testing.T) {
Message: String("CastList"),
},
},
Reader: strings.NewReader(""),
Inter: 10,
}
result = ToMap(valid)
utils.AssertEqual(t, "tea", result["Msg"])
utils.AssertNil(t, result["Reader"])
utils.AssertEqual(t, map[string]interface{}{"Message": "message"}, result["Cast"])
utils.AssertEqual(t, []interface{}{"test", ""}, result["ListPtr"])
utils.AssertEqual(t, []interface{}{"list"}, result["List"])
@@ -356,6 +364,40 @@ func Test_GetBackoffTime(t *testing.T) {
utils.AssertEqual(t, true, IntValue(ms) <= 3)
}
var key = `-----BEGIN RSA PRIVATE KEY-----
MIIBPAIBAAJBAN5I1VCLYr2IlTLrFpwUGcnwl8yi6V8Mdw+myxfusNgEWiH/FQ4T
AZsIveiLOz9Gcc8m2mZSxst2qGII00scpiECAwEAAQJBAJZEhnA8yjN28eXKJy68
J/LsQrKEL1+h/ZsHFqTHJ6XfiA0CXjbjPsa4jEbpyilMTSgUyoKdJ512ioeco2n6
xUECIQD/JUHaKSuxz55t3efKdppqfopb92mJ2NuPJgrJI70OCwIhAN8HZ0bzr/4a
DLvYCDUKvOj3GzsV1dtBwWuHBaZEafQDAiEAtTnrel//7z5/U55ow4BW0gmrkQM9
bXIhEZ59zryZzl0CIQDFmBqRCu9eshecCP7kd3n88IjopSTOV4iUypBfyXcRnwIg
eXNxUx+BCu2We36+c0deE2+vizL1s6f5XhE6l4bqtiM=
-----END RSA PRIVATE KEY-----`
var cert = `-----BEGIN CERTIFICATE-----
MIIBvDCCAWYCCQDKjNYQxar0mjANBgkqhkiG9w0BAQsFADBlMQswCQYDVQQGEwJh
czEMMAoGA1UECAwDYXNmMQwwCgYDVQQHDANzYWQxCzAJBgNVBAoMAnNkMQ0wCwYD
VQQLDARxd2VyMQswCQYDVQQDDAJzZjERMA8GCSqGSIb3DQEJARYCd2UwHhcNMjAx
MDE5MDI0MDMwWhcNMzAxMDE3MDI0MDMwWjBlMQswCQYDVQQGEwJhczEMMAoGA1UE
CAwDYXNmMQwwCgYDVQQHDANzYWQxCzAJBgNVBAoMAnNkMQ0wCwYDVQQLDARxd2Vy
MQswCQYDVQQDDAJzZjERMA8GCSqGSIb3DQEJARYCd2UwXDANBgkqhkiG9w0BAQEF
AANLADBIAkEA3kjVUItivYiVMusWnBQZyfCXzKLpXwx3D6bLF+6w2ARaIf8VDhMB
mwi96Is7P0ZxzybaZlLGy3aoYgjTSxymIQIDAQABMA0GCSqGSIb3DQEBCwUAA0EA
ZjePopbFugNK0US1MM48V1S2petIsEcxbZBEk/wGqIzrY4RCFKMtbtPSgTDUl3D9
XePemktG22a54ItVJ5FpcQ==
-----END CERTIFICATE-----`
var ca = `-----BEGIN CERTIFICATE-----
MIIBuDCCAWICCQCLw4OWpjlJCDANBgkqhkiG9w0BAQsFADBjMQswCQYDVQQGEwJm
ZDEMMAoGA1UECAwDYXNkMQswCQYDVQQHDAJxcjEKMAgGA1UECgwBZjEMMAoGA1UE
CwwDc2RhMQswCQYDVQQDDAJmZDESMBAGCSqGSIb3DQEJARYDYXNkMB4XDTIwMTAx
OTAyNDQwNFoXDTIzMDgwOTAyNDQwNFowYzELMAkGA1UEBhMCZmQxDDAKBgNVBAgM
A2FzZDELMAkGA1UEBwwCcXIxCjAIBgNVBAoMAWYxDDAKBgNVBAsMA3NkYTELMAkG
A1UEAwwCZmQxEjAQBgkqhkiG9w0BCQEWA2FzZDBcMA0GCSqGSIb3DQEBAQUAA0sA
MEgCQQCxXZTl5IO61Lqd0fBBOSy7ER1gsdA0LkvflP5HEaQygjecLGfrAtD/DWu0
/sxCcBVnQRoP9Yp0ijHJwgXvBnrNAgMBAAEwDQYJKoZIhvcNAQELBQADQQBJF+/4
DEMilhlFY+o9mqCygFVxuvHtQVhpPS938H2h7/P6pXN65jK2Y5hHefZEELq9ulQe
91iBwaQ4e9racCgP
-----END CERTIFICATE-----`
func Test_DoRequest(t *testing.T) {
origTestHookDo := hookDo
defer func() { hookDo = origTestHookDo }()
@@ -365,7 +407,6 @@ func Test_DoRequest(t *testing.T) {
}
}
request := NewRequest()
request.Port = Int(80)
request.Method = String("TEA TEST")
resp, err := DoRequest(request, nil)
utils.AssertNil(t, resp)
@@ -410,6 +451,32 @@ func Test_DoRequest(t *testing.T) {
resp, err = DoRequest(request, runtimeObj)
utils.AssertNil(t, err)
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
runtimeObj["key"] = "private rsa key"
runtimeObj["cert"] = "private certification"
runtimeObj["ignoreSSL"] = true
resp, err = DoRequest(request, runtimeObj)
utils.AssertNotNil(t, err)
utils.AssertNil(t, resp)
runtimeObj["key"] = key
runtimeObj["cert"] = cert
runtimeObj["ca"] = "private ca"
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@cs.aliyun.com"
_, err = DoRequest(request, runtimeObj)
utils.AssertNotNil(t, err)
runtimeObj["ca"] = ca
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@cs.aliyuncs.com"
resp, err = DoRequest(request, runtimeObj)
utils.AssertNil(t, err)
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
request.Protocol = String("HTTP")
runtimeObj["ignoreSSL"] = false
resp, err = DoRequest(request, runtimeObj)
utils.AssertNil(t, err)
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
}
func Test_DoRequestWithConcurrent(t *testing.T) {
@@ -498,7 +565,7 @@ func Test_getHttpProxy(t *testing.T) {
func Test_SetDialContext(t *testing.T) {
runtime := &RuntimeObject{}
dialcontext := setDialContext(runtime, 80)
dialcontext := setDialContext(runtime)
ctx, cancelFunc := context.WithTimeout(context.Background(), 1*time.Second)
utils.AssertNotNil(t, cancelFunc)
c, err := dialcontext(ctx, "127.0.0.1", "127.0.0.2")
@@ -652,7 +719,7 @@ func Test_validate(t *testing.T) {
str2 := "test"
val.Str = &str2
err = validate(reflect.ValueOf(val))
utils.AssertEqual(t, "test is not matched ^[a-d]*$", err.Error())
utils.AssertEqual(t, "test is not matched [a-d]*", err.Error())
val.Str = &str0
val.List = []*string{&str0}
@@ -720,3 +787,11 @@ func Test_Prettify(t *testing.T) {
str = Prettify(nil)
utils.AssertEqual(t, str, "null")
}
func Test_TransInt32AndInt(t *testing.T) {
a := ToInt(Int32(10))
utils.AssertEqual(t, IntValue(a), 10)
b := ToInt32(a)
utils.AssertEqual(t, Int32Value(b), int32(10))
}