Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fa780870a7 | |||
| 9144ca7f27 | |||
| 174725cf27 | |||
| 45df60e197 |
+37
-4
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"crypto/x509"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -35,7 +36,7 @@ var hookDo = func(fn func(req *http.Request) (*http.Response, error)) func(req *
|
||||
}
|
||||
|
||||
var basicTypes = []string{
|
||||
"int", "int64", "float32", "float64", "string", "bool", "uint64",
|
||||
"int", "int16", "int64", "int32", "float32", "float64", "string", "bool", "uint64", "uint32", "uint16",
|
||||
}
|
||||
|
||||
// Verify whether the parameters meet the requirements
|
||||
@@ -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)
|
||||
@@ -317,6 +324,10 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
continue
|
||||
} else if key == "host" {
|
||||
httpRequest.Header["Host"] = []string{*value}
|
||||
delete(httpRequest.Header, "host")
|
||||
} else if key == "user-agent" {
|
||||
httpRequest.Header["User-Agent"] = []string{*value}
|
||||
delete(httpRequest.Header, "user-agent")
|
||||
} else {
|
||||
httpRequest.Header[key] = []string{*value}
|
||||
}
|
||||
@@ -363,8 +374,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)
|
||||
@@ -927,7 +959,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)
|
||||
}
|
||||
}
|
||||
|
||||
+64
-3
@@ -50,14 +50,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 {
|
||||
@@ -356,6 +356,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 }()
|
||||
@@ -390,6 +424,7 @@ func Test_DoRequest(t *testing.T) {
|
||||
utils.AssertEqual(t, `Internal error`, err.Error())
|
||||
|
||||
request.Headers["host"] = String("tea-cn-hangzhou.aliyuncs.com:80")
|
||||
request.Headers["user-agent"] = String("test")
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, resp)
|
||||
utils.AssertEqual(t, `Internal error`, err.Error())
|
||||
@@ -409,6 +444,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"
|
||||
resp, 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) {
|
||||
@@ -651,7 +712,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}
|
||||
|
||||
Reference in New Issue
Block a user