File size: 5,008 Bytes
e676a42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import base64
import json
import time

import requests
from crypto_plus import CryptoPlus
from flask import Flask, request

rsa = CryptoPlus.loads('''-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC72/8TD242+vn0FDQm8YyeY2WH
rNIxpiCgGT6H5EbDgo7mAXy5+LtJ/imCrqfYli4mwW3SPagtGlTo1OrlqafX+pIs
ehYnKuMQEW9nPbJ0z3ItrFx1cTC70Dx3mk6mrK+KOx6XgfiLqrgGy/wysbPX5PdN
Apg4Wc3GsMk8UpdtGQIDAQAB
-----END PUBLIC KEY-----''')


def is_expired(access_token):
    if not access_token:
        return True
    try:
        message, signature = access_token.rsplit('.', 1)
        header, payload = message.split('.')
        payload = payload + '=' * - (len(payload) % - 4)
        signature = signature + '=' * - (len(signature) % - 4)
        exp = json.loads(base64.b64decode(payload).decode()).get('exp')
        return exp - time.time() < 10 or not rsa.verify(message.encode(), base64.urlsafe_b64decode(signature))
    except:
        return True


app = Flask(__name__)


@app.route('/', methods=['GET', 'POST'])
def download_url_form():
    if request.method == 'POST':
        file_id = request.form['file_id']
        access_token = request.form['access_token']
        if is_expired(access_token):
            return 'token is expired', 401

        drive_info = requests.post('https://bj29.api.aliyunpds.com/v2/drive/list_my_drives', json={
        }, headers={
            "Authorization": f"Bearer {access_token}",
        }).json()
        driver_id_map = {i['category']: i['drive_id'] for i in drive_info['items'] if
                         i['category'] in ['backup', 'resource']}
        resp = requests.post('https://bj29.api.aliyunpds.com/v2/file/get_download_url', json={
            'drive_id': driver_id_map['resource'],
            'file_id': file_id,
            'expire_sec': 115200,
        }, headers={
            'Authorization': f'Bearer {access_token}',
        })
        if resp.status_code == 404:
            resp = requests.post('https://bj29.api.aliyunpds.com/v2/file/get_download_url', json={
                'drive_id': driver_id_map['backup'],
                'file_id': file_id,
                'expire_sec': 115200,
            }, headers={
                'Authorization': f'Bearer {access_token}',
            })
        if resp.status_code == 200:
            data = resp.json()
            url = data.get('cdn_url')
            if not url:
                url = data.get('url')
            return f'<h1>Download URL:</h1><p>{url}</p>'
        return f'<h1>Error:</h1><p>{resp.reason}</p>'

    # 如果是 GET 请求,渲染表单
    return '''
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Download URL Form</title>
            <style>
                body {
                    font-family: Arial, sans-serif;
                    background-color: #f0f0f0;
                }
                .container {
                    max-width: 400px;
                    margin: 50px auto;
                    padding: 20px;
                    background-color: #fff;
                    border-radius: 5px;
                    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                }
                h1 {
                    font-size: 24px;
                    margin-bottom: 20px;
                    text-align: center;
                }
                form {
                    display: flex;
                    flex-direction: column;
                }
                label {
                    font-size: 16px;
                    margin-bottom: 8px;
                }
                input[type="text"] {
                    padding: 10px;
                    margin-bottom: 20px;
                    border: 1px solid #ccc;
                    border-radius: 5px;
                    font-size: 16px;
                }
                button {
                    padding: 10px;
                    background-color: #007bff;
                    color: #fff;
                    border: none;
                    border-radius: 5px;
                    font-size: 16px;
                    cursor: pointer;
                }
                button:hover {
                    background-color: #0056b3;
                }
            </style>
        </head>
        <body>
            <div class="container">
                <h1>Download URL Form</h1>
                <form action="/" method="post">
                    <label for="file_id">File ID:</label>
                    <input type="text" id="file_id" name="file_id" required>
                    <label for="access_token">Access Token:</label>
                    <input type="text" id="access_token" name="access_token" required>
                    <button type="submit">Get Download URL</button>
                </form>
            </div>
        </body>
        </html>
    '''


if __name__ == '__main__':
    app.run('0.0.0.0',7860)