utils.go 8.9 KB

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