2022-06-15 09:11:27 +02:00
#!/usr/bin/env python3
2022-08-09 10:28:30 +00:00
import argparse , sys , platform , os , multiprocessing
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
2022-08-23 21:56:33 +00:00
def composecmd ( cmd ) :
2023-04-11 20:21:49 +02:00
return os . system ( f " (which docker-compose &> /dev/null && (docker-compose -p firegex { cmd } || exit 0)) || (which docker &> /dev/null && (docker compose -p firegex { cmd } || exit 0)) || echo ' Docker not found!, please install docker and docker compose ' " )
2022-08-23 21:56:33 +00:00
def dockercmd ( cmd ) :
2023-04-11 20:21:49 +02:00
return os . system ( f " (which docker &> /dev/null && (docker { cmd } || exit 0)) || echo ' Docker not found!, please install docker and docker-compose ' " )
2022-08-23 21:56:33 +00: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 )
parser . add_argument ( ' --keep ' , ' -k ' , required = False , action = " store_true " , help = ' Keep the docker-compose file generated ' , default = False )
2022-08-03 13:44:30 +02:00
parser . add_argument ( ' --build ' , " -b " , required = False , action = " store_true " , help = ' Build the container locally ' , 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__ ) ) )
2022-06-15 09:11:27 +02:00
2022-08-10 10:23:37 +00:00
start_operation = not ( args . stop or args . restart )
2022-08-09 10:28:30 +00: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 )
2022-07-21 20:25:39 +02:00
exit ( )
2022-08-09 10:28:30 +00:00
if args . threads < 1 :
args . threads = multiprocessing . cpu_count ( )
2022-08-10 10:23:37 +00:00
if start_operation :
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 :
puts ( " Insert the password for firegex: " , end = " " , color = colors . yellow , is_bold = True )
psw_set = input ( )
2022-06-15 09:11:27 +02:00
with open ( " docker-compose.yml " , " wt " ) as compose :
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
2022-08-13 08:49:18 +00: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
2022-08-13 08:49:18 +00: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 )
2022-08-23 21:56:33 +00:00
composecmd ( " restart " )
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 )
2022-08-23 21:56:33 +00:00
composecmd ( " down " )
2022-08-09 10:28:30 +00:00
else :
2022-08-13 08:49:18 +00:00
if not args . build :
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 )
2022-08-23 21:56:33 +00:00
composecmd ( " up -d --build " )
2022-08-09 10:28:30 +00:00
finally :
if not args . keep :
os . remove ( " docker-compose.yml " )
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 ( )