mirror of
https://github.com/j93es/browser-use-oauth.git
synced 2026-06-04 07:21:52 +09:00
Refactor authentication and session management
- Removed old llm_login and session scripts, replacing them with a new structure for handling SSO login and session management. - Introduced a new prompt system for collecting SSO redirect URLs, ensuring compliance with security protocols. - Implemented a robust backend notification system for tracking scan initiation. - Enhanced browser profile configuration and resource management for improved session handling. - Added utility functions for environment variable checks and logging. - Updated the overall architecture to improve maintainability and readability.
This commit is contained in:
parent
2d8a7d5cfb
commit
b68425f523
16 changed files with 251 additions and 232 deletions
40
lib/utils/__init__.py
Normal file
40
lib/utils/__init__.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
from lib.utils.config import (
|
||||
BACKEND_URL,
|
||||
GOOGLE_API_KEY,
|
||||
GOOGLE_MODEL,
|
||||
GOOGLE_PLANNER_MODEL,
|
||||
)
|
||||
|
||||
|
||||
def show_info():
|
||||
print("🔧 환경 설정:")
|
||||
print(browser_use_version())
|
||||
print(f"🔗 Backend URL: {BACKEND_URL}")
|
||||
print(
|
||||
f"🔑 Google API Key: {'*' * (len(GOOGLE_API_KEY) - 4) + GOOGLE_API_KEY[-4:] if GOOGLE_API_KEY else None}"
|
||||
)
|
||||
print(f"🌐 Google Model: {GOOGLE_MODEL}")
|
||||
print(f"🌐 Google Planner Model: {GOOGLE_PLANNER_MODEL}")
|
||||
|
||||
|
||||
def browser_use_version():
|
||||
try:
|
||||
# run uv pip show browser-use
|
||||
import subprocess
|
||||
|
||||
result = subprocess.run(
|
||||
["uv", "pip", "show", "browser-use"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True,
|
||||
)
|
||||
|
||||
print("📦 Browser Use 패키지 정보:")
|
||||
return result.stdout.strip()
|
||||
except ImportError:
|
||||
return None
|
||||
|
||||
|
||||
def env_cheker():
|
||||
if GOOGLE_API_KEY is None:
|
||||
raise ValueError("GOOGLE_API_KEY 환경변수가 설정되지 않았습니다.")
|
||||
21
lib/utils/backend_client.py
Normal file
21
lib/utils/backend_client.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import requests
|
||||
from config import BACKEND_URL
|
||||
|
||||
def notify_backend(target_url):
|
||||
# Backend에 스캔 시작을 알림
|
||||
try:
|
||||
response = requests.post(
|
||||
f"{BACKEND_URL}/start", params={"url": target_url}, timeout=5
|
||||
)
|
||||
if response.status_code == 200:
|
||||
print(f"✅ Backend notified: {response.text}")
|
||||
else:
|
||||
print(f"⚠️ Backend notification failed: {response.status_code}")
|
||||
except requests.exceptions.ConnectionError:
|
||||
print(
|
||||
f"⚠️ Backend server not available at {BACKEND_URL}. Continuing without notification."
|
||||
)
|
||||
except requests.exceptions.Timeout:
|
||||
print(f"⚠️ Backend notification timed out. Continuing without notification.")
|
||||
except Exception as e:
|
||||
print(f"⚠️ Failed to notify backend: {e}")
|
||||
29
lib/utils/browser_use/__init__.py
Normal file
29
lib/utils/browser_use/__init__.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from func import *
|
||||
import clean_resources as clean_resources_func
|
||||
|
||||
# Initialize configuration
|
||||
proxy_url = setup_proxy()
|
||||
storage_state_path = setup_storage_state()
|
||||
|
||||
# Create browser profile
|
||||
profile = BrowserProfile(
|
||||
# Security settings
|
||||
disable_security=True,
|
||||
stealth=True,
|
||||
|
||||
# Display settings
|
||||
headless=False,
|
||||
device_scale_factor=1,
|
||||
window_size={"width": 1600, "height": 900},
|
||||
viewport={"width": 1600, "height": 900},
|
||||
|
||||
# Data persistence
|
||||
user_data_dir=None,
|
||||
storage_state=storage_state_path,
|
||||
|
||||
# Network settings
|
||||
proxy={"server": proxy_url} if proxy_url else None,
|
||||
|
||||
# Additional arguments
|
||||
args=get_browser_args(),
|
||||
)
|
||||
25
lib/utils/browser_use/clean_resources.py
Normal file
25
lib/utils/browser_use/clean_resources.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from pathlib import Path
|
||||
|
||||
async def clean_resources(agent=None, session=None):
|
||||
"""리소스를 정리하는 함수"""
|
||||
storage_state_temp_path = Path("./data/storage_state_temp.json").resolve()
|
||||
if storage_state_temp_path.exists():
|
||||
try:
|
||||
# remove file
|
||||
print(f"🗑️ 임시 스토리지 상태 파일 삭제 중: {storage_state_temp_path}")
|
||||
# unlink removes the file
|
||||
storage_state_temp_path.unlink()
|
||||
print("🗑️ 임시 스토리지 상태 파일 삭제 완료.")
|
||||
except Exception as e:
|
||||
print(f"⚠️ 임시 스토리지 상태 파일 삭제 실패: {e}")
|
||||
|
||||
if agent:
|
||||
try:
|
||||
await agent.close()
|
||||
except Exception as e:
|
||||
print(f"⚠️ 에이전트 리소스 정리 실패: {e}")
|
||||
if session:
|
||||
try:
|
||||
await session.close()
|
||||
except Exception as e:
|
||||
print(f"⚠️ 세션 리소스 정리 실패: {e}")
|
||||
69
lib/utils/browser_use/func.py
Normal file
69
lib/utils/browser_use/func.py
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import os
|
||||
from pathlib import Path
|
||||
from dotenv import load_dotenv
|
||||
from browser_use import BrowserProfile
|
||||
|
||||
# Load environment variables
|
||||
load_dotenv(override=True)
|
||||
|
||||
def setup_proxy():
|
||||
"""Configure proxy settings from environment variables."""
|
||||
proxy_host = os.getenv("PROXY_HOST")
|
||||
proxy_port = os.getenv("PROXY_PORT")
|
||||
|
||||
if proxy_host and proxy_port:
|
||||
proxy_url = f"http://{proxy_host}:{proxy_port}"
|
||||
print(f"🔗 Using proxy: {proxy_host}:{proxy_port}")
|
||||
return proxy_url
|
||||
else:
|
||||
print("🔗 No proxy configured, using direct connection.")
|
||||
return None
|
||||
|
||||
|
||||
def setup_storage_state():
|
||||
"""Setup browser storage state for session persistence."""
|
||||
storage_state_path = Path("./data/storage_state.json").resolve()
|
||||
storage_state_temp_path = Path("./data/storage_state_temp.json").resolve()
|
||||
|
||||
if storage_state_path.exists():
|
||||
if storage_state_temp_path.exists():
|
||||
storage_state_temp_path.unlink()
|
||||
|
||||
storage_state_temp_path.write_text(
|
||||
storage_state_path.read_text(encoding="utf-8"), encoding="utf-8"
|
||||
)
|
||||
print(f"🔄 Using existing storage state: {storage_state_temp_path}")
|
||||
return str(storage_state_temp_path)
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_browser_args():
|
||||
"""Get browser arguments for enhanced compatibility and security."""
|
||||
return [
|
||||
# Security and isolation
|
||||
"--disable-web-security",
|
||||
"--disable-site-isolation-trials",
|
||||
"--disable-features=IsolateOrigins,site-per-process",
|
||||
"--ignore-certificate-errors",
|
||||
"--ignore-ssl-errors",
|
||||
"--allow-running-insecure-content",
|
||||
# Performance and rendering
|
||||
"--disable-features=VizDisplayCompositor",
|
||||
"--disable-dev-shm-usage",
|
||||
# Popup and automation
|
||||
"--disable-popup-blocking",
|
||||
"--disable-blink-features=AutomationControlled",
|
||||
# Browser behavior
|
||||
"--no-first-run",
|
||||
"--no-service-autorun",
|
||||
"--no-default-browser-check",
|
||||
"--password-store=basic",
|
||||
"--use-mock-keychain",
|
||||
# Extensions
|
||||
"--disable-extensions-file-access-check",
|
||||
"--disable-extensions-http-throttling",
|
||||
"--disable-component-extensions-with-background-pages",
|
||||
# Language
|
||||
f"--lang={os.getenv('LANG', 'en_US')}",
|
||||
]
|
||||
11
lib/utils/browser_use/model.py
Normal file
11
lib/utils/browser_use/model.py
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
from typing import List
|
||||
from pydantic import BaseModel
|
||||
|
||||
# 출력 모델
|
||||
class OAuth(BaseModel):
|
||||
provider: str
|
||||
oauth_uri: str
|
||||
|
||||
|
||||
class OAuthList(BaseModel):
|
||||
oauth_providers: List[OAuth]
|
||||
8
lib/utils/config.py
Normal file
8
lib/utils/config.py
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
import os
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv(verbose=True, override=True)
|
||||
|
||||
BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:11081")
|
||||
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
||||
GOOGLE_MODEL = os.getenv("GOOGLE_MODEL", "gemini-2.5-flash-preview-05-20")
|
||||
GOOGLE_PLANNER_MODEL = os.getenv("GOOGLE_PLANNER_MODEL", "gemini-2.5-pro-preview-06-05")
|
||||
36
lib/utils/is_html.py
Normal file
36
lib/utils/is_html.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import requests
|
||||
|
||||
def is_html_url(url: str, timeout: float = 10.0) -> bool:
|
||||
"""
|
||||
주어진 URL에 HEAD 요청을 보내고, 응답 헤더의 Content-Type이 HTML인지 확인합니다.
|
||||
- url: 검사할 URL 문자열
|
||||
- timeout: 요청 타임아웃(초 단위)
|
||||
|
||||
반환값:
|
||||
- Content-Type이 'text/html' 로 시작하면 True, 그렇지 않으면 False
|
||||
"""
|
||||
|
||||
try:
|
||||
with requests.get(url, timeout=timeout, stream=True) as response:
|
||||
# 응답 코드가 200번대가 아니면 False로 간주
|
||||
if not response.ok:
|
||||
return False
|
||||
|
||||
content_type = response.headers.get('Content-Type', '')
|
||||
# Content-Type에 'text/html'이 포함되어 있으면 HTML로 간주
|
||||
return content_type.lower().startswith('text/html')
|
||||
except requests.RequestException:
|
||||
return False
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_urls = [
|
||||
'https://www.example.com',
|
||||
'https://api.github.com', # JSON API라서 HTML이 아닐 확률이 높음
|
||||
'https://raw.githubusercontent.com' # 텍스트 파일 등 다양한 타입
|
||||
]
|
||||
|
||||
for url in test_urls:
|
||||
if is_html_url(url):
|
||||
print(f"[HTML] {url}")
|
||||
else:
|
||||
print(f"[Not HTML] {url}")
|
||||
29
lib/utils/logger.py
Normal file
29
lib/utils/logger.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
from pathlib import Path
|
||||
from datetime import datetime
|
||||
|
||||
# 미리 정해진 파일 경로
|
||||
FILE_PATH = Path("data/log.txt")
|
||||
|
||||
def logger(msg: str) -> None:
|
||||
try:
|
||||
"""
|
||||
msg 문자열을 파일 끝에 추가합니다.
|
||||
- 파일이 없으면 새로 생성
|
||||
- 디렉터리가 없으면 생성
|
||||
"""
|
||||
# 상위 디렉터리 생성 (이미 있으면 무시)
|
||||
FILE_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# 현재 시각 구해서 포맷팅
|
||||
now = datetime.now()
|
||||
timestamp = now.strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 메시지에 개행이 없으면 자동으로 붙이기
|
||||
newline = "" if msg.endswith("\n") else "\n"
|
||||
line = f"[{timestamp}] {msg}{newline}"
|
||||
|
||||
# 'a' 모드: 파일이 없으면 생성, 있으면 이어쓰기
|
||||
with FILE_PATH.open(mode="a", encoding="utf-8") as f:
|
||||
f.write(line)
|
||||
except:
|
||||
print(msg)
|
||||
36
lib/utils/read_txt.py
Normal file
36
lib/utils/read_txt.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
def read_lines_between(filepath: str, start_line: int, end_line: int) -> list[str]:
|
||||
"""
|
||||
파일에서 start_line번 째 줄부터 end_line번 째 줄까지 읽어와
|
||||
각 줄을 요소로 갖는 리스트를 반환하는 함수.
|
||||
|
||||
Parameters:
|
||||
----------
|
||||
filepath : str
|
||||
읽을 텍스트 파일의 경로
|
||||
start_line : int
|
||||
읽기 시작할 행 번호 (1부터 시작)
|
||||
end_line : int
|
||||
읽을 마지막 행 번호 (start_line <= end_line)
|
||||
|
||||
Returns:
|
||||
-------
|
||||
list[str]
|
||||
각 줄을 문자열로 저장한 리스트.
|
||||
파일에 해당 범위의 줄이 없으면 가능한 만큼만 반환.
|
||||
"""
|
||||
|
||||
if start_line < 1 or end_line < start_line:
|
||||
raise ValueError("start_line은 1 이상이어야 하며, end_line은 start_line 이상이어야 합니다.")
|
||||
|
||||
selected_lines: list[str] = []
|
||||
with open(filepath, 'r', encoding='utf-8') as f:
|
||||
for idx, line in enumerate(f, start=1):
|
||||
if idx < start_line:
|
||||
# 아직 읽기 시작 전
|
||||
continue
|
||||
if idx > end_line:
|
||||
# 읽을 범위를 벗어났으므로 중단
|
||||
break
|
||||
# 줄 끝의 개행 문자를 제거하고 리스트에 추가
|
||||
selected_lines.append(line.rstrip('\n'))
|
||||
return selected_lines
|
||||
Loading…
Add table
Add a link
Reference in a new issue