Closed fabriziosalmi closed 1 year ago
Sure, I'll provide a simple backend using Flask, which will check a domain against an SQLite database containing the blacklisted domains.
First, if you haven't already, set up a virtual environment:
python3 -m venv env
source env/bin/activate
Install the necessary packages:
pip install Flask flask_sqlalchemy
server.py
):This code sets up a Flask app with an SQLite database and provides an endpoint to check if a domain is blacklisted.
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blacklist.db'
db = SQLAlchemy(app)
# Define the Blacklist model
class Blacklist(db.Model):
id = db.Column(db.Integer, primary_key=True)
domain = db.Column(db.String(150), unique=True, nullable=False)
@app.route('/check_domain', methods=['POST'])
def check_domain():
data = request.get_json()
if not data or 'domain' not in data:
return jsonify({'error': 'Domain not provided'}), 400
domain = data['domain']
blacklisted = Blacklist.query.filter_by(domain=domain).first()
return jsonify({'blacklisted': bool(blacklisted)})
if __name__ == '__main__':
# Initialize the database (create it if it doesn't exist)
db.create_all()
app.run(port=5000, debug=True)
Before running the server for the first time, you might want to populate the database with some blacklisted domains. Here's how you can add a few for testing:
# At the end of the server.py file, add:
if __name__ == '__main__':
# ... existing code ...
# Sample blacklisted domains for testing
sample_domains = ["badexample.com", "malicious.net", "phishy.org"]
# Add these to the database if they don't exist
for domain in sample_domains:
if not Blacklist.query.filter_by(domain=domain).first():
db.session.add(Blacklist(domain=domain))
db.session.commit()
Execute server.py
to start the Flask server:
python server.py
Your server should now be running on http://localhost:5000/
. You can test the endpoint by sending a POST request with a JSON payload containing a domain to the /check_domain
endpoint. If the domain is in the sample list, it will return {"blacklisted": true}
, otherwise {"blacklisted": false}
.
Remember, for a production deployment, you would:
debug=True
flag from app.run()
.
Creating a Firefox extension to check if a domain is blacklisted requires a different approach, but it's definitely feasible. Below is a basic outline of how you can achieve this:
1. Extension Setup
manifest.json
):The manifest file contains metadata about the extension, like its name and permissions it requires.
background.js
):This script runs in the background and can be used to communicate with your server, checking if a domain is blacklisted.
popup.html
):This is the UI that shows when you click on the extension icon. For simplicity, we'll just display a button the user can press to check the current domain.
popup.js
):This script will run when the popup is opened and handle the button click.
2. Backend Server:
Set up a server (e.g., Flask, Express.js) to receive requests from the extension and check if a domain is blacklisted.
For brevity, I won't include the backend code here, but it would essentially accept a domain as input, query your blacklist database, and then respond with whether or not the domain is blacklisted.
3. Package and Test the Extension:
manifest.json
,background.js
,popup.html
,popup.js
, and any icons) in a folder.about:debugging
, clicking "This Firefox", then "Load Temporary Add-on", and select any file in your extension's directory.4. Distribution:
Once you're satisfied with the extension's functionality, you can submit it to the Firefox Add-ons website (AMO) for others to use.
Keep in mind that you'll have to ensure the security and privacy of your users, especially if you're