diff --git a/detect.go b/detect.go index ca88dd6..588877d 100644 --- a/detect.go +++ b/detect.go @@ -9,6 +9,7 @@ import ( var hc = &http.Client{Timeout: 120 * time.Millisecond} +// Clouds type type Clouds struct { Aws string Azure string @@ -19,6 +20,7 @@ type Clouds struct { Vr string } +// Detect function func Detect() string { if runtime.GOOS != "darwin" { var c Clouds diff --git a/hypervisor.go b/hypervisor.go index 9154072..f41f715 100644 --- a/hypervisor.go +++ b/hypervisor.go @@ -1,6 +1,8 @@ package libdetectcloud import ( + "regexp" + "github.com/digitalocean/go-smbios/smbios" ) @@ -13,6 +15,15 @@ type BIOS struct { Hypervisor string } +var hypervisors = map[string][]string{ + "VMWare": []string{"vmware", "vm ware"}, + "Xen": []string{"xen"}, + "Hyper-V": []string{"microsoft", "hyperv", "hyper-v", "hyper v"}, + "VirtualBox": []string{"virtual box", "virtual-box", "virtualbox"}, + "KVM": []string{"virt-manager", "virt manager", "virtmanager", "hvm", "kvm"}, + "Parallels": []string{"parallels"}, +} + //BIOSInfo function gets information from the SMBIOS func BIOSInfo() (*BIOS, error) { rc, _, err := smbios.Stream() @@ -39,5 +50,18 @@ func BIOSInfo() (*BIOS, error) { ProductName: info[1], SerialNumber: info[3], } + for h, s := range hypervisors { + for _, i := range s { + re, err := regexp.Compile(`.*` + i + `.*`) + if err != nil { + return b, nil + } + if re.MatchString(b.Manufacturer) || re.MatchString(b.ProductName) { + b.IsVM = true + b.Hypervisor = h + return b, nil + } + } + } return b, nil }