From cd13b76773f241b00f42e8cf70db7fd8578456d6 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Tue, 25 Oct 2022 00:00:08 +0800 Subject: [PATCH] add aria2 rpc debug in debug console page --- src/langs/zh_Hans.txt | 8 +++ src/langs/zh_Hant.txt | 8 +++ src/scripts/config/defaultLanguage.js | 8 +++ src/scripts/controllers/debug.js | 99 ++++++++++++++++++++++++++- src/views/debug.html | 50 +++++++++++++- 5 files changed, 170 insertions(+), 3 deletions(-) diff --git a/src/langs/zh_Hans.txt b/src/langs/zh_Hans.txt index e13705b..b790e5a 100644 --- a/src/langs/zh_Hans.txt +++ b/src/langs/zh_Hans.txt @@ -268,6 +268,14 @@ Clear Logs=清空日志 Are you sure you want to clear debug logs?=您是否要清除调试日志? Show Detail=显示详情 Log Detail=日志详情 +Aria2 RPC Debug=Aria2 RPC 调试 +Aria2 RPC Request Method=Aria2 RPC 请求方法 +Aria2 RPC Request Parameters=Aria2 RPC 请求参数 +Aria2 RPC Response=Aria2 RPC 响应 +Execute=执行 +RPC method is illegal!=RPC方法错误! +AriaNg does not support this RPC method!=AriaNg 不支持该RPC方法! +RPC request parameters are invalid!=RPC 请求参数无效! Type is illegal!=类型错误! Parameter is invalid!=请求参数无效! Option value cannot be empty!=参数内容不能为空! diff --git a/src/langs/zh_Hant.txt b/src/langs/zh_Hant.txt index 75f030a..33f2246 100644 --- a/src/langs/zh_Hant.txt +++ b/src/langs/zh_Hant.txt @@ -268,6 +268,14 @@ Clear Logs=清除記錄 Are you sure you want to clear debug logs?=您是否要清除偵錯記錄? Show Detail=顯示詳情 Log Detail=記錄詳情 +Aria2 RPC Debug=Aria2 RPC 偵錯 +Aria2 RPC Request Method=Aria2 RPC 要求方法 +Aria2 RPC Request Parameters=Aria2 RPC 要求參數 +Aria2 RPC Response=Aria2 RPC 回應 +Execute=執行 +RPC method is illegal!=RPC方法錯誤! +AriaNg does not support this RPC method!=AriaNg 不支援該RPC方法! +RPC request parameters are invalid!=RPC 要求參數無效! Type is illegal!=類型錯誤! Parameter is invalid!=要求參數無效! Option value cannot be empty!=參數內容不能為空! diff --git a/src/scripts/config/defaultLanguage.js b/src/scripts/config/defaultLanguage.js index 5cfb655..9243f6f 100644 --- a/src/scripts/config/defaultLanguage.js +++ b/src/scripts/config/defaultLanguage.js @@ -273,6 +273,14 @@ 'Are you sure you want to clear debug logs?': 'Are you sure you want to clear debug logs?', 'Show Detail': 'Show Detail', 'Log Detail': 'Log Detail', + 'Aria2 RPC Debug': 'Aria2 RPC Debug', + 'Aria2 RPC Request Method': 'Aria2 RPC Request Method', + 'Aria2 RPC Request Parameters': 'Aria2 RPC Request Parameters', + 'Aria2 RPC Response': 'Aria2 RPC Response', + 'Execute': 'Execute', + 'RPC method is illegal!': 'RPC method is illegal!', + 'AriaNg does not support this RPC method!': 'AriaNg does not support this RPC method!', + 'RPC request parameters are invalid!': 'RPC request parameters are invalid!', 'Type is illegal!': 'Type is illegal!', 'Parameter is invalid!': 'Parameter is invalid!', 'Option value cannot be empty!': 'Option value cannot be empty!', diff --git a/src/scripts/controllers/debug.js b/src/scripts/controllers/debug.js index 12ca28f..920e080 100644 --- a/src/scripts/controllers/debug.js +++ b/src/scripts/controllers/debug.js @@ -1,11 +1,15 @@ (function () { 'use strict'; - angular.module('ariaNg').controller('AriaNgDebugController', ['$rootScope', '$scope', '$location', '$interval', '$timeout', 'ariaNgConstants', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', function ($rootScope, $scope, $location, $interval, $timeout, ariaNgConstants, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgSettingService) { + angular.module('ariaNg').controller('AriaNgDebugController', ['$rootScope', '$scope', '$location', '$interval', '$timeout', '$filter', 'ariaNgConstants', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgKeyboardService', 'ariaNgSettingService', 'aria2RpcService', function ($rootScope, $scope, $location, $interval, $timeout, $filter, ariaNgConstants, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgKeyboardService, ariaNgSettingService, aria2RpcService) { var tabStatusItems = [ { name: 'logs', show: true + }, + { + name: 'rpc', + show: true } ]; var debugLogRefreshPromise = null; @@ -22,6 +26,21 @@ return items; }; + var loadAria2RPCMethods = function () { + if ($scope.context.availableRpcMethods && $scope.context.availableRpcMethods.length) { + return; + } + + return aria2RpcService.listMethods({ + silent: false, + callback: function (response) { + if (response.success) { + $scope.context.availableRpcMethods = response.data; + } + } + }); + }; + $scope.context = { currentTab: 'logs', logMaxCount: ariaNgConstants.cachedDebugLogsLimit, @@ -30,7 +49,11 @@ logListDisplayOrder: 'time:desc', logLevelFilter: 'DEBUG', logs: [], - currentLog: null + currentLog: null, + availableRpcMethods: [], + rpcRequestMethod: '', + rpcRequestParameters: '{}', + rpcResponse: null }; $scope.enableDebugMode = function () { @@ -38,6 +61,10 @@ }; $scope.changeTab = function (tabName) { + if (tabName === 'rpc') { + $rootScope.loadPromise = loadAria2RPCMethods(); + } + $scope.context.currentTab = tabName; }; @@ -123,6 +150,74 @@ $scope.context.currentLog = null; }); + $scope.executeAria2Method = function () { + if (!$scope.context.rpcRequestMethod || $scope.context.rpcRequestMethod.indexOf('.') < 0) { + ariaNgCommonService.showError('RPC method is illegal!'); + return; + } + + var methodNameParts = $scope.context.rpcRequestMethod.split('.'); + + if (methodNameParts.length !== 2) { + ariaNgCommonService.showError('RPC method is illegal!'); + return; + } + + var methodName = methodNameParts[1]; + + if (!angular.isFunction(aria2RpcService[methodName])) { + ariaNgCommonService.showError('AriaNg does not support this RPC method!'); + return; + } + + var context = { + silent: false, + callback: function (response) { + if (response) { + $scope.context.rpcResponse = $filter('json')(response.data); + } else { + $scope.context.rpcResponse = $filter('json')(response); + } + } + }; + + var parameters = {}; + + try { + parameters = angular.fromJson($scope.context.rpcRequestParameters); + } catch (ex) { + ariaNgLogService.error('[AriaNgDebugController.executeAria2Method] failed to parse request parameters: ' + $scope.context.rpcRequestParameters, ex); + ariaNgCommonService.showError('RPC request parameters are invalid!'); + return; + } + + for (var key in parameters) { + if (!parameters.hasOwnProperty(key) || key === 'silent' || key === 'callback') { + continue; + } + + context[key] = parameters[key]; + } + + return aria2RpcService[methodName](context); + }; + + $scope.requestParametersTextboxKeyDown = function (event) { + if (!ariaNgSettingService.getKeyboardShortcuts()) { + return; + } + + if (ariaNgKeyboardService.isCtrlEnterPressed(event) && $scope.executeMethodForm.$valid) { + if (event.preventDefault) { + event.preventDefault(); + } + + $scope.executeAria2Method(); + + return false; + } + }; + $scope.$on('$destroy', function () { if (debugLogRefreshPromise) { $interval.cancel(debugLogRefreshPromise); diff --git a/src/views/debug.html b/src/views/debug.html index 3689d7a..3ada626 100644 --- a/src/views/debug.html +++ b/src/views/debug.html @@ -1,9 +1,12 @@ -
+
+
+
+
+
+
+ Aria2 RPC Request Method +
+
+ +
+
+
+
+ Aria2 RPC Request Parameters +
+
+ +
+
+
+
+ Aria2 RPC Response +
+
+ +
+
+
+
+
+ +
+
+
+
+