()
| 764 | } |
| 765 | |
| 766 | func (s *Server) start() (reterr error) { |
| 767 | var closePool closeOnErrorPool |
| 768 | defer closePool.closeAllIfError(&reterr) |
| 769 | |
| 770 | exe, err := os.Executable() |
| 771 | if err != nil { |
| 772 | switch runtime.GOOS { |
| 773 | case "js", "wasip1": |
| 774 | // These platforms don't implement os.Executable (at least as of Go |
| 775 | // 1.21), but we don't really care much: it's only used as a default |
| 776 | // directory and hostname when they're not supplied. But we can fall |
| 777 | // back to "tsnet" as well. |
| 778 | exe = "tsnet" |
| 779 | case "ios", "darwin": |
| 780 | // When compiled as a framework (via TailscaleKit in libtailscale), |
| 781 | // os.Executable() returns an error on iOS. The same failure occurs |
| 782 | // on macOS (darwin) when the framework is loaded in a process |
| 783 | // launched by a debugger or certain host environments (e.g. Xcode), |
| 784 | // where the OS does not expose a resolvable executable path to the |
| 785 | // embedded Go runtime. Fall back to "tsnet" in both cases — the |
| 786 | // value is only used as a default hostname/directory when neither |
| 787 | // Server.Hostname nor Server.Dir is set. |
| 788 | exe = "tsnet" |
| 789 | default: |
| 790 | return err |
| 791 | } |
| 792 | } |
| 793 | prog := strings.TrimSuffix(strings.ToLower(filepath.Base(exe)), ".exe") |
| 794 | |
| 795 | s.hostname = s.Hostname |
| 796 | if s.hostname == "" { |
| 797 | s.hostname = prog |
| 798 | } |
| 799 | |
| 800 | s.rootPath = s.Dir |
| 801 | if s.Store != nil { |
| 802 | _, isMemStore := s.Store.(*mem.Store) |
| 803 | if isMemStore && !s.Ephemeral { |
| 804 | return fmt.Errorf("in-memory store is only supported for Ephemeral nodes") |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | if s.rootPath == "" { |
| 809 | confDir, err := os.UserConfigDir() |
| 810 | if err != nil { |
| 811 | return err |
| 812 | } |
| 813 | s.rootPath = filepath.Join(confDir, "tsnet-"+prog) |
| 814 | } |
| 815 | if err := os.MkdirAll(s.rootPath, 0700); err != nil { |
| 816 | return err |
| 817 | } |
| 818 | if fi, err := os.Stat(s.rootPath); err != nil { |
| 819 | return err |
| 820 | } else if !fi.IsDir() { |
| 821 | return fmt.Errorf("%v is not a directory", s.rootPath) |
| 822 | } |
| 823 |
no test coverage detected