add captcha to memload()
This commit is contained in:
parent
4cf9810755
commit
143bfd80bc
@ -68,22 +68,46 @@ def check_permissions():
|
|||||||
def memload():
|
def memload():
|
||||||
"""
|
"""
|
||||||
"""
|
"""
|
||||||
|
from random import random
|
||||||
|
r = str(random())[2:8]
|
||||||
|
hi = 'Enter the numbers {} to confirm that you are not a robot: '.format(r)
|
||||||
|
try:
|
||||||
|
t0 = monotonic()
|
||||||
|
inp = input(hi)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
print('KeyboardInterrupt\nExit')
|
||||||
|
exit(1)
|
||||||
|
try:
|
||||||
|
os.setreuid(1, 1)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
if inp != r:
|
||||||
|
print('Captcha is not passed ("{}" != "{}")'.format(inp, r))
|
||||||
|
print('memload() is not for robots\nExit')
|
||||||
|
exit(1)
|
||||||
|
if monotonic() - t0 > 30:
|
||||||
|
print('Captcha is not passed (timeout expired)')
|
||||||
|
print('memload() is not for robots\nExit')
|
||||||
|
exit(1)
|
||||||
|
else:
|
||||||
|
print('-' * 68)
|
||||||
with open('/proc/meminfo') as f:
|
with open('/proc/meminfo') as f:
|
||||||
mem_list = f.readlines()
|
mem_list = f.readlines()
|
||||||
mem_list_names = []
|
mem_list_names = []
|
||||||
for s in mem_list:
|
for s in mem_list:
|
||||||
mem_list_names.append(s.split(':')[0])
|
mem_list_names.append(s.split(':')[0])
|
||||||
if mem_list_names[2] != 'MemAvailable':
|
try:
|
||||||
errprint('Your Linux kernel is too old, Linux 3.14+ requied\nExit')
|
mem_available_index = mem_list_names.index('MemAvailable')
|
||||||
exit(1)
|
except ValueError:
|
||||||
|
print('Your Linux kernel is too old, Linux 3.14+ required\nExit')
|
||||||
swap_total_index = mem_list_names.index('SwapTotal')
|
swap_total_index = mem_list_names.index('SwapTotal')
|
||||||
swap_free_index = swap_total_index + 1
|
swap_free_index = mem_list_names.index('SwapFree')
|
||||||
|
|
||||||
def check_mem_and_swap():
|
def check_mem_and_swap():
|
||||||
"""find mem_available, swap_total, swap_free"""
|
"""find mem_available, swap_total, swap_free"""
|
||||||
with open('/proc/meminfo') as f:
|
with open('/proc/meminfo') as f:
|
||||||
for n, line in enumerate(f):
|
for n, line in enumerate(f):
|
||||||
if n == 2:
|
if n == mem_available_index:
|
||||||
mem_available = int(line.split(':')[1][:-4])
|
mem_available = int(line.split(':')[1][:-4])
|
||||||
continue
|
continue
|
||||||
if n == swap_total_index:
|
if n == swap_total_index:
|
||||||
@ -99,27 +123,13 @@ def memload():
|
|||||||
' '.format(
|
' '.format(
|
||||||
round(mem_available / 1024),
|
round(mem_available / 1024),
|
||||||
round(swap_free / 1024)))
|
round(swap_free / 1024)))
|
||||||
try:
|
|
||||||
luid_init = rline1('/proc/1/loginuid')
|
|
||||||
except Exception as e:
|
|
||||||
print(e)
|
|
||||||
exit(1)
|
|
||||||
luid_self = rline1('/proc/self/loginuid')
|
|
||||||
if luid_init == luid_self:
|
|
||||||
print('The option is available only for logged in users.')
|
|
||||||
print('Self loginuid: {}'.format(luid_self))
|
|
||||||
print('Init loginuid: {}'.format(luid_init))
|
|
||||||
print('Self login UID must not be equal to init login UID to continue.'
|
|
||||||
)
|
|
||||||
print('Exit')
|
|
||||||
exit(1)
|
|
||||||
|
|
||||||
|
hi = 'Warning! The process will consume memory until 40 MiB of mem' \
|
||||||
|
'ory\n(MemAvailable + SwapFree) remain free, and it will be t' \
|
||||||
|
'erminated via SIGUSR1\nat the end. This may cause the system' \
|
||||||
|
' to freeze and processes to terminate.\nDo you want to conti' \
|
||||||
|
'nue? [No/Yes] '
|
||||||
try:
|
try:
|
||||||
hi = 'Warning! The process will consume memory until 40 MiB of mem' \
|
|
||||||
'ory\n(MemAvailable + SwapFree) remain free, and it will be t' \
|
|
||||||
'erminated via SIGUSR1\nat the end. This may cause the system' \
|
|
||||||
' to freeze and processes to terminate.\nDo you want to conti' \
|
|
||||||
'nue? [No/Yes] '
|
|
||||||
inp = input(hi)
|
inp = input(hi)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print('KeyboardInterrupt\nExit')
|
print('KeyboardInterrupt\nExit')
|
||||||
@ -143,7 +153,7 @@ def memload():
|
|||||||
print('Self terminating by SIGUSR1')
|
print('Self terminating by SIGUSR1')
|
||||||
os.kill(self_pid, SIGUSR1)
|
os.kill(self_pid, SIGUSR1)
|
||||||
else:
|
else:
|
||||||
ex.append(bytearray(1024 * 50)) # step size is 50 KiB
|
ex.append(bytearray(1024 * 40)) # step size is 40 KiB
|
||||||
u = monotonic() - z
|
u = monotonic() - z
|
||||||
if u <= 0.01:
|
if u <= 0.01:
|
||||||
continue
|
continue
|
||||||
|
Loading…
Reference in New Issue
Block a user