Compare commits
21 Commits
Author | SHA1 | Date | |
---|---|---|---|
d6d2f52922 | |||
9162e782a0 | |||
dc41ceb99b | |||
c5e274f52a | |||
3781043c78 | |||
1485ab2677 | |||
337bf08cd3 | |||
22665aa19a | |||
1ab6b4e201 | |||
d97afb691b | |||
b63e65880f | |||
44cbe0522c | |||
fedab86c30 | |||
731dbf6c3a | |||
d00f75c814 | |||
f5b8815a84 | |||
99d06c7449 | |||
8e7b2c5837 | |||
3d3a97288a | |||
c2142cc03a | |||
3ce94de823 |
357
alist-proxy.js
357
alist-proxy.js
@ -1,22 +1,36 @@
|
||||
const HOST = "YOUR_HOST";
|
||||
const TOKEN = "YOUR_TOKEN";
|
||||
|
||||
addEventListener("fetch", (event) => {
|
||||
const request = event.request;
|
||||
const url = new URL(request.url);
|
||||
const sign = url.searchParams.get("sign");
|
||||
if (request.method === "OPTIONS") {
|
||||
// Handle CORS preflight requests
|
||||
event.respondWith(handleOptions(request));
|
||||
} else if (sign && sign.length === 16) {
|
||||
// Handle requests to the Down server
|
||||
event.respondWith(handleDownload(request));
|
||||
} else {
|
||||
// Handle requests to the API server
|
||||
event.respondWith(handleRequest(event));
|
||||
}
|
||||
});
|
||||
|
||||
const corsHeaders = {
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
"Access-Control-Allow-Methods": "GET,HEAD,POST,OPTIONS",
|
||||
"Access-Control-Max-Age": "86400",
|
||||
};
|
||||
|
||||
!function(a){"use strict";function b(a,b){var c=(65535&a)+(65535&b),d=(a>>16)+(b>>16)+(c>>16);return d<<16|65535&c}function c(a,b){return a<<b|a>>>32-b}function d(a,d,e,f,g,h){return b(c(b(b(d,a),b(f,h)),g),e)}function e(a,b,c,e,f,g,h){return d(b&c|~b&e,a,b,f,g,h)}function f(a,b,c,e,f,g,h){return d(b&e|c&~e,a,b,f,g,h)}function g(a,b,c,e,f,g,h){return d(b^c^e,a,b,f,g,h)}function h(a,b,c,e,f,g,h){return d(c^(b|~e),a,b,f,g,h)}function i(a,c){a[c>>5]|=128<<c%32,a[(c+64>>>9<<4)+14]=c;var d,i,j,k,l,m=1732584193,n=-271733879,o=-1732584194,p=271733878;for(d=0;d<a.length;d+=16)i=m,j=n,k=o,l=p,m=e(m,n,o,p,a[d],7,-680876936),p=e(p,m,n,o,a[d+1],12,-389564586),o=e(o,p,m,n,a[d+2],17,606105819),n=e(n,o,p,m,a[d+3],22,-1044525330),m=e(m,n,o,p,a[d+4],7,-176418897),p=e(p,m,n,o,a[d+5],12,1200080426),o=e(o,p,m,n,a[d+6],17,-1473231341),n=e(n,o,p,m,a[d+7],22,-45705983),m=e(m,n,o,p,a[d+8],7,1770035416),p=e(p,m,n,o,a[d+9],12,-1958414417),o=e(o,p,m,n,a[d+10],17,-42063),n=e(n,o,p,m,a[d+11],22,-1990404162),m=e(m,n,o,p,a[d+12],7,1804603682),p=e(p,m,n,o,a[d+13],12,-40341101),o=e(o,p,m,n,a[d+14],17,-1502002290),n=e(n,o,p,m,a[d+15],22,1236535329),m=f(m,n,o,p,a[d+1],5,-165796510),p=f(p,m,n,o,a[d+6],9,-1069501632),o=f(o,p,m,n,a[d+11],14,643717713),n=f(n,o,p,m,a[d],20,-373897302),m=f(m,n,o,p,a[d+5],5,-701558691),p=f(p,m,n,o,a[d+10],9,38016083),o=f(o,p,m,n,a[d+15],14,-660478335),n=f(n,o,p,m,a[d+4],20,-405537848),m=f(m,n,o,p,a[d+9],5,568446438),p=f(p,m,n,o,a[d+14],9,-1019803690),o=f(o,p,m,n,a[d+3],14,-187363961),n=f(n,o,p,m,a[d+8],20,1163531501),m=f(m,n,o,p,a[d+13],5,-1444681467),p=f(p,m,n,o,a[d+2],9,-51403784),o=f(o,p,m,n,a[d+7],14,1735328473),n=f(n,o,p,m,a[d+12],20,-1926607734),m=g(m,n,o,p,a[d+5],4,-378558),p=g(p,m,n,o,a[d+8],11,-2022574463),o=g(o,p,m,n,a[d+11],16,1839030562),n=g(n,o,p,m,a[d+14],23,-35309556),m=g(m,n,o,p,a[d+1],4,-1530992060),p=g(p,m,n,o,a[d+4],11,1272893353),o=g(o,p,m,n,a[d+7],16,-155497632),n=g(n,o,p,m,a[d+10],23,-1094730640),m=g(m,n,o,p,a[d+13],4,681279174),p=g(p,m,n,o,a[d],11,-358537222),o=g(o,p,m,n,a[d+3],16,-722521979),n=g(n,o,p,m,a[d+6],23,76029189),m=g(m,n,o,p,a[d+9],4,-640364487),p=g(p,m,n,o,a[d+12],11,-421815835),o=g(o,p,m,n,a[d+15],16,530742520),n=g(n,o,p,m,a[d+2],23,-995338651),m=h(m,n,o,p,a[d],6,-198630844),p=h(p,m,n,o,a[d+7],10,1126891415),o=h(o,p,m,n,a[d+14],15,-1416354905),n=h(n,o,p,m,a[d+5],21,-57434055),m=h(m,n,o,p,a[d+12],6,1700485571),p=h(p,m,n,o,a[d+3],10,-1894986606),o=h(o,p,m,n,a[d+10],15,-1051523),n=h(n,o,p,m,a[d+1],21,-2054922799),m=h(m,n,o,p,a[d+8],6,1873313359),p=h(p,m,n,o,a[d+15],10,-30611744),o=h(o,p,m,n,a[d+6],15,-1560198380),n=h(n,o,p,m,a[d+13],21,1309151649),m=h(m,n,o,p,a[d+4],6,-145523070),p=h(p,m,n,o,a[d+11],10,-1120210379),o=h(o,p,m,n,a[d+2],15,718787259),n=h(n,o,p,m,a[d+9],21,-343485551),m=b(m,i),n=b(n,j),o=b(o,k),p=b(p,l);return[m,n,o,p]}function j(a){var b,c="";for(b=0;b<32*a.length;b+=8)c+=String.fromCharCode(a[b>>5]>>>b%32&255);return c}function k(a){var b,c=[];for(c[(a.length>>2)-1]=void 0,b=0;b<c.length;b+=1)c[b]=0;for(b=0;b<8*a.length;b+=8)c[b>>5]|=(255&a.charCodeAt(b/8))<<b%32;return c}function l(a){return j(i(k(a),8*a.length))}function m(a,b){var c,d,e=k(a),f=[],g=[];for(f[15]=g[15]=void 0,e.length>16&&(e=i(e,8*a.length)),c=0;16>c;c+=1)f[c]=909522486^e[c],g[c]=1549556828^e[c];return d=i(f.concat(k(b)),512+8*b.length),j(i(g.concat(d),640))}function n(a){var b,c,d="0123456789abcdef",e="";for(c=0;c<a.length;c+=1)b=a.charCodeAt(c),e+=d.charAt(b>>>4&15)+d.charAt(15&b);return e}function o(a){return unescape(encodeURIComponent(a))}function p(a){return l(o(a))}function q(a){return n(p(a))}function r(a,b){return m(o(a),o(b))}function s(a,b){return n(r(a,b))}function t(a,b,c){return b?c?r(b,a):s(b,a):c?p(a):q(a)}"function"==typeof define&&define.amd?define(function(){return t}):a.md5=t}(this);
|
||||
|
||||
async function handleRequest(request) {
|
||||
async function handleDownload(request) {
|
||||
const origin = request.headers.get("origin");
|
||||
const url = new URL(request.url);
|
||||
const path = decodeURI(url.pathname);
|
||||
const sign = url.searchParams.get("sign");
|
||||
const name = path.split("/").pop();
|
||||
const right = md5(`alist-${TOKEN}-${name}`).slice(8, 24);
|
||||
if (sign !== right){
|
||||
if (sign !== right) {
|
||||
const resp = new Response(
|
||||
JSON.stringify({
|
||||
code: 401,
|
||||
@ -48,7 +62,7 @@ async function handleRequest(request) {
|
||||
}
|
||||
request = new Request(res.data.url, request);
|
||||
if (res.data.headers) {
|
||||
for(const header of res.data.headers){
|
||||
for (const header of res.data.headers) {
|
||||
request.headers.set(header.name, header.value);
|
||||
}
|
||||
}
|
||||
@ -66,6 +80,143 @@ async function handleRequest(request) {
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Respond to the request
|
||||
* @param {Request} request
|
||||
*/
|
||||
async function handleRequest(event) {
|
||||
const { request } = event;
|
||||
|
||||
//请求头部、返回对象
|
||||
let reqHeaders = new Headers(request.headers),
|
||||
outBody,
|
||||
outStatus = 200,
|
||||
outStatusText = "OK",
|
||||
outCt = null,
|
||||
outHeaders = new Headers({
|
||||
"Access-Control-Allow-Origin": reqHeaders.get("Origin"),
|
||||
"Access-Control-Allow-Methods": "GET, POST, PUT, PATCH, DELETE, OPTIONS",
|
||||
"Access-Control-Allow-Headers":
|
||||
reqHeaders.get("Access-Control-Allow-Headers") ||
|
||||
"Accept, Authorization, Cache-Control, Content-Type, DNT, If-Modified-Since, Keep-Alive, Origin, User-Agent, X-Requested-With, Token, x-access-token, Notion-Version",
|
||||
});
|
||||
|
||||
try {
|
||||
//取域名第一个斜杠后的所有信息为代理链接
|
||||
let url = request.url.substr(8);
|
||||
url = decodeURIComponent(url.substr(url.indexOf("/") + 1));
|
||||
|
||||
//需要忽略的代理
|
||||
if (
|
||||
request.method == "OPTIONS" &&
|
||||
reqHeaders.has("access-control-request-headers")
|
||||
) {
|
||||
//输出提示
|
||||
return new Response(null, PREFLIGHT_INIT);
|
||||
} else if (
|
||||
url.length < 3 ||
|
||||
url.indexOf(".") == -1 ||
|
||||
url == "favicon.ico" ||
|
||||
url == "robots.txt"
|
||||
) {
|
||||
return Response.redirect("https://baidu.com", 301);
|
||||
}
|
||||
//阻断
|
||||
else if (blocker.check(url)) {
|
||||
return Response.redirect("https://baidu.com", 301);
|
||||
} else {
|
||||
//补上前缀 http://
|
||||
url = url
|
||||
.replace(/https:(\/)*/, "https://")
|
||||
.replace(/http:(\/)*/, "http://");
|
||||
if (url.indexOf("://") == -1) {
|
||||
url = "http://" + url;
|
||||
}
|
||||
//构建 fetch 参数
|
||||
let fp = {
|
||||
method: request.method,
|
||||
headers: {},
|
||||
};
|
||||
|
||||
//保留头部其它信息
|
||||
let he = reqHeaders.entries();
|
||||
for (let h of he) {
|
||||
if (!["content-length"].includes(h[0])) {
|
||||
fp.headers[h[0]] = h[1];
|
||||
}
|
||||
}
|
||||
// 是否带 body
|
||||
if (["POST", "PUT", "PATCH", "DELETE"].indexOf(request.method) >= 0) {
|
||||
const ct = (reqHeaders.get("content-type") || "").toLowerCase();
|
||||
if (ct.includes("application/json")) {
|
||||
let requestJSON = await request.json();
|
||||
console.log(typeof requestJSON);
|
||||
fp.body = JSON.stringify(requestJSON);
|
||||
} else if (
|
||||
ct.includes("application/text") ||
|
||||
ct.includes("text/html")
|
||||
) {
|
||||
fp.body = await request.text();
|
||||
} else if (ct.includes("form")) {
|
||||
// fp.body = await request.formData();
|
||||
fp.body = await request.text();
|
||||
} else {
|
||||
fp.body = await request.blob();
|
||||
}
|
||||
}
|
||||
// 发起 fetch
|
||||
let fr = await fetch(url, fp);
|
||||
outCt = fr.headers.get("content-type");
|
||||
if (outCt.includes("application/text") || outCt.includes("text/html")) {
|
||||
try {
|
||||
// 添加base
|
||||
let newFr = new HTMLRewriter()
|
||||
.on("head", {
|
||||
element(element) {
|
||||
element.prepend(`<base href="${url}" />`, {
|
||||
html: true,
|
||||
});
|
||||
},
|
||||
})
|
||||
.transform(fr);
|
||||
fr = newFr;
|
||||
} catch (e) {}
|
||||
}
|
||||
outStatus = fr.status;
|
||||
outStatusText = fr.statusText;
|
||||
outBody = fr.body;
|
||||
}
|
||||
} catch (err) {
|
||||
outCt = "application/json";
|
||||
outBody = JSON.stringify({
|
||||
code: -1,
|
||||
msg: JSON.stringify(err.stack) || err,
|
||||
});
|
||||
}
|
||||
|
||||
//设置类型
|
||||
if (outCt && outCt != "") {
|
||||
outHeaders.set("content-type", outCt);
|
||||
}
|
||||
|
||||
let response = new Response(outBody, {
|
||||
status: outStatus,
|
||||
statusText: outStatusText,
|
||||
headers: outHeaders,
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
const blocker = {
|
||||
keys: [],
|
||||
check: function (url) {
|
||||
url = url.toLowerCase();
|
||||
let len = blocker.keys.filter((x) => url.includes(x)).length;
|
||||
return len != 0;
|
||||
},
|
||||
};
|
||||
|
||||
function handleOptions(request) {
|
||||
// Make sure the necessary headers are present
|
||||
// for this to be a valid pre-flight request
|
||||
@ -101,25 +252,179 @@ function handleOptions(request) {
|
||||
}
|
||||
}
|
||||
|
||||
addEventListener("fetch", (event) => {
|
||||
const request = event.request;
|
||||
// const url = new URL(request.url)
|
||||
if (request.method === "OPTIONS") {
|
||||
// Handle CORS preflight requests
|
||||
event.respondWith(handleOptions(request));
|
||||
} else if (
|
||||
request.method === "GET" ||
|
||||
request.method === "HEAD" ||
|
||||
request.method === "POST"
|
||||
) {
|
||||
// Handle requests to the API server
|
||||
event.respondWith(handleRequest(request));
|
||||
} else {
|
||||
event.respondWith(
|
||||
new Response(null, {
|
||||
status: 405,
|
||||
statusText: "Method Not Allowed",
|
||||
})
|
||||
);
|
||||
!(function (a) {
|
||||
"use strict";
|
||||
function b(a, b) {
|
||||
var c = (65535 & a) + (65535 & b),
|
||||
d = (a >> 16) + (b >> 16) + (c >> 16);
|
||||
return (d << 16) | (65535 & c);
|
||||
}
|
||||
});
|
||||
function c(a, b) {
|
||||
return (a << b) | (a >>> (32 - b));
|
||||
}
|
||||
function d(a, d, e, f, g, h) {
|
||||
return b(c(b(b(d, a), b(f, h)), g), e);
|
||||
}
|
||||
function e(a, b, c, e, f, g, h) {
|
||||
return d((b & c) | (~b & e), a, b, f, g, h);
|
||||
}
|
||||
function f(a, b, c, e, f, g, h) {
|
||||
return d((b & e) | (c & ~e), a, b, f, g, h);
|
||||
}
|
||||
function g(a, b, c, e, f, g, h) {
|
||||
return d(b ^ c ^ e, a, b, f, g, h);
|
||||
}
|
||||
function h(a, b, c, e, f, g, h) {
|
||||
return d(c ^ (b | ~e), a, b, f, g, h);
|
||||
}
|
||||
function i(a, c) {
|
||||
(a[c >> 5] |= 128 << c % 32), (a[(((c + 64) >>> 9) << 4) + 14] = c);
|
||||
var d,
|
||||
i,
|
||||
j,
|
||||
k,
|
||||
l,
|
||||
m = 1732584193,
|
||||
n = -271733879,
|
||||
o = -1732584194,
|
||||
p = 271733878;
|
||||
for (d = 0; d < a.length; d += 16)
|
||||
(i = m),
|
||||
(j = n),
|
||||
(k = o),
|
||||
(l = p),
|
||||
(m = e(m, n, o, p, a[d], 7, -680876936)),
|
||||
(p = e(p, m, n, o, a[d + 1], 12, -389564586)),
|
||||
(o = e(o, p, m, n, a[d + 2], 17, 606105819)),
|
||||
(n = e(n, o, p, m, a[d + 3], 22, -1044525330)),
|
||||
(m = e(m, n, o, p, a[d + 4], 7, -176418897)),
|
||||
(p = e(p, m, n, o, a[d + 5], 12, 1200080426)),
|
||||
(o = e(o, p, m, n, a[d + 6], 17, -1473231341)),
|
||||
(n = e(n, o, p, m, a[d + 7], 22, -45705983)),
|
||||
(m = e(m, n, o, p, a[d + 8], 7, 1770035416)),
|
||||
(p = e(p, m, n, o, a[d + 9], 12, -1958414417)),
|
||||
(o = e(o, p, m, n, a[d + 10], 17, -42063)),
|
||||
(n = e(n, o, p, m, a[d + 11], 22, -1990404162)),
|
||||
(m = e(m, n, o, p, a[d + 12], 7, 1804603682)),
|
||||
(p = e(p, m, n, o, a[d + 13], 12, -40341101)),
|
||||
(o = e(o, p, m, n, a[d + 14], 17, -1502002290)),
|
||||
(n = e(n, o, p, m, a[d + 15], 22, 1236535329)),
|
||||
(m = f(m, n, o, p, a[d + 1], 5, -165796510)),
|
||||
(p = f(p, m, n, o, a[d + 6], 9, -1069501632)),
|
||||
(o = f(o, p, m, n, a[d + 11], 14, 643717713)),
|
||||
(n = f(n, o, p, m, a[d], 20, -373897302)),
|
||||
(m = f(m, n, o, p, a[d + 5], 5, -701558691)),
|
||||
(p = f(p, m, n, o, a[d + 10], 9, 38016083)),
|
||||
(o = f(o, p, m, n, a[d + 15], 14, -660478335)),
|
||||
(n = f(n, o, p, m, a[d + 4], 20, -405537848)),
|
||||
(m = f(m, n, o, p, a[d + 9], 5, 568446438)),
|
||||
(p = f(p, m, n, o, a[d + 14], 9, -1019803690)),
|
||||
(o = f(o, p, m, n, a[d + 3], 14, -187363961)),
|
||||
(n = f(n, o, p, m, a[d + 8], 20, 1163531501)),
|
||||
(m = f(m, n, o, p, a[d + 13], 5, -1444681467)),
|
||||
(p = f(p, m, n, o, a[d + 2], 9, -51403784)),
|
||||
(o = f(o, p, m, n, a[d + 7], 14, 1735328473)),
|
||||
(n = f(n, o, p, m, a[d + 12], 20, -1926607734)),
|
||||
(m = g(m, n, o, p, a[d + 5], 4, -378558)),
|
||||
(p = g(p, m, n, o, a[d + 8], 11, -2022574463)),
|
||||
(o = g(o, p, m, n, a[d + 11], 16, 1839030562)),
|
||||
(n = g(n, o, p, m, a[d + 14], 23, -35309556)),
|
||||
(m = g(m, n, o, p, a[d + 1], 4, -1530992060)),
|
||||
(p = g(p, m, n, o, a[d + 4], 11, 1272893353)),
|
||||
(o = g(o, p, m, n, a[d + 7], 16, -155497632)),
|
||||
(n = g(n, o, p, m, a[d + 10], 23, -1094730640)),
|
||||
(m = g(m, n, o, p, a[d + 13], 4, 681279174)),
|
||||
(p = g(p, m, n, o, a[d], 11, -358537222)),
|
||||
(o = g(o, p, m, n, a[d + 3], 16, -722521979)),
|
||||
(n = g(n, o, p, m, a[d + 6], 23, 76029189)),
|
||||
(m = g(m, n, o, p, a[d + 9], 4, -640364487)),
|
||||
(p = g(p, m, n, o, a[d + 12], 11, -421815835)),
|
||||
(o = g(o, p, m, n, a[d + 15], 16, 530742520)),
|
||||
(n = g(n, o, p, m, a[d + 2], 23, -995338651)),
|
||||
(m = h(m, n, o, p, a[d], 6, -198630844)),
|
||||
(p = h(p, m, n, o, a[d + 7], 10, 1126891415)),
|
||||
(o = h(o, p, m, n, a[d + 14], 15, -1416354905)),
|
||||
(n = h(n, o, p, m, a[d + 5], 21, -57434055)),
|
||||
(m = h(m, n, o, p, a[d + 12], 6, 1700485571)),
|
||||
(p = h(p, m, n, o, a[d + 3], 10, -1894986606)),
|
||||
(o = h(o, p, m, n, a[d + 10], 15, -1051523)),
|
||||
(n = h(n, o, p, m, a[d + 1], 21, -2054922799)),
|
||||
(m = h(m, n, o, p, a[d + 8], 6, 1873313359)),
|
||||
(p = h(p, m, n, o, a[d + 15], 10, -30611744)),
|
||||
(o = h(o, p, m, n, a[d + 6], 15, -1560198380)),
|
||||
(n = h(n, o, p, m, a[d + 13], 21, 1309151649)),
|
||||
(m = h(m, n, o, p, a[d + 4], 6, -145523070)),
|
||||
(p = h(p, m, n, o, a[d + 11], 10, -1120210379)),
|
||||
(o = h(o, p, m, n, a[d + 2], 15, 718787259)),
|
||||
(n = h(n, o, p, m, a[d + 9], 21, -343485551)),
|
||||
(m = b(m, i)),
|
||||
(n = b(n, j)),
|
||||
(o = b(o, k)),
|
||||
(p = b(p, l));
|
||||
return [m, n, o, p];
|
||||
}
|
||||
function j(a) {
|
||||
var b,
|
||||
c = "";
|
||||
for (b = 0; b < 32 * a.length; b += 8)
|
||||
c += String.fromCharCode((a[b >> 5] >>> b % 32) & 255);
|
||||
return c;
|
||||
}
|
||||
function k(a) {
|
||||
var b,
|
||||
c = [];
|
||||
for (c[(a.length >> 2) - 1] = void 0, b = 0; b < c.length; b += 1) c[b] = 0;
|
||||
for (b = 0; b < 8 * a.length; b += 8)
|
||||
c[b >> 5] |= (255 & a.charCodeAt(b / 8)) << b % 32;
|
||||
return c;
|
||||
}
|
||||
function l(a) {
|
||||
return j(i(k(a), 8 * a.length));
|
||||
}
|
||||
function m(a, b) {
|
||||
var c,
|
||||
d,
|
||||
e = k(a),
|
||||
f = [],
|
||||
g = [];
|
||||
for (
|
||||
f[15] = g[15] = void 0, e.length > 16 && (e = i(e, 8 * a.length)), c = 0;
|
||||
16 > c;
|
||||
c += 1
|
||||
)
|
||||
(f[c] = 909522486 ^ e[c]), (g[c] = 1549556828 ^ e[c]);
|
||||
return (d = i(f.concat(k(b)), 512 + 8 * b.length)), j(i(g.concat(d), 640));
|
||||
}
|
||||
function n(a) {
|
||||
var b,
|
||||
c,
|
||||
d = "0123456789abcdef",
|
||||
e = "";
|
||||
for (c = 0; c < a.length; c += 1)
|
||||
(b = a.charCodeAt(c)), (e += d.charAt((b >>> 4) & 15) + d.charAt(15 & b));
|
||||
return e;
|
||||
}
|
||||
function o(a) {
|
||||
return unescape(encodeURIComponent(a));
|
||||
}
|
||||
function p(a) {
|
||||
return l(o(a));
|
||||
}
|
||||
function q(a) {
|
||||
return n(p(a));
|
||||
}
|
||||
function r(a, b) {
|
||||
return m(o(a), o(b));
|
||||
}
|
||||
function s(a, b) {
|
||||
return n(r(a, b));
|
||||
}
|
||||
function t(a, b, c) {
|
||||
return b ? (c ? r(b, a) : s(b, a)) : c ? p(a) : q(a);
|
||||
}
|
||||
"function" == typeof define && define.amd
|
||||
? define(function () {
|
||||
return t;
|
||||
})
|
||||
: (a.md5 = t);
|
||||
})(this);
|
||||
|
1
alist.go
1
alist.go
@ -25,6 +25,7 @@ func Init() bool {
|
||||
log.Infof("current password: %s", pass.Value)
|
||||
return false
|
||||
}
|
||||
server.InitIndex()
|
||||
bootstrap.InitSettings()
|
||||
bootstrap.InitAccounts()
|
||||
bootstrap.InitCache()
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"gorm.io/gorm"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func InitSettings() {
|
||||
@ -54,14 +55,14 @@ func InitSettings() {
|
||||
},
|
||||
{
|
||||
Key: "icon color",
|
||||
Value: "teal.300",
|
||||
Value: "#1890ff",
|
||||
Description: "icon's color",
|
||||
Type: "string",
|
||||
Group: model.PUBLIC,
|
||||
},
|
||||
{
|
||||
Key: "text types",
|
||||
Value: "txt,htm,html,xml,java,properties,sql,js,md,json,conf,ini,vue,php,py,bat,gitignore,yml,go,sh,c,cpp,h,hpp,tsx",
|
||||
Value: strings.Join(conf.TextTypes, ","),
|
||||
Type: "string",
|
||||
Description: "text type extensions",
|
||||
},
|
||||
@ -121,20 +122,21 @@ func InitSettings() {
|
||||
Key: "customize head",
|
||||
Value: `<style>
|
||||
.chakra-ui-light{
|
||||
background-image: linear-gradient(120deg,#e0c3fc 0%,#8ec5fc 100%) !important;
|
||||
background-attachment: fixed;
|
||||
background-color: #FAF5FF;
|
||||
}
|
||||
.main-box {
|
||||
border-radius: 15px !important;
|
||||
box-shadow: unset !important;
|
||||
}
|
||||
.chakra-ui-light .main-box {
|
||||
background-color: white !important;
|
||||
background-color: rgba(255,255,255,0.9) !important;
|
||||
}
|
||||
.chakra-ui-light .readme-box {
|
||||
background-color: white !important;
|
||||
background-color: rgba(255,255,255,0.9) !important;
|
||||
}
|
||||
.readme-box {
|
||||
border-radius: 15px !important;
|
||||
box-shadow: unset !important;
|
||||
}
|
||||
</style>`,
|
||||
Type: "text",
|
||||
|
8
build.sh
8
build.sh
@ -37,8 +37,14 @@ yarn
|
||||
if [ "$1" == "release" ]; then
|
||||
yarn build --base="https://cdn.jsdelivr.net/gh/Xhofe/alist-web@cdn/v2/$webCommit"
|
||||
mv dist/assets ..
|
||||
mv dist/index.html ../alist/public
|
||||
# 构建local
|
||||
yarn build
|
||||
mv dist/index.html dist/local.html
|
||||
mv dist/* ../alist/public
|
||||
else
|
||||
yarn build
|
||||
mv dist/* ../alist/public
|
||||
fi
|
||||
cd ..
|
||||
|
||||
@ -61,8 +67,6 @@ ldflags="\
|
||||
-X 'github.com/Xhofe/alist/conf.GitTag=$gitTag' \
|
||||
"
|
||||
|
||||
cp -R ../alist-web/dist/* public
|
||||
|
||||
if [ "$1" == "release" ]; then
|
||||
xgo -out alist -ldflags="$ldflags" .
|
||||
else
|
||||
|
@ -17,6 +17,7 @@ type Config struct {
|
||||
Https bool `json:"https"`
|
||||
CertFile string `json:"cert_file"`
|
||||
KeyFile string `json:"key_file"`
|
||||
Local bool `json:"local"`
|
||||
}
|
||||
|
||||
func DefaultConfig() *Config {
|
||||
|
@ -29,7 +29,9 @@ var (
|
||||
)
|
||||
|
||||
var (
|
||||
TextTypes = []string{"txt", "go", "md"}
|
||||
TextTypes = []string{"txt", "htm", "html", "xml", "java", "properties", "sql",
|
||||
"js", "md", "json", "conf", "ini", "vue", "php", "py", "bat", "gitignore", "yml",
|
||||
"go", "sh", "c", "cpp", "h", "hpp", "tsx", "vtt", "srt", "ass"}
|
||||
OfficeTypes = []string{"doc", "docx", "xls", "xlsx", "ppt", "pptx", "pdf"}
|
||||
VideoTypes = []string{"mp4", "mkv", "avi", "mov", "rmvb", "webm"}
|
||||
AudioTypes = []string{"mp3", "flac", "ogg", "m4a"}
|
||||
|
@ -11,20 +11,23 @@ import (
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"math/rand"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var pan123Client = resty.New()
|
||||
type BaseResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Pan123TokenResp struct {
|
||||
Code int `json:"code"`
|
||||
BaseResp
|
||||
Data struct {
|
||||
Token string `json:"token"`
|
||||
} `json:"data"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
type Pan123File struct {
|
||||
@ -38,30 +41,32 @@ type Pan123File struct {
|
||||
}
|
||||
|
||||
type Pan123Files struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
BaseResp
|
||||
Data struct {
|
||||
InfoList []Pan123File `json:"InfoList"`
|
||||
Next string `json:"Next"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
type Pan123DownResp struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data struct {
|
||||
BaseResp
|
||||
Data struct {
|
||||
DownloadUrl string `json:"DownloadUrl"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
func (driver Pan123) Login(account *model.Account) error {
|
||||
url := "https://www.123pan.com/api/user/sign_in"
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
var resp Pan123TokenResp
|
||||
_, err := pan123Client.R().
|
||||
_, err := base.RestyClient.R().
|
||||
SetResult(&resp).
|
||||
SetBody(base.Json{
|
||||
"passport": account.Username,
|
||||
"password": account.Password,
|
||||
}).Post("https://www.123pan.com/api/user/sign_in")
|
||||
}).Post(url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -97,50 +102,88 @@ func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123
|
||||
res := make([]Pan123File, 0)
|
||||
for next != "-1" {
|
||||
var resp Pan123Files
|
||||
_, err := pan123Client.R().SetResult(&resp).
|
||||
SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||
SetQueryParams(map[string]string{
|
||||
"driveId": "0",
|
||||
"limit": "100",
|
||||
"next": next,
|
||||
"orderBy": account.OrderBy,
|
||||
"orderDirection": account.OrderDirection,
|
||||
"parentFileId": parentId,
|
||||
"trashed": "false",
|
||||
}).Get("https://www.123pan.com/api/file/list")
|
||||
query := map[string]string{
|
||||
"driveId": "0",
|
||||
"limit": "100",
|
||||
"next": next,
|
||||
"orderBy": account.OrderBy,
|
||||
"orderDirection": account.OrderDirection,
|
||||
"parentFileId": parentId,
|
||||
"trashed": "false",
|
||||
}
|
||||
_, err := driver.Request("https://www.123pan.com/api/file/list",
|
||||
base.Get, nil, query, nil, &resp, false, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Code != 0 {
|
||||
if resp.Code == 401 {
|
||||
err := driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.GetFiles(parentId, account)
|
||||
}
|
||||
return nil, fmt.Errorf(resp.Message)
|
||||
}
|
||||
next = resp.Data.Next
|
||||
res = append(res, resp.Data.InfoList...)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) Post(url string, data base.Json, account *model.Account) ([]byte, error) {
|
||||
res, err := pan123Client.R().
|
||||
SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||
SetBody(data).Post(url)
|
||||
func (driver Pan123) Request(url string, method int, headers, query map[string]string, data *base.Json, resp interface{}, proxy bool, account *model.Account) ([]byte, error) {
|
||||
rawUrl := url
|
||||
if account.APIProxyUrl != "" && proxy {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Authorization", "Bearer "+account.AccessToken)
|
||||
if headers != nil {
|
||||
req.SetHeaders(headers)
|
||||
}
|
||||
if query != nil {
|
||||
req.SetQueryParams(query)
|
||||
}
|
||||
if data != nil {
|
||||
req.SetBody(data)
|
||||
}
|
||||
if resp != nil {
|
||||
req.SetResult(resp)
|
||||
}
|
||||
var res *resty.Response
|
||||
var err error
|
||||
switch method {
|
||||
case base.Get:
|
||||
res, err = req.Get(url)
|
||||
case base.Post:
|
||||
res, err = req.Post(url)
|
||||
default:
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
body := res.Body()
|
||||
if jsoniter.Get(body, "code").ToInt() != 0 {
|
||||
code := jsoniter.Get(body, "code").ToInt()
|
||||
if code != 0 {
|
||||
if code == 401 {
|
||||
err := driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.Request(rawUrl, method, headers, query, data, resp, proxy, account)
|
||||
}
|
||||
return nil, errors.New(jsoniter.Get(body, "message").ToString())
|
||||
}
|
||||
return body, nil
|
||||
}
|
||||
|
||||
//func (driver Pan123) Post(url string, data base.Json, account *model.Account) ([]byte, error) {
|
||||
// res, err := pan123Client.R().
|
||||
// SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||
// SetBody(data).Post(url)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// body := res.Body()
|
||||
// if jsoniter.Get(body, "code").ToInt() != 0 {
|
||||
// return nil, errors.New(jsoniter.Get(body, "message").ToString())
|
||||
// }
|
||||
// return body, nil
|
||||
//}
|
||||
|
||||
func (driver Pan123) GetFile(path string, account *model.Account) (*Pan123File, error) {
|
||||
dir, name := filepath.Split(path)
|
||||
dir = utils.ParsePath(dir)
|
||||
@ -166,7 +209,7 @@ func RandStr(length int) string {
|
||||
str := "123456789abcdefghijklmnopqrstuvwxyz"
|
||||
bytes := []byte(str)
|
||||
var result []byte
|
||||
rand.Seed(time.Now().UnixNano()+ int64(rand.Intn(100)))
|
||||
rand.Seed(time.Now().UnixNano() + int64(rand.Intn(100)))
|
||||
for i := 0; i < length; i++ {
|
||||
result = append(result, bytes[rand.Intn(len(bytes))])
|
||||
}
|
||||
@ -186,5 +229,4 @@ func HMAC(message string, secret string) string {
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&Pan123{})
|
||||
pan123Client.SetRetryCount(3)
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
log "github.com/sirupsen/logrus"
|
||||
url "net/url"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
@ -22,8 +22,8 @@ type Pan123 struct{}
|
||||
|
||||
func (driver Pan123) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "123Pan",
|
||||
OnlyProxy: false,
|
||||
Name: "123Pan",
|
||||
NeedSetLink: true,
|
||||
}
|
||||
}
|
||||
|
||||
@ -125,35 +125,36 @@ func (driver Pan123) Files(path string, account *model.Account) ([]model.File, e
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Pan123) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.GetFile(utils.ParsePath(path), account)
|
||||
func (driver Pan123) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
log.Debugf("%+v", args)
|
||||
file, err := driver.GetFile(utils.ParsePath(args.Path), account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var resp Pan123DownResp
|
||||
_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||
SetBody(base.Json{
|
||||
"driveId": 0,
|
||||
"etag": file.Etag,
|
||||
"fileId": file.FileId,
|
||||
"fileName": file.FileName,
|
||||
"s3keyFlag": file.S3KeyFlag,
|
||||
"size": file.Size,
|
||||
"type": file.Type,
|
||||
}).Post("https://www.123pan.com/api/file/download_info")
|
||||
var headers map[string]string
|
||||
if args.IP != "" && args.IP != "::1" {
|
||||
headers = map[string]string{
|
||||
//"X-Real-IP": "1.1.1.1",
|
||||
"X-Forwarded-For": args.IP,
|
||||
}
|
||||
}
|
||||
data := base.Json{
|
||||
"driveId": 0,
|
||||
"etag": file.Etag,
|
||||
"fileId": file.FileId,
|
||||
"fileName": file.FileName,
|
||||
"s3keyFlag": file.S3KeyFlag,
|
||||
"size": file.Size,
|
||||
"type": file.Type,
|
||||
}
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/download_info",
|
||||
base.Post, headers, nil, &data, &resp, false, account)
|
||||
//_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken).
|
||||
// SetBody().Post("https://www.123pan.com/api/file/download_info")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.Code != 0 {
|
||||
if resp.Code == 401 {
|
||||
err := driver.Login(account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.Link(path, account)
|
||||
}
|
||||
return nil, fmt.Errorf(resp.Message)
|
||||
}
|
||||
u, err := url.Parse(resp.Data.DownloadUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -164,11 +165,11 @@ func (driver Pan123) Link(path string, account *model.Account) (*base.Link, erro
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
link := base.Link{}
|
||||
link := base.Link{
|
||||
Url: resp.Data.DownloadUrl,
|
||||
}
|
||||
if res.StatusCode() == 302 {
|
||||
link.Url = res.Header().Get("location")
|
||||
} else {
|
||||
link.Url = resp.Data.DownloadUrl
|
||||
}
|
||||
return &link, nil
|
||||
}
|
||||
@ -181,11 +182,6 @@ func (driver Pan123) Path(path string, account *model.Account) (*model.File, []m
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
file.Url = link.Url
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
@ -221,7 +217,9 @@ func (driver Pan123) MakeDir(path string, account *model.Account) error {
|
||||
"size": 0,
|
||||
"type": 1,
|
||||
}
|
||||
_, err = driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/upload_request",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//_, err = driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(dir, account)
|
||||
}
|
||||
@ -243,7 +241,9 @@ func (driver Pan123) Move(src string, dst string, account *model.Account) error
|
||||
"fileId": fileId,
|
||||
"fileName": dstName,
|
||||
}
|
||||
_, err = driver.Post("https://www.123pan.com/api/file/rename", data, account)
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/rename",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//_, err = driver.Post("https://www.123pan.com/api/file/rename", data, account)
|
||||
} else {
|
||||
// move
|
||||
dstDirFile, err := driver.File(dstDir, account)
|
||||
@ -255,7 +255,9 @@ func (driver Pan123) Move(src string, dst string, account *model.Account) error
|
||||
"fileId": fileId,
|
||||
"parentFileId": parentFileId,
|
||||
}
|
||||
_, err = driver.Post("https://www.123pan.com/api/file/mod_pid", data, account)
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/mod_pid",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//_, err = driver.Post("https://www.123pan.com/api/file/mod_pid", data, account)
|
||||
}
|
||||
if err != nil {
|
||||
_ = base.DeleteCache(srcDir, account)
|
||||
@ -278,7 +280,9 @@ func (driver Pan123) Delete(path string, account *model.Account) error {
|
||||
"operation": true,
|
||||
"fileTrashInfoList": file,
|
||||
}
|
||||
_, err = driver.Post("https://www.123pan.com/api/file/trash", data, account)
|
||||
_, err = driver.Request("https://www.123pan.com/api/file/trash",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//_, err = driver.Post("https://www.123pan.com/api/file/trash", data, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
@ -311,7 +315,9 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
|
||||
"size": file.GetSize(),
|
||||
"type": 0,
|
||||
}
|
||||
res, err := driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
||||
res, err := driver.Request("https://www.123pan.com/api/file/upload_request",
|
||||
base.Post, nil, nil, &data, nil, false, account)
|
||||
//res, err := driver.Post("https://www.123pan.com/api/file/upload_request", data, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -319,19 +325,19 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro
|
||||
var resp UploadResp
|
||||
kSecret := jsoniter.Get(res, "data.SecretAccessKey").ToString()
|
||||
nowTimeStr := time.Now().String()
|
||||
Date := strings.ReplaceAll(strings.Split(nowTimeStr, "T")[0],"-","")
|
||||
Date := strings.ReplaceAll(strings.Split(nowTimeStr, "T")[0], "-", "")
|
||||
|
||||
StringToSign := fmt.Sprintf("%s\n%s\n%s\n%s",
|
||||
"AWS4-HMAC-SHA256",
|
||||
nowTimeStr,
|
||||
fmt.Sprintf("%s/us-east-1/s3/aws4_request", Date),
|
||||
)
|
||||
)
|
||||
|
||||
kDate := HMAC("AWS4"+kSecret, Date)
|
||||
kRegion := HMAC(kDate, "us-east-1")
|
||||
kService := HMAC(kRegion, "s3")
|
||||
kSigning := HMAC(kService, "aws4_request")
|
||||
_, err = pan123Client.R().SetResult(&resp).SetHeaders(map[string]string{
|
||||
_, err = base.RestyClient.R().SetResult(&resp).SetHeaders(map[string]string{
|
||||
"Authorization": fmt.Sprintf("AWS4-HMAC-SHA256 Credential=%s/%s/us-east-1/s3/aws4_request, SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-security-token;x-amz-user-agent, Signature=%s",
|
||||
jsoniter.Get(res, "data.AccessKeyId"),
|
||||
Date,
|
||||
|
@ -26,8 +26,8 @@ type Cloud189 struct{}
|
||||
|
||||
func (driver Cloud189) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "189Cloud",
|
||||
OnlyProxy: false,
|
||||
Name: "189Cloud",
|
||||
NeedSetLink: true,
|
||||
}
|
||||
}
|
||||
|
||||
@ -138,8 +138,8 @@ func (driver Cloud189) Files(path string, account *model.Account) ([]model.File,
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Cloud189) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(utils.ParsePath(path), account)
|
||||
func (driver Cloud189) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(utils.ParsePath(args.Path), account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -167,7 +167,7 @@ func (driver Cloud189) Link(path string, account *model.Account) (*base.Link, er
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return driver.Link(path, account)
|
||||
return driver.Link(args, account)
|
||||
}
|
||||
}
|
||||
if resp.ResCode != 0 {
|
||||
@ -194,11 +194,6 @@ func (driver Cloud189) Path(path string, account *model.Account) (*model.File, [
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
file.Url = link.Url
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
@ -229,7 +224,7 @@ func (driver Cloud189) MakeDir(path string, account *model.Account) error {
|
||||
"parentFolderId": parent.Id,
|
||||
"folderName": name,
|
||||
}
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/file/createFolder.action", "POST", form,nil, account)
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/file/createFolder.action", "POST", form, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(dir, account)
|
||||
}
|
||||
@ -257,7 +252,7 @@ func (driver Cloud189) Move(src string, dst string, account *model.Account) erro
|
||||
idKey: srcFile.Id,
|
||||
nameKey: dstName,
|
||||
}
|
||||
_, err = driver.Request(url, "POST", form,nil, account)
|
||||
_, err = driver.Request(url, "POST", form, nil, account)
|
||||
} else {
|
||||
// move
|
||||
dstDirFile, err := driver.File(dstDir, account)
|
||||
@ -284,7 +279,7 @@ func (driver Cloud189) Move(src string, dst string, account *model.Account) erro
|
||||
"targetFolderId": dstDirFile.Id,
|
||||
"taskInfos": string(taskInfosBytes),
|
||||
}
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form,nil, account)
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
|
||||
}
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(srcDir, account)
|
||||
@ -323,7 +318,7 @@ func (driver Cloud189) Copy(src string, dst string, account *model.Account) erro
|
||||
"targetFolderId": dstDirFile.Id,
|
||||
"taskInfos": string(taskInfosBytes),
|
||||
}
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form,nil, account)
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(dstDir, account)
|
||||
}
|
||||
@ -356,7 +351,7 @@ func (driver Cloud189) Delete(path string, account *model.Account) error {
|
||||
"targetFolderId": "",
|
||||
"taskInfos": string(taskInfosBytes),
|
||||
}
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form,nil, account)
|
||||
_, err = driver.Request("https://cloud.189.cn/api/open/batch/createBatchTask.action", "POST", form, nil, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(utils.Dir(path), account)
|
||||
}
|
||||
@ -377,11 +372,11 @@ func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) er
|
||||
}
|
||||
res, err := driver.UploadRequest("/person/initMultiUpload", map[string]string{
|
||||
"parentFolderId": parentFile.Id,
|
||||
"fileName": file.Name,
|
||||
"fileSize": strconv.FormatInt(int64(file.Size),10),
|
||||
"sliceSize": strconv.FormatInt(int64(DEFAULT),10),
|
||||
"lazyCheck": "1",
|
||||
},account)
|
||||
"fileName": file.Name,
|
||||
"fileSize": strconv.FormatInt(int64(file.Size), 10),
|
||||
"sliceSize": strconv.FormatInt(int64(DEFAULT), 10),
|
||||
"lazyCheck": "1",
|
||||
}, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -409,21 +404,21 @@ func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) er
|
||||
md5s = append(md5s, md5Str)
|
||||
md5Sum.Write(byteData)
|
||||
res, err = driver.UploadRequest("/person/getMultiUploadUrls", map[string]string{
|
||||
"partInfo": fmt.Sprintf("%s-%s",strconv.FormatInt(i,10),md5Base64),
|
||||
"partInfo": fmt.Sprintf("%s-%s", strconv.FormatInt(i, 10), md5Base64),
|
||||
"uploadFileId": uploadFileId,
|
||||
},account)
|
||||
}, account)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uploadData := jsoniter.Get(res,"uploadUrls.partNumber_"+strconv.FormatInt(i,10))
|
||||
headers := strings.Split(uploadData.Get("requestHeader").ToString(),"&")
|
||||
uploadData := jsoniter.Get(res, "uploadUrls.partNumber_"+strconv.FormatInt(i, 10))
|
||||
headers := strings.Split(uploadData.Get("requestHeader").ToString(), "&")
|
||||
req, err := http.NewRequest("PUT", uploadData.Get("requestURL").ToString(), bytes.NewBuffer(byteData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _,header := range headers{
|
||||
for _, header := range headers {
|
||||
kv := strings.Split(header, "=")
|
||||
req.Header.Set(kv[0],strings.Join(kv[1:],"="))
|
||||
req.Header.Set(kv[0], strings.Join(kv[1:], "="))
|
||||
}
|
||||
res, err := base.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
@ -432,12 +427,12 @@ func (driver Cloud189) Upload(file *model.FileStream, account *model.Account) er
|
||||
log.Debugf("%+v", res)
|
||||
}
|
||||
id := md5Sum.Sum(nil)
|
||||
res,err = driver.UploadRequest("/person/commitMultiUploadFile", map[string]string{
|
||||
res, err = driver.UploadRequest("/person/commitMultiUploadFile", map[string]string{
|
||||
"uploadFileId": uploadFileId,
|
||||
"fileMd5": hex.EncodeToString(id),
|
||||
"sliceMd5": utils.GetMD5Encode(strings.Join(md5s,"\n")),
|
||||
"lazyCheck":"1",
|
||||
},account)
|
||||
"fileMd5": hex.EncodeToString(id),
|
||||
"sliceMd5": utils.GetMD5Encode(strings.Join(md5s, "\n")),
|
||||
"lazyCheck": "1",
|
||||
}, account)
|
||||
if err == nil {
|
||||
_ = base.DeleteCache(file.ParentPath, account)
|
||||
}
|
||||
|
@ -21,8 +21,8 @@ type AliDrive struct{}
|
||||
|
||||
func (driver AliDrive) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "AliDrive",
|
||||
OnlyProxy: false,
|
||||
Name: "AliDrive",
|
||||
NeedSetLink: true,
|
||||
}
|
||||
}
|
||||
|
||||
@ -158,8 +158,8 @@ func (driver AliDrive) Files(path string, account *model.Account) ([]model.File,
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver AliDrive) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(path, account)
|
||||
func (driver AliDrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(args.Path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -183,7 +183,7 @@ func (driver AliDrive) Link(path string, account *model.Account) (*base.Link, er
|
||||
return nil, err
|
||||
} else {
|
||||
_ = model.SaveAccount(account)
|
||||
return driver.Link(path, account)
|
||||
return driver.Link(args, account)
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("%s", e.Message)
|
||||
@ -201,11 +201,6 @@ func (driver AliDrive) Path(path string, account *model.Account) (*model.File, [
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
file.Url = link.Url
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
|
@ -101,7 +101,8 @@ func (driver Alist) Files(path string, account *model.Account) ([]model.File, er
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Alist) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
func (driver Alist) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
path := args.Path
|
||||
path = utils.ParsePath(path)
|
||||
name := utils.Base(path)
|
||||
flag := "d"
|
||||
|
@ -9,28 +9,49 @@ import (
|
||||
)
|
||||
|
||||
type DriverConfig struct {
|
||||
Name string
|
||||
OnlyProxy bool
|
||||
NoLink bool // 必须本机返回的
|
||||
Name string
|
||||
OnlyProxy bool
|
||||
NoLink bool // 必须本机返回的
|
||||
ApiProxy bool // 使用API中转的
|
||||
NeedSetLink bool // 需要设置链接的
|
||||
}
|
||||
|
||||
type Args struct {
|
||||
Path string
|
||||
IP string
|
||||
}
|
||||
|
||||
type Driver interface {
|
||||
// Config 配置
|
||||
Config() DriverConfig
|
||||
// Items 账号所需参数
|
||||
Items() []Item
|
||||
// Save 保存时处理
|
||||
Save(account *model.Account, old *model.Account) error
|
||||
// File 取文件
|
||||
File(path string, account *model.Account) (*model.File, error)
|
||||
// Files 取文件夹
|
||||
Files(path string, account *model.Account) ([]model.File, error)
|
||||
Link(path string, account *model.Account) (*Link, error)
|
||||
// Link 取链接
|
||||
Link(args Args, account *model.Account) (*Link, error)
|
||||
// Path 取路径(文件或文件夹)
|
||||
Path(path string, account *model.Account) (*model.File, []model.File, error)
|
||||
// Proxy 代理处理
|
||||
Proxy(c *gin.Context, account *model.Account)
|
||||
// Preview 预览
|
||||
Preview(path string, account *model.Account) (interface{}, error)
|
||||
// MakeDir 创建文件夹
|
||||
MakeDir(path string, account *model.Account) error
|
||||
// Move 移动/改名
|
||||
Move(src string, dst string, account *model.Account) error
|
||||
// Copy 拷贝
|
||||
Copy(src string, dst string, account *model.Account) error
|
||||
// Delete 删除
|
||||
Delete(path string, account *model.Account) error
|
||||
// Upload 上传
|
||||
Upload(file *model.FileStream, account *model.Account) error
|
||||
// TODO
|
||||
//Search(path string, keyword string, account *model.Account) ([]*model.File, error)
|
||||
MakeDir(path string, account *model.Account) error
|
||||
Move(src string, dst string, account *model.Account) error
|
||||
Copy(src string, dst string, account *model.Account) error
|
||||
Delete(path string, account *model.Account) error
|
||||
Upload(file *model.FileStream, account *model.Account) error
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
@ -84,13 +105,22 @@ func GetDrivers() map[string][]Item {
|
||||
},
|
||||
}, v.Items()...)
|
||||
}
|
||||
res[k] = append(res[k], Item{
|
||||
Name: "proxy_url",
|
||||
Label: "proxy_url",
|
||||
Type: TypeString,
|
||||
Required: false,
|
||||
Description: "proxy url",
|
||||
})
|
||||
res[k] = append([]Item{
|
||||
{
|
||||
Name: "down_proxy_url",
|
||||
Label: "down_proxy_url",
|
||||
Type: TypeString,
|
||||
},
|
||||
}, res[k]...)
|
||||
if v.Config().ApiProxy {
|
||||
res[k] = append([]Item{
|
||||
{
|
||||
Name: "api_proxy_url",
|
||||
Label: "api_proxy_url",
|
||||
Type: TypeString,
|
||||
},
|
||||
}, res[k]...)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
@ -105,6 +135,8 @@ func init() {
|
||||
return http.ErrUseLastResponse
|
||||
}),
|
||||
)
|
||||
NoRedirectClient.SetHeader("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")
|
||||
RestyClient.SetHeader("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")
|
||||
userAgent := "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
|
||||
NoRedirectClient.SetHeader("user-agent", userAgent)
|
||||
RestyClient.SetHeader("user-agent", userAgent)
|
||||
RestyClient.SetRetryCount(3)
|
||||
}
|
||||
|
@ -146,7 +146,8 @@ func (driver FTP) Files(path string, account *model.Account) ([]model.File, erro
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (driver FTP) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
func (driver FTP) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
path := args.Path
|
||||
path = utils.ParsePath(path)
|
||||
realPath := utils.Join(account.RootFolder, path)
|
||||
conn, err := driver.Login(account)
|
||||
|
@ -15,8 +15,9 @@ type GoogleDrive struct{}
|
||||
|
||||
func (driver GoogleDrive) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "GoogleDrive",
|
||||
OnlyProxy: true,
|
||||
Name: "GoogleDrive",
|
||||
OnlyProxy: true,
|
||||
ApiProxy: true,
|
||||
}
|
||||
}
|
||||
|
||||
@ -92,10 +93,10 @@ func (driver GoogleDrive) File(path string, account *model.Account) (*model.File
|
||||
|
||||
func (driver GoogleDrive) Files(path string, account *model.Account) ([]model.File, error) {
|
||||
path = utils.ParsePath(path)
|
||||
var rawFiles []GoogleFile
|
||||
var rawFiles []File
|
||||
cache, err := base.GetCache(path, account)
|
||||
if err == nil {
|
||||
rawFiles, _ = cache.([]GoogleFile)
|
||||
rawFiles, _ = cache.([]File)
|
||||
} else {
|
||||
file, err := driver.File(path, account)
|
||||
if err != nil {
|
||||
@ -116,8 +117,8 @@ func (driver GoogleDrive) Files(path string, account *model.Account) ([]model.Fi
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(path, account)
|
||||
func (driver GoogleDrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(args.Path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -125,26 +126,15 @@ func (driver GoogleDrive) Link(path string, account *model.Account) (*base.Link,
|
||||
return nil, base.ErrNotFile
|
||||
}
|
||||
url := fmt.Sprintf("https://www.googleapis.com/drive/v3/files/%s?includeItemsFromAllDrives=true&supportsAllDrives=true", file.Id)
|
||||
var e GoogleError
|
||||
_, _ = googleClient.R().SetError(&e).
|
||||
SetHeader("Authorization", "Bearer "+account.AccessToken).
|
||||
Get(url)
|
||||
if e.Error.Code != 0 {
|
||||
if e.Error.Code == 401 {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
_ = model.SaveAccount(account)
|
||||
return nil, err
|
||||
}
|
||||
return driver.Link(path, account)
|
||||
}
|
||||
return nil, fmt.Errorf("%s: %v", e.Error.Message, e.Error.Errors)
|
||||
_, err = driver.Request(url, base.Get, nil, nil, nil, nil, nil, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
link := base.Link{
|
||||
Url: url + "&alt=media",
|
||||
Headers: []base.Header{
|
||||
{
|
||||
Name: "Authorization",
|
||||
Name: "Authorization",
|
||||
Value: "Bearer " + account.AccessToken,
|
||||
},
|
||||
},
|
||||
@ -159,8 +149,7 @@ func (driver GoogleDrive) Path(path string, account *model.Account) (*model.File
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if file.Type != conf.FOLDER {
|
||||
//file.Url, _ = driver.Link(path, account)
|
||||
if !file.IsDir() {
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
@ -198,4 +187,4 @@ func (driver GoogleDrive) Upload(file *model.FileStream, account *model.Account)
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
var _ base.Driver = (*GoogleDrive)(nil)
|
||||
var _ base.Driver = (*GoogleDrive)(nil)
|
||||
|
@ -7,23 +7,25 @@ import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/go-resty/resty/v2"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var googleClient = resty.New()
|
||||
|
||||
type GoogleTokenError struct {
|
||||
type TokenError struct {
|
||||
Error string `json:"error"`
|
||||
ErrorDescription string `json:"error_description"`
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) RefreshToken(account *model.Account) error {
|
||||
url := "https://www.googleapis.com/oauth2/v4/token"
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
var resp base.TokenResp
|
||||
var e GoogleTokenError
|
||||
_, err := googleClient.R().SetResult(&resp).SetError(&e).
|
||||
var e TokenError
|
||||
res, err := base.RestyClient.R().SetResult(&resp).SetError(&e).
|
||||
SetFormData(map[string]string{
|
||||
"client_id": account.ClientId,
|
||||
"client_secret": account.ClientSecret,
|
||||
@ -33,6 +35,7 @@ func (driver GoogleDrive) RefreshToken(account *model.Account) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.Error != "" {
|
||||
return fmt.Errorf(e.Error)
|
||||
}
|
||||
@ -41,7 +44,7 @@ func (driver GoogleDrive) RefreshToken(account *model.Account) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type GoogleFile struct {
|
||||
type File struct {
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
MimeType string `json:"mimeType"`
|
||||
@ -53,7 +56,7 @@ func (driver GoogleDrive) IsDir(mimeType string) bool {
|
||||
return mimeType == "application/vnd.google-apps.folder" || mimeType == "application/vnd.google-apps.shortcut"
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) FormatFile(file *GoogleFile) *model.File {
|
||||
func (driver GoogleDrive) FormatFile(file *File) *model.File {
|
||||
f := &model.File{
|
||||
Id: file.Id,
|
||||
Name: file.Name,
|
||||
@ -72,12 +75,12 @@ func (driver GoogleDrive) FormatFile(file *GoogleFile) *model.File {
|
||||
return f
|
||||
}
|
||||
|
||||
type GoogleFiles struct {
|
||||
NextPageToken string `json:"nextPageToken"`
|
||||
Files []GoogleFile `json:"files"`
|
||||
type Files struct {
|
||||
NextPageToken string `json:"nextPageToken"`
|
||||
Files []File `json:"files"`
|
||||
}
|
||||
|
||||
type GoogleError struct {
|
||||
type Error struct {
|
||||
Error struct {
|
||||
Errors []struct {
|
||||
Domain string `json:"domain"`
|
||||
@ -91,68 +94,86 @@ type GoogleError struct {
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
func (driver GoogleDrive) GetFiles(id string, account *model.Account) ([]GoogleFile, error) {
|
||||
func (driver GoogleDrive) GetFiles(id string, account *model.Account) ([]File, error) {
|
||||
pageToken := "first"
|
||||
res := make([]GoogleFile, 0)
|
||||
res := make([]File, 0)
|
||||
for pageToken != "" {
|
||||
if pageToken == "first" {
|
||||
pageToken = ""
|
||||
}
|
||||
var resp GoogleFiles
|
||||
var e GoogleError
|
||||
_, err := googleClient.R().SetResult(&resp).SetError(&e).
|
||||
SetHeader("Authorization", "Bearer "+account.AccessToken).
|
||||
SetQueryParams(map[string]string{
|
||||
"orderBy": "folder,name,modifiedTime desc",
|
||||
"fields": "files(id,name,mimeType,size,modifiedTime),nextPageToken",
|
||||
"pageSize": "1000",
|
||||
"q": fmt.Sprintf("'%s' in parents and trashed = false", id),
|
||||
"includeItemsFromAllDrives": "true",
|
||||
"supportsAllDrives": "true",
|
||||
"pageToken": pageToken,
|
||||
}).Get("https://www.googleapis.com/drive/v3/files")
|
||||
var resp Files
|
||||
query := map[string]string{
|
||||
"orderBy": "folder,name,modifiedTime desc",
|
||||
"fields": "files(id,name,mimeType,size,modifiedTime),nextPageToken",
|
||||
"pageSize": "1000",
|
||||
"q": fmt.Sprintf("'%s' in parents and trashed = false", id),
|
||||
"includeItemsFromAllDrives": "true",
|
||||
"supportsAllDrives": "true",
|
||||
"pageToken": pageToken,
|
||||
}
|
||||
_, err := driver.Request("https://www.googleapis.com/drive/v3/files",
|
||||
base.Get, nil, query, nil, nil, &resp, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if e.Error.Code != 0 {
|
||||
if e.Error.Code == 401 {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
_ = model.SaveAccount(account)
|
||||
return nil, err
|
||||
}
|
||||
return driver.GetFiles(id, account)
|
||||
}
|
||||
return nil, fmt.Errorf("%s: %v", e.Error.Message, e.Error.Errors)
|
||||
}
|
||||
pageToken = resp.NextPageToken
|
||||
res = append(res, resp.Files...)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
//func (driver GoogleDrive) GetFile(path string, account *model.Account) (*GoogleFile, error) {
|
||||
// dir, name := filepath.Split(path)
|
||||
// dir = utils.ParsePath(dir)
|
||||
// _, _, err := driver.ParentPath(dir, account)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// parentFiles_, _ := conf.Cache.Get(conf.Ctx, fmt.Sprintf("%s%s", account.Name, dir))
|
||||
// parentFiles, _ := parentFiles_.([]GoogleFile)
|
||||
// for _, file := range parentFiles {
|
||||
// if file.Name == name {
|
||||
// if !driver.IsDir(file.MimeType) {
|
||||
// return &file, err
|
||||
// } else {
|
||||
// return nil, drivers.ErrNotFile
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return nil, drivers.ErrPathNotFound
|
||||
//}
|
||||
func (driver GoogleDrive) Request(url string, method int, headers, query, form map[string]string, data *base.Json, resp interface{}, account *model.Account) ([]byte, error) {
|
||||
rawUrl := url
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Authorization", "Bearer "+account.AccessToken)
|
||||
if headers != nil {
|
||||
req.SetHeaders(headers)
|
||||
}
|
||||
if query != nil {
|
||||
req.SetQueryParams(query)
|
||||
}
|
||||
if form != nil {
|
||||
req.SetFormData(form)
|
||||
}
|
||||
if data != nil {
|
||||
req.SetBody(data)
|
||||
}
|
||||
if resp != nil {
|
||||
req.SetResult(resp)
|
||||
}
|
||||
var res *resty.Response
|
||||
var err error
|
||||
var e Error
|
||||
req.SetError(&e)
|
||||
switch method {
|
||||
case base.Get:
|
||||
res, err = req.Get(url)
|
||||
case base.Post:
|
||||
res, err = req.Post(url)
|
||||
default:
|
||||
return nil, base.ErrNotSupport
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.Error.Code != 0 {
|
||||
if e.Error.Code == 401 {
|
||||
err = driver.RefreshToken(account)
|
||||
if err != nil {
|
||||
_ = model.SaveAccount(account)
|
||||
return nil, err
|
||||
}
|
||||
return driver.Request(rawUrl, method, headers, query, form, data, resp, account)
|
||||
}
|
||||
return nil, fmt.Errorf("%s: %v", e.Error.Message, e.Error.Errors)
|
||||
}
|
||||
return res.Body(), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
base.RegisterDriver(&GoogleDrive{})
|
||||
googleClient.SetRetryCount(3)
|
||||
}
|
||||
|
@ -14,8 +14,8 @@ type Lanzou struct{}
|
||||
|
||||
func (driver Lanzou) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "Lanzou",
|
||||
OnlyProxy: false,
|
||||
Name: "Lanzou",
|
||||
NeedSetLink: true,
|
||||
}
|
||||
}
|
||||
|
||||
@ -114,8 +114,8 @@ func (driver Lanzou) Files(path string, account *model.Account) ([]model.File, e
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Lanzou) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(path, account)
|
||||
func (driver Lanzou) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(args.Path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -145,11 +145,6 @@ func (driver Lanzou) Path(path string, account *model.Account) (*model.File, []m
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
file.Url = link.Url
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
|
@ -123,8 +123,8 @@ func (driver Native) Files(path string, account *model.Account) ([]model.File, e
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Native) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
fullPath := filepath.Join(account.RootFolder, path)
|
||||
func (driver Native) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
fullPath := filepath.Join(account.RootFolder, args.Path)
|
||||
s, err := os.Stat(fullPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
@ -14,11 +14,9 @@ import (
|
||||
|
||||
type Onedrive struct{}
|
||||
|
||||
|
||||
func (driver Onedrive) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "Onedrive",
|
||||
OnlyProxy: false,
|
||||
}
|
||||
}
|
||||
|
||||
@ -173,8 +171,8 @@ func (driver Onedrive) Files(path string, account *model.Account) ([]model.File,
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver Onedrive) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.GetFile(account, path)
|
||||
func (driver Onedrive) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.GetFile(account, args.Path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -194,7 +192,6 @@ func (driver Onedrive) Path(path string, account *model.Account) (*model.File, [
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
//file.Url, _ = driver.Link(path, account)
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
@ -232,4 +229,4 @@ func (driver Onedrive) Upload(file *model.FileStream, account *model.Account) er
|
||||
return base.ErrNotImplement
|
||||
}
|
||||
|
||||
var _ base.Driver = (*Onedrive)(nil)
|
||||
var _ base.Driver = (*Onedrive)(nil)
|
||||
|
@ -15,7 +15,9 @@ type PikPak struct{}
|
||||
|
||||
func (driver PikPak) Config() base.DriverConfig {
|
||||
return base.DriverConfig{
|
||||
Name: "PikPak",
|
||||
Name: "PikPak",
|
||||
NeedSetLink: true,
|
||||
ApiProxy: true,
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,7 +46,6 @@ func (driver PikPak) Items() []base.Item {
|
||||
|
||||
func (driver PikPak) Save(account *model.Account, old *model.Account) error {
|
||||
err := driver.Login(account)
|
||||
_ = model.SaveAccount(account)
|
||||
return err
|
||||
}
|
||||
|
||||
@ -99,8 +100,8 @@ func (driver PikPak) Files(path string, account *model.Account) ([]model.File, e
|
||||
return files, nil
|
||||
}
|
||||
|
||||
func (driver PikPak) Link(path string, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(path, account)
|
||||
func (driver PikPak) Link(args base.Args, account *model.Account) (*base.Link, error) {
|
||||
file, err := driver.File(args.Path, account)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -123,11 +124,6 @@ func (driver PikPak) Path(path string, account *model.Account) (*model.File, []m
|
||||
return nil, nil, err
|
||||
}
|
||||
if !file.IsDir() {
|
||||
link, err := driver.Link(path, account)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
file.Url = link.Url
|
||||
return file, nil, nil
|
||||
}
|
||||
files, err := driver.Files(path, account)
|
||||
|
@ -2,6 +2,7 @@ package pikpak
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/drivers/base"
|
||||
"github.com/Xhofe/alist/model"
|
||||
@ -20,6 +21,10 @@ type RespErr struct {
|
||||
}
|
||||
|
||||
func (driver PikPak) Login(account *model.Account) error {
|
||||
url := "https://user.mypikpak.com/v1/auth/signin"
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
var e RespErr
|
||||
res, err := base.RestyClient.R().SetError(&e).SetBody(base.Json{
|
||||
"captcha_token": "",
|
||||
@ -27,11 +32,12 @@ func (driver PikPak) Login(account *model.Account) error {
|
||||
"client_secret": "dbw2OtmVEeuUvIptb1Coyg",
|
||||
"username": account.Username,
|
||||
"password": account.Password,
|
||||
}).Post("https://user.mypikpak.com/v1/auth/signin")
|
||||
}).Post(url)
|
||||
if err != nil {
|
||||
account.Status = err.Error()
|
||||
return err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.ErrorCode != 0 {
|
||||
account.Status = e.Error
|
||||
return errors.New(e.Error)
|
||||
@ -40,17 +46,23 @@ func (driver PikPak) Login(account *model.Account) error {
|
||||
account.Status = "work"
|
||||
account.RefreshToken = jsoniter.Get(data, "refresh_token").ToString()
|
||||
account.AccessToken = jsoniter.Get(data, "access_token").ToString()
|
||||
_ = model.SaveAccount(account)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver PikPak) RefreshToken(account *model.Account) error {
|
||||
url := "https://user.mypikpak.com/v1/auth/token"
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
var e RespErr
|
||||
res, err := base.RestyClient.R().SetError(&e).SetBody(base.Json{
|
||||
res, err := base.RestyClient.R().SetError(&e).
|
||||
SetHeader("user-agent", "").SetBody(base.Json{
|
||||
"client_id": "YNxT9w7GMdWvEOKa",
|
||||
"client_secret": "dbw2OtmVEeuUvIptb1Coyg",
|
||||
"grant_type": "refresh_token",
|
||||
"refresh_token": account.RefreshToken,
|
||||
}).Post("https://user.mypikpak.com/v1/auth/token")
|
||||
}).Post(url)
|
||||
if err != nil {
|
||||
account.Status = err.Error()
|
||||
return err
|
||||
@ -60,15 +72,22 @@ func (driver PikPak) RefreshToken(account *model.Account) error {
|
||||
// refresh_token 失效,重新登陆
|
||||
return driver.Login(account)
|
||||
}
|
||||
return errors.New(e.Error)
|
||||
}
|
||||
data := res.Body()
|
||||
account.Status = "work"
|
||||
account.RefreshToken = jsoniter.Get(data, "refresh_token").ToString()
|
||||
account.AccessToken = jsoniter.Get(data, "access_token").ToString()
|
||||
log.Debugf("%s\n %+v", res.String(), account)
|
||||
_ = model.SaveAccount(account)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (driver PikPak) Request(url string, method int, query map[string]string, data *base.Json, resp interface{}, account *model.Account) ([]byte, error) {
|
||||
rawUrl := url
|
||||
if account.APIProxyUrl != "" {
|
||||
url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url)
|
||||
}
|
||||
req := base.RestyClient.R()
|
||||
req.SetHeader("Authorization", "Bearer "+account.AccessToken)
|
||||
if query != nil {
|
||||
@ -97,6 +116,7 @@ func (driver PikPak) Request(url string, method int, query map[string]string, da
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.Debug(res.String())
|
||||
if e.ErrorCode != 0 {
|
||||
if e.ErrorCode == 16 {
|
||||
// login / refresh token
|
||||
@ -104,8 +124,7 @@ func (driver PikPak) Request(url string, method int, query map[string]string, da
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_ = model.SaveAccount(account)
|
||||
return driver.Request(url, method, query, data, resp, account)
|
||||
return driver.Request(rawUrl, method, query, data, resp, account)
|
||||
} else {
|
||||
return nil, errors.New(e.Error)
|
||||
}
|
||||
|
@ -31,10 +31,11 @@ type Account struct {
|
||||
SiteUrl string `json:"site_url"`
|
||||
SiteId string `json:"site_id"`
|
||||
InternalType string `json:"internal_type"`
|
||||
WebdavProxy bool `json:"webdav_proxy"`
|
||||
Proxy bool `json:"proxy"` // 是否中转
|
||||
WebdavProxy bool `json:"webdav_proxy"` // 开启之后只会webdav走中转
|
||||
Proxy bool `json:"proxy"` // 是否中转,开启之后web和webdav都会走中转
|
||||
//AllowProxy bool `json:"allow_proxy"` // 是否允许中转下载
|
||||
ProxyUrl string `json:"proxy_url"` // 用于中转下载服务的URL
|
||||
DownProxyUrl string `json:"down_proxy_url"` // 用于中转下载服务的URL 两处 1. path请求中返回的链接 2. down下载时进行302
|
||||
APIProxyUrl string `json:"api_proxy_url"` // 用于中转api的地址
|
||||
}
|
||||
|
||||
var accountsMap = map[string]Account{}
|
||||
|
@ -30,7 +30,7 @@ func Down(c *gin.Context) {
|
||||
Proxy(c)
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(path, account)
|
||||
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
@ -48,22 +48,31 @@ func Proxy(c *gin.Context) {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
// 只有三种情况允许中转:
|
||||
// 只有以下几种情况允许中转:
|
||||
// 1. 账号开启中转
|
||||
// 2. driver只能中转
|
||||
// 3. 是文本类型文件
|
||||
// 4. 开启webdav中转(需要验证sign)
|
||||
if !account.Proxy && !driver.Config().OnlyProxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
|
||||
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
|
||||
return
|
||||
// 只开启了webdav中转,验证sign
|
||||
ok := false
|
||||
if account.WebdavProxy {
|
||||
_, ok = c.Get("sign")
|
||||
}
|
||||
if !ok {
|
||||
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
|
||||
return
|
||||
}
|
||||
}
|
||||
// 中转时有中转机器使用中转机器,若携带标志位则表明不能再走中转机器了
|
||||
if account.ProxyUrl != "" && c.Param("d") != "1" {
|
||||
if account.DownProxyUrl != "" && c.Param("d") != "1" {
|
||||
name := utils.Base(rawPath)
|
||||
link := fmt.Sprintf("%s%s?sign=%s", account.ProxyUrl, rawPath, utils.SignWithToken(name, conf.Token))
|
||||
link := fmt.Sprintf("%s%s?sign=%s", account.DownProxyUrl, rawPath, utils.SignWithToken(name, conf.Token))
|
||||
c.Redirect(302, link)
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(path, account)
|
||||
// 对于中转,不需要重设IP
|
||||
link, err := driver.Link(base.Args{Path: path}, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
@ -71,6 +80,7 @@ func Proxy(c *gin.Context) {
|
||||
// 本机读取数据
|
||||
if account.Type == "FTP" {
|
||||
c.Data(http.StatusOK, "application/octet-stream", link.Data)
|
||||
return
|
||||
}
|
||||
// 本机文件直接返回文件
|
||||
if account.Type == "Native" {
|
||||
|
@ -41,11 +41,18 @@ func Path(c *gin.Context) {
|
||||
if file != nil {
|
||||
// 对于中转文件或只能中转,将链接修改为中转链接
|
||||
if driver.Config().OnlyProxy || account.Proxy {
|
||||
if account.ProxyUrl != "" {
|
||||
file.Url = fmt.Sprintf("%s%s?sign=%s", account.ProxyUrl, req.Path, utils.SignWithToken(file.Name, conf.Token))
|
||||
if account.DownProxyUrl != "" {
|
||||
file.Url = fmt.Sprintf("%s%s?sign=%s", account.DownProxyUrl, req.Path, utils.SignWithToken(file.Name, conf.Token))
|
||||
} else {
|
||||
file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path)
|
||||
}
|
||||
} else if driver.Config().NeedSetLink {
|
||||
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
file.Url = link.Url
|
||||
}
|
||||
c.JSON(200, common.Resp{
|
||||
Code: 200,
|
||||
@ -72,7 +79,7 @@ func Path(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 返回真实的链接,且携带头,只提供给中转程序使用
|
||||
// Link 返回真实的链接,且携带头,只提供给中转程序使用
|
||||
func Link(c *gin.Context) {
|
||||
var req common.PathReq
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
@ -94,14 +101,13 @@ func Link(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
link, err := driver.Link(path, account)
|
||||
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
|
||||
if err != nil {
|
||||
common.ErrorResp(c, err, 500)
|
||||
return
|
||||
}
|
||||
common.SuccessResp(c, link)
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func Preview(c *gin.Context) {
|
||||
|
@ -14,6 +14,7 @@ func DownCheck(c *gin.Context) {
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
name := utils.Base(rawPath)
|
||||
if sign == utils.SignWithToken(name, conf.Token) {
|
||||
c.Set("sign", true)
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
@ -24,4 +25,4 @@ func DownCheck(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
@ -49,8 +49,8 @@ func InitApiRouter(r *gin.Engine) {
|
||||
|
||||
admin.POST("/link", controllers.Link)
|
||||
}
|
||||
Static(r)
|
||||
WebDav(r)
|
||||
Static(r)
|
||||
}
|
||||
|
||||
func Cors(r *gin.Engine) {
|
||||
|
@ -10,11 +10,16 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
||||
func init() {
|
||||
index, err := public.Public.Open("index.html")
|
||||
func InitIndex() {
|
||||
var index fs.File
|
||||
var err error
|
||||
if conf.Conf.Local {
|
||||
index, err = public.Public.Open("local.html")
|
||||
} else {
|
||||
index, err = public.Public.Open("index.html")
|
||||
}
|
||||
if err != nil {
|
||||
log.Errorf(err.Error())
|
||||
log.Fatalf(err.Error())
|
||||
return
|
||||
}
|
||||
data, _ := ioutil.ReadAll(index)
|
||||
@ -22,6 +27,7 @@ func init() {
|
||||
}
|
||||
|
||||
func Static(r *gin.Engine) {
|
||||
//InitIndex()
|
||||
assets, err := fs.Sub(public.Public, "assets")
|
||||
if err != nil {
|
||||
log.Fatalf("can't find assets folder")
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
"github.com/Xhofe/alist/model"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net"
|
||||
"net/http"
|
||||
"path"
|
||||
"path/filepath"
|
||||
@ -101,6 +102,25 @@ func (fs *FileSystem) Files(rawPath string) ([]model.File, error) {
|
||||
// }
|
||||
//}
|
||||
|
||||
func ClientIP(r *http.Request) string {
|
||||
xForwardedFor := r.Header.Get("X-Forwarded-For")
|
||||
ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
|
||||
if ip != "" {
|
||||
return ip
|
||||
}
|
||||
|
||||
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
|
||||
if ip != "" {
|
||||
return ip
|
||||
}
|
||||
|
||||
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
|
||||
return ip
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
|
||||
rawPath = utils.ParsePath(rawPath)
|
||||
log.Debugf("get link path: %s", rawPath)
|
||||
@ -123,7 +143,7 @@ func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
|
||||
link += "?sign" + sign
|
||||
}
|
||||
} else {
|
||||
link_, err := driver.Link(path_, account)
|
||||
link_, err := driver.Link(base.Args{Path: path_, IP: ClientIP(r)}, account)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -145,7 +165,7 @@ func (fs *FileSystem) CreateDirectory(ctx context.Context, rawPath string) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return driver.MakeDir(path_,account)
|
||||
return driver.MakeDir(path_, account)
|
||||
}
|
||||
|
||||
func (fs *FileSystem) Upload(ctx context.Context, r *http.Request, rawPath string) error {
|
||||
@ -218,7 +238,7 @@ func moveFiles(ctx context.Context, fs *FileSystem, src string, dst string, over
|
||||
if srcAccount.Name != dstAccount.Name {
|
||||
return http.StatusMethodNotAllowed, errInvalidDestination
|
||||
}
|
||||
err = driver.Move(srcPath,dstPath,srcAccount)
|
||||
err = driver.Move(srcPath, dstPath, srcAccount)
|
||||
if err != nil {
|
||||
log.Debug(err)
|
||||
return http.StatusInternalServerError, err
|
||||
@ -248,7 +268,7 @@ func copyFiles(ctx context.Context, fs *FileSystem, src string, dst string, over
|
||||
// TODO 跨账号复制
|
||||
return http.StatusMethodNotAllowed, errInvalidDestination
|
||||
}
|
||||
err = driver.Copy(srcPath,dstPath,srcAccount)
|
||||
err = driver.Copy(srcPath, dstPath, srcAccount)
|
||||
if err != nil {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request, fs *FileSyst
|
||||
if status != 0 {
|
||||
w.WriteHeader(status)
|
||||
if status != http.StatusNoContent {
|
||||
w.Write([]byte(StatusText(status)))
|
||||
_, _ = w.Write([]byte(StatusText(status)))
|
||||
}
|
||||
}
|
||||
if h.Logger != nil {
|
||||
@ -222,7 +222,10 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request, fs *
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
if reqPath == "/" {
|
||||
_, err = w.Write([]byte("Please connect using software that supports WebDAV instead of a browser.\n"))
|
||||
return http.StatusMethodNotAllowed, err
|
||||
}
|
||||
exist, file := isPathExist(ctx, fs, reqPath)
|
||||
if !exist {
|
||||
return http.StatusNotFound, nil
|
||||
|
Reference in New Issue
Block a user