forked from taleden/EDSY
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlonglink
110 lines (104 loc) · 4.44 KB
/
longlink
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
#!/usr/local/bin/python3
"""
EDSY was created using assets and imagery from Elite Dangerous, with the permission of Frontier Developments plc, for non-commercial purposes.
It is not endorsed by nor reflects the views or opinions of Frontier Developments and no employee of Frontier Developments was involved in the making of it.
Except where noted otherwise, all design, markup and script code for EDSY is copyright (c) 2015-2024 taleden
and is provided under a Creative Commons Attribution-NonCommercial 4.0 International License (http://creativecommons.org/licenses/by-nc/4.0/).
The Elite Dangerous game logic and data in this file remains the property of Frontier Developments plc, and is used here as authorized by
Frontier Customer Services (https://forums.frontier.co.uk/index.php?threads/elite-dangerous-media-usage-rules.510879/).
"""
import cgi, cgitb, json, os, requests, sys
from time import sleep
cgitb.enable()
LOCAL = ('REQUEST_URI' not in os.environ)
DEV = (not LOCAL) and os.environ['REQUEST_URI'].startswith('/dev/')
try:
outHeaders = {
'status': 500,
'cache-control': 'no-cache,no-store,must-revalidate,private',
'expires': 'Sat, 01 Jan 2000 00:00:00 GMT',
'pragma': 'no-cache',
'strict-transport-security': 'max-age=300; includeSubDomains', # max-age=31536000; preload
}
outBody = list()
slug = ''
if (not LOCAL) and os.environ['REQUEST_URI'].startswith('/dev/s/' if DEV else '/s/'):
slug = os.environ['REQUEST_URI'][(7 if DEV else 3):]
else:
inForm = cgi.FieldStorage()
slug = inForm.getfirst('slug') if ('slug' in inForm) else ''
#if path/form
if slug.startswith('v'): # v.gd
reqParams = {
'format': 'simple',
'shorturl': slug[1:],
}
req = requests.get('https://v.gd/forward.php', params=reqParams, timeout=10)
if req.status_code == 502: # 502 Bad Gateway means rate limited
sleep(5)
req = requests.get('https://v.gd/forward.php', params=reqParams, timeout=10)
#if 502
if req.status_code == 200:
url = req.text
urltokens = url.split('/',3)
if (len(urltokens) >= 3) and (urltokens[0] in {'http:','https:'}) and (urltokens[1] == '') and (urltokens[2] in {'edsy.org','www.edsy.org'}):
outHeaders['status'] = 301
outHeaders['location'] = url
else:
outHeaders['status'] = 400
outHeaders['content-type'] = 'text/plain'
outBody.append('invalid shortlink')
#if edsy.org
else:
outHeaders['status'] = req.status_code
outHeaders['content-type'] = req.headers['content-type']
outBody.append(req.text)
#if 200
elif slug.startswith('u'): # ulvis.net
reqParams = {
'id': slug[1:],
'type': 'json',
}
req = requests.get('https://ulvis.net/API/read/get', params=reqParams, timeout=10)
if req.status_code == 502: # 502 Bad Gateway means rate limited
sleep(5)
req = requests.get('https://ulvis.net/API/read/get', params=reqParams, timeout=10)
#if 502
if req.status_code == 200:
try:
resp = json.loads(req.text)
urltokens = resp.get('data',{}).get('full','').split('/',3) if isinstance(resp, dict) else ['','','']
if (
isinstance(resp, dict) and resp.get('success') and isinstance(resp.get('data',None),dict) and
resp['data'].get('id') == slug[1:] and (len(urltokens) >= 3) and (urltokens[0] in {'http:','https:'}) and (urltokens[1] == '') and (urltokens[2] in {'edsy.org','www.edsy.org'})
):
outHeaders['status'] = 301
outHeaders['location'] = resp['data']['full']
else:
outHeaders['status'] = 400
outHeaders['content-type'] = 'text/plain'
outBody.append('invalid shortlink' + ((': ' + req.text) if DEV else ''))
#if edsy.org
except json.JSONDecodeError:
outHeaders['status'] = 400
outBody.append('got invalid response' + ((': ' + req.text) if DEV else ''))
#try
else:
outHeaders['status'] = req.status_code
outHeaders['content-type'] = req.headers['content-type']
outBody.append(req.text)
#if 200
else:
outHeaders['status'] = 400
outHeaders['content-type'] = 'text/plain'
outBody.append(('invalid' if slug else 'missing') + ' shortlink')
#if slug
print("%s%s\r\n%s" % (
"\r\n".join(("%s: %s" % (key,val)) for key,val in outHeaders.items()),
"\r\n" if outHeaders else "",
"\n".join(outBody)
), end="")
except:
print("Status:500\r\nCache-Control: no-cache,no-store,must-revalidate,private\r\nExpires: Sat, 01 Jan 2000 00:00:00 GMT\r\nPragma: no-cache\r\nStrict-Transport-Security: max-age=300; includeSubDomains\r\nContent-type: text/html\r\n\r\n", end="")
print(cgitb.html(sys.exc_info()))
#try/except