main.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. ArOZ Online WebSocket Reflector
  3. DO NOT SEND CONFIDENTIAL INFORMATION TO WSR IN PLAIN TEXT!
  4. Designed to be used with ArOZ Online System for cross module communication purposes
  5. Developed (Glue together) by Toby Chui feat. ArOZ Online Project
  6. **/
  7. // Websocket Related Functions:
  8. // Copyright 2013 The Gorilla WebSocket Authors. All rights reserved.
  9. // Use of this source code is governed by a BSD-style
  10. // license that can be found in the LICENSE file.
  11. package main
  12. import (
  13. "flag"
  14. "log"
  15. "net/http"
  16. "fmt"
  17. "os"
  18. "io/ioutil"
  19. "runtime"
  20. )
  21. var addr = flag.String("addr", ":65530", "http service address")
  22. func check(e error) {
  23. if e != nil {
  24. panic(e)
  25. }
  26. }
  27. func serveHome(w http.ResponseWriter, r *http.Request) {
  28. log.Println("[request]",r.URL)
  29. //For HDS protocol compatibility, these two standard functions are included in the system
  30. if r.URL.Path == "/status"{
  31. //Response will only be given when the websocket server is on, so the return result of status must be ON
  32. fmt.Fprintf(w, "ON")
  33. return
  34. }
  35. if r.URL.Path == "/info"{
  36. //Show standard information for HDS scanner, make sure that they don't try to connect to this instance
  37. fmt.Fprintf(w, "ArOZ Online Websocket Reflector_ref.ao.com.imuslab")
  38. return
  39. }
  40. if r.URL.Path == "/uuid"{
  41. //Read the devices uuid from the dedicated path and return it to the browser
  42. var uuidPath = "/etc/AOB/device_identity.config"
  43. if runtime.GOOS == "windows" {
  44. uuidPath = "C:\\AOB\\device_identity.config"
  45. }
  46. if _, err := os.Stat(uuidPath); err == nil {
  47. //Check if the ArOZ Online UUID file exists or not. If Yes, read uuid from file.
  48. dat, err := ioutil.ReadFile(uuidPath)
  49. check(err)
  50. fmt.Fprintf(w,string(dat))
  51. }else{
  52. //The file not found. Just send an error message.
  53. fmt.Fprintf(w,"ERROR. ArOZ Online device_identify.config file not found.")
  54. }
  55. return
  56. }
  57. if r.URL.Path != "/" {
  58. http.Error(w, "Not found", http.StatusNotFound)
  59. return
  60. }
  61. if r.Method != "GET" {
  62. http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
  63. return
  64. }
  65. http.ServeFile(w, r, "info.html")
  66. }
  67. func main() {
  68. fmt.Println("[ArOZ Online WebSocket Server]")
  69. fmt.Println("[info] init in progress...")
  70. flag.Parse()
  71. hub := newHub()
  72. go hub.run()
  73. fmt.Println("[info] Ready!")
  74. http.HandleFunc("/", serveHome)
  75. http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
  76. serveWs(hub, w, r)
  77. })
  78. err := http.ListenAndServe(*addr, nil)
  79. if err != nil {
  80. log.Fatal("ListenAndServe: ", err)
  81. }
  82. }