utils.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (c) 2016, Psiphon Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. package common
  20. import (
  21. "bytes"
  22. "compress/zlib"
  23. "context"
  24. "crypto/rand"
  25. std_errors "errors"
  26. "fmt"
  27. "io"
  28. "io/ioutil"
  29. "math"
  30. "net/url"
  31. "os"
  32. "time"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/wildcard"
  35. )
  36. const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
  37. // Contains is a helper function that returns true
  38. // if the target string is in the list.
  39. func Contains(list []string, target string) bool {
  40. for _, listItem := range list {
  41. if listItem == target {
  42. return true
  43. }
  44. }
  45. return false
  46. }
  47. // ContainsWildcard returns true if target matches
  48. // any of the patterns. Patterns may contain the
  49. // '*' wildcard.
  50. func ContainsWildcard(patterns []string, target string) bool {
  51. for _, pattern := range patterns {
  52. if wildcard.Match(pattern, target) {
  53. return true
  54. }
  55. }
  56. return false
  57. }
  58. // ContainsAny returns true if any string in targets
  59. // is present in the list.
  60. func ContainsAny(list, targets []string) bool {
  61. for _, target := range targets {
  62. if Contains(list, target) {
  63. return true
  64. }
  65. }
  66. return false
  67. }
  68. // ContainsInt returns true if the target int is
  69. // in the list.
  70. func ContainsInt(list []int, target int) bool {
  71. for _, listItem := range list {
  72. if listItem == target {
  73. return true
  74. }
  75. }
  76. return false
  77. }
  78. // GetStringSlice converts an interface{} which is
  79. // of type []interace{}, and with the type of each
  80. // element a string, to []string.
  81. func GetStringSlice(value interface{}) ([]string, bool) {
  82. slice, ok := value.([]interface{})
  83. if !ok {
  84. return nil, false
  85. }
  86. strSlice := make([]string, len(slice))
  87. for index, element := range slice {
  88. str, ok := element.(string)
  89. if !ok {
  90. return nil, false
  91. }
  92. strSlice[index] = str
  93. }
  94. return strSlice, true
  95. }
  96. // MakeSecureRandomBytes is a helper function that wraps
  97. // crypto/rand.Read.
  98. func MakeSecureRandomBytes(length int) ([]byte, error) {
  99. randomBytes := make([]byte, length)
  100. _, err := rand.Read(randomBytes)
  101. if err != nil {
  102. return nil, errors.Trace(err)
  103. }
  104. return randomBytes, nil
  105. }
  106. // GetCurrentTimestamp returns the current time in UTC as
  107. // an RFC 3339 formatted string.
  108. func GetCurrentTimestamp() string {
  109. return time.Now().UTC().Format(time.RFC3339)
  110. }
  111. // TruncateTimestampToHour truncates an RFC 3339 formatted string
  112. // to hour granularity. If the input is not a valid format, the
  113. // result is "".
  114. func TruncateTimestampToHour(timestamp string) string {
  115. t, err := time.Parse(time.RFC3339, timestamp)
  116. if err != nil {
  117. return ""
  118. }
  119. return t.Truncate(1 * time.Hour).Format(time.RFC3339)
  120. }
  121. // Compress returns zlib compressed data
  122. func Compress(data []byte) []byte {
  123. var compressedData bytes.Buffer
  124. writer := zlib.NewWriter(&compressedData)
  125. writer.Write(data)
  126. writer.Close()
  127. return compressedData.Bytes()
  128. }
  129. // Decompress returns zlib decompressed data
  130. func Decompress(data []byte) ([]byte, error) {
  131. reader, err := zlib.NewReader(bytes.NewReader(data))
  132. if err != nil {
  133. return nil, errors.Trace(err)
  134. }
  135. uncompressedData, err := ioutil.ReadAll(reader)
  136. reader.Close()
  137. if err != nil {
  138. return nil, errors.Trace(err)
  139. }
  140. return uncompressedData, nil
  141. }
  142. // FormatByteCount returns a string representation of the specified
  143. // byte count in conventional, human-readable format.
  144. func FormatByteCount(bytes uint64) string {
  145. // Based on: https://bitbucket.org/psiphon/psiphon-circumvention-system/src/b2884b0d0a491e55420ed1888aea20d00fefdb45/Android/app/src/main/java/com/psiphon3/psiphonlibrary/Utils.java?at=default#Utils.java-646
  146. base := uint64(1024)
  147. if bytes < base {
  148. return fmt.Sprintf("%dB", bytes)
  149. }
  150. exp := int(math.Log(float64(bytes)) / math.Log(float64(base)))
  151. return fmt.Sprintf(
  152. "%.1f%c", float64(bytes)/math.Pow(float64(base), float64(exp)), "KMGTPEZ"[exp-1])
  153. }
  154. // CopyBuffer calls io.CopyBuffer, masking out any src.WriteTo or dst.ReadFrom
  155. // to force use of the specified buf.
  156. func CopyBuffer(dst io.Writer, src io.Reader, buf []byte) (written int64, err error) {
  157. return io.CopyBuffer(struct{ io.Writer }{dst}, struct{ io.Reader }{src}, buf)
  158. }
  159. func CopyNBuffer(dst io.Writer, src io.Reader, n int64, buf []byte) (written int64, err error) {
  160. // Based on io.CopyN:
  161. // https://github.com/golang/go/blob/release-branch.go1.11/src/io/io.go#L339
  162. written, err = CopyBuffer(dst, io.LimitReader(src, n), buf)
  163. if written == n {
  164. return n, nil
  165. }
  166. if written < n && err == nil {
  167. err = io.EOF
  168. }
  169. return
  170. }
  171. // FileExists returns true if a file, or directory, exists at the given path.
  172. func FileExists(filePath string) bool {
  173. if _, err := os.Stat(filePath); err != nil && os.IsNotExist(err) {
  174. return false
  175. }
  176. return true
  177. }
  178. // SafeParseURL wraps url.Parse, stripping the input URL from any error
  179. // message. This allows logging url.Parse errors without unintentially logging
  180. // PII that may appear in the input URL.
  181. func SafeParseURL(rawurl string) (*url.URL, error) {
  182. parsedURL, err := url.Parse(rawurl)
  183. if err != nil {
  184. // Unwrap yields just the url.Error error field without the url.Error URL
  185. // and operation fields.
  186. err = std_errors.Unwrap(err)
  187. if err == nil {
  188. err = std_errors.New("SafeParseURL: Unwrap failed")
  189. } else {
  190. err = fmt.Errorf("url.Parse: %v", err)
  191. }
  192. }
  193. return parsedURL, err
  194. }
  195. // SafeParseRequestURI wraps url.ParseRequestURI, stripping the input URL from
  196. // any error message. This allows logging url.ParseRequestURI errors without
  197. // unintentially logging PII that may appear in the input URL.
  198. func SafeParseRequestURI(rawurl string) (*url.URL, error) {
  199. parsedURL, err := url.ParseRequestURI(rawurl)
  200. if err != nil {
  201. err = std_errors.Unwrap(err)
  202. if err == nil {
  203. err = std_errors.New("SafeParseRequestURI: Unwrap failed")
  204. } else {
  205. err = fmt.Errorf("url.ParseRequestURI: %v", err)
  206. }
  207. }
  208. return parsedURL, err
  209. }
  210. // SleepWithContext returns after the specified duration or once the input ctx
  211. // is done, whichever is first.
  212. func SleepWithContext(ctx context.Context, duration time.Duration) {
  213. timer := time.NewTimer(duration)
  214. defer timer.Stop()
  215. select {
  216. case <-timer.C:
  217. case <-ctx.Done():
  218. }
  219. }