Compare commits
4 Commits
fix-validate
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 75efc7f189 | |||
| af5f206bd8 | |||
| 5c6eb77b42 | |||
| 0f6d084c67 |
+21
-18
@@ -94,6 +94,7 @@ type RuntimeObject struct {
|
||||
IgnoreSSL *bool `json:"ignoreSSL" xml:"ignoreSSL"`
|
||||
ReadTimeout *int `json:"readTimeout" xml:"readTimeout"`
|
||||
ConnectTimeout *int `json:"connectTimeout" xml:"connectTimeout"`
|
||||
IdleTimeout *int `json:"idleTimeout" xml:"idleTimeout"`
|
||||
LocalAddr *string `json:"localAddr" xml:"localAddr"`
|
||||
HttpProxy *string `json:"httpProxy" xml:"httpProxy"`
|
||||
HttpsProxy *string `json:"httpsProxy" xml:"httpsProxy"`
|
||||
@@ -114,7 +115,7 @@ type RuntimeObject struct {
|
||||
|
||||
func (r *RuntimeObject) getClientTag(domain string) string {
|
||||
return strconv.FormatBool(BoolValue(r.IgnoreSSL)) + strconv.Itoa(IntValue(r.ReadTimeout)) +
|
||||
strconv.Itoa(IntValue(r.ConnectTimeout)) + StringValue(r.LocalAddr) + StringValue(r.HttpProxy) +
|
||||
strconv.Itoa(IntValue(r.ConnectTimeout)) + strconv.Itoa(IntValue(r.IdleTimeout)) + StringValue(r.LocalAddr) + StringValue(r.HttpProxy) +
|
||||
StringValue(r.HttpsProxy) + StringValue(r.NoProxy) + StringValue(r.Socks5Proxy) + StringValue(r.Socks5NetWork) + domain
|
||||
}
|
||||
|
||||
@@ -128,6 +129,7 @@ func NewRuntimeObject(runtime map[string]interface{}) *RuntimeObject {
|
||||
IgnoreSSL: TransInterfaceToBool(runtime["ignoreSSL"]),
|
||||
ReadTimeout: TransInterfaceToInt(runtime["readTimeout"]),
|
||||
ConnectTimeout: TransInterfaceToInt(runtime["connectTimeout"]),
|
||||
IdleTimeout: TransInterfaceToInt(runtime["idleTimeout"]),
|
||||
LocalAddr: TransInterfaceToString(runtime["localAddr"]),
|
||||
HttpProxy: TransInterfaceToString(runtime["httpProxy"]),
|
||||
HttpsProxy: TransInterfaceToString(runtime["httpsProxy"]),
|
||||
@@ -321,7 +323,7 @@ func DoRequest(request *Request, runtimeObject *RuntimeObject) (response *Respon
|
||||
if !defaultClient.ifInit || defaultClient.httpClient.Transport == nil {
|
||||
defaultClient.httpClient.Transport = trans
|
||||
}
|
||||
defaultClient.httpClient.Timeout = time.Duration(IntValue(runtimeObject.ReadTimeout)) * time.Millisecond
|
||||
defaultClient.httpClient.Timeout = time.Duration(IntValue(runtimeObject.ConnectTimeout)+IntValue(runtimeObject.ReadTimeout)) * time.Millisecond
|
||||
defaultClient.ifInit = true
|
||||
defaultClient.Unlock()
|
||||
}
|
||||
@@ -440,7 +442,7 @@ func DoRequestWithCtx(ctx context.Context, request *Request, runtimeObject *Runt
|
||||
if !defaultClient.ifInit || defaultClient.httpClient.Transport == nil {
|
||||
defaultClient.httpClient.Transport = trans
|
||||
}
|
||||
defaultClient.httpClient.Timeout = time.Duration(IntValue(runtimeObject.ReadTimeout)) * time.Millisecond
|
||||
defaultClient.httpClient.Timeout = time.Duration(IntValue(runtimeObject.ConnectTimeout)+IntValue(runtimeObject.ReadTimeout)) * time.Millisecond
|
||||
defaultClient.ifInit = true
|
||||
defaultClient.Unlock()
|
||||
}
|
||||
@@ -502,6 +504,7 @@ func DoRequestWithCtx(ctx context.Context, request *Request, runtimeObject *Runt
|
||||
|
||||
func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, error) {
|
||||
trans := new(http.Transport)
|
||||
trans.ResponseHeaderTimeout = time.Duration(IntValue(runtime.ReadTimeout)) * time.Millisecond
|
||||
httpProxy, err := getHttpProxy(StringValue(req.Protocol), StringValue(req.Domain), runtime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -555,7 +558,7 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
Password: password,
|
||||
}
|
||||
}
|
||||
dialer, err := proxy.SOCKS5(strings.ToLower(StringValue(runtime.Socks5NetWork)), socks5Proxy.String(), auth,
|
||||
dialer, err := proxy.SOCKS5(strings.ToLower(StringValue(runtime.Socks5NetWork)), socks5Proxy.Host, auth,
|
||||
&net.Dialer{
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Millisecond,
|
||||
DualStack: true,
|
||||
@@ -573,6 +576,9 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
trans.MaxIdleConns = IntValue(runtime.MaxIdleConns)
|
||||
trans.MaxIdleConnsPerHost = IntValue(runtime.MaxIdleConns)
|
||||
}
|
||||
if runtime.IdleTimeout != nil && *runtime.IdleTimeout > 0 {
|
||||
trans.IdleConnTimeout = time.Duration(IntValue(runtime.IdleTimeout)) * time.Millisecond
|
||||
}
|
||||
return trans, nil
|
||||
}
|
||||
|
||||
@@ -691,7 +697,7 @@ func getSocks5Proxy(runtime *RuntimeObject) (proxy *url.URL, err error) {
|
||||
func getLocalAddr(localAddr string) (addr *net.TCPAddr) {
|
||||
if localAddr != "" {
|
||||
addr = &net.TCPAddr{
|
||||
IP: []byte(localAddr),
|
||||
IP: net.ParseIP(localAddr),
|
||||
}
|
||||
}
|
||||
return addr
|
||||
@@ -699,20 +705,17 @@ func getLocalAddr(localAddr string) (addr *net.TCPAddr) {
|
||||
|
||||
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{
|
||||
IP: []byte(StringValue(runtime.LocalAddr)),
|
||||
}
|
||||
return (&net.Dialer{
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Second,
|
||||
DualStack: true,
|
||||
LocalAddr: netAddr,
|
||||
}).DialContext(ctx, network, address)
|
||||
timeout := time.Duration(IntValue(runtime.ConnectTimeout)) * time.Millisecond
|
||||
dialer := &net.Dialer{
|
||||
Timeout: timeout,
|
||||
Resolver: &net.Resolver{
|
||||
PreferGo: false,
|
||||
},
|
||||
}
|
||||
return (&net.Dialer{
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Second,
|
||||
DualStack: true,
|
||||
}).DialContext(ctx, network, address)
|
||||
if runtime.LocalAddr != nil && StringValue(runtime.LocalAddr) != "" {
|
||||
dialer.LocalAddr = getLocalAddr(StringValue(runtime.LocalAddr))
|
||||
}
|
||||
return dialer.DialContext(ctx, network, address)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -732,12 +732,37 @@ func Test_DoRequest(t *testing.T) {
|
||||
return mockResponse(200, ``, nil)
|
||||
}
|
||||
}
|
||||
// Test socks5 proxy with user:password@host format
|
||||
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@ecs.aliyun.com"
|
||||
runtimeObj["localAddr"] = "127.0.0.1"
|
||||
resp, err = DoRequest(request, NewRuntimeObject(runtimeObj))
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
// Test socks5 proxy with explicit port
|
||||
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@ecs.aliyun.com:1080"
|
||||
resp, err = DoRequest(request, NewRuntimeObject(runtimeObj))
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
// Test socks5 proxy without authentication
|
||||
runtimeObj["socks5Proxy"] = "socks5://proxy.example.com:1080"
|
||||
resp, err = DoRequest(request, NewRuntimeObject(runtimeObj))
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
// Test socks5 proxy with IPv6 address
|
||||
runtimeObj["socks5Proxy"] = "socks5://[::1]:1080"
|
||||
resp, err = DoRequest(request, NewRuntimeObject(runtimeObj))
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
// Test socks5 proxy with IPv6 and authentication
|
||||
runtimeObj["socks5Proxy"] = "socks5://user:pass@[2001:db8::1]:1080"
|
||||
resp, err = DoRequest(request, NewRuntimeObject(runtimeObj))
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
runtimeObj["key"] = "private rsa key"
|
||||
runtimeObj["cert"] = "private certification"
|
||||
runtimeObj["ca"] = "private ca"
|
||||
@@ -1506,3 +1531,49 @@ func TestForceUint64(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Test_getLocalAddr(t *testing.T) {
|
||||
// Test empty address
|
||||
addr := getLocalAddr("")
|
||||
utils.AssertNil(t, addr)
|
||||
|
||||
// Test valid IPv4 address
|
||||
addr = getLocalAddr("127.0.0.1")
|
||||
utils.AssertNotNil(t, addr)
|
||||
utils.AssertEqual(t, "127.0.0.1", addr.IP.String())
|
||||
|
||||
// Test valid IPv6 address
|
||||
addr = getLocalAddr("::1")
|
||||
utils.AssertNotNil(t, addr)
|
||||
utils.AssertEqual(t, "::1", addr.IP.String())
|
||||
|
||||
// Test invalid IP
|
||||
addr = getLocalAddr("invalid")
|
||||
utils.AssertNotNil(t, addr)
|
||||
utils.AssertNil(t, addr.IP)
|
||||
}
|
||||
|
||||
func Test_setDialContext(t *testing.T) {
|
||||
runtime := &RuntimeObject{
|
||||
ConnectTimeout: Int(1000),
|
||||
LocalAddr: String("127.0.0.1"),
|
||||
}
|
||||
dialerFunc := setDialContext(runtime)
|
||||
utils.AssertNotNil(t, dialerFunc)
|
||||
}
|
||||
|
||||
func Test_TimeoutLogic(t *testing.T) {
|
||||
runtime := &RuntimeObject{
|
||||
ConnectTimeout: Int(1000),
|
||||
ReadTimeout: Int(2000),
|
||||
}
|
||||
|
||||
req := &Request{
|
||||
Protocol: String("http"),
|
||||
Domain: String("localhost"),
|
||||
}
|
||||
trans, err := getHttpTransport(req, runtime)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertNotNil(t, trans)
|
||||
utils.AssertEqual(t, time.Duration(2000)*time.Millisecond, trans.ResponseHeaderTimeout)
|
||||
}
|
||||
|
||||
+308
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
+15
-16
@@ -386,7 +386,7 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
if !defaultClient.ifInit || defaultClient.httpClient.Transport == nil {
|
||||
defaultClient.httpClient.Transport = trans
|
||||
}
|
||||
defaultClient.httpClient.Timeout = time.Duration(IntValue(runtimeObject.ReadTimeout)) * time.Millisecond
|
||||
defaultClient.httpClient.Timeout = time.Duration(IntValue(runtimeObject.ConnectTimeout)+IntValue(runtimeObject.ReadTimeout)) * time.Millisecond
|
||||
defaultClient.ifInit = true
|
||||
defaultClient.Unlock()
|
||||
}
|
||||
@@ -442,6 +442,7 @@ func DoRequest(request *Request, requestRuntime map[string]interface{}) (respons
|
||||
|
||||
func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, error) {
|
||||
trans := new(http.Transport)
|
||||
trans.ResponseHeaderTimeout = time.Duration(IntValue(runtime.ReadTimeout)) * time.Millisecond
|
||||
httpProxy, err := getHttpProxy(StringValue(req.Protocol), StringValue(req.Domain), runtime)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -495,7 +496,7 @@ func getHttpTransport(req *Request, runtime *RuntimeObject) (*http.Transport, er
|
||||
Password: password,
|
||||
}
|
||||
}
|
||||
dialer, err := proxy.SOCKS5(strings.ToLower(StringValue(runtime.Socks5NetWork)), socks5Proxy.String(), auth,
|
||||
dialer, err := proxy.SOCKS5(strings.ToLower(StringValue(runtime.Socks5NetWork)), socks5Proxy.Host, auth,
|
||||
&net.Dialer{
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Millisecond,
|
||||
DualStack: true,
|
||||
@@ -602,7 +603,7 @@ func getSocks5Proxy(runtime *RuntimeObject) (proxy *url.URL, err error) {
|
||||
func getLocalAddr(localAddr string) (addr *net.TCPAddr) {
|
||||
if localAddr != "" {
|
||||
addr = &net.TCPAddr{
|
||||
IP: []byte(localAddr),
|
||||
IP: net.ParseIP(localAddr),
|
||||
}
|
||||
}
|
||||
return addr
|
||||
@@ -610,20 +611,18 @@ func getLocalAddr(localAddr string) (addr *net.TCPAddr) {
|
||||
|
||||
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{
|
||||
IP: []byte(StringValue(runtime.LocalAddr)),
|
||||
}
|
||||
return (&net.Dialer{
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Second,
|
||||
DualStack: true,
|
||||
LocalAddr: netAddr,
|
||||
}).DialContext(ctx, network, address)
|
||||
}
|
||||
return (&net.Dialer{
|
||||
Timeout: time.Duration(IntValue(runtime.ConnectTimeout)) * time.Second,
|
||||
timeout := time.Duration(IntValue(runtime.ConnectTimeout)) * time.Millisecond
|
||||
dialer := &net.Dialer{
|
||||
Timeout: timeout,
|
||||
Resolver: &net.Resolver{
|
||||
PreferGo: false,
|
||||
},
|
||||
DualStack: true,
|
||||
}).DialContext(ctx, network, address)
|
||||
}
|
||||
if runtime.LocalAddr != nil && StringValue(runtime.LocalAddr) != "" {
|
||||
dialer.LocalAddr = getLocalAddr(StringValue(runtime.LocalAddr))
|
||||
}
|
||||
return dialer.DialContext(ctx, network, address)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -560,12 +560,37 @@ func Test_DoRequest(t *testing.T) {
|
||||
return mockResponse(200, ``, nil)
|
||||
}
|
||||
}
|
||||
// Test socks5 proxy with user:password@host format
|
||||
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@ecs.aliyun.com"
|
||||
runtimeObj["localAddr"] = "127.0.0.1"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
// Test socks5 proxy with explicit port
|
||||
runtimeObj["socks5Proxy"] = "socks5://someuser:somepassword@ecs.aliyun.com:1080"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
// Test socks5 proxy without authentication
|
||||
runtimeObj["socks5Proxy"] = "socks5://proxy.example.com:1080"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
// Test socks5 proxy with IPv6 address
|
||||
runtimeObj["socks5Proxy"] = "socks5://[::1]:1080"
|
||||
resp, err = DoRequest(request, runtimeObj)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertEqual(t, "test", StringValue(resp.Headers["tea"]))
|
||||
|
||||
// Test socks5 proxy with IPv6 and authentication
|
||||
runtimeObj["socks5Proxy"] = "socks5://user:pass@[2001:db8::1]:1080"
|
||||
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["ca"] = "private ca"
|
||||
@@ -942,3 +967,49 @@ func Test_TransInt32AndInt(t *testing.T) {
|
||||
b := ToInt32(a)
|
||||
utils.AssertEqual(t, Int32Value(b), int32(10))
|
||||
}
|
||||
|
||||
func Test_getLocalAddr(t *testing.T) {
|
||||
// Test empty address
|
||||
addr := getLocalAddr("")
|
||||
utils.AssertNil(t, addr)
|
||||
|
||||
// Test valid IPv4 address
|
||||
addr = getLocalAddr("127.0.0.1")
|
||||
utils.AssertNotNil(t, addr)
|
||||
utils.AssertEqual(t, "127.0.0.1", addr.IP.String())
|
||||
|
||||
// Test valid IPv6 address
|
||||
addr = getLocalAddr("::1")
|
||||
utils.AssertNotNil(t, addr)
|
||||
utils.AssertEqual(t, "::1", addr.IP.String())
|
||||
|
||||
// Test invalid IP
|
||||
addr = getLocalAddr("invalid")
|
||||
utils.AssertNotNil(t, addr)
|
||||
utils.AssertNil(t, addr.IP)
|
||||
}
|
||||
|
||||
func Test_setDialContext(t *testing.T) {
|
||||
runtime := &RuntimeObject{
|
||||
ConnectTimeout: Int(1000),
|
||||
LocalAddr: String("127.0.0.1"),
|
||||
}
|
||||
dialerFunc := setDialContext(runtime)
|
||||
utils.AssertNotNil(t, dialerFunc)
|
||||
}
|
||||
|
||||
func Test_TimeoutLogic(t *testing.T) {
|
||||
runtime := &RuntimeObject{
|
||||
ConnectTimeout: Int(1000),
|
||||
ReadTimeout: Int(2000),
|
||||
}
|
||||
|
||||
req := &Request{
|
||||
Protocol: String("http"),
|
||||
Domain: String("localhost"),
|
||||
}
|
||||
trans, err := getHttpTransport(req, runtime)
|
||||
utils.AssertNil(t, err)
|
||||
utils.AssertNotNil(t, trans)
|
||||
utils.AssertEqual(t, time.Duration(2000)*time.Millisecond, trans.ResponseHeaderTimeout)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user