Skip to content

About

Networking and Distributed System Programming Project

Topics

Resources

Contributing

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

πŸ“ Distributed File-Sharing System

A distributed file-sharing application built with Python that demonstrates data consistency and replication strategies in distributed systems. Authenticated users can upload, download, list, rename, and delete files from a primary server that automatically replicates data to a replica server with transparent failover.

Course: Networking & Distributed Systems Programming β€” Group 4

Python Flask License


✨ Features

  • JWT Authentication β€” Secure registration, login, and session management with 30-minute token expiry
  • File Operations β€” Upload, download, rename, delete, and list files through a web interface
  • Automatic Replication β€” Every upload is synchronously replicated from primary to replica server
  • Transparent Failover β€” Read requests automatically reroute to the replica when the primary is down
  • Storage Quotas β€” Per-user quota enforcement (50 MB default) prevents abuse
  • Shareable Links β€” Generate public, private, or user-specific sharing links
  • Duplicate Handling β€” Automatic rename (report.pdf β†’ report_1.pdf) on filename conflicts

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                       CLIENT ZONE                           β”‚
β”‚   [Browser 1]         [Browser 2]         [Browser N]       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                            β”‚ HTTP (port 5000)
                            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      SERVER ZONE (LAN)                      β”‚
β”‚                                                             β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚   β”‚         Flask Web App (app.py)           β”‚              β”‚
β”‚   β”‚     auth.py  Β·  templates/  Β·  JWT       β”‚              β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚           β”‚ TCP :9000                β”‚ Failover TCP :9001    β”‚
β”‚           β–Ό                          β–Ό                      β”‚
β”‚   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”‚
β”‚   β”‚ Primary Server │──────▢ β”‚ Replica Server β”‚              β”‚
β”‚   β”‚  :9000         β”‚ Repl.  β”‚  :9001         β”‚              β”‚
β”‚   β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β””β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜              β”‚
β”‚           β–Ό                         β–Ό                       β”‚
β”‚     /shared_files/            /shared_files/                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quickstart

Prerequisites

  • Python 3.12+ installed on all server machines
  • All machines on the same Local Area Network (Wi-Fi or Ethernet)
  • A modern web browser (Chrome, Firefox, Edge)

1. Clone & Install

git clone https://github.com/QweciKuranchie/file-sharing-system.git
cd file-sharing-system
pip install -r requirements.txt

2. Configure (Optional)

Set environment variables to override defaults, or edit config.py:

Variable Default Description
JWT_SECRET_KEY Random per-process Secret for signing JWT tokens
PRIMARY_HOST 127.0.0.1 Primary server IP address
PRIMARY_PORT 9000 Primary server TCP port
REPLICA_HOST 127.0.0.1 Replica server IP address
REPLICA_PORT 9001 Replica server TCP port
FLASK_PORT 5000 Flask HTTP port
TCP_CLIENT_SECRET default-test-client-secret-12345 Secret for client and Flask app TCP connections
TCP_REPLICATION_SECRET default-test-replication-secret-67890 Secret for replication module TCP connections
SECRET_KEY dev-secret-key-67890 (Dev) Secret key for Flask session cookie signatures

3. Start the Servers

Start each component in a separate terminal. Order matters β€” start the replica first.

Development Mode:

By default, the application runs in development mode and uses hardcoded test secrets.

# Terminal 1 β€” Replica Server
python replica_server.py

# Terminal 2 β€” Primary Server
python primary_server.py

# Terminal 3 β€” Flask Web App
python app.py

Production Mode:

In production, you must set ENV=production or FLASK_ENV=production along with custom secrets:

# Terminal 1 β€” Replica Server
export ENV=production
export TCP_CLIENT_SECRET="your-secure-client-secret"
export TCP_REPLICATION_SECRET="your-secure-replication-secret"
python replica_server.py

# Terminal 2 β€” Primary Server
export ENV=production
export TCP_CLIENT_SECRET="your-secure-client-secret"
export TCP_REPLICATION_SECRET="your-secure-replication-secret"
python primary_server.py

