utils.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. "encoding/base64"
  25. "encoding/hex"
  26. "errors"
  27. "fmt"
  28. "io/ioutil"
  29. "math"
  30. "math/big"
  31. "runtime"
  32. "strings"
  33. "time"
  34. )
  35. const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
  36. // Contains is a helper function that returns true
  37. // if the target string is in the list.
  38. func Contains(list []string, target string) bool {
  39. for _, listItem := range list {
  40. if listItem == target {
  41. return true
  42. }
  43. }
  44. return false
  45. }
  46. // ContainsAny returns true if any string in targets
  47. // is present in the list.
  48. func ContainsAny(list, targets []string) bool {
  49. for _, target := range targets {
  50. if Contains(list, target) {
  51. return true
  52. }
  53. }
  54. return false
  55. }
  56. // ContainsInt returns true if the target int is
  57. // in the list.
  58. func ContainsInt(list []int, target int) bool {
  59. for _, listItem := range list {
  60. if listItem == target {
  61. return true
  62. }
  63. }
  64. return false
  65. }
  66. // GetStringSlice converts an interface{} which is
  67. // of type []interace{}, and with the type of each
  68. // element a string, to []string.
  69. func GetStringSlice(value interface{}) ([]string, bool) {
  70. slice, ok := value.([]interface{})
  71. if !ok {
  72. return nil, false
  73. }
  74. strSlice := make([]string, len(slice))
  75. for index, element := range slice {
  76. str, ok := element.(string)
  77. if !ok {
  78. return nil, false
  79. }
  80. strSlice[index] = str
  81. }
  82. return strSlice, true
  83. }
  84. // FlipCoin is a helper function that randomly
  85. // returns true or false.
  86. //
  87. // If the underlying random number generator fails,
  88. // FlipCoin still returns false.
  89. func FlipCoin() bool {
  90. randomInt, _ := MakeSecureRandomInt(2)
  91. return randomInt == 1
  92. }
  93. // FlipWeightedCoin returns the result of a weighted
  94. // random coin flip. If the weight is 0.5, the outcome
  95. // is equally likely to be true or false. If the weight
  96. // is 1.0, the outcome is always true, and if the
  97. // weight is 0.0, the outcome is always false.
  98. //
  99. // Input weights > 1.0 are treated as 1.0.
  100. //
  101. // If the underlying random number generator fails,
  102. // FlipWeightedCoin still returns a result.
  103. func FlipWeightedCoin(weight float64) bool {
  104. if weight > 1.0 {
  105. weight = 1.0
  106. }
  107. n, _ := MakeSecureRandomInt64(math.MaxInt64)
  108. f := float64(n) / float64(math.MaxInt64)
  109. return f > 1.0-weight
  110. }
  111. // MakeSecureRandomInt is a helper function that wraps
  112. // MakeSecureRandomInt64.
  113. func MakeSecureRandomInt(max int) (int, error) {
  114. randomInt, err := MakeSecureRandomInt64(int64(max))
  115. return int(randomInt), err
  116. }
  117. // MakeSecureRandomInt64 is a helper function that wraps
  118. // crypto/rand.Int, which returns a uniform random value in [0, max).
  119. func MakeSecureRandomInt64(max int64) (int64, error) {
  120. if max <= 0 {
  121. return 0, nil
  122. }
  123. randomInt, err := rand.Int(rand.Reader, big.NewInt(max))
  124. if err != nil {
  125. return 0, ContextError(err)
  126. }
  127. return randomInt.Int64(), nil
  128. }
  129. // MakeSecureRandomPerm returns a random permutation of [0,max).
  130. func MakeSecureRandomPerm(max int) ([]int, error) {
  131. // Based on math/rand.Rand.Perm:
  132. // https://github.com/golang/go/blob/release-branch.go1.9/src/math/rand/rand.go#L189
  133. perm := make([]int, max)
  134. for i := 1; i < max; i++ {
  135. j, err := MakeSecureRandomInt(i + 1)
  136. if err != nil {
  137. return nil, ContextError(err)
  138. }
  139. perm[i] = perm[j]
  140. perm[j] = i
  141. }
  142. return perm, nil
  143. }
  144. // MakeSecureRandomBytes is a helper function that wraps
  145. // crypto/rand.Read.
  146. func MakeSecureRandomBytes(length int) ([]byte, error) {
  147. randomBytes := make([]byte, length)
  148. n, err := rand.Read(randomBytes)
  149. if err != nil {
  150. return nil, ContextError(err)
  151. }
  152. if n != length {
  153. return nil, ContextError(errors.New("insufficient random bytes"))
  154. }
  155. return randomBytes, nil
  156. }
  157. // MakeSecureRandomPadding selects a random padding length in the indicated
  158. // range and returns a random byte array of the selected length.
  159. // If maxLength <= minLength, the padding is minLength.
  160. func MakeSecureRandomPadding(minLength, maxLength int) ([]byte, error) {
  161. var padding []byte
  162. paddingSize, err := MakeSecureRandomInt(maxLength - minLength)
  163. if err != nil {
  164. return nil, ContextError(err)
  165. }
  166. paddingSize += minLength
  167. padding, err = MakeSecureRandomBytes(paddingSize)
  168. if err != nil {
  169. return nil, ContextError(err)
  170. }
  171. return padding, nil
  172. }
  173. // MakeRandomPeriod returns a random duration, within a given range.
  174. // If max <= min, the duration is min.
  175. func MakeRandomPeriod(min, max time.Duration) (time.Duration, error) {
  176. period, err := MakeSecureRandomInt64(max.Nanoseconds() - min.Nanoseconds())
  177. if err != nil {
  178. return 0, ContextError(err)
  179. }
  180. return min + time.Duration(period), nil
  181. }
  182. // MakeRandomStringHex returns a hex encoded random string.
  183. // byteLength specifies the pre-encoded data length.
  184. func MakeRandomStringHex(byteLength int) (string, error) {
  185. bytes, err := MakeSecureRandomBytes(byteLength)
  186. if err != nil {
  187. return "", ContextError(err)
  188. }
  189. return hex.EncodeToString(bytes), nil
  190. }
  191. // MakeRandomStringBase64 returns a base64 encoded random string.
  192. // byteLength specifies the pre-encoded data length.
  193. func MakeRandomStringBase64(byteLength int) (string, error) {
  194. bytes, err := MakeSecureRandomBytes(byteLength)
  195. if err != nil {
  196. return "", ContextError(err)
  197. }
  198. return base64.RawURLEncoding.EncodeToString(bytes), nil
  199. }
  200. // Jitter returns n +/- the given factor.
  201. // For example, for n = 100 and factor = 0.1, the
  202. // return value will be in the range [90, 110].
  203. func Jitter(n int64, factor float64) int64 {
  204. a := int64(math.Ceil(float64(n) * factor))
  205. r, _ := MakeSecureRandomInt64(2*a + 1)
  206. return n + r - a
  207. }
  208. // JitterDuration is a helper function that wraps Jitter.
  209. func JitterDuration(
  210. d time.Duration, factor float64) time.Duration {
  211. return time.Duration(Jitter(int64(d), factor))
  212. }
  213. // GetCurrentTimestamp returns the current time in UTC as
  214. // an RFC 3339 formatted string.
  215. func GetCurrentTimestamp() string {
  216. return time.Now().UTC().Format(time.RFC3339)
  217. }
  218. // TruncateTimestampToHour truncates an RFC 3339 formatted string
  219. // to hour granularity. If the input is not a valid format, the
  220. // result is "".
  221. func TruncateTimestampToHour(timestamp string) string {
  222. t, err := time.Parse(time.RFC3339, timestamp)
  223. if err != nil {
  224. return ""
  225. }
  226. return t.Truncate(1 * time.Hour).Format(time.RFC3339)
  227. }
  228. // getFunctionName is a helper that extracts a simple function name from
  229. // full name returned byruntime.Func.Name(). This is used to declutter
  230. // log messages containing function names.
  231. func getFunctionName(pc uintptr) string {
  232. funcName := runtime.FuncForPC(pc).Name()
  233. index := strings.LastIndex(funcName, "/")
  234. if index != -1 {
  235. funcName = funcName[index+1:]
  236. }
  237. return funcName
  238. }
  239. // GetParentContext returns the parent function name and source file
  240. // line number.
  241. func GetParentContext() string {
  242. pc, _, line, _ := runtime.Caller(2)
  243. return fmt.Sprintf("%s#%d", getFunctionName(pc), line)
  244. }
  245. // ContextError prefixes an error message with the current function
  246. // name and source file line number.
  247. func ContextError(err error) error {
  248. if err == nil {
  249. return nil
  250. }
  251. pc, _, line, _ := runtime.Caller(1)
  252. return fmt.Errorf("%s#%d: %s", getFunctionName(pc), line, err)
  253. }
  254. // Compress returns zlib compressed data
  255. func Compress(data []byte) []byte {
  256. var compressedData bytes.Buffer
  257. writer := zlib.NewWriter(&compressedData)
  258. writer.Write(data)
  259. writer.Close()
  260. return compressedData.Bytes()
  261. }
  262. // Decompress returns zlib decompressed data
  263. func Decompress(data []byte) ([]byte, error) {
  264. reader, err := zlib.NewReader(bytes.NewReader(data))
  265. if err != nil {
  266. return nil, ContextError(err)
  267. }
  268. uncompressedData, err := ioutil.ReadAll(reader)
  269. reader.Close()
  270. if err != nil {
  271. return nil, ContextError(err)
  272. }
  273. return uncompressedData, nil
  274. }
  275. // FormatByteCount returns a string representation of the specified
  276. // byte count in conventional, human-readable format.
  277. func FormatByteCount(bytes uint64) string {
  278. // 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
  279. base := uint64(1024)
  280. if bytes < base {
  281. return fmt.Sprintf("%dB", bytes)
  282. }
  283. exp := int(math.Log(float64(bytes)) / math.Log(float64(base)))
  284. return fmt.Sprintf(
  285. "%.1f%c", float64(bytes)/math.Pow(float64(base), float64(exp)), "KMGTPEZ"[exp-1])
  286. }