-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathali.js
166 lines (149 loc) · 4.39 KB
/
ali.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
Fileball挂载阿里云盘
版本:2.2312
[General]
force-http-engine-hosts = %APPEND% aliyun.example.com:0
[Script]
阿里云盘 = type=http-request,pattern=^http:\/\/aliyun\.example\.com,requires-body=1,script-path=https://raw.githubusercontent.com/githubdulong/Script/master/ali.js,max-size=0,debug=0
作者:@小白脸 @Chosen Ome
使用方法:
添加Synoogy协议,账号随便填,密码填cookie 获取ck的方法如下
阿里云Token获取地址(需要用阿里云盘扫描alist的二维码获得)[ https://alist-doc.nn.ci/docs/driver/aliyundrive ]
Fileball挂载图标:https://raw.githubusercontent.com/githubdulong/Script/master/Images/Fileball.json
*/
var url = $request.url;
//console.log(url);
var accessToken = $persistentStore.read("ali_access_token") ?? "";
var driveId = $persistentStore.read("ali_drive_id") ?? "";
var headers = {
Referer: "https://www.aliyundrive.com/",
"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
"Content-Type": "application/json",
};
var myResponse = {
status: 200,
};
var obj = {};
function hex2str(hex) {
var trimedStr = hex.trim();
var rawStr = trimedStr.substr(0, 2).toLowerCase() === "0x" ? trimedStr.substr(2) : trimedStr;
var len = rawStr.length;
if (len % 2 !== 0) {
return "";
}
var curCharCode;
var resultStr = [];
for (var i = 0; i < len; i = i + 2) {
curCharCode = parseInt(rawStr.substr(i, 2), 16);
resultStr.push(String.fromCharCode(curCharCode));
}
return resultStr.join("");
}
function http(req) {
return new Promise((res) => {
$httpClient.post(req, (err, resp, data) => {
res(JSON.parse(data));
});
});
}
!(async () => {
if (url.indexOf("/webapi/auth.cgi") != -1) {
// 登录接口,替换为刷新refresh token
const body = $request.body;
const password = body.match(/passwd=([^&]*)/)[1];
const refreshToken = $persistentStore.read("ali_refresh_token") ?? password;
const data = {
refresh_token: refreshToken,
grant_type: "refresh_token",
};
const req = {
url: "https://auth.aliyundrive.com/v2/account/token",
headers: headers,
body: JSON.stringify(data),
};
const json = await http(req);
if (json.refresh_token && json.access_token && json.default_drive_id) {
$persistentStore.write(json.refresh_token, "ali_refresh_token");
$persistentStore.write(json.access_token, "ali_access_token");
$persistentStore.write(json.default_drive_id, "ali_drive_id");
obj = {
success: true,
data: {
sid: json.access_token,
},
};
myResponse.body = JSON.stringify(obj);
$done({ response: myResponse });
}
} else if (url.match(/entry\.cgi$/)) {
const body = $request.body;
if (typeof body === "string") {
// 当前的请求为加载目录
if (body.indexOf("list_share") != -1 || body.indexOf("method=list") != -1) {
headers.authorization = "Bearer " + accessToken;
const parentId =
body.match(/folder_path=([^&]*)/) === null ? "root" : body.match(/folder_path=([^&]*)/)[1];
var isRootFolder = parentId === "root";
const data = {
drive_id: driveId,
fields: "*",
parent_file_id: parentId,
limit: 200,
};
const req = {
url: "https://api.aliyundrive.com/v2/file/list",
headers: headers,
body: JSON.stringify(data),
};
const items = (await http(req)).items;
var files = [];
items.forEach(function (item) {
const file = {
isdir: item.type === "folder",
path: item.file_id,
name: item.name,
additional: {
size: item.size,
},
};
files.push(file);
});
const result = isRootFolder
? {
total: 0,
offset: 0,
shares: files,
}
: {
total: 0,
offset: 0,
files: files,
};
obj = {
success: true,
data: result,
};
myResponse.body = JSON.stringify(obj);
$done({ response: myResponse });
}
}
}else{
const fileid =
url.match("fbdownload") ?
hex2str( url.match(/dlink=%22(.*)%22/)[1] ) : url.match(/path=(.*$)/)[1];
const body = {
drive_id: driveId,
expire_sec: 14400,
file_id: fileid,
};
headers.authorization = "Bearer " + accessToken;
const req = {
url: "https://api.aliyundrive.com/v2/file/get_download_url",
headers: headers,
body: JSON.stringify(body),
};
const link = (await http(req)).url;
$done({ response: { status: 302, headers: { Location: link } } });
}
})();