• <tfoot id="ukgsw"><input id="ukgsw"></input></tfoot>
    
    • 久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
      分享

      Golang獲取機器碼(MachineCode、PhysicalId)

       紫火神兵 2019-02-20

      Golang獲取機器碼(MachineCode、PhysicalId)

      sdlyfjx · 大約20小時之前 · 34 次點擊 · 預計閱讀時間 5 分鐘 · 不到1分鐘之前 開始瀏覽    
      package main
      
      import (
      	"context"
      	"crypto/md5"
      	"crypto/rand"
      	"encoding/base64"
      	"encoding/hex"
      	"errors"
      	"fmt"
      	"github.com/StackExchange/wmi"
      	"golang.org/x/sys/windows"
      	"net"
      	"sort"
      	"strings"
      	"time"
      	"unsafe"
      )
      
      func main(){
      	t := time.Now()
      	a:=GetPhysicalID()
      	fmt.Println(time.Since(t), a)
      }
      
      func GetPhysicalID() string{
      	var ids []string
      	if guid,err := getMachineGuid(); err != nil{
      		panic(err.Error())
      	}else{
      		ids = append(ids, guid)
      	}
      	if cpuinfo,err := getCPUInfo();err != nil && len(cpuinfo) > 0 {
      		panic(err.Error())
      	}else{
      		ids = append(ids, cpuinfo[0].VendorID+cpuinfo[0].PhysicalID)
      	}
      	if mac,err := getMACAddress();err!=nil{
      		panic(err.Error())
      	}else{
      		ids = append(ids, mac)
      	}
      	sort.Strings(ids)
      	idsstr := strings.Join(ids, "|/|")
      	return GetMd5String(idsstr,true,true)
      }
      
      func getMACAddress() (string, error){
      	netInterfaces, err := net.Interfaces()
      	if err != nil {
      		panic(err.Error())
      	}
      	mac,macerr := "",errors.New("無法獲取到正確的MAC地址")
      	for i := 0; i < len(netInterfaces); i++ {
      		//fmt.Println(netInterfaces[i])
      		if (netInterfaces[i].Flags & net.FlagUp) != 0 && (netInterfaces[i].Flags & net.FlagLoopback) == 0{
      			addrs, _ := netInterfaces[i].Addrs()
      			for _, address := range addrs {
      				ipnet, ok := address.(*net.IPNet)
      				//fmt.Println(ipnet.IP)
      				if  ok && ipnet.IP.IsGlobalUnicast() {
      					// 如果IP是全局單撥地址,則返回MAC地址
      					mac = netInterfaces[i].HardwareAddr.String()
      					return mac,nil
      				}
      			}
      		}
      	}
      	return mac,macerr
      }
      
      type cpuInfo struct {
      	CPU        int32    `json:"cpu"`
      	VendorID   string   `json:"vendorId"`
      	PhysicalID string   `json:"physicalId"`
      }
      
      type win32_Processor struct {
      	Manufacturer              string
      	ProcessorID               *string
      }
      
      func getCPUInfo() ([]cpuInfo, error) {
      	var ret []cpuInfo
      	var dst []win32_Processor
      	q := wmi.CreateQuery(&dst, "")
      	fmt.Println(q)
      	if err := wmiQuery(q, &dst); err != nil {
      		return ret, err
      	}
      
      	var procID string
      	for i, l := range dst {
      		procID = ""
      		if l.ProcessorID != nil {
      			procID = *l.ProcessorID
      		}
      
      		cpu := cpuInfo{
      			CPU:        int32(i),
      			VendorID:   l.Manufacturer,
      			PhysicalID: procID,
      		}
      		ret = append(ret, cpu)
      	}
      
      	return ret, nil
      }
      
      // WMIQueryWithContext - wraps wmi.Query with a timed-out context to avoid hanging
      func wmiQuery(query string, dst interface{}, connectServerArgs ...interface{}) error {
      	ctx := context.Background()
      	if _, ok := ctx.Deadline(); !ok {
      		ctxTimeout, cancel := context.WithTimeout(ctx, 3000000000)//超時時間3s
      		defer cancel()
      		ctx = ctxTimeout
      	}
      
      	errChan := make(chan error, 1)
      	go func() {
      		errChan <- wmi.Query(query, dst, connectServerArgs...)
      	}()
      
      	select {
      	case <-ctx.Done():
      		return ctx.Err()
      	case err := <-errChan:
      		return err
      	}
      }
      
      func getMachineGuid() (string, error) {
      	// there has been reports of issues on 32bit using golang.org/x/sys/windows/registry, see https://github.com/shirou/gopsutil/pull/312#issuecomment-277422612
      	// for rationale of using windows.RegOpenKeyEx/RegQueryValueEx instead of registry.OpenKey/GetStringValue
      	var h windows.Handle
      	err := windows.RegOpenKeyEx(windows.HKEY_LOCAL_MACHINE, windows.StringToUTF16Ptr(`SOFTWARE\Microsoft\Cryptography`), 0, windows.KEY_READ|windows.KEY_WOW64_64KEY, &h)
      	if err != nil {
      		return "", err
      	}
      	defer windows.RegCloseKey(h)
      
      	const windowsRegBufLen = 74 // len(`{`) + len(`abcdefgh-1234-456789012-123345456671` * 2) + len(`}`) // 2 == bytes/UTF16
      	const uuidLen = 36
      
      	var regBuf [windowsRegBufLen]uint16
      	bufLen := uint32(windowsRegBufLen)
      	var valType uint32
      	err = windows.RegQueryValueEx(h, windows.StringToUTF16Ptr(`MachineGuid`), nil, &valType, (*byte)(unsafe.Pointer(&regBuf[0])), &bufLen)
      	if err != nil {
      		return "", err
      	}
      
      	hostID := windows.UTF16ToString(regBuf[:])
      	hostIDLen := len(hostID)
      	if hostIDLen != uuidLen {
      		return "", fmt.Errorf("HostID incorrect: %q\n", hostID)
      	}
      
      	return hostID, nil
      }
      
      //生成32位md5字串
      func GetMd5String(s string, upper bool, half bool) string {
      	h := md5.New()
      	h.Write([]byte(s))
      	result := hex.EncodeToString(h.Sum(nil))
      	if upper == true {
      		result = strings.ToUpper(result)
      	}
      	if half == true {
      		result = result[8:24]
      	}
      	return result
      }
      
      //利用隨機數生成Guid字串
      func UniqueId() string {
      	b := make([]byte, 48)
      	if _,err := rand.Read(b); err!=nil{
      		return ""
      	}
      	return GetMd5String(base64.URLEncoding.EncodeToString(b), true,false)
      } 
      這里僅根據CPU、系統GUID和MAC地址來生成的機器碼。WINDOWS下貌似無法獲取硬盤序列號(沒找到怎么獲?。?span style="color:#B2B2B2;">請選中你要保存的內容,粘貼到此文本框

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發表

        請遵守用戶 評論公約

        類似文章 更多

        主站蜘蛛池模板: 亚洲熟女精品一区二区| 人妻中文字幕不卡精品| 亚洲av免费成人在线| 亚欧洲乱码视频一二三区| 中文字幕无码久久一区| 无码激情亚洲一区| 精品麻豆国产色欲色欲色欲WWW| 精品人妻av区乱码| 亚洲日本高清一区二区三区| 99久久久精品免费观看国产 | 无套内射视频囯产| 奇米777四色成人影视| 宅男在线永久免费观看网| 欧美福利电影A在线播放| 国产亚洲AV无码AV男人的天堂| 亚洲精品免费一二三区| 久久99国产精品久久99小说| 少妇无套内射中出视频| 亚洲AV无码专区电影在线观看| 国产成人一区二区三区免费| 亚洲AV无码专区亚洲AV| 亚洲欧美人成电影在线观看| 同桌上课脱裙子让我帮他自慰 | 亚洲伊人久久精品影院| 中文字幕亚洲国产精品| 高清不卡一区二区三区| 免费AV片在线观看网址| 亚洲AV永久纯肉无码精品动漫| 潮喷失禁大喷水无码| 影音先锋啪啪av资源网站| 中文字幕理伦午夜福利片| 欧美巨大极度另类| 日韩欧美一卡2卡3卡4卡无卡免费2020| 熟妇人妻不卡中文字幕| 亚洲中文字幕日产无码成人片| 亚洲成AV人片在线观看麦芽| 免费人成黄页在线观看国产| 熟女系列丰满熟妇AV| 久久综合伊人77777| 亚洲AV伊人久久综合密臀性色 | 理论片午午伦夜理片久久|