(20250210) Started working on MikroTik config automation.
This commit is contained in:
@@ -6,11 +6,11 @@
|
||||
|
||||
DATE:
|
||||
|
||||
Friday, 6th Dec., 2024.
|
||||
Monday, 10th Feb., 2025.
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To provide a structure to receive auth details of various chat apps (like Telegram and WhatsApp).
|
||||
To provide a structure to receive auth details for MikroTik devices.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
@@ -37,11 +37,12 @@ sys.path.append("..")
|
||||
|
||||
# For making data behaviour_models:
|
||||
from pydantic import BaseModel, Field, field_validator, PastDatetime, model_validator
|
||||
from typing import Optional, Literal, Union
|
||||
from typing import Optional, Literal, Union, Any, List
|
||||
|
||||
# My utils:
|
||||
from utils_v2.string import regex
|
||||
from utils_v2.date_time import date_time
|
||||
from utils_v2.network import ip
|
||||
|
||||
# To work with date and time:
|
||||
import datetime
|
||||
@@ -75,22 +76,118 @@ REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class TelegramAuth(BaseModel):
|
||||
def parse_vlans(vlan_range_str) -> List[int]:
|
||||
|
||||
botId: str = Field(
|
||||
description = "The id of the bot (that can be invoked with '@').",
|
||||
"""
|
||||
Converts VLAN ids from a string representation to an array of integers.
|
||||
:param vlan_range_str: A string like ""0,1,2,10-15,20".
|
||||
:return: A list like [0, 1, 2, 10, 11, 12, 13, 14, 15, 20].
|
||||
"""
|
||||
|
||||
# Start with an empty array:
|
||||
result = []
|
||||
|
||||
# Split the string by commas:
|
||||
parts = vlan_range_str.replace(" ", "").split(",")
|
||||
|
||||
# Loop through each part
|
||||
for part in parts:
|
||||
|
||||
# Check if the part is a range (contains a hyphen):
|
||||
if "-" in part:
|
||||
|
||||
# Add all numbers in the range to the result list:
|
||||
start, end = part.split("-")
|
||||
result.extend(range(int(start), int(end) + 1))
|
||||
|
||||
# Add the single number to the result list:
|
||||
else: result.append(int(part))
|
||||
|
||||
# Done here:
|
||||
result = sorted(result)
|
||||
print(result)
|
||||
return result
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
# *** CLASSES ***
|
||||
# ***** ****
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class MikroTikPPPoE1000Auth(BaseModel):
|
||||
|
||||
siteName: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
botName: str = Field(
|
||||
description = "The display name of the bot.",
|
||||
nasIp: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
botToken: str = Field(
|
||||
description = "the token granted by BotFather",
|
||||
nasPort: int | None = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
radius: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
secret: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
username: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
password: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
location: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
snmpCommunity: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
vlanRange: List[int] = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
privateIpPool: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True,
|
||||
default = "100.64.0.0/22",
|
||||
validate_default = True
|
||||
)
|
||||
|
||||
publicIpPool: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
@@ -103,20 +200,124 @@ class TelegramAuth(BaseModel):
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
# ┏┓ •
|
||||
# ┃┃┏┓┏┓┏┓┏┓┏┓╋┓┏┓┏
|
||||
# ┣┛┛ ┗┛┣┛┗ ┛ ┗┗┗ ┛
|
||||
# ┛
|
||||
|
||||
@property
|
||||
def firstPrivateIp(self) -> str:
|
||||
return self.privateIpPool.split("-")[0]
|
||||
|
||||
@property
|
||||
def lastPrivateIp(self) -> str:
|
||||
return self.privateIpPool.split("-")[1]
|
||||
|
||||
@property
|
||||
def firstPublicIp(self) -> str:
|
||||
return self.publicIpPool.split("-")[0]
|
||||
|
||||
@property
|
||||
def lastPublicIp(self) -> str:
|
||||
return self.publicIpPool.split("-")[1]
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@field_validator("nasPort", mode = "before")
|
||||
def validate_port_no(cls, value):
|
||||
if isinstance(value, (str, float)): value = int(value)
|
||||
return value
|
||||
|
||||
@field_validator("privateIpPool", "publicIpPool", mode = "before")
|
||||
def validate_ip_range(cls, value):
|
||||
return ip.to_hyphen_notation(value)
|
||||
|
||||
@field_validator("vlanRange", mode = "before")
|
||||
def validate_vlans(cls, value):
|
||||
return parse_vlans(value)
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class WhatsAppNimbusAuth(BaseModel):
|
||||
class MikroTikHotspot1000Auth(BaseModel):
|
||||
|
||||
apiKey: str = Field(
|
||||
description = "???",
|
||||
siteName: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
senderId: str = Field(
|
||||
description = "???",
|
||||
nasIp: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
nasPort: int | None = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
radius: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
secret: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
username: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
password: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
location: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
snmpCommunity: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
vlanRange: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
privateIpPool: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
publicIpPool: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
domainName: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
@@ -129,6 +330,59 @@ class WhatsAppNimbusAuth(BaseModel):
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@field_validator("nasPort", mode = "before")
|
||||
def validate_port_no(cls, value):
|
||||
if isinstance(value, (str, float)): value = int(value)
|
||||
return value
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class MikroTikAuthResponse(BaseModel):
|
||||
|
||||
success: bool = Field(
|
||||
description = "To indicate whether or not, the action was a success",
|
||||
frozen = False,
|
||||
default = False
|
||||
)
|
||||
|
||||
message: str = Field(
|
||||
description = "To explain what happened in the process of handling the OAuth callback.",
|
||||
frozen = False,
|
||||
default = "ERR: Message not captured."
|
||||
)
|
||||
|
||||
exception: Any = Field(
|
||||
description = "To pass on any exception that occurred in the process.",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
# ┏┓ ┏┓
|
||||
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
|
||||
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
|
||||
|
||||
pass
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
pass
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
OBJECTIVE:
|
||||
|
||||
To provide a structure to receive auth details for MikroTik devices.
|
||||
Maa chude, yaar. Ab fark nahin padtaa.
|
||||
|
||||
REFERENCES:
|
||||
|
||||
@@ -75,68 +75,26 @@ REGEX_SESSION_TOKEN = r"^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]
|
||||
# *****************************************************************************************************************
|
||||
|
||||
|
||||
class MikroTikPPPoE1000Auth(BaseModel):
|
||||
class MikroTikConfigAttemptResponse(BaseModel):
|
||||
|
||||
siteName: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
success: bool = Field(
|
||||
description = "To indicate whether or not, the action was a success",
|
||||
frozen = False,
|
||||
default = False
|
||||
)
|
||||
|
||||
nasIp: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
message: str = Field(
|
||||
description = "To explain what happened in the process of handling the OAuth callback.",
|
||||
frozen = False,
|
||||
default = "ERR: Message not captured."
|
||||
)
|
||||
|
||||
nasPort: int | None = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
frozen = True,
|
||||
exception: Any = Field(
|
||||
description = "To pass on any exception that occurred in the process.",
|
||||
frozen = False,
|
||||
default = None
|
||||
)
|
||||
|
||||
radius: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
secret: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
username: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
password: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
location: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
snmpCommunity: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
publicIpPool: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
@@ -145,121 +103,23 @@ class MikroTikPPPoE1000Auth(BaseModel):
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
# ┏┓ ┏┓
|
||||
# ┃ ┓┏┏╋┏┓┏┳┓ ┣ ┓┏┏┓┏┏
|
||||
# ┗┛┗┻┛┗┗┛┛┗┗ ┻ ┗┻┛┗┗┛
|
||||
|
||||
@field_validator("nasPort", mode = "before")
|
||||
def validate_port_no(cls, value):
|
||||
if isinstance(value, (str, float)): value = int(value)
|
||||
return value
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class MikroTikHotspot1000Auth(BaseModel):
|
||||
|
||||
siteName: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
nasIp: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
nasPort: int | None = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
frozen = True,
|
||||
default = None
|
||||
)
|
||||
|
||||
radius: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
secret: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
username: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
password: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
location: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
snmpCommunity: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
vlanRange: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
privateIpPool: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
publicIpPool: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
domainName: str = Field(
|
||||
description = "Don't know, and don't want to know.",
|
||||
min_length = 1,
|
||||
frozen = True
|
||||
)
|
||||
|
||||
# ┏┓ ┏•
|
||||
# ┃ ┏┓┏┓╋┓┏┓
|
||||
# ┗┛┗┛┛┗┛┗┗┫
|
||||
# ┛
|
||||
|
||||
class Config:
|
||||
extra = "forbid"
|
||||
pass
|
||||
|
||||
# ┓┏ ┓• ┓ •
|
||||
# ┃┃┏┓┃┓┏┫┏┓╋┓┏┓┏┓
|
||||
# ┗┛┗┻┗┗┗┻┗┻┗┗┗┛┛┗
|
||||
|
||||
@field_validator("nasPort", mode = "before")
|
||||
def validate_port_no(cls, value):
|
||||
if isinstance(value, (str, float)): value = int(value)
|
||||
return value
|
||||
pass
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
class MikroTikAuthResponse(BaseModel):
|
||||
class MikroTikRollBackAttemptResponse(BaseModel):
|
||||
|
||||
success: bool = Field(
|
||||
description = "To indicate whether or not, the action was a success",
|
||||
|
||||
Reference in New Issue
Block a user