utils.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. "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. // FlipCoin is a helper function that randomly
  97. // returns true or false.
  98. //
  99. // If the underlying random number generator fails,
  100. // FlipCoin still returns false.
  101. func FlipCoin() bool {
  102. randomInt, _ := MakeSecureRandomInt(2)
  103. return randomInt == 1
  104. }
  105. // FlipWeightedCoin returns the result of a weighted
  106. // random coin flip. If the weight is 0.5, the outcome
  107. // is equally likely to be true or false. If the weight
  108. // is 1.0, the outcome is always true, and if the
  109. // weight is 0.0, the outcome is always false.
  110. //
  111. // Input weights > 1.0 are treated as 1.0.
  112. //
  113. // If the underlying random number generator fails,
  114. // FlipWeightedCoin still returns a result.
  115. func FlipWeightedCoin(weight float64) bool {
  116. if weight > 1.0 {
  117. weight = 1.0
  118. }
  119. n, _ := MakeSecureRandomInt64(math.MaxInt64)
  120. f := float64(n) / float64(math.MaxInt64)
  121. return f > 1.0-weight
  122. }
  123. // MakeSecureRandomInt is a helper function that wraps
  124. // MakeSecureRandomInt64.
  125. func MakeSecureRandomInt(max int) (int, error) {
  126. randomInt, err := MakeSecureRandomInt64(int64(max))
  127. return int(randomInt), err
  128. }
  129. // MakeSecureRandomInt64 is a helper function that wraps
  130. // crypto/rand.Int, which returns a uniform random value in [0, max).
  131. func MakeSecureRandomInt64(max int64) (int64, error) {
  132. if max <= 0 {
  133. return 0, nil
  134. }
  135. randomInt, err := rand.Int(rand.Reader, big.NewInt(max))
  136. if err != nil {
  137. return 0, ContextError(err)
  138. }
  139. return randomInt.Int64(), nil
  140. }
  141. // MakeSecureRandomPerm returns a random permutation of [0,max).
  142. func MakeSecureRandomPerm(max int) ([]int, error) {
  143. // Based on math/rand.Rand.Perm:
  144. // https://github.com/golang/go/blob/release-branch.go1.9/src/math/rand/rand.go#L189
  145. perm := make([]int, max)
  146. for i := 1; i < max; i++ {
  147. j, err := MakeSecureRandomInt(i + 1)
  148. if err != nil {
  149. return nil, ContextError(err)
  150. }
  151. perm[i] = perm[j]
  152. perm[j] = i
  153. }
  154. return perm, nil
  155. }
  156. // MakeSecureRandomBytes is a helper function that wraps
  157. // crypto/rand.Read.
  158. func MakeSecureRandomBytes(length int) ([]byte, error) {
  159. randomBytes := make([]byte, length)
  160. n, err := rand.Read(randomBytes)
  161. if err != nil {
  162. return nil, ContextError(err)
  163. }
  164. if n != length {
  165. return nil, ContextError(errors.New("insufficient random bytes"))
  166. }
  167. return randomBytes, nil
  168. }
  169. // MakeSecureRandomRange selects a random int in [min, max].
  170. // If max < min, min is returned.
  171. func MakeSecureRandomRange(min, max int) (int, error) {
  172. if max < min {
  173. return min, nil
  174. }
  175. n, err := MakeSecureRandomInt(max - min + 1)
  176. if err != nil {
  177. return 0, ContextError(err)
  178. }
  179. n += min
  180. return n, nil
  181. }
  182. // MakeSecureRandomPadding selects a random padding length in the indicated
  183. // range and returns a random byte array of the selected length.
  184. // If maxLength <= minLength, the padding is minLength.
  185. func MakeSecureRandomPadding(minLength, maxLength int) ([]byte, error) {
  186. paddingSize, err := MakeSecureRandomRange(minLength, maxLength)
  187. if err != nil {
  188. return nil, ContextError(err)
  189. }
  190. padding, err := MakeSecureRandomBytes(paddingSize)
  191. if err != nil {
  192. return nil, ContextError(err)
  193. }
  194. return padding, nil
  195. }
  196. // MakeSecureRandomPeriod returns a random duration, within a given range.
  197. // If max <= min, the duration is min.
  198. func MakeSecureRandomPeriod(min, max time.Duration) (time.Duration, error) {
  199. period, err := MakeSecureRandomInt64(max.Nanoseconds() - min.Nanoseconds())
  200. if err != nil {
  201. return 0, ContextError(err)
  202. }
  203. return min + time.Duration(period), nil
  204. }
  205. // MakeSecureRandomStringHex returns a hex encoded random string.
  206. // byteLength specifies the pre-encoded data length.
  207. func MakeSecureRandomStringHex(byteLength int) (string, error) {
  208. bytes, err := MakeSecureRandomBytes(byteLength)
  209. if err != nil {
  210. return "", ContextError(err)
  211. }
  212. return hex.EncodeToString(bytes), nil
  213. }
  214. // MakeSecureRandomStringBase64 returns a base64 encoded random string.
  215. // byteLength specifies the pre-encoded data length.
  216. func MakeSecureRandomStringBase64(byteLength int) (string, error) {
  217. bytes, err := MakeSecureRandomBytes(byteLength)
  218. if err != nil {
  219. return "", ContextError(err)
  220. }
  221. return base64.RawURLEncoding.EncodeToString(bytes), nil
  222. }
  223. // Jitter returns n +/- the given factor.
  224. // For example, for n = 100 and factor = 0.1, the
  225. // return value will be in the range [90, 110].
  226. func Jitter(n int64, factor float64) int64 {
  227. a := int64(math.Ceil(float64(n) * factor))
  228. r, _ := MakeSecureRandomInt64(2*a + 1)
  229. return n + r - a
  230. }
  231. // JitterDuration is a helper function that wraps Jitter.
  232. func JitterDuration(
  233. d time.Duration, factor float64) time.Duration {
  234. return time.Duration(Jitter(int64(d), factor))
  235. }
  236. // GetCurrentTimestamp returns the current time in UTC as
  237. // an RFC 3339 formatted string.
  238. func GetCurrentTimestamp() string {
  239. return time.Now().UTC().Format(time.RFC3339)
  240. }
  241. // TruncateTimestampToHour truncates an RFC 3339 formatted string
  242. // to hour granularity. If the input is not a valid format, the
  243. // result is "".
  244. func TruncateTimestampToHour(timestamp string) string {
  245. t, err := time.Parse(time.RFC3339, timestamp)
  246. if err != nil {
  247. return ""
  248. }
  249. return t.Truncate(1 * time.Hour).Format(time.RFC3339)
  250. }
  251. // getFunctionName is a helper that extracts a simple function name from
  252. // full name returned byruntime.Func.Name(). This is used to declutter
  253. // log messages containing function names.
  254. func getFunctionName(pc uintptr) string {
  255. funcName := runtime.FuncForPC(pc).Name()
  256. index := strings.LastIndex(funcName, "/")
  257. if index != -1 {
  258. funcName = funcName[index+1:]
  259. }
  260. return funcName
  261. }
  262. // GetParentContext returns the parent function name and source file
  263. // line number.
  264. func GetParentContext() string {
  265. pc, _, line, _ := runtime.Caller(2)
  266. return fmt.Sprintf("%s#%d", getFunctionName(pc), line)
  267. }
  268. // ContextError prefixes an error message with the current function
  269. // name and source file line number.
  270. func ContextError(err error) error {
  271. if err == nil {
  272. return nil
  273. }
  274. pc, _, line, _ := runtime.Caller(1)
  275. return fmt.Errorf("%s#%d: %s", getFunctionName(pc), line, err)
  276. }
  277. // Compress returns zlib compressed data
  278. func Compress(data []byte) []byte {
  279. var compressedData bytes.Buffer
  280. writer := zlib.NewWriter(&compressedData)
  281. writer.Write(data)
  282. writer.Close()
  283. return compressedData.Bytes()
  284. }
  285. // Decompress returns zlib decompressed data
  286. func Decompress(data []byte) ([]byte, error) {
  287. reader, err := zlib.NewReader(bytes.NewReader(data))
  288. if err != nil {
  289. return nil, ContextError(err)
  290. }
  291. uncompressedData, err := ioutil.ReadAll(reader)
  292. reader.Close()
  293. if err != nil {
  294. return nil, ContextError(err)
  295. }
  296. return uncompressedData, nil
  297. }
  298. // FormatByteCount returns a string representation of the specified
  299. // byte count in conventional, human-readable format.
  300. func FormatByteCount(bytes uint64) string {
  301. // 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
  302. base := uint64(1024)
  303. if bytes < base {
  304. return fmt.Sprintf("%dB", bytes)
  305. }
  306. exp := int(math.Log(float64(bytes)) / math.Log(float64(base)))
  307. return fmt.Sprintf(
  308. "%.1f%c", float64(bytes)/math.Pow(float64(base), float64(exp)), "KMGTPEZ"[exp-1])
  309. }