utils.go 9.8 KB

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