From 82519d6f8739e484a8f0b6abef5b9a62f895650a Mon Sep 17 00:00:00 2001
From: zhangzengfei <zhangzengfei@iotlink.com>
Date: 星期二, 19 十一月 2019 19:24:59 +0800
Subject: [PATCH] fix: machine code

---
 licence.go |   56 +++++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 53 insertions(+), 3 deletions(-)

diff --git a/licence.go b/licence.go
index 6469996..14ed41c 100644
--- a/licence.go
+++ b/licence.go
@@ -6,8 +6,9 @@
 	"time"
 
 	"github.com/shirou/gopsutil/cpu"
-	// "github.com/shirou/gopsutil/disk"
 	"github.com/shirou/gopsutil/host"
+	psnet "github.com/shirou/gopsutil/net"
+	// "github.com/shirou/gopsutil/disk"
 )
 
 type Licence struct {
@@ -27,7 +28,7 @@
 	// CPU
 	if cpu, err := cpu.Info(); err == nil {
 		for _, c := range cpu {
-			strings.Join([]string{machineCode, c.String()}, "-")
+			machineCode = strings.Join([]string{machineCode, c.String()}, "-cpu-")
 		}
 	}
 
@@ -41,12 +42,61 @@
 
 	// Host
 	if host, err := host.Info(); err == nil {
-		strings.Join([]string{machineCode, host.HostID}, "-")
+		machineCode = strings.Join([]string{machineCode, host.HostID}, "-host-")
+	}
+
+	// Net
+	if net, err := psnet.Interfaces(); err == nil {
+		for _, n := range net {
+			if n.MTU == 1500 && !VNetFilter(&n) {
+				machineCode = strings.Join([]string{machineCode, n.HardwareAddr}, "-mac-")
+			}
+		}
 	}
 
 	return GetMd5String(machineCode, true, false)
 }
 
+func VNetFilter(iface *psnet.InterfaceStat) bool {
+	vNetNamePrefix := [...]string{
+		"docker",
+		"br-",
+		"veth",
+		"Npcap",
+		"VirtualBox",
+	}
+
+	vNetAddrPrefix := [...]string{
+		"00:05:69", //vmware1
+		"00:0C:29", //vmware2
+		"00:50:56", //vmware3
+		"00:1c:14", //vmware4
+		"00:1C:42", //parallels1
+		"00:03:FF", //microsoft virtual pc
+		"00:0F:4B", //virtual iron 4
+		"00:16:3E", //red hat xen , oracle vm , xen source, novell xen
+		"08:00:27", //virtualbox
+	}
+
+	if iface.HardwareAddr == "" {
+		return true
+	}
+
+	for _, v := range vNetAddrPrefix {
+		if strings.HasPrefix(iface.HardwareAddr, v) {
+			return true
+		}
+	}
+
+	for _, v := range vNetNamePrefix {
+		if strings.HasPrefix(iface.Name, v) {
+			return true
+		}
+	}
+
+	return false
+}
+
 func GenerateLicence(machineCode, timeOut, key string) string {
 	timeLayout := "2006-01-02 15:04:05"  //杞寲鎵�闇�妯℃澘
 	loc, _ := time.LoadLocation("Local") //鑾峰彇鏃跺尯

--
Gitblit v1.8.0