datasource_cache.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package models
  2. import (
  3. "crypto/tls"
  4. "crypto/x509"
  5. "errors"
  6. "net"
  7. "net/http"
  8. "sync"
  9. "time"
  10. )
  11. type proxyTransportCache struct {
  12. cache map[int64]cachedTransport
  13. sync.Mutex
  14. }
  15. type cachedTransport struct {
  16. updated time.Time
  17. *http.Transport
  18. }
  19. var ptc = proxyTransportCache{
  20. cache: make(map[int64]cachedTransport),
  21. }
  22. func (ds *DataSource) GetHttpClient() (*http.Client, error) {
  23. transport, err := ds.GetHttpTransport()
  24. if err != nil {
  25. return nil, err
  26. }
  27. return &http.Client{
  28. Timeout: 30 * time.Second,
  29. Transport: transport,
  30. }, nil
  31. }
  32. func (ds *DataSource) GetHttpTransport() (*http.Transport, error) {
  33. ptc.Lock()
  34. defer ptc.Unlock()
  35. if t, present := ptc.cache[ds.Id]; present && ds.Updated.Equal(t.updated) {
  36. return t.Transport, nil
  37. }
  38. tlsConfig, err := ds.GetTLSConfig()
  39. if err != nil {
  40. return nil, err
  41. }
  42. tlsConfig.Renegotiation = tls.RenegotiateFreelyAsClient
  43. transport := &http.Transport{
  44. TLSClientConfig: tlsConfig,
  45. Proxy: http.ProxyFromEnvironment,
  46. Dial: (&net.Dialer{
  47. Timeout: 30 * time.Second,
  48. KeepAlive: 30 * time.Second,
  49. DualStack: true,
  50. }).Dial,
  51. TLSHandshakeTimeout: 10 * time.Second,
  52. ExpectContinueTimeout: 1 * time.Second,
  53. MaxIdleConns: 100,
  54. IdleConnTimeout: 90 * time.Second,
  55. }
  56. ptc.cache[ds.Id] = cachedTransport{
  57. Transport: transport,
  58. updated: ds.Updated,
  59. }
  60. return transport, nil
  61. }
  62. func (ds *DataSource) GetTLSConfig() (*tls.Config, error) {
  63. var tlsSkipVerify, tlsClientAuth, tlsAuthWithCACert bool
  64. if ds.JsonData != nil {
  65. tlsClientAuth = ds.JsonData.Get("tlsAuth").MustBool(false)
  66. tlsAuthWithCACert = ds.JsonData.Get("tlsAuthWithCACert").MustBool(false)
  67. tlsSkipVerify = ds.JsonData.Get("tlsSkipVerify").MustBool(false)
  68. }
  69. tlsConfig := &tls.Config{
  70. InsecureSkipVerify: tlsSkipVerify,
  71. }
  72. if tlsClientAuth || tlsAuthWithCACert {
  73. decrypted := ds.SecureJsonData.Decrypt()
  74. if tlsAuthWithCACert && len(decrypted["tlsCACert"]) > 0 {
  75. caPool := x509.NewCertPool()
  76. ok := caPool.AppendCertsFromPEM([]byte(decrypted["tlsCACert"]))
  77. if !ok {
  78. return nil, errors.New("Failed to parse TLS CA PEM certificate")
  79. }
  80. tlsConfig.RootCAs = caPool
  81. }
  82. if tlsClientAuth {
  83. cert, err := tls.X509KeyPair([]byte(decrypted["tlsClientCert"]), []byte(decrypted["tlsClientKey"]))
  84. if err != nil {
  85. return nil, err
  86. }
  87. tlsConfig.Certificates = []tls.Certificate{cert}
  88. }
  89. }
  90. return tlsConfig, nil
  91. }