# Path: hashview/models.py
# class Rules(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     last_updated = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     path = db.Column(db.String(256), nullable=False)
#     size = db.Column(db.Integer, nullable=False, default=0)
#     checksum = db.Column(db.String(64), nullable=False)
# 
# class Tasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)
#     hc_attackmode = db.Column(db.String(25), nullable=False) # dictionary, mask, bruteforce, combinator
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     wl_id = db.Column(db.Integer)
#     rule_id = db.Column(db.Integer)
#     hc_mask = db.Column(db.String(50))
# 
# class Jobs(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     status = db.Column(db.String(20), nullable=False)           # Running, Paused, Completed, Queued, Canceled, Ready, Incomplete
#     started_at = db.Column(db.DateTime, nullable=True)          # These defaults should be changed
#     ended_at = db.Column(db.DateTime, nullable=True)            # These defaults should be changed
#     hashfile_id = db.Column(db.Integer, nullable=True)
#     customer_id = db.Column(db.Integer, db.ForeignKey('customers.id'), nullable=False)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
# 
# class JobTasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     job_id = db.Column(db.Integer, nullable=False)
#     task_id = db.Column(db.Integer, nullable=False)
#     command = db.Column(db.String(1024))                  
#     status = db.Column(db.String(50), nullable=False)       # Running, Paused, Not Started, Completed, Queued, Canceled, Importing
#     agent_id = db.Column(db.Integer, db.ForeignKey('agents.id'))
# 
# class Users(db.Model, UserMixin):
#     id = db.Column(db.Integer, primary_key=True)
#     first_name = db.Column(db.String(20), nullable=False)
#     last_name = db.Column(db.String(20), nullable=False)
#     email_address = db.Column(db.String(50), unique=True, nullable=False)
#     password = db.Column(db.String(60), nullable=False)
#     admin = db.Column(db.Boolean, nullable=False, default=False)
#     pushover_app_id = db.Column(db.String(50), nullable=True)
#     pushover_user_key = db.Column(db.String(50), nullable=True)
#     wordlists = db.relationship('Wordlists', backref='tbd', lazy=True)
#     rules = db.relationship('Rules', backref='owner', lazy=True)
#     jobs = db.relationship('Jobs', backref='owner', lazy=True)
#     tasks = db.relationship('Tasks', backref='owner', lazy=True)
#     taskgroups = db.relationship('TaskGroups', backref='owner', lazy=True)
# 
#     def get_reset_token(self, expires_sec=1800):
#         s = Serializer(Config.SECRET_KEY, expires_sec)
#         return s.dumps({'user_id': self.id}).decode('utf-8')
# 
#     @staticmethod
#     def verify_reset_token(token):
#         s = Serializer(Config.SECRET_KEY)
#         try:
#             user_id = s.loads(token)['user_id']
#         except:
#             return None     
#         return Users.query.get(user_id)
# 
# Path: hashview/rules/forms.py
# class RulesForm(FlaskForm):
#     name = StringField('Name', validators=([DataRequired()]))
#     rules = FileField('Upload Rules')
#     submit = SubmitField('upload')  
# 
# Path: hashview/utils/utils.py
# def save_file(path, form_file):
#     random_hex = secrets.token_hex(8)
#     file_name = random_hex + os.path.split(form_file.filename)[0] + '.txt'
#     file_path = os.path.join(current_app.root_path, path, file_name)
#     form_file.save(file_path)
#     return file_path
# 
# def get_linecount(filepath):
#     
#     with open(filepath, 'rb') as fp:
#         c_generator = _count_generator(fp.raw.read)
#         count = sum(buffer.count(b'\n') for buffer in c_generator)
#         return count + 1
# 
# def get_filehash(filepath):
#     sha256_hash = hashlib.sha256()
#     with open(filepath,"rb") as f:
#         # Read and update hash string value in blocks of 4K
#         for byte_block in iter(lambda: f.read(4096),b""):
#             sha256_hash.update(byte_block)
#     return sha256_hash.hexdigest()
# 
# Path: hashview.py
# def data_retention_cleanup():


import os
from flask import Blueprint, render_template, flash, url_for, redirect, current_app
from flask_login import login_required, current_user
from hashview.models import Rules, Tasks, Jobs, JobTasks, Users
from hashview.rules.forms import RulesForm
from hashview.utils.utils import save_file, get_linecount, get_filehash
from hashview import db


rules = Blueprint('rules', __name__)

#############################################
# Rules
#############################################

@rules.route("/rules", methods=['GET'])
@login_required
def rules_list():
    rules = Rules.query.all()
    tasks = Tasks.query.all()
    jobs = Jobs.query.all()
    jobtasks = JobTasks.query.all()

# Path: hashview/models.py
# class Rules(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     last_updated = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     path = db.Column(db.String(256), nullable=False)
#     size = db.Column(db.Integer, nullable=False, default=0)
#     checksum = db.Column(db.String(64), nullable=False)
# 
# class Tasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(100), nullable=False)
#     hc_attackmode = db.Column(db.String(25), nullable=False) # dictionary, mask, bruteforce, combinator
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
#     wl_id = db.Column(db.Integer)
#     rule_id = db.Column(db.Integer)
#     hc_mask = db.Column(db.String(50))
# 
# class Jobs(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     name = db.Column(db.String(50), nullable=False)
#     created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
#     status = db.Column(db.String(20), nullable=False)           # Running, Paused, Completed, Queued, Canceled, Ready, Incomplete
#     started_at = db.Column(db.DateTime, nullable=True)          # These defaults should be changed
#     ended_at = db.Column(db.DateTime, nullable=True)            # These defaults should be changed
#     hashfile_id = db.Column(db.Integer, nullable=True)
#     customer_id = db.Column(db.Integer, db.ForeignKey('customers.id'), nullable=False)
#     owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
# 
# class JobTasks(db.Model):
#     id = db.Column(db.Integer, primary_key=True)
#     job_id = db.Column(db.Integer, nullable=False)
#     task_id = db.Column(db.Integer, nullable=False)
#     command = db.Column(db.String(1024))                  
#     status = db.Column(db.String(50), nullable=False)       # Running, Paused, Not Started, Completed, Queued, Canceled, Importing
#     agent_id = db.Column(db.Integer, db.ForeignKey('agents.id'))
# 
# class Users(db.Model, UserMixin):
#     id = db.Column(db.Integer, primary_key=True)
#     first_name = db.Column(db.String(20), nullable=False)
#     last_name = db.Column(db.String(20), nullable=False)
#     email_address = db.Column(db.String(50), unique=True, nullable=False)
#     password = db.Column(db.String(60), nullable=False)
#     admin = db.Column(db.Boolean, nullable=False, default=False)
#     pushover_app_id = db.Column(db.String(50), nullable=True)
#     pushover_user_key = db.Column(db.String(50), nullable=True)
#     wordlists = db.relationship('Wordlists', backref='tbd', lazy=True)
#     rules = db.relationship('Rules', backref='owner', lazy=True)
#     jobs = db.relationship('Jobs', backref='owner', lazy=True)
#     tasks = db.relationship('Tasks', backref='owner', lazy=True)
#     taskgroups = db.relationship('TaskGroups', backref='owner', lazy=True)
# 
#     def get_reset_token(self, expires_sec=1800):
#         s = Serializer(Config.SECRET_KEY, expires_sec)
#         return s.dumps({'user_id': self.id}).decode('utf-8')
# 
#     @staticmethod
#     def verify_reset_token(token):
#         s = Serializer(Config.SECRET_KEY)
#         try:
#             user_id = s.loads(token)['user_id']
#         except:
#             return None     
#         return Users.query.get(user_id)
# 
# Path: hashview/rules/forms.py
# class RulesForm(FlaskForm):
#     name = StringField('Name', validators=([DataRequired()]))
#     rules = FileField('Upload Rules')
#     submit = SubmitField('upload')  
# 
# Path: hashview/utils/utils.py
# def save_file(path, form_file):
#     random_hex = secrets.token_hex(8)
#     file_name = random_hex + os.path.split(form_file.filename)[0] + '.txt'
#     file_path = os.path.join(current_app.root_path, path, file_name)
#     form_file.save(file_path)
#     return file_path
# 
# def get_linecount(filepath):
#     
#     with open(filepath, 'rb') as fp:
#         c_generator = _count_generator(fp.raw.read)
#         count = sum(buffer.count(b'\n') for buffer in c_generator)
#         return count + 1
# 
# def get_filehash(filepath):
#     sha256_hash = hashlib.sha256()
#     with open(filepath,"rb") as f:
#         # Read and update hash string value in blocks of 4K
#         for byte_block in iter(lambda: f.read(4096),b""):
#             sha256_hash.update(byte_block)
#     return sha256_hash.hexdigest()
# 
# Path: hashview.py
# def data_retention_cleanup():


# Path: hashview/rules/routes.py
import os
from flask import Blueprint, render_template, flash, url_for, redirect, current_app
from flask_login import login_required, current_user
from hashview.models import Rules, Tasks, Jobs, JobTasks, Users
from hashview.rules.forms import RulesForm
from hashview.utils.utils import save_file, get_linecount, get_filehash
from hashview import db



rules = Blueprint('rules', __name__)

#############################################
# Rules
#############################################

@rules.route("/rules", methods=['GET'])
@login_required
def rules_list():
    rules = Rules.query.all()
    tasks = Tasks.query.all()
    jobs = Jobs.query.all()
    jobtasks = JobTasks.query.all()

    users = Users.query.all()