utils.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. "sync"
  31. "sync/atomic"
  32. "syscall"
  33. "time"
  34. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  35. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/crypto/ssh"
  36. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
  37. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/inproxy"
  38. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/quic"
  39. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/refraction"
  40. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/resolver"
  41. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/stacktrace"
  42. "github.com/wlynxg/anet"
  43. )
  44. // MakePsiphonUserAgent constructs a User-Agent value to use for web service
  45. // requests made by the tunnel-core client. The User-Agent includes useful stats
  46. // information; it is to be used only for HTTPS requests, where the header
  47. // cannot be seen by an adversary.
  48. func MakePsiphonUserAgent(config *Config) string {
  49. userAgent := "psiphon-tunnel-core"
  50. if config.ClientVersion != "" {
  51. userAgent += fmt.Sprintf("/%s", config.ClientVersion)
  52. }
  53. if config.ClientPlatform != "" {
  54. userAgent += fmt.Sprintf(" (%s)", config.ClientPlatform)
  55. }
  56. return userAgent
  57. }
  58. func DecodeCertificate(encodedCertificate string) (certificate *x509.Certificate, err error) {
  59. derEncodedCertificate, err := base64.StdEncoding.DecodeString(encodedCertificate)
  60. if err != nil {
  61. return nil, errors.Trace(err)
  62. }
  63. certificate, err = x509.ParseCertificate(derEncodedCertificate)
  64. if err != nil {
  65. return nil, errors.Trace(err)
  66. }
  67. return certificate, nil
  68. }
  69. // TrimError removes the middle of over-long error message strings
  70. func TrimError(err error) error {
  71. const MAX_LEN = 100
  72. message := fmt.Sprintf("%s", err)
  73. if len(message) > MAX_LEN {
  74. return std_errors.New(message[:MAX_LEN/2] + "..." + message[len(message)-MAX_LEN/2:])
  75. }
  76. return err
  77. }
  78. // IsAddressInUseError returns true when the err is due to EADDRINUSE/WSAEADDRINUSE.
  79. func IsAddressInUseError(err error) bool {
  80. if err, ok := err.(*net.OpError); ok {
  81. if err, ok := err.Err.(*os.SyscallError); ok {
  82. if err.Err == syscall.EADDRINUSE {
  83. return true
  84. }
  85. // Special case for Windows, WSAEADDRINUSE (10048). In the case
  86. // where the socket already bound to the port has set
  87. // SO_EXCLUSIVEADDRUSE, the error will instead be WSAEACCES (10013).
  88. if errno, ok := err.Err.(syscall.Errno); ok {
  89. if int(errno) == 10048 || int(errno) == 10013 {
  90. return true
  91. }
  92. }
  93. }
  94. }
  95. return false
  96. }
  97. // SyncFileWriter wraps a file and exposes an io.Writer. At predefined
  98. // steps, the file is synced (flushed to disk) while writing.
  99. type SyncFileWriter struct {
  100. file *os.File
  101. step int
  102. count int
  103. }
  104. // NewSyncFileWriter creates a SyncFileWriter.
  105. func NewSyncFileWriter(file *os.File) *SyncFileWriter {
  106. return &SyncFileWriter{
  107. file: file,
  108. step: 2 << 16,
  109. count: 0}
  110. }
  111. // Write implements io.Writer with periodic file syncing.
  112. func (writer *SyncFileWriter) Write(p []byte) (n int, err error) {
  113. n, err = writer.file.Write(p)
  114. if err != nil {
  115. return
  116. }
  117. writer.count += n
  118. if writer.count >= writer.step {
  119. err = writer.file.Sync()
  120. writer.count = 0
  121. }
  122. return
  123. }
  124. // emptyAddr implements the net.Addr interface. emptyAddr is intended to be
  125. // used as a stub, when a net.Addr is required but not used.
  126. type emptyAddr struct {
  127. }
  128. func (e *emptyAddr) String() string {
  129. return ""
  130. }
  131. func (e *emptyAddr) Network() string {
  132. return ""
  133. }
  134. // channelConn implements the net.Conn interface. channelConn allows use of
  135. // SSH.Channels in contexts where a net.Conn is expected. Only Read/Write/Close
  136. // are implemented and the remaining functions are stubs and expected to not
  137. // be used.
  138. type channelConn struct {
  139. ssh.Channel
  140. }
  141. func newChannelConn(channel ssh.Channel) *channelConn {
  142. return &channelConn{
  143. Channel: channel,
  144. }
  145. }
  146. func (conn *channelConn) LocalAddr() net.Addr {
  147. return new(emptyAddr)
  148. }
  149. func (conn *channelConn) RemoteAddr() net.Addr {
  150. return new(emptyAddr)
  151. }
  152. func (conn *channelConn) SetDeadline(_ time.Time) error {
  153. return errors.TraceNew("unsupported")
  154. }
  155. func (conn *channelConn) SetReadDeadline(_ time.Time) error {
  156. return errors.TraceNew("unsupported")
  157. }
  158. func (conn *channelConn) SetWriteDeadline(_ time.Time) error {
  159. return errors.TraceNew("unsupported")
  160. }
  161. func emitMemoryMetrics() {
  162. var memStats runtime.MemStats
  163. runtime.ReadMemStats(&memStats)
  164. NoticeInfo("Memory metrics at %s: goroutines %d | objects %d | alloc %s | inuse %s | sys %s | cumulative %d %s",
  165. stacktrace.GetParentFunctionName(),
  166. runtime.NumGoroutine(),
  167. memStats.HeapObjects,
  168. common.FormatByteCount(memStats.HeapAlloc),
  169. common.FormatByteCount(memStats.HeapInuse+memStats.StackInuse+memStats.MSpanInuse+memStats.MCacheInuse),
  170. common.FormatByteCount(memStats.Sys),
  171. memStats.Mallocs,
  172. common.FormatByteCount(memStats.TotalAlloc))
  173. }
  174. func emitDatastoreMetrics() {
  175. NoticeInfo("Datastore metrics at %s: %s", stacktrace.GetParentFunctionName(), GetDataStoreMetrics())
  176. }
  177. func emitDNSMetrics(resolver *resolver.Resolver) {
  178. NoticeInfo("DNS metrics at %s: %s", stacktrace.GetParentFunctionName(), resolver.GetMetrics())
  179. }
  180. func DoGarbageCollection() {
  181. debug.SetGCPercent(5)
  182. debug.FreeOSMemory()
  183. }
  184. // conditionallyEnabledComponents implements the
  185. // protocol.ConditionallyEnabledComponents interface.
  186. type conditionallyEnabledComponents struct {
  187. }
  188. func (c conditionallyEnabledComponents) QUICEnabled() bool {
  189. return quic.Enabled()
  190. }
  191. func (c conditionallyEnabledComponents) RefractionNetworkingEnabled() bool {
  192. return refraction.Enabled()
  193. }
  194. func (c conditionallyEnabledComponents) InproxyEnabled() bool {
  195. return inproxy.Enabled()
  196. }
  197. // FileMigration represents the action of moving a file, or directory, to a new
  198. // location.
  199. type FileMigration struct {
  200. // Name is the name of the migration for logging because file paths are not
  201. // logged as they may contain sensitive information.
  202. Name string
  203. // OldPath is the current location of the file.
  204. OldPath string
  205. // NewPath is the location that the file should be moved to.
  206. NewPath string
  207. // IsDir should be set to true if the file is a directory.
  208. IsDir bool
  209. }
  210. // DoFileMigration performs the specified file move operation. An error will be
  211. // returned and the operation will not performed if: a file is expected, but a
  212. // directory is found; a directory is expected, but a file is found; or a file,
  213. // or directory, already exists at the target path of the move operation.
  214. // Note: an attempt is made to redact any file paths from the returned error.
  215. func DoFileMigration(migration FileMigration) error {
  216. // Prefix string added to any errors for debug purposes.
  217. errPrefix := ""
  218. if len(migration.Name) > 0 {
  219. errPrefix = fmt.Sprintf("(%s) ", migration.Name)
  220. }
  221. if !common.FileExists(migration.OldPath) {
  222. return errors.TraceNew(errPrefix + "old path does not exist")
  223. }
  224. info, err := os.Stat(migration.OldPath)
  225. if err != nil {
  226. return errors.Tracef(errPrefix+"error getting file info: %s", common.RedactFilePathsError(err, migration.OldPath))
  227. }
  228. if info.IsDir() != migration.IsDir {
  229. if migration.IsDir {
  230. return errors.TraceNew(errPrefix + "expected directory but found file")
  231. }
  232. return errors.TraceNew(errPrefix + "expected but found directory")
  233. }
  234. if common.FileExists(migration.NewPath) {
  235. return errors.TraceNew(errPrefix + "file already exists, will not overwrite")
  236. }
  237. err = os.Rename(migration.OldPath, migration.NewPath)
  238. if err != nil {
  239. return errors.Tracef(errPrefix+"renaming file failed with error %s", common.RedactFilePathsError(err, migration.OldPath, migration.NewPath))
  240. }
  241. return nil
  242. }
  243. // GetNetworkType returns a network type name, suitable for metrics, which is
  244. // derived from the network ID.
  245. func GetNetworkType(networkID string) string {
  246. // Unlike the logic in loggingNetworkIDGetter.GetNetworkID, we don't take the
  247. // arbitrary text before the first "-" since some platforms without network
  248. // detection support stub in random values to enable tactics. Instead we
  249. // check for and use the common network type prefixes currently used in
  250. // NetworkIDGetter implementations.
  251. if strings.HasPrefix(networkID, "WIFI") {
  252. return "WIFI"
  253. }
  254. if strings.HasPrefix(networkID, "MOBILE") {
  255. return "MOBILE"
  256. }
  257. if strings.HasPrefix(networkID, "WIRED") {
  258. return "WIRED"
  259. }
  260. if strings.HasPrefix(networkID, "VPN") {
  261. return "VPN"
  262. }
  263. return "UNKNOWN"
  264. }
  265. var (
  266. clientAPILevelDisableInproxyPortMapping atomic.Bool
  267. applyClientAPILevelMutex sync.Mutex
  268. )
  269. func applyClientAPILevel(config *Config) {
  270. applyClientAPILevelMutex.Lock()
  271. defer applyClientAPILevelMutex.Unlock()
  272. if config.ClientAPILevel == 0 {
  273. return
  274. }
  275. if runtime.GOOS == "android" {
  276. // When specified, ClientAPILevel should be Build.VERSION.SDK_INT on
  277. // Android.
  278. //
  279. // Avoid android_get_device_api_level call.
  280. //
  281. // Use ClientAPILevel to explicitly set the Android API level in the
  282. // anet packet, which provides a custom alternative to net.Interfaces
  283. // on Android. Applying Build.VERSION.SDK_INT here avoids anet
  284. // calling the lic function android_get_device_api_level call, which
  285. // may not always be available.
  286. //
  287. // The Android API level is required in order for anet to select its
  288. // custom Interfaces on Android >= 11, where the stock net.Interfaces
  289. // doesn't work (see Go issue 40569). The stock net.Interfaces should be
  290. // selected on Android < 11, as testing shows that anet.Interfaces fails
  291. // to enumerate all interfaces, including wlan, on older Android versions.
  292. //
  293. // anet.SetAndroidVersion must be called before the Controller starts in
  294. // order to ensure the explicit version is applied before any call to
  295. // android_get_device_api_level. Note that anet doesn't synchronize
  296. // access to its internal anet.customAndroidApiLevel value;
  297. // applyClientAPILevelMutex prevents concurrent writes by the following
  298. // anet.SetAndroidVersion call, but reads by anet remain unsynchronized
  299. // apart from scheduling the applyClientAPILevel call before the
  300. // Controller starts.
  301. //
  302. // anet.SetAndroidVersion takes the Android OS version, not API level, but
  303. // internally it has only two states: if the specified Android OS version
  304. // is >= 11, customAndroidApiLevel is set to 30 and the custom Interfaces
  305. // is enabled; otherwise, the stock net.Interfaces is used.
  306. if config.ClientAPILevel >= 30 {
  307. anet.SetAndroidVersion(11)
  308. } else {
  309. anet.SetAndroidVersion(10)
  310. }
  311. // Disable the tailscale portmapper on Android < 12.
  312. //
  313. // On Android < 12, the exec.Command calls in tailscale helper
  314. // functions, including likelyHomeRouterIPAndroid, will currently hit
  315. // Go issue 70508: on Go 1.23, the standard library implementation of
  316. // exec.Command calls pidfd_open, which crashes shared libraries with
  317. // SIGSYS, "Cause: seccomp prevented call to disallowed arm64 system
  318. // call 434". Note that tailscale currently works around this by
  319. // patching out the pidfd calls from its fork of Go; see tailscale
  320. // issue 13452.
  321. if config.ClientAPILevel < 31 {
  322. clientAPILevelDisableInproxyPortMapping.Store(true)
  323. }
  324. }
  325. }