|
1 | 1 | package loader |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bufio" |
4 | 5 | "fmt" |
5 | 6 | "os" |
6 | | - "path/filepath" |
7 | | - "runtime" |
8 | 7 | "strings" |
9 | 8 |
|
10 | 9 | "github.com/JacksonTheMaster/StationeersServerUI/v5/src/config" |
@@ -165,59 +164,35 @@ func PrintConfigDetails(logLevel ...string) { |
165 | 164 | logger.Config.Debug("=======================================") |
166 | 165 | } |
167 | 166 |
|
168 | | -func SanityCheck() error { |
169 | | - |
170 | | - if runtime.GOOS == "windows" { |
171 | | - return nil |
172 | | - } |
173 | | - |
174 | | - // Check if running as root (UID 0) |
175 | | - if os.Geteuid() == 0 { |
176 | | - return fmt.Errorf("root: SSUI should not be run as root") |
| 167 | +func IsInsideContainer() bool { |
| 168 | + // Check .dockerenv file (Docker-specific) |
| 169 | + if _, err := os.Stat("/.dockerenv"); err == nil { |
| 170 | + config.SetIsDockerContainer(true) |
| 171 | + return true |
177 | 172 | } |
| 173 | + // Check cgroup (works for Docker and other container runtimes) |
| 174 | + return isContainerFromCGroup() |
| 175 | +} |
178 | 176 |
|
179 | | - // Get the current executable path from /proc/self/exe |
180 | | - exePath, err := os.Readlink("/proc/self/exe") |
| 177 | +func isContainerFromCGroup() bool { |
| 178 | + file, err := os.Open("/proc/1/cgroup") |
181 | 179 | if err != nil { |
182 | | - return err |
183 | | - } |
184 | | - // Get the directory path of the executable |
185 | | - dirPath := filepath.Dir(exePath) |
186 | | - // Change the working directory to the executable's directory |
187 | | - cwd, err := os.Getwd() |
188 | | - if err != nil { |
189 | | - return err |
190 | | - } |
191 | | - |
192 | | - if cwd != dirPath && !strings.Contains(dirPath, "/tmp") { |
193 | | - err = os.Chdir(dirPath) |
194 | | - if err != nil { |
195 | | - return err |
| 180 | + return false |
| 181 | + } |
| 182 | + defer file.Close() |
| 183 | + |
| 184 | + scanner := bufio.NewScanner(file) |
| 185 | + for scanner.Scan() { |
| 186 | + line := scanner.Text() |
| 187 | + // Check for various container runtime indicators |
| 188 | + if strings.Contains(line, "docker") || |
| 189 | + strings.Contains(line, "containerd") || |
| 190 | + strings.Contains(line, "kubepods") || |
| 191 | + strings.Contains(line, "crio") || |
| 192 | + strings.Contains(line, "libpod") { |
| 193 | + config.SetIsDockerContainer(true) |
| 194 | + return true |
196 | 195 | } |
197 | 196 | } |
198 | | - |
199 | | - // Check if current working directory is writable |
200 | | - workDir, err := os.Getwd() |
201 | | - if err != nil { |
202 | | - return fmt.Errorf("failed to get working directory: %w", err) |
203 | | - } |
204 | | - |
205 | | - // Try to create a temporary file to test write permissions |
206 | | - testFile := filepath.Join(workDir, ".write_test") |
207 | | - if err := os.WriteFile(testFile, []byte("test"), 0600); err != nil { |
208 | | - return fmt.Errorf("cannot write to working directory %s: %w", workDir, err) |
209 | | - } |
210 | | - // Clean up test file |
211 | | - if err := os.Remove(testFile); err != nil { |
212 | | - return fmt.Errorf("failed to clean up sanity check writetest file: %w", err) |
213 | | - } |
214 | | - |
215 | | - // Check if steamcmd package is installed (requires further testing, disabled for now) |
216 | | - //cmd := exec.Command("dpkg-query", "-W", "-f='${Status}'", "steamcmd") |
217 | | - //output, err := cmd.CombinedOutput() |
218 | | - //if err == nil && strings.Contains(string(output), "install ok installed") { |
219 | | - // return fmt.Errorf("steamcmd package is installed") |
220 | | - //} |
221 | | - |
222 | | - return nil |
| 197 | + return false |
223 | 198 | } |
0 commit comments