2022-06-15 09:11:27 +02:00
#!/usr/bin/env python3
2023-04-12 11:04:37 +02:00
import argparse , sys , platform , os , multiprocessing , subprocess , getpass
2022-06-15 09:11:27 +02:00
2022-06-19 20:11:28 +02:00
pref = " \033 [ "
reset = f " { pref } 0m "
class colors :
black = " 30m "
red = " 31m "
green = " 32m "
yellow = " 33m "
blue = " 34m "
magenta = " 35m "
cyan = " 36m "
white = " 37m "
def puts ( text , * args , color = colors . white , is_bold = False , * * kwargs ) :
print ( f ' { pref } { 1 if is_bold else 0 } ; { color } ' + text + reset , * args , * * kwargs )
def sep ( ) : puts ( " ----------------------------------- " , is_bold = True )
2022-08-09 10:28:30 +00:00
2023-04-11 21:15:30 +02:00
def check_if_exists ( program ) :
return subprocess . call ( [ ' sh ' , ' -c ' , program ] , stdout = subprocess . DEVNULL , stderr = subprocess . STDOUT ) == 0
def composecmd ( cmd , composefile = None ) :
if composefile :
cmd = f " -f { composefile } { cmd } "
if not check_if_exists ( " docker ps " ) :
return puts ( " Cannot use docker, the user hasn ' t the permission or docker isn ' t running " , color = colors . red )
elif check_if_exists ( " docker compose " ) :
return os . system ( f " docker compose { cmd } " )
elif check_if_exists ( " docker-compose " ) :
return os . system ( f " docker-compose { cmd } " )
else :
puts ( " Docker compose not found! please install docker compose! " , color = colors . red )
2022-08-23 21:56:33 +00:00
def dockercmd ( cmd ) :
2023-04-11 21:15:30 +02:00
if check_if_exists ( " docker " ) :
return os . system ( f " docker { cmd } " )
elif not check_if_exists ( " docker ps " ) :
puts ( " Cannot use docker, the user hasn ' t the permission or docker isn ' t running " , color = colors . red )
else :
puts ( " Docker not found! please install docker! " , color = colors . red )
def run_checks ( ) :
if not check_if_exists ( " docker " ) :
puts ( " Docker not found! please install docker and docker compose! " , color = colors . red )
exit ( )
elif not check_if_exists ( " docker-compose " ) and not check_if_exists ( " docker compose " ) :
print ( check_if_exists ( " docker-compose " ) , check_if_exists ( " docker compose " ) )
puts ( " Docker compose not found! please install docker compose! " , color = colors . red )
exit ( )
if not check_if_exists ( " docker ps " ) :
puts ( " Cannot use docker, the user hasn ' t the permission or docker isn ' t running " , color = colors . red )
exit ( )
2022-08-23 21:56:33 +00:00
2023-04-12 12:05:03 +02:00
2022-06-15 09:11:27 +02:00
parser = argparse . ArgumentParser ( )
2022-06-19 20:11:28 +02:00
parser . add_argument ( ' --port ' , " -p " , type = int , required = False , help = ' Port where open the web service of the firewall ' , default = 4444 )
2022-08-09 10:28:30 +00:00
parser . add_argument ( ' --threads ' , " -t " , type = int , required = False , help = ' Number of threads started for each service/utility ' , default = - 1 )
parser . add_argument ( ' --no-autostart ' , " -n " , required = False , action = " store_true " , help = ' Save docker-compose file and not start the container ' , default = False )
2023-04-11 22:15:17 +02:00
parser . add_argument ( ' --build ' , " -b " , required = False , action = " store_true " , help = ' Build the container locally ' , default = False )
2023-04-11 21:15:30 +02:00
parser . add_argument ( ' --keep ' , ' -k ' , required = False , action = " store_true " , help = ' Keep the firegex-compose.yml file generated ' , default = False )
2022-08-09 10:28:30 +00:00
parser . add_argument ( ' --stop ' , ' -s ' , required = False , action = " store_true " , help = ' Stop firegex execution ' , default = False )
2022-08-10 10:23:37 +00:00
parser . add_argument ( ' --restart ' , ' -r ' , required = False , action = " store_true " , help = ' Restart firegex ' , default = False )
2022-08-09 10:28:30 +00:00
parser . add_argument ( ' --psw-no-interactive ' , type = str , required = False , help = ' Password for no-interactive mode ' , default = None )
2022-08-10 10:23:37 +00:00
parser . add_argument ( ' --startup-psw ' , ' -P ' , required = False , action = " store_true " , help = ' Insert password in the startup screen of firegex ' , default = False )
2022-06-26 13:29:54 +02:00
2022-06-15 09:11:27 +02:00
args = parser . parse_args ( )
2022-08-09 10:28:30 +00:00
os . chdir ( os . path . dirname ( os . path . realpath ( __file__ ) ) )
2023-04-11 21:15:30 +02:00
run_checks ( )
2022-08-10 10:23:37 +00:00
start_operation = not ( args . stop or args . restart )
2023-04-11 22:15:17 +02:00
if args . build and not os . path . isfile ( " ./Dockerfile " ) :
puts ( " This is not a clone of firegex, to build firegex the clone of the repository is needed! " , color = colors . red )
exit ( )
2022-07-21 20:25:39 +02:00
2023-04-12 12:05:03 +02:00
if args . threads < 1 :
2022-08-09 10:28:30 +00:00
args . threads = multiprocessing . cpu_count ( )
2022-08-10 10:23:37 +00:00
if start_operation :
2023-04-12 12:05:03 +02:00
if check_if_exists ( " docker ps --filter ' name=^firegex$ ' --no-trunc | grep firegex " ) :
puts ( " Firegex is already running! use --help to see options useful to manage firegex execution " , color = colors . yellow )
exit ( )
2022-08-09 10:28:30 +00:00
sep ( )
puts ( f " Firegex " , color = colors . yellow , end = " " )
puts ( " will start on port " , end = " " )
puts ( f " { args . port } " , color = colors . cyan )
2022-06-15 09:11:27 +02:00
2022-08-09 10:28:30 +00:00
psw_set = None
2022-08-10 10:23:37 +00:00
if start_operation :
2022-08-09 10:28:30 +00:00
if args . psw_no_interactive :
psw_set = args . psw_no_interactive
elif not args . startup_psw :
2023-04-12 11:04:37 +02:00
while True :
2023-04-12 11:07:42 +02:00
puts ( " Insert the password for firegex: " , end = " " , color = colors . yellow , is_bold = True , flush = True )
2023-04-12 11:04:37 +02:00
psw_set = getpass . getpass ( " " )
2023-04-12 11:07:42 +02:00
puts ( " Confirm the password: " , end = " " , color = colors . yellow , is_bold = True , flush = True )
2023-04-12 11:04:37 +02:00
check = getpass . getpass ( " " )
if check != psw_set :
2023-04-12 11:08:30 +02:00
puts ( " Passwords don ' t match! " , color = colors . red , is_bold = True , flush = True )
2023-04-12 11:04:37 +02:00
else :
break
2023-04-11 22:15:17 +02:00
composefile = " firegex-compose.yml "
with open ( composefile , " wt " ) as compose :
2022-06-15 09:11:27 +02:00
if " linux " in sys . platform and not ' microsoft-standard ' in platform . uname ( ) . release : #Check if not is a wsl also
compose . write ( f """
version : ' 3.9 '
services :
firewall :
restart : unless - stopped
2023-04-12 12:05:03 +02:00
container_name : firegex
2023-04-11 22:15:17 +02:00
{ " build: . " if args . build else " image: ghcr.io/pwnzer0tt1/firegex " }
2022-06-15 09:11:27 +02:00
network_mode : " host "
environment :
2022-06-28 13:26:06 +02:00
- PORT = { args . port }
2022-07-22 00:34:57 +02:00
- NTHREADS = { args . threads }
2022-08-09 10:28:30 +00:00
{ " - HEX_SET_PSW= " + psw_set . encode ( ) . hex ( ) if psw_set else " " }
2022-06-15 09:11:27 +02:00
volumes :
- / execute / db
2022-07-07 09:45:27 +02:00
cap_add :
- NET_ADMIN
2022-06-15 09:11:27 +02:00
""" )
2022-07-19 15:17:34 +02:00
2022-06-15 09:11:27 +02:00
else :
2022-06-19 20:11:28 +02:00
sep ( )
puts ( " --- WARNING --- " , color = colors . yellow )
puts ( " You are not in a linux machine, due to docker limitation on other platform, the firewall will not work in this machine. You will only see the interface of firegex. " , color = colors . red )
2022-06-15 09:11:27 +02:00
compose . write ( f """
version : ' 3.9 '
services :
firewall :
restart : unless - stopped
2023-04-12 12:05:03 +02:00
container_name : firegex
2023-04-11 22:15:17 +02:00
{ " build: . " if args . build else " image: ghcr.io/pwnzer0tt1/firegex " }
2022-06-15 09:11:27 +02:00
ports :
- { args . port } : { args . port }
environment :
2022-06-28 13:26:06 +02:00
- PORT = { args . port }
2022-07-22 00:34:57 +02:00
- NTHREADS = { args . threads }
2022-08-09 10:28:30 +00:00
{ " - HEX_SET_PSW= " + psw_set . encode ( ) . hex ( ) if psw_set else " " }
2022-06-15 09:11:27 +02:00
volumes :
- / execute / db
2022-07-07 09:45:27 +02:00
cap_add :
- NET_ADMIN
2022-06-15 09:11:27 +02:00
""" )
2022-06-19 20:11:28 +02:00
sep ( )
if not args . no_autostart :
2022-08-09 10:28:30 +00:00
try :
2022-08-10 10:23:37 +00:00
if args . restart :
puts ( " Running ' docker-compose restart ' \n " , color = colors . green )
2023-04-11 21:15:30 +02:00
composecmd ( " restart " , composefile )
2022-08-10 10:23:37 +00:00
elif args . stop :
2022-08-09 10:28:30 +00:00
puts ( " Running ' docker-compose down ' \n " , color = colors . green )
2023-04-11 21:15:30 +02:00
composecmd ( " down " , composefile )
2022-08-09 10:28:30 +00:00
else :
2023-04-11 22:15:17 +02:00
if not args . build :
2022-08-13 08:49:18 +00:00
puts ( " Downloading docker image from github packages ' docker pull ghcr.io/pwnzer0tt1/firegex ' " , color = colors . green )
2022-08-23 21:56:33 +00:00
dockercmd ( " pull ghcr.io/pwnzer0tt1/firegex " )
2022-08-09 10:28:30 +00:00
puts ( " Running ' docker-compose up -d --build ' \n " , color = colors . green )
2023-04-11 21:15:30 +02:00
composecmd ( " up -d --build " , composefile )
2022-08-09 10:28:30 +00:00
finally :
if not args . keep :
2023-04-11 22:15:17 +02:00
os . remove ( composefile )
2022-06-19 20:11:28 +02:00
else :
2022-08-09 10:28:30 +00:00
puts ( " Done! You can start/stop firegex with docker-compose up -d --build " , color = colors . yellow )
2022-06-19 20:11:28 +02:00
sep ( )