From a99b68c04e4436d9b0f14e98c8965134ae4aa82b Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Wed, 23 Dec 2015 00:26:27 -0600 Subject: [PATCH] added basic database stuff --- Vagrantfile | 1 + ctf.nginx | 13 ++++++ deploy | 4 +- scripts/requirements.txt | 3 ++ scripts/setup.sh | 24 ++++++++--- server/api/__init__.py | 1 + server/api/models.py | 3 ++ server/app.py | 91 +++++++++++----------------------------- web/index.html | 8 ++++ 9 files changed, 75 insertions(+), 73 deletions(-) create mode 100644 ctf.nginx create mode 100644 server/api/__init__.py create mode 100644 server/api/models.py create mode 100644 web/index.html diff --git a/Vagrantfile b/Vagrantfile index 8e5b14d..6a97d6a 100644 --- a/Vagrantfile +++ b/Vagrantfile @@ -11,6 +11,7 @@ Vagrant.configure(2) do |config| config.vm.synced_folder "server", "/home/vagrant/server" config.vm.synced_folder "scripts", "/home/vagrant/scripts" + config.vm.synced_folder "web", "/srv/http/ctf" config.vm.provision :shell, :path => "scripts/setup.sh" config.ssh.forward_agent = true diff --git a/ctf.nginx b/ctf.nginx new file mode 100644 index 0000000..e992e67 --- /dev/null +++ b/ctf.nginx @@ -0,0 +1,13 @@ +server { + listen 80 default_server; + listen [::]:80 default_server ipv6only=on; + + root /srv/http/ctf; + index index.html index.htm; + + server_name localhost; + + location / { + try_files $uri $uri/ =404; + } +} \ No newline at end of file diff --git a/deploy b/deploy index 50f4853..4789c79 100755 --- a/deploy +++ b/deploy @@ -2,7 +2,9 @@ echo "Stopping the server..." pkill gunicorn +sudo service nginx stop echo "Starting the server..." cd /home/vagrant/server -gunicorn --bind 0.0.0.0:8000 -w 1 "app:app" +sudo service nginx start +gunicorn --bind 0.0.0.0:8000 -w 1 "app:app" \ No newline at end of file diff --git a/scripts/requirements.txt b/scripts/requirements.txt index 1167f2f..08461f8 100644 --- a/scripts/requirements.txt +++ b/scripts/requirements.txt @@ -1,2 +1,5 @@ Flask +mysql-python +Flask-SQLAlchemy +SQLAlchemy gunicorn \ No newline at end of file diff --git a/scripts/setup.sh b/scripts/setup.sh index 043bc62..65c33cf 100644 --- a/scripts/setup.sh +++ b/scripts/setup.sh @@ -1,13 +1,27 @@ #!/bin/bash +MYSQL_ROOT_PASSWORD="i_hate_passwords" + echo "Updating system..." -sudo apt-get -y update -sudo apt-get -y upgrade +apt-get -y update +apt-get -y upgrade + +echo "Preparing for MySQL installation..." +sudo debconf-set-selections <<< "mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSWORD" +sudo debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD" echo "Installing dependencies..." -sudo apt-get -y install python-pip +apt-get -y install python +apt-get -y install python-pip +apt-get -y install python-dev libmysqlclient-dev +apt-get -y install nginx +apt-get -y install mysql-server echo "Installing pip dependencies..." -sudo pip install -r scripts/requirements.txt +pip install -r scripts/requirements.txt -echo 'PATH=$PATH:/vagrant' >> /etc/profile \ No newline at end of file +echo "PATH=$PATH:/vagrant" >> /etc/profile +cp /vagrant/ctf.nginx /etc/nginx/sites-enabled/ctf +rm /etc/nginx/sites-enabled/default + +mysql -u root -p "$MYSQL_ROOT_PASSWORD" -e "CREATE DATABASE easyctf;" \ No newline at end of file diff --git a/server/api/__init__.py b/server/api/__init__.py new file mode 100644 index 0000000..5b42b9b --- /dev/null +++ b/server/api/__init__.py @@ -0,0 +1 @@ +import api.models \ No newline at end of file diff --git a/server/api/models.py b/server/api/models.py new file mode 100644 index 0000000..30b9456 --- /dev/null +++ b/server/api/models.py @@ -0,0 +1,3 @@ +from flask.ext.sqlalchemy import SQLAlchemy + +db = SQLAlchemy() \ No newline at end of file diff --git a/server/app.py b/server/app.py index df19576..1e2caa5 100644 --- a/server/app.py +++ b/server/app.py @@ -1,75 +1,32 @@ +from argparse import ArgumentParser from flask import Flask -import sys + import config +import json +import sys app = Flask(__name__) app.secret_key = config.SECRET -#Home Page -@app.route("/") -def hello_world(): - return "Hello, EasyCTF!" -#Login Page -@app.route('/login') -def login(): - return "EasyCTF Login" -#Registration Page -@app.route('/register') -def register(): - return "EasyCTF Register" -#Scoreboard Page -@app.route('/scoreboard') -def scoreboard(): - return "EasyCTF Scoreboard" -#Problems Page -@app.route('/problems') -def problems(): - return "EasyCTF Problems" -#Account Page -@app.route('/account') -def account(): - return "EasyCTF Account" -#Programming Page -@app.route('/programming') -def programming(): - return "EasyCTF Programming" -#Chat Page -@app.route('/chat') -def chat(): - return "EasyCTF Chat" -#About Page -@app.route('/about') -def about(): - return "EasyCTF About" -#Forgot Password Page -@app.route('/forgot_password') -def forgot_password(): - return "EasyCTF Forgot Password" -#Logout Page -@app.route('/logout') -def logout(): - return "EasyCTF Logout" -#Rules Page -@app.route('/rules') -def rules(): - return "EasyCTF Rules" -#Team Page -@app.route('/team') -def team(): - return "EasyCTF Team" -#Shell Page -@app.route('/shell') -def shell(): - return "EasyCTF Shell" -#Updates Page -@app.route('/updates') -def updates(): - return "EasyCTF Updates" -#Reset Password Page -@app.route('/reset_password') -def reset_password(): - return "EasyCTF Reset" +@app.route("/api") +def api(): + return json.dumps({ "success": 1, "message": "The API is online." }) if __name__ == "__main__": - app.debug = "--debug" in sys.argv - app.run(port=8000) + with app.app_context(): + parser = ArgumentParser(description="EasyCTF Server Configuration") + parser.add_argument("-d", "--debug", action="store_true", help="Run the server in debug mode.", default=False) + args = parser.parse_args() + keyword_args, _ = dict(args._get_kwargs()), args._get_args() + + app.debug = keyword_args["debug"] + + app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:i_hate_passwords@localhost/easyctf" + app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False + + from api.models import db + db.init_app(app) + db.create_all() + print db + + app.run(host="0.0.0.0", port=8000) \ No newline at end of file diff --git a/web/index.html b/web/index.html new file mode 100644 index 0000000..e49fc63 --- /dev/null +++ b/web/index.html @@ -0,0 +1,8 @@ + + + EasyCTF 2016 + + +

Hello, EasyCTF!

+ + \ No newline at end of file