-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfraud_browser.py
183 lines (132 loc) · 5.7 KB
/
fraud_browser.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import os
import json
import pandas as pd
from pyngrok import ngrok
from dotenv import load_dotenv
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_required, logout_user, current_user, login_user
from flask import Flask, render_template, redirect, url_for, request, send_file
from werkzeug.security import generate_password_hash, check_password_hash
import stripe
STRIPE_API_VERSION = "2020-08-27"
load_dotenv()
os.chdir(os.path.dirname(os.path.abspath(__file__)))
basedir = os.path.abspath(os.path.dirname(__file__))
db_path = os.path.join(basedir, 'users.db')
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.config['SECRET_KEY'] = os.getenv('DB_SECRET_KEY')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + db_path
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
session = db.session
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(15), unique=True)
password = db.Column(db.String(80))
@login_manager.user_loader
def load_user(user_id):
return db.session.get(User, int(user_id))
@app.route('/', methods=['GET', 'POST'])
def login():
if current_user.is_authenticated:
return redirect(url_for('fraud'))
return render_template('login.html')
@app.route('/login_handler', methods=['POST'])
def login_handler():
if current_user.is_authenticated:
return redirect(url_for('fraud'))
username = request.form.get('username')
password = request.form.get('password')
user = User.query.filter_by(username=username).first()
if user and check_password_hash(user.password, password):
login_user(user, remember=True)
return redirect(url_for('fraud'))
return '<h1>Invalid username or password!</h1>', 401
@app.route('/signup_handler', methods=['POST'])
def signup_handler():
username = request.form.get('username')
password = generate_password_hash(request.form.get('password'), method='scrypt')
new_user = User(username=username, password=password)
db.session.add(new_user)
db.session.commit()
return redirect(url_for('login'))
@app.route('/download')
@login_required
def download_file():
p = f"csvs/{current_user.username}_fingerprints.csv"
return send_file(p, as_attachment=True)
@app.route('/fraud', methods=['GET', 'POST'])
@login_required
def fraud():
return render_template('fraud.html')
@app.route('/fraud_handler', methods=['GET', 'POST'])
@login_required
def fraud_handler():
stripe.api_key = os.getenv('STRIPE_TOKEN')
stripe.api_version = STRIPE_API_VERSION
# Use sets for faster existence checking
spaces = set()
checked_spaces = set()
fingerprints = set()
checked_fingerprints = set()
final_dict = {}
def search_and_retrieve(space_id=None, fingerprint=None):
if space_id is not None:
query = f"metadata['space_id']:'{space_id}'"
else: # If space_id is not provided, fingerprint must be
query = f"payment_method_details.card.fingerprint:'{fingerprint}'"
charge_list = stripe.Charge.search(query=query)
for charge in charge_list.auto_paging_iter():
if len(charge.metadata) != 0 and 'space_id' in charge.metadata:
current_space_id = charge.metadata['space_id']
current_fingerprint = charge.payment_method_details.card.fingerprint
if current_space_id not in final_dict:
final_dict[current_space_id] = set()
if current_fingerprint not in final_dict[current_space_id]:
fingerprints.add(current_fingerprint)
final_dict[current_space_id].add(current_fingerprint)
if current_space_id not in spaces:
spaces.add(current_space_id)
# Take a cc fingerprint via input
first_fingerprint = request.form.get('fingerprint')
if len(first_fingerprint) != 0:
search_and_retrieve(fingerprint=first_fingerprint)
while spaces > checked_spaces or fingerprints > checked_fingerprints:
for space in spaces - checked_spaces:
checked_spaces.add(space)
search_and_retrieve(space_id=space)
for fingerprint in fingerprints - checked_fingerprints:
checked_fingerprints.add(fingerprint)
search_and_retrieve(fingerprint=fingerprint)
d = [(f"https://internal.signalwire.com/spaces/{space}", list(final_dict[space])) for space in final_dict]
user_name = current_user
df = pd.DataFrame(d, columns=('Space', 'fingerprint'))
df.to_csv(f'csvs/{user_name.username}_fingerprints.csv', index=False, encoding='utf-8')
return results(user_name)
@app.route('/results', methods=['GET', 'POST'])
@login_required
def results(user_name):
data = pd.read_csv(f'csvs/{user_name.username}_fingerprints.csv')
users = User.query.all()
user_csv = data.to_json()
user_csv = json.loads(user_csv)
return render_template('results.html', tables=[data.to_html()], titles=[''], users=users, json=user_csv)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect(url_for('login'))
def start_ngrok():
url = ngrok.connect(5000).public_url
print(' * Tunnel URL:', url)
if __name__ == '__main__':
with app.app_context():
db.create_all()
if os.environ.get('WERKZEUG_RUN_MAIN') != 'true':
start_ngrok()
app.run(debug=True)