from config import config from datetime import datetime from flask_debugtoolbar import DebugToolbarExtension from flask_login import LoginManager from flask_bcrypt import Bcrypt from logging import Formatter from logging.handlers import RotatingFileHandler import os import redis import logging import uuid db = AlchemyBase() migrate = Migrate() toolbar = DebugToolbarExtension() session = Session() session.auto_flush = False cache = Cache() login_manager = LoginManager() bcrypt = Bcrypt() class ApplicationFactory(object): @staticmethod def create_application(env='development'): return Application(env).app class Application(object): def __init__(self, env):
def init(conf=None, verbose=0, logfile=None, gunicorn=True, unittest=False, debug=False): """Initialize the whole application. :param conf: Configuration file to use :type conf: str :param verbose: Set the verbosity level :type verbose: int :param logfile: Store the logs in the given file :type logfile: str :param gunicorn: Enable gunicorn engine instead of flask's default :type gunicorn: bool :param unittest: Are we running tests (used for test only) :type unittest: bool :param debug: Enable debug mode :type debug: bool :returns: A :class:`burpui.server.BUIServer` object """ from flask_login import LoginManager from flask_bower import Bower from .utils import basic_login_from_request, ReverseProxied from .server import BUIServer as BurpUI from .routes import view from .api import api, apibp logger = logging.getLogger('burp-ui') # The debug argument used to be a boolean so we keep supporting this format if isinstance(verbose, bool): if verbose: verbose = logging.DEBUG else: verbose = logging.CRITICAL else: levels = [ logging.CRITICAL, logging.ERROR, logging.WARNING, logging.INFO, logging.DEBUG ] if verbose >= len(levels): verbose = len(levels) - 1 if not verbose: verbose = 0 verbose = levels[verbose] if logfile: from logging.handlers import RotatingFileHandler handler = RotatingFileHandler( logfile, maxBytes=1024 * 1024 * 100, backupCount=5 ) else: from logging import StreamHandler handler = StreamHandler() if verbose > logging.DEBUG: LOG_FORMAT = ( '[%(asctime)s] %(levelname)s in ' '%(module)s.%(funcName)s: %(message)s' ) else: LOG_FORMAT = ( '-' * 80 + '\n' + '%(levelname)s in %(module)s.%(funcName)s ' + '[%(pathname)s:%(lineno)d]:\n' + '%(message)s\n' + '-' * 80 ) handler.setLevel(verbose) handler.setFormatter(Formatter(LOG_FORMAT)) logger.setLevel(verbose) logger.addHandler(handler) logger.debug( 'conf: {}\n'.format(conf) + 'verbose: {}\n'.format(logging.getLevelName(verbose)) + 'logfile: {}\n'.format(logfile) + 'gunicorn: {}\n'.format(gunicorn) + 'debug: {}\n'.format(debug) + 'unittest: {}'.format(unittest) ) if not unittest: from ._compat import patch_json patch_json() if gunicorn: from gevent import monkey monkey.patch_all() # We initialize the core app = BurpUI() app.enable_logger() app.gunicorn = gunicorn app.config['CFG'] = None # FIXME: strange behavior when bundling errors # app.config['BUNDLE_ERRORS'] = True app.config['REMEMBER_COOKIE_HTTPONLY'] = True app.jinja_env.globals.update( isinstance=isinstance, list=list, version_id='{}-{}'.format(__version__, __release__) ) if debug and not gunicorn: # pragma: no cover app.config['DEBUG'] = True and not unittest app.config['TESTING'] = True and not unittest # Still need to test conf file here because the init function can be called # by gunicorn directly app.config['CFG'] = lookup_config(conf) logger.info('Using configuration: {}'.format(app.config['CFG'])) app.setup(app.config['CFG']) # manage application secret key if app.secret_key and (app.secret_key.lower() == 'none' or (app.secret_key.lower() == 'random' and gunicorn)): logger.warning('Your setup is not secure! Please consider setting a' ' secret key in your configuration file') app.secret_key = 'Burp-UI' if not app.secret_key or app.secret_key.lower() == 'random': from base64 import b64encode app.secret_key = b64encode(os.urandom(256)) app.wsgi_app = ReverseProxied(app.wsgi_app, app) # Manage gunicorn special tricks & improvements if gunicorn: # pragma: no cover logger.info('Using gunicorn') from werkzeug.contrib.fixers import ProxyFix if app.storage and app.storage.lower() == 'redis': if app.redis: part = app.redis.split(':') host = part[0] try: port = int(part[1]) except: port = 6379 else: host = 'localhost' port = 6379 logger.debug('Using redis {}:{}'.format(host, port)) try: from redis import Redis from flask_session import Session red = Redis(host=host, port=port) app.config['SESSION_TYPE'] = 'redis' app.config['SESSION_REDIS'] = red app.config['SESSION_USE_SIGNER'] = app.secret_key is not None app.config['SESSION_PERMANENT'] = False ses = Session() ses.init_app(app) except Exception as e: logger.warning('Unable to initialize redis: {}'.format(str(e))) pass api.cache.init_app( app, config={ 'CACHE_TYPE': 'redis', 'CACHE_REDIS_HOST': host, 'CACHE_REDIS_PORT': port, 'CACHE_REDIS_DB': 1 } ) # clear cache at startup in case we removed or added servers with app.app_context(): api.cache.clear() else: api.cache.init_app(app) app.wsgi_app = ProxyFix(app.wsgi_app) else: api.cache.init_app(app) # We initialize the API api.init_bui(app) api.version = __version__ api.release = __release__ api.__url__ = __url__ api.__doc__ = __doc__ app.register_blueprint(apibp) # Then we load our routes view.init_bui(app) view.__url__ = __url__ view.__doc__ = __doc__ app.register_blueprint(view) # And the login_manager app.login_manager = LoginManager() app.login_manager.login_view = 'view.login' app.login_manager.login_message_category = 'info' app.login_manager.session_protection = 'strong' app.login_manager.init_app(app) app.config.setdefault( 'BOWER_COMPONENTS_ROOT', os.path.join('static', 'vendor') ) app.config.setdefault('BOWER_REPLACE_URL_FOR', True) bower = Bower() bower.init_app(app) @app.before_request def setup_request(): if app.scookie: from flask import request criteria = [ request.is_secure, request.headers.get('X-Forwarded-Proto', 'http') == 'https' ] app.config['SESSION_COOKIE_SECURE'] = \ app.config['REMEMBER_COOKIE_SECURE'] = any(criteria) @app.login_manager.user_loader def load_user(userid): """User loader callback""" if app.auth != 'none': return app.uhandler.user(userid) return None @app.login_manager.request_loader def load_user_from_request(request): """User loader from request callback""" if app.auth != 'none': return basic_login_from_request(request, app) return app
logging.getLogger("exceptions").exception(err_msg) return err_msg + ": " + traceback.format_exc(), 409 t0 = time.time() template_dir = os.path.join(os.path.dirname(__file__), "templates") server = Flask(__name__, static_url_path='', template_folder=template_dir) server.debug = False server.config['SESSION_TYPE'] = "redis" server.config['SESSION_KEY_PREFIX'] = "mxcube:session:" server.config['SECRET_KEY'] = "nosecretfornow" server.register_error_handler(Exception, exception_handler) _session = Session() _session.init_app(server) socketio = SocketIO(manage_session=False) socketio.init_app(server) # the following test prevents Flask from initializing twice # (because of the Reloader) if not server.debug or os.environ.get("WERKZEUG_RUN_MAIN") == "true": mxcube.init(hwr, cmdline_options.hwr_directory, cmdline_options.allow_remote, cmdline_options.ra_timeout, cmdline_options.video_device, cmdline_options.log_file)
app.config.from_pyfile("../config.cfg") logger.setupLogging(app) assets.setupAssets(app) login_manager = LoginManager() login_manager.init_app(app) socketio = SocketIO() socketio.init_app(app) mail = Mail() mail.init_app(app) sess = Session() sess.init_app(app) app.url_map.strict_slashes = False from pingpong.controllers.AdminController import adminController from pingpong.controllers.ApiController import apiController from pingpong.controllers.AuthenticationController import authenticationController from pingpong.controllers.ErrorController import errorController from pingpong.controllers.IsmController import ismController from pingpong.controllers.CourtesyController import courtesyController from pingpong.controllers.LeaderboardController import leaderboardController from pingpong.controllers.MainController import mainController from pingpong.controllers.MatchController import matchController from pingpong.controllers.OfficeController import officeController from pingpong.controllers.PlayerController import playerController
import os import redis from flask import Flask from flask_session import Session from APP.house_views import house_blueprint from APP.models import db from APP.user_veiws import user_blueprint from utils.setting import BASE_DIR se = Session() def create_app(): static_dir = os.path.join(BASE_DIR, 'static') templates_dir = os.path.join(BASE_DIR, 'templates') app = Flask(__name__, static_folder=static_dir, template_folder=templates_dir) app.register_blueprint(blueprint=user_blueprint, url_prefix='/user') app.register_blueprint(blueprint=house_blueprint, url_prefix='/house') app.config['SQLALCHEMY_DATABASE_URI'] = \ "mysql+pymysql://root:123456@localhost:3306/jiajuflask" app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SECRET_KEY'] = 'secret_key'
from helpers import apology, login_required from Predict import Predict import tensorflow import logging import sys # Configure application app = Flask(__name__) # Ensure templates are auto-reloaded app.config["TEMPLATES_AUTO_RELOAD"] = True # Configure session to use filesystem (instead of signed cookies) app.config["SESSION_FILE_DIR"] = mkdtemp() app.config["SESSION_PERMANENT"] = False app.config["SESSION_TYPE"] = "filesystem" Session(app) db = SQL("sqlite:///heart.db") @app.route("/", methods=["GET", "POST"]) def main(): return render_template("main.html") @app.route("/more", methods=["GET", "POST"]) @login_required def more(): # Directs the user to the more html when they try to open it if request.method == "GET": return render_template("more.html")
#TODO: Main application from flask import Flask from flask_script import Manager from sql import SQL from flask_bootstrap import Bootstrap from flask_session import Session from flask_mail import Mail APP = Flask(__name__) APP.config.from_pyfile('config.py') MAIL = Mail(APP) MANAGER = Manager(APP) BOOTSTRAP = Bootstrap(APP) SESSION = Session(APP) if APP.config["DEBUG"]: @APP.after_request def after_request(response): response.headers[ "Cache-Control"] = "no-cache, no-store, must-revalidate" response.headers["Expires"] = 0 response.headers["Pragma"] = "no-cache" return response db = SQL("sqlite:///fproj.db") from views import *
import sqlite3 import sys import json from flask_bcrypt import Bcrypt from flask_bcrypt import check_password_hash from tzlocal import get_localzone import datetime from pytz import timezone from bs4 import BeautifulSoup f = 0 name_aricle = 0 title_article = 0 app.config['SESSION_TYPE'] = 'memcached' app.config['SECRET_KEY'] = 'super key' sess = Session() app.config['PERMANENT_SESSION_LIFETIME'] = datetime.timedelta(days=365) UPLOAD_FOLDER = '/home/denis/app/static/video/' ALLOWED_EXTENSIONS = set(['mp4', 'mpeg', '3g2', '3gp', 'asf']) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER DATABASE = '/home/denis/app/sql/video_anketa.bd' bcrypt = Bcrypt(app) @app.route("/cookie/<email>") def cookie(email): session['email'] = email print(session.get('email')) return redirect("http://185.3.94.78:5000/Всё")
# create application instance app = Flask(__name__, static_folder="../static") logging.basicConfig(level=logging.DEBUG) app.config['SESSION_TYPE'] = 'sqlalchemy' app.config['SESSION_COOKIE_NAME'] = 'PHPSESSID' config = db_connection.config app.config[ 'SQLALCHEMY_DATABASE_URI'] = f"mariadb+mariadbconnector://{config['user']}:{config['password']}@{config['host']}:{config['port']}/{config['database']}" app.config[ 'SQLALCHEMY_TRACK_MODIFICATIONS'] = False # quiet warning message, deprecated but required alchemy = SQLAlchemy(app) app.config['SESSION_SQLALCHEMY'] = alchemy server_session = Session(app) #TODO: find out if this is bad server_session.app.session_interface.db.create_all() login_manager = LoginManager() login_manager.init_app(app) #pusher credentials configuration app.config['PUSHER_APP_ID'] = os.environ['PUSHER_APP_ID'] app.config['PUSHER_KEY'] = os.environ['PUSHER_KEY'] app.config['PUSHER_CLUSTER'] = os.environ['PUSHER_CLUSTER'] app.config['PUSHER_SECRET'] = os.environ['PUSHER_SECRET'] app.config['PUSHER_SSL'] = False #pusher server location configuration if 'PUSHER_HOST' in os.environ and 'PUSHER_PORT' in os.environ: app.config['PUSHER_HOST'] = os.environ['PUSHER_HOST']
def create_app(): app = Flask(__name__, template_folder='../templates', static_folder='../static') app.json_encoder = CustomJSONEncoder cache_buster = CacheBuster(config={'extensions': ['.js', '.css']}) cache_buster.init_app(app) Compress(app) if args.cors: CORS(app) db_uri = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4'.format( args.db_user, args.db_pass, args.db_host, args.db_port, args.db_name) app.config['SQLALCHEMY_DATABASE_URI'] = db_uri app.config['SQLALCHEMY_ENGINE_OPTIONS'] = { 'pool_size': 0 # No limit. } app.config['SQLALCHEMY_POOL_RECYCLE'] = args.db_pool_recycle app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app) if args.client_auth: app.config['SESSION_TYPE'] = 'redis' r = redis.Redis(args.redis_host, args.redis_port) app.config['SESSION_REDIS'] = r app.config['SESSION_USE_SIGNER'] = True app.config['PERMANENT_SESSION_LIFETIME'] = \ timedelta(days=args.session_duration) app.secret_key = args.secret_key Session(app) if args.basic_auth: accepted_auth_types.append('basic') for config in args.basic_auth_access_configs: name = config.split(':')[1] if name not in valid_access_configs: valid_access_configs.append(name) if args.discord_auth: accepted_auth_types.append('discord') for config in args.discord_access_configs: length = len(config.split(':')) name = (config.split(':')[2] if length == 3 else config.split(':')[1]) if name not in valid_access_configs: valid_access_configs.append(name) if args.telegram_auth: accepted_auth_types.append('telegram') for config in args.telegram_access_configs: name = config.split(':')[1] if name not in valid_access_configs: valid_access_configs.append(name) image_generator = ImageGenerator() @app.before_request def validate_request(): # Get real IP behind trusted reverse proxy. ip_addr = request.remote_addr if ip_addr in args.trusted_proxies: ip_addr = request.headers.get('X-Forwarded-For', ip_addr) if args.client_auth: session['ip'] = ip_addr session['last_active'] = datetime.utcnow() @app.route('/') @auth_required def map_page(*_args, **kwargs): if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) user_args = get_args(kwargs['access_config']) settings = { 'centerLat': user_args.center_lat, 'centerLng': user_args.center_lng, 'customTileServers': user_args.custom_tile_servers, 'maxZoomLevel': user_args.max_zoom_level, 'showAllZoomLevel': user_args.show_all_zoom_level, 'clusterZoomLevel': user_args.cluster_zoom_level, 'clusterZoomLevelMobile': user_args.cluster_zoom_level_mobile, 'maxClusterRadius': user_args.max_cluster_radius, 'spiderfyClusters': user_args.spiderfy_clusters, 'removeMarkersOutsideViewport': (not user_args.markers_outside_viewport), 'autoPanPopup': not user_args.no_autopan_popup, 'geocoder': not user_args.no_geocoder, 'isStartMarkerMovable': not user_args.lock_start_marker, 'generateImages': user_args.generate_images, 'statsSidebar': not user_args.no_stats_sidebar, 'twelveHourClock': user_args.twelve_hour_clock, 'mapUpdateInverval': user_args.map_update_interval, 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always, 'pokemons': not user_args.no_pokemon, 'upscaledPokemon': ([int(i) for i in user_args.upscaled_pokemon.split(',')] if user_args.upscaled_pokemon is not None else []), 'pokemonValues': (not user_args.no_pokemon and not user_args.no_pokemon_values), 'catchRates': user_args.catch_rates, 'rarity': (not user_args.no_pokemon and user_args.rarity and user_args.rarity_update_frequency), 'rarityFileName': user_args.rarity_filename, 'pokemonCries': (not user_args.no_pokemon and user_args.pokemon_cries), 'gyms': not user_args.no_gyms, 'gymSidebar': ((not user_args.no_gyms or not user_args.no_raids) and not user_args.no_gym_sidebar), 'gymFilters': (not user_args.no_gyms and not user_args.no_gym_filters), 'raids': not user_args.no_raids, 'raidFilters': (not user_args.no_raids and not user_args.no_raid_filters), 'pokestops': not user_args.no_pokestops, 'quests': not user_args.no_pokestops and not user_args.no_quests, 'invasions': (not user_args.no_pokestops and not user_args.no_invasions), 'lures': not user_args.no_pokestops and not user_args.no_lures, 'weather': not user_args.no_weather, 'spawnpoints': not user_args.no_spawnpoints, 'scannedLocs': not user_args.no_scanned_locs, 's2Cells': not user_args.no_s2_cells, 'ranges': not user_args.no_ranges, 'nests': user_args.nests, 'nestParks': user_args.nest_parks, 'nestParksFileName': user_args.nest_parks_filename, 'exParks': user_args.ex_parks, 'exParksFileName': user_args.ex_parks_filename } return render_template( 'map.html', version=version, lang=user_args.locale, map_title=user_args.map_title, custom_favicon=user_args.custom_favicon, header_image=not user_args.no_header_image, header_image_name=user_args.header_image, client_auth=user_args.client_auth, logged_in=is_logged_in(), admin=is_admin(), madmin_url=user_args.madmin_url, donate_url=user_args.donate_url, patreon_url=user_args.patreon_url, discord_url=user_args.discord_url, messenger_url=user_args.messenger_url, telegram_url=user_args.telegram_url, whatsapp_url=user_args.whatsapp_url, pokemon_history_page=(settings['pokemons'] and not user_args.no_pokemon_history_page), quest_page=settings['quests'] and not user_args.no_quest_page, analytics_id=user_args.analytics_id, settings=settings, i18n=i18n) @app.route('/pokemon-history') @auth_required def pokemon_history_page(*_args, **kwargs): if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) user_args = get_args(kwargs['access_config']) if user_args.no_pokemon or user_args.no_pokemon_history_page: if args.client_auth: if is_logged_in(): abort(403) else: return redirect(url_for('login_page')) else: abort(404) settings = { 'centerLat': user_args.center_lat, 'centerLng': user_args.center_lng, 'generateImages': user_args.generate_images, 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always } return render_template('pokemon-history.html', version=version, lang=user_args.locale, map_title=user_args.map_title, custom_favicon=user_args.custom_favicon, header_image=not user_args.no_header_image, header_image_name=user_args.header_image, client_auth=user_args.client_auth, logged_in=is_logged_in(), admin=is_admin(), madmin_url=user_args.madmin_url, donate_url=user_args.donate_url, patreon_url=user_args.patreon_url, discord_url=user_args.discord_url, messenger_url=user_args.messenger_url, telegram_url=user_args.telegram_url, whatsapp_url=user_args.whatsapp_url, quest_page=(not user_args.no_pokestops and not user_args.no_quests and not user_args.no_quest_page), analytics_id=user_args.analytics_id, settings=settings, i18n=i18n) @app.route('/quests') @auth_required def quest_page(*_args, **kwargs): if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) user_args = get_args(kwargs['access_config']) if (user_args.no_pokestops or user_args.no_quests or user_args.no_quest_page): if args.client_auth: if is_logged_in(): abort(403) else: return redirect(url_for('login_page')) else: abort(404) settings = { 'generateImages': user_args.generate_images, 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always } return render_template( 'quest.html', version=version, lang=user_args.locale, map_title=user_args.map_title, custom_favicon=user_args.custom_favicon, header_image=not user_args.no_header_image, header_image_name=user_args.header_image, client_auth=user_args.client_auth, logged_in=is_logged_in(), admin=is_admin(), madmin_url=user_args.madmin_url, donate_url=user_args.donate_url, patreon_url=user_args.patreon_url, discord_url=user_args.discord_url, messenger_url=user_args.messenger_url, telegram_url=user_args.telegram_url, whatsapp_url=user_args.whatsapp_url, pokemon_history_page=(not user_args.no_pokemon and not user_args.no_pokemon_history_page), analytics_id=user_args.analytics_id, settings=settings, i18n=i18n) @app.route('/mobile') @auth_required def mobile_page(*_args, **kwargs): if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) user_args = get_args(kwargs['access_config']) # todo: Check if client is Android/iOS/Desktop for geolink, currently # only supports Android. pokemon_list = [] settings = { 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always } # Allow client to specify location. lat = request.args.get('lat', user_args.center_lat, type=float) lon = request.args.get('lon', user_args.center_lng, type=float) origin_point = LatLng.from_degrees(lat, lon) for pokemon in convert_pokemon_list( Pokemon.get_active(None, None, None, None)): pokemon_point = LatLng.from_degrees(pokemon['latitude'], pokemon['longitude']) diff = pokemon_point - origin_point diff_lat = diff.lat().degrees diff_lng = diff.lng().degrees direction = (('N' if diff_lat >= 0 else 'S') if abs(diff_lat) > 1e-4 else '') +\ (('E' if diff_lng >= 0 else 'W') if abs(diff_lng) > 1e-4 else '') entry = { 'id': pokemon['pokemon_id'], 'name': get_pokemon_name(pokemon['pokemon_id']), 'card_dir': direction, 'distance': int( origin_point.get_distance(pokemon_point).radians * 6366468.241830914), 'time_to_disappear': '%d min %d sec' % (divmod( (pokemon['disappear_time'] - datetime.utcnow()).seconds, 60)), 'disappear_time': pokemon['disappear_time'], 'disappear_sec': (pokemon['disappear_time'] - datetime.utcnow()).seconds, 'latitude': pokemon['latitude'], 'longitude': pokemon['longitude'] } pokemon_list.append((entry, entry['distance'])) pokemon_list = [y[0] for y in sorted(pokemon_list, key=lambda x: x[1])] return render_template('mobile.html', version=version, custom_favicon=user_args.custom_favicon, pokemon_list=pokemon_list, origin_lat=lat, origin_lng=lon, analytics_id=user_args.analytics_id, settings=settings, i18n=i18n) @app.route('/login') def login_page(): if not args.client_auth: abort(404) if is_logged_in(): return redirect(url_for('map_page')) settings = { 'motd': args.motd, 'motdTitle': args.motd_title, 'motdText': args.motd_text, 'motdPages': args.motd_pages, 'showMotdAlways': args.show_motd_always } return render_template( 'login.html', version=version, lang=args.locale, map_title=args.map_title, custom_favicon=args.custom_favicon, header_image=not args.no_header_image, header_image_name=args.header_image, madmin_url=args.madmin_url, donate_url=args.donate_url, patreon_url=args.patreon_url, discord_url=args.discord_url, messenger_url=args.messenger_url, telegram_url=args.telegram_url, whatsapp_url=args.whatsapp_url, analytics_id=args.analytics_id, basic_auth=args.basic_auth, discord_auth=args.discord_auth, telegram_auth=args.telegram_auth, pokemon_history_page=(not args.no_pokemon and not args.no_pokemon_history_page), quest_page=(not args.no_pokestops and not args.no_quests and not args.no_quest_page), settings=settings, i18n=i18n) @app.route('/login/<auth_type>') def login(auth_type): if not args.client_auth: abort(404) if is_logged_in(): return redirect(url_for('map_page')) if auth_type not in accepted_auth_types: abort(404) authenticator = auth_factory.get_authenticator(auth_type) auth_uri = authenticator.get_authorization_url() return redirect(auth_uri) @app.route('/login/basic') def basic_login_page(): if not args.basic_auth: abort(404) settings = { 'motd': args.motd, 'motdTitle': args.motd_title, 'motdText': args.motd_text, 'motdPages': args.motd_pages, 'showMotdAlways': args.show_motd_always } return render_template( 'basic-login.html', version=version, lang=args.locale, map_title=args.map_title, custom_favicon=args.custom_favicon, header_image=not args.no_header_image, header_image_name=args.header_image, madmin_url=args.madmin_url, donate_url=args.donate_url, patreon_url=args.patreon_url, discord_url=args.discord_url, messenger_url=args.messenger_url, telegram_url=args.telegram_url, whatsapp_url=args.whatsapp_url, pokemon_history_page=(not args.no_pokemon and not args.no_pokemon_history_page), quest_page=(not args.no_pokestops and not args.no_quests and not args.no_quest_page), analytics_id=args.analytics_id, settings=settings, i18n=i18n) @app.route('/login/telegram') def telegram_login_page(): if not args.telegram_auth: abort(404) settings = { 'motd': args.motd, 'motdTitle': args.motd_title, 'motdText': args.motd_text, 'motdPages': args.motd_pages, 'showMotdAlways': args.show_motd_always } return render_template( 'telegram.html', version=version, lang=args.locale, map_title=args.map_title, custom_favicon=args.custom_favicon, header_image=not args.no_header_image, header_image_name=args.header_image, madmin_url=args.madmin_url, donate_url=args.donate_url, patreon_url=args.patreon_url, discord_url=args.discord_url, messenger_url=args.messenger_url, telegram_url=args.telegram_url, whatsapp_url=args.whatsapp_url, pokemon_history_page=(not args.no_pokemon and not args.no_pokemon_history_page), quest_page=(not args.no_pokestops and not args.no_quests and not args.no_quest_page), analytics_id=args.analytics_id, telegram_bot_username=args.telegram_bot_username, server_uri=args.server_uri, settings=settings, i18n=i18n) @app.route('/auth/<auth_type>') def auth(auth_type): if not args.client_auth: abort(404) if is_logged_in(): return redirect(url_for('map_page')) if auth_type not in accepted_auth_types: abort(404) success = auth_factory.get_authenticator(auth_type).authorize() if not success: if auth_type == 'basic': return redirect(url_for('basic_login_page') + '?success=false') elif auth_type == 'discord': return redirect(url_for('login_page')) elif auth_type == 'telegram': return redirect(url_for('telegram_login_page')) if args.no_multiple_logins: r = app.config['SESSION_REDIS'] sessions = get_sessions(r) for s in sessions: if ('auth_type' in s and s['auth_type'] == session['auth_type'] and s['id'] == session['id']): r.delete('session:' + s['session_id']) return redirect(url_for('map_page')) @app.route('/logout') def logout(): if not args.client_auth: abort(404) if is_logged_in(): if session['auth_type'] in accepted_auth_types: a = auth_factory.get_authenticator(session['auth_type']) a.end_session() else: session.clear() return redirect(url_for('map_page')) @app.route('/admin') def admin_page(): return redirect(url_for('users_page')) @app.route('/admin/users') @auth_required def users_page(*_args, **kwargs): if not args.client_auth: abort(404) if not kwargs['has_permission']: return redirect(kwargs['redirect_uri']) if not is_admin(): abort(403) user_args = get_args(kwargs['access_config']) settings = { 'motd': user_args.motd, 'motdTitle': user_args.motd_title, 'motdText': user_args.motd_text, 'motdPages': user_args.motd_pages, 'showMotdAlways': user_args.show_motd_always } return render_template( 'users.html', version=version, lang=user_args.locale, map_title=user_args.map_title, custom_favicon=user_args.custom_favicon, header_image=not user_args.no_header_image, header_image_name=user_args.header_image, madmin_url=user_args.madmin_url, donate_url=user_args.donate_url, patreon_url=user_args.patreon_url, discord_url=user_args.discord_url, messenger_url=user_args.messenger_url, telegram_url=user_args.telegram_url, whatsapp_url=user_args.whatsapp_url, analytics_id=user_args.analytics_id, pokemon_history_page=(not user_args.no_pokemon and not user_args.no_pokemon_history_page), quest_page=(not user_args.no_pokestops and not user_args.no_quests and not user_args.no_quest_page), settings=settings, i18n=i18n) @app.route('/raw-data') @auth_required def raw_data(*_args, **kwargs): if not kwargs['has_permission']: abort(401) user_args = get_args(kwargs['access_config']) # Make sure fingerprint isn't blacklisted. fingerprint_blacklisted = any([fingerprints['no_referrer'](request)]) if fingerprint_blacklisted: log.debug('User denied access: blacklisted fingerprint.') abort(403) d = {} # Request time of this request. d['timestamp'] = datetime.utcnow() # Request time of previous request. if request.args.get('timestamp'): timestamp = int(request.args.get('timestamp')) timestamp -= 1000 # Overlap, for rounding errors. else: timestamp = 0 swLat = request.args.get('swLat') swLng = request.args.get('swLng') neLat = request.args.get('neLat') neLng = request.args.get('neLng') oSwLat = request.args.get('oSwLat') oSwLng = request.args.get('oSwLng') oNeLat = request.args.get('oNeLat') oNeLng = request.args.get('oNeLng') # Pass current coords as old coords. d['oSwLat'] = swLat d['oSwLng'] = swLng d['oNeLat'] = neLat d['oNeLng'] = neLng if (oSwLat is not None and oSwLng is not None and oNeLat is not None and oNeLng is not None): # If old coords are not equal to current coords we have # moved/zoomed! if (oSwLng < swLng and oSwLat < swLat and oNeLat > neLat and oNeLng > neLng): new_area = False # We zoomed in no new area uncovered. elif not (oSwLat == swLat and oSwLng == swLng and oNeLat == neLat and oNeLng == neLng): new_area = True else: new_area = False pokemon = (request.args.get('pokemon') == 'true' and not user_args.no_pokemon) seen = request.args.get('seen') == 'true' appearances = request.args.get('appearances') == 'true' appearances_details = request.args.get('appearancesDetails') == 'true' gyms = request.args.get('gyms') == 'true' and not user_args.no_gyms raids = request.args.get('raids') == 'true' and not user_args.no_raids pokestops = (request.args.get('pokestops') == 'true' and not user_args.no_pokestops) eventless_pokestops = request.args.get('eventlessPokestops') == 'true' quests = (request.args.get('quests') == 'true' and not user_args.no_quests) invasions = (request.args.get('invasions') == 'true' and not user_args.no_invasions) lures = request.args.get('lures') == 'true' and not user_args.no_lures weather = (request.args.get('weather') == 'true' and not user_args.no_weather) spawnpoints = (request.args.get('spawnpoints') == 'true' and not user_args.no_spawnpoints) scanned_locs = (request.args.get('scannedLocs') == 'true' and not user_args.no_scanned_locs) nests = request.args.get('nests') == 'true' and user_args.nests exclude_nearby_cells = request.args.get('excludeNearbyCells') == 'true' all_pokemon = request.args.get('allPokemon') == 'true' all_gyms = request.args.get('allGyms') == 'true' all_pokestops = request.args.get('allPokestops') == 'true' all_weather = request.args.get('allWeather') == 'true' all_spawnpoints = request.args.get('allSpawnpoints') == 'true' all_scanned_locs = request.args.get('allScannedLocs') == 'true' all_nests = request.args.get('allNests') == 'true' if all_pokemon: d['allPokemon'] = True if all_gyms: d['allGyms'] = True if all_pokestops: d['allPokestops'] = True if all_weather: d['allWeather'] = True if all_spawnpoints: d['allSpawnpoints'] = True if all_scanned_locs: d['allScannedLocs'] = True if all_nests: d['allNests'] = True if exclude_nearby_cells: d['excludeNearbyCells'] = True geofences = (parse_geofence_file('geofences/' + user_args.geofence_file) if user_args.geofence_file else None) exclude_geofences = ( parse_geofence_file('geofences/' + user_args.geofence_exclude_file) if user_args.geofence_exclude_file else None) if pokemon: verified_despawn = user_args.verified_despawn_time eids = None ids = None if (request.args.get('eids') and request.args.get('prionotif', 'false') == 'false'): request_eids = request.args.get('eids').split(',') eids = [int(i) for i in request_eids] elif not request.args.get('eids') and request.args.get('ids'): request_ids = request.args.get('ids').split(',') ids = [int(i) for i in request_ids] if timestamp == 0 or all_pokemon: # If this is first request since switch on, load # all pokemon on screen. d['pokemons'] = convert_pokemon_list( Pokemon.get_active( swLat, swLng, neLat, neLng, eids=eids, ids=ids, geofences=geofences, exclude_geofences=exclude_geofences, verified_despawn_time=verified_despawn, exclude_nearby_cells=exclude_nearby_cells)) else: # If map is already populated only request modified Pokemon # since last request time. d['pokemons'] = convert_pokemon_list( Pokemon.get_active( swLat, swLng, neLat, neLng, timestamp=timestamp, eids=eids, ids=ids, geofences=geofences, exclude_geofences=exclude_geofences, verified_despawn_time=verified_despawn, exclude_nearby_cells=exclude_nearby_cells)) if new_area: # If screen is moved add newly uncovered Pokemon to the # ones that were modified since last request time. d['pokemons'].extend( convert_pokemon_list( Pokemon.get_active( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, eids=eids, ids=ids, geofences=geofences, exclude_geofences=exclude_geofences, verified_despawn_time=verified_despawn, exclude_nearby_cells=exclude_nearby_cells))) if request.args.get('reids'): request_reids = request.args.get('reids').split(',') reids = [int(x) for x in request_reids] d['pokemons'] += convert_pokemon_list( Pokemon.get_active( swLat, swLng, neLat, neLng, ids=reids, geofences=geofences, exclude_geofences=exclude_geofences, verified_despawn_time=verified_despawn, exclude_nearby_cells=exclude_nearby_cells)) d['reids'] = reids if seen: d['seen'] = Pokemon.get_seen(int(request.args.get('duration')), geofences=geofences, exclude_geofences=exclude_geofences) if appearances: d['appearances'] = Pokemon.get_appearances( request.args.get('pokemonid'), request.args.get('formid'), int(request.args.get('duration')), geofences=geofences, exclude_geofences=exclude_geofences) if appearances_details: d['appearancesTimes'] = ( Pokemon.get_appearances_times_by_spawnpoint( request.args.get('pokemonid'), request.args.get('spawnpoint_id'), request.args.get('formid'), int(request.args.get('duration')), geofences=geofences, exclude_geofences=exclude_geofences)) if gyms or raids: if timestamp == 0 or all_gyms: d['gyms'] = Gym.get_gyms(swLat, swLng, neLat, neLng, raids=raids, geofences=geofences, exclude_geofences=exclude_geofences) else: d['gyms'] = Gym.get_gyms(swLat, swLng, neLat, neLng, timestamp=timestamp, raids=raids, geofences=geofences, exclude_geofences=exclude_geofences) if new_area: d['gyms'].extend( Gym.get_gyms(swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, raids=raids, geofences=geofences, exclude_geofences=exclude_geofences)) if pokestops and (eventless_pokestops or quests or invasions or lures): if timestamp == 0 or all_pokestops: d['pokestops'] = Pokestop.get_pokestops( swLat, swLng, neLat, neLng, eventless_stops=eventless_pokestops, quests=quests, invasions=invasions, lures=lures, geofences=geofences, exclude_geofences=exclude_geofences) else: d['pokestops'] = Pokestop.get_pokestops( swLat, swLng, neLat, neLng, timestamp=timestamp, eventless_stops=eventless_pokestops, quests=quests, invasions=invasions, lures=lures, geofences=geofences, exclude_geofences=exclude_geofences) if new_area: d['pokestops'].extend( Pokestop.get_pokestops( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, eventless_stops=eventless_pokestops, quests=quests, invasions=invasions, lures=lures, geofences=geofences, exclude_geofences=exclude_geofences)) if weather: if timestamp == 0 or all_weather: d['weather'] = Weather.get_weather( swLat, swLng, neLat, neLng, geofences=geofences, exclude_geofences=exclude_geofences) else: d['weather'] = Weather.get_weather( swLat, swLng, neLat, neLng, timestamp=timestamp, geofences=geofences, exclude_geofences=exclude_geofences) if new_area: d['weather'] += Weather.get_weather( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, geofences=geofences, exclude_geofences=exclude_geofences) if spawnpoints: if timestamp == 0 or all_spawnpoints: d['spawnpoints'] = TrsSpawn.get_spawnpoints( swLat=swLat, swLng=swLng, neLat=neLat, neLng=neLng, geofences=geofences, exclude_geofences=exclude_geofences) else: d['spawnpoints'] = TrsSpawn.get_spawnpoints( swLat=swLat, swLng=swLng, neLat=neLat, neLng=neLng, timestamp=timestamp, geofences=geofences, exclude_geofences=exclude_geofences) if new_area: d['spawnpoints'] += TrsSpawn.get_spawnpoints( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, geofences=geofences, exclude_geofences=exclude_geofences) if scanned_locs: if timestamp == 0 or all_scanned_locs: d['scannedlocs'] = ScannedLocation.get_recent( swLat, swLng, neLat, neLng, geofences=geofences, exclude_geofences=exclude_geofences) else: d['scannedlocs'] = ScannedLocation.get_recent( swLat, swLng, neLat, neLng, timestamp=timestamp, geofences=geofences, exclude_geofences=exclude_geofences) if new_area: d['scannedlocs'] += ScannedLocation.get_recent( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, geofences=geofences, exclude_geofences=exclude_geofences) if nests: if timestamp == 0 or all_nests: d['nests'] = Nest.get_nests( swLat, swLng, neLat, neLng, geofences=geofences, exclude_geofences=exclude_geofences) else: d['nests'] = Nest.get_nests( swLat, swLng, neLat, neLng, timestamp=timestamp, geofences=geofences, exclude_geofences=exclude_geofences) if new_area: d['nests'] += Nest.get_nests( swLat, swLng, neLat, neLng, oSwLat=oSwLat, oSwLng=oSwLng, oNeLat=oNeLat, oNeLng=oNeLng, geofences=geofences, exclude_geofences=exclude_geofences) return jsonify(d) @app.route('/raw-data/users') def users_data(): if not args.client_auth: abort(404) if not is_admin(): abort(403) sessions = get_sessions(app.config['SESSION_REDIS']) users = [] for s in sessions: if 'auth_type' in s: del s['_permanent'] users.append(s) return jsonify(users) @app.route('/pkm_img') def pokemon_img(): raw = 'raw' in request.args pkm = int(request.args.get('pkm')) gender = int(request.args.get('gender', '0')) form = int(request.args.get('form', '0')) costume = int(request.args.get('costume', '0')) evolution = int(request.args.get('evolution', '0')) shiny = 'shiny' in request.args weather = int(request.args.get('weather', '0')) if raw: filename = image_generator.get_pokemon_raw_icon( pkm, gender=gender, form=form, costume=costume, evolution=evolution, shiny=shiny, weather=weather) else: filename = image_generator.get_pokemon_map_icon( pkm, gender=gender, form=form, costume=costume, evolution=evolution, weather=weather) return send_file(filename, mimetype='image/png') @app.route('/gym_img') def gym_img(): team = request.args.get('team') level = int(request.args.get('level')) raid_level = int(request.args.get('raid-level', '0')) pkm = int(request.args.get('pkm', '0')) form = int(request.args.get('form', '0')) costume = int(request.args.get('costume', '0')) evolution = int(request.args.get('evolution', '0')) in_battle = 'in-battle' in request.args ex_raid_eligible = 'ex-raid-eligible' in request.args if level < 0 or level > 6 or (pkm > 0 and raid_level == 0): abort(400) return send_file(image_generator.get_gym_icon(team, level, raid_level, pkm, form, costume, evolution, ex_raid_eligible, in_battle), mimetype='image/png') @app.route('/robots.txt') def render_robots_txt(): return render_template('robots.txt') @app.route('/serviceWorker.min.js') def render_service_worker_js(): return send_from_directory('../static/dist/js', 'serviceWorker.min.js') return app
import requests import json import redis from flask_session import Session from flask_caching import Cache config = {'DEBUG': True, 'CACHE_TYPE': 'simple', 'CACHE_DEFAULT_TIMEOUT': 300} app = Flask(__name__) app.config.from_mapping(config) cache = Cache(app) app.config['SESSION_TYPE'] = 'redis' app.config['SESSION_REDIS'] = redis.from_url('redis://session_server:6379') sess = Session() sess.init_app(app) auth = HTTPBasicAuth('admin', 'devops101') def getSession(key): return session.get(key, 'none') def setSession(key, value): session[key] = value def resetSession(): session.clear()
if app.config['AWS_S3_BUCKET_NAME'] is None: raise ValueError('AWS_S3_BUCKET_NAME environment variable is not set.') if app.config['AWS_ACCESS_KEY'] is None: raise ValueError('AWS_ACCESS_KEY environment variable is not set.') if app.config['AWS_SECRET_ACCESS_KEY'] is None: raise ValueError('AWS_SECRET_ACCESS_KEY environment variable is not set.') s3 = boto3.resource( 's3', aws_access_key_id=app.config['AWS_ACCESS_KEY'], aws_secret_access_key=app.config['AWS_SECRET_ACCESS_KEY'] ) session = Session(app) # Ensure responses aren't cached @app.after_request def after_request(response): response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate' response.headers['Expires'] = 0 response.headers['Pragma'] = 'no-cache' return response @app.context_processor def inject_s3(): return dict(s3=s3)
import flask from flask_oauthlib.client import OAuth from flask_socketio import SocketIO, emit, join_room from flask_session import Session import gevent from gevent import monkey monkey.patch_all() import config APP = flask.Flask(__name__, template_folder='static/templates') APP.debug = True APP.secret_key = 'development' APP.config['SESSION_TYPE'] = 'filesystem' Session(APP) socketio = SocketIO(APP, manage_session=False, async_mode="gevent") OAUTH = OAuth(APP) MSGRAPH = OAUTH.remote_app( 'microsoft', consumer_key=config.CLIENT_ID, consumer_secret=config.CLIENT_SECRET, request_token_params={'scope': config.SCOPES}, base_url=config.RESOURCE + config.API_VERSION + '/', request_token_url=None, access_token_method='POST', access_token_url=config.AUTHORITY_URL + config.TOKEN_ENDPOINT, authorize_url=config.AUTHORITY_URL + config.AUTH_ENDPOINT) @APP.route('/')
def init_ext(app): Session(app=app) db.init_app(app=app) migrate = Migrate() migrate.init_app(app=app, db=db)
def init_ext(app): Session(app) migrate.init_app(app=app, db=db) db.init_app(app) cache.init_app(app) mail.init_app(app)