-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
162 lines (115 loc) · 4.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import logging
import os
import secrets
import threading
import requests
import socket
import sys
from db.database import *
from datetime import datetime
from utils import *
from email_utils import *
from flask_mail import Mail, Message
from flask import Flask, jsonify, flash, request, send_file,render_template
app = Flask(__name__)
database = EmailDatabase()
app.config.from_object(__name__)
secret = secrets.token_urlsafe(32)
file_handler = logging.FileHandler(filename='app.log')
stdout_handler = logging.StreamHandler(sys.stdout)
handlers = [file_handler, stdout_handler]
logging.basicConfig(
level=logging.DEBUG,
format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
handlers=handlers
)
logger = logging.getLogger('LOGGER_NAME')
app.secret_key = secret
notify_email,notify_password,notify_stmp_server = get_options(logging)
app.config.update(
DEBUG=True,
#EMAIL SETTINGS
MAIL_SERVER=notify_stmp_server,
MAIL_PORT=465,
MAIL_USE_SSL=True,
MAIL_USERNAME = notify_email,
MAIL_PASSWORD = notify_password
)
mail = Mail(app)
def get_my_ip():
return request.remote_addr
def send_on_open_email(sender, receiver, ip, tracking_code,counter):
try:
msg = Message(f"PyTracker - {receiver} has opened the email.",
sender=notify_email,
recipients=[sender])
current_time = datetime.now().strftime("%H:%M:%S")
msg.html = render_template(modify_current_html_email(receiver,ip,current_time,counter))
mail.send(msg)
app.logger.warning('Mail sent!')
except Exception as e:
print(e)
app.logger.warning(e)
@app.route("/add_data")
def add_data():
return database.add_sample_data()
@app.route("/get_data")
def get_data():
return database.get_data()
@app.route("/")
def show_settings_home_menu():
return render_template(get_current_html_template())
def process_row(row,tracking_code,ip):
counter = int(row[2])
print("The counter is : " + str(counter))
if counter > 1:
sender = row[0]
receiver = row[1]
send_on_open_email(sender, receiver,ip,tracking_code,counter)
else:
print("Receiver is the sender as the counter is 1: " + str(row[2]))
@app.route("/image", methods=["GET"])
def render_image():
tracking_code = int(request.args.get('tracking_code'))
app.logger.warning("Someone has opened the email with this tracking code {}".format(tracking_code))
ip = get_my_ip()
my_ip = get_local_ip()
app.logger.info("PyTracker server IP is {0} and receiver ip is {1}".format(my_ip,ip))
if str(ip) != str(my_ip):
app.logger.info("Searching for tracking_code {0} in csv file".format(tracking_code))
found,row = database.find_row_by_tracking_code(tracking_code)
if found:
app.logger.warning("Tracking code {} was found".format(tracking_code))
process_row(row,tracking_code,ip)
else:
app.logger.warning("Tracking code {} is lost and can't be tracked".format(tracking_code))
return send_file('sprites/pi.png', mimetype='image/gif')
else:
app.logger.warning("Receiver is the sender as they have the same IP")
return send_file('sprites/pi.png', mimetype='image/gif')
@app.route("/get_tracking_code_for_email")
def get_tracking_code_for_email():
sender = request.args.get('sender')
receiver = request.args.get('receiver')
#this part creates a random tracking code for current email and returns the html code to inject on the email.
if sender and receiver:
if check(sender) and check(receiver):
app.logger.warning("Creating tracking code for {0} and {1}".format(sender,receiver))
tracking_code = create_tracking_code() #Creates tracking code for current email.
logging.warning(f'{sender}, {receiver}')
url = request.url_root
html_code = f'<img src={url}image?tracking_code={tracking_code}></img>'
database.write_data(sender, receiver, 0,tracking_code) #writes the data so it can be recall in the future if server has been shutdown for example.
app.logger.warning("Current tracking code: " + tracking_code)
return jsonify(status_code="200",
inject_code=html_code,tracking_code=tracking_code)
else:
return jsonify(status_code="500")
else:
return jsonify(status_code="404")
if __name__ == "__main__":
try:
port = int(os.environ.get('PORT', 5000))
app.run(host="0.0.0.0", port=port)
except:
logging.exception('error')