utils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2015, 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 psiphon
  20. import (
  21. "crypto/x509"
  22. "encoding/base64"
  23. "errors"
  24. "fmt"
  25. "net"
  26. "net/url"
  27. "os"
  28. "syscall"
  29. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  30. )
  31. func DecodeCertificate(encodedCertificate string) (certificate *x509.Certificate, err error) {
  32. derEncodedCertificate, err := base64.StdEncoding.DecodeString(encodedCertificate)
  33. if err != nil {
  34. return nil, common.ContextError(err)
  35. }
  36. certificate, err = x509.ParseCertificate(derEncodedCertificate)
  37. if err != nil {
  38. return nil, common.ContextError(err)
  39. }
  40. return certificate, nil
  41. }
  42. // FilterUrlError transforms an error, when it is a url.Error, removing
  43. // the URL value. This is to avoid logging private user data in cases
  44. // where the URL may be a user input value.
  45. // This function is used with errors returned by net/http and net/url,
  46. // which are (currently) of type url.Error. In particular, the round trip
  47. // function used by our HttpProxy, http.Client.Do, returns errors of type
  48. // url.Error, with the URL being the url sent from the user's tunneled
  49. // applications:
  50. // https://github.com/golang/go/blob/release-branch.go1.4/src/net/http/client.go#L394
  51. func FilterUrlError(err error) error {
  52. if urlErr, ok := err.(*url.Error); ok {
  53. err = &url.Error{
  54. Op: urlErr.Op,
  55. URL: "",
  56. Err: urlErr.Err,
  57. }
  58. }
  59. return err
  60. }
  61. // TrimError removes the middle of over-long error message strings
  62. func TrimError(err error) error {
  63. const MAX_LEN = 100
  64. message := fmt.Sprintf("%s", err)
  65. if len(message) > MAX_LEN {
  66. return errors.New(message[:MAX_LEN/2] + "..." + message[len(message)-MAX_LEN/2:])
  67. }
  68. return err
  69. }
  70. // IsAddressInUseError returns true when the err is due to EADDRINUSE/WSAEADDRINUSE.
  71. func IsAddressInUseError(err error) bool {
  72. if err, ok := err.(*net.OpError); ok {
  73. if err, ok := err.Err.(*os.SyscallError); ok {
  74. if err.Err == syscall.EADDRINUSE {
  75. return true
  76. }
  77. // Special case for Windows (WSAEADDRINUSE = 10048)
  78. if errno, ok := err.Err.(syscall.Errno); ok {
  79. if 10048 == int(errno) {
  80. return true
  81. }
  82. }
  83. }
  84. }
  85. return false
  86. }
  87. // SyncFileWriter wraps a file and exposes an io.Writer. At predefined
  88. // steps, the file is synced (flushed to disk) while writing.
  89. type SyncFileWriter struct {
  90. file *os.File
  91. step int
  92. count int
  93. }
  94. // NewSyncFileWriter creates a SyncFileWriter.
  95. func NewSyncFileWriter(file *os.File) *SyncFileWriter {
  96. return &SyncFileWriter{
  97. file: file,
  98. step: 2 << 16,
  99. count: 0}
  100. }
  101. // Write implements io.Writer with periodic file syncing.
  102. func (writer *SyncFileWriter) Write(p []byte) (n int, err error) {
  103. n, err = writer.file.Write(p)
  104. if err != nil {
  105. return
  106. }
  107. writer.count += n
  108. if writer.count >= writer.step {
  109. err = writer.file.Sync()
  110. writer.count = 0
  111. }
  112. return
  113. }