mirror of
https://github.com/netbirdio/libdetectcloud.git
synced 2026-05-22 17:09:55 -07:00
add hypervisor detection
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user