-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
118 lines (98 loc) · 3.7 KB
/
server.py
File metadata and controls
118 lines (98 loc) · 3.7 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
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
from base64 import urlsafe_b64encode
from bottle import post, request, response, route, run
from base.sqlite import Sqlite
import json
@route("/")
def index():
response.set_header("Content-Type", "text/plain; charset=utf-8")
return "Welcome to IDEC!"
@route("/list.txt")
def echoareas_list():
response.set_header("Content-Type", "text/plain; charset=utf-8")
response.set_header("Access-Control-Allow-Origin", "*")
config = json.loads(open("server.json").read())
echoareas = []
for echoarea in config["echoareas"]:
echoareas.append(echoarea["name"])
counts = base.get_counts(echoareas)
list_txt = []
for echoarea in config["echoareas"]:
list_txt.append("{}:{}:{}".format(
echoarea["name"],
counts[echoarea["name"]] if echoarea["name"] in counts else 0,
echoarea["description"]
))
return "\n".join(list_txt) + "\n\n"
@route("/blacklist.txt")
def blacklist():
response.set_header("Content-Type", "text/plain; charset=utf-8")
return "\n".join(base.get_blacklist()) + "\n\n"
@route("/e/<echoarea>")
def echoarea_index(echoarea):
response.set_header("Content-Type", "text/plain; charset=utf-8")
response.set_header("Access-Control-Allow-Origin", "*")
return "\n".join(base.get_index([echoarea])) + "\n\n"
@route("/m/<msgid>")
def message(msgid):
response.set_header("Content-Type", "text/plain; charset=utf-8")
response.set_header("Access-Control-Allow-Origin", "*")
return base.get_message(msgid)
@route("/u/e/<echoareas:path>")
def universal_echoareas_index(echoareas):
response.set_header("Content-Type", "text/plain; charset=utf-8")
echoareas = echoareas.split("/")
start, end, slc = 0, 0, False
if ":" in echoareas[-1]:
slc = echoareas[-1].split(":")
start, end, slc = int(slc[0]), int(slc[1]), True
echoareas = echoareas[:-1]
ue_index = []
for echoarea in echoareas:
ue_index.append(echoarea)
ss = start
index = base.get_index([echoarea])
if start < 0 and start + len(index) < 0:
ss = 0
elif start > len(index):
ss = end * -1
if start + end == 0:
ue_index = ue_index + index[ss:]
elif slc:
ue_index = ue_index + index[ss:ss + end]
else:
ue_index = ue_index + index
return "\n".join(ue_index) + "\n\n"
@route("/u/m/<msgids:path>")
def universal_bundle(msgids):
response.set_header("Content-Type", "text/plain; charset=utf-8")
bundle = []
for msgid in msgids.split("/"):
if base.is_message_exists(msgid):
encoded = urlsafe_b64encode(base.get_message(msgid).encode())
bundle.append(msgid + ":" + encoded.decode("utf-8"))
return "\n".join(bundle) + "\n\n"
@post("/u/point")
@route("/u/point/<pauth>/<tmsg>")
def receive_message(pauth: str = "", tmsg: str = ""):
response.set_header("Content-Type", "text/plain; charset=utf-8")
response.set_header("Access-Control-Allow-Origin", "*")
if request.method == "POST":
pauth = request.POST["pauth"]
tmsg = request.POST["tmsg"]
point = base.check_point(config["nodename"], pauth)
if point:
status = base.toss_message(point, tmsg)
return status
return "error:login incorrect"
@route("/x/c/<echoareas:path>")
def echoareas_count(echoareas: str):
response.set_header("Content-Type", "text/plain; charset=utf-8")
echoareas = echoareas.split("/")
counts = base.get_counts(echoareas)
xc = ""
for echoarea in echoareas:
xc += "{}:{}\n".format(echoarea, counts[echoarea])
return xc
config = json.loads(open("server.json").read())
base = Sqlite(config["base"])
run(host="0.0.0.0", port=62220)