Spaces:
Sleeping
Sleeping
# common.py | |
import extra_streamlit_components as stx | |
import streamlit as st | |
import logging | |
import os | |
from time import time | |
from requests_oauthlib import OAuth2Session | |
from streamlit import runtime | |
from streamlit.runtime.scriptrunner import get_script_run_ctx | |
import ipaddress | |
logging.basicConfig(level=logging.INFO) | |
logger = logging.getLogger("__name__") | |
logger.debug("調査用ログ") | |
# 接続元制御 | |
ALLOW_IP_ADDRESS = os.environ["ALLOW_IP_ADDRESS"] | |
# 接続元IP取得 | |
def get_remote_ip(): | |
ctx = get_script_run_ctx() | |
session_info = runtime.get_instance().get_client(ctx.session_id) | |
return session_info.request.remote_ip | |
# 接続元IP許可判定 | |
def is_allow_ip_address(): | |
remote_ip = get_remote_ip() | |
logger.info("remote_ip") | |
logger.info(remote_ip) | |
# localhost | |
if remote_ip == "::1": | |
return True | |
# プライベートIP | |
ipaddr = ipaddress.IPv4Address(remote_ip) | |
logger.info("ipaddr") | |
logger.info(ipaddr) | |
if ipaddr.is_private: | |
return True | |
# その他(許可リスト判定) | |
return remote_ip in ALLOW_IP_ADDRESS | |
#ログインの確認 | |
def check_login(): | |
if 'authentication_status' not in st.session_state: | |
st.session_state['authentication_status'] = None | |
if st.session_state["authentication_status"] is None or False: | |
st.warning("**ログインしてください**") | |
st.stop() | |