support rpc auth secret token

This commit is contained in:
MaysWind 2016-06-04 15:33:40 +08:00
parent 244aac0f3a
commit 0c9dbbf9e2
11 changed files with 146 additions and 169 deletions

View file

@ -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 版本",

View file

@ -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:'
});
})();

View file

@ -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',

View file

@ -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);
}]);
})();

View file

@ -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);
}]);
})();

View file

@ -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);
}]);
})();

View file

@ -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));
}
};
}]);

View file

@ -92,8 +92,9 @@
callback: callback
})
},
getGlobalStat: function (callback) {
getGlobalStat: function (callback, silent) {
return aria2RpcService.getGlobalStat({
silent: !!silent,
callback: function (result) {
if (!callback) {
return;

View file

@ -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;

View file

@ -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');
},

View file

@ -46,6 +46,15 @@
</select>
</div>
</div>
<div class="row">
<div class="setting-key setting-key-without-desc col-sm-4">
<span translate>Aria2 RPC Secret Token</span>
<span class="asterisk">*</span>
</div>
<div class="setting-value col-sm-8">
<input class="form-control" type="password" ng-model="settings.secret" ng-change="settingService.setSecret(settings.secret)"/>
</div>
</div>
<div class="row">
<div class="setting-key setting-key-without-desc col-sm-4">
<span translate>Global Stat Refresh Interval</span>