utils.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. _, err := rand.Read(randomBytes)
  98. if err != nil {
  99. return nil, errors.Trace(err)
  100. }
  101. return randomBytes, nil
  102. }
  103. // GetCurrentTimestamp returns the current time in UTC as
  104. // an RFC 3339 formatted string.
  105. func GetCurrentTimestamp() string {
  106. return time.Now().UTC().Format(time.RFC3339)
  107. }
  108. // TruncateTimestampToHour truncates an RFC 3339 formatted string
  109. // to hour granularity. If the input is not a valid format, the
  110. // result is "".
  111. func TruncateTimestampToHour(timestamp string) string {
  112. t, err := time.Parse(time.RFC3339, timestamp)
  113. if err != nil {
  114. return ""
  115. }
  116. return t.Truncate(1 * time.Hour).Format(time.RFC3339)
  117. }
  118. // Compress returns zlib compressed data
  119. func Compress(data []byte) []byte {
  120. var compressedData bytes.Buffer
  121. writer := zlib.NewWriter(&compressedData)
  122. writer.Write(data)
  123. writer.Close()
  124. return compressedData.Bytes()
  125. }
  126. // Decompress returns zlib decompressed data
  127. func Decompress(data []byte) ([]byte, error) {
  128. reader, err := zlib.NewReader(bytes.NewReader(data))
  129. if err != nil {
  130. return nil, errors.Trace(err)
  131. }
  132. uncompressedData, err := ioutil.ReadAll(reader)
  133. reader.Close()
  134. if err != nil {
  135. return nil, errors.Trace(err)
  136. }
  137. return uncompressedData, nil
  138. }
  139. // FormatByteCount returns a string representation of the specified
  140. // byte count in conventional, human-readable format.
  141. func FormatByteCount(bytes uint64) string {
  142. // 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
  143. base := uint64(1024)
  144. if bytes < base {
  145. return fmt.Sprintf("%dB", bytes)
  146. }
  147. exp := int(math.Log(float64(bytes)) / math.Log(float64(base)))
  148. return fmt.Sprintf(
  149. "%.1f%c", float64(bytes)/math.Pow(float64(base), float64(exp)), "KMGTPEZ"[exp-1])
  150. }
  151. // CopyBuffer calls io.CopyBuffer, masking out any src.WriteTo or dst.ReadFrom
  152. // to force use of the specified buf.
  153. func CopyBuffer(dst io.Writer, src io.Reader, buf []byte) (written int64, err error) {
  154. return io.CopyBuffer(struct{ io.Writer }{dst}, struct{ io.Reader }{src}, buf)
  155. }
  156. func CopyNBuffer(dst io.Writer, src io.Reader, n int64, buf []byte) (written int64, err error) {
  157. // Based on io.CopyN:
  158. // https://github.com/golang/go/blob/release-branch.go1.11/src/io/io.go#L339
  159. written, err = CopyBuffer(dst, io.LimitReader(src, n), buf)
  160. if written == n {
  161. return n, nil
  162. }
  163. if written < n && err == nil {
  164. err = io.EOF
  165. }
  166. return
  167. }
  168. // FileExists returns true if a file, or directory, exists at the given path.
  169. func FileExists(filePath string) bool {
  170. if _, err := os.Stat(filePath); err != nil && os.IsNotExist(err) {
  171. return false
  172. }
  173. return true
  174. }
  175. // FileMigration represents the action of moving a file, or directory, to a new
  176. // location.
  177. type FileMigration struct {
  178. // OldPath is the current location of the file.
  179. OldPath string
  180. // NewPath is the location that the file should be moved to.
  181. NewPath string
  182. // IsDir should be set to true if the file is a directory.
  183. IsDir bool
  184. }
  185. // DoFileMigration performs the specified file move operation. An error will be
  186. // returned and the operation will not performed if: a file is expected, but a
  187. // directory is found; a directory is expected, but a file is found; or a file,
  188. // or directory, already exists at the target path of the move operation.
  189. func DoFileMigration(migration FileMigration) error {
  190. if !FileExists(migration.OldPath) {
  191. return errors.Tracef("%s does not exist", migration.OldPath)
  192. }
  193. info, err := os.Stat(migration.OldPath)
  194. if err != nil {
  195. return errors.Tracef("error getting file info of %s: %s", migration.OldPath, err.Error())
  196. }
  197. if info.IsDir() != migration.IsDir {
  198. if migration.IsDir {
  199. return errors.Tracef("expected directory %s to be directory but found file", migration.OldPath)
  200. }
  201. return errors.Tracef("expected %s to be file but found directory",
  202. migration.OldPath)
  203. }
  204. if FileExists(migration.NewPath) {
  205. return errors.Tracef("%s already exists, will not overwrite", migration.NewPath)
  206. }
  207. err = os.Rename(migration.OldPath, migration.NewPath)
  208. if err != nil {
  209. return errors.Tracef("renaming %s as %s failed with error %s", migration.OldPath, migration.NewPath, err.Error())
  210. }
  211. return nil
  212. }