-
Notifications
You must be signed in to change notification settings - Fork 0
/
spy.py
executable file
·1463 lines (1312 loc) · 51.6 KB
/
spy.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# pylint: disable=line-too-long, consider-using-f-string, c-extension-no-member
"""
################################################################################
# SPY.PY # slv #
################################################################################
# Shows users logged into glftpd, in terminal or as web page #
# Like 'gl_spy' and 'webspy' from foo-tools #
# Uses SHM and glftpd's 'ONLINE' C struct, module sysv_ipc is required #
# See README and comments in spy.conf for install and config instructions #
################################################################################
"""
import struct
import re
import time
import datetime
import configparser
import os
import sys
import socket
import calendar
import collections
import subprocess
import sysv_ipc
_WITH_GEOIP = False
_WITH_HTTPD = True
_WITH_FLASK = True
_WITH_BUNDLE = False
PYINSTALLER = False
if _WITH_HTTPD:
import http.server
import socketserver
import threading
if _WITH_FLASK:
import flask
if _WITH_GEOIP:
import geoip2.webservice
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
PYINSTALLER = True
VERSION = "20240803"
SCRIPT_PATH = os.path.abspath(__file__) if os.path.abspath(__file__) else os.path.realpath(sys.argv[0])
if PYINSTALLER:
SCRIPT_PATH = os.path.realpath(sys.argv[0])
if _WITH_BUNDLE:
BUNDLE_DIR = os.path.abspath(os.path.dirname(__file__)) if os.path.dirname(__file__) else sys._MEIPASS
SCRIPT = os.path.basename(sys.argv[0])
SCRIPT_NAME = os.path.splitext(SCRIPT)[0]
SCRIPT_DIR = os.path.dirname(SCRIPT_PATH)
UPLOADS = DOWNLOADS = 0
TOTAL_UP_SPEED = TOTAL_DN_SPEED = 0
ONLINEUSERS = BROWSERS = IDLERS = 0
SHOWALL = 0
GEOIP2_BUF = {}
GEOIP2_CLIENT = None
CLI_MODE = 1
HTTPD_MODE = 0
FLASK_MODE = 0
FLASK_PROXY= False
CLI_SEARCH = 0 # search username in list, not that usefull - disabled by default
SPY_VERSTR = f"spy-{VERSION}"
if _WITH_GEOIP:
SPY_VERSTR += '-geoip'
if _WITH_HTTPD:
SPY_VERSTR += '-httpd'
if _WITH_FLASK:
SPY_VERSTR += '-flask'
# handle args
##############
if '-h' in sys.argv or '--help' in sys.argv:
print(f'./{SCRIPT_NAME} [--cli|--httpd|--flask|--web]')
sys.exit(0)
elif '-v' in sys.argv or '--version' in sys.argv:
print(SPY_VERSTR)
sys.exit(0)
elif len(sys.argv) > 1 and len(sys.argv[1]) in [5, 7]:
if '--cli' in sys.argv or '--spy' in sys.argv:
CLI_MODE = 1
print('No need specify spy/cli mode as its the default')
elif _WITH_HTTPD and sys.argv[1] == '--httpd':
CLI_MODE = 0
HTTPD_MODE = 1
elif _WITH_FLASK and ('--web' in sys.argv or '--flask' in sys.argv):
CLI_MODE = 0
FLASK_MODE = 1
else:
sys.exit(0)
else:
if len(sys.argv) > 1 and sys.argv[1][0] == '-':
print("Error: invalid option, try '-h'\n")
sys.exit(1)
# config file
##############
CONFIGFILE = f'{SCRIPT_DIR}/{SCRIPT_NAME}.conf'
CONFIG_FN = None
config = configparser.ConfigParser()
config_errors = []
# f'{os.getcwd()}/spy.conf'
for CONFIG_FN in set([CONFIGFILE, f'{SCRIPT_DIR}/spy.conf']):
try:
os.path.isfile(CONFIG_FN)
except OSError as config_err:
config_errors.append(config_err)
else:
break
try:
with open(CONFIG_FN, 'r', encoding='utf-8', errors='ignore') as config_obj:
config.read_string("[DEFAULT]\n" + config_obj.read())
except IOError as config_err:
config_errors.append(config_err)
if len(config_errors) > 0:
print('Error: opening config file')
for config_err in config_errors:
print(config_err)
sys.exit(1)
try:
glrootpath = config['DEFAULT']['glrootpath']
ipc_key = config.get('DEFAULT', 'ipc_key', fallback='')
maxusers = config.getint('DEFAULT', 'maxusers', fallback=20)
nocase = config.getboolean('DEFAULT', 'case_insensitive', fallback=False)
idle_barrier = config.getint('DEFAULT', 'idle_barrier', fallback=30)
threshold = config.getint('DEFAULT', 'speed_threshold', fallback=1024)
refresh = config.getfloat('DEFAULT', 'refresh', fallback=1)
color = config.getint('DEFAULT', 'color', fallback=1)
debug = config.getint('DEFAULT', 'debug', fallback=0)
httpd_host = config.get('WEB', 'httpd_host', fallback='localhost')
httpd_port = config.getint('WEB', 'httpd_port', fallback=8080)
flask_host = config.get('WEB', 'flask_host', fallback='localhost')
flask_port = config.getint('WEB', 'flask_port', fallback=5000)
geoip2_accountid = config['GEOIP']['geoip2_accountid']
geoip2_licensekey = config['GEOIP']['geoip2_licensekey']
geoip2_proxy = config.get('GEOIP', 'geoip_proxy', fallback=None)
geoip2_enable = config.getboolean('GEOIP', 'geoip2_enable', fallback=False)
except (KeyError, configparser.InterpolationError) as conf_err:
print(f'Error: check config file\n{conf_err}')
sys.exit(1)
MAXUSERS = maxusers if maxusers else 0
THRESHOLD = threshold if threshold else 0
IDLE_BARRIER = idle_barrier if idle_barrier else 0
GEOIP2_ENABLE = geoip2_enable if geoip2_enable else False
REFRESH = refresh if refresh else 1
DEBUG = debug if debug else 0
if CLI_MODE:
import signal
import select
import tty
TTY_SETTINGS = tty.tcgetattr(sys.stdin)
if _WITH_FLASK:
FLASK_OPTIONS = {}
FLASK_OPTIONS['host'] = flask_host
FLASK_OPTIONS['port'] = flask_port
# flask dev mode
if os.getenv("FLASK_DEBUG") == '1' or DEBUG > 1:
FLASK_OPTIONS['debug'] = True
if os.getenv("FLASK_THREADED") == '1':
FLASK_OPTIONS['threaded'] = False
if os.getenv("FLASK_PROXY") == '1':
FLASK_PROXY = True
if _WITH_HTTPD:
HTTPD_OPTIONS = (httpd_host, httpd_port)
if FLASK_PROXY:
from werkzeug.middleware.proxy_fix import ProxyFix
if PYINSTALLER:
FLASK_OPTIONS['debug'] = False
# glftpd data
##############
# shm and struct (default ipc_key: 0x0000dead=57005)
IPC_KEY = ipc_key if ipc_key else "0x0000DEAD"
KEY = int(IPC_KEY, 16)
NULL_CHAR = b'\x00'
if DEBUG > 3:
print(
f'DEBUG:\tIPC_KEY={IPC_KEY} KEY={KEY} sysv_ipc.SHM_RDONLY={sysv_ipc.SHM_RDONLY}\n',
f'\tfmt = {KEY:#010x}', id(KEY)
)
# converted from structonline.h and arranged like struct_ONLINE:
# tag(64s), username(24s), status(h) <...> procid(i)
STRUCT_FORMAT = ' \
64s 24s 256s h 256s 256s i i \
2i \
2i \
2I \
2I \
i \
'
# pylint: disable=invalid-name
struct_ONLINE = collections.namedtuple(
'struct_ONLINE',
'tagline username status ssl_flag host currentdir groupid login_time \
tstart_tv_sec tstart_tv_usec \
txfer_tv_sec txfer_tv_usec \
bytes_xfer1 bytes_xfer2 \
bytes_txfer1 bytes_txfer2 \
procid'
)
# set global var with gl's version
GL_VER = ""
try:
glbin_popt = dict(stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
with subprocess.Popen(f'{glrootpath}/bin/glftpd', **glbin_popt) as glbin_proc:
glbin_out, glbin_err = glbin_proc.communicate()
if not glbin_err:
GL_VER = glbin_out.decode().split('\n', maxsplit=1)[0]
except FileNotFoundError:
pass
# set global var with groupfile contents (/etc/group)
GROUPFILE = None
try:
with open(f'{glrootpath}/etc/group', 'r', encoding='utf-8', errors='ignore') as grp_file:
GROUPFILE = grp_file.readlines()
except IOError:
pass
# set global var for users dir (/ftp-data/users)
USERS_DIR = None
for gl_udir_name in [f"{glrootpath}/ftp-data/users", "/ftp-data/users"]:
if os.path.isdir(gl_udir_name):
USERS_DIR=gl_udir_name
break
# set global var for totalusers, use spy.conf or glftpd.conf's maxusers
glconf_maxusers = 0
for glconf_fn in [f'{glrootpath}/../glftpd.conf', f'{glrootpath}/glftpd.conf', '/etc/glftpd.conf']:
try:
with open(glconf_fn, 'r', encoding='utf-8', errors='ignore') as glconf_obj:
for glconf_line in glconf_obj.readlines():
if re.search(r'^max_users \d', glconf_line):
for mu_cnt in glconf_line.split()[1:]:
glconf_maxusers += int(mu_cnt)
break
except IOError:
pass
TOTALUSERS = 0
if MAXUSERS == -1 and glconf_maxusers > 0:
TOTALUSERS = glconf_maxusers
else:
TOTALUSERS = maxusers
HELP_TEXT = """
Main screen:
Up/Down keys scroll in user list, and PgUp/PgDn/Home/End
Shortcut keys '0-9' will jump to user list <num>
User info:
Press 'v', ENTER or Right to view detailed user info
Use 'n' and 'p' or Left/Right for next/previous login
Use 'k' to kick selected user *needs root*
ESC returns to main screen
Press 'q' key or CTRL-C to Quit
"""
# geoip
########
if _WITH_GEOIP and GEOIP2_ENABLE:
GEOIP2_CLIENT = geoip2.webservice.Client(
geoip2_accountid,
geoip2_licensekey,
host='geolite.info',
proxy=geoip2_proxy if geoip2_proxy not in [None, 'None'] else None
)
# classes
###########
class User:
""" user struct as namedtuple, calc stats """
uploads = UPLOADS
downloads = DOWNLOADS
total = 0
total_up_speed = TOTAL_UP_SPEED
total_dn_speed = TOTAL_DN_SPEED
total_speed = 0
browsers = BROWSERS
idlers = IDLERS
geoip2_client = GEOIP2_CLIENT if GEOIP2_CLIENT else None
geoip2_shown_err = False
tls_mode = [
'None', # no ssl
'Control', # ssl on control
'Both' # ssl on control and data
]
def __init__(self, user_tuple, online=0):
self.user_tuple = user_tuple
self.name = self.get_name()
self.group = self.get_group()
self.online = online
self.mb_xfered = self.get_mb_xfered()
(self.addr, self.ip) = self.get_ip()
def get_name(self) -> str:
""" get username from tuple """
return self.user_tuple.username.split(NULL_CHAR, 1)[0].decode()
def get_group(self) -> str:
""" get group name using gid from tuple """
if self.user_tuple.groupid >= 0:
return get_group(self.user_tuple.groupid)
return ""
def get_ip(self) -> tuple:
""" get adress and ip using host from tuple """
addr = ""
ip = "0.0.0.0"
if self.user_tuple.host != '':
(_, addr) = self.get('host').split('@', 2)[0:2]
# ipv4/6
if (''.join((addr).split('.', 3)).isdigit()) or (':' in addr):
ip = addr
# addr is not a fqdn
elif '.' not in addr:
ip = '127.0.0.1' if addr == 'localhost' else '0.0.0.0'
else:
try:
ip = socket.gethostbyname(addr)
except OSError:
pass
return (addr, ip)
def get_bytes_xfer(self) -> int:
""" bytes_xfer: convert 2 uint32 to uint64 """
return self.get('bytes_xfer2') * pow(2, 32) + self.get('bytes_xfer1')
def get_bytes_txfer(self) -> int:
""" bytes_txfer: convert 2 uint32 to uint64 """
return self.get('bytes_txfer2') * pow(2, 32) + self.get('bytes_txfer1')
def get_mb_xfered(self) -> int:
""" convert tranfered bytes to mb """
return (abs(self.get_bytes_xfer() / 1024 / 1024)) if self.get_bytes_xfer() else 0
def get_traf_dir(self) -> str:
""" traffic direction """
if self.get_bytes_xfer():
if self.get('status')[:4] == 'RETR':
return "Dn"
if self.get('status')[:4] == 'STOR' or self.get('status')[:4] == 'APPE':
return "Up"
def get(self, attr):
""" get attributes from tuple """
r = getattr(self.user_tuple, attr)
if DEBUG > 1:
print(f'DEBUG: user.get() attr={attr} type={type(r)}')
if isinstance(r, bytes):
return r.split(NULL_CHAR, 1)[0].decode()
return r
class Handler(http.server.BaseHTTPRequestHandler):
""" HTTP Requests """
def do_GET(self):
""" GET Method """
head = [
"<!DOCTYPE html><html lang='en'>",
"<head>",
" <title>webspy | http.server</title>",
" <style>",
" html { font-family: 'Courier New', monospace; }",
" </style>",
" <meta http-equiv='Refresh' content='1'>",
"</head>",
"<body>",
]
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
for line in head:
self.wfile.write(bytes(f"{line}\n", "utf-8"))
self.wfile.write(bytes(format_html(), "utf-8"))
self.wfile.write(bytes("\n</body>\n", "utf-8"))
self.wfile.write(bytes("</html>\n", "utf-8"))
class TCPServerThread(threading.Thread):
""" Thread for http server """
def run(self):
""" Start server """
with socketserver.TCPServer((HTTPD_OPTIONS), Handler, False) as httpd:
httpd.allow_reuse_address = True
httpd.server_bind()
httpd.server_activate()
httpd.serve_forever()
class Esc:
""" return ANSI ESC code sequence
https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
'#H': move cursor to home position (0, 0)
'#A': move cursor up <num> lines
'#C': move cursor right <num> columns
'#E': start of next line, <num> lines down
'#F': start of prev line, <num> lines up
'0J': erase from cursor until end of screen
'2J': erase entire screen
"""
@staticmethod
def __new__(cls, code):
return f'\N{ESC}[{code}'
class Style:
""" return escape code sequence or empty string if color is off """
@staticmethod
def __new__(cls, mode):
if color == 0:
return ''
return {
'r': '\x1b[0m', # reset all
'b': '\x1b[1m', # bold
'u': '\x1b[4m', # underline
'bl': '\x1b[5m', # blink
'rb': '\x1b[22m', # reset bold
}[mode]
class Color:
""" return color code or empty string if color is off """
""" use UPPERCASE code for bright colors 0-7 """
map = {
'k': 0, # black
'r': 1, # red
'g': 2, # green
'y': 3, # yellow
'b': 4, # blue
'm': 5, # magenta
'c': 6, # cyan
'w': 7, # white
'd': 9, # default
}
@staticmethod
def __new__(cls, code):
if color == 0:
return ''
fg = bg = 'd'
try:
(fg, bg) = code.split(',')
except ValueError:
pass
return "{0}{1};{2}m".format(
'\x1b[',
f'{Color.map[fg] + 30}' if fg.islower() else f'{Color.map[fg.lower()] + 90}',
f'{Color.map[bg] + 40}' if bg.islower() else f'{Color.map[bg.lower()] + 100}'
)
class Theme:
""" theme for cli mode """
# unicode line drawing chars:
draw = {
'h': '\U00002500', # horizontal -
'v': '\U00002502', # vertical |
'ul': '\U0000250C', # up left
'ur': '\U00002510', # up right
'dl': '\U00002514', # down left |_
'dr': '\U00002518', # down right _|
'vl': '\U00002524', # vertical left -|
'vr': '\U0000251C' # vertical right |-
}
maincolor = f"{Style('b')}{Color('k,d')}" # gray/black
logocolor = f"{Style('b')}{Color('m,d')}" # magenta
delimiter = f"{maincolor}|{Style('r')}" # pipe '|'
vchar = f"{maincolor}{draw['v']}{Style('r')}"
def __init__(self):
self.columns = self.get_columns()
self.lines = self.get_lines()
self.max_col = self.columns - 11
self.fill = " " * self.columns if self.columns > 80 else ""
self.separator = self.fmt_separator()
self.header = self.fmt_header()
self.footer = self.fmt_footer()
self.spacer = self.fmt_spacer()
def get_columns(self) -> int:
""" get term width """
return os.get_terminal_size().columns if os.get_terminal_size().columns > 1 else 80
def get_lines(self) -> int:
""" get term height """
return os.get_terminal_size().lines if os.get_terminal_size().lines > 1 else 25
def fmt_separator(self) -> str:
""" format separator """
# '|-----------------|'
draw = self.draw
return "{mc}{vr}{hline}{vl}{rst}".format(
mc=self.maincolor, vr=draw['vr'], hline=draw['h']*(self.columns-9),
vl=draw['vl'], rst=Style('r')
)
def fmt_header(self) -> str:
""" format header """
draw = self.draw
return "{mc}{ul}{hline}{ur}{rst}".format(
mc=self.maincolor, ul=draw['ul'], ur=draw['ur'],hline=draw['h']*(self.columns-9), rst=Style('r')
)
def fmt_footer(self) -> str:
""" format footer """
draw = self.draw
return "{mc}{dl}{hline}[{lc}PY-SPY{mc}]{h}{h}{h}{dr}{rst}".format(
mc=self.maincolor, dl=draw['dl'], hline=draw['h']*(self.columns-20),
lc=self.logocolor, h=draw['h'], dr=draw['dr'], rst=Style('r')
)
def fmt_spacer(self) -> str:
""" format spacer """
# '| <~~~ space ~~~> |'
return "{v} {sp:<{col}.{col}} {v}".format(v=self.vchar, sp=' ', col=self.max_col)
def fmt_uinfo_title(self, text, col) -> str:
""" format title on userinfo screen """
return "{v} {text:<{col}.{col}} {v}".format(
v=self.vchar, text=f"{Style('u')}{text}{Style('r')}", col=col
)
def fmt_uinfo_line(self, text, col) -> str:
""" format line on userinfo screen """
return "{v} {sp:>4.4}{text:<{col}.{col}} {v}".format(v=self.vchar, sp=' ', text=text, col=col)
# functions
############
def get_group(gid) -> str:
""" get group name using gid """
if GROUPFILE:
for line in GROUPFILE:
if line.split(':')[2] == str(gid):
return line.split(':')[0]
return ""
def get_gid(g_name) -> int:
""" get group id using name """
if GROUPFILE:
for line in GROUPFILE:
if line.split(':')[0] == g_name:
return line.split(':')[2]
return 0
def get_idle(seconds) -> str:
""" calc idle time """
return time.strftime("%H:%M:%S", time.gmtime(seconds))
def get_filesize(filename) -> int:
""" get filesize in bytes """
for file in filename, f'{glrootpath}{filename}':
try:
return os.path.getsize(file)
except OSError:
pass
return 0
def get_geocode(client, userip, shown_err) -> list:
""" get geoip2 country code for ip """
iso_code = "xX"
theme = Theme()
if DEBUG > 0:
for prefix in ['127.', '10.', '172.16.1', '172.16.2', '172.16.3', '192.168.']:
if userip.startswith(prefix):
if DEBUG > 3:
print(f'DEBUG: geoip2 MATCH "{prefix}" in "{userip}"')
return [client, 'DEBUG', shown_err]
if GEOIP2_BUF.get(userip):
iso_code = GEOIP2_BUF[userip]
else:
try:
if DEBUG == 1:
print(f'DEBUG: geoip2 "{userip}" not cached, GEOIP2_BUF="{GEOIP2_BUF}"')
iso_code = client.country(userip).country.iso_code
GEOIP2_BUF[userip] = iso_code
except geoip2.errors.GeoIP2Error as err:
# var shown_err makes sure we only show the error once
if (err.__class__.__name__ in ['AddressNotFoundError', 'OutOfQueriesError']) and shown_err == 0:
shown_err = 1
text = f"{Color('r,k')}Error: geoip2 {err.__class__.__name__} {err}"
mcol = theme.max_col+8 if color else theme.max_col-8
if len(text) + 8 > mcol:
text = text[:73] + ' ...'
print("{0} {1:<{2}.{2}} {0}".format(Theme.vchar, text, mcol))
print(theme.footer)
print(f"{Esc('2F')}", end="")
time.sleep(2.5)
return [client, iso_code, shown_err]
def conv_speed(speed) -> list:
""" convert and format speed """
if speed > (THRESHOLD * THRESHOLD):
return [float('{:7.2f}'.format(speed / 1024**2)), 'GiB/s']
if speed > THRESHOLD:
return [float('{:7.1f}'.format(speed / 1024)), 'MiB/s']
return [float('{:7.0f}'.format(speed)), 'KiB/s']
def cli_stty_sane():
""" default terminal settings, turn on echo """
# restore orig tty settings (termios)
tty.tcsetattr(sys.stdin, tty.TCSANOW, TTY_SETTINGS)
def cli_input(user_action, cli_refresh=0.5):
""" read user input from stdin """
# put terminal in mode 'cbreak' or 'raw' (no CR, disables ISIG)
# tty.setraw(), tty.setcbreak()
# https://github.com/python/cpython/blob/3.7/Lib/tty.py
screen_redraw = 0
key = ""
tty.setraw(sys.stdin.fileno())
if select.select([sys.stdin], [], [], cli_refresh) == ([sys.stdin], [], []):
key = sys.stdin.buffer.raw.read(3).decode(sys.stdin.encoding)
if key[:2].strip().isdigit():
user_action = 1
# [vV], ENTER, SPACE, RIGHT
elif ((key in ['v', 'V', '\r', '\x13', '\N{SPACE}']) or
(user_action == 0 and key == '\N{ESC}[C')):
key = 1
user_action = 1
elif key in ['k', 'K']:
user_action = 2
screen_redraw = 1
elif key in ['h', 'H', '?']:
user_action = 3
screen_redraw = 1
# [nN], RIGHT
elif key in ['n', 'N', '\N{ESC}[C']:
user_action = 4
# [pP], LEFT
elif key in ['p', 'P', '\N{ESC}[D']:
user_action = 5
elif CLI_SEARCH and key == '/':
user_action = 7
# UP
elif key == '\N{ESC}[A':
user_action = 10
# DOWN
elif key == '\N{ESC}[B':
user_action = 11
elif key in ['q', 'Q']:
user_action = 9
# HOME
elif key == '\N{ESC}[1':
user_action = 12
# END
elif key == '\N{ESC}[4':
user_action = 13
# PGUP
elif key == '\N{ESC}[5':
user_action = 14
# PGDN
elif key == '\N{ESC}[6':
user_action = 15
# ESC
elif key == '\N{ESC}':
if user_action == 0:
user_action = 9
elif user_action == 1:
user_action = 6
# CTRL-C
elif key == '\x03':
cli_sigint_handler(any, any)
tty.tcsetattr(sys.stdin, tty.TCSANOW, TTY_SETTINGS)
return dict(key=key, user_action=user_action, screen_redraw=screen_redraw)
def cli_dialog(title, text):
""" show dialog box """
theme = Theme()
draw = Theme.draw
upos = str(theme.lines - 4)
lpos = str(int(theme.columns / 10)-3)
rpad = 10
width = 72 - rpad
tpad = f"{' '*int(width/2-8)}" # pad title
print(f"{Esc(upos+'A')}{Color('k,w')}")
print(f"{Esc(lpos+'C')}{draw['ul']}{draw['h']*(width)}{draw['ur']}")
print(f"{Esc(lpos+'C')}{draw['v']}{' '*width}{draw['v']}")
print(f"{Esc(lpos+'C')}{draw['v']} {tpad}{Color('k,c')} {title} {Color('k,w')}{' '*(width-(len(tpad)+12))}{draw['v']}")
for line in text.splitlines():
print(f"{Esc(lpos+'C')}{draw['v']}{line}{' '*(width-len(line))}{draw['v']}")
print(f"{Esc(lpos+'C')}{draw['dl']}{draw['h']*(width)}{draw['dr']}")
print(f"{Style('r')}")
def cli_user_info(users, u_idx, user_cache=[]) -> list:
""" show formatted user details from login and userfile """
theme = Theme()
mcol_title = theme.max_col+8 if color else theme.max_col
i = 0
print(theme.header)
if theme.lines > 25:
print(theme.spacer)
i += 1
print(theme.fmt_uinfo_title('Login', mcol_title))
print(theme.spacer)
if users[u_idx].get('procid'):
ssl_flag = users[u_idx].get('ssl_flag')
ssl_msg = User.tls_mode[ssl_flag] if ssl_flag in range(0, len(User.tls_mode)) else 'UNKNOWN'
ip = users[u_idx].ip
iso_code = None
try:
iso_code = users[u_idx].iso_code
except AttributeError:
if u_idx in range(0, len(user_cache)) and users[u_idx].get('procid') == user_cache[u_idx].get('procid'):
try:
iso_code = user_cache[u_idx].iso_code
except AttributeError:
pass
last_dl = '{:.1f}GB'.format(round(int(users[u_idx].get_bytes_txfer()) / 1024**3, 1))
login_info = [
f"Username: '{Style('b')}{users[u_idx].name}{Style('r')}' [{u_idx}/{len(users)-1}]",
f"PID: {users[u_idx].get('procid')} SSL: {ssl_msg}",
f"RHost: {users[u_idx].get('host')}",
f"IP: {ip}{' ' + iso_code if iso_code else ''}",
f"Tagline: {users[u_idx].get('tagline')}",
f"Currentdir: {users[u_idx].get('currentdir')}",
f"Status: {users[u_idx].get('status')}",
f"Last DL: {last_dl}",
" "
]
mcol = theme.max_col+4 if color else theme.max_col-4
for l in login_info:
print(theme.fmt_uinfo_line(l, mcol))
mcol = theme.max_col-4
print(theme.separator)
if theme.lines > 25:
print(theme.spacer)
i += 1
r = get_userfile(users[u_idx].name)
if r.get('status') == "Success":
print(theme.fmt_uinfo_title('Userfile', mcol_title))
print(theme.spacer)
for key, val in r.get('result').items():
text = f"{key}: {val}"
if len(text) + 8 > theme.max_col:
text = text[:61] + ' ...'
print(theme.fmt_uinfo_line(text, theme.max_col-4))
else:
text = f"User '{users[u_idx].name}' not found..."
print("{0} {1:<{2}.{2}} {0}".format(Theme.vchar, text, theme.max_col))
time.sleep(2)
while 23 + i < theme.lines:
print(theme.spacer)
i += 1
print(theme.footer)
def cli_uinfo_prompt(p_cnt):
""" show prompt on userinfo screen """
rpad = ' ' * (3 - p_cnt)
progress = '{0:{fill}<{width}}'.format('', fill='.', width=int(p_cnt))
prompt = f"> Press {Color('k,w')}n{Style('r')} for next login, " \
f"{Color('k,w')}p{Style('r')} previous, " \
f"{Color('k,w')}k{Style('r')} kill or " \
f"{Color('k,w')}ESC{Style('r')} to go back"
print("")
print(f"{prompt}{progress}{rpad}", end="")
print(f"{Esc('1F')}", end="")
def cli_action(user_action, key, screen_redraw, user_scroll, search_user):
""" get user input and run action """
theme = Theme()
# action: userinfo
if user_action == 1:
users = []
for user in get_users():
users.append(set_stats(user))
u_idx = p_cnt = 0
# set u_idx from shortcut keys 0-9, search_user or user_scroll
if isinstance(key, str) and key.isdigit() and int(key) in range(0, len(users)):
u_idx = int(key)
elif CLI_SEARCH:
i = 0
for user in users:
if search_user in user.name and i in range(0, len(users)):
u_idx = i
break
i += 1
if user_scroll:
u_idx += user_scroll
print(f"{Esc('2J')}{Esc('H')}", end="")
# show user details and wait for user input
cli_user_info(users, u_idx)
input_result = kill_result = None
while True:
input_result = cli_input(user_action, 0.3)
# uinfo: kill
if input_result.get('user_action') == 2:
if u_idx in range(0, len(users)):
u_name = users[int(u_idx)].get('username')
kill_result = kill_procid(u_name, users)
if kill_result.get('status') == "Success":
print("{0:<{1}.{1}}".format(f"{kill_result.get('status')}: Killed PID '{kill_result.get('procid')}' ...", theme.columns))
time.sleep(2)
else:
print("{0:<{1}.{1}}".format(f"{kill_result.get('error')}", theme.columns))
time.sleep(3)
print(f"{' ':<{theme.columns}}")
print(f"{Esc('2J')}{Esc('H')}", end="")
break
# uinfo: next
if input_result.get('user_action') == 4:
u_idx = u_idx + 1 if (u_idx + 1 < len(users)) else 0
print(f"{Esc('2J')}{Esc('H')}", end="")
cli_user_info(get_users(), u_idx, users)
# uinfo: prev
elif input_result.get('user_action') == 5:
u_idx = u_idx-1 if (u_idx-1 < len(users) and u_idx > 0) else 0
print(f"{Esc('2J')}{Esc('H')}", end="")
cli_user_info(get_users(), u_idx, users)
# uinfo: help
if input_result.get('user_action') == 3:
cli_dialog("Help", HELP_TEXT)
while not cli_input(user_action).get('key'):
time.sleep(0.1)
print(f"{Esc('2J')}{Esc('H')}", end="")
cli_user_info(get_users(), u_idx, users)
# uinfo: back (ESC), quit
if input_result.get('user_action') in [6, 9]:
break
p_cnt = 0 if p_cnt > 3 else p_cnt
cli_uinfo_prompt(p_cnt)
p_cnt += 1
tty.tcsetattr(sys.stdin, tty.TCSANOW, TTY_SETTINGS)
user_action = input_result.get('user_action')
screen_redraw = 1
# action: show help popup
elif user_action == 3:
cli_dialog("Help", HELP_TEXT)
while not cli_input(user_action, 0.3).get('key'):
time.sleep(0.1)
user_action = 0
screen_redraw = 1
# action: search user
elif CLI_SEARCH and user_action == 7:
prompt = f"> Search for username [ENTER]: {Style('bl')}_{Style('r')}"
print("{0:<{1}.{1}}".format(prompt, theme.columns))
stdin_string = ""
c = 0
while True:
user_input = sys.stdin.read(1)
# backspace
if user_input in [ '\b', '\x08', '\x7f' ]:
c -= 1 if c > 0 else 0
stdin_string = stdin_string[:-1]
print(f"{Esc('1A')}{Esc(str(c+len(prompt)-9)+'C')} ")
# ENTER
elif user_input == '\n':
break
else:
stdin_string += user_input
print(f"{Esc('1A')}{Esc(str(c+len(prompt)-9)+'C')}{user_input}")
c += 1
search_user = stdin_string
user_action = 0
# action: quit (ESC)
elif user_action == 9:
cli_sigint_handler(any, any)
sys.exit(0)
# action: scroll user list up (-1)
elif user_action == 10:
user_action = 0
screen_redraw = 1
user_scroll -= 1
# action: scroll user list down (+1)
elif user_action == 11:
user_action = 0
screen_redraw = 1
user_scroll += 1
# action: scroll to user list end (0)
elif user_action == 12:
user_action = 0
screen_redraw = 1
user_scroll = 0
# action: scroll to user list end
elif user_action == 13:
user_action = 0
screen_redraw = 1
user_scroll = len(get_users())-1
# action: scroll user list page up (-5)
elif user_action == 14:
user_action = 0
screen_redraw = 1
user_scroll -= 5
# action: scroll user list page down (+5)
elif user_action == 15:
user_action = 0
screen_redraw = 1
user_scroll += 5
# handle any other key presses
elif user_action == 0 and len(key) > 0:
screen_redraw = 1
text = "Invalid option ... press 'h' for help"
print("{0:>2.2}{1:<{2}}".format(
' ', f"{Style('b')}{Color('r,k')}{text}{Style('r')}", theme.columns)
)
print(f"{Esc('2F')}", end="")
time.sleep(1)
else:
user_action = 0
screen_redraw = 1
return [user_action, screen_redraw, user_scroll, search_user]
def get_userfile(u_name) -> dict:
""" get fields from userfile """
userfile_text = ""
try:
with open(f'{USERS_DIR}/{u_name}', 'r', encoding='utf-8', errors='ignore') as userfile:
userfile_text = userfile.readlines()
except FileNotFoundError:
return dict(status="FileNotFound")
if userfile_text == "":
return dict(status="UserNotFound")
u_fields = {}
value = []
prev = ""
for line in userfile_text:
for key in ['FLAGS', 'CREDITS', 'GROUP', 'IP']:
if key in line:
if key != prev:
value = []
if line.startswith('CREDITS'):
value = 0
try:
c = int((re.sub(r'^CREDITS [^0](\d+).*', r'\1', line)).strip())
except ValueError:
pass
if isinstance(c, int) and c > 0:
value = f"{round(int(c) / 1024**2)}GB"
else:
value.append(line.strip().split(' ')[1])
u_fields[key] = value
prev = key
return dict(status="Success", result=u_fields)
def kill_procid(u_name, users):
""" kill user using procid """
for user in users:
if user.name == u_name:
#if os.popen(f"ps --no-headers -o comm -p {user.get('procid')}").read().strip() == 'glftpd':
with open(os.path.join('/proc', str(user.get('procid')), 'comm'), 'rb') as fp:
if fp.read().split(b'\x00')[0].decode().strip() == 'glftpd':
try:
procid = user.get('procid')
os.kill(int(procid), 15)
return dict(status="Success", procid=procid)
except OSError as err:
return dict(status="NotRoot", procid=procid, error=err)
return dict(status="NotFound")