util.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package internal
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "crypto/x509"
  6. "encoding/json"
  7. "errors"
  8. "io/ioutil"
  9. "net/http"
  10. "regexp"
  11. "time"
  12. )
  13. // GetTLSConfig generate tls.Config from CAFile
  14. func GetTLSConfig(CAFile string) (*tls.Config, error) {
  15. caCert, err := ioutil.ReadFile(CAFile)
  16. if err != nil {
  17. return nil, err
  18. }
  19. caCertPool := x509.NewCertPool()
  20. if ok := caCertPool.AppendCertsFromPEM(caCert); !ok {
  21. return nil, errors.New("Failed to add CA to pool")
  22. }
  23. tlsConfig := &tls.Config{
  24. RootCAs: caCertPool,
  25. }
  26. tlsConfig.BuildNameToCertificate()
  27. return tlsConfig, nil
  28. }
  29. // CreateHTTPClient returns a http.Client
  30. func CreateHTTPClient(CAFile string) (*http.Client, error) {
  31. var tlsConfig *tls.Config
  32. var err error
  33. if CAFile != "" {
  34. tlsConfig, err = GetTLSConfig(CAFile)
  35. if err != nil {
  36. return nil, err
  37. }
  38. }
  39. tr := &http.Transport{
  40. MaxIdleConnsPerHost: 20,
  41. TLSClientConfig: tlsConfig,
  42. }
  43. return &http.Client{
  44. Transport: tr,
  45. Timeout: 5 * time.Second,
  46. }, nil
  47. }
  48. // PostJSON posts json object to url
  49. func PostJSON(url string, obj interface{}, client *http.Client) (*http.Response, error) {
  50. if client == nil {
  51. client, _ = CreateHTTPClient("")
  52. }
  53. b := new(bytes.Buffer)
  54. if err := json.NewEncoder(b).Encode(obj); err != nil {
  55. return nil, err
  56. }
  57. return client.Post(url, "application/json; charset=utf-8", b)
  58. }
  59. // GetJSON gets a json response from url
  60. func GetJSON(url string, obj interface{}, client *http.Client) (*http.Response, error) {
  61. if client == nil {
  62. client, _ = CreateHTTPClient("")
  63. }
  64. resp, err := client.Get(url)
  65. if err != nil {
  66. return resp, err
  67. }
  68. if resp.StatusCode != http.StatusOK {
  69. return resp, errors.New("HTTP status code is not 200")
  70. }
  71. defer resp.Body.Close()
  72. body, err := ioutil.ReadAll(resp.Body)
  73. if err != nil {
  74. return resp, err
  75. }
  76. return resp, json.Unmarshal(body, obj)
  77. }
  78. func ExtractSizeFromRsyncLog(content []byte) string {
  79. // (?m) flag enables multi-line mode
  80. re := regexp.MustCompile(`(?m)^Total file size: ([0-9\.]+[KMGTP]?) bytes`)
  81. matches := re.FindAllSubmatch(content, -1)
  82. // fmt.Printf("%q\n", matches)
  83. if len(matches) == 0 {
  84. return ""
  85. }
  86. return string(matches[len(matches)-1][1])
  87. }