utils.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. "time"
  30. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh"
  32. )
  33. // MakePsiphonUserAgent constructs a User-Agent value to use for web service
  34. // requests made by the tunnel-core client. The User-Agent includes useful stats
  35. // information; it is to be used only for HTTPS requests, where the header
  36. // cannot be seen by an adversary.
  37. func MakePsiphonUserAgent(config *Config) string {
  38. userAgent := "psiphon-tunnel-core"
  39. if config.ClientVersion != "" {
  40. userAgent += fmt.Sprintf("/%s", config.ClientVersion)
  41. }
  42. if config.ClientPlatform != "" {
  43. userAgent += fmt.Sprintf(" (%s)", config.ClientPlatform)
  44. }
  45. return userAgent
  46. }
  47. func DecodeCertificate(encodedCertificate string) (certificate *x509.Certificate, err error) {
  48. derEncodedCertificate, err := base64.StdEncoding.DecodeString(encodedCertificate)
  49. if err != nil {
  50. return nil, common.ContextError(err)
  51. }
  52. certificate, err = x509.ParseCertificate(derEncodedCertificate)
  53. if err != nil {
  54. return nil, common.ContextError(err)
  55. }
  56. return certificate, nil
  57. }
  58. // FilterUrlError transforms an error, when it is a url.Error, removing
  59. // the URL value. This is to avoid logging private user data in cases
  60. // where the URL may be a user input value.
  61. // This function is used with errors returned by net/http and net/url,
  62. // which are (currently) of type url.Error. In particular, the round trip
  63. // function used by our HttpProxy, http.Client.Do, returns errors of type
  64. // url.Error, with the URL being the url sent from the user's tunneled
  65. // applications:
  66. // https://github.com/golang/go/blob/release-branch.go1.4/src/net/http/client.go#L394
  67. func FilterUrlError(err error) error {
  68. if urlErr, ok := err.(*url.Error); ok {
  69. err = &url.Error{
  70. Op: urlErr.Op,
  71. URL: "",
  72. Err: urlErr.Err,
  73. }
  74. }
  75. return err
  76. }
  77. // TrimError removes the middle of over-long error message strings
  78. func TrimError(err error) error {
  79. const MAX_LEN = 100
  80. message := fmt.Sprintf("%s", err)
  81. if len(message) > MAX_LEN {
  82. return errors.New(message[:MAX_LEN/2] + "..." + message[len(message)-MAX_LEN/2:])
  83. }
  84. return err
  85. }
  86. // IsAddressInUseError returns true when the err is due to EADDRINUSE/WSAEADDRINUSE.
  87. func IsAddressInUseError(err error) bool {
  88. if err, ok := err.(*net.OpError); ok {
  89. if err, ok := err.Err.(*os.SyscallError); ok {
  90. if err.Err == syscall.EADDRINUSE {
  91. return true
  92. }
  93. // Special case for Windows (WSAEADDRINUSE = 10048)
  94. if errno, ok := err.Err.(syscall.Errno); ok {
  95. if 10048 == int(errno) {
  96. return true
  97. }
  98. }
  99. }
  100. }
  101. return false
  102. }
  103. // SyncFileWriter wraps a file and exposes an io.Writer. At predefined
  104. // steps, the file is synced (flushed to disk) while writing.
  105. type SyncFileWriter struct {
  106. file *os.File
  107. step int
  108. count int
  109. }
  110. // NewSyncFileWriter creates a SyncFileWriter.
  111. func NewSyncFileWriter(file *os.File) *SyncFileWriter {
  112. return &SyncFileWriter{
  113. file: file,
  114. step: 2 << 16,
  115. count: 0}
  116. }
  117. // Write implements io.Writer with periodic file syncing.
  118. func (writer *SyncFileWriter) Write(p []byte) (n int, err error) {
  119. n, err = writer.file.Write(p)
  120. if err != nil {
  121. return
  122. }
  123. writer.count += n
  124. if writer.count >= writer.step {
  125. err = writer.file.Sync()
  126. writer.count = 0
  127. }
  128. return
  129. }
  130. // emptyAddr implements the net.Addr interface. emptyAddr is intended to be
  131. // used as a stub, when a net.Addr is required but not used.
  132. type emptyAddr struct {
  133. }
  134. func (e *emptyAddr) String() string {
  135. return ""
  136. }
  137. func (e *emptyAddr) Network() string {
  138. return ""
  139. }
  140. // channelConn implements the net.Conn interface. channelConn allows use of
  141. // SSH.Channels in contexts where a net.Conn is expected. Only Read/Write/Close
  142. // are implemented and the remaining functions are stubs and expected to not
  143. // be used.
  144. type channelConn struct {
  145. ssh.Channel
  146. }
  147. func newChannelConn(channel ssh.Channel) *channelConn {
  148. return &channelConn{
  149. Channel: channel,
  150. }
  151. }
  152. func (conn *channelConn) LocalAddr() net.Addr {
  153. return new(emptyAddr)
  154. }
  155. func (conn *channelConn) RemoteAddr() net.Addr {
  156. return new(emptyAddr)
  157. }
  158. func (conn *channelConn) SetDeadline(_ time.Time) error {
  159. return common.ContextError(errors.New("unsupported"))
  160. }
  161. func (conn *channelConn) SetReadDeadline(_ time.Time) error {
  162. return common.ContextError(errors.New("unsupported"))
  163. }
  164. func (conn *channelConn) SetWriteDeadline(_ time.Time) error {
  165. return common.ContextError(errors.New("unsupported"))
  166. }