utils.go 3.9 KB

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