-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiing.py
executable file
·435 lines (405 loc) · 14.4 KB
/
iing.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python3
import api, points, base64, time, codecs, os, hashlib
from api.bottle import *
from shutil import copyfile
def write_to_log(line):
ip = request.environ.get('HTTP_X_FORWARDED_FOR') or request.environ.get('REMOTE_ADDR')
timestump = time.strftime("%d.%m.%y %H:%M", time.gmtime(int(time.time())))
open("iing.log", "a").write("%s, %s: %s\n" %(timestump, ip, line))
@route("/list.txt")
def list_txt():
write_to_log("list.txt")
response.set_header("content-type", "text/plain; charset=utf-8")
echoareas = api.echoareas
echoareas = echoareas + api.shortareas
echolist = ""
for echoarea in sorted(echoareas, key=lambda echoareas: echoareas[0]):
try:
echolist = echolist + "%s:%s:%s\n" % (echoarea[0], api.get_echoarea_count(echoarea[0]), echoarea[1])
except:
echolist = echolist + "%s:0:%s\n" % (echoarea[0], echoarea[1])
return echolist
@route("/u/e/<names:path>")
def index_list(names):
write_to_log("u/e/%s" % names)
response.set_header('content-type','text/plain; charset=utf-8')
result = ""
r = False
s = 0
e = 0
names = names.split("/")
if ":" in names[-1]:
s = int(names[-1].split(":")[0])
e = int(names[-1].split(":")[1])
r = True
names = names[0:-1]
for echoarea in names:
result = result + echoarea + "\n"
try:
ss = s
if s < 0 and s + int(api.get_echoarea_count(echoarea)) < 0:
ss = 0
elif s > int(api.get_echoarea_count(echoarea)):
ss = e * -1
if r:
if s + e == 0:
msglist = api.get_echoarea(echoarea)[ss:]
else:
msglist = api.get_echoarea(echoarea)[ss:ss + e]
else:
msglist = api.get_echoarea(echoarea)
result = result + "\n".join(msglist) + "\n"
except: result = result + ""
return result
@route("/u/m/<msgids:path>")
def outmsg(msgids):
response.set_header ('content-type','text/plain; charset=utf-8')
result = ""
for msgid in msgids.split("/"):
result = result + msgid + ":"
try:
msg = str.encode(api.get_msg(msgid))
result = result + (base64.b64encode(msg)).decode("utf-8") + "\n"
except:
result = result + "\n"
return result
def point(pauth, tmsg):
msgfrom, addr = points.check_point(pauth)
if not addr:
return "auth error!"
return api.toss_msg(msgfrom, addr, tmsg)
@route("/u/point/<pauth>/<tmsg:path>")
def point_get(pauth, tmsg):
write_to_log("point/%s GET\n" % pauth)
return point(pauth, tmsg)
@post("/u/point")
def point_post():
write_to_log("point/%s POST" % request.POST["pauth"])
return point(request.POST["pauth"], request.POST["tmsg"])
@route("/blacklist.txt")
def blacklist_txt():
write_to_log("blacklist.txt")
response.set_header("content-type", "text/plain; charset=utf-8")
return open("blacklist.txt", "r").read()
@route("/e/<echoarea>")
def e(echoarea):
write_to_log("e/%s" % echoarea)
response.set_header("content-type", "text/plain; charset=utf-8")
return "\n".join(api.get_echoarea(echoarea))
@route("/m/<msgid>")
def m(msgid):
response.set_header("content-type", "text/plain; charset=utf-8")
try:
return api.get_msg(msgid)
except:
return ""
@route("/x/small-echolist")
def x_small_echolist():
write_to_log("x/small-echolist")
response.set_header("content-type", "text/plain; charset=utf-8")
api.load_config()
echoareas = api.echoareas
echoareas = echoareas + api.shortareas
echolist = ""
for echoarea in sorted(echoareas, key=lambda echoareas: echoareas[0]):
echolist = echolist + echoarea[0] + "\n"
return echolist
@route("/x/caesium")
def x_caesium():
write_to_log("x/caesium")
response.set_header("content-type", "text/plain; charset=utf-8")
api.load_config()
echoareas = api.echoareas
echoareas = echoareas +api.shortareas
echolist = ""
for echoarea in sorted(echoareas, key=lambda echoareas: echoareas[0]):
echolist = echolist + "echo %s %s\n" % (echoarea[0], echoarea[1])
return echolist
@route("/x/file")
def file_error():
response.set_header("content-type", "text/plain; charset=utf-8")
return get_public_file_index()
@route("/x/filelist")
def public_filelist():
write_to_log("x/filelist")
response.set_header("content-type", "text/plain; charset=utf-8")
result = ""
for f in sorted(api.get_public_file_index(), key = lambda x: x[0]):
result = result + ":".join(f)
return result
@post("/x/filelist")
@route("/x/filelist/<pauth>")
def private_filelist(pauth = False):
response.set_header("content-type", "text/plain; charset=utf-8")
if not pauth:
try:
pauth = request.POST["pauth"]
except:
return "auth error!"
write_to_log("x/filelist %s" % pauth)
result = ""
files = sorted(api.get_public_file_index())
msgfrom, addr = points.check_point(pauth)
if not addr:
for f in sorted(files):
result = result + ":".join(f)
return result
files = files + api.get_file_index()
try:
files = files + api.get_private_file_index(msgfrom)
except:
None
for f in sorted(files):
result = result + ":".join(f)
return result
@route("/x/file/<filename:path>")
def get_public_file(filename):
ip = request['REMOTE_ADDR']
open("iing.log", "a").write("%s: x/file/%s\n" % (ip, filename))
response.set_header("content-type", "text/plain; charset=utf-8")
public_files = []
for line in codecs.open("files/indexes/public_files.txt", "r", "utf8").read().split("\n"):
try:
public_files.append(line.split(":")[0])
except:
None
if filename in public_files:
return static_file(filename, "files/")
else:
return "file not found"
@post("/x/file")
def post_file():
response.set_header("content-type", "text/plain; charset=utf-8")
try:
pauth = request.POST["pauth"]
except:
pauth = False
try:
filename = request.POST["filename"]
except:
filename = False
if not filename:
return "not filename found"
write_to_log("x/file/%s/%s POST" % (pauth, filename))
files = []
msgfrom, addr = points.check_point(pauth)
if addr:
for line in codecs.open("files/indexes/public_files.txt", "r", "utf8").read().split("\n"):
try:
files.append(line.split(":")[0])
except:
None
for line in codecs.open("files/indexes/files.txt", "r", "utf8").read().split("\n"):
try:
files.append(line.split(":")[0])
except:
None
if os.path.exists("files/indexes/" + msgfrom + "_files.txt"):
for line in codecs.open("files/indexes/" + msgfrom + "_files.txt", "r", "utf8").read().split("\n"):
try:
files.append(line.split(":")[0])
except:
None
fechoes = []
for fecho in os.listdir("fecho"):
fechoes.append(fecho)
for fecho in fechoes:
f = codecs.open("fecho/%s" % fecho, "r").read().split("\n")
for row in f:
if len(row) > 0:
r = row.split(":")
files.append(fecho + "/" + r[1])
if filename in files:
return static_file(filename, "files/")
else:
return "file not found"
files = []
for line in codecs.open("files/indexes/public_files.txt", "r", "utf8").read().split("\n"):
try:
files.append(line.split(":")[0])
except:
None
if filename in files:
return static_file(filename, "files/")
else:
return "file not found"
@route("/x/c/<echoareas:path>")
def xc(echoareas):
write_to_log("x/c/%s" % echoareas)
response.set_header("content-type", "text/plain; charset=utf-8")
try:
echoareas = echoareas.split("/")
ret = []
for echoarea in echoareas:
ret.append("%s:%s" %(echoarea, api.get_echoarea_count(echoarea)))
return "\n".join(ret)
except:
return ""
@route("/x/features")
def features():
response.set_header("content-type", "text/plain; charset=utf-8")
return "u/e\nlist.txt\nblacklist.txt\nx/file\nx/small-echolist\nx/caesium\nx/c\nf/"
@route("/robots.txt")
def robots():
response.set_header("content-type", "text/plain; charset=utf-8")
if api.norobots:
return "User-agent: *\nDisallow: /"
else:
return "User-agent: *\nAllow: /"
@route("/f/c/<fechoes:path>")
def fecho_counts(fechoes):
response.set_header("content-type", "text/plain; charset=utf-8")
fechoes = fechoes.split("/")
counts = ""
for fecho in fechoes:
counts = counts + fecho + ":" + str(len(api.get_fechoarea(fecho))) + "\n"
return counts
@route("/f/e/<fechoes:path>")
def fecho_index(fechoes):
index = ""
fechoes = fechoes.split("/")
ip = request['REMOTE_ADDR']
open("iing.log", "a").write("%s: f/e/%s\n" % (ip, fechoes))
response.set_header("content-type", "text/plain; charset=utf-8")
s = 0
e = 0
if ":" in fechoes[-1]:
s = int(fechoes[-1].split(":")[0])
e = int(fechoes[-1].split(":")[1])
r = True
fechoes = fechoes[0:-1]
files = []
if s != 0 and e != 0:
s = int(s)
e = int(e)
for fecho in fechoes:
files.append([fecho])
ss = s
if s < 0 and s + len(api.get_fechoarea(fecho)) < 0:
ss = 0
elif s > len(api.get_fechoarea(fecho)):
ss = e * -1
if s + e == 0:
for f in api.get_fechoarea(fecho)[ss:]:
files.append(f)
else:
for f in api.get_fechoarea(fecho)[ss:ss + e]:
files.append(f)
else:
for fecho in fechoes:
files.append([fecho])
for f in api.get_fechoarea(fecho):
files.append(f)
blacklist = open("fblacklist.txt", "r").read().split("\n")
for row in files:
print(row)
if not row[0] in blacklist:
if len(row) > 1:
index = index + ":".join(row) + "\n"
else:
index = index + row[0] + "\n"
return index
@route("/f/f/<fecho>/<fid>")
def fecho_file(fecho, fid):
if not os.path.exists("fecho/%s" % fecho):
return "file echoarea not found"
blacklist = open("fblacklist.txt", "r").read().split("\n")
if fid in blacklist:
return "file blocked"
fecho_ = open("fecho/%s" % fecho, "r").read().split("\n")
fids = [f.split(":")[0] for f in fecho_]
files = [f.split(":")[1] for f in fecho_ if len(f) > 0]
if not fid in fids:
return "file not found"
return static_file(files[fids.index(fid)], "files/%s/" % fecho)
@post("/f/p")
def fecho_post():
response.set_header("content-type", "text/plain; charset=utf-8")
try:
pauth = request.POST["pauth"]
except:
pauth = False
try:
fecho = request.POST["fecho"]
except:
fecho = False
try:
f = request.files.get("file")
except:
f = False
try:
dsc = request.POST["dsc"]
except:
dsc = False
if pauth and fecho and f and dsc:
if not api.file_filter(f.raw_filename):
return 'incorrect filename'
if api.fecho_filter(fecho):
msgfrom, addr = points.check_point(pauth)
if addr:
f.save("temp")
if not os.path.exists("files/%s" % fecho):
os.makedirs("files/%s" % fecho)
hsh = api.fhsh(open("./temp", "rb").read())
hshs = []
try:
for row in open("fecho/%s" % fecho, "r").read().split("\n"):
hshs.append(row.split(":")[0])
except:
None
blacklist = open("fblacklist.txt", "r").read().split("\n")
if not hsh in hshs and not hsh in blacklist:
name = f.raw_filename
while os.path.exists("files/%s/%s" % (fecho, name)):
tmp = name.split(".")
name = ".".join(tmp[:-1])
suffix = name.split("_")[-1]
if suffix == name:
suffix = "0"
try:
s = int(suffix)
s += 1
post = "_" + str(s)
except:
post = "_1"
if suffix != "0":
Name = name.replace("_" + suffix, post) + "." + tmp[-1]
else:
name = name + post + "." + tmp[-1]
try:
size = str(os.stat("temp").st_size)
except:
size = "0"
copyfile("temp", "files/%s/%s" % (fecho, name))
os.remove("temp")
codecs.open("fecho/%s" % fecho, "a", "utf8").write("%s:%s:%s:%s,%s:%s\n" % (hsh, name, size, api.nodename, addr, dsc.replace("\n", " ").replace("\r", "")))
codecs.open("files/indexes/files.txt", "a", "utf8").write("%s/%s:%s\n" % (fecho, name, dsc.replace("\n", " ").replace("\r", "")))
return "file ok:%s" % hsh
else:
os.remove("./temp")
return "file exists"
os.remove("./temp")
else:
return "error: no auth"
else:
return "incorrect file echoarea"
@route("/f/blacklist.txt")
def file_blacklist():
write_to_log("f/blacklist.txt")
response.set_header("content-type", "text/plain; charset=utf-8")
return open("fblacklist.txt", "r").read()
@route("/f/list.txt")
def felist():
write_to_log("f/list.tx")
response.set_header("content-type", "text/plain; charset=utf-8")
ret = ""
for fecho in api.fechoareas:
flen = len(open("fecho/%s" % fecho[0], "r").read().split("\n")) - 1
ret = ret + fecho[0] + ":" + str(flen) + ":" + fecho[1] + "\n"
return ret
api.check_config()
api.load_config()
api.init()
if api.web_interface:
from api.web import *
run(host="0.0.0.0", port=3000)#, quiet=True)