mac-cpu.go 969 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. package api
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/shirou/gopsutil/net"
  5. )
  6. func Mac(r *GinRouter) {
  7. r.GET("/mac/info", MacInfo)
  8. }
  9. func MacInfo(c *gin.Context, apictx *ApiSession) (interface{}, error) {
  10. // diskInfo, _ := disk.Partitions(false)
  11. // fmt.Printf("%+v\n", diskInfo)
  12. // netInfo, _ := net.Interfaces()
  13. // fmt.Printf("%+v\n", netInfo)
  14. // cpuInfo, _ := cpu.Info()
  15. // fmt.Printf("%+v\n", cpuInfo)
  16. // memInfo, _ := mem.VirtualMemory()
  17. // fmt.Printf("%+v\n", memInfo)
  18. interfaces, err := net.Interfaces()
  19. if err != nil {
  20. return nil, err
  21. }
  22. macAddr := getRealMacAddr(interfaces)
  23. return UtilMd5(macAddr), nil
  24. }
  25. // b0:6e:bf:c5:32:30
  26. func getRealMacAddr(netInterfaces []net.InterfaceStat) string {
  27. loindex := 0
  28. for _, netInterface := range netInterfaces {
  29. if netInterface.Name == "lo" {
  30. loindex = netInterface.Index
  31. break
  32. }
  33. }
  34. if len(netInterfaces) < loindex+1 {
  35. return ""
  36. }
  37. return netInterfaces[loindex].HardwareAddr
  38. }