//go:build linux
// +build linux

package machineid

import (
	"crypto/md5"
	"encoding/hex"
	"io/ioutil"
	"net"
)

func getMachineID() (s string, err error) {
	paths := []string{
		"/var/lib/dbus/machine-id",
		"/etc/machine-id",
		"/sys/class/dmi/id/product_uuid",
	}
	var (
		buf []byte
	)
	//读取指定的文件
	for _, p := range paths {
		if buf, err = ioutil.ReadFile(p); err == nil {
			s = string(buf)
			return
		}
	}
	//计算网卡信息
	h := md5.New()
	if is, err := net.Interfaces(); err == nil {
		for _, i := range is {
			if i.HardwareAddr != nil {
				h.Write(i.HardwareAddr)
			}
		}
	}
	s = hex.EncodeToString(h.Sum(nil))
	return
}
