瀏覽代碼

[MODULES][Terminal] Release v1.4

DSR! 2 年之前
父節點
當前提交
3462fe1577

二進制
modules/build/Terminal.tar.gz


文件差異過大導致無法顯示
+ 0 - 0
modules/build/modules.json


+ 39 - 6
modules/src/Terminal/api/module.php

@@ -4,7 +4,9 @@ namespace pineapple;
 
 class Terminal extends Module
 {
-    const TTYD = "/sd/usr/bin/ttyd";
+    const TTYD_PATH = "/usr/bin/ttyd";
+    const TTYD_SD_PATH = "/sd/usr/bin/ttyd";
+    const LOG_PATH = "/pineapple/modules/Terminal/module.log";
 
     public function route()
     {
@@ -27,9 +29,23 @@ class Terminal extends Module
             case "getStatus":
                 $this->getStatus();
                 break;
+            case "getLog":
+                $this->getLog();
+                break;
         }
     }
 
+    protected function addLog($massage)
+    {
+        $entry = "[" . date("Y-m-d H:i:s") . "] {$massage}\n";
+        file_put_contents(self::LOG_PATH, $entry, FILE_APPEND);
+    }
+
+    protected function getTerminalPath()
+    {
+        return $this->isSDAvailable() ? self::TTYD_SD_PATH : self::TTYD_PATH;
+    }
+
     protected function getDependenciesStatus()
     {
         $response = [
@@ -106,12 +122,20 @@ class Terminal extends Module
             "message" => $status,
         ];
         */
-        if (!\helper\checkRunning(self::TTYD)) {
-            $this->execBackground(self::TTYD . ' -p 1477 -i br-lan /bin/login');
+        $terminal = $this->getTerminalPath();
+        $status = \helper\checkRunning($terminal);
+        if (!$status) {
+            $command = "{$terminal} -p 1477 -i br-lan /bin/login";
+            $this->execBackground($command);
+
             sleep(1);
+            $status = \helper\checkRunning($terminal);
+            if (!$status) {
+                $this->addLog("Terminal could not be run! command: {$command}");
+            }
         }
 
-        $this->response = ["success" => \helper\checkRunning(self::TTYD)];
+        $this->response = ["success" => $status];
     }
 
     protected function stopTerminal()
@@ -125,11 +149,20 @@ class Terminal extends Module
         ];
         */
         exec("/usr/bin/pkill ttyd");
-        $this->response = ["success" => !\helper\checkRunning(self::TTYD)];
+        $this->response = ["success" => !\helper\checkRunning($this->getTerminalPath())];
     }
 
     protected function getStatus()
     {
-        $this->response = ["status" => \helper\checkRunning(self::TTYD)];
+        $this->response = ["status" => \helper\checkRunning($this->getTerminalPath())];
+    }
+
+    protected function getLog()
+    {
+        if (!file_exists(self::LOG_PATH)) {
+            touch(self::LOG_PATH);
+        }
+
+        $this->response = ["moduleLog" => file_get_contents(self::LOG_PATH)];
     }
 }

+ 2 - 0
modules/src/Terminal/html/inject.html

@@ -55,6 +55,7 @@ $(function() {
     $scope.status = false;
 
     $scope.openTerminalPanel = (function () {
+        $('#terminal-open').addClass('active');
         $('#terminal-module').collapse('show');
         $('#terminal').collapse('show').append('<iframe id="terminal-iframe" class="terminal-content"src="modules/Terminal/html/iframe.html"></iframe>');
 
@@ -79,6 +80,7 @@ $(function() {
     });
 
     $scope.closeTerminalPanel = (function () {
+        $('#terminal-open').removeClass('active');
         $('#terminal-module').collapse('hide');
         $('#terminal').empty();
     });

+ 12 - 0
modules/src/Terminal/js/module.js

@@ -2,6 +2,7 @@ registerController('Terminal_Dependencies', ['$api', '$scope', '$rootScope', '$i
     $scope.install = "Loading...";
     $scope.installLabel = "";
     $scope.processing = false;
+    $scope.moduleLog = '';
     $rootScope.installedDependencies = false;
 
     $scope.refreshStatus = function () {
@@ -31,6 +32,7 @@ registerController('Terminal_Dependencies', ['$api', '$scope', '$rootScope', '$i
                 if (response.success === true) {
                     $scope.processing = false;
                     $scope.refreshStatus();
+                    $scope.getLog();
                     $interval.cancel(dependenciesInstallStatusInterval);
                 }
             });
@@ -51,5 +53,15 @@ registerController('Terminal_Dependencies', ['$api', '$scope', '$rootScope', '$i
         });
     };
 
+    $scope.getLog = function () {
+        $api.request({
+            module: "Terminal",
+            action: "getLog"
+        }, function (response) {
+            $scope.moduleLog = response.moduleLog;
+        })
+    };
+
     $scope.refreshStatus();
+    $scope.getLog();
 }]);

+ 16 - 3
modules/src/Terminal/module.html

@@ -1,6 +1,6 @@
-<!-- Controls -->
-<div class="row">
-    <div class="col-md-4" ng-controller="Terminal_Dependencies">
+<div class="row" ng-controller="Terminal_Dependencies">
+    <!-- Controls -->
+    <div class="col-md-4">
         <div class="panel panel-default">
             <div class="panel-heading">
                 <h3 class="panel-title">Controls</h3>
@@ -21,4 +21,17 @@
             </div>
         </div>
     </div>
+
+    <!-- Log -->
+    <div class="col-md-12">
+        <div class="panel panel-default">
+            <div class="panel-heading">
+                <h3 class="panel-title">Log</h3>
+            </div>
+            <div class="panel-body">
+                <p ng-show="moduleLog === ''">No log entries found yet.</p>
+                <pre class="scrollable-pre log-pre" ng-hide="moduleLog === ''">{{ moduleLog }}</pre>
+            </div>
+        </div>
+    </div>
 </div>

+ 1 - 1
modules/src/Terminal/module.info

@@ -6,6 +6,6 @@
         "tetra"
     ],
     "title": "Terminal",
-    "version": "1.3",
+    "version": "1.4",
     "injectJS": "js/loader.js"
 }

+ 3 - 1
modules/src/Terminal/scripts/dependencies.sh

@@ -1,6 +1,6 @@
 #!/bin/sh
 
-LOGFILE="/tmp/terminal.log"
+LOGFILE="/pineapple/modules/Terminal/module.log"
 TIMESTAMP=`date "+[%Y-%m-%d %H:%M:%S]"`
 
 function add_log {
@@ -29,6 +29,7 @@ if [[ "$1" = "install" ]]; then
         opkg --dest sd install ttyd >> $LOGFILE
         if [[ $? -ne 0 ]]; then
             add_log "ERROR: opkg --dest sd install ttyd failed"
+            rm /tmp/terminal.progress
             exit 1
         fi
     else
@@ -37,6 +38,7 @@ if [[ "$1" = "install" ]]; then
         opkg install ttyd >> $LOGFILE
         if [[ $? -ne 0 ]]; then
             add_log "ERROR: opkg install ttyd failed"
+            rm /tmp/terminal.progress
             exit 1
         fi
     fi

部分文件因文件數量過多而無法顯示