hardwareinfo.go 3.0 KB

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