main.go 3.2 KB

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