def run(self): while True: try: self.getUserOptions() except KeyboardInterrupt: Console.info('Quitting') break
def parse_next_page(self, next_page_url): Console.info('Going to next page') # TODO caching for easier backward navigation # self.cached_posts[self.URL] = self.posts self.posts = [] self.URL = next_page_url self.parse_result()
def write_encripted_data_to_file(self, file_data): file = f'encrypted_{self.file_name}' file = os.path.join(self.file_root_dir, file) Console.info('Writting encrypted data to file...') with open(file, 'wb') as f: time.sleep(1) file_data.write(f) Console.log(f'{file} written successfully')
def appFactory(choice, modules): try: Console.info(f"{modules[str(choice)]['desc']}") time.sleep(.5) return modules[str(choice)]['name']() except KeyError as e: Console.error('Wrong module') exit(1)
def process_compression(self, path_to_file): root_dir = path_to_file[2] f_name = path_to_file[1] compressed_file, was_success = self.compress(root_dir, path_to_file) choice = input( f"\n{Console.green(f'Encrypt { compressed_file[0] } ? (Y/n)')}") if choice.lower() not in ['n', 'y'] or choice.lower() == 'n': Console.info('Bye!!') return self.encrypt_file(compressed_file)
def compress(self, root_dir, path_to_file): if os.path.isdir(path_to_file[0]): compress_name = f'{path_to_file[0]}.tar.gz' elif os.path.isfile(path_to_file[0]): file_name = path_to_file[0] file_name = file_name.split('.')[-2] compress_name = f'{file_name}.tar.gz' time.sleep(.3) Console.info(f'Compressing to {compress_name}, please wait...') self.actual_compression(path_to_file[0]) return path_to_file, True
def main(modules): time.sleep(.3) Console.info('Welcome to libs helper\n') time.sleep(.5) instructions(modules) try: choice = int(input('\nPick your poison > ')) app = appFactory(choice, modules) app.run() except ValueError as e: Console.error('Invalid input') exit(1)
def enc_pdf(self): parser = PdfFileWriter() Console.info(f'Loading {self.file}...') time.sleep(.5) pdf = self.get_pdf_file() for page in range(pdf.numPages): parser.addPage(pdf.getPage(page)) Console.info('Encrypting data...') time.sleep(.4) parser.encrypt(self.passwd) self.write_encripted_data_to_file(parser) time.sleep(.2) self.choose_to_delete(self.file)
def choose_to_delete(self, file_name): choice = input( Console.yellow(f'Do you wish to delete {file_name}? (N/y) > ')) choice = choice.strip().lower() not_valid = choice not in ['n', 'y'] if not_valid: Console.error('Please choose a valid input!') time.sleep(.2) self.choose_to_delete(file_name) if choice == 'n': Console.log('Bye!') return Console.info(f'Deleting {file_name}...') delete_file(file_name)
def generate_password(self): passwd_chars = self.get_chosen_chars() Console.info(f'Your choice {passwd_chars}') time.sleep(.2) Console.info('Generating password, Please wait...') time.sleep(1) pass_list = random.sample(passwd_chars, int(self.password_length)) generated_passwd = "".join(pass_list) Console.info( f'Your newly generated password is {Console.green(generated_passwd)}' ) time.sleep(.4) Console.log('[+] Done') raise KeyboardInterrupt
import os.path import sys from utils import Console, red, white SHL = Console("Checkup") if not os.path.exists(os.path.join("app", "public")): if "-unittest" in [x.strip().lower() for x in sys.argv]: # start args SHL.warning(f"public folder is missing but ignored for unittest mode.") SHL.info(f"Creating dummy index.html for unittests.") os.mkdir(os.path.join("app", "public")) with open(os.path.join("app", "public", "index.html"), "w") as fh: fh.write("<body>ok<body/>") else: raise RuntimeError(f"{red}public folder is missing, use 'ng build --prod' and try again{white}") SHL.info(f"Start_checkup passed.")
import os.path import json import discord from discord.errors import LoginFailure from discord.utils import get as discord_fetch from utils import Console, cfg, app_scheduler, SuccessEmbed, ErrorEmbed, InfoEmbed loop = asyncio.new_event_loop() app_scheduler.main_loop = loop client = discord.Client(loop=loop) SHL = Console(prefix="BundestagsBot") CONTENT_PATH = "content" if os.path.isdir("content") else "content-default" SHL.info(f"Using content path: {CONTENT_PATH}") about_embed = InfoEmbed(title="Help") with open(os.path.join("static", "info.txt"), "r", encoding="utf-8") as fh: about_embed.description = fh.read() class UserStat: def __init__(self, user_obj: discord.Member): self.user_obj = user_obj self.count = 1 def __str__(self): return f"{self.user_obj.display_name}: {self.count}"
class UTMFailOver: def __init__(self, cfg, curr, targ): self.cfg = cfg self.LOG = Console() # 상태 : active, standby # curr : 현재상태 # targ : 다음상태 self.curr = curr self.targ = targ def loop_l3_connectivity(self, run_l3_check=False, max_count = 10, time_sleep=3): # L3 연결성 체크 count = 0 if run_l3_check: while (count < max_count): if self.check_l3(): break self.LOG.log(" ====================================================") if count != max_count-1: time.sleep(time_sleep) count = count + 1 # utm의 interface가 올라왔는지 ping과 arp로 확인한다. # target의 ip 주소로 ping을 하고, # 그 결과 arp를 확인하여 mac주소가 설정값과 동일한지 확인 def is_l3_connected(self, ip_addr, mac_in_cfg): mac_in_real = NetUtil().get_mac_from_arp(ip_addr) self.LOG.log(" - CONF ip= %-15s mac= %s" % (ip_addr, mac_in_cfg)) self.LOG.log(" - REAL ip= %-15s mac= %s" % (ip_addr, mac_in_real)) if mac_in_cfg.upper() == mac_in_real.upper(): return True else: return False def check_l3(self): customer = self.cfg.get("failover", "customer") ip_red = self.cfg.get(customer, "ip_red") ip_green = self.cfg.get(customer, "ip_green") mac_red_in_cfg = self.cfg.get(self.target, "port_red_mac") mac_green_in_cfg = self.cfg.get(self.target, "port_green_mac") self.LOG.info(" *** [INFO] Checking L3 connectivity...") red_l3_failover = is_l3_connected(ip_red, mac_red_in_cfg) green_l3_failover = is_l3_connected(ip_green, mac_green_in_cfg) if red_l3_failover and green_l3_failover: self.LOG.info(" *** 성공!!!") return True else: self.LOG.error(" *** 진행중!!!") return False # L2 연결성 체크, 성공시 L3 연결 체크 문의 def loop_l2_connectivity(self, ask_l3_check=False, max_count = 10, time_sleep=3 ): run_l3_check=False count = 0 while (count < max_count): if self.check_l2(): if ask_l3_check: run_l3_check=InteractionUtil().ask_user("L3 연결을 체크하시겠습니까?") break self.LOG.log(" ====================================================") if count != max_count-1: time.sleep(time_sleep) count = count + 1 return run_l3_check # 스위치 인터페이스 중 active가 disable되고 # standby가 active되었는지를 확인 def is_l2_connected(self, intf_curr, intf_targ): l2_connected = False if intf_curr.is_disabled() and intf_targ.is_enabled(): l2_connected = True else: l2_connected = False return l2_connected # switch의 interface가 올라왔는지 확인한다. # current interface는 down되고, target interface는 up def check_l2(self): port_red_curr = self.cfg.get(self.curr, "port_red") port_red_targ = self.cfg.get(self.targ, "port_red") port_green_curr = self.cfg.get(self.curr, "port_green") port_green_targ = self.cfg.get(self.targ, "port_green") intf_red_curr = Interface(port_red_curr) intf_red_targ = Interface(port_red_targ) intf_green_curr = Interface(port_green_curr) intf_green_targ = Interface(port_green_targ) red_failover = self.is_l2_connected(intf_red_curr, intf_red_targ) green_failover = self.is_l2_connected(intf_green_curr, intf_green_targ) self.LOG.info(" *** [INFO] Checking L2 connectivity...") self.LOG.log(" - %-5s interface failover result= %s" % ("RED", red_failover)) self.LOG.log(" - %-5s interface failover result= %s" % ("GREEN", green_failover)) if red_failover and green_failover: self.LOG.log(" ----------------------------------------------------") self.LOG.info(" *** 성공!!!") self.LOG.log(" ----------------------------------------------------") return True else: self.LOG.log(" ----------------------------------------------------") self.LOG.error(" *** 진행중!!!") self.LOG.log(" ----------------------------------------------------") return False def failover(self, curr, targ): curr_port_green = self.cfg.get(curr, "port_green") curr_port_red = self.cfg.get(curr, "port_red") targ_port_green = self.cfg.get(targ, "port_green") targ_port_red = self.cfg.get(targ, "port_red") intf_curr_green = Interface(curr_port_green) intf_curr_red = Interface(curr_port_red) intf_targ_green = Interface(targ_port_green) intf_targ_red = Interface(targ_port_red) self.LOG.info(" *** [INFO] Running failover for GREEN interface") self.LOG.log(" - Shutdown interface= %s" % intf_curr_green.interface_id) intf_curr_green.shutdown(True) self.LOG.log(" - Enable interface= %s" % intf_targ_green.interface_id) intf_targ_green.shutdown(False) self.LOG.info(" *** [INFO] Running failover for RED interface") self.LOG.log(" - Shutdown interface= %s" % intf_curr_red.interface_id) intf_curr_red.shutdown(True) self.LOG.log(" - Enable interface= %s" % intf_targ_red.interface_id) intf_targ_red.shutdown(False) self.LOG.log(" ----------------------------------------------------") def is_applied(self, cfg_name): customer = self.cfg.get("failover", "customer") cfg_red_ip = self.cfg.get(customer, "red_ip") cfg_green_ip = self.cfg.get(customer, "green_ip") cfg_red_port = self.cfg.get(cfg_name, "port_red") cfg_green_port = self.cfg.get(cfg_name, "port_green") cfg_orange_port = self.cfg.get(cfg_name, "port_orange") intf_red = Interface(cfg_red_port) intf_green = Interface(cfg_green_port) intf_orange = Interface(cfg_orange_port) if intf_red.is_enabled() and intf_green.is_enabled() and intf_orange.is_enabled(): return True else: return False def show_config(self, cfg_name, title): self.LOG.log(" ----------------------------------------------------") self.LOG.info(" *** %s (%s)" % ( title, cfg_name) ) self.LOG.log(" ----------------------------------------------------") customer = self.cfg.get("failover", "customer") cfg_red_ip = self.cfg.get(customer, "red_ip") cfg_green_ip = self.cfg.get(customer, "green_ip") cfg_red_port = self.cfg.get(cfg_name, "port_red") cfg_green_port = self.cfg.get(cfg_name, "port_green") cfg_orange_port = self.cfg.get(cfg_name, "port_orange") intf_red = Interface(cfg_red_port) intf_green = Interface(cfg_green_port) intf_orange = Interface(cfg_orange_port) self.LOG.info(" - %s" % "RED") self.LOG.log(" . switch port= %s (%s)" % (cfg_red_port, intf_red.get_status())) self.LOG.log(" . ip address = %s" % cfg_red_ip) self.LOG.info(" - %s" % "GREEN") self.LOG.log(" . switch port= %s (%s)" % (cfg_green_port, intf_red.get_status())) self.LOG.log(" . ip address = %s" % cfg_green_ip) self.LOG.info(" - %s" % "ORANGE") self.LOG.log(" . switch port= %s (%s)" % (cfg_orange_port, intf_red.get_status())) self.LOG.log(" ----------------------------------------------------")
from datetime import datetime, timedelta import os.path import json import uuid from typing import Optional from werkzeug.security import generate_password_hash, check_password_hash import pyotp from utils import Console, red, white, cfg from app.runtime_settings import production_mode, unittest_mode SHL = Console("UserManager") SHL.info(f"Initializing UserManager.") BASE_PATH = os.path.dirname(__file__) login_file = "login.json" if os.path.isfile( os.path.join(BASE_PATH, "login.json")) else "login-default.json" if unittest_mode: login_file = "login-unittest.json" LOGIN_INFO = os.path.join(BASE_PATH, login_file) SHL.info(f"Login info in file {LOGIN_INFO}") class __UserManager: def __init__(self): SHL.info(f"Loading login info.") try: with open(LOGIN_INFO, 'r', encoding="utf-8") as fh: data = json.load(fh)
from app.rate_limiter import limiter SHL = Console("Routes") @app.route("/") @limiter.exempt def index(): return send_from_directory(os.path.join("public"), "index.html") @app.route("/401") @limiter.exempt def unauthorized_access(): return make_response("401", 401) @app.route("/secret") @token_auth.login_required def secret(): return "secret" @app.route('/public/<path:path>') @limiter.exempt def send_assets(path): return send_from_directory(os.path.join("public"), path) SHL.info(f"Registered static routes.")
import sys from utils import Console, white, cfg, red SHL = Console("SettingsInit") start_args = [x.strip().lower() for x in sys.argv] login_disabled = cfg.get("logindisabled", False) # default from cfg if "-disablelogin" in start_args: # overwrite by parameter login_disabled = True if login_disabled: SHL.info(f"Disabled authentication.") debug_mode = cfg.get("debug_enabled", False) # default from cfg if "-debug" in start_args: # overwrite by parameter debug_mode = True if debug_mode: SHL.info(f"Enabled debug_mode.") unittest_mode = False if "-unittest" in start_args: SHL.info(f"Enabled unittest mode.") unittest_mode = True debug_mode = False cfg.load_unittest_config() production_mode = False if "-prod" in start_args:
import sys import logging from flask import Flask from utils import Console SHL = Console("Startup") if "-log" in [x.strip().lower() for x in sys.argv]: SHL.info(f"Setting loggers to info level.") else: SHL.info(f"Setting loggers to error level.") logging.getLogger('app').setLevel(logging.ERROR) logging.getLogger('app.flask_app').setLevel(logging.ERROR) logging.getLogger('flask').setLevel(logging.ERROR) logging.getLogger('flask.app').setLevel(logging.ERROR) cli = sys.modules['flask.cli'] cli.show_server_banner = lambda *x: None
jsonify({ "status": "failed", "message": "invalid json" }), 400) totp_token = request_data.get("totp_token", None) if str(totp_token).strip().lower() in ["null", "none", ""]: data = {"status": "failed", "message": "invalid totp_token"} return make_response(jsonify(data), 400) if user_manager.check_2fa_temp_token(totp_token=str(totp_token)): if user_manager.set_2fa_temp_token(): data = {"status": "success", "message": "2fa enabled"} return jsonify(data) data = {"status": "failed", "message": "could not enable 2fa"} return make_response(jsonify(data), 500) data = {"status": "failed", "message": "invalid totp_token"} return make_response(jsonify(data), 400) @app.route("/login/2fa/link") @token_auth.login_required def login_2fa_link(): link = user_manager.get_2fa_link() data = { "status": "success" if link else "failed", "message": link if link else "2fa not enabled" } return make_response(jsonify(data), 200 if link else 412) SHL.info(f"Registered login routes.")
import os as __os from flask import Flask as __Flask from utils import Console, blue2, white SHL = Console("FlaskApp") SHL.info(f"Creating FlaskApp.") app = __Flask(__name__) app.config['SECRET_KEY'] = '1234567890!"§$%&/()=?' app.config['JSON_SORT_KEYS'] = False app.config['UPLOAD_FOLDER'] = __os.path.join("", "storage", "uploads") app.config['MAX_CONTENT_LENGTH'] = 3.5 * 1024 * 1024 # 3.5 Mb limit
def remove_file(self, filename): Console.warn(f'Removing { filename }, Please wait...') delete_file(filename) time.sleep(.3) Console.info(f'All done.')
def app_instrucions(self): Console.info('lets generate your new password') time.sleep(.5)
"message": "invalid json" }), 400) try: request_data.pop("list_id", None) request_data.pop("created_at", None) db_interface.todo_list_insert_or_update(obj=TodoList(to_parse=request_data).to_sql_obj()) except CreationError as e: return make_response(jsonify({ "status": "failed", "message": e.get_error_message() }), 400) except DatabaseError: return make_response(jsonify({ "status": "failed", "message": "database error" }), 500) else: return jsonify({ "status": "success", "message": "todolist created" }) if request.method == "DELETE": return make_response("OK", 501) if request.method == "PATCH": return make_response("OK", 501) return make_response("invalid method", 405) SHL.info(f"Registered api routes.")
from flask import request from flask_limiter import Limiter from utils import cfg, Console from .flask_app import app from .runtime_settings import unittest_mode SHL = Console("RateLimiter") def determine_ip(): if request.headers.get("X-Forwarded-For", request.remote_addr) == cfg.get("own_ip"): return request.headers.get( "X-Auth-For", request.headers.get("X-Forwarded-For", request.remote_addr)) return request.headers.get("X-Forwarded-For", request.remote_addr) defaults = ["10000000 per day", "500000 per hour" ] if unittest_mode else ["10000 per day", "500 per hour"] limiter = Limiter( app, key_func=lambda: determine_ip, default_limits=["10000 per day", "500 per hour"], ) SHL.info(f"Registered flask limiter.")