-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
executable file
·269 lines (226 loc) · 9.47 KB
/
server.py
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/usr/bin/env python
import tornado.curl_httpclient
import tornado.ioloop
import tornado.web
from tornado.web import HTTPError
from tornado import template
from tornado import gen
import argparse
import collections
import json
import logging
import os
import re
import socket
import uuid
import urllib
import redis
logger = logging.getLogger(__name__)
tornado.ioloop.IOLoop.current().set_blocking_log_threshold(1)
APP_KEY = os.environ.get('DBX_APP_KEY')
APP_SECRET = os.environ.get('DBX_APP_SECRET')
BASE_URL = os.environ.get('BASE_URL')
COOKIE_SECRET = os.environ.get('COOKIE_SECRET')
DEBUG = os.environ.get('DEBUG') == '1'
USER_AGENT = 'PubFolder/1.0 (Python 3.6)'
io_loop = tornado.ioloop.IOLoop.current()
http_client = tornado.curl_httpclient.CurlAsyncHTTPClient(io_loop, max_clients=16)
# DB operations
red = redis.StrictRedis(host='localhost', port=6379, db=0)
def store_auth(uid, access_token):
red.set('u:' + uid, access_token.encode('utf-8'))
def get_auth(uid):
tok = red.get('u:' + uid)
if tok:
tok = tok.decode('utf-8')
return tok
class OurHandler(tornado.web.RequestHandler):
def write_error(self, status_code, **kwargs):
self.set_status(status_code)
if 'message' in kwargs:
message = kwargs['message']
else:
try:
message = kwargs['exc_info'][1].log_message
except Exception:
message = 'Unknown error.'
self.render('error.html', error_message='Error %s' % status_code, details=message)
def render(self, template_name, **kwargs):
if 'logged_in' not in kwargs:
kwargs['logged_in'] = bool(self.get_secure_cookie('dbx_uid'))
return super().render(template_name, **kwargs)
class PublicFolderHandler(OurHandler):
@gen.coroutine
def get(self, uid, path):
token = get_auth(uid)
if token is None:
# TODO: render something saying that the user is not authenticated with us
self.render('error.html', error_message="404: Page Not Found :(", details="This is not the page you are looking for.")
return
# Try to list shared links for the user for this path
url = 'https://api.dropboxapi.com/2/sharing/list_shared_links'
body = json.dumps({
"path": '/' + path,
})
hreq = tornado.httpclient.HTTPRequest(url, method='POST', user_agent=USER_AGENT, headers={
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
}, body=body, request_timeout=120)
try:
resp = yield http_client.fetch(hreq)
except Exception as e:
logger.exception("Exception when requesting %r", url)
raise HTTPError(500, 'Unable to obtain link from Dropbox')
js = json.loads(resp.buffer.read().decode('utf8'))
# TODO: Filter links without public visibility
try:
url = js['links'][0]['url']
lower_path = js['links'][0]['path_lower'][1:]
except IndexError:
lower_path = None
url = None
if url is None:
# Generate the shared link for that path
url = 'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings'
body = json.dumps({
"path": '/' + path,
"settings": {
"requested_visibility": "public",
},
})
hreq = tornado.httpclient.HTTPRequest(url, method='POST', user_agent=USER_AGENT, headers={
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
}, body=body, request_timeout=120)
try:
resp = yield http_client.fetch(hreq)
except Exception as e:
logger.exception("Exception when requesting %r", url)
raise HTTPError(500, 'Unable to obtain link from Dropbox')
js = json.loads(resp.buffer.read().decode('utf8'))
url = js['url']
lower_path = js['path_lower'][1:]
# Make it a direct link
try:
sid = re.search(r'^https://www.dropbox.com/s/([^/]*)/', url).group(1)
except Exception:
# Fall back on using the DL link
url = url.replace('?dl=0', '?dl=1')
else:
url = 'https://dl.dropboxusercontent.com/1/view/{sid}/{path}'.format(sid=sid, path=lower_path)
self.redirect(url)
class ListFolderHandler(OurHandler):
@gen.coroutine
def get(self, path):
token = None
uid = self.get_secure_cookie('dbx_uid')
if uid:
uid = uid.decode('utf8')
token = get_auth(uid)
if token is None:
self.clear_cookie('dbx_uid')
self.render('error.html', error_message="User not found", details="You are not logged in, or your Dropbox authorization has expired. Please login again to reauthorize your Dropbox.")
return
path = path.rstrip('/')
url = 'https://api.dropboxapi.com/2/files/list_folder'
body = json.dumps({
"path": path,
})
hreq = tornado.httpclient.HTTPRequest(url, method='POST', user_agent=USER_AGENT, headers={
'Authorization': 'Bearer ' + token,
'Content-Type': 'application/json',
}, body=body, request_timeout=120)
try:
resp = yield http_client.fetch(hreq)
except Exception as e:
logger.exception("Exception when requesting %r", url)
raise HTTPError(500, 'Unable to obtain folder list from Dropbox')
js = json.loads(resp.buffer.read().decode('utf8'))
for entry in js['entries']:
if entry['.tag'] == 'file':
entry['our_path'] = urllib.parse.urljoin(BASE_URL, uid) + entry['path_lower']
else:
entry['our_path'] = urllib.parse.urljoin(BASE_URL, 'list') + entry['path_lower'] + '/'
self.render('list.html', entries=js['entries'])
class RootHandler(OurHandler):
@gen.coroutine
def get(self):
self.render('home.html')
class FAQHandler(OurHandler):
@gen.coroutine
def get(self):
self.render('faq.html')
class Error404Handler(OurHandler):
@gen.coroutine
def get(self, path):
self.set_status(404)
self.render('error.html', error_message="404: Page Not Found :(", details="This is not the page you are looking for.")
class LoginHandler(OurHandler):
@gen.coroutine
def get(self):
url = urllib.parse.urljoin(BASE_URL, '/login/continue')
self.render("login.html",client_id=APP_KEY, redirect_uri=urllib.parse.quote(url))
class LoginContinueHandler(OurHandler):
@gen.coroutine
def get(self):
code = self.get_argument('code')
url = 'https://api.dropboxapi.com/oauth2/token'
body = urllib.parse.urlencode({
'code': code,
'grant_type': 'authorization_code',
'redirect_uri': urllib.parse.urljoin(BASE_URL, '/login/continue'),
})
hreq = tornado.httpclient.HTTPRequest(url, method='POST', user_agent=USER_AGENT,
auth_username=APP_KEY, auth_password=APP_SECRET,
body=body, request_timeout=120)
try:
resp = yield http_client.fetch(hreq)
except Exception:
logger.exception("Exception when requesting %r", url)
raise HTTPError(500, 'Unable to obtain authorization code from Dropbox')
js = json.loads(resp.buffer.read().decode('utf8'))
self.set_secure_cookie('dbx_uid', js['uid'])
store_auth(uid=js['uid'], access_token=js['access_token'])
# Example JS:
# {
# access_token: "-c0JMq0qLncAAAAAAABqMedBF0-_QbmwWHToG4jXXctB1qNBSxm0aZYhupbtC3PK",
# token_type: "bearer",
# uid: "113409",
# account_id: "dbid:AABkoAh-HU3E5gJ2Ed1XR_Yait3IBadk0Ps"
# }
self.redirect('/')
class LogoutHandler(OurHandler):
@gen.coroutine
def get(self):
self.clear_cookie('dbx_uid')
self.render('error.html', error_message='Logged out', details='You have been logged out.', logged_in=False)
def make_app():
return tornado.web.Application([
(r"/", RootHandler),
(r"/(\d+)/(.*)", PublicFolderHandler),
(r"/list(/?.*)", ListFolderHandler),
(r"/login", LoginHandler),
(r"/login/continue", LoginContinueHandler),
(r"/logout", LogoutHandler),
(r"/faq", FAQHandler),
(r'/(favicon.ico)', tornado.web.StaticFileHandler, {'path': 'static'}),
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': 'static'}),
(r"/(.*)", Error404Handler),
], template_path="templates", cookie_secret=COOKIE_SECRET, debug=DEBUG)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser(description="Run the Tornado server")
parser.add_argument('start_port', default=8000, type=int, nargs='?',
help="What port should we start on? (default 8000)")
parser.add_argument('offset_port', default=0, type=int, nargs='?',
help="Add this number to the port number (optional)")
args = parser.parse_args()
port = args.start_port + args.offset_port
logger.info("Starting Tornado on port %s", port)
app = make_app()
app.listen(port, address='127.0.0.1')
try:
tornado.ioloop.IOLoop.current().start()
finally:
print("Stopping the server :(")
Error404Handler