2020-08-03 20:48:58 +08:00
|
|
|
package sdk
|
|
|
|
|
|
|
|
import (
|
2020-08-08 15:09:58 +08:00
|
|
|
"bytes"
|
2020-08-03 20:48:58 +08:00
|
|
|
"context"
|
2020-08-04 21:14:06 +08:00
|
|
|
"crypto/tls"
|
2020-08-03 20:48:58 +08:00
|
|
|
"fmt"
|
|
|
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth"
|
2020-08-04 12:23:34 +08:00
|
|
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/auth/credentials"
|
2020-08-04 00:51:19 +08:00
|
|
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/requests"
|
|
|
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/responses"
|
2020-08-06 12:15:51 +08:00
|
|
|
"golib.gaore.com/GaoreGo/gaore-common-sdk-go/sdk/utils"
|
2020-08-03 20:48:58 +08:00
|
|
|
"net"
|
|
|
|
"net/http"
|
2020-08-08 15:09:58 +08:00
|
|
|
"net/http/httputil"
|
2020-08-05 12:14:49 +08:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2020-08-03 20:48:58 +08:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Version = "0.0.1"
|
|
|
|
var defaultConnectTimeout = 5 * time.Second
|
|
|
|
var defaultReadTimeout = 10 * time.Second
|
2020-08-05 17:01:10 +08:00
|
|
|
var defaultDomain = ".gaore.com"
|
2020-08-03 20:48:58 +08:00
|
|
|
var DefaultUserAgent = fmt.Sprintf("GaoreGoSdk (%s;%s) Golang/%s Core/%s", runtime.GOOS, runtime.GOARCH, strings.Trim(runtime.Version(), "go"), Version)
|
|
|
|
|
2020-08-06 15:41:02 +08:00
|
|
|
var debug utils.Debug
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
debug = utils.Init("sdk")
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:48:58 +08:00
|
|
|
type Client struct {
|
|
|
|
Host string
|
|
|
|
httpClient *http.Client
|
|
|
|
isInsecure bool
|
2020-08-04 00:51:19 +08:00
|
|
|
signer auth.Signer
|
2020-08-03 20:48:58 +08:00
|
|
|
readTimeout time.Duration
|
|
|
|
connectTimeout time.Duration
|
|
|
|
config *Config
|
2020-08-05 12:14:49 +08:00
|
|
|
httpProxy string
|
|
|
|
httpsProxy string
|
|
|
|
noProxy string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) GetNoProxy() string {
|
|
|
|
return client.noProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) SetNoProxy(noProxy string) {
|
|
|
|
client.noProxy = noProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) GetHttpsProxy() string {
|
|
|
|
return client.httpsProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) SetHttpsProxy(httpsProxy string) {
|
|
|
|
client.httpsProxy = httpsProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) SetHttpProxy(httpProxy string) {
|
|
|
|
client.httpProxy = httpProxy
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) GetHttpProxy() string {
|
|
|
|
return client.httpProxy
|
2020-08-03 20:48:58 +08:00
|
|
|
}
|
|
|
|
|
2020-08-04 21:14:06 +08:00
|
|
|
func (client *Client) GetHTTPSInsecure() bool {
|
|
|
|
return client.isInsecure
|
|
|
|
}
|
|
|
|
|
2020-08-04 12:23:34 +08:00
|
|
|
func (client *Client) InitClientConfig() (config *Config) {
|
2020-08-04 21:14:06 +08:00
|
|
|
if client.config != nil {
|
|
|
|
return client.config
|
|
|
|
} else {
|
|
|
|
return NewConfig()
|
|
|
|
}
|
2020-08-04 12:23:34 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-30 18:40:57 +08:00
|
|
|
func (client *Client) Init() (err error) {
|
|
|
|
return client.InitWithAccessKey("", "", "")
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:15:51 +08:00
|
|
|
func (client *Client) InitWithAccessKey(accessKeyId, accessKeySecret, accessKeyFrom string) (err error) {
|
2020-08-04 12:23:34 +08:00
|
|
|
config := client.InitWithConfig()
|
|
|
|
credential := &credentials.BaseCredential{
|
|
|
|
AccessKeyId: accessKeyId,
|
|
|
|
AccessKeySecret: accessKeySecret,
|
2020-08-06 12:15:51 +08:00
|
|
|
AccessKeyFrom: accessKeyFrom,
|
2020-08-04 12:23:34 +08:00
|
|
|
}
|
2020-08-06 12:15:51 +08:00
|
|
|
return client.InitWithOptions(config, credential)
|
2020-08-04 12:23:34 +08:00
|
|
|
}
|
|
|
|
|
2020-09-23 20:23:35 +08:00
|
|
|
func (client *Client) InitWithAliAppcode(accessKeyId, accessKeySecret string, env ...string) (err error) {
|
2020-09-23 12:14:23 +08:00
|
|
|
config := client.InitWithConfig()
|
|
|
|
credential := credentials.NewAliAppcodeCredential(accessKeyId, accessKeySecret)
|
2020-09-23 20:23:35 +08:00
|
|
|
|
|
|
|
if len(env) > 0 {
|
|
|
|
config.Env = env[0]
|
|
|
|
}
|
|
|
|
|
2020-09-23 12:14:23 +08:00
|
|
|
return client.InitWithOptions(config, credential)
|
|
|
|
}
|
|
|
|
|
2020-08-08 15:09:58 +08:00
|
|
|
func (client *Client) InitWithStsToken(accessKeyId, accessKeySecret, accessKeyFrom string) (err error) {
|
|
|
|
config := client.InitWithConfig()
|
|
|
|
credential := &credentials.StdTokenCredential{
|
|
|
|
AccessKeyId: accessKeyId,
|
|
|
|
AccessKeySecret: accessKeySecret,
|
|
|
|
AccessKeyFrom: accessKeyFrom,
|
|
|
|
}
|
|
|
|
return client.InitWithOptions(config, credential)
|
|
|
|
}
|
|
|
|
|
2020-08-06 12:15:51 +08:00
|
|
|
func (client *Client) InitWithOptions(config *Config, credential auth.Credential) (err error) {
|
2020-08-03 20:48:58 +08:00
|
|
|
client.httpClient = &http.Client{}
|
2020-08-05 17:01:10 +08:00
|
|
|
client.config = config
|
2020-08-03 20:48:58 +08:00
|
|
|
|
|
|
|
if config.Transport != nil {
|
|
|
|
client.httpClient.Transport = config.Transport
|
|
|
|
} else if config.HttpTransport != nil {
|
|
|
|
client.httpClient.Transport = config.HttpTransport
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Timeout > 0 {
|
|
|
|
client.httpClient.Timeout = config.Timeout
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:51:19 +08:00
|
|
|
client.signer, err = auth.NewSignerWithCredential(credential, client.ProcessCommonRequestWithSigner)
|
2020-08-05 17:01:10 +08:00
|
|
|
return
|
2020-08-03 20:48:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) InitWithConfig() (config *Config) {
|
|
|
|
if client.config != nil {
|
|
|
|
return client.config
|
|
|
|
} else {
|
|
|
|
return NewConfig()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:51:19 +08:00
|
|
|
func (client *Client) ProcessCommonRequestWithSigner(request *requests.CommonRequest, signerInterface interface{}) (response *responses.CommonResponse, err error) {
|
|
|
|
if signer, isSigner := signerInterface.(auth.Signer); isSigner {
|
|
|
|
response = responses.NewCommonResponse()
|
|
|
|
err = client.DoActionWithSigner(request, response, signer)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
panic("should not be here")
|
|
|
|
}
|
|
|
|
|
2020-08-03 20:48:58 +08:00
|
|
|
func Timeout(connectTimeout time.Duration) func(ctx context.Context, net, addr string) (c net.Conn, err error) {
|
|
|
|
return func(ctx context.Context, network, address string) (c net.Conn, err error) {
|
|
|
|
return (&net.Dialer{
|
|
|
|
Timeout: connectTimeout,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext(ctx, network, address)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:51:19 +08:00
|
|
|
func (client *Client) setTimeOut(request requests.AcsRequest) {
|
2020-08-03 20:48:58 +08:00
|
|
|
readTimeout, connectTimeout := client.getTimeOut(request)
|
|
|
|
client.httpClient.Timeout = readTimeout
|
|
|
|
if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil {
|
|
|
|
trans.DialContext = Timeout(connectTimeout)
|
|
|
|
client.httpClient.Transport = trans
|
|
|
|
} else if client.httpClient.Transport == nil {
|
|
|
|
client.httpClient.Transport = &http.Transport{
|
|
|
|
DialContext: Timeout(connectTimeout),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:51:19 +08:00
|
|
|
func (client *Client) getTimeOut(request requests.AcsRequest) (time.Duration, time.Duration) {
|
2020-08-03 20:48:58 +08:00
|
|
|
readTimeOut := defaultReadTimeout
|
|
|
|
connectTimeOut := defaultConnectTimeout
|
|
|
|
|
|
|
|
reqReadTimeout := request.GetReadTimeout()
|
|
|
|
reqConnectTimeout := request.GetConnectTimeout()
|
|
|
|
if reqReadTimeout != 0*time.Millisecond {
|
|
|
|
readTimeOut = reqReadTimeout
|
|
|
|
} else if client.readTimeout != 0*time.Microsecond {
|
|
|
|
readTimeOut = client.readTimeout
|
|
|
|
} else if client.httpClient.Timeout != 0 {
|
|
|
|
readTimeOut = client.httpClient.Timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqConnectTimeout != 0*time.Microsecond {
|
|
|
|
connectTimeOut = reqConnectTimeout
|
|
|
|
} else if client.connectTimeout != 0*time.Millisecond {
|
|
|
|
connectTimeOut = client.connectTimeout
|
|
|
|
}
|
|
|
|
return readTimeOut, connectTimeOut
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:14:06 +08:00
|
|
|
func (client *Client) getHTTPSInsecure(request requests.AcsRequest) (insecure bool) {
|
|
|
|
if request.GetHTTPSInsecure() != nil {
|
|
|
|
insecure = *request.GetHTTPSInsecure()
|
|
|
|
} else {
|
|
|
|
insecure = client.GetHTTPSInsecure()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-04 00:51:19 +08:00
|
|
|
func (client *Client) DoAction(request requests.AcsRequest, response responses.AcsResponse) (err error) {
|
|
|
|
return client.DoActionWithSigner(request, response, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) DoActionWithSigner(request requests.AcsRequest, response responses.AcsResponse, signer auth.Signer) (err error) {
|
2020-08-04 11:22:37 +08:00
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
httpRequest, err := client.buildRequestWithSigner(request, signer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:14:06 +08:00
|
|
|
client.setTimeOut(request)
|
2020-08-05 12:14:49 +08:00
|
|
|
proxy, err := client.getHttpProxy(httpRequest.URL.Scheme)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
noProxy := client.getNoProxy(httpRequest.URL.Scheme)
|
|
|
|
var flag bool
|
|
|
|
for _, value := range noProxy {
|
|
|
|
if strings.HasPrefix(value, "*") {
|
|
|
|
value = fmt.Sprint(".%s", value)
|
|
|
|
}
|
|
|
|
noProxyReg, err := regexp.Compile(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if noProxyReg.MatchString(httpRequest.Host) {
|
|
|
|
flag = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 21:14:06 +08:00
|
|
|
|
|
|
|
if trans, ok := client.httpClient.Transport.(*http.Transport); ok && trans != nil {
|
|
|
|
if trans.TLSClientConfig != nil {
|
|
|
|
trans.TLSClientConfig.InsecureSkipVerify = client.getHTTPSInsecure(request)
|
|
|
|
} else {
|
|
|
|
trans.TLSClientConfig = &tls.Config{
|
|
|
|
InsecureSkipVerify: client.getHTTPSInsecure(request),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
if proxy != nil && !flag {
|
|
|
|
trans.Proxy = http.ProxyURL(proxy)
|
|
|
|
}
|
|
|
|
|
2020-08-04 21:14:06 +08:00
|
|
|
client.httpClient.Transport = trans
|
|
|
|
}
|
|
|
|
|
2020-08-08 15:09:58 +08:00
|
|
|
dump, err := httputil.DumpRequest(httpRequest, true)
|
|
|
|
debug("client %s", bytes.NewBuffer(dump).String())
|
2020-08-04 21:14:06 +08:00
|
|
|
|
2020-08-08 15:09:58 +08:00
|
|
|
var httpResponse *http.Response
|
2020-08-05 12:14:49 +08:00
|
|
|
httpResponse, err = hookDo(client.httpClient.Do)(httpRequest)
|
2020-08-05 17:01:10 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
2020-08-05 12:14:49 +08:00
|
|
|
}
|
|
|
|
|
2020-08-05 17:01:10 +08:00
|
|
|
err = responses.Unmarshal(response, httpResponse, request.GetAcceptFormat())
|
2020-08-05 12:14:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) buildRequestWithSigner(request requests.AcsRequest, signer auth.Signer) (httpRequest *http.Request, err error) {
|
|
|
|
// init param
|
2020-08-05 17:01:10 +08:00
|
|
|
domain := request.GetDomain()
|
2020-09-23 20:23:35 +08:00
|
|
|
if strings.Index(domain.Default, ".") < 0 {
|
|
|
|
domain.Default += defaultDomain
|
2020-08-05 17:01:10 +08:00
|
|
|
request.SetDomain(domain)
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
if request.GetScheme() == "" {
|
|
|
|
request.SetScheme(client.config.Scheme)
|
|
|
|
}
|
|
|
|
|
2020-09-23 20:23:35 +08:00
|
|
|
if request.GetEnv() == "" && client.config.Env != "" {
|
|
|
|
request.SetEnv(client.config.Env)
|
|
|
|
}
|
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
err = requests.InitParam(request)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-08-05 17:01:10 +08:00
|
|
|
// build signature
|
|
|
|
var finalSigner auth.Signer
|
|
|
|
if signer != nil {
|
|
|
|
finalSigner = signer
|
|
|
|
} else {
|
|
|
|
finalSigner = client.signer
|
|
|
|
}
|
|
|
|
err = auth.Sign(request, finalSigner)
|
2020-08-05 12:14:49 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-08-05 17:01:10 +08:00
|
|
|
|
|
|
|
// build request
|
2020-08-05 12:14:49 +08:00
|
|
|
requestMethod := request.GetMethod()
|
|
|
|
requestUrl := request.BuildUrl()
|
|
|
|
body := request.GetBodyReader()
|
|
|
|
httpRequest, err = http.NewRequest(requestMethod, requestUrl, body)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range request.GetHeaders() {
|
|
|
|
httpRequest.Header[key] = []string{val}
|
|
|
|
}
|
|
|
|
|
|
|
|
if host, isContainsHost := request.GetHeaders()["host"]; isContainsHost {
|
|
|
|
httpRequest.Host = host
|
|
|
|
}
|
2020-09-23 20:23:35 +08:00
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
userAgent := DefaultUserAgent
|
|
|
|
httpRequest.Header.Set("User-Agent", userAgent)
|
2020-08-06 12:15:51 +08:00
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (client *Client) getHttpProxy(scheme string) (proxy *url.URL, err error) {
|
|
|
|
switch scheme {
|
|
|
|
case "https":
|
|
|
|
if client.GetHttpsProxy() != "" {
|
|
|
|
proxy, err = url.Parse(client.httpsProxy)
|
|
|
|
} else if rawurl := os.Getenv("HTTPS_PROXY"); rawurl != "" {
|
|
|
|
proxy, err = url.Parse(rawurl)
|
|
|
|
} else if rawurl := os.Getenv("https_proxy"); rawurl != "" {
|
|
|
|
proxy, err = url.Parse(rawurl)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if client.GetHttpProxy() != "" {
|
|
|
|
proxy, err = url.Parse(client.httpProxy)
|
|
|
|
} else if rawurl := os.Getenv("HTTP_PROXY"); rawurl != "" {
|
|
|
|
proxy, err = url.Parse(rawurl)
|
|
|
|
} else if rawurl := os.Getenv("http_proxy"); rawurl != "" {
|
|
|
|
proxy, err = url.Parse(rawurl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2020-08-04 21:14:06 +08:00
|
|
|
}
|
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
func (client *Client) getNoProxy(scheme string) []string {
|
|
|
|
var urls []string
|
|
|
|
if client.GetNoProxy() != "" {
|
|
|
|
urls = strings.Split(client.noProxy, ",")
|
|
|
|
} else if rawurl := os.Getenv("NO_PROXY"); rawurl != "" {
|
|
|
|
urls = strings.Split(rawurl, ",")
|
|
|
|
} else if rawurl := os.Getenv("no_proxy"); rawurl != "" {
|
|
|
|
urls = strings.Split(rawurl, ",")
|
|
|
|
}
|
|
|
|
return urls
|
|
|
|
}
|
2020-08-04 21:14:06 +08:00
|
|
|
|
2020-08-05 12:14:49 +08:00
|
|
|
func hookDo(fn func(req *http.Request) (*http.Response, error)) func(req *http.Request) (*http.Response, error) {
|
|
|
|
return fn
|
2020-08-03 20:48:58 +08:00
|
|
|
}
|