hardwareinfo.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package hardwareinfo
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "os/exec"
  7. "strings"
  8. )
  9. /*
  10. Hardware Info
  11. author: tobychui
  12. This module is a migrated module from the original system.info.go script
  13. */
  14. type CPUInfo struct {
  15. Model string
  16. Freq string
  17. Instruction string
  18. Hardware string
  19. Revision string
  20. }
  21. type LogicalDisk struct {
  22. DriveLetter string
  23. FileSystem string
  24. FreeSpace string
  25. }
  26. type ArOZInfo struct {
  27. BuildVersion string
  28. DeviceVendor string
  29. DeviceModel string
  30. VendorIcon string
  31. SN string
  32. HostOS string
  33. CPUArch string
  34. HostName string
  35. }
  36. type Server struct {
  37. hostInfo ArOZInfo
  38. }
  39. func NewInfoServer(a ArOZInfo) *Server {
  40. return &Server{
  41. hostInfo: a,
  42. }
  43. }
  44. /*
  45. PrintSystemHardwareDebugMessage print system information on Windows.
  46. Which is lagging but helpful for debugging wmic on Windows
  47. */
  48. func PrintSystemHardwareDebugMessage() {
  49. log.Println("Windows Version: " + wmicGetinfo("os", "Caption")[0])
  50. log.Println("Total Memory: " + wmicGetinfo("ComputerSystem", "TotalPhysicalMemory")[0] + "B")
  51. log.Println("Processor: " + wmicGetinfo("cpu", "Name")[0])
  52. log.Println("Following disk was detected:")
  53. for _, info := range wmicGetinfo("diskdrive", "Model") {
  54. log.Println(info)
  55. }
  56. }
  57. func (s *Server) GetArOZInfo(w http.ResponseWriter, r *http.Request) {
  58. /*
  59. ArOZInfo := ArOZInfo{
  60. BuildVersion: build_version + "." + internal_version,
  61. DeviceVendor: deviceVendor,
  62. DeviceModel: deviceModel,
  63. VendorIcon: "../../" + iconVendor,
  64. SN: deviceUUID,
  65. HostOS: runtime.GOOS,
  66. CPUArch: runtime.GOARCH,
  67. }
  68. */
  69. var jsonData []byte
  70. jsonData, err := json.Marshal(s.hostInfo)
  71. if err != nil {
  72. log.Println(err)
  73. }
  74. sendJSONResponse(w, string(jsonData))
  75. }
  76. func wmicGetinfo(wmicName string, itemName string) []string {
  77. //get systeminfo
  78. var InfoStorage []string
  79. cmd := exec.Command("chcp", "65001")
  80. cmd = exec.Command("wmic", wmicName, "list", "full", "/format:list")
  81. if wmicName == "os" {
  82. cmd = exec.Command("wmic", wmicName, "get", "*", "/format:list")
  83. }
  84. if len(wmicName) > 6 {
  85. if wmicName[0:6] == "Win32_" {
  86. cmd = exec.Command("wmic", "path", wmicName, "get", "*", "/format:list")
  87. }
  88. }
  89. out, _ := cmd.CombinedOutput()
  90. strOut := string(out)
  91. strSplitedOut := strings.Split(strOut, "\n")
  92. for _, strConfig := range strSplitedOut {
  93. if strings.Contains(strConfig, "=") {
  94. strSplitedConfig := strings.SplitN(strConfig, "=", 2)
  95. if strSplitedConfig[0] == itemName {
  96. strSplitedConfigReplaced := strings.Replace(strSplitedConfig[1], "\r", "", -1)
  97. InfoStorage = append(InfoStorage, strSplitedConfigReplaced)
  98. }
  99. }
  100. }
  101. if len(InfoStorage) == 0 {
  102. InfoStorage = append(InfoStorage, "Undefined")
  103. }
  104. return InfoStorage
  105. }
  106. func filterGrepResults(result string, sep string) string {
  107. if strings.Contains(result, sep) == false {
  108. return result
  109. }
  110. tmp := strings.Split(result, sep)
  111. resultString := tmp[1]
  112. return strings.TrimSpace(resultString)
  113. }