# Terminal 3 β€” Flask Web App
export ENV=production
export SECRET_KEY="your-secure-session-key"
export TCP_CLIENT_SECRET="your-secure-client-secret"
python app.py

4. Open the App

Navigate to http://<flask-machine-ip>:5000 in your browser.


πŸ“‚ Project Structure

file-sharing-system/
β”œβ”€β”€ app.py                  ← Flask web application & HTTP routing
β”œβ”€β”€ auth.py                 ← JWT authentication & user management
β”œβ”€β”€ config.py               ← Centralised configuration
β”œβ”€β”€ database.py             ← SQLite schema initialisation & connection helpers
β”œβ”€β”€ primary_server.py       ← Primary TCP file server
β”œβ”€β”€ replica_server.py       ← Replica TCP file server
β”œβ”€β”€ replication.py          ← Replication & failover logic
β”œβ”€β”€ requirements.txt        ← Python dependencies
β”œβ”€β”€ test_auth.py            ← Auth module test suite
β”œβ”€β”€ shared_files/           ← Uploaded file storage (gitignored)
β”œβ”€β”€ templates/              ← HTML templates for the web UI
β”‚   β”œβ”€β”€ login.html
β”‚   β”œβ”€β”€ register.html
β”‚   β”œβ”€β”€ dashboard.html
β”‚   └── profile.html
└── docs/                   ← Project documentation
    β”œβ”€β”€ SRS_FileSharing_Group4.md
    β”œβ”€β”€ Protocol_Spec_Group4.md
    └── Architecture_Diagram_Group4.md

πŸ—„οΈ Data Model

users

Column Type Notes
id INTEGER Primary key, auto-increment
username TEXT Unique
email TEXT Unique, stored lowercase
password_hash TEXT Werkzeug scrypt/pbkdf2 hash
quota_limit_bytes INTEGER Default 52 428 800 (50 MB)
quota_used_bytes INTEGER Default 0
created_at TEXT UTC ISO 8601 timestamp (YYYY-MM-DDTHH:MM:SSZ)

files

Column Type Notes
id INTEGER Primary key, auto-increment
filename TEXT Name on disk (may be renamed for duplicates)
original_name TEXT Name the user uploaded
file_type TEXT File extension / MIME category
file_size_bytes INTEGER Size in bytes
uploaded_at TEXT UTC ISO 8601 timestamp (YYYY-MM-DDTHH:MM:SSZ)
owner_id INTEGER FK β†’ users.id, cascade delete

πŸ§ͺ Running Tests

pip install pytest
python -m pytest test_auth.py -v

Expected: 32 tests passed covering database init, registration, password hashing, login, JWT validation, and quota management.


πŸ“‘ API Overview

Authentication

Method Endpoint Description
POST /register Create a new user account
POST /login Authenticate and receive JWT session
GET /logout Clear session and redirect to login

File Operations

Method Endpoint Description
GET /dashboard Main UI with file list
POST /upload Upload a file (max 10 MB)
GET /download/<filename> Download a file
POST /delete/<filename> Delete a file
POST /rename/<filename> Rename a file
GET /files JSON list of files
GET /profile User profile & quota usage

Sharing

Method Endpoint Description
POST /share/<filename> Generate a shareable link
GET /shared/<token> Access a shared file
GET /shared-with-me List files shared with you

Full protocol specification: docs/Protocol_Spec_Group4.md


βš™οΈ Technology Stack

Layer Technology Purpose
Client HTML + CSS + JS Web UI in the browser
Web Framework Flask HTTP routing, templates, sessions
Authentication PyJWT + Werkzeug JWT tokens + password hashing
Database SQLite User accounts & file metadata
Server Comms Python socket TCP between Flask ↔ Primary ↔ Replica
Concurrency Python threading Handle multiple clients simultaneously

πŸ“„ License

This project is licensed under the MIT License.

About

Networking and Distributed System Programming Project

Topics

Resources

Contributing

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages