transport.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package http
  2. import (
  3. "crypto/tls"
  4. "fmt"
  5. "net/http"
  6. "path/filepath"
  7. "time"
  8. "github.com/tv42/httpunix"
  9. )
  10. func unixTransport(cfg Config) *httpunix.Transport {
  11. // Note that clientTimeout doesn't work when using httpunix.Transport, so we set ResponseHeaderTimeout instead
  12. t := &httpunix.Transport{
  13. DialTimeout: time.Duration(cfg.DialTimeout) * time.Second,
  14. RequestTimeout: 5 * time.Second,
  15. ResponseHeaderTimeout: time.Duration(cfg.Timeout) * time.Second,
  16. }
  17. t.RegisterLocation("c", filepath.Join(cfg.WorkDir, cfg.Socket))
  18. return t
  19. }
  20. func httpTransport(cfg Config) *http.Transport {
  21. t := &http.Transport{
  22. IdleConnTimeout: time.Duration(cfg.HttpIdleConnTimeout) * time.Second,
  23. WriteBufferSize: cfg.HttpWriteBufferSize,
  24. ReadBufferSize: cfg.HttpReadBufferSize,
  25. }
  26. return t
  27. }
  28. func newTLSConfig(cfg Config) (*tls.Config, error) {
  29. rootCAPool, err := loadRootCaCerts(cfg.TlsRootCA)
  30. if err != nil {
  31. return nil, err
  32. }
  33. var getClientCertFunc func(*tls.CertificateRequestInfo) (*tls.Certificate, error) = nil
  34. if len(cfg.TlsClientCert) != 0 && len(cfg.TlsClientKey) != 0 {
  35. getClientCertFunc = func(info *tls.CertificateRequestInfo) (certificate *tls.Certificate, e error) {
  36. c, err := tls.LoadX509KeyPair(cfg.TlsClientCert, cfg.TlsClientKey)
  37. if err != nil {
  38. return nil, fmt.Errorf("failed to load client key pair from '%v', '%v': %v", cfg.TlsClientCert, cfg.TlsClientKey, err)
  39. }
  40. return &c, nil
  41. }
  42. }
  43. return &tls.Config{
  44. RootCAs: rootCAPool,
  45. InsecureSkipVerify: cfg.TlsInsecureSkipVerify,
  46. GetClientCertificate: getClientCertFunc,
  47. }, nil
  48. }