Docker: single container compose
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -7,10 +7,10 @@
|
|||||||
**/.pnp.js
|
**/.pnp.js
|
||||||
|
|
||||||
# testing
|
# testing
|
||||||
/frontend/coverage
|
/firewall/frontend/coverage
|
||||||
|
|
||||||
# production
|
# production
|
||||||
/frontend/build
|
/firewall/frontend/build
|
||||||
|
|
||||||
# misc
|
# misc
|
||||||
**/.DS_Store
|
**/.DS_Store
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
version: '3.9'
|
version: '3.9'
|
||||||
|
|
||||||
services:
|
services:
|
||||||
frontend:
|
firewall:
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
build: frontend
|
build: firewall
|
||||||
backend:
|
network_mode: "host"
|
||||||
restart: unless-stopped
|
environment:
|
||||||
build: backend
|
- NGINX_PORT=8080
|
||||||
ports:
|
|
||||||
- 80:80
|
|
||||||
@@ -1,3 +1,17 @@
|
|||||||
|
|
||||||
|
#Frontend build
|
||||||
|
FROM node:16-alpine AS frontend
|
||||||
|
RUN apk add --update npm
|
||||||
|
RUN npm install -g npm@latest
|
||||||
|
RUN mkdir /app
|
||||||
|
WORKDIR /app
|
||||||
|
ADD ./frontend/package.json .
|
||||||
|
ADD ./frontend/package-lock.json .
|
||||||
|
RUN npm install
|
||||||
|
COPY ./frontend/ .
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
#Building main conteiner
|
||||||
FROM python:3-buster
|
FROM python:3-buster
|
||||||
|
|
||||||
RUN apt-get update && apt-get -y install supervisor build-essential libboost-dev nginx
|
RUN apt-get update && apt-get -y install supervisor build-essential libboost-dev nginx
|
||||||
@@ -12,8 +26,13 @@ COPY . /execute/
|
|||||||
COPY ./config/nginx.conf /etc/nginx/nginx.conf
|
COPY ./config/nginx.conf /etc/nginx/nginx.conf
|
||||||
COPY ./config/supervisord.conf /etc/supervisor/supervisord.conf
|
COPY ./config/supervisord.conf /etc/supervisor/supervisord.conf
|
||||||
|
|
||||||
|
#Copy react app in the main container
|
||||||
|
COPY --from=frontend /app/build/ ./frontend/
|
||||||
|
|
||||||
RUN usermod -a -G root nobody
|
RUN usermod -a -G root nobody
|
||||||
RUN chown -R nobody:root /execute && \
|
RUN chown -R nobody:root /execute && \
|
||||||
chmod -R 660 /execute && chmod -R u+X /execute
|
chmod -R 660 /execute && chmod -R u+X /execute
|
||||||
|
|
||||||
ENTRYPOINT ["/usr/bin/supervisord","-c","/etc/supervisor/supervisord.conf"]
|
ENTRYPOINT ["/usr/bin/supervisord","-c","/etc/supervisor/supervisord.conf"]
|
||||||
|
|
||||||
|
|
||||||
@@ -1,44 +1,7 @@
|
|||||||
import sqlite3
|
import sqlite3, random, string, subprocess
|
||||||
from flask import Flask, jsonify, request
|
from flask import Flask, jsonify, request
|
||||||
import random
|
|
||||||
|
|
||||||
|
|
||||||
class SQLite():
|
|
||||||
def __init__(self, db_name) -> None:
|
|
||||||
self.conn = None
|
|
||||||
self.cur = None
|
|
||||||
self.db_name = db_name
|
|
||||||
|
|
||||||
def connect(self) -> None:
|
|
||||||
try:
|
|
||||||
self.conn = sqlite3.connect(self.db_name + '.db', check_same_thread = False)
|
|
||||||
except:
|
|
||||||
with open(self.db_name + '.db', 'x') as f:
|
|
||||||
pass
|
|
||||||
|
|
||||||
self.conn = sqlite3.connect(self.db_name + '.db', check_same_thread = False)
|
|
||||||
|
|
||||||
self.cur = self.conn.cursor()
|
|
||||||
|
|
||||||
def disconnect(self) -> None:
|
|
||||||
self.conn.close()
|
|
||||||
|
|
||||||
def check_integrity(self, tables = {}) -> None:
|
|
||||||
for t in tables:
|
|
||||||
self.cur.execute('''
|
|
||||||
SELECT name FROM sqlite_master WHERE type='table' AND name=?;
|
|
||||||
''', (t,))
|
|
||||||
|
|
||||||
if len(self.cur.fetchall()) == 0:
|
|
||||||
self.cur.execute(f'CREATE TABLE main.{t}({"".join([(c + " " + tables[t][c] + ", ") for c in tables[t]])[:-2]});')
|
|
||||||
|
|
||||||
def query(self, query, values = ()):
|
|
||||||
self.cur.execute(query, values)import sqlite3
|
|
||||||
from flask import Flask, jsonify, request
|
|
||||||
import random
|
|
||||||
import string
|
|
||||||
import subprocess
|
|
||||||
|
|
||||||
class SQLite():
|
class SQLite():
|
||||||
def __init__(self, db_name) -> None:
|
def __init__(self, db_name) -> None:
|
||||||
self.conn = None
|
self.conn = None
|
||||||
@@ -303,231 +266,4 @@ if __name__ == '__main__':
|
|||||||
|
|
||||||
#uwsgi
|
#uwsgi
|
||||||
subprocess.run(["uwsgi","--http","127.0.0.1:8080","--master","--module","app:app"])
|
subprocess.run(["uwsgi","--http","127.0.0.1:8080","--master","--module","app:app"])
|
||||||
return self.cur.fetchall()
|
|
||||||
|
|
||||||
|
|
||||||
db = SQLite('firegex')
|
|
||||||
db.connect()
|
|
||||||
app = Flask(__name__)
|
|
||||||
|
|
||||||
@app.route('/api/general-stats')
|
|
||||||
def get_general_stats():
|
|
||||||
n_services = db.query('''
|
|
||||||
SELECT COUNT (*) FROM services;
|
|
||||||
''')[0][0]
|
|
||||||
n_regexes = db.query('''
|
|
||||||
SELECT COUNT (*) FROM regexes;
|
|
||||||
''')[0][0]
|
|
||||||
n_packets = db.query('''
|
|
||||||
SELECT SUM(blocked_packets) FROM regexes;
|
|
||||||
''')[0][0]
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'services': n_services,
|
|
||||||
'regexes': n_regexes,
|
|
||||||
'closed': n_packets if n_packets else 0
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/services')
|
|
||||||
def get_services():
|
|
||||||
res = []
|
|
||||||
for i in db.query('SELECT * FROM services;'):
|
|
||||||
n_regex = db.query('SELECT COUNT (*) FROM regexes WHERE service_id = ?;', (i[1],))[0][0]
|
|
||||||
n_pacchetti = db.query('SELECT SUM(blocked_packets) FROM regexes WHERE service_id = ?;', (i[1],))[0][0]
|
|
||||||
|
|
||||||
res.append({
|
|
||||||
'id': i[1],
|
|
||||||
'status': i[0],
|
|
||||||
'public_port': i[3],
|
|
||||||
'internal_port': i[2],
|
|
||||||
'n_regex': n_regex,
|
|
||||||
'n_packets': n_pacchetti if n_pacchetti else 0,
|
|
||||||
})
|
|
||||||
|
|
||||||
return jsonify(res)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/service/<serv>')
|
|
||||||
def get_service(serv):
|
|
||||||
q = db.query('SELECT * FROM services WHERE service_id = ?;', (serv,))
|
|
||||||
|
|
||||||
res = {}
|
|
||||||
if len(q) != 0:
|
|
||||||
n_regex = db.query('SELECT COUNT (*) FROM regexes WHERE service_id = ?;', (serv,))[0][0]
|
|
||||||
n_pacchetti = db.query('SELECT SUM(blocked_packets) FROM regexes WHERE service_id = ?;', (serv,))[0][0]
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'id': q[0][1],
|
|
||||||
'status': q[0][0],
|
|
||||||
'public_port': q[0][3],
|
|
||||||
'internal_port': q[0][2],
|
|
||||||
'n_packets': n_pacchetti if n_pacchetti else 0,
|
|
||||||
'n_regex': n_regex
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/service/<serv>/stop')
|
|
||||||
def get_service_stop(serv):
|
|
||||||
db.query('''
|
|
||||||
UPDATE services SET status = 'stop' WHERE service_id = ?;
|
|
||||||
''', (serv,))
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'status': 'ok'
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/service/<serv>/start')
|
|
||||||
def get_service_start(serv):
|
|
||||||
db.query('''
|
|
||||||
UPDATE services SET status = 'active' WHERE service_id = ?;
|
|
||||||
''', (serv,))
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'status': 'ok'
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/service/<serv>/delete')
|
|
||||||
def get_service_delete(serv):
|
|
||||||
db.query('''
|
|
||||||
DELETE FROM services WHERE service_id = ?;
|
|
||||||
''', (serv,))
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'status': 'ok'
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/service/<serv>/terminate')
|
|
||||||
def get_service_termite(serv):
|
|
||||||
db.query('''
|
|
||||||
UPDATE services SET status = 'stop' WHERE service_id = ?;
|
|
||||||
''', (serv,))
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'status': 'ok'
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/service/<serv>/regen-port')
|
|
||||||
def get_regen_port(serv):
|
|
||||||
db.query('UPDATE services SET public_port = ? WHERE service_id = ?;', (random.randint(30000, 45000), serv))
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'status': 'ok'
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/service/<serv>/regexes')
|
|
||||||
def get_service_regexes(serv):
|
|
||||||
res = []
|
|
||||||
for i in db.query('SELECT * FROM regexes WHERE service_id = ?;', (serv,)):
|
|
||||||
res.append({
|
|
||||||
'id': i[5],
|
|
||||||
'service_id': i[2],
|
|
||||||
'regex': i[0],
|
|
||||||
'is_blacklist': i[3],
|
|
||||||
'mode': i[1],
|
|
||||||
'n_packets': i[4]
|
|
||||||
})
|
|
||||||
|
|
||||||
return jsonify(res)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/regex/<int:regex_id>')
|
|
||||||
def get_regex_id(regex_id):
|
|
||||||
q = db.query('SELECT * FROM regexes WHERE regex_id = ?;', (regex_id,))
|
|
||||||
|
|
||||||
res = {}
|
|
||||||
if len(q) != 0:
|
|
||||||
res = {
|
|
||||||
'id': regex_id,
|
|
||||||
'service_id': q[0][2],
|
|
||||||
'regex': q[0][0],
|
|
||||||
'is_blacklist': q[0][3],
|
|
||||||
'mode': q[0][1],
|
|
||||||
'n_packets': q[0][4]
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/regex/<int:regex_id>/delete')
|
|
||||||
def get_regex_delete(regex_id):
|
|
||||||
db.query('DELETE FROM regexes WHERE regex_id = ?;', (regex_id,))
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'status': 'ok'
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/regexes/add', methods = ['POST'])
|
|
||||||
def post_regexes_add():
|
|
||||||
req = request.get_json(force = True)
|
|
||||||
|
|
||||||
db.query('''
|
|
||||||
INSERT INTO regexes (regex_id, service_id, regex, is_blacklist, mode) VALUES (?, ?, ?, ?, ?);
|
|
||||||
''', (random.randint(1, 1 << 32), req['service_id'], req['regex'], req['is_blacklist'], req['mode']))
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'status': 'ok'
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/services/add', methods = ['POST'])
|
|
||||||
def post_services_add():
|
|
||||||
req = request.get_json(force = True)
|
|
||||||
|
|
||||||
db.query('''
|
|
||||||
INSERT INTO services (service_id, internal_port, public_port, status) VALUES (?, ?, ?, ?)
|
|
||||||
''', (req['name'], req['port'], random.randint(30000, 45000), 'stop'))
|
|
||||||
|
|
||||||
res = {
|
|
||||||
'status': 'ok'
|
|
||||||
}
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
import subprocess
|
|
||||||
# DB init
|
|
||||||
db.check_integrity({
|
|
||||||
'regexes': {
|
|
||||||
'regex': 'TEXT NOT NULL',
|
|
||||||
'mode': 'CHAR(1)',
|
|
||||||
'service_id': 'TEXT NOT NULL',
|
|
||||||
'is_blacklist': 'CHAR(50) NOT NULL',
|
|
||||||
'blocked_packets': 'INTEGER DEFAULT 0',
|
|
||||||
'regex_id': 'INTEGER NOT NULL'
|
|
||||||
},
|
|
||||||
'services': {
|
|
||||||
'status': 'CHAR(50)',
|
|
||||||
'service_id': 'TEXT NOT NULL',
|
|
||||||
'internal_port': 'INT NOT NULL',
|
|
||||||
'public_port': 'INT NOT NULL',
|
|
||||||
'name': 'TEXT NOT NULL'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
#uwsgi
|
|
||||||
subprocess.run(["uwsgi","--http","127.0.0.1:8080","--master","--module","app:app"])
|
|
||||||
|
|
||||||
@@ -10,12 +10,13 @@ events {
|
|||||||
|
|
||||||
http{
|
http{
|
||||||
server {
|
server {
|
||||||
listen 80;
|
listen ${NGINX_PORT};
|
||||||
server_name _;
|
server_name _;
|
||||||
|
|
||||||
|
|
||||||
|
root /execute/frontend/;
|
||||||
location / {
|
location / {
|
||||||
include proxy_params;
|
try_files $uri /index.html;
|
||||||
proxy_pass http://frontend:3000/;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
location /api/ {
|
location /api/ {
|
||||||
|
Before Width: | Height: | Size: 3.8 KiB After Width: | Height: | Size: 3.8 KiB |
|
Before Width: | Height: | Size: 5.2 KiB After Width: | Height: | Size: 5.2 KiB |
|
Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 9.4 KiB |
@@ -1,14 +0,0 @@
|
|||||||
FROM node:16-alpine
|
|
||||||
|
|
||||||
#React project copy
|
|
||||||
RUN apk add --update npm
|
|
||||||
RUN npm install -g npm@latest
|
|
||||||
RUN mkdir /app
|
|
||||||
WORKDIR /app
|
|
||||||
ADD package.json .
|
|
||||||
ADD package-lock.json .
|
|
||||||
RUN npm install
|
|
||||||
RUN npm install serve -g
|
|
||||||
COPY . .
|
|
||||||
RUN npm run build
|
|
||||||
ENTRYPOINT [ "serve", "-s", "build" ]
|
|
||||||
Reference in New Issue
Block a user