hardwareinfo.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package hardwareinfo
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "os/exec"
  7. "strings"
  8. "imuslab.com/arozos/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. }
  64. utils.SendJSONResponse(w, string(jsonData))
  65. }
  66. func wmicGetinfo(wmicName string, itemName string) []string {
  67. //get systeminfo
  68. var InfoStorage []string
  69. cmd := exec.Command("chcp", "65001")
  70. cmd = exec.Command("wmic", wmicName, "list", "full", "/format:list")
  71. if wmicName == "os" {
  72. cmd = exec.Command("wmic", wmicName, "get", "*", "/format:list")
  73. }
  74. if len(wmicName) > 6 {
  75. if wmicName[0:6] == "Win32_" {
  76. cmd = exec.Command("wmic", "path", wmicName, "get", "*", "/format:list")
  77. }
  78. }
  79. out, _ := cmd.CombinedOutput()
  80. strOut := string(out)
  81. strSplitedOut := strings.Split(strOut, "\n")
  82. for _, strConfig := range strSplitedOut {
  83. if strings.Contains(strConfig, "=") {
  84. strSplitedConfig := strings.SplitN(strConfig, "=", 2)
  85. if strSplitedConfig[0] == itemName {
  86. strSplitedConfigReplaced := strings.Replace(strSplitedConfig[1], "\r", "", -1)
  87. InfoStorage = append(InfoStorage, strSplitedConfigReplaced)
  88. }
  89. }
  90. }
  91. if len(InfoStorage) == 0 {
  92. InfoStorage = append(InfoStorage, "Undefined")
  93. }
  94. return InfoStorage
  95. }
  96. func filterGrepResults(result string, sep string) string {
  97. if strings.Contains(result, sep) == false {
  98. return result
  99. }
  100. tmp := strings.Split(result, sep)
  101. resultString := tmp[1]
  102. return strings.TrimSpace(resultString)
  103. }