utils.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. "context"
  24. "crypto/rand"
  25. std_errors "errors"
  26. "fmt"
  27. "io"
  28. "io/ioutil"
  29. "math"
  30. "net/url"
  31. "os"
  32. "strings"
  33. "time"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/wildcard"
  37. )
  38. const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
  39. // Contains is a helper function that returns true
  40. // if the target string is in the list.
  41. func Contains(list []string, target string) bool {
  42. for _, listItem := range list {
  43. if listItem == target {
  44. return true
  45. }
  46. }
  47. return false
  48. }
  49. // ContainsWildcard returns true if target matches
  50. // any of the patterns. Patterns may contain the
  51. // '*' wildcard.
  52. func ContainsWildcard(patterns []string, target string) bool {
  53. for _, pattern := range patterns {
  54. if wildcard.Match(pattern, target) {
  55. return true
  56. }
  57. }
  58. return false
  59. }
  60. // ContainsAny returns true if any string in targets
  61. // is present in the list.
  62. func ContainsAny(list, targets []string) bool {
  63. for _, target := range targets {
  64. if Contains(list, target) {
  65. return true
  66. }
  67. }
  68. return false
  69. }
  70. // ContainsInt returns true if the target int is
  71. // in the list.
  72. func ContainsInt(list []int, target int) bool {
  73. for _, listItem := range list {
  74. if listItem == target {
  75. return true
  76. }
  77. }
  78. return false
  79. }
  80. // GetStringSlice converts an interface{} which is
  81. // of type []interace{}, and with the type of each
  82. // element a string, to []string.
  83. func GetStringSlice(value interface{}) ([]string, bool) {
  84. slice, ok := value.([]interface{})
  85. if !ok {
  86. return nil, false
  87. }
  88. strSlice := make([]string, len(slice))
  89. for index, element := range slice {
  90. str, ok := element.(string)
  91. if !ok {
  92. return nil, false
  93. }
  94. strSlice[index] = str
  95. }
  96. return strSlice, true
  97. }
  98. // MakeSecureRandomBytes is a helper function that wraps
  99. // crypto/rand.Read.
  100. func MakeSecureRandomBytes(length int) ([]byte, error) {
  101. randomBytes := make([]byte, length)
  102. _, err := rand.Read(randomBytes)
  103. if err != nil {
  104. return nil, errors.Trace(err)
  105. }
  106. return randomBytes, nil
  107. }
  108. // GetCurrentTimestamp returns the current time in UTC as
  109. // an RFC 3339 formatted string.
  110. func GetCurrentTimestamp() string {
  111. return time.Now().UTC().Format(time.RFC3339)
  112. }
  113. // TruncateTimestampToHour truncates an RFC 3339 formatted string
  114. // to hour granularity. If the input is not a valid format, the
  115. // result is "".
  116. func TruncateTimestampToHour(timestamp string) string {
  117. t, err := time.Parse(time.RFC3339, timestamp)
  118. if err != nil {
  119. return ""
  120. }
  121. return t.Truncate(1 * time.Hour).Format(time.RFC3339)
  122. }
  123. // ParseTimeOfDayMinutes parses a time of day in HH:MM 24-hour format and
  124. // returns the number of minutes since midnight.
  125. func ParseTimeOfDayMinutes(value string) (int, error) {
  126. t, err := time.Parse("15:04", value)
  127. if err != nil {
  128. return 0, errors.Trace(err)
  129. }
  130. return t.Hour()*60 + t.Minute(), nil
  131. }
  132. const (
  133. CompressionNone = int32(0)
  134. CompressionZlib = int32(1)
  135. )
  136. // Compress compresses data with the specified algorithm.
  137. func Compress(compression int32, data []byte) ([]byte, error) {
  138. if compression == CompressionNone {
  139. return data, nil
  140. }
  141. if compression != CompressionZlib {
  142. return nil, errors.TraceNew("unknown compression algorithm")
  143. }
  144. var compressedData bytes.Buffer
  145. writer := zlib.NewWriter(&compressedData)
  146. _, err := writer.Write(data)
  147. if err != nil {
  148. return nil, errors.Trace(err)
  149. }
  150. _ = writer.Close()
  151. return compressedData.Bytes(), nil
  152. }
  153. // Decompress decompresses data with the specified algorithm.
  154. func Decompress(compression int32, data []byte) ([]byte, error) {
  155. if compression == CompressionNone {
  156. return data, nil
  157. }
  158. if compression != CompressionZlib {
  159. return nil, errors.TraceNew("unknown compression algorithm")
  160. }
  161. reader, err := zlib.NewReader(bytes.NewReader(data))
  162. if err != nil {
  163. return nil, errors.Trace(err)
  164. }
  165. uncompressedData, err := ioutil.ReadAll(reader)
  166. _ = reader.Close()
  167. if err != nil {
  168. return nil, errors.Trace(err)
  169. }
  170. return uncompressedData, nil
  171. }
  172. // FormatByteCount returns a string representation of the specified
  173. // byte count in conventional, human-readable format.
  174. func FormatByteCount(bytes uint64) string {
  175. // 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
  176. base := uint64(1024)
  177. if bytes < base {
  178. return fmt.Sprintf("%dB", bytes)
  179. }
  180. exp := int(math.Log(float64(bytes)) / math.Log(float64(base)))
  181. return fmt.Sprintf(
  182. "%.1f%c", float64(bytes)/math.Pow(float64(base), float64(exp)), "KMGTPEZ"[exp-1])
  183. }
  184. // CopyBuffer calls io.CopyBuffer, masking out any src.WriteTo or dst.ReadFrom
  185. // to force use of the specified buf.
  186. func CopyBuffer(dst io.Writer, src io.Reader, buf []byte) (written int64, err error) {
  187. return io.CopyBuffer(struct{ io.Writer }{dst}, struct{ io.Reader }{src}, buf)
  188. }
  189. func CopyNBuffer(dst io.Writer, src io.Reader, n int64, buf []byte) (written int64, err error) {
  190. // Based on io.CopyN:
  191. // https://github.com/golang/go/blob/release-branch.go1.11/src/io/io.go#L339
  192. written, err = CopyBuffer(dst, io.LimitReader(src, n), buf)
  193. if written == n {
  194. return n, nil
  195. }
  196. if written < n && err == nil {
  197. err = io.EOF
  198. }
  199. return
  200. }
  201. // FileExists returns true if a file, or directory, exists at the given path.
  202. func FileExists(filePath string) bool {
  203. if _, err := os.Stat(filePath); err != nil && os.IsNotExist(err) {
  204. return false
  205. }
  206. return true
  207. }
  208. // SafeParseURL wraps url.Parse, stripping the input URL from any error
  209. // message. This allows logging url.Parse errors without unintentially logging
  210. // PII that may appear in the input URL.
  211. func SafeParseURL(rawurl string) (*url.URL, error) {
  212. parsedURL, err := url.Parse(rawurl)
  213. if err != nil {
  214. // Unwrap yields just the url.Error error field without the url.Error URL
  215. // and operation fields.
  216. err = std_errors.Unwrap(err)
  217. if err == nil {
  218. err = std_errors.New("SafeParseURL: Unwrap failed")
  219. } else {
  220. err = fmt.Errorf("url.Parse: %v", err)
  221. }
  222. }
  223. return parsedURL, err
  224. }
  225. // SafeParseRequestURI wraps url.ParseRequestURI, stripping the input URL from
  226. // any error message. This allows logging url.ParseRequestURI errors without
  227. // unintentially logging PII that may appear in the input URL.
  228. func SafeParseRequestURI(rawurl string) (*url.URL, error) {
  229. parsedURL, err := url.ParseRequestURI(rawurl)
  230. if err != nil {
  231. err = std_errors.Unwrap(err)
  232. if err == nil {
  233. err = std_errors.New("SafeParseRequestURI: Unwrap failed")
  234. } else {
  235. err = fmt.Errorf("url.ParseRequestURI: %v", err)
  236. }
  237. }
  238. return parsedURL, err
  239. }
  240. // SleepWithContext returns after the specified duration or once the input ctx
  241. // is done, whichever is first.
  242. func SleepWithContext(ctx context.Context, duration time.Duration) {
  243. timer := time.NewTimer(duration)
  244. defer timer.Stop()
  245. select {
  246. case <-timer.C:
  247. case <-ctx.Done():
  248. }
  249. }
  250. // SleepWithJitter returns after the specified duration, with random jitter
  251. // applied, or once the input ctx is done, whichever is first.
  252. func SleepWithJitter(ctx context.Context, duration time.Duration, jitter float64) {
  253. timer := time.NewTimer(prng.JitterDuration(duration, jitter))
  254. defer timer.Stop()
  255. select {
  256. case <-ctx.Done():
  257. case <-timer.C:
  258. }
  259. }
  260. // ValueOrDefault returns the input value, or, when value is the zero value of
  261. // its type, defaultValue.
  262. func ValueOrDefault[T comparable](value, defaultValue T) T {
  263. var zero T
  264. if value == zero {
  265. return defaultValue
  266. }
  267. return value
  268. }
  269. // MergeContextCancel returns a context which has the properties of the 1st
  270. // input content and merges in the cancellation signal of the 2nd context, so
  271. // the returned context is cancelled when either input context is cancelled.
  272. //
  273. // See (and adapted from): https://pkg.go.dev/context#example-AfterFunc-Merge
  274. func MergeContextCancel(ctx, cancelCtx context.Context) (context.Context, context.CancelFunc) {
  275. ctx, cancel := context.WithCancelCause(ctx)
  276. stop := context.AfterFunc(cancelCtx, func() {
  277. cancel(context.Cause(cancelCtx))
  278. })
  279. return ctx, func() {
  280. stop()
  281. cancel(context.Canceled)
  282. }
  283. }
  284. // MaxDuration returns the maximum duration in durations or 0 if durations is
  285. // empty.
  286. func MaxDuration(durations ...time.Duration) time.Duration {
  287. if len(durations) == 0 {
  288. return 0
  289. }
  290. max := durations[0]
  291. for _, d := range durations[1:] {
  292. if d > max {
  293. max = d
  294. }
  295. }
  296. return max
  297. }
  298. // ToRandomASCIICasing returns s with each ASCII letter randomly mapped to
  299. // either its upper or lower case.
  300. func ToRandomASCIICasing(s string, seed *prng.Seed) string {
  301. PRNG := prng.NewPRNGWithSeed(seed)
  302. var b strings.Builder
  303. b.Grow(len(s))
  304. for _, r := range s {
  305. isLower := ('a' <= r && r <= 'z')
  306. isUpper := ('A' <= r && r <= 'Z')
  307. if (isLower || isUpper) && PRNG.FlipCoin() {
  308. b.WriteRune(r ^ 0x20)
  309. } else {
  310. b.WriteRune(r)
  311. }
  312. }
  313. return b.String()
  314. }