|
| 1 | +package autostart |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/JacksonTheMaster/StationeersServerUI/v5/src/config" |
| 12 | + "github.com/JacksonTheMaster/StationeersServerUI/v5/src/logger" |
| 13 | +) |
| 14 | + |
| 15 | +func SetupBinarySymlink() error { |
| 16 | + if runtime.GOOS != "linux" { |
| 17 | + return fmt.Errorf("not on linux") |
| 18 | + } |
| 19 | + // Check if we are inside a docker container, if so, don't try to create a symlink |
| 20 | + if IsInsideContainer() { |
| 21 | + logger.Install.Info("Inside a container, skipping symlink creation.") |
| 22 | + return fmt.Errorf("inside a container") |
| 23 | + } |
| 24 | + |
| 25 | + if err := updateSymlink(); err != nil { |
| 26 | + return err |
| 27 | + } |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +func updateSymlink() error { |
| 32 | + // Get the absolute path of the current executable |
| 33 | + executablePath, err := os.Executable() |
| 34 | + if err != nil { |
| 35 | + return err |
| 36 | + } |
| 37 | + |
| 38 | + // Get the directory of the executable |
| 39 | + executableDir := filepath.Dir(executablePath) |
| 40 | + |
| 41 | + // Construct the target filename (e.g., StationeersServerControl5.0.x86_64) |
| 42 | + currentExecutableName := "StationeersServerControlv" + config.GetVersion() + ".x86_64" |
| 43 | + |
| 44 | + // Construct the full path to the target executable |
| 45 | + targetPath := filepath.Join(executableDir, currentExecutableName) |
| 46 | + |
| 47 | + symlinkPath := filepath.Join(executableDir, "StationeersServerUI.lnk") |
| 48 | + |
| 49 | + // Remove existing symlink if it exists |
| 50 | + if err := os.Remove(symlinkPath); err != nil && !os.IsNotExist(err) { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + // Create the symlink |
| 55 | + return os.Symlink(targetPath, symlinkPath) |
| 56 | +} |
| 57 | + |
| 58 | +func IsInsideContainer() bool { |
| 59 | + // Check .dockerenv file (Docker-specific) |
| 60 | + if _, err := os.Stat("/.dockerenv"); err == nil { |
| 61 | + return true |
| 62 | + } |
| 63 | + |
| 64 | + // Check cgroup (works for Docker and other container runtimes) |
| 65 | + return isContainerFromCGroup() |
| 66 | +} |
| 67 | + |
| 68 | +func isContainerFromCGroup() bool { |
| 69 | + file, err := os.Open("/proc/1/cgroup") |
| 70 | + if err != nil { |
| 71 | + return false |
| 72 | + } |
| 73 | + defer file.Close() |
| 74 | + |
| 75 | + scanner := bufio.NewScanner(file) |
| 76 | + for scanner.Scan() { |
| 77 | + line := scanner.Text() |
| 78 | + // Check for various container runtime indicators |
| 79 | + if strings.Contains(line, "docker") || |
| 80 | + strings.Contains(line, "containerd") || |
| 81 | + strings.Contains(line, "kubepods") || |
| 82 | + strings.Contains(line, "crio") || |
| 83 | + strings.Contains(line, "libpod") { |
| 84 | + return true |
| 85 | + } |
| 86 | + } |
| 87 | + return false |
| 88 | +} |
0 commit comments