From 0c9dbbf9e208d73248b15f0d282844f1e8068ef6 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sat, 4 Jun 2016 15:33:40 +0800 Subject: [PATCH] support rpc auth secret token --- app/langs/zh-CN.json | 1 + app/scripts/config/constants.js | 4 +- app/scripts/config/language-default.js | 1 + app/scripts/controllers/list.js | 8 +- app/scripts/controllers/main.js | 8 +- app/scripts/controllers/task-detail.js | 14 +- app/scripts/services/aria2RpcService.js | 236 ++++++++----------- app/scripts/services/aria2SettingService.js | 3 +- app/scripts/services/aria2TaskService.js | 11 +- app/scripts/services/ariaNgSettingService.js | 20 +- app/views/settings-ariang.html | 9 + 11 files changed, 146 insertions(+), 169 deletions(-) diff --git a/app/langs/zh-CN.json b/app/langs/zh-CN.json index 5e71f8f..80e1d26 100644 --- a/app/langs/zh-CN.json +++ b/app/langs/zh-CN.json @@ -74,6 +74,7 @@ "Aria2 RPC Host": "Aria2 RPC 主机", "Aria2 RPC Port": "Aria2 RPC 端口", "Aria2 RPC Protocol": "Aria2 RPC 协议", + "Aria2 RPC Secret Token": "Aria2 RPC 密钥", "Global Stat Refresh Interval": "全局状态刷新间隔", "Download Task Refresh Interval": "下载任务刷新间隔", "Aria2 Version": "Aria2 版本", diff --git a/app/scripts/config/constants.js b/app/scripts/config/constants.js index 2f69299..a69cf36 100644 --- a/app/scripts/config/constants.js +++ b/app/scripts/config/constants.js @@ -11,11 +11,13 @@ rpcHost: '', rpcPort: '6800', protocol: 'http', + secret: '', globalStatRefreshInterval: 1000, downloadTaskRefreshInterval: 1000 }).constant('aria2RpcConstants', { rpcServiceVersion: '2.0', rpcServiceName: 'aria2', - rpcSystemServiceName: 'system' + rpcSystemServiceName: 'system', + rpcTokenPrefix: 'token:' }); })(); diff --git a/app/scripts/config/language-default.js b/app/scripts/config/language-default.js index 1dd3b91..56bca69 100644 --- a/app/scripts/config/language-default.js +++ b/app/scripts/config/language-default.js @@ -78,6 +78,7 @@ 'Aria2 RPC Host': 'Aria2 RPC Host', 'Aria2 RPC Port': 'Aria2 RPC Port', 'Aria2 RPC Protocol': 'Aria2 RPC Protocol', + 'Aria2 RPC Secret Token': 'Aria2 RPC Secret Token', 'Global Stat Refresh Interval': 'Global Stat Refresh Interval', 'Download Task Refresh Interval': 'Download Task Refresh Interval', 'Aria2 Version': 'Aria2 Version', diff --git a/app/scripts/controllers/list.js b/app/scripts/controllers/list.js index 76b785c..528124a 100644 --- a/app/scripts/controllers/list.js +++ b/app/scripts/controllers/list.js @@ -7,7 +7,7 @@ var pauseDownloadTaskRefresh = false; var needRequestWholeInfo = true; - var refreshDownloadTask = function () { + var refreshDownloadTask = function (silent) { if (pauseDownloadTaskRefresh) { return; } @@ -28,7 +28,7 @@ aria2TaskService.processDownloadTasks($rootScope.taskContext.list); $rootScope.taskContext.enableSelectAll = $rootScope.taskContext.list.length > 1; } - }); + }, silent); }; $scope.filterByTaskName = function (task) { @@ -55,7 +55,7 @@ if (ariaNgSettingService.getDownloadTaskRefreshInterval() > 0) { downloadTaskRefreshPromise = $interval(function () { - refreshDownloadTask(); + refreshDownloadTask(true); }, ariaNgSettingService.getDownloadTaskRefreshInterval()); } @@ -84,6 +84,6 @@ } }); - $rootScope.loadPromise = refreshDownloadTask(); + $rootScope.loadPromise = refreshDownloadTask(false); }]); })(); diff --git a/app/scripts/controllers/main.js b/app/scripts/controllers/main.js index d6b6b7d..c82b655 100644 --- a/app/scripts/controllers/main.js +++ b/app/scripts/controllers/main.js @@ -4,10 +4,10 @@ angular.module('ariaNg').controller('MainController', ['$rootScope', '$scope', '$route', '$interval', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $route, $interval, ariaNgCommonService, ariaNgSettingService, aria2TaskService, aria2SettingService) { var globalStatRefreshPromise = null; - var refreshGlobalStat = function () { + var refreshGlobalStat = function (silent) { return aria2SettingService.getGlobalStat(function (result) { $scope.globalStat = result; - }); + }, silent); }; $scope.isTaskSelected = function () { @@ -94,7 +94,7 @@ if (ariaNgSettingService.getGlobalStatRefreshInterval() > 0) { globalStatRefreshPromise = $interval(function () { - refreshGlobalStat(); + refreshGlobalStat(true); }, ariaNgSettingService.getGlobalStatRefreshInterval()); } @@ -104,6 +104,6 @@ } }); - refreshGlobalStat(); + refreshGlobalStat(false); }]); })(); diff --git a/app/scripts/controllers/task-detail.js b/app/scripts/controllers/task-detail.js index cb4eb03..581a3ea 100644 --- a/app/scripts/controllers/task-detail.js +++ b/app/scripts/controllers/task-detail.js @@ -15,20 +15,20 @@ return aria2SettingService.getSpecifiedOptions(keys); }; - var refreshBtPeers = function (task) { + var refreshBtPeers = function (task, silent) { return aria2TaskService.getBtTaskPeers(task.gid, function (result) { if (!ariaNgCommonService.extendArray(result, $scope.peers, 'peerId')) { $scope.peers = result; } $scope.healthPercent = aria2TaskService.estimateHealthPercentFromPeers(task, $scope.peers); - }); + }, silent); }; - var refreshDownloadTask = function () { + var refreshDownloadTask = function (silent) { return aria2TaskService.getTaskStatus($routeParams.gid, function (result) { if (result.status == 'active' && result.bittorrent) { - refreshBtPeers(result); + refreshBtPeers(result, true); } else { if (tabOrders.indexOf('btpeers') >= 0) { tabOrders.splice(tabOrders.indexOf('btpeers'), 1); @@ -44,7 +44,7 @@ $rootScope.taskContext.list = [$scope.task]; $rootScope.taskContext.selected = {}; $rootScope.taskContext.selected[$scope.task.gid] = true; - }); + }, silent); }; $scope.context = { @@ -116,7 +116,7 @@ if (ariaNgSettingService.getDownloadTaskRefreshInterval() > 0) { downloadTaskRefreshPromise = $interval(function () { - refreshDownloadTask(); + refreshDownloadTask(true); }, ariaNgSettingService.getDownloadTaskRefreshInterval()); } @@ -126,6 +126,6 @@ } }); - $rootScope.loadPromise = refreshDownloadTask(); + $rootScope.loadPromise = refreshDownloadTask(false); }]); })(); diff --git a/app/scripts/services/aria2RpcService.js b/app/scripts/services/aria2RpcService.js index 1c62ee8..20555e1 100644 --- a/app/scripts/services/aria2RpcService.js +++ b/app/scripts/services/aria2RpcService.js @@ -4,19 +4,40 @@ angular.module('ariaNg').factory('aria2RpcService', ['$q', 'aria2RpcConstants', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2HttpRpcService', 'aria2WebSocketRpcService', function ($q, aria2RpcConstants, ariaNgCommonService, ariaNgSettingService, aria2HttpRpcService, aria2WebSocketRpcService) { var protocol = ariaNgSettingService.getProtocol(); + var checkIsSystemMethod = function (methodName) { + return methodName.indexOf(aria2RpcConstants.rpcSystemServiceName + '.') == 0; + }; + var getAria2MethodFullName = function (methodName) { return aria2RpcConstants.rpcServiceName + '.' + methodName; }; var invoke = function (method, context) { + var isSystemMethod = checkIsSystemMethod(method); + var rpcSecretToken = ariaNgSettingService.getSecret(); + var finalParams = []; + + if (rpcSecretToken && !isSystemMethod) { + finalParams.push(aria2RpcConstants.rpcTokenPrefix + rpcSecretToken); + } + + if (angular.isArray(context.params) && context.params.length > 0) { + for (var i = 0; i < context.params.length; i++) { + finalParams.push(context.params[i]); + } + } + context.uniqueId = ariaNgCommonService.generateUniqueId(); context.requestBody = { jsonrpc: aria2RpcConstants.rpcServiceVersion, - method: (method.indexOf(aria2RpcConstants.rpcSystemServiceName + '.') != 0 ? getAria2MethodFullName(method) : method), - id: context.uniqueId, - params: context.params + method: (!isSystemMethod ? getAria2MethodFullName(method) : method), + id: context.uniqueId }; + if (finalParams.length > 0) { + context.requestBody.params = finalParams; + } + if (protocol == 'ws') { return aria2WebSocketRpcService.request(context); } else { @@ -44,6 +65,33 @@ }); }; + var buildRequestContext = function () { + var context = {}; + + if (arguments.length > 0) { + var invokeContext = arguments[0]; + + context.silent = invokeContext.silent === true; + context.callback = invokeContext.callback; + } + + if (arguments.length > 1) { + var params = []; + + for (var i = 1; i < arguments.length; i++) { + if (arguments[i] != null && !angular.isUndefined(arguments[i])) { + params.push(arguments[i]); + } + } + + if (params.length > 0) { + context.params = params; + } + } + + return context; + }; + return { getBasicTaskParams: function () { return [ @@ -76,16 +124,10 @@ // return invoke('addMetalink', context); // }, remove: function (context) { - return invoke('remove', { - params: [context.gid], - callback: context.callback - }); + return invoke('remove', buildRequestContext(context, context.gid)); }, forceRemove: function (context) { - return invoke('forceRemove', { - params: [context.gid], - callback: context.callback - }); + return invoke('forceRemove', buildRequestContext(context, context.gid)); }, forceRemoveMulti: function (context) { var contexts = []; @@ -99,21 +141,13 @@ return invokeMulti(this.forceRemove, contexts, 'gid', context.callback); }, pause: function (context) { - return invoke('pause', { - params: [context.gid], - callback: context.callback - }); + return invoke('pause', buildRequestContext(context, context.gid)); }, pauseAll: function (context) { - return invoke('pauseAll', { - callback: context.callback - }); + return invoke('pauseAll', buildRequestContext(context)); }, forcePause: function (context) { - return invoke('forcePause', { - params: [context.gid], - callback: context.callback - }); + return invoke('forcePause', buildRequestContext(context, context.gid)); }, forcePauseMulti: function (context) { var contexts = []; @@ -127,15 +161,10 @@ return invokeMulti(this.forcePause, contexts, 'gid', context.callback); }, forcePauseAll: function (context) { - return invoke('forcePauseAll', { - callback: context.callback - }); + return invoke('forcePauseAll', buildRequestContext(context)); }, unpause: function (context) { - return invoke('unpause', { - params: [context.gid], - callback: context.callback - }); + return invoke('unpause', buildRequestContext(context, context.gid)); }, unpauseMulti: function (context) { var contexts = []; @@ -149,167 +178,88 @@ return invokeMulti(this.unpause, contexts, 'gid', context.callback); }, unpauseAll: function (context) { - return invoke('unpauseAll', { - callback: context.callback - }); + return invoke('unpauseAll', buildRequestContext(context)); }, tellStatus: function (context) { - return invoke('tellStatus', { - params: [context.gid], - callback: context.callback - }); + return invoke('tellStatus', buildRequestContext(context, context.gid)); }, getUris: function (context) { - return invoke('getUris', { - params: [context.gid], - callback: context.callback - }); + return invoke('getUris', buildRequestContext(context, context.gid)); }, getFiles: function (context) { - return invoke('getFiles', { - params: [context.gid], - callback: context.callback - }); + return invoke('getFiles', buildRequestContext(context, context.gid)); }, getPeers: function (context) { - return invoke('getPeers', { - params: [context.gid], - callback: context.callback - }); + return invoke('getPeers', buildRequestContext(context, context.gid)); }, getServers: function (context) { - return invoke('getServers', { - params: [context.gid], - callback: context.callback - }); + return invoke('getServers', buildRequestContext(context, context.gid)); }, tellActive: function (context) { - var requestContext = { - callback: context.callback - }; - - if (context.requestParams) { - requestContext.params = [context.requestParams]; - } - - return invoke('tellActive', requestContext); + return invoke('tellActive', buildRequestContext(context, + angular.isUndefined(context.requestParams) ? null : context.requestParams + )); }, tellWaiting: function (context) { - var requestContext = { - params: [0, 1000], - callback: context.callback - }; - - if (!angular.isUndefined(context.offset)) { - requestContext.params[0] = context.offset; - } - - if (!angular.isUndefined(context.num)) { - requestContext.params[1] = context.num; - } - - if (context.requestParams) { - requestContext.params.push(context.requestParams); - } - - return invoke('tellWaiting', requestContext); + return invoke('tellWaiting', buildRequestContext(context, + angular.isUndefined(context.offset) ? 0 : context.offset, + angular.isUndefined(context.num) ? 1000 : context.num, + angular.isUndefined(context.requestParams) ? null : context.requestParams + )); }, tellStopped: function (context) { - var requestContext = { - params: [0, 1000], - callback: context.callback - }; - - if (!angular.isUndefined(context.offset)) { - requestContext.params[0] = context.offset; - } - - if (!angular.isUndefined(context.num)) { - requestContext.params[1] = context.num; - } - - if (context.requestParams) { - requestContext.params.push(context.requestParams); - } - - return invoke('tellStopped', requestContext); + return invoke('tellStopped', buildRequestContext(context, + angular.isUndefined(context.offset) ? 0 : context.offset, + angular.isUndefined(context.num) ? 1000 : context.num, + angular.isUndefined(context.requestParams) ? null : context.requestParams + )); }, changePosition: function (context) { - return invoke('changePosition', { - params: [context.gid, context.pos, context.how], - callback: context.callback - }); + return invoke('changePosition', buildRequestContext(context, context.gid, context.pos, context.how)); }, // changeUri: function (context) { // return invoke('changeUri', context); // }, getOption: function (context) { - return invoke('getOption', { - params: [context.gid], - callback: context.callback - }); + return invoke('getOption', buildRequestContext(context, context.gid)); }, changeOption: function (context) { - return invoke('changeOption', { - params: [context.gid, context.options], - callback: context.callback - }); + return invoke('changeOption', buildRequestContext(context, context.gid, context.options)); }, getGlobalOption: function (context) { - return invoke('getGlobalOption', { - callback: context.callback - }); + return invoke('getGlobalOption', buildRequestContext(context)); }, changeGlobalOption: function (context) { - return invoke('changeGlobalOption', { - params: [context.options], - callback: context.callback - }); + return invoke('changeGlobalOption', buildRequestContext(context, context.options)); }, getGlobalStat: function (context) { - return invoke('getGlobalStat', { - callback: context.callback - }); + return invoke('getGlobalStat', buildRequestContext(context)); }, purgeDownloadResult: function (context) { - return invoke('purgeDownloadResult', { - callback: context.callback - }); + return invoke('purgeDownloadResult', buildRequestContext(context)); }, removeDownloadResult: function (context) { - return invoke('removeDownloadResult', { - params: [context.gid], - callback: context.callback - }); + return invoke('removeDownloadResult', buildRequestContext(context, context.gid)); }, getVersion: function (context) { - return invoke('getVersion', { - callback: context.callback - }); + return invoke('getVersion', buildRequestContext(context)); }, getSessionInfo: function (context) { - return invoke('getSessionInfo', { - callback: context.callback - }); + return invoke('getSessionInfo', buildRequestContext(context)); }, shutdown: function (context) { - return invoke('shutdown', { - callback: context.callback - }); + return invoke('shutdown', buildRequestContext(context)); }, forceShutdown: function (context) { - return invoke('forceShutdown', { - callback: context.callback - }); + return invoke('forceShutdown', buildRequestContext(context)); }, saveSession: function (context) { - return invoke('saveSession', { - callback: context.callback - }); + return invoke('saveSession', buildRequestContext(context)); }, multicall: function (context) { var requestContext = { params: [], + silent: context.silent === true, callback: context.callback }; @@ -323,9 +273,7 @@ return invoke('system.multicall', requestContext); }, listMethods: function (context) { - return invoke('system.listMethods', { - callback: context.callback - }); + return invoke('system.listMethods', buildRequestContext(context)); } }; }]); diff --git a/app/scripts/services/aria2SettingService.js b/app/scripts/services/aria2SettingService.js index 32d4ac7..ea45630 100644 --- a/app/scripts/services/aria2SettingService.js +++ b/app/scripts/services/aria2SettingService.js @@ -92,8 +92,9 @@ callback: callback }) }, - getGlobalStat: function (callback) { + getGlobalStat: function (callback, silent) { return aria2RpcService.getGlobalStat({ + silent: !!silent, callback: function (result) { if (!callback) { return; diff --git a/app/scripts/services/aria2TaskService.js b/app/scripts/services/aria2TaskService.js index 6a08eea..bf72e0c 100644 --- a/app/scripts/services/aria2TaskService.js +++ b/app/scripts/services/aria2TaskService.js @@ -95,7 +95,7 @@ }; return { - getTaskList: function (type, full, callback) { + getTaskList: function (type, full, callback, silent) { var invokeMethod = null; if (type == 'downloading') { @@ -110,6 +110,7 @@ return invokeMethod({ requestParams: full ? aria2RpcService.getFullTaskParams() : aria2RpcService.getBasicTaskParams(), + silent: !!silent, callback: function (result) { if (!callback) { return; @@ -119,9 +120,10 @@ } }); }, - getTaskStatus: function (gid, callback) { + getTaskStatus: function (gid, callback, silent) { return aria2RpcService.tellStatus({ gid: gid, + silent: !!silent, callback: function (result) { if (!callback) { return; @@ -141,16 +143,17 @@ setTaskOption: function (gid, key, value, callback) { var data = {}; data[key] = value; - + return aria2RpcService.changeOption({ gid: gid, options: data, callback: callback }); }, - getBtTaskPeers: function (gid, callback) { + getBtTaskPeers: function (gid, callback, silent) { return aria2RpcService.getPeers({ gid: gid, + silent: !!silent, callback: function (result) { if (!callback) { return; diff --git a/app/scripts/services/ariaNgSettingService.js b/app/scripts/services/ariaNgSettingService.js index f778bf4..3096335 100644 --- a/app/scripts/services/ariaNgSettingService.js +++ b/app/scripts/services/ariaNgSettingService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgSettingService', ['$location', '$translate', 'amMoment', 'localStorageService', 'ariaNgConstants', 'ariaNgDefaultOptions', 'ariaNgLanguages', function ($location, $translate, amMoment, localStorageService, ariaNgConstants, ariaNgDefaultOptions, ariaNgLanguages) { + angular.module('ariaNg').factory('ariaNgSettingService', ['$location', '$base64', '$translate', 'amMoment', 'localStorageService', 'ariaNgConstants', 'ariaNgDefaultOptions', 'ariaNgLanguages', function ($location, $base64, $translate, amMoment, localStorageService, ariaNgConstants, ariaNgDefaultOptions, ariaNgLanguages) { var getDefaultRpcHost = function () { return $location.$$host; }; @@ -47,11 +47,12 @@ options.rpcHost = getDefaultRpcHost(); } + if (options.secret) { + options.secret = $base64.decode(options.secret); + } + return options; }, - setAllOptions: function (options) { - setOptions(options); - }, applyLanguage: function (lang) { if (!ariaNgLanguages[lang]) { return false; @@ -93,6 +94,17 @@ setProtocol: function (value) { setOption('protocol', value); }, + getSecret: function () { + var value = getOption('secret'); + return (value ? $base64.decode(value) : value); + }, + setSecret: function (value) { + if (value) { + value = $base64.encode(value); + } + + setOption('secret', value); + }, getGlobalStatRefreshInterval: function () { return getOption('globalStatRefreshInterval'); }, diff --git a/app/views/settings-ariang.html b/app/views/settings-ariang.html index 63fc9e6..9df8842 100644 --- a/app/views/settings-ariang.html +++ b/app/views/settings-ariang.html @@ -46,6 +46,15 @@ +
+
+ Aria2 RPC Secret Token + * +
+
+ +
+
Global Stat Refresh Interval