utils.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "crypto/rand"
  24. "fmt"
  25. "io"
  26. "io/ioutil"
  27. "math"
  28. "os"
  29. "time"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/wildcard"
  32. )
  33. const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
  34. // Contains is a helper function that returns true
  35. // if the target string is in the list.
  36. func Contains(list []string, target string) bool {
  37. for _, listItem := range list {
  38. if listItem == target {
  39. return true
  40. }
  41. }
  42. return false
  43. }
  44. // ContainsWildcard returns true if target matches
  45. // any of the patterns. Patterns may contain the
  46. // '*' wildcard.
  47. func ContainsWildcard(patterns []string, target string) bool {
  48. for _, pattern := range patterns {
  49. if wildcard.Match(pattern, target) {
  50. return true
  51. }
  52. }
  53. return false
  54. }
  55. // ContainsAny returns true if any string in targets
  56. // is present in the list.
  57. func ContainsAny(list, targets []string) bool {
  58. for _, target := range targets {
  59. if Contains(list, target) {
  60. return true
  61. }
  62. }
  63. return false
  64. }
  65. // ContainsInt returns true if the target int is
  66. // in the list.
  67. func ContainsInt(list []int, target int) bool {
  68. for _, listItem := range list {
  69. if listItem == target {
  70. return true
  71. }
  72. }
  73. return false
  74. }
  75. // GetStringSlice converts an interface{} which is
  76. // of type []interace{}, and with the type of each
  77. // element a string, to []string.
  78. func GetStringSlice(value interface{}) ([]string, bool) {
  79. slice, ok := value.([]interface{})
  80. if !ok {
  81. return nil, false
  82. }
  83. strSlice := make([]string, len(slice))
  84. for index, element := range slice {
  85. str, ok := element.(string)
  86. if !ok {
  87. return nil, false
  88. }
  89. strSlice[index] = str
  90. }
  91. return strSlice, true
  92. }
  93. // MakeSecureRandomBytes is a helper function that wraps
  94. // crypto/rand.Read.
  95. func MakeSecureRandomBytes(length int) ([]byte, error) {
  96. randomBytes := make([]byte, length)
  97. n, err := rand.Read(randomBytes)
  98. if err != nil {
  99. return nil, errors.Trace(err)
  100. }
  101. if n != length {
  102. return nil, errors.TraceNew("insufficient random bytes")
  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. func CopyNBuffer(dst io.Writer, src io.Reader, n int64, buf []byte) (written int64, err error) {
  155. // Based on io.CopyN:
  156. // https://github.com/golang/go/blob/release-branch.go1.11/src/io/io.go#L339
  157. written, err = io.CopyBuffer(dst, io.LimitReader(src, n), buf)
  158. if written == n {
  159. return n, nil
  160. }
  161. if written < n && err == nil {
  162. err = io.EOF
  163. }
  164. return
  165. }
  166. // FileExists returns true if a file, or directory, exists at the given path.
  167. func FileExists(filePath string) bool {
  168. if _, err := os.Stat(filePath); err != nil && os.IsNotExist(err) {
  169. return false
  170. }
  171. return true
  172. }
  173. // FileMigration represents the action of moving a file, or directory, to a new
  174. // location.
  175. type FileMigration struct {
  176. // OldPath is the current location of the file.
  177. OldPath string
  178. // NewPath is the location that the file should be moved to.
  179. NewPath string
  180. // IsDir should be set to true if the file is a directory.
  181. IsDir bool
  182. }
  183. // DoFileMigration performs the specified file move operation. An error will be
  184. // returned and the operation will not performed if: a file is expected, but a
  185. // directory is found; a directory is expected, but a file is found; or a file,
  186. // or directory, already exists at the target path of the move operation.
  187. func DoFileMigration(migration FileMigration) error {
  188. if !FileExists(migration.OldPath) {
  189. return errors.Tracef("%s does not exist", migration.OldPath)
  190. }
  191. info, err := os.Stat(migration.OldPath)
  192. if err != nil {
  193. return errors.Tracef("error getting file info of %s: %s", migration.OldPath, err.Error())
  194. }
  195. if info.IsDir() != migration.IsDir {
  196. if migration.IsDir {
  197. return errors.Tracef("expected directory %s to be directory but found file", migration.OldPath)
  198. }
  199. return errors.Tracef("expected %s to be file but found directory",
  200. migration.OldPath)
  201. }
  202. if FileExists(migration.NewPath) {
  203. return errors.Tracef("%s already exists, will not overwrite", migration.NewPath)
  204. }
  205. err = os.Rename(migration.OldPath, migration.NewPath)
  206. if err != nil {
  207. return errors.Tracef("renaming %s as %s failed with error %s", migration.OldPath, migration.NewPath, err.Error())
  208. }
  209. return nil
  210. }