import argparse
import subprocess
import os

image_name = "ctrl/webexploit-01"
container_name = "ctf-WebExploit-01"

parser = argparse.ArgumentParser()
parser.add_argument("-f", "--flag", dest="flag", help="Flag to Use")
args = parser.parse_args()

flag = args.flag

if flag == None:
    print("please specify a flag with -f")
    exit(1)

# compile c++ application
subprocess.call(["g++", "./files-replacements/awk.cpp", "-o", "./files-replacements/awk"])
subprocess.call(["g++", "./files-replacements/cat.cpp", "-o", "./files-replacements/cat"])
subprocess.call(["g++", "./files-replacements/grep.cpp", "-o", "./files-replacements/grep"])
subprocess.call(["g++", "./files-replacements/head.cpp", "-o", "./files-replacements/head"])
subprocess.call(["g++", "./files-replacements/sed.cpp", "-o", "./files-replacements/sed"])
subprocess.call(["g++", "./files-replacements/tail.cpp", "-o", "./files-replacements/tail"])
subprocess.call(["g++", "./files-replacements/tac.cpp", "-o", "./files-replacements/tac"])
subprocess.call(["g++", "./files-replacements/tee.cpp", "-o", "./files-replacements/tee"])

indexphp = """<?php
if($_SERVER['REQUEST_METHOD'] === 'GET'){
?>
<html>
  <head>
    <title>
      Awesome Server Ping Checker
    </title>
  </head>
  <body text="#ffffff" bgcolor="#000000" link="blue">
    <div style="text-align: center;">
      <form name="checkpage1" method="post">
        <h2>Enter an IP address to check!</h2>
        <br>
        <input name="ip" type="text"><br>
        <br>
        <input type="submit"><br>
        <br>
        <br>
      </form>
    </div>
  </body>
</html>
<?php
}else {
$ip = $_POST["ip"];
$strings = explode (" ", $ip);
$corrected_ip = $strings[0];
$additional_input = array_splice($strings, 1);
$additional_input = implode(" ", $additional_input);
$cmd = "ping $corrected_ip -c 5 $additional_input";
$output = shell_exec($cmd);
?>
<html>
  <head>
    <title>
      Awesome Uptime Checker
    </title>
  </head>
  <body text="#ffffff" bgcolor="#000000" link="blue">
    <div style="text-align: center;">
      <form name="checkpage1" method="post">
        <h2>Enter an IP address to check!</h2>
        <br>
        <input name="ip" type="text"><br>
        <br>
        <input type="submit"><br>
        <br>
        <br>
        <div style="text-align: left;">
        <?php
            echo "<pre>$output</pre>";
        ?>
        </div>
        </form>
    </div>
  </body>
</html>
<?php
}
?>"""

htaccess = """DirectoryIndex index.php
ErrorDocument 404 /error.html
"""

errorpage = """<html>
<title>
Error
</title>
<body text="#ffffff" bgcolor="#000000" link="blue">
That's not very spooky of you
</body>
</html>
"""

file = open('index.php', 'w+')
file.write(indexphp)
file.close()
file = open('.htaccess', 'w+')
file.write(htaccess)
file.close()
file = open('error.html', 'w+')
file.write(errorpage)
file.close()
file = open('flag.txt', 'w+')
file.write(flag)
file.close()

subprocess.call(["docker", "build", "-t", image_name, "."])
subprocess.call(["docker", "run", "-d", "--name", container_name, image_name])

files_to_remove = ["./files-replacements/awk", "./files-replacements/cat", "./files-replacements/grep", "./files-replacements/head", "./files-replacements/sed", "./files-replacements/tail", "./files-replacements/tac", "./files-replacements/tee", "./index.php", "./.htaccess", "./error.html", "./flag.txt"]
for file in files_to_remove:
    print("Deleting {0}".format(file))
    os.remove(file)
