-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathshortlink
114 lines (108 loc) · 4.75 KB
/
shortlink
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
#!/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
'content-type': 'text/plain',
}
outBody = list()
inForm = cgi.FieldStorage()
service = inForm.getfirst('service') if ('service' in inForm) else ''
url = inForm.getfirst('url') if ('url' in inForm) else ''
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'}):
if service == 'v.gd':
reqParams = {
'format': 'simple',
'url': url,
}
req = requests.get('https://v.gd/create.php', params=reqParams, timeout=10)
if req.status_code == 502: # 502 Bad Gateway means rate limited
sleep(5)
req = requests.get('https://v.gd/create.php', params=reqParams, timeout=10)
#if 502
if req.status_code == 200:
shorttokens = req.text.split('/',3)
if (len(shorttokens) >= 4) and (shorttokens[0] in {'http:','https:'}) and (shorttokens[1] == '') and (shorttokens[2] in {'v.gd','www.v.gd'}):
outHeaders['status'] = 200
outBody.append('https://edsy.org' + ('/dev/s/' if DEV else '/s/') + 'v' + shorttokens[3])
else:
outHeaders['status'] = 400
outBody.append('got invalid shortlink' + ((': ' + req.text) if DEV else ''))
#if v.gd
else:
outHeaders['status'] = req.status_code
outHeaders['content-type'] = req.headers['content-type']
outBody.append(req.text)
#if 200
elif service == 'ulvis.net':
reqParams = {
'url': url,
'type': 'json',
'private': 1,
}
req = requests.get('https://ulvis.net/API/write/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/write/get', params=reqParams, timeout=10)
#if 502
if req.status_code == 200:
try:
resp = json.loads(req.text)
if (
isinstance(resp, dict) and resp.get('success') and isinstance(resp.get('data',None),dict) and
'id' in resp['data'] and resp['data'].get('url','').startswith('https://ulvis.net/') and resp['data'].get('full') == url
):
outHeaders['status'] = 200
outBody.append('https://edsy.org' + ('/dev/s/' if DEV else '/s/') + 'u' + resp['data']['id'])
else:
outHeaders['status'] = 400
outBody.append('got invalid response' + ((': ' + req.text) if DEV else ''))
outBody.append(str(resp.get('success')))
outBody.append(str(type(resp.get('data'))))
outBody.append(resp.get('data',{}).get('url') + " =?= " + url)
outBody.append(resp.get('data',{}).get('full'))
outBody.append(resp.get('data',{}).get('id'))
#if ok
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
outBody.append('invalid service')
#if service
else:
outHeaders['status'] = 400
outBody.append(('invalid' if url else 'missing') + ' url')
#if url
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