fabriziosalmi / blacklists

Hourly updated domains blacklist 🚫
https://github.com/fabriziosalmi/blacklists/releases/download/latest/blacklist.txt
GNU General Public License v3.0
141 stars 6 forks source link

Firefox extension #24

Closed fabriziosalmi closed 1 year ago

fabriziosalmi commented 1 year ago

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

  1. Manifest file (manifest.json):

The manifest file contains metadata about the extension, like its name and permissions it requires.

{
  "manifest_version": 3,
  "name": "Blacklist Checker",
  "version": "1.0",
  "description": "Checks if a domain is blacklisted.",
  "permissions": ["activeTab", "webRequest", "storage"],
  "background": {
    "service_worker": "background.js"
  },
  "browser_action": {
    "default_popup": "popup.html",
    "default_icon": {
      "48": "icon.png"
    }
  }
}
  1. Background Script (background.js):

This script runs in the background and can be used to communicate with your server, checking if a domain is blacklisted.

browser.browserAction.onClicked.addListener((tab) => {
    const currentDomain = new URL(tab.url).hostname;
    // Here, you can send the domain to your server to check if it's blacklisted
    fetch('http://your_server_endpoint/', {
        method: 'POST',
        body: JSON.stringify({ domain: currentDomain }),
        headers: {
            'Content-Type': 'application/json'
        }
    })
    .then(response => response.json())
    .then(data => {
        if (data.blacklisted) {
            // Handle domain being blacklisted
            alert(`${currentDomain} is blacklisted!`);
        } else {
            // Handle domain not being blacklisted
            alert(`${currentDomain} is not blacklisted.`);
        }
    });
});
  1. Popup (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.

<!DOCTYPE html>
<html>
<head>
    <title>Blacklist Checker</title>
</head>
<body>
    <button id="checkButton">Check if Blacklisted</button>

    <script src="popup.js"></script>
</body>
</html>
  1. Popup Script (popup.js):

This script will run when the popup is opened and handle the button click.

document.getElementById("checkButton").addEventListener("click", function() {
    browser.runtime.sendMessage({ action: "checkDomain" });
});

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:

  1. Place all your extension files (manifest.json, background.js, popup.html, popup.js, and any icons) in a folder.
  2. Load the folder into Firefox by navigating to about:debugging, clicking "This Firefox", then "Load Temporary Add-on", and select any file in your extension's directory.
  3. You should now see your extension's icon in the Firefox toolbar. Click on it and test the functionality.

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

fabriziosalmi commented 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.

Backend Server Setup:

  1. Setup Environment:

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
  1. Backend Code (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)
  1. Database Setup:

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()

Running the Server:

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: