main.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "os/signal"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "syscall"
  12. "imuslab.com/WsTTY/mod/aroz"
  13. )
  14. var (
  15. handler *aroz.ArozHandler
  16. )
  17. func SetupCloseHandler() {
  18. c := make(chan os.Signal, 2)
  19. signal.Notify(c, os.Interrupt, syscall.SIGTERM)
  20. go func() {
  21. <-c
  22. log.Println("\r- Shutting down WsTTY Service.")
  23. os.Exit(0)
  24. }()
  25. }
  26. func main() {
  27. //Start the aoModule pipeline (which will parse the flags as well). Pass in the module launch information
  28. handler = aroz.HandleFlagParse(aroz.ServiceInfo{
  29. Name: "WsTTY",
  30. Desc: "arozos Websocket Terminal",
  31. Group: "System Tools",
  32. IconPath: "SystemAO/wstty/img/small_icon.png",
  33. Version: "1.0",
  34. StartDir: "wstty/",
  35. SupportFW: true,
  36. LaunchFWDir: "wstty/",
  37. InitFWSize: []int{740, 500},
  38. })
  39. SetupCloseHandler()
  40. //Start the gotty and rproxy it to the main system
  41. if runtime.GOOS == "windows" {
  42. //Not supported, and this should not be compiled to windows binary
  43. panic("Not supported platform")
  44. }
  45. absolutePath := ""
  46. if runtime.GOARCH == "amd64" {
  47. abs, _ := filepath.Abs("./gotty/gotty_linux_amd64")
  48. absolutePath = abs
  49. } else if runtime.GOARCH == "arm" {
  50. abs, _ := filepath.Abs("./gotty/gotty_linux_arm")
  51. absolutePath = abs
  52. } else if runtime.GOARCH == "arm64" {
  53. abs, _ := filepath.Abs("./gotty/ggotty_linux_arm64")
  54. absolutePath = abs
  55. } else {
  56. //Unsupported platform. Default use amd64
  57. abs, _ := filepath.Abs("./gotty/gotty_linux_amd64")
  58. absolutePath = abs
  59. }
  60. //Extract port number from listening addr
  61. tmp := strings.Split(handler.Port, ":")
  62. tmp = tmp[len(tmp)-1:]
  63. portOnly := strings.Join(tmp, "")
  64. log.Println("Starting WsTTY Adapter on: ", portOnly)
  65. //Start the gotty. This shoud be blocking by itself
  66. cmd := exec.Command(absolutePath, "-w", "-p", portOnly, "-a", "localhost", "--ws-origin", `\w*`, "bash", "--init-file", "bashstart")
  67. cmd.Stdout = os.Stdout
  68. cmd.Stderr = os.Stderr
  69. if err := cmd.Start(); err != nil {
  70. //Fail to start gotty. Disable this module
  71. ioutil.WriteFile(".disabled", []byte(""), 0755)
  72. return
  73. }
  74. }