paths.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package paths returns platform and user-specific default paths to
  5. // Tailscale files and directories.
  6. package paths
  7. import (
  8. "os"
  9. "path/filepath"
  10. "runtime"
  11. "sync/atomic"
  12. "tailscale.com/version/distro"
  13. )
  14. // AppSharedDir is a string set by the iOS or Android app on start
  15. // containing a directory we can read/write in.
  16. var AppSharedDir atomic.Value
  17. // DefaultTailscaledSocket returns the path to the tailscaled Unix socket
  18. // or the empty string if there's no reasonable default.
  19. func DefaultTailscaledSocket() string {
  20. if runtime.GOOS == "windows" {
  21. return ""
  22. }
  23. if runtime.GOOS == "darwin" {
  24. return "/var/run/tailscaled.socket"
  25. }
  26. if distro.Get() == distro.Synology {
  27. // TODO(maisem): be smarter about this. We can parse /etc/VERSION.
  28. const dsm6Sock = "/var/packages/Tailscale/etc/tailscaled.sock"
  29. const dsm7Sock = "/var/packages/Tailscale/var/tailscaled.sock"
  30. if fi, err := os.Stat(dsm6Sock); err == nil && !fi.IsDir() {
  31. return dsm6Sock
  32. }
  33. if fi, err := os.Stat(dsm7Sock); err == nil && !fi.IsDir() {
  34. return dsm7Sock
  35. }
  36. }
  37. if fi, err := os.Stat("/var/run"); err == nil && fi.IsDir() {
  38. return "/var/run/tailscale/tailscaled.sock"
  39. }
  40. return "tailscaled.sock"
  41. }
  42. var stateFileFunc func() string
  43. // DefaultTailscaledStateFile returns the default path to the
  44. // tailscaled state file, or the empty string if there's no reasonable
  45. // default value.
  46. func DefaultTailscaledStateFile() string {
  47. if f := stateFileFunc; f != nil {
  48. return f()
  49. }
  50. if runtime.GOOS == "windows" {
  51. return filepath.Join(os.Getenv("LocalAppData"), "Tailscale", "server-state.conf")
  52. }
  53. return ""
  54. }