alist icon indicating copy to clipboard operation
alist copied to clipboard

百度网盘动态上传域名

Open 21paradox opened this issue 9 months ago • 2 comments

Please make sure of the following things

  • [X] I have read the documentation.
  • [X] I'm sure there are no duplicate issues or discussions.
  • [X] I'm sure this feature is not implemented.
  • [X] I'm sure it's a reasonable and popular requirement.

Description of the feature / 需求描述

百度网盘分片上传前增加了一个动态获取域名的步骤,文档: https://pan.baidu.com/union/doc/nksg0s9vi

Suggested solution / 实现思路

我遇到了大文件上传80%多报错的情况,使用的10mbps云主机,百度云svip + 16 * 1024 * 1024 分块上传到80%会出现 error=31355 复现步骤是复制一个本地存储文件到百度云. 目前我是使用一个代理脚本解决

希望alist 能集成上传前动态获取域名步骤

代理脚本(上传api改成http://localhost:3100)

const httpProxy = require('http-proxy');
const http = require('http');
const https = require('https');
const url = require('url');
const fetch = require('node-fetch');
const qs = require('querystring')

const httpsAgent = new https.Agent({
    keepAlive: true,
    keepAliveMsecs: 50 * 1000,
});

const domainMap = {}

var server = http.createServer(async function (req, res) {
    const proxy = httpProxy.createProxyServer({
        target: 'http://pan.baidu.com',
        agent: httpsAgent,
        changeOrigin: true,
        toProxy: true,
        proxyTimeout: 100 * 1000,
        timeout: 100 * 1000,
    }); 

    const urlParsed = url.parse(req.url, true)
    let domain0
    if (domainMap[urlParsed.query.uploadid]) {
        domain0 = domainMap[urlParsed.query.uploadid]
    } else {
        const qstr = qs.stringify({
            method: 'locateupload',
            appid: 250528,
            access_token: urlParsed.query.access_token,
            path: urlParsed.query.path,
            uploadid: urlParsed.query.uploadid,
            upload_version: '2.0'
        });

        const locateResRaw = await fetch(`https://d.pcs.baidu.com/rest/2.0/pcs/file?${qstr}`);
        const locateRes = await locateResRaw.json()
        domain0 = locateRes.servers[0].server;

        setTimeout(() => {
            delete domainMap[urlParsed.query.uploadid]
        }, locateRes.expire * 1000 - 5000) //不确定expire是不是这个意思
    }

    console.log(domain0, urlParsed.query.uploadid)
    proxy.web(req, res, { target: domain0 }, (err) => {
        console.log(err)
    });
});

const port = 3100;
console.log(`listening on port ${port}`)
server.listen(port);

Additional context / 附件

No response

21paradox avatar May 25 '24 07:05 21paradox