module.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace pineapple;
  3. class Terminal extends Module
  4. {
  5. const TTYD_PATH = "/usr/bin/ttyd";
  6. const TTYD_SD_PATH = "/sd/usr/bin/ttyd";
  7. const LOG_PATH = "/pineapple/modules/Terminal/module.log";
  8. public function route()
  9. {
  10. switch ($this->request->action) {
  11. case "getDependenciesStatus":
  12. $this->getDependenciesStatus();
  13. break;
  14. case "managerDependencies":
  15. $this->managerDependencies();
  16. break;
  17. case "getDependenciesInstallStatus":
  18. $this->getDependenciesInstallStatus();
  19. break;
  20. case "startTerminal":
  21. $this->startTerminal();
  22. break;
  23. case "stopTerminal":
  24. $this->stopTerminal();
  25. break;
  26. case "getStatus":
  27. $this->getStatus();
  28. break;
  29. case "getLog":
  30. $this->getLog();
  31. break;
  32. }
  33. }
  34. protected function addLog($massage)
  35. {
  36. $entry = "[" . date("Y-m-d H:i:s") . "] {$massage}\n";
  37. file_put_contents(self::LOG_PATH, $entry, FILE_APPEND);
  38. }
  39. protected function getTerminalPath()
  40. {
  41. if ($this->isSDAvailable() && file_exists(self::TTYD_SD_PATH)) {
  42. return self::TTYD_SD_PATH;
  43. }
  44. return self::TTYD_PATH;
  45. }
  46. protected function getDependenciesStatus()
  47. {
  48. $response = [
  49. "installed" => false,
  50. "install" => "Install",
  51. "installLabel" => "success",
  52. "processing" => false
  53. ];
  54. if (file_exists("/tmp/terminal.progress")) {
  55. $response["install"] = "Installing...";
  56. $response["installLabel"] = "warning";
  57. $response["processing"] = true;
  58. } else if (!$this->checkPanelVersion()) {
  59. $response["install"] = "Upgrade Pineapple version first!";
  60. $response["installLabel"] = "warning";
  61. } else if ($this->checkDependencyInstalled()) {
  62. $response["install"] = "Remove";
  63. $response["installLabel"] = "danger";
  64. $response["installed"] = true;
  65. }
  66. $this->response = $response;
  67. }
  68. protected function checkPanelVersion()
  69. {
  70. $version = \helper\getFirmwareVersion();
  71. $version = str_replace("+", "", $version);
  72. return version_compare($version, "2.8.0") >= 0;
  73. }
  74. protected function checkDependencyInstalled()
  75. {
  76. if ($this->checkDependency("ttyd")) {
  77. if (!$this->uciGet("ttyd.@ttyd[0].port")) {
  78. $this->uciSet("ttyd.@ttyd[0].port", "1477");
  79. //$this->uciSet("ttyd.@ttyd[0].index", "/pineapple/modules/Terminal/ttyd/iframe.html");
  80. exec("/etc/init.d/ttyd disable");
  81. }
  82. return true;
  83. }
  84. return false;
  85. }
  86. protected function managerDependencies()
  87. {
  88. if (!$this->checkPanelVersion()) {
  89. $this->response = ["success" => true];
  90. return true;
  91. }
  92. $action = $this->checkDependencyInstalled() ? "remove" : "install";
  93. $this->execBackground("/pineapple/modules/Terminal/scripts/dependencies.sh {$action}");
  94. $this->response = ["success" => true];
  95. }
  96. protected function getDependenciesInstallStatus()
  97. {
  98. $this->response = ["success" => !file_exists("/tmp/terminal.progress")];
  99. }
  100. protected function startTerminal()
  101. {
  102. /*
  103. exec("/etc/init.d/ttyd start", $info);
  104. $status = implode("\n", $info);
  105. $this->response = [
  106. "success" => empty(trim($status)),
  107. "message" => $status,
  108. ];
  109. */
  110. $terminal = $this->getTerminalPath();
  111. $status = \helper\checkRunning($terminal);
  112. if (!$status) {
  113. $command = "{$terminal} -p 1477 -i br-lan /bin/login";
  114. $this->execBackground($command);
  115. sleep(1);
  116. $status = \helper\checkRunning($terminal);
  117. if (!$status) {
  118. $this->addLog("Terminal could not be run! command: {$command}");
  119. }
  120. }
  121. $this->response = ["success" => $status];
  122. }
  123. protected function stopTerminal()
  124. {
  125. /*
  126. exec("/etc/init.d/ttyd stop", $info);
  127. $status = implode("\n", $info);
  128. $this->response = [
  129. "success" => empty(trim($status)),
  130. "message" => $status,
  131. ];
  132. */
  133. exec("/usr/bin/pkill ttyd");
  134. $status = \helper\checkRunning($this->getTerminalPath());
  135. if ($status) {
  136. $this->addLog("Terminal could not be stop! command: /usr/bin/pkill ttyd");
  137. }
  138. $this->response = ["success" => !$status];
  139. }
  140. protected function getStatus()
  141. {
  142. $this->response = ["status" => \helper\checkRunning($this->getTerminalPath())];
  143. }
  144. protected function getLog()
  145. {
  146. if (!file_exists(self::LOG_PATH)) {
  147. touch(self::LOG_PATH);
  148. }
  149. $this->response = ["moduleLog" => file_get_contents(self::LOG_PATH)];
  150. }
  151. }