system.info.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "os/exec"
  7. "runtime"
  8. "strconv"
  9. "strings"
  10. )
  11. // CPUInfoS xxx
  12. type CPUInfoS struct {
  13. Model string
  14. Freq string
  15. Instruction string
  16. Hardware string
  17. Revision string
  18. }
  19. type LogicalDiskS struct {
  20. DriveLetter string
  21. FileSystem string
  22. FreeSpace string
  23. }
  24. type ArOZInfoS struct {
  25. BuildVersion string
  26. DeviceVendor string
  27. DeviceModel string
  28. VendorIcon string
  29. SN string
  30. }
  31. //InitShowSysInformation xxx
  32. func system_info_serviec_init() {
  33. log.Println("Operation System: " + runtime.GOOS)
  34. log.Println("System Architecture: " + runtime.GOARCH)
  35. if runtime.GOOS == "windows" {
  36. /*
  37. //Skip this shit so it will not lag windows server on launch
  38. log.Println("Windows Version: " + wmicGetinfo("os", "Caption")[0])
  39. log.Println("Total Memory: " + wmicGetinfo("ComputerSystem", "TotalPhysicalMemory")[0] + "B")
  40. log.Println("Processor: " + wmicGetinfo("cpu", "Name")[0])
  41. log.Println("Following disk was detected:")
  42. for _, info := range wmicGetinfo("diskdrive", "Model") {
  43. log.Println(info)
  44. }
  45. */
  46. //this features only working on windows, so display on win at now
  47. http.HandleFunc("/system/info/getCPUinfo", getCPUinfo)
  48. http.HandleFunc("/system/info/ifconfig", ifconfig)
  49. http.HandleFunc("/system/info/getDriveStat", getDriveStat)
  50. http.HandleFunc("/system/info/usbPorts", getUSB)
  51. http.HandleFunc("/system/info/getRAMinfo", getRAMinfo)
  52. } else if runtime.GOOS == "linux" {
  53. //this features only working on windows, so display on win at now
  54. http.HandleFunc("/system/info/getCPUinfo", getCPUinfoLinux)
  55. http.HandleFunc("/system/info/ifconfig", ifconfigLinux)
  56. http.HandleFunc("/system/info/getDriveStat", getDriveStatLinux)
  57. http.HandleFunc("/system/info/usbPorts", getUSBLinux)
  58. http.HandleFunc("/system/info/getRAMinfo", getRAMinfoLinux)
  59. }
  60. http.HandleFunc("/system/info/getArOZInfo", getArOZInfo)
  61. //Register as a system setting
  62. registerSetting(settingModule{
  63. Name: "Host Info",
  64. Desc: "System Information",
  65. IconPath: "SystemAO/info/img/small_icon.png",
  66. Group: "Info",
  67. StartDir: "SystemAO/info/index.html",
  68. })
  69. }
  70. func ifconfigLinux(w http.ResponseWriter, r *http.Request) {
  71. //Check if user has logged in
  72. if system_auth_chkauth(w, r) == false {
  73. redirectToLoginPage(w, r)
  74. return
  75. }
  76. cmdin := `ip link show`
  77. cmd := exec.Command("bash", "-c", cmdin)
  78. networkInterfaces, err := cmd.CombinedOutput()
  79. if err != nil {
  80. networkInterfaces = []byte{}
  81. }
  82. nic := strings.Split(string(networkInterfaces), "\n")
  83. var arr []string
  84. for _, info := range nic {
  85. thisInfo := string(info)
  86. arr = append(arr, thisInfo)
  87. }
  88. var jsonData []byte
  89. jsonData, err = json.Marshal(arr)
  90. if err != nil {
  91. log.Println(err)
  92. }
  93. sendTextResponse(w, string(jsonData))
  94. }
  95. func getDriveStatLinux(w http.ResponseWriter, r *http.Request) {
  96. //Check if user has logged in
  97. if system_auth_chkauth(w, r) == false {
  98. redirectToLoginPage(w, r)
  99. return
  100. }
  101. //Get drive status using df command
  102. cmdin := `df -k | sed -e /Filesystem/d`
  103. cmd := exec.Command("bash", "-c", cmdin)
  104. dev, err := cmd.CombinedOutput()
  105. if err != nil {
  106. dev = []byte{}
  107. }
  108. drives := strings.Split(string(dev), "\n")
  109. if len(drives) == 0 {
  110. sendErrorResponse(w, "Invalid disk information")
  111. return
  112. }
  113. var arr []LogicalDiskS
  114. for _, driveInfo := range drives {
  115. if driveInfo == "" {
  116. continue
  117. }
  118. for strings.Contains(driveInfo, " ") {
  119. driveInfo = strings.Replace(driveInfo, " ", " ", -1)
  120. }
  121. driveInfoChunk := strings.Split(driveInfo, " ")
  122. freespaceInByte, _ := StringToInt64(driveInfoChunk[3])
  123. LogicalDisk := LogicalDiskS{
  124. DriveLetter: driveInfoChunk[5],
  125. FileSystem: driveInfoChunk[0],
  126. FreeSpace: Int64ToString(freespaceInByte * 1024), //df show disk space in 1KB blocks
  127. }
  128. arr = append(arr, LogicalDisk)
  129. }
  130. var jsonData []byte
  131. jsonData, err = json.Marshal(arr)
  132. if err != nil {
  133. log.Println(err)
  134. }
  135. sendTextResponse(w, string(jsonData))
  136. }
  137. func getUSBLinux(w http.ResponseWriter, r *http.Request) {
  138. //Check if user has logged in
  139. if system_auth_chkauth(w, r) == false {
  140. redirectToLoginPage(w, r)
  141. return
  142. }
  143. cmdin := `lsusb`
  144. cmd := exec.Command("bash", "-c", cmdin)
  145. usbd, err := cmd.CombinedOutput()
  146. if err != nil {
  147. usbd = []byte{}
  148. }
  149. usbDrives := strings.Split(string(usbd), "\n")
  150. var arr []string
  151. for _, info := range usbDrives {
  152. arr = append(arr, info)
  153. }
  154. var jsonData []byte
  155. jsonData, err = json.Marshal(arr)
  156. if err != nil {
  157. log.Println(err)
  158. }
  159. sendTextResponse(w, string(jsonData))
  160. }
  161. func systemInfoFilterFilterGrepResults(result string, sep string) string {
  162. if strings.Contains(result, sep) == false {
  163. return result
  164. }
  165. tmp := strings.Split(result, sep)
  166. resultString := tmp[1]
  167. return strings.TrimSpace(resultString)
  168. }
  169. func getCPUinfoLinux(w http.ResponseWriter, r *http.Request) {
  170. //Check if user has logged in
  171. if system_auth_chkauth(w, r) == false {
  172. redirectToLoginPage(w, r)
  173. return
  174. }
  175. cmdin := `cat /proc/cpuinfo | grep -m1 "model name"`
  176. cmd := exec.Command("bash", "-c", cmdin)
  177. hardware, err := cmd.CombinedOutput()
  178. if err != nil {
  179. hardware = []byte("??? ")
  180. }
  181. cmdin = `lscpu | grep -m1 "Model name"`
  182. cmd = exec.Command("bash", "-c", cmdin)
  183. cpuModel, err := cmd.CombinedOutput()
  184. if err != nil {
  185. cpuModel = []byte("Generic Processor")
  186. }
  187. cmdin = `lscpu | grep "CPU max MHz"`
  188. cmd = exec.Command("bash", "-c", cmdin)
  189. speed, err := cmd.CombinedOutput()
  190. if err != nil {
  191. cmdin = `cat /proc/cpuinfo | grep -m1 "cpu MHz"`
  192. cmd = exec.Command("bash", "-c", cmdin)
  193. intelSpeed, err := cmd.CombinedOutput()
  194. if err != nil {
  195. speed = []byte("??? ")
  196. }
  197. speed = intelSpeed
  198. }
  199. cmdin = `cat /proc/cpuinfo | grep -m1 "Hardware"`
  200. cmd = exec.Command("bash", "-c", cmdin)
  201. cpuhardware, err := cmd.CombinedOutput()
  202. if err != nil {
  203. } else {
  204. hardware = cpuhardware
  205. }
  206. //On ARM
  207. cmdin = `cat /proc/cpuinfo | grep -m1 "Revision"`
  208. cmd = exec.Command("bash", "-c", cmdin)
  209. revision, err := cmd.CombinedOutput()
  210. if err != nil {
  211. //On x64
  212. cmdin = `cat /proc/cpuinfo | grep -m1 "family"`
  213. cmd = exec.Command("bash", "-c", cmdin)
  214. intelrev, err := cmd.CombinedOutput()
  215. if err != nil {
  216. revision = []byte("??? ")
  217. } else {
  218. revision = intelrev
  219. }
  220. }
  221. //Get Arch
  222. cmdin = `uname --m`
  223. cmd = exec.Command("bash", "-c", cmdin)
  224. arch, err := cmd.CombinedOutput()
  225. if err != nil {
  226. arch = []byte("??? ")
  227. }
  228. CPUInfo := CPUInfoS{
  229. Freq: systemInfoFilterFilterGrepResults(string(speed), ":"),
  230. Hardware: systemInfoFilterFilterGrepResults(string(hardware), ":"),
  231. Instruction: systemInfoFilterFilterGrepResults(string(arch), ":"),
  232. Model: systemInfoFilterFilterGrepResults(string(cpuModel), ":"),
  233. Revision: systemInfoFilterFilterGrepResults(string(revision), ":"),
  234. }
  235. var jsonData []byte
  236. jsonData, err = json.Marshal(CPUInfo)
  237. if err != nil {
  238. log.Println(err)
  239. }
  240. sendTextResponse(w, string(jsonData))
  241. }
  242. func getRAMinfoLinux(w http.ResponseWriter, r *http.Request) {
  243. cmd := exec.Command("grep", "MemTotal", "/proc/meminfo")
  244. out, _ := cmd.CombinedOutput()
  245. strOut := string(out)
  246. strOut = strings.ReplaceAll(strOut, "MemTotal:", "")
  247. strOut = strings.ReplaceAll(strOut, "kB", "")
  248. strOut = strings.ReplaceAll(strOut, " ", "")
  249. strOut = strings.ReplaceAll(strOut, "\n", "")
  250. ramSize, _ := strconv.Atoi(strOut)
  251. ramSizeInt := ramSize * 1000
  252. var jsonData []byte
  253. jsonData, err := json.Marshal(ramSizeInt)
  254. if err != nil {
  255. log.Println(err)
  256. }
  257. sendTextResponse(w, string(jsonData))
  258. }
  259. func getCPUinfo(w http.ResponseWriter, r *http.Request) {
  260. //Check if user has logged in
  261. if system_auth_chkauth(w, r) == false {
  262. redirectToLoginPage(w, r)
  263. return
  264. }
  265. CPUInfo := CPUInfoS{
  266. Freq: wmicGetinfo("cpu", "CurrentClockSpeed")[0],
  267. Hardware: "unknown",
  268. Instruction: wmicGetinfo("cpu", "Caption")[0],
  269. Model: wmicGetinfo("cpu", "Name")[0],
  270. Revision: "unknown",
  271. }
  272. var jsonData []byte
  273. jsonData, err := json.Marshal(CPUInfo)
  274. if err != nil {
  275. log.Println(err)
  276. }
  277. sendTextResponse(w, string(jsonData))
  278. }
  279. func ifconfig(w http.ResponseWriter, r *http.Request) {
  280. //Check if user has logged in
  281. if system_auth_chkauth(w, r) == false {
  282. redirectToLoginPage(w, r)
  283. return
  284. }
  285. var arr []string
  286. for _, info := range wmicGetinfo("nic", "ProductName") {
  287. arr = append(arr, info)
  288. }
  289. var jsonData []byte
  290. jsonData, err := json.Marshal(arr)
  291. if err != nil {
  292. log.Println(err)
  293. }
  294. sendTextResponse(w, string(jsonData))
  295. }
  296. func getDriveStat(w http.ResponseWriter, r *http.Request) {
  297. //Check if user has logged in
  298. if system_auth_chkauth(w, r) == false {
  299. redirectToLoginPage(w, r)
  300. return
  301. }
  302. var DeviceID []string = wmicGetinfo("logicaldisk", "DeviceID")
  303. var FileSystem []string = wmicGetinfo("logicaldisk", "FileSystem")
  304. var FreeSpace []string = wmicGetinfo("logicaldisk", "FreeSpace")
  305. var arr []LogicalDiskS
  306. for i, info := range DeviceID {
  307. LogicalDisk := LogicalDiskS{
  308. DriveLetter: info,
  309. FileSystem: FileSystem[i],
  310. FreeSpace: FreeSpace[i],
  311. }
  312. arr = append(arr, LogicalDisk)
  313. }
  314. var jsonData []byte
  315. jsonData, err := json.Marshal(arr)
  316. if err != nil {
  317. log.Println(err)
  318. }
  319. sendTextResponse(w, string(jsonData))
  320. }
  321. func getUSB(w http.ResponseWriter, r *http.Request) {
  322. //Check if user has logged in
  323. if system_auth_chkauth(w, r) == false {
  324. redirectToLoginPage(w, r)
  325. return
  326. }
  327. var arr []string
  328. for _, info := range wmicGetinfo("Win32_USBHub", "Description") {
  329. arr = append(arr, info)
  330. }
  331. var jsonData []byte
  332. jsonData, err := json.Marshal(arr)
  333. if err != nil {
  334. log.Println(err)
  335. }
  336. sendTextResponse(w, string(jsonData))
  337. }
  338. func getRAMinfo(w http.ResponseWriter, r *http.Request) {
  339. //Check if user has logged in
  340. if system_auth_chkauth(w, r) == false {
  341. redirectToLoginPage(w, r)
  342. return
  343. }
  344. var RAMsize int = 0
  345. for _, info := range wmicGetinfo("memorychip", "Capacity") {
  346. DIMMCapacity, _ := strconv.Atoi(info)
  347. RAMsize += DIMMCapacity
  348. }
  349. var jsonData []byte
  350. jsonData, err := json.Marshal(RAMsize)
  351. if err != nil {
  352. log.Println(err)
  353. }
  354. sendTextResponse(w, string(jsonData))
  355. }
  356. func getArOZInfo(w http.ResponseWriter, r *http.Request) {
  357. //Check if user has logged in
  358. if system_auth_chkauth(w, r) == false {
  359. redirectToLoginPage(w, r)
  360. return
  361. }
  362. ArOZInfo := ArOZInfoS{
  363. BuildVersion: build_version + "." + internal_version,
  364. DeviceVendor: deviceVendor,
  365. DeviceModel: deviceModel,
  366. VendorIcon: "../../" + iconVendor,
  367. SN: deviceUUID,
  368. }
  369. var jsonData []byte
  370. jsonData, err := json.Marshal(ArOZInfo)
  371. if err != nil {
  372. log.Println(err)
  373. }
  374. sendTextResponse(w, string(jsonData))
  375. }
  376. func wmicGetinfo(wmicName string, itemName string) []string {
  377. //get systeminfo
  378. var InfoStorage []string
  379. cmd := exec.Command("chcp", "65001")
  380. cmd = exec.Command("wmic", wmicName, "list", "full", "/format:list")
  381. if wmicName == "os" {
  382. cmd = exec.Command("wmic", wmicName, "get", "*", "/format:list")
  383. }
  384. if len(wmicName) > 6 {
  385. if wmicName[0:6] == "Win32_" {
  386. cmd = exec.Command("wmic", "path", wmicName, "get", "*", "/format:list")
  387. }
  388. }
  389. out, _ := cmd.CombinedOutput()
  390. strOut := string(out)
  391. strSplitedOut := strings.Split(strOut, "\n")
  392. for _, strConfig := range strSplitedOut {
  393. if strings.Contains(strConfig, "=") {
  394. strSplitedConfig := strings.SplitN(strConfig, "=", 2)
  395. if strSplitedConfig[0] == itemName {
  396. strSplitedConfigReplaced := strings.Replace(strSplitedConfig[1], "\r", "", -1)
  397. InfoStorage = append(InfoStorage, strSplitedConfigReplaced)
  398. }
  399. }
  400. }
  401. if len(InfoStorage) == 0 {
  402. InfoStorage = append(InfoStorage, "Undefined")
  403. }
  404. return InfoStorage
  405. }