mirror of
https://github.com/j93es/browser-use-oauth.git
synced 2026-06-04 07:51: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
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]
|
||||
Loading…
Add table
Add a link
Reference in a new issue