util.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // FindAllSubmatchInFile calls re.FindAllSubmatch to find matches in given file
  79. func FindAllSubmatchInFile(fileName string, re *regexp.Regexp) (matches [][][]byte, err error) {
  80. if fileName == "/dev/null" {
  81. err = errors.New("Invalid log file")
  82. return
  83. }
  84. if content, err := ioutil.ReadFile(fileName); err == nil {
  85. matches = re.FindAllSubmatch(content, -1)
  86. // fmt.Printf("FindAllSubmatchInFile: %q\n", matches)
  87. }
  88. return
  89. }
  90. // ExtractSizeFromLog uses a regexp to extract the size from log files
  91. func ExtractSizeFromLog(logFile string, re *regexp.Regexp) string {
  92. matches, _ := FindAllSubmatchInFile(logFile, re)
  93. if matches == nil || len(matches) == 0 {
  94. return ""
  95. }
  96. // return the first capture group of the last occurrence
  97. return string(matches[len(matches)-1][1])
  98. }
  99. // ExtractSizeFromRsyncLog extracts the size from rsync logs
  100. func ExtractSizeFromRsyncLog(logFile string) string {
  101. // (?m) flag enables multi-line mode
  102. re := regexp.MustCompile(`(?m)^Total file size: ([0-9\.]+[KMGTP]?) bytes`)
  103. return ExtractSizeFromLog(logFile, re)
  104. }