wip
This commit is contained in:
parent
3b2f1db4ce
commit
5c16964b76
47 changed files with 2080 additions and 1053 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -4,11 +4,12 @@ from .extensions import db, bcrypt
|
|||
from datetime import datetime
|
||||
|
||||
login_manager = LoginManager()
|
||||
login_manager.login_view = 'auth.login'
|
||||
login_manager.login_view = "auth.login"
|
||||
|
||||
|
||||
class User(UserMixin, db.Model):
|
||||
__tablename__ = 'users'
|
||||
|
||||
__tablename__ = "users"
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
username = db.Column(db.String(64), unique=True, nullable=True)
|
||||
email = db.Column(db.String(120), unique=True, nullable=False)
|
||||
|
@ -16,19 +17,20 @@ class User(UserMixin, db.Model):
|
|||
is_admin = db.Column(db.Boolean, default=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
last_seen = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
|
||||
|
||||
def __repr__(self):
|
||||
return f'<User {self.username}>'
|
||||
|
||||
return f"<User {self.username}>"
|
||||
|
||||
def set_password(self, password):
|
||||
self.password_hash = generate_password_hash(password)
|
||||
|
||||
|
||||
def check_password(self, password):
|
||||
return check_password_hash(self.password_hash, password)
|
||||
|
||||
def get_id(self):
|
||||
return str(self.id)
|
||||
|
||||
|
||||
@login_manager.user_loader
|
||||
def load_user(user_id):
|
||||
return User.query.get(int(user_id))
|
||||
return User.query.get(int(user_id))
|
||||
|
|
|
@ -10,13 +10,12 @@ from flask_wtf.csrf import CSRFProtect
|
|||
db = SQLAlchemy()
|
||||
migrate = Migrate()
|
||||
login_manager = LoginManager()
|
||||
login_manager.login_view = 'auth.login'
|
||||
login_manager.login_message = 'Please log in to access this page.'
|
||||
login_manager.login_message_category = 'info'
|
||||
login_manager.login_view = "auth.login"
|
||||
login_manager.login_message = "Please log in to access this page."
|
||||
login_manager.login_message_category = "info"
|
||||
|
||||
bcrypt = Bcrypt()
|
||||
csrf = CSRFProtect()
|
||||
limiter = Limiter(
|
||||
key_func=get_remote_address,
|
||||
default_limits=["200 per day", "50 per hour"]
|
||||
key_func=get_remote_address, default_limits=["200 per day", "50 per hour"]
|
||||
)
|
||||
|
|
|
@ -8,42 +8,49 @@ from flask_login import UserMixin
|
|||
# User model has been moved to app.core.auth
|
||||
# Import it from there instead if needed: from app.core.auth import User
|
||||
|
||||
|
||||
class Port(db.Model):
|
||||
__tablename__ = 'ports'
|
||||
|
||||
__tablename__ = "ports"
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
app_id = db.Column(db.Integer, db.ForeignKey('apps.id', ondelete='CASCADE'), nullable=False)
|
||||
app_id = db.Column(
|
||||
db.Integer, db.ForeignKey("apps.id", ondelete="CASCADE"), nullable=False
|
||||
)
|
||||
port_number = db.Column(db.Integer, nullable=False)
|
||||
protocol = db.Column(db.String(10), default='TCP') # TCP, UDP, etc.
|
||||
protocol = db.Column(db.String(10), default="TCP") # TCP, UDP, etc.
|
||||
description = db.Column(db.String(200))
|
||||
|
||||
|
||||
# Relationship
|
||||
app = db.relationship('App', back_populates='ports')
|
||||
|
||||
app = db.relationship("App", back_populates="ports")
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Port {self.port_number}/{self.protocol}>'
|
||||
return f"<Port {self.port_number}/{self.protocol}>"
|
||||
|
||||
|
||||
class Server(db.Model):
|
||||
__tablename__ = 'servers'
|
||||
|
||||
__tablename__ = "servers"
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
hostname = db.Column(db.String(64), nullable=False)
|
||||
ip_address = db.Column(db.String(39), nullable=False) # IPv4 or IPv6
|
||||
subnet_id = db.Column(db.Integer, db.ForeignKey('subnets.id'), nullable=False)
|
||||
subnet_id = db.Column(db.Integer, db.ForeignKey("subnets.id"), nullable=False)
|
||||
documentation = db.Column(db.Text)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
updated_at = db.Column(
|
||||
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
|
||||
)
|
||||
|
||||
# Relationships
|
||||
subnet = db.relationship('Subnet', back_populates='servers')
|
||||
apps = db.relationship('App', back_populates='server', cascade='all, delete-orphan')
|
||||
|
||||
subnet = db.relationship("Subnet", back_populates="servers")
|
||||
apps = db.relationship("App", back_populates="server", cascade="all, delete-orphan")
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Server {self.hostname}>'
|
||||
return f"<Server {self.hostname}>"
|
||||
|
||||
|
||||
class Subnet(db.Model):
|
||||
__tablename__ = 'subnets'
|
||||
|
||||
__tablename__ = "subnets"
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
cidr = db.Column(db.String(18), unique=True, nullable=False) # e.g., 192.168.1.0/24
|
||||
location = db.Column(db.String(64))
|
||||
|
@ -51,43 +58,48 @@ class Subnet(db.Model):
|
|||
last_scanned = db.Column(db.DateTime)
|
||||
auto_scan = db.Column(db.Boolean, default=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
updated_at = db.Column(
|
||||
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
|
||||
)
|
||||
|
||||
# Relationships
|
||||
servers = db.relationship('Server', back_populates='subnet')
|
||||
|
||||
servers = db.relationship("Server", back_populates="subnet")
|
||||
|
||||
def __repr__(self):
|
||||
return f'<Subnet {self.cidr}>'
|
||||
|
||||
return f"<Subnet {self.cidr}>"
|
||||
|
||||
@property
|
||||
def used_ips(self):
|
||||
"""Number of IPs used in this subnet (servers)"""
|
||||
return len(self.servers)
|
||||
|
||||
|
||||
# Getter and setter for active_hosts as JSON
|
||||
@property
|
||||
def active_hosts_list(self):
|
||||
if not self.active_hosts:
|
||||
return []
|
||||
return json.loads(self.active_hosts)
|
||||
|
||||
|
||||
@active_hosts_list.setter
|
||||
def active_hosts_list(self, hosts):
|
||||
self.active_hosts = json.dumps(hosts)
|
||||
|
||||
|
||||
class App(db.Model):
|
||||
__tablename__ = 'apps'
|
||||
|
||||
__tablename__ = "apps"
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(64), nullable=False)
|
||||
server_id = db.Column(db.Integer, db.ForeignKey('servers.id'), nullable=False)
|
||||
server_id = db.Column(db.Integer, db.ForeignKey("servers.id"), nullable=False)
|
||||
documentation = db.Column(db.Text)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
|
||||
|
||||
updated_at = db.Column(
|
||||
db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow
|
||||
)
|
||||
|
||||
# Relationships
|
||||
server = db.relationship('Server', back_populates='apps')
|
||||
ports = db.relationship('Port', back_populates='app', cascade='all, delete-orphan')
|
||||
|
||||
server = db.relationship("Server", back_populates="apps")
|
||||
ports = db.relationship("Port", back_populates="app", cascade="all, delete-orphan")
|
||||
|
||||
def __repr__(self):
|
||||
return f'<App {self.name}>'
|
||||
return f"<App {self.name}>"
|
||||
|
|
|
@ -3,45 +3,42 @@ import markdown as md_package
|
|||
import re
|
||||
from flask import Blueprint
|
||||
|
||||
bp = Blueprint('filters', __name__)
|
||||
bp = Blueprint("filters", __name__)
|
||||
|
||||
|
||||
def github_style_admonition(text):
|
||||
"""Transform GitHub-style alerts (> [!NOTE], etc.) to custom HTML"""
|
||||
patterns = {
|
||||
r'> \[!NOTE\](.*?)(?:\n\n|\Z)': '<div class="markdown-alert markdown-alert-note"><p class="markdown-alert-title">Note</p>\\1</div>',
|
||||
r'> \[!TIP\](.*?)(?:\n\n|\Z)': '<div class="markdown-alert markdown-alert-tip"><p class="markdown-alert-title">Tip</p>\\1</div>',
|
||||
r'> \[!IMPORTANT\](.*?)(?:\n\n|\Z)': '<div class="markdown-alert markdown-alert-important"><p class="markdown-alert-title">Important</p>\\1</div>',
|
||||
r'> \[!WARNING\](.*?)(?:\n\n|\Z)': '<div class="markdown-alert markdown-alert-warning"><p class="markdown-alert-title">Warning</p>\\1</div>',
|
||||
r'> \[!CAUTION\](.*?)(?:\n\n|\Z)': '<div class="markdown-alert markdown-alert-caution"><p class="markdown-alert-title">Caution</p>\\1</div>'
|
||||
r"> \[!NOTE\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-note"><p class="markdown-alert-title">Note</p>\\1</div>',
|
||||
r"> \[!TIP\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-tip"><p class="markdown-alert-title">Tip</p>\\1</div>',
|
||||
r"> \[!IMPORTANT\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-important"><p class="markdown-alert-title">Important</p>\\1</div>',
|
||||
r"> \[!WARNING\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-warning"><p class="markdown-alert-title">Warning</p>\\1</div>',
|
||||
r"> \[!CAUTION\](.*?)(?:\n\n|\Z)": '<div class="markdown-alert markdown-alert-caution"><p class="markdown-alert-title">Caution</p>\\1</div>',
|
||||
}
|
||||
|
||||
|
||||
for pattern, replacement in patterns.items():
|
||||
text = re.sub(pattern, replacement, text, flags=re.DOTALL)
|
||||
|
||||
|
||||
return text
|
||||
|
||||
@bp.app_template_filter('markdown')
|
||||
|
||||
@bp.app_template_filter("markdown")
|
||||
def markdown_filter(text):
|
||||
"""Convert markdown text to HTML with support for GitHub-style features"""
|
||||
if text:
|
||||
# Pre-process GitHub-style alerts
|
||||
text = github_style_admonition(text)
|
||||
|
||||
|
||||
# Convert to HTML with regular markdown
|
||||
html = md_package.markdown(
|
||||
text,
|
||||
extensions=[
|
||||
'tables',
|
||||
'fenced_code',
|
||||
'codehilite',
|
||||
'nl2br'
|
||||
]
|
||||
text, extensions=["tables", "fenced_code", "codehilite", "nl2br"]
|
||||
)
|
||||
|
||||
|
||||
return html
|
||||
return ""
|
||||
|
||||
@bp.app_template_filter('ip_network')
|
||||
|
||||
@bp.app_template_filter("ip_network")
|
||||
def ip_network_filter(cidr):
|
||||
"""Convert a CIDR string to an IP network object"""
|
||||
try:
|
||||
|
@ -49,7 +46,8 @@ def ip_network_filter(cidr):
|
|||
except ValueError:
|
||||
return None
|
||||
|
||||
@bp.app_template_filter('ip_address')
|
||||
|
||||
@bp.app_template_filter("ip_address")
|
||||
def ip_address_filter(ip):
|
||||
"""Convert an IP string to an IP address object"""
|
||||
try:
|
||||
|
@ -57,10 +55,11 @@ def ip_address_filter(ip):
|
|||
except ValueError:
|
||||
return None
|
||||
|
||||
@bp.app_template_global('get_ip_network')
|
||||
|
||||
@bp.app_template_global("get_ip_network")
|
||||
def get_ip_network(cidr):
|
||||
"""Global function to get an IP network object from CIDR"""
|
||||
try:
|
||||
return ipaddress.ip_network(cidr, strict=False)
|
||||
except ValueError:
|
||||
return None
|
||||
return None
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue