Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
82 lines (68 loc) · 3.08 KB
/
Copy pathserver.py
File metadata and controls
82 lines (68 loc) · 3.08 KB
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
from flask import Flask, jsonify, request
import requests
from io import BytesIO
from PIL import Image, ImageOps
from websockets.sync.client import connect
app = Flask(__name__)
http_methods = {"am": requests.get, "ar": requests.post, "az": requests.put, "eu": requests.delete, "bg": requests.patch, "ca": requests.options}
http_body_methods = (requests.post, requests.put, requests.patch)
types = {"text/html": "h", "text/css": "c", "text/plain": "t", "image/avif": "i", "image/bmp": "i", "image/gif": "i", "image/jpeg": "i", "image/png": "i", "image/tiff": "i", "application/json": "j", "application/xml": "x", "text/xml": "x"}
color_to_b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
def process_image(_image: bytes):
image = Image.open(BytesIO(_image))
image = image.convert("RGB")
if image.width > 256 or image.height > 256:
image = ImageOps.contain(image, (256, 256))
palette = Image.open("palette.png")
image = image.quantize(palette=palette)
string = ""
for j in range(image.height):
for i in range(image.width):
pixel = image.getpixel((i, j))
char = color_to_b64[pixel]
string += char
string += "="
return string
@app.route('/translate', methods=['GET'])
def server():
full_txt = request.args["text"].split("|")
txt = full_txt[0]
command = request.args["language"]
match txt.split(":")[0]: # Scheme of URI
case "http" | "https":
method = http_methods[command]
if method in http_body_methods:
body = full_txt[1]
result = method(txt, body)
filetype = types.get(result.headers["content-type"].split(";")[0], "d")
data = {"result": "|".join((str(result.status_code), filetype, result.text))}
else:
result = method(txt)
if request.args["language"] == "ca":
filetype = "n"
header = "allow"
else:
filetype = types.get(result.headers["content-type"].split(";")[0], "d")
match filetype:
case "h" | "c" | "t" | "j" | "x":
text = result.text
case "i":
text = process_image(result.content)
case "d":
text = result.content.hex()
case "n":
text = result.headers[header]
data = {"result": "|".join((str(result.status_code), filetype, text))}
case "ws" | "wss":
body = full_txt[1]
with connect(txt) as socket:
socket.send(body)
data = {"result": f"{socket.recv()}"}
case _:
data = {"result": "ERROR! That protocol is not supported. Supported protocols: HTTP, WebSocket"}
print(data)
res = jsonify(data)
res.headers.add('Access-Control-Allow-Origin', '*')
return res
if __name__ == '__main__':
app.run(host='127.0.0.1', port=443, ssl_context=("server.crt", "server.key"))