main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "net/http"
  6. "os"
  7. "os/exec"
  8. "os/signal"
  9. "path/filepath"
  10. "runtime"
  11. "strings"
  12. "syscall"
  13. "imuslab.com/WsTTY/mod/aroz"
  14. "imuslab.com/WsTTY/mod/wsshell"
  15. )
  16. var (
  17. handler *aroz.ArozHandler
  18. )
  19. func SetupCloseHandler() {
  20. c := make(chan os.Signal, 2)
  21. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  22. go func() {
  23. <-c
  24. log.Println("\r- Shutting down WsTTY Service.")
  25. os.Exit(0)
  26. }()
  27. }
  28. func main() {
  29. //Create a platform dependent aroz service register
  30. if runtime.GOOS == "windows" {
  31. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  32. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  33. Name: "WsTTY",
  34. Desc: "arozos Websocket Terminal",
  35. Group: "System Tools",
  36. IconPath: "wstty/img/small_icon.png",
  37. Version: "1.1",
  38. StartDir: "wstty/console.html",
  39. SupportFW: true,
  40. LaunchFWDir: "wstty/console.html",
  41. InitFWSize: []int{740, 500},
  42. })
  43. } else {
  44. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  45. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  46. Name: "WsTTY",
  47. Desc: "arozos Websocket Terminal",
  48. Group: "System Tools",
  49. IconPath: "img/icons/wstty/small_icon.png",
  50. Version: "1.1",
  51. StartDir: "wstty/",
  52. SupportFW: true,
  53. LaunchFWDir: "wstty/",
  54. InitFWSize: []int{740, 500},
  55. })
  56. }
  57. SetupCloseHandler()
  58. //Start the gotty and rproxy it to the main system
  59. if runtime.GOOS == "windows" {
  60. //Switch to using wsshell module
  61. terminal := wsshell.NewWebSocketShellTerminal()
  62. http.HandleFunc("/tty/", terminal.HandleOpen)
  63. //Register the standard web services urls
  64. fs := http.FileServer(http.Dir("./web"))
  65. http.Handle("/", fs)
  66. //Any log println will be shown in the core system via STDOUT redirection. But not STDIN.
  67. log.Println("WsTTY (Windows Compatible Mode) started. Listening on " + handler.Port)
  68. err := http.ListenAndServe(handler.Port, nil)
  69. if err != nil {
  70. log.Fatal(err)
  71. }
  72. } else if runtime.GOOS == "linux" {
  73. //Use wstty directly
  74. absolutePath := ""
  75. if runtime.GOARCH == "amd64" {
  76. abs, _ := filepath.Abs("./gotty/gotty_linux_amd64")
  77. absolutePath = abs
  78. } else if runtime.GOARCH == "arm" {
  79. abs, _ := filepath.Abs("./gotty/gotty_linux_arm")
  80. absolutePath = abs
  81. } else if runtime.GOARCH == "arm64" {
  82. abs, _ := filepath.Abs("./gotty/ggotty_linux_arm64")
  83. absolutePath = abs
  84. } else {
  85. //Unsupported platform. Default use amd64
  86. abs, _ := filepath.Abs("./gotty/gotty_linux_amd64")
  87. absolutePath = abs
  88. }
  89. //Extract port number from listening addr
  90. tmp := strings.Split(handler.Port, ":")
  91. tmp = tmp[len(tmp)-1:]
  92. portOnly := strings.Join(tmp, "")
  93. log.Println("WsTTY Started. Listening on ", portOnly)
  94. //Start the gotty. This shoud be blocking by itself
  95. cmd := exec.Command(absolutePath, "-w", "-p", portOnly, "-a", "localhost", "--ws-origin", `\w*`, "bash", "--init-file", "bashstart")
  96. cmd.Stdout = os.Stdout
  97. cmd.Stderr = os.Stderr
  98. if err := cmd.Start(); err != nil {
  99. //Fail to start gotty. Disable this module
  100. ioutil.WriteFile(".disabled", []byte(""), 0755)
  101. return
  102. }
  103. } else {
  104. panic("Not supported platform: " + runtime.GOOS)
  105. }
  106. }