Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3fb58a5
File upload, encryption, download, decryption, start, pause and resume
Frahane Mar 23, 2024
b51ef38
Merge branch 'NESS-Network:master' into master
Frahane Mar 23, 2024
2d531c0
improved for block to block encryption
Frahane Mar 24, 2024
c874bd7
Web client for displaying node list
Frahane Apr 15, 2024
5f275b5
Merge branch 'NESS-Network:master' into master
Frahane Apr 15, 2024
500ba3e
Having issue with key generation in KeyManager.py while web client to…
Frahane Apr 17, 2024
a21894c
Change BlockchainRPC to BlockchainRpcKey in KeyManager; update return…
Frahane Apr 18, 2024
eae31da
Merge branch 'NESS-Network:master' into master
Frahane Apr 18, 2024
f022d62
Merge branch 'NESS-Network:master' into master
Frahane Apr 20, 2024
242031a
Merge branch 'NESS-Network:master' into master
Frahane Apr 21, 2024
9e2639f
Merge branch 'NESS-Network:master' into master
Frahane Apr 27, 2024
7e33392
Merge branch 'NESS-Network:master' into master
Frahane Apr 28, 2024
3f2109f
Merge branch 'NESS-Network:master' into master
Frahane Oct 28, 2024
57358df
Improvement
Frahane May 5, 2025
6a2a671
Merge branch 'master' of https://github.com/Frahane/PrivatenessTools
Frahane May 5, 2025
3665d2a
Issues
Frahane May 6, 2025
84ef8f7
Adding init
Frahane May 6, 2025
ff5f0bc
Fixing Issues
Frahane May 7, 2025
3435008
Modules importation Issue is fixed
Frahane May 15, 2025
6800bab
Merge branch 'NESS-Network:master' into master
Frahane May 19, 2025
46dfa80
Merge branch 'NESS-Network:master' into master
Frahane May 19, 2025
94b239d
Merge branch 'NESS-Network:master' into master
Frahane May 24, 2025
9be4dea
Rebuilding App
Frahane May 27, 2025
a7c8ddc
Merge branch 'NESS-Network:master' into master
Frahane May 27, 2025
c92c726
Merge branch 'NESS-Network:master' into master
Frahane May 27, 2025
63de604
Merge branch 'NESS-Network:master' into master
Frahane Jun 2, 2025
0c895a2
Fixing user.py sel
Frahane Jun 2, 2025
a9febc1
Merge branch 'master' of https://github.com/Frahane/PrivatenessTools
Frahane Jun 2, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added EncryptDecrypt/__init__.py
Empty file.
346 changes: 346 additions & 0 deletions EncryptDecrypt/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,346 @@
import sys
import os
import threading
import json
from flask import Flask, render_template, request, redirect, url_for, flash, jsonify, send_file
import subprocess
# extend PYTHONPATH to project root
dir_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
sys.path.insert(0, dir_root)

# Import container for dependency injection
from framework.Container import Container
# Import key management and cryptors from NessKeys
from NessKeys.KeyManager import KeyManager
from services.node import node as NodesUpdater
from NessKeys.cryptors.Aes import Aes as AESCipher
from NessKeys.cryptors.Salsa20 import Salsa20 as Salsa20Cipher
from NessKeys.JsonChecker.Checker import JsonChecker

# Initialize Flask app
def create_app():
app = Flask(__name__)
# Configuration
app.config.from_mapping(
SECRET_KEY=os.environ.get('FLASK_SECRET', 'supersecret'),
UPLOAD_FOLDER=os.environ.get('UPLOAD_FOLDER', os.path.join(dir_root, 'uploads')),
NODES_JSON=os.environ.get('NODES_JSON', os.path.join(dir_root, 'nodes.json')),
MAX_CONTENT_LENGTH=16 * 1024 * 1024 # 16MB limit
)

# Ensure upload folder exists
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)

# Thread-safe storage for background tasks
tasks_lock = threading.Lock()
running_tasks = {}

# Helpers
def get_km():
return Container.KeyManager()
filemgr = Container.FileManager()

# ----- Routes -----

@app.route('/')
def index():
return render_template('index.html')

@app.route('/keygen', methods=['GET', 'POST'])
def keygen():
if request.method == 'GET':
return render_template('keygen.html')
username = request.form['username']
entropy = int(request.form.get('entropy', 256))
try:
km = get_km()
km.createUsersKey(username, entropy)
flash(f'User key generated: {username}', 'success')
except Exception as e:
flash(f'Error generating key: {e}', 'danger')
return redirect(url_for('keygen'))

@app.route('/nodes', methods=['GET', 'POST'])
def nodes():
km = get_km()
if request.method == 'POST':
mode = request.form['mode']
try:
if mode == 'public':
NodesUpdater.fetch_public_list()
flash('Public list fetched', 'success')
else:
rpc = {
'host': request.form['rpc_host'],
'port': request.form['rpc_port'],
'user': request.form['rpc_user'],
'password': request.form['rpc_password']
}
NodesUpdater.update_blockchain(rpc, app.config['NODES_JSON'])
flash('Updated from RPC', 'success')
except Exception as e:
flash(f'Error managing nodes: {e}', 'danger')
return redirect(url_for('nodes'))
nodes = km.listNodes()
return render_template('nodes.html', nodes=nodes)

@app.route('/node', methods=['POST'])
def select_node():
"""Mark a node as selected in the nodes.json file, creating it if missing."""
node_url = request.form['node_url']
nodes_file = app.config['NODES_JSON']
try:
# Load or initialize nodes JSON
if not os.path.exists(nodes_file):
data = {'nodes': [], 'selected_node': node_url}
else:
with open(nodes_file, 'r') as f:
try:
data = json.load(f)
except json.JSONDecodeError:
data = {}
# Ensure dict
if not isinstance(data, dict):
data = {'nodes': data}
data['selected_node'] = node_url
# Write back
with open(nodes_file, 'w') as f:
json.dump(data, f, indent=2)
flash(f'Node selected: {node_url}', 'success')
except Exception as e:
flash(f'Error selecting node: {e}', 'danger')
return redirect(url_for('nodes'))

@app.route('/user', methods=['GET', 'POST'])
def user():
# POST: perform sel | nvs | worm via user.py
if request.method == 'POST':
username = request.form.get('username', '').strip()
action = request.form.get('action', '').strip()

if not username:
flash('Please select a user first.', 'warning')
return redirect(url_for('user'))

if action not in ('sel', 'nvs', 'worm'):
flash('Invalid action.', 'danger')
return redirect(url_for('user'))

cli = os.path.join(dir_root, 'user.py')
cmd = [sys.executable, cli, action, username]

try:
result = subprocess.run(
cmd,
cwd=dir_root,
capture_output=True,
text=True,
check=True
)
# Use stdout if available, else fallback to a canned message
out = result.stdout.strip() or f'User {action} completed for {username}.'
flash(out, 'success')
except subprocess.CalledProcessError as e:
err = e.stderr.strip() or e.stdout.strip() or str(e)
flash(f'Error ({action}): {err}', 'danger')

return redirect(url_for('user'))

# GET: fetch users via `user.py ls`
users = []
cli = os.path.join(dir_root, 'user.py')
try:
result = subprocess.run(
[sys.executable, cli, 'ls'],
cwd=dir_root,
capture_output=True,
text=True,
check=True
)
for line in result.stdout.splitlines():
line = line.strip()
# Lines that start with '|' and contain real content
if line.startswith('|') and not set(line.strip()).issubset({'|','-'}):
name = line.strip('|').strip()
# strip CLI arrows: ==> user <==
name = name.replace('==>', '').replace('<==', '').strip()
if name.lower() != 'username':
users.append(name)
except subprocess.CalledProcessError as e:
flash('Failed to fetch users. Please ensure user.py is working.', 'danger')

return render_template('user.html', users=users)

@app.route('/publish', methods=['GET', 'POST'])
def publish():
if request.method == 'GET':
return render_template('publish.html')
action = request.form['action']
try:
km = get_km()
if action == 'user':
km.prepareUserNVS()
elif action == 'worm':
km.prepareWormNVS()
elif action == 'nodes':
km.prepareNodesNVS()
flash('Prepared NVS records', 'success')
except Exception as e:
flash(f'Error preparing NVS: {e}', 'danger')
return redirect(url_for('publish'))

# File system operations
@app.route('/upload', methods=['POST'])
def upload():
file = request.files['file']
dest = request.form.get('destination', '')
try:
filemgr.upload(file, dest)
flash('Upload successful', 'success')
except Exception as e:
flash(f'Error uploading: {e}', 'danger')
return redirect(url_for('index'))

@app.route('/download')
def download():
path = request.args.get('path')
try:
return filemgr.download(path)
except Exception as e:
flash(f'Error downloading: {e}', 'danger')
return redirect(url_for('index'))

@app.route('/mkdir', methods=['POST'])
def mkdir():
path = request.form['path']
try:
filemgr.mkdir(path)
flash('Directory created', 'success')
except Exception as e:
flash(f'Error: {e}', 'danger')
return redirect(url_for('index'))

@app.route('/rmdir', methods=['POST'])
def rmdir():
path = request.form['path']
try:
filemgr.rmdir(path)
flash('Directory removed', 'success')
except Exception as e:
flash(f'Error: {e}', 'danger')
return redirect(url_for('index'))

@app.route('/ls')
def ls():
path = request.args.get('path', '')
try:
return jsonify(filemgr.ls(path))
except Exception as e:
return jsonify({'error': str(e)})

@app.route('/move', methods=['POST'])
def move():
src = request.form['src']
dst = request.form['dst']
try:
filemgr.move(src, dst)
flash('Move successful', 'success')
except Exception as e:
flash(f'Error: {e}', 'danger')
return redirect(url_for('index'))

@app.route('/remove', methods=['POST'])
def remove():
path = request.form['path']
try:
filemgr.remove(path)
flash('Removed', 'success')
except Exception as e:
flash(f'Error: {e}', 'danger')
return redirect(url_for('index'))

@app.route('/tree')
def tree():
root = request.args.get('root', '')
try:
return jsonify(filemgr.tree(root))
except Exception as e:
return jsonify({'error': str(e)})

@app.route('/quota')
def quota():
try:
return jsonify(filemgr.quota())
except Exception as e:
return jsonify({'error': str(e)})

@app.route('/fileinfo')
def fileinfo():
path = request.args.get('path')
try:
return jsonify(filemgr.fileinfo(path))
except Exception as e:
return jsonify({'error': str(e)})

# Encryption/Decryption
@app.route('/encrypt', methods=['GET', 'POST'], endpoint='encrypt_route')
def encrypt():
if request.method == 'GET':
return render_template('encrypt.html')
algorithm = request.form['algorithm']
file = request.files['file']
km = get_km()
key = km.getCurrentKey() # uses selected user context
cipher = AESCipher(key) if algorithm == 'aes' else Salsa20Cipher(key)
infile = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(infile)
outfile = cipher.encrypt_file(infile)
metadata = cipher.get_metadata()
return render_template('encrypt_result.html', filename=os.path.basename(outfile), metadata=metadata)

@app.route('/decrypt', methods=['GET', 'POST'], endpoint='decrypt_route')
def decrypt():
if request.method == 'GET':
return render_template('decrypt.html')
algorithm = request.form['algorithm']
key_hex = request.form['key']
nonce = request.form.get('nonce')
iv = request.form.get('iv')
file = request.files['file']
key = bytes.fromhex(key_hex)
cipher = AESCipher(key, iv=bytes.fromhex(iv)) if algorithm == 'aes' else Salsa20Cipher(key, nonce=bytes.fromhex(nonce))
infile = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(infile)
outfile = cipher.decrypt_file(infile)
return render_template('decrypt_result.html', filename=os.path.basename(outfile))

# Background tasks
@app.route('/task/<task_id>/start', methods=['POST'])
def task_start(task_id):
with tasks_lock:
running_tasks[task_id] = {'status': 'running', 'progress': 0}
return jsonify(running_tasks[task_id])

@app.route('/task/<task_id>/pause', methods=['POST'])
def task_pause(task_id):
with tasks_lock:
if task_id in running_tasks:
running_tasks[task_id]['status'] = 'paused'
return jsonify(running_tasks.get(task_id, {}))

@app.route('/task/<task_id>/resume', methods=['POST'])
def task_resume(task_id):
with tasks_lock:
if task_id in running_tasks:
running_tasks[task_id]['status'] = 'running'
return jsonify(running_tasks.get(task_id, {}))

@app.route('/task/<task_id>/progress', methods=['GET'])
def task_progress(task_id):
return jsonify(running_tasks.get(task_id, {'status': 'unknown', 'progress': 0}))

return app

if __name__ == '__main__':
app = create_app()
app.run(host='0.0.0.0', port=5000, debug=True)
Loading