rand vnc port from 7000 to 7999, deamonizing websockets and avoid zombies

This commit is contained in:
deflax 2018-03-10 04:40:58 +02:00
parent 52828f6ca6
commit 363553bb7d
7 changed files with 125 additions and 16 deletions

View file

@ -69,7 +69,7 @@ def phyidgen(slave_name, unit_type):
if str(unit_type) == 'lxc': if str(unit_type) == 'lxc':
full_list = list(range(2000,2999)) full_list = list(range(2000,2999))
if str(unit_type) == 'kvm': if str(unit_type) == 'kvm':
full_list = list(range(100,150)) full_list = list(range(101,150))
exclude_list = [] exclude_list = []
directory = 'db/' directory = 'db/'
for dbfile in os.listdir(directory): for dbfile in os.listdir(directory):

View file

@ -19,9 +19,12 @@ def spawn(target, options):
a_options += c_option a_options += c_option
try: try:
command_line = 'python3 runwebsockify.py ' + a_options + vnctarget command_line = 'python3 runwebsockify.py -D ' + a_options + vnctarget
args = shlex.split(command_line) args = shlex.split(command_line)
p = subprocess.Popen(args) #print(str(args))
print('spawned!') p = subprocess.Popen(args, shell=False)
print('vnc connector API:{} <--> {}:{} spawned !'.format(target['listen_port'], target['target_host'], target['target_port']))
p.wait()
print('done.')
except: except:
raise raise

View file

@ -41,8 +41,8 @@ def create(json):
except Exception as e: except Exception as e:
ioconfig.logger.warning('grid> slave not predefined. I will query for a capable one.') ioconfig.logger.warning('grid> slave not predefined. I will query for a capable one.')
#slave_name = str(grid.query_happiness(region_id, weight)) #slave_name = str(grid.query_happiness(region_id, weight))
slave_name = 'warrior' #slave_name = 'warrior'
#slave_name = 'lexx' slave_name = 'lexx'
ioconfig.logger.info('{}> slave selected'.format(slave_name)) ioconfig.logger.info('{}> slave selected'.format(slave_name))
proxobject = auth(slave_name) proxobject = auth(slave_name)
real_slave_name = proxobject.cluster.status.get()[0]['name'] real_slave_name = proxobject.cluster.status.get()[0]['name']
@ -322,10 +322,11 @@ def vmvnc(json):
slaveip = ioconfig.parser.get(str(slave_name), 'ipv4') slaveip = ioconfig.parser.get(str(slave_name), 'ipv4')
#slaveport = socket['port'] #slaveport = socket['port']
slaveport = ticket['port'] slaveport = ticket['port']
slave_id = 1 #TODO: fix this
vnchost = ioconfig.parser.get('general', 'novnc_host') vnchost = ioconfig.parser.get('general', 'novnc_host')
listenport = str(int(slaveport) + 1000 + (int(slave_id) * 100)) #TODO: max 100 parallel connections/slave. listenport = random.randint(7000, 9999)
#listenport = random.randint(7000, 7001)
vnc_target = { 'target_host': slaveip, vnc_target = { 'target_host': slaveip,
'target_port': slaveport, 'target_port': slaveport,
@ -342,11 +343,11 @@ def vmvnc(json):
novnc.spawn(vnc_target, vnc_options) novnc.spawn(vnc_target, vnc_options)
external_url = ioconfig.parser.get('general', 'novnc_url') external_url = ioconfig.parser.get('general', 'novnc_url')
prefix = external_url + "?host=" + vnchost + "&port=" + listenport + "&view_only=false&encrypt=1&true_color=1&password=" prefix = external_url + "?host=" + vnchost + "&port=" + str(listenport) + "&view_only=false&encrypt=1&true_color=1&password="
vnc_url = prefix + ticket['ticket'] vnc_url = prefix + ticket['ticket']
time.sleep(3) #wait few seconds for the parallel vncwebsocket time.sleep(3) #wait few seconds for the parallel vncwebsocket
ioconfig.logger.info('{}[{}]> vnc port {} ready'.format(vm_owner, slave_name, listenport)) ioconfig.logger.info('{}[{}]> vnc port {} ready'.format(vm_owner, slave_name, str(listenport)))
#response = { 'status':'VNC', 'fqdn':external_url, 'host':myip, 'port':listenport, 'encrypt':'0', 'true_color':'1', 'ticket':ticket['ticket'] } #response = { 'status':'VNC', 'fqdn':external_url, 'host':myip, 'port':listenport, 'encrypt':'0', 'true_color':'1', 'ticket':ticket['ticket'] }
response = { 'status':'VNC', 'url':vnc_url } response = { 'status':'VNC', 'url':vnc_url }
#print(vnc_url) #print(vnc_url)

19
proxmaster.service Normal file
View file

@ -0,0 +1,19 @@
[Unit]
Description=Proxmaster
After=network.target
After=systemd-user-sessions.service
After=network-online.target
[Service]
User=master
Type=forking
ExecStart=/usr/bin/screen -dmS proxmaster /home/master/proxmaster/start.sh
#ExecStop=
TimeoutSec=30
Restart=on-failure
RestartSec=30
StartLimitInterval=350
StartLimitBurst=10
[Install]
WantedBy=multi-user.target

View file

@ -1,10 +1,10 @@
#!/bin/bash #!/bin/bash
# Log rotation # Log rotation
LOG_DIR=${HOME}/proxmaster/log DIR=${HOME}/proxmaster
LOG_FILE="${LOG_DIR}/proxmaster.log" LOG_FILE="${DIR}/log/proxmaster.log"
mkdir -p $LOG_DIR mkdir -p $DIR/log
TIME=`date -u +%s` TIME=`date -u +%s`
@ -14,6 +14,5 @@ else
touch ${LOG_FILE} touch ${LOG_FILE}
fi fi
cd ${LOG_DIR}/..
#startuwsgi instance #startuwsgi instance
uwsgi config.ini uwsgi --logdate --ini config.ini

View file

@ -2,7 +2,7 @@
#makes jsons human (and machine) readable #makes jsons human (and machine) readable
for filename in db/*.json ; do for filename in ../db/*.json ; do
echo $filename; echo $filename;
python3 -m json.tool $filename python3 -m json.tool $filename
read; read;

87
tools/netstat.py Normal file
View file

@ -0,0 +1,87 @@
#!/usr/bin/python
import pwd
import os
import re
import glob
PROC_TCP = "/proc/net/tcp"
STATE = {
'01':'ESTABLISHED',
'02':'SYN_SENT',
'03':'SYN_RECV',
'04':'FIN_WAIT1',
'05':'FIN_WAIT2',
'06':'TIME_WAIT',
'07':'CLOSE',
'08':'CLOSE_WAIT',
'09':'LAST_ACK',
'0A':'LISTEN',
'0B':'CLOSING'
}
def _load():
''' Read the table of tcp connections & remove header '''
with open(PROC_TCP,'r') as f:
content = f.readlines()
content.pop(0)
return content
def _hex2dec(s):
return str(int(s,16))
def _ip(s):
ip = [(_hex2dec(s[6:8])),(_hex2dec(s[4:6])),(_hex2dec(s[2:4])),(_hex2dec(s[0:2]))]
return '.'.join(ip)
def _remove_empty(array):
return [x for x in array if x !='']
def _convert_ip_port(array):
host,port = array.split(':')
return _ip(host),_hex2dec(port)
def netstat():
'''
Function to return a list with status of tcp connections at linux systems
To get pid of all network process running on system, you must run this script
as superuser
'''
content=_load()
result = []
for line in content:
line_array = _remove_empty(line.split(' ')) # Split lines and remove empty spaces.
l_host,l_port = _convert_ip_port(line_array[1]) # Convert ipaddress and port from hex to decimal.
r_host,r_port = _convert_ip_port(line_array[2])
tcp_id = line_array[0]
state = STATE[line_array[3]]
uid = pwd.getpwuid(int(line_array[7]))[0] # Get user from UID.
inode = line_array[9] # Need the inode to get process pid.
pid = _get_pid_of_inode(inode) # Get pid prom inode.
try: # try read the process name.
exe = os.readlink('/proc/'+pid+'/exe')
except:
exe = None
nline = [tcp_id, uid, l_host+':'+l_port, r_host+':'+r_port, state, pid, exe]
result.append(nline)
return result
def _get_pid_of_inode(inode):
'''
To retrieve the process pid, check every running process and look for one using
the given inode.
'''
for item in glob.glob('/proc/[0-9]*/fd/[0-9]*'):
try:
if re.search(inode,os.readlink(item)):
return item.split('/')[2]
except:
pass
return None
if __name__ == '__main__':
for conn in netstat():
print conn