utils.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. std_errors "errors"
  24. "fmt"
  25. "net"
  26. "os"
  27. "runtime"
  28. "runtime/debug"
  29. "syscall"
  30. "time"
  31. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  32. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh"
  33. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/quic"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/refraction"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/resolver"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/stacktrace"
  38. )
  39. // MakePsiphonUserAgent constructs a User-Agent value to use for web service
  40. // requests made by the tunnel-core client. The User-Agent includes useful stats
  41. // information; it is to be used only for HTTPS requests, where the header
  42. // cannot be seen by an adversary.
  43. func MakePsiphonUserAgent(config *Config) string {
  44. userAgent := "psiphon-tunnel-core"
  45. if config.ClientVersion != "" {
  46. userAgent += fmt.Sprintf("/%s", config.ClientVersion)
  47. }
  48. if config.ClientPlatform != "" {
  49. userAgent += fmt.Sprintf(" (%s)", config.ClientPlatform)
  50. }
  51. return userAgent
  52. }
  53. func DecodeCertificate(encodedCertificate string) (certificate *x509.Certificate, err error) {
  54. derEncodedCertificate, err := base64.StdEncoding.DecodeString(encodedCertificate)
  55. if err != nil {
  56. return nil, errors.Trace(err)
  57. }
  58. certificate, err = x509.ParseCertificate(derEncodedCertificate)
  59. if err != nil {
  60. return nil, errors.Trace(err)
  61. }
  62. return certificate, nil
  63. }
  64. // TrimError removes the middle of over-long error message strings
  65. func TrimError(err error) error {
  66. const MAX_LEN = 100
  67. message := fmt.Sprintf("%s", err)
  68. if len(message) > MAX_LEN {
  69. return std_errors.New(message[:MAX_LEN/2] + "..." + message[len(message)-MAX_LEN/2:])
  70. }
  71. return err
  72. }
  73. // IsAddressInUseError returns true when the err is due to EADDRINUSE/WSAEADDRINUSE.
  74. func IsAddressInUseError(err error) bool {
  75. if err, ok := err.(*net.OpError); ok {
  76. if err, ok := err.Err.(*os.SyscallError); ok {
  77. if err.Err == syscall.EADDRINUSE {
  78. return true
  79. }
  80. // Special case for Windows, WSAEADDRINUSE (10048). In the case
  81. // where the socket already bound to the port has set
  82. // SO_EXCLUSIVEADDRUSE, the error will instead be WSAEACCES (10013).
  83. if errno, ok := err.Err.(syscall.Errno); ok {
  84. if int(errno) == 10048 || int(errno) == 10013 {
  85. return true
  86. }
  87. }
  88. }
  89. }
  90. return false
  91. }
  92. // SyncFileWriter wraps a file and exposes an io.Writer. At predefined
  93. // steps, the file is synced (flushed to disk) while writing.
  94. type SyncFileWriter struct {
  95. file *os.File
  96. step int
  97. count int
  98. }
  99. // NewSyncFileWriter creates a SyncFileWriter.
  100. func NewSyncFileWriter(file *os.File) *SyncFileWriter {
  101. return &SyncFileWriter{
  102. file: file,
  103. step: 2 << 16,
  104. count: 0}
  105. }
  106. // Write implements io.Writer with periodic file syncing.
  107. func (writer *SyncFileWriter) Write(p []byte) (n int, err error) {
  108. n, err = writer.file.Write(p)
  109. if err != nil {
  110. return
  111. }
  112. writer.count += n
  113. if writer.count >= writer.step {
  114. err = writer.file.Sync()
  115. writer.count = 0
  116. }
  117. return
  118. }
  119. // emptyAddr implements the net.Addr interface. emptyAddr is intended to be
  120. // used as a stub, when a net.Addr is required but not used.
  121. type emptyAddr struct {
  122. }
  123. func (e *emptyAddr) String() string {
  124. return ""
  125. }
  126. func (e *emptyAddr) Network() string {
  127. return ""
  128. }
  129. // channelConn implements the net.Conn interface. channelConn allows use of
  130. // SSH.Channels in contexts where a net.Conn is expected. Only Read/Write/Close
  131. // are implemented and the remaining functions are stubs and expected to not
  132. // be used.
  133. type channelConn struct {
  134. ssh.Channel
  135. }
  136. func newChannelConn(channel ssh.Channel) *channelConn {
  137. return &channelConn{
  138. Channel: channel,
  139. }
  140. }
  141. func (conn *channelConn) LocalAddr() net.Addr {
  142. return new(emptyAddr)
  143. }
  144. func (conn *channelConn) RemoteAddr() net.Addr {
  145. return new(emptyAddr)
  146. }
  147. func (conn *channelConn) SetDeadline(_ time.Time) error {
  148. return errors.TraceNew("unsupported")
  149. }
  150. func (conn *channelConn) SetReadDeadline(_ time.Time) error {
  151. return errors.TraceNew("unsupported")
  152. }
  153. func (conn *channelConn) SetWriteDeadline(_ time.Time) error {
  154. return errors.TraceNew("unsupported")
  155. }
  156. func emitMemoryMetrics() {
  157. var memStats runtime.MemStats
  158. runtime.ReadMemStats(&memStats)
  159. NoticeInfo("Memory metrics at %s: goroutines %d | objects %d | alloc %s | inuse %s | sys %s | cumulative %d %s",
  160. stacktrace.GetParentFunctionName(),
  161. runtime.NumGoroutine(),
  162. memStats.HeapObjects,
  163. common.FormatByteCount(memStats.HeapAlloc),
  164. common.FormatByteCount(memStats.HeapInuse+memStats.StackInuse+memStats.MSpanInuse+memStats.MCacheInuse),
  165. common.FormatByteCount(memStats.Sys),
  166. memStats.Mallocs,
  167. common.FormatByteCount(memStats.TotalAlloc))
  168. }
  169. func emitDatastoreMetrics() {
  170. NoticeInfo("Datastore metrics at %s: %s", stacktrace.GetParentFunctionName(), GetDataStoreMetrics())
  171. }
  172. func emitDNSMetrics(resolver *resolver.Resolver) {
  173. NoticeInfo("DNS metrics at %s: %s", stacktrace.GetParentFunctionName(), resolver.GetMetrics())
  174. }
  175. func DoGarbageCollection() {
  176. debug.SetGCPercent(5)
  177. debug.FreeOSMemory()
  178. }
  179. // conditionallyEnabledComponents implements the
  180. // protocol.ConditionallyEnabledComponents interface.
  181. type conditionallyEnabledComponents struct {
  182. }
  183. func (c conditionallyEnabledComponents) QUICEnabled() bool {
  184. return quic.Enabled()
  185. }
  186. func (c conditionallyEnabledComponents) RefractionNetworkingEnabled() bool {
  187. return refraction.Enabled()
  188. }
  189. // FileMigration represents the action of moving a file, or directory, to a new
  190. // location.
  191. type FileMigration struct {
  192. // Name is the name of the migration for logging because file paths are not
  193. // logged as they may contain sensitive information.
  194. Name string
  195. // OldPath is the current location of the file.
  196. OldPath string
  197. // NewPath is the location that the file should be moved to.
  198. NewPath string
  199. // IsDir should be set to true if the file is a directory.
  200. IsDir bool
  201. }
  202. // DoFileMigration performs the specified file move operation. An error will be
  203. // returned and the operation will not performed if: a file is expected, but a
  204. // directory is found; a directory is expected, but a file is found; or a file,
  205. // or directory, already exists at the target path of the move operation.
  206. // Note: an attempt is made to redact any file paths from the returned error.
  207. func DoFileMigration(migration FileMigration) error {
  208. // Prefix string added to any errors for debug purposes.
  209. errPrefix := ""
  210. if len(migration.Name) > 0 {
  211. errPrefix = fmt.Sprintf("(%s) ", migration.Name)
  212. }
  213. if !common.FileExists(migration.OldPath) {
  214. return errors.TraceNew(errPrefix + "old path does not exist")
  215. }
  216. info, err := os.Stat(migration.OldPath)
  217. if err != nil {
  218. return errors.Tracef(errPrefix+"error getting file info: %s", common.RedactFilePathsError(err, migration.OldPath))
  219. }
  220. if info.IsDir() != migration.IsDir {
  221. if migration.IsDir {
  222. return errors.TraceNew(errPrefix + "expected directory but found file")
  223. }
  224. return errors.TraceNew(errPrefix + "expected but found directory")
  225. }
  226. if common.FileExists(migration.NewPath) {
  227. return errors.TraceNew(errPrefix + "file already exists, will not overwrite")
  228. }
  229. err = os.Rename(migration.OldPath, migration.NewPath)
  230. if err != nil {
  231. return errors.Tracef(errPrefix+"renaming file failed with error %s", common.RedactFilePathsError(err, migration.OldPath, migration.NewPath))
  232. }
  233. return nil
  234. }