mirror of
https://github.com/andrewkdinh/simple-contact.git
synced 2024-11-23 21:14:39 -08:00
first commit
This commit is contained in:
parent
f852d7a495
commit
5cc68b7d0a
2
.env.example
Normal file
2
.env.example
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
HTTP_ENDPOINT=https://example.invalid
|
||||||
|
ESCAPE_HTML=False
|
9
Dockerfile
Normal file
9
Dockerfile
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
FROM tiangolo/uwsgi-nginx-flask:python3.8
|
||||||
|
LABEL maintainer="Andrew Dinh <simple-contact@andrewkdinh.com>"
|
||||||
|
LABEL version="0.1.0"
|
||||||
|
|
||||||
|
EXPOSE 8888
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
ADD . .
|
||||||
|
RUN python3 -m pip install --no-cache-dir -r /app/requirements.txt
|
26
README.md
Normal file
26
README.md
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# Simple Contact
|
||||||
|
|
||||||
|
Extremely simple contact form with a CAPTCHA. Entries are sent to the specified HTTP endpoint.
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
1. Install `git`, `docker`, and `docker-compose`
|
||||||
|
2.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://github.com/andrewkdinh/simple-contact.git
|
||||||
|
git clone https://github.com/daniel-e/rust-captcha.git
|
||||||
|
cd simple-contact
|
||||||
|
cp .env.example .env
|
||||||
|
# Edit .env
|
||||||
|
docker-compose up -d
|
||||||
|
```
|
||||||
|
3. Visit `http://localhost:8672`
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
- Built with Python, Flask, Docker, Rust CAPTCHA, and water.css
|
||||||
|
|
||||||
|
Mirrors: [GitHub](https://github.com/andrewkdinh/simple-contact) (main), [Gitea](https://gitea.andrewkdinh.com/andrewkdinh/simple-contact)
|
||||||
|
|
||||||
|
Licensed under [AGPL 3.0](./LICENSE) | Copyright (c) 2021 Andrew Dinh
|
19
docker-compose.yml
Normal file
19
docker-compose.yml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
version: '2.0'
|
||||||
|
services:
|
||||||
|
simple-contact:
|
||||||
|
container_name: simple-contact
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
restart: unless-stopped
|
||||||
|
ports:
|
||||||
|
- 8672:80
|
||||||
|
depends_on:
|
||||||
|
- rust-captcha
|
||||||
|
environment:
|
||||||
|
- HTTP_ENDPOINT=${HTTP_ENDPOINT} # Send messages to this endpoint
|
||||||
|
- ESCAPE_HTML=${ESCAPE_HTML} # Whether the contents of the message should be escaped for HTML
|
||||||
|
rust-captcha:
|
||||||
|
container_name: rust-captcha
|
||||||
|
build:
|
||||||
|
context: ../rust-captcha/docker
|
||||||
|
restart: unless-stopped
|
71
main.py
Normal file
71
main.py
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from flask import Flask, render_template, redirect, request
|
||||||
|
import requests
|
||||||
|
from typing import List
|
||||||
|
import os
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
|
||||||
|
HTTP_ENDPOINT = os.getenv('HTTP_ENDPOINT')
|
||||||
|
ESCAPE_HTML = os.getenv('ESCAPE_HTML')
|
||||||
|
if not HTTP_ENDPOINT:
|
||||||
|
print("ERROR: HTTP_ENDPOINT not set")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
if not ESCAPE_HTML:
|
||||||
|
print("ERROR: ESCAPE_HTML not set")
|
||||||
|
exit(1)
|
||||||
|
if ESCAPE_HTML == "True":
|
||||||
|
ESCAPE_HTML = True
|
||||||
|
elif ESCAPE_HTML == "False":
|
||||||
|
ESCAPE_HTML = False
|
||||||
|
else:
|
||||||
|
print("ERROR: ESCAPE_HTML must be set to True or False")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
@app.route("/", methods=["GET", "POST"])
|
||||||
|
def index():
|
||||||
|
try:
|
||||||
|
if request.method == "GET":
|
||||||
|
captcha_id, captcha_png = captcha_get(ttl=600)
|
||||||
|
return render_template("index.html", captcha_id = captcha_id, captcha_png = captcha_png)
|
||||||
|
elif request.method == "POST":
|
||||||
|
captcha_id = request.form.get('captcha-id')
|
||||||
|
captcha_solution = request.form.get('captcha-solution')
|
||||||
|
success, trials_left = captcha_validate(captcha_id, captcha_solution)
|
||||||
|
if not success:
|
||||||
|
return render_template('message.html', message = "Failed captcha", attempts_left = trials_left)
|
||||||
|
message = request.form.get('message')
|
||||||
|
if ESCAPE_HTML:
|
||||||
|
message = message.replace("<", "<").replace(">", ">")
|
||||||
|
if message != "":
|
||||||
|
requests.post(HTTP_ENDPOINT, data={'subject': 'New Simple Contact message', 'message': message})
|
||||||
|
return render_template('message.html', message = "Your message was sent successfully")
|
||||||
|
else:
|
||||||
|
raise TypeError("Invalid method")
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
|
return render_template('message.html', message="Error occurred")
|
||||||
|
|
||||||
|
def captcha_get(max_tries: int = 3, ttl: int = 120, difficulty: str = "medium") -> List[str]:
|
||||||
|
""" Creates a captcha and returns [id, base64 encoded png] """
|
||||||
|
response = requests.post(f"http://rust-captcha:8000/new/{difficulty}/{max_tries}/{ttl}", headers={'X-Client-ID': 'Simple Contact'}).json()
|
||||||
|
if response["error_code"] != 0:
|
||||||
|
raise Exception(response)
|
||||||
|
return [response["result"]["id"], response["result"]["png"]]
|
||||||
|
|
||||||
|
def captcha_validate(captcha_id: str, captcha_solution: str) -> List:
|
||||||
|
""" Validates a captcha and returns [success, trials_left] """
|
||||||
|
if len(captcha_id) > 100 or len(captcha_solution) > 10:
|
||||||
|
return [False, 0]
|
||||||
|
response = requests.post(f"http://rust-captcha:8000/solution/{captcha_id}/{captcha_solution}", headers={'X-Client-ID': 'Simple Contact'}).json()
|
||||||
|
if response["error_code"] != 0:
|
||||||
|
print(f"http://rust-captcha:8000/solution/{captcha_id}/{captcha_solution}")
|
||||||
|
raise Exception(response)
|
||||||
|
if response["result"]["solution"] == "accepted":
|
||||||
|
return [True, 0]
|
||||||
|
return [False, response["result"]["trials_left"]]
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
app.run(host="0.0.0.0", debug=True, port=8888)
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Flask~=1.1.2
|
||||||
|
requests~=2.25.1
|
36
templates/index.html
Normal file
36
templates/index.html
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<title>Simple Contact</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
|
<!-- water.dark.css -->
|
||||||
|
<style>
|
||||||
|
body{font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif;line-height:1.4;max-width:800px;margin:20px auto;padding:0 10px;color:#dbdbdb;background:#202b38;text-rendering:optimizeLegibility}button,input,textarea{transition:background-color .1s linear,border-color .1s linear,color .1s linear,box-shadow .1s linear,transform .1s ease}h1{font-size:2.2em;margin-top:0}h1,h2,h3,h4,h5,h6{margin-bottom:12px}h1,h2,h3,h4,h5,h6,strong{color:#fff}b,h1,h2,h3,h4,h5,h6,strong,th{font-weight:600}blockquote{border-left:4px solid rgba(0,150,191,.67);margin:1.5em 0;padding:.5em 1em;font-style:italic}blockquote>footer{margin-top:10px;font-style:normal}address,blockquote cite{font-style:normal}a[href^=mailto]:before{content:"📧 "}a[href^=tel]:before{content:"📞 "}a[href^=sms]:before{content:"💬 "}button,input[type=button],input[type=checkbox],input[type=submit]{cursor:pointer}input:not([type=checkbox]):not([type=radio]),select{display:block}button,input,select,textarea{color:#fff;background-color:#161f27;font-family:inherit;font-size:inherit;margin-right:6px;margin-bottom:6px;padding:10px;border:none;border-radius:6px;outline:none}button,input:not([type=checkbox]):not([type=radio]),select,textarea{-webkit-appearance:none}textarea{margin-right:0;width:100%;box-sizing:border-box;resize:vertical}button,input[type=button],input[type=submit]{padding-right:30px;padding-left:30px}button:hover,input[type=button]:hover,input[type=submit]:hover{background:#324759}button:focus,input:focus,select:focus,textarea:focus{box-shadow:0 0 0 2px rgba(0,150,191,.67)}button:active,input[type=button]:active,input[type=checkbox]:active,input[type=radio]:active,input[type=submit]:active{transform:translateY(2px)}button:disabled,input:disabled,select:disabled,textarea:disabled{cursor:not-allowed;opacity:.5}::-webkit-input-placeholder{color:#a9a9a9}:-ms-input-placeholder{color:#a9a9a9}::-ms-input-placeholder{color:#a9a9a9}::placeholder{color:#a9a9a9}a{text-decoration:none;color:#41adff}a:hover{text-decoration:underline}code,kbd{background:#161f27;color:#ffbe85;padding:5px;border-radius:6px}pre>code{padding:10px;display:block;overflow-x:auto}img{max-width:100%}hr{border:none;border-top:1px solid #dbdbdb}table{border-collapse:collapse;margin-bottom:10px;width:100%}td,th{padding:6px;text-align:left}th{border-bottom:1px solid #dbdbdb}tbody tr:nth-child(2n){background-color:#161f27}::-webkit-scrollbar{height:10px;width:10px}::-webkit-scrollbar-track{background:#161f27;border-radius:6px}::-webkit-scrollbar-thumb{background:#324759;border-radius:6px}::-webkit-scrollbar-thumb:hover{background:#415c73}
|
||||||
|
|
||||||
|
#message {
|
||||||
|
height: 200px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Simple Contact</h1>
|
||||||
|
<hr />
|
||||||
|
|
||||||
|
<form action="/" method="POST">
|
||||||
|
<textarea required id="message" name="message" placeholder="Message"></textarea>
|
||||||
|
|
||||||
|
<div style="padding: 5px 0">
|
||||||
|
<img src="data:image/png;base64, {{ captcha_png }}" />
|
||||||
|
</div>
|
||||||
|
<input required type="text" name="captcha-solution" placeholder="CAPTCHA Solution">
|
||||||
|
<input type="hidden" name="captcha-id" value="{{ captcha_id }}">
|
||||||
|
<button type="submit">Validate</button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
<p>Source code available on <a target="_blank" rel="noopener noreferrer" href="https://github.com/andrewkdinh/simple-contact">GitHub</a> and <a target="_blank" rel="noopener noreferrer" href="https://gitea.andrewkdinh.com/andrewkdinh/simple-contact">Gitea</a></p>
|
||||||
|
<p>Licensed under <a target="_blank" rel="noopener noreferrer" href="https://www.gnu.org/licenses/agpl-3.0.html">AGPL 3.0</a> | Styling with <a target="_blank" rel="noopener noreferrer" href="https://github.com/kognise/water.css">water.css</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
24
templates/message.html
Normal file
24
templates/message.html
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
|
<title>Simple Contact</title>
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
|
||||||
|
<!-- water.dark.css -->
|
||||||
|
<style>
|
||||||
|
body{font-family:system-ui,-apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu,Cantarell,Fira Sans,Droid Sans,Helvetica Neue,sans-serif;line-height:1.4;max-width:800px;margin:20px auto;padding:0 10px;color:#dbdbdb;background:#202b38;text-rendering:optimizeLegibility}button,input,textarea{transition:background-color .1s linear,border-color .1s linear,color .1s linear,box-shadow .1s linear,transform .1s ease}h1{font-size:2.2em;margin-top:0}h1,h2,h3,h4,h5,h6{margin-bottom:12px}h1,h2,h3,h4,h5,h6,strong{color:#fff}b,h1,h2,h3,h4,h5,h6,strong,th{font-weight:600}blockquote{border-left:4px solid rgba(0,150,191,.67);margin:1.5em 0;padding:.5em 1em;font-style:italic}blockquote>footer{margin-top:10px;font-style:normal}address,blockquote cite{font-style:normal}a[href^=mailto]:before{content:"📧 "}a[href^=tel]:before{content:"📞 "}a[href^=sms]:before{content:"💬 "}button,input[type=button],input[type=checkbox],input[type=submit]{cursor:pointer}input:not([type=checkbox]):not([type=radio]),select{display:block}button,input,select,textarea{color:#fff;background-color:#161f27;font-family:inherit;font-size:inherit;margin-right:6px;margin-bottom:6px;padding:10px;border:none;border-radius:6px;outline:none}button,input:not([type=checkbox]):not([type=radio]),select,textarea{-webkit-appearance:none}textarea{margin-right:0;width:100%;box-sizing:border-box;resize:vertical}button,input[type=button],input[type=submit]{padding-right:30px;padding-left:30px}button:hover,input[type=button]:hover,input[type=submit]:hover{background:#324759}button:focus,input:focus,select:focus,textarea:focus{box-shadow:0 0 0 2px rgba(0,150,191,.67)}button:active,input[type=button]:active,input[type=checkbox]:active,input[type=radio]:active,input[type=submit]:active{transform:translateY(2px)}button:disabled,input:disabled,select:disabled,textarea:disabled{cursor:not-allowed;opacity:.5}::-webkit-input-placeholder{color:#a9a9a9}:-ms-input-placeholder{color:#a9a9a9}::-ms-input-placeholder{color:#a9a9a9}::placeholder{color:#a9a9a9}a{text-decoration:none;color:#41adff}a:hover{text-decoration:underline}code,kbd{background:#161f27;color:#ffbe85;padding:5px;border-radius:6px}pre>code{padding:10px;display:block;overflow-x:auto}img{max-width:100%}hr{border:none;border-top:1px solid #dbdbdb}table{border-collapse:collapse;margin-bottom:10px;width:100%}td,th{padding:6px;text-align:left}th{border-bottom:1px solid #dbdbdb}tbody tr:nth-child(2n){background-color:#161f27}::-webkit-scrollbar{height:10px;width:10px}::-webkit-scrollbar-track{background:#161f27;border-radius:6px}::-webkit-scrollbar-thumb{background:#324759;border-radius:6px}::-webkit-scrollbar-thumb:hover{background:#415c73}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<p>{{ message }}</p>
|
||||||
|
{% if attempts_left %}
|
||||||
|
<p>You have {{ attempts_left }} attempt(s) left</p>
|
||||||
|
<input type="button" value="Go back" onclick="window.history.back()" />
|
||||||
|
{% elif attempts_left == 0 %}
|
||||||
|
<p>Captcha is now invalid (reload page after you go back)</p>
|
||||||
|
<input type="button" value="Go back" onclick="window.history.back()" />
|
||||||
|
{% endif %}
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user