Skip to content

Commit

Permalink
fixed auth
Browse files Browse the repository at this point in the history
  • Loading branch information
jae-jae committed May 7, 2018
1 parent 173f918 commit 5ca7096
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "__MSG_appName__",
"version": "0.0.5",
"version": "0.0.6",
"manifest_version": 2,
"description": "__MSG_appDescription__",
"author": "Jaeger <JaegerCode@gmail.com>",
Expand Down
46 changes: 32 additions & 14 deletions app/scripts.babel/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,20 @@ function add(down) {
function postaria2obj(addobj, callback = null) {
var httppost = new XMLHttpRequest();
var aria2jsonrpcpath = getStorage('path');
httppost.open('POST', aria2jsonrpcpath + '?tm=' + (new Date()).getTime().toString(), true);
var ifregurl = aria2url_reg(aria2jsonrpcpath);
if (ifregurl) {
if (!window.btoa) {
return 'base64_error';

var result = parse_url(aria2jsonrpcpath);
var auth = result[1];
if (auth && auth.indexOf('token:') == 0) {
if (addobj.params) {
addobj.params.unshift(auth);
} else {
httppost.setRequestHeader('Authorization', 'Basic ' + btoa(ifregurl));
addobj.params = [auth];
}
}

httppost.open('POST', result[0] + '?tm=' + (new Date()).getTime().toString(), true);
httppost.setRequestHeader('Authorization', auth);

httppost.onerror = function () {
console.log('Error aria2 configuration!');
if (addobj[0] && addobj[0].method === 'aria2.addUri') {
Expand All @@ -130,11 +135,24 @@ function postaria2obj(addobj, callback = null) {

}

function aria2url_reg(url) {
if (url.split('@')[0] == url) {
return null;
function parse_url(url) {
var auth_str = request_auth(url);
var auth = null;
if (auth_str) {
if (auth_str.indexOf('token:') == 0) {
auth = auth_str;
} else {
auth = 'Basic ' + btoa(auth_str);
}
}
var url_path = remove_auth(url);
function request_auth(url) {
return url.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1];
}
function remove_auth(url) {
return url.replace(/^((?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/, '$1$2$3');
}
return url.split('@')[0].match('/^(http:\\/\\/\|https:\\/\\/)?(.*)\/')[2];
return [url_path, auth];
}


Expand All @@ -159,24 +177,24 @@ function combination(down, cookies) {
header.push('Referer: ' + down.referrer);

if (down.filename == '') {
var post_obj = [{
var post_obj = {
'jsonrpc': '2.0',
'method': 'aria2.addUri',
'id': (new Date()).getTime().toString(),
'params': [[down.finalUrl], {
'header': header
}]
}];
};
} else {
var post_obj = [{
var post_obj = {
'jsonrpc': '2.0',
'method': 'aria2.addUri',
'id': (new Date()).getTime().toString(),
'params': [[down.finalUrl], {
'out': decodeURIComponent(down.filename),
'header': header
}]
}];
};
}
return post_obj;
}
Expand Down

0 comments on commit 5ca7096

Please sign in to comment.