utils.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. // MakeSecureRandomRange selects a random int in [min, max].
  158. // If max < min, min is returned.
  159. func MakeSecureRandomRange(min, max int) (int, error) {
  160. if max < min {
  161. return min, nil
  162. }
  163. n, err := MakeSecureRandomInt(max - min + 1)
  164. if err != nil {
  165. return 0, ContextError(err)
  166. }
  167. n += min
  168. return n, nil
  169. }
  170. // MakeSecureRandomPadding selects a random padding length in the indicated
  171. // range and returns a random byte array of the selected length.
  172. // If maxLength <= minLength, the padding is minLength.
  173. func MakeSecureRandomPadding(minLength, maxLength int) ([]byte, error) {
  174. paddingSize, err := MakeSecureRandomRange(minLength, maxLength)
  175. if err != nil {
  176. return nil, ContextError(err)
  177. }
  178. padding, err := MakeSecureRandomBytes(paddingSize)
  179. if err != nil {
  180. return nil, ContextError(err)
  181. }
  182. return padding, nil
  183. }
  184. // MakeSecureRandomPeriod returns a random duration, within a given range.
  185. // If max <= min, the duration is min.
  186. func MakeSecureRandomPeriod(min, max time.Duration) (time.Duration, error) {
  187. period, err := MakeSecureRandomInt64(max.Nanoseconds() - min.Nanoseconds())
  188. if err != nil {
  189. return 0, ContextError(err)
  190. }
  191. return min + time.Duration(period), nil
  192. }
  193. // MakeSecureRandomStringHex returns a hex encoded random string.
  194. // byteLength specifies the pre-encoded data length.
  195. func MakeSecureRandomStringHex(byteLength int) (string, error) {
  196. bytes, err := MakeSecureRandomBytes(byteLength)
  197. if err != nil {
  198. return "", ContextError(err)
  199. }
  200. return hex.EncodeToString(bytes), nil
  201. }
  202. // MakeSecureRandomStringBase64 returns a base64 encoded random string.
  203. // byteLength specifies the pre-encoded data length.
  204. func MakeSecureRandomStringBase64(byteLength int) (string, error) {
  205. bytes, err := MakeSecureRandomBytes(byteLength)
  206. if err != nil {
  207. return "", ContextError(err)
  208. }
  209. return base64.RawURLEncoding.EncodeToString(bytes), nil
  210. }
  211. // Jitter returns n +/- the given factor.
  212. // For example, for n = 100 and factor = 0.1, the
  213. // return value will be in the range [90, 110].
  214. func Jitter(n int64, factor float64) int64 {
  215. a := int64(math.Ceil(float64(n) * factor))
  216. r, _ := MakeSecureRandomInt64(2*a + 1)
  217. return n + r - a
  218. }
  219. // JitterDuration is a helper function that wraps Jitter.
  220. func JitterDuration(
  221. d time.Duration, factor float64) time.Duration {
  222. return time.Duration(Jitter(int64(d), factor))
  223. }
  224. // GetCurrentTimestamp returns the current time in UTC as
  225. // an RFC 3339 formatted string.
  226. func GetCurrentTimestamp() string {
  227. return time.Now().UTC().Format(time.RFC3339)
  228. }
  229. // TruncateTimestampToHour truncates an RFC 3339 formatted string
  230. // to hour granularity. If the input is not a valid format, the
  231. // result is "".
  232. func TruncateTimestampToHour(timestamp string) string {
  233. t, err := time.Parse(time.RFC3339, timestamp)
  234. if err != nil {
  235. return ""
  236. }
  237. return t.Truncate(1 * time.Hour).Format(time.RFC3339)
  238. }
  239. // getFunctionName is a helper that extracts a simple function name from
  240. // full name returned byruntime.Func.Name(). This is used to declutter
  241. // log messages containing function names.
  242. func getFunctionName(pc uintptr) string {
  243. funcName := runtime.FuncForPC(pc).Name()
  244. index := strings.LastIndex(funcName, "/")
  245. if index != -1 {
  246. funcName = funcName[index+1:]
  247. }
  248. return funcName
  249. }
  250. // GetParentContext returns the parent function name and source file
  251. // line number.
  252. func GetParentContext() string {
  253. pc, _, line, _ := runtime.Caller(2)
  254. return fmt.Sprintf("%s#%d", getFunctionName(pc), line)
  255. }
  256. // ContextError prefixes an error message with the current function
  257. // name and source file line number.
  258. func ContextError(err error) error {
  259. if err == nil {
  260. return nil
  261. }
  262. pc, _, line, _ := runtime.Caller(1)
  263. return fmt.Errorf("%s#%d: %s", getFunctionName(pc), line, err)
  264. }
  265. // Compress returns zlib compressed data
  266. func Compress(data []byte) []byte {
  267. var compressedData bytes.Buffer
  268. writer := zlib.NewWriter(&compressedData)
  269. writer.Write(data)
  270. writer.Close()
  271. return compressedData.Bytes()
  272. }
  273. // Decompress returns zlib decompressed data
  274. func Decompress(data []byte) ([]byte, error) {
  275. reader, err := zlib.NewReader(bytes.NewReader(data))
  276. if err != nil {
  277. return nil, ContextError(err)
  278. }
  279. uncompressedData, err := ioutil.ReadAll(reader)
  280. reader.Close()
  281. if err != nil {
  282. return nil, ContextError(err)
  283. }
  284. return uncompressedData, nil
  285. }
  286. // FormatByteCount returns a string representation of the specified
  287. // byte count in conventional, human-readable format.
  288. func FormatByteCount(bytes uint64) string {
  289. // 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
  290. base := uint64(1024)
  291. if bytes < base {
  292. return fmt.Sprintf("%dB", bytes)
  293. }
  294. exp := int(math.Log(float64(bytes)) / math.Log(float64(base)))
  295. return fmt.Sprintf(
  296. "%.1f%c", float64(bytes)/math.Pow(float64(base), float64(exp)), "KMGTPEZ"[exp-1])
  297. }