Skip to content

Commit 3ee9d13

Browse files
committed
Merging Issue-1 into develop
2 parents 85231bf + fa7ad77 commit 3ee9d13

28 files changed

Lines changed: 456 additions & 111 deletions

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
/coverage
66
/build
7+
/docker/resumes
78

8-
/src/backend/challenge_archives/*.zip
9+
backend/challenge_archives/*.zip
910

1011
.DS_Store
1112
.env
1213
.env.*
14+
/docker/.env
1315

1416
npm-debug.log*
1517
yarn-debug.log*

README.md

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,39 @@ Website repo for the Kent Hack It CTF
44
## What is this?
55
The frontend uses ReactJS that communicates to a ExpressJS backend that handles: user login/registration, team creation/joining, and flag submission.
66

7-
## :hammer: Install Dependencies
7+
## :computer: Development Installation
8+
### :hammer: Install Dependencies
89
```bash
910
# if you do not have NodeJS installed
1011
sudo apt update && sudo apt install nodejs npm
11-
12-
# use npm to install latest node
13-
sudo npm install -g n && sudo n stable
14-
15-
# if you do not want to keep the older nodejs binary on the system:
16-
# replace old nodejs for new nodejs
17-
$oldNode = $(which nodejs)
18-
sudo rm -f $oldNode
19-
20-
# create a symlink named nodejs to execute the node binary
21-
sudo ln -s $(which node) $oldNode
2212
```
2313

24-
## :wrench: Build
14+
### :wrench: Build
2515
```bash
2616
# installs dependencies/packages tracked by this project
2717
npm install .
2818
# compile and run the frontend locally
2919
npm start
3020
```
3121

32-
Move the `.env` file into the project root directory, then run the following to run the backend locally.
22+
Move the web related `.env` file into the project root directory, then run the following to run the backend locally.
3323
```bash
34-
# if you want to use the latest node binary replace nodejs -> node
35-
nodejs ./src/backend/server.js
24+
nodejs backend/server.mjs
3625
```
3726

3827
## :chart_with_upwards_trend: Production
39-
You will need to install a dependency to use `serve`, this is recommended after running `npm run build`.
28+
This project uses Docker :whale: for production set-up
29+
```bash
30+
sudo apt update && sudo apt install docker.io docker-compose
31+
```
32+
The following commands will build the docker via compose which builds the multi-docker system.
33+
You will need to move the `.env` into the project root folder before running the following:
4034
```bash
41-
sudo npm install -g serve
35+
docker network create traefik
36+
docker-compose --env-file .env -p khi -f docker/docker-compose.yml up --build
4237
```
43-
Prepare production build and serve as a static server.
38+
The following commands will build ONLY the khi website docker image that you later start via `docker run -d ...`.
4439
```bash
45-
npm run build && server -s build
40+
docker build -f docker/Dockerfile -t image_name .
41+
docker run -d -p 8080:80 --name docker_name image_name
4642
```

src/backend/db.js renamed to backend/db.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ function MongoURI() {
1212
const db_pass = process.env.DB_PASS;
1313
const db_name = process.env.DB_NAME;
1414

15-
return "mongodb://" + db_user + ":" + db_pass + "@localhost:27017/" + db_name;
15+
return "mongodb://" + db_user + ":" + db_pass + "@khi_db:27017/" + db_name + "?authSource=admin";
1616
}
1717

18+
console.log(`[*] Attempting to Connect to: ${MongoURI()}`);
1819
// Connect to MongoDB
1920
mongoose.connect(MongoURI()).then(() => {
2021
console.log('Connected to MongoDB');
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import GetChallenges, { LoginUser, LoginAdmin, RegisterUser,
66
GetAllUsers, GetAllTeams, RemoveTeam, RemoveUser,
77
UpdateChallenge, AdminGetChallenges, CreateChallenge,
88
DeleteChallenge, RegisterAdmin, RemoveAdmin, GetAdmins,
9-
ValidateAdmin, GetLeaderboardData } from './db.js';
9+
ValidateAdmin, GetLeaderboardData } from './db.mjs';
1010
import rateLimit from 'express-rate-limit';
1111
import cookieParser from 'cookie-parser';
1212
import sanitize from 'sanitize-filename';
@@ -67,16 +67,16 @@ const app = express();
6767
#################################################################
6868
*/
6969
const host = "0.0.0.0"
70-
const port = process.env.REACT_APP_BACKEND_PORT;
70+
const port = 4000;
7171

7272
// Allows data to be sent from post requests
7373
app.use(express.urlencoded({ extended: true }));
7474
app.use(express.json()); // middleware to handle JSON
7575
app.use(cookieParser()); // Access cookies
7676

7777
const allowedOrigins = [
78-
`http://localhost:${process.env.FRONT_END_PORT}`, // local dev
79-
`http://${process.env.LAN_HOST}:${process.env.FRONT_END_PORT}`, // LAN live frontend
78+
`http://localhost:80`, // local dev
79+
`http://localhost:8080`,
8080
// 'https://khi.io' // production site
8181
];
8282

