|
|
@@ -68,6 +68,78 @@ func GetIp() (ip string) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func GetNetworkIps() []string {
|
|
|
+ var networkIps []string
|
|
|
+ ips, err := net.InterfaceAddrs()
|
|
|
+ if err != nil {
|
|
|
+ log.Println(err)
|
|
|
+ return networkIps
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, a := range ips {
|
|
|
+ if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
|
|
|
+ if ipNet.IP.To4() != nil {
|
|
|
+ ip := ipNet.IP.String()
|
|
|
+ // Include common private network ranges
|
|
|
+ if strings.HasPrefix(ip, "10.") ||
|
|
|
+ strings.HasPrefix(ip, "172.") ||
|
|
|
+ strings.HasPrefix(ip, "192.168.") {
|
|
|
+ networkIps = append(networkIps, ip)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return networkIps
|
|
|
+}
|
|
|
+
|
|
|
+// IsRunningInContainer detects if the application is running inside a container
|
|
|
+func IsRunningInContainer() bool {
|
|
|
+ // Method 1: Check for .dockerenv file (Docker containers)
|
|
|
+ if _, err := os.Stat("/.dockerenv"); err == nil {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+
|
|
|
+ // Method 2: Check cgroup for container indicators
|
|
|
+ if data, err := os.ReadFile("/proc/1/cgroup"); err == nil {
|
|
|
+ content := string(data)
|
|
|
+ if strings.Contains(content, "docker") ||
|
|
|
+ strings.Contains(content, "containerd") ||
|
|
|
+ strings.Contains(content, "kubepods") ||
|
|
|
+ strings.Contains(content, "/lxc/") {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Method 3: Check environment variables commonly set by container runtimes
|
|
|
+ containerEnvVars := []string{
|
|
|
+ "KUBERNETES_SERVICE_HOST",
|
|
|
+ "DOCKER_CONTAINER",
|
|
|
+ "container",
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, envVar := range containerEnvVars {
|
|
|
+ if os.Getenv(envVar) != "" {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Method 4: Check if init process is not the traditional init
|
|
|
+ if data, err := os.ReadFile("/proc/1/comm"); err == nil {
|
|
|
+ comm := strings.TrimSpace(string(data))
|
|
|
+ // In containers, process 1 is often not "init" or "systemd"
|
|
|
+ if comm != "init" && comm != "systemd" {
|
|
|
+ // Additional check: if it's a common container entrypoint
|
|
|
+ if strings.Contains(comm, "docker") ||
|
|
|
+ strings.Contains(comm, "containerd") ||
|
|
|
+ strings.Contains(comm, "runc") {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
var sizeKB = 1024
|
|
|
var sizeMB = sizeKB * 1024
|
|
|
var sizeGB = sizeMB * 1024
|