From 36ec36b58f7401f6163f1df3a5fdcdd7e2a57713 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Mon, 20 Mar 2017 23:35:34 +0800 Subject: [PATCH] command line api support set rpc --- src/langs/zh_CN.txt | 5 +- src/scripts/config/defaultLanguage.js | 3 ++ src/scripts/controllers/command.js | 53 +++++++++++++++++++- src/scripts/core/router.js | 4 ++ src/scripts/services/ariaNgSettingService.js | 53 +++++++++++++++++++- 5 files changed, 113 insertions(+), 5 deletions(-) diff --git a/src/langs/zh_CN.txt b/src/langs/zh_CN.txt index 4122a92..6925c6e 100644 --- a/src/langs/zh_CN.txt +++ b/src/langs/zh_CN.txt @@ -177,12 +177,15 @@ Disabled=禁用 BitTorrent=BitTorrent Changes to the settings take effect after refreshing page.=设置将在页面刷新后生效. Type is illegal!=类型错误! -Parameter is invalid!=请求参数无效 +Parameter is invalid!=请求参数无效! Option value cannot be empty!=参数内容不能为空! Input number is invalid!=输入的数字无效! Input number is below min value!=输入的数字小于最小值 {{value}} ! Input number is above max value!=输入的数字大于最大值 {{value}} ! Input value is invalid!=输入的内容无效! +Protocol is invalid!=协议无效! +RPC host cannot be empty!=RPC 主机不能为空! +RPC secret is not base64 encoded!=RPC 密钥不是 Base64 编码后的字符串! Tap to configure and get started with AriaNg.=您还没有进行过设置, 点击这里进行设置. [error] diff --git a/src/scripts/config/defaultLanguage.js b/src/scripts/config/defaultLanguage.js index 042ef62..88c0ca0 100644 --- a/src/scripts/config/defaultLanguage.js +++ b/src/scripts/config/defaultLanguage.js @@ -187,6 +187,9 @@ 'Input number is below min value!': 'Input number is below min value {{value}}!', 'Input number is above max value!': 'Input number is above max value {{value}}!', 'Input value is invalid!': 'Input value is invalid!', + 'Protocol is invalid!': 'Protocol is invalid!', + 'RPC host cannot be empty!': 'RPC host cannot be empty!', + 'RPC secret is not base64 encoded!': 'RPC secret is not base64 encoded!', 'Tap to configure and get started with AriaNg.': 'Tap to configure and get started with AriaNg.', 'error': { 'unknown': 'Unknown error occurred.', diff --git a/src/scripts/controllers/command.js b/src/scripts/controllers/command.js index 92af294..889b886 100644 --- a/src/scripts/controllers/command.js +++ b/src/scripts/controllers/command.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').controller('CommandController', ['$rootScope', '$location', '$routeParams', 'base64', 'ariaNgCommonService', 'aria2TaskService', 'ariaNgLogService', function ($rootScope, $location, $routeParams, base64, ariaNgCommonService, aria2TaskService, ariaNgLogService) { + angular.module('ariaNg').controller('CommandController', ['$rootScope', '$window', '$location', '$routeParams', 'base64', 'ariaNgDefaultOptions', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2TaskService', 'ariaNgLogService', function ($rootScope, $window, $location, $routeParams, base64, ariaNgDefaultOptions, ariaNgCommonService, ariaNgSettingService, aria2TaskService, ariaNgLogService) { var path = $location.path(); var newUrlDownload = function (url) { @@ -22,8 +22,57 @@ var url = base64.urldecode(base64Url); $rootScope.loadPromise = newUrlDownload(url); ariaNgLogService.info('[CommandController] new download: ' + url); + } else if (path.indexOf('/settings/rpc/set/') === 0) { + var rpcProtocol = $routeParams.protocol; + var rpcHost = $routeParams.host; + var rpcPort = $routeParams.port || ariaNgDefaultOptions.rpcPort; + var rpcInterface =$routeParams.interface || ariaNgDefaultOptions.rpcInterface; + var secret = $routeParams.secret || ariaNgDefaultOptions.secret; + + ariaNgLogService.info('[CommandController] set rpc: ' + rpcProtocol + '://' + rpcHost + ':' + rpcPort + '/' + rpcInterface + ', secret: ' + secret); + + if (!rpcProtocol || (rpcProtocol !== 'http' && rpcProtocol !== 'https' && rpcProtocol !== 'ws' && rpcProtocol !== 'wss')) { + ariaNgCommonService.showError('Protocol is invalid!'); + return; + } + + if (!rpcHost) { + ariaNgCommonService.showError('RPC host cannot be empty!'); + return; + } + + if (secret) { + try { + secret = base64.urldecode(secret); + } catch (ex) { + ariaNgCommonService.showError('RPC secret is not base64 encoded!'); + return; + } + } + + var newSetting = { + rpcAlias: '', + rpcHost: rpcHost, + rpcPort: rpcPort, + rpcInterface: rpcInterface, + protocol: rpcProtocol, + httpMethod: ariaNgDefaultOptions.httpMethod, + secret: secret + }; + + if (ariaNgSettingService.isRpcSettingEqualsDefault(newSetting)) { + $location.path('/downloading'); + } else { + ariaNgSettingService.setDefaultRpcSetting(newSetting, { + keepCurrent: false, + forceSet: true + }); + + $location.path('/downloading'); + $window.location.reload(); + } } else { - ariaNgCommonService.error('Parameter is invalid!'); + ariaNgCommonService.showError('Parameter is invalid!'); } }]); }()); diff --git a/src/scripts/core/router.js b/src/scripts/core/router.js index f7cdfb9..89a68b4 100644 --- a/src/scripts/core/router.js +++ b/src/scripts/core/router.js @@ -67,6 +67,10 @@ templateUrl: 'views/settings-aria2.html', controller: 'Aria2SettingsController' }) + .when('/settings/rpc/set/:protocol/:host/:port/:interface/:secret?', { + template: '', + controller: 'CommandController' + }) .when('/status', { templateUrl: 'views/status.html', controller: 'Aria2StatusController' diff --git a/src/scripts/services/ariaNgSettingService.js b/src/scripts/services/ariaNgSettingService.js index c0c9459..d239bed 100644 --- a/src/scripts/services/ariaNgSettingService.js +++ b/src/scripts/services/ariaNgSettingService.js @@ -297,7 +297,12 @@ return setting; }, - setDefaultRpcSetting: function (setting) { + setDefaultRpcSetting: function (setting, params) { + params = angular.extend({ + keepCurrent: true, + forceSet: false + }, params); + var options = getOptions(); var currentSetting = cloneRpcSetting(options); currentSetting.rpcId = ariaNgCommonService.generateUniqueId(); @@ -316,8 +321,19 @@ } } + if (params.forceSet) { + newDefaultSetting = cloneRpcSetting(setting); + + if (newDefaultSetting.secret) { + newDefaultSetting.secret = base64.encode(newDefaultSetting.secret); + } + } + if (newDefaultSetting) { - options.extendRpcServers.splice(0, 0, currentSetting); + if (params.keepCurrent) { + options.extendRpcServers.splice(0, 0, currentSetting); + } + options = angular.extend(options, newDefaultSetting); } @@ -325,6 +341,39 @@ return setting; }, + isRpcSettingEqualsDefault: function (setting) { + if (!setting) { + return false; + } + + var options = this.getAllOptions(); + + if (options.rpcHost !== setting.rpcHost) { + return false; + } + + if (options.rpcPort !== setting.rpcPort) { + return false; + } + + if (options.rpcInterface !== setting.rpcInterface) { + return false; + } + + if (options.protocol !== setting.protocol) { + return false; + } + + if (options.httpMethod !== setting.httpMethod) { + return false; + } + + if (options.secret !== setting.secret) { + return false; + } + + return true; + }, getGlobalStatRefreshInterval: function () { return getOption('globalStatRefreshInterval'); },