docker/Dockerfile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
FROM nginx:latest
2+
WORKDIR /
3+
4+
# setup for mongo container manually:
5+
# docker run -d --name khi_db -e MONGO_INITDB_ROOT_USERNAME=env.user -e MONGO_INITDB_ROOT_PASSWORD=env.password -e MONGO_INITDB_DATABASE=env.db -v mongodb_data:/data/db -p 27017:27017 mongo:latest
6+
RUN apt-get update && apt-get install -y supervisor sudo nodejs npm net-tools
7+
8+
# ensure directories exist
9+
RUN mkdir /var/www
10+
RUN mkdir /var/www/khi
11+
RUN mkdir /var/www/project
12+
13+
# give www-data ability to run commands and interact with web-root
14+
RUN chsh -s /bin/bash www-data
15+
RUN chown -R www-data:www-data /var/www
16+
17+
# move production build into docker and move
18+
# nginx conf file into the docker
19+
COPY public /var/www/project/public
20+
COPY src /var/www/project/src
21+
COPY .env /var/www/project/.env
22+
COPY package.json /var/www/project/package.json
23+
24+
# create symlink to .env
25+
RUN ln -s /var/www/project/.env /var/www/khi/.env
26+
RUN ln -s /var/www/project/package.json /var/www/khi/package.json
27+
28+
# build react app
29+
RUN su www-data -s /bin/sh -c "cd /var/www/project && npm install . && npm run build && cp -r build/* -t /var/www/khi"
30+
31+
COPY docker/khi /etc/nginx/conf.d/khi.conf
32+
33+
# move backend to web root
34+
COPY backend /var/www/khi/backend
35+
36+
# remove default nginx config
37+
RUN rm /etc/nginx/conf.d/default.conf
38+
39+
# install needed nodejs packages to allow the backend to run
40+
RUN su www-data -s /bin/sh -c "cd /var/www/khi && npm install ."
41+
42+
EXPOSE 80
43+
44+
# start an auto restart service for the backend
45+
COPY docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf
46+
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

docker/docker-compose.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
services:
2+
db:
3+
image: mongo:latest
4+
container_name: khi_db
5+
restart: always
6+
environment:
7+
- MONGO_INITDB_ROOT_USERNAME=${DB_USER}
8+
- MONGO_INITDB_ROOT_PASSWORD=${DB_PASS}
9+
- MONGO_INITDB_DATABASE=khi2025
10+
volumes:
11+
- db_data:/data/db
12+
- ./init_mongo.sh:/docker-entrypoint-initdb.d/init.sh:ro
13+
healthcheck:
14+
test: ["CMD", "mongosh", "--eval", "db.adminCommand('ping')"]
15+
interval: 10s
16+
timeout: 5s
17+
retries: 5
18+
networks:
19+
- app_internal
20+
21+
app:
22+
depends_on:
23+
db:
24+
condition: service_healthy
25+
build:
26+
context: ../
27+
dockerfile: docker/Dockerfile
28+
ports:
29+
- "8080:80"
30+
environment:
31+
- DATABASE_URL=mongodb://${DB_USER}:${DB_PASS}@khi_db:27017/khi2025?authSource=admin
32+
- ORIGIN=http://0.0.0.0:80
33+
labels:
34+
- "traefik.enable=true"
35+
- "traefik.http.routers.khi.rule=Host(`khi.io`)"
36+
- "traefik.http.routers.khi.entrypoints=websecure"
37+
- "traefik.http.routers.khi.tls.certresolver=letsencrypt"
38+
- "traefik.http.services.khi.loadbalancer.server.port=80"
39+
volumes:
40+
- ./resumes:/app/resumes
41+
networks:
42+
- app_internal
43+
- traefik
44+
45+
networks:
46+
app_internal:
47+
traefik:
48+
external: true
49+
50+
volumes:
51+
db_data:

docker/init_mongo.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
# THIS SCRIPT IS RESPONSIBLE FOR CREATING THE SUPER ADMIN
3+
# FOR THE KHI WEBSITE (THIS ADMIN CANNOT BE REMOVED FROM THE ADMIN PANEL)
4+
5+
username="admin" # OPTIONAL to change
6+
password="admin" # CHANGE THIS IN PRODUCTION
7+
8+
salt="" # SALT .env
9+
hashed=$(echo -n "${salt}${password}" | sha256sum | awk '{print $1}')
10+
11+
mongosh -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" --authenticationDatabase admin "$MONGO_INITDB_DATABASE" <<EOF
12+
db.admins.insertOne({
13+
username: "$username",
14+
password: "$hashed",
15+
isProtected: true,
16+
created_at: new Date()
17+
});
18+
EOF > /root/init_mongo.log

docker/khi

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
server {
2+
listen 80;
3+
server_name khi;
4+
5+
root /var/www/khi;
6+
index index.html;
7+
8+
location / {
9+
try_files $uri /index.html;
10+
}
11+
12+
# points to backend (reverse proxy)
13+
location /api/ {
14+
client_max_body_size 25M;
15+
proxy_pass http://127.0.0.1:4000/;
16+
proxy_http_version 1.1;
17+
proxy_set_header Host $host;
18+
proxy_set_header X-Real-IP $remote_addr;
19+
}
20+
}

docker/supervisord.conf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[supervisord]
2+
nodaemon=true
3+
4+
[program:nginx]
5+
command=/usr/sbin/nginx -g "daemon off;"
6+
autorestart=true
7+
8+
[program:backend]
9+
command=/usr/bin/nodejs /var/www/khi/backend/server.mjs
10+
directory=/var/www/khi
11+
autorestart=true
12+
user=www-data
13+
stdout_logfile=/var/log/backend.log
14+
stderr_logfile=/var/log/backend_err.log

0 commit comments

Comments
 (0)