Skip to content

Instantly share code, notes, and snippets.

@pangyuteng
Last active August 6, 2024 19:37
Show Gist options
  • Save pangyuteng/a0bd92268efd03044237082859785db8 to your computer and use it in GitHub Desktop.
Save pangyuteng/a0bd92268efd03044237082859785db8 to your computer and use it in GitHub Desktop.
just for fun
  • setup oracle vm and expose port 80 just for the heck of it


ufw allow ssh
ufw allow http
ufw enable

apt install fail2ban -yq

systemctl enable fail2ban
systemctl start fail2ban

curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh ./get-docker.sh

git clone https://gist.github.com/a0bd92268efd03044237082859785db8.git app
cd app
mkdir log
docker compose up -d



import os
import logging
from logging.handlers import TimedRotatingFileHandler
from flask import Flask, request, jsonify, render_template
#logging.basicConfig(filename='log.txt',level=logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
handler = TimedRotatingFileHandler('/opt/log/log.txt',when='midnight',backupCount=1000)
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
app = Flask(__name__)
@app.after_request
def log_request(response):
if 'X-Forwarded-For' in request.headers:
proxy_data = request.headers['X-Forwarded-For']
ip_list = proxy_data.split(',')
user_ip = ip_list[0] # first address in list is User IP
else:
user_ip = request.remote_addr
app.logger.debug(f"Request: {request.method} {request.url} {user_ip} {response.status_code}")
return response
@app.route('/')
def index():
return jsonify('Hello, World!')
LOG_FILE = "/opt/log/log.txt"
@app.route('/hohoho')
def hohoho():
if not os.path.exists(LOG_FILE):
return jsonify("log file not found")
with open(LOG_FILE,'r') as f:
content = f.read()
log_list = content.split("\n")
return render_template('hohoho.html',log_list=log_list)
services:
app:
image: flask
build:
context: .
dockerfile: Dockerfile
restart: always
command: flask run --host=0.0.0.0 --port=5000
ports:
- '5000:5000'
volumes:
- $PWD/log:/opt/log
FROM python:3.12-slim-bullseye
RUN pip3 install flask
WORKDIR /opt
RUN mkdir -p /opt/log
COPY app.py /opt/app.py
COPY hohoho.html /opt/templates/hohoho.html
<html>
<head>
<title>hohoho</title>
</head>
<body>
{% for item in log_list %}
{{ item }}<br>
{% endfor %}
</body>
</html>
events {}
http {
server {
listen 443 ssl http2;
server_name pangyuteng.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/cert.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:5000;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment