Skip to content

Commit e1e42f3

Browse files
committed
residential proxy verified
1 parent 0805ab9 commit e1e42f3

File tree

5 files changed

+804
-8
lines changed

5 files changed

+804
-8
lines changed
Lines changed: 366 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,366 @@
1+
"""
2+
Example: Using Residential Proxies with SentienceAgent
3+
4+
Demonstrates how to configure and use residential proxies with SentienceBrowser
5+
for web automation while protecting your real IP address.
6+
7+
Proxy Support:
8+
- HTTP, HTTPS, and SOCKS5 proxies
9+
- Authentication (username/password)
10+
- Environment variable configuration
11+
- WebRTC leak protection (automatic)
12+
- Self-signed SSL certificate handling (automatic for proxies)
13+
14+
HTTPS Certificate Handling:
15+
When using a proxy, the SDK automatically sets `ignore_https_errors=True` to handle
16+
residential proxies that use self-signed SSL certificates. This is common with proxy
17+
providers and prevents `ERR_CERT_AUTHORITY_INVALID` errors.
18+
19+
Note: HTTPS errors are ONLY ignored when a proxy is configured - normal browsing
20+
(without proxy) maintains full SSL certificate validation for security.
21+
22+
Usage:
23+
# Method 1: Direct proxy argument
24+
python examples/residential_proxy_agent.py
25+
26+
# Method 2: Environment variable
27+
export SENTIENCE_PROXY="http://user:pass@proxy.example.com:8080"
28+
python examples/residential_proxy_agent.py
29+
30+
Requirements:
31+
- OpenAI API key (OPENAI_API_KEY) for LLM
32+
- Optional: Sentience API key (SENTIENCE_API_KEY) for Pro/Enterprise features
33+
- Optional: Proxy server credentials
34+
"""
35+
36+
import os
37+
38+
from sentience import SentienceAgent, SentienceBrowser
39+
from sentience.agent_config import AgentConfig
40+
from sentience.llm_provider import OpenAIProvider
41+
42+
43+
def example_proxy_direct_argument():
44+
"""Example 1: Configure proxy via direct argument"""
45+
print("=" * 60)
46+
print("Example 1: Proxy via Direct Argument")
47+
print("=" * 60)
48+
49+
# Configure your proxy credentials here
50+
# Supported formats:
51+
# - HTTP: http://user:pass@proxy.example.com:8080
52+
# - HTTPS: https://user:pass@proxy.example.com:8443
53+
# - SOCKS5: socks5://user:pass@proxy.example.com:1080
54+
# - No auth: http://proxy.example.com:8080
55+
56+
proxy_url = "http://user:pass@proxy.example.com:8080"
57+
58+
# Create browser with proxy
59+
browser = SentienceBrowser(
60+
proxy=proxy_url, # Direct proxy configuration
61+
headless=False, # Set to True for production
62+
)
63+
64+
openai_key = os.environ.get("OPENAI_API_KEY")
65+
if not openai_key:
66+
print("❌ Error: OPENAI_API_KEY not set")
67+
return
68+
69+
llm = OpenAIProvider(api_key=openai_key, model="gpt-4o-mini")
70+
agent = SentienceAgent(browser, llm, verbose=True)
71+
72+
try:
73+
print("\n🚀 Starting browser with proxy...")
74+
browser.start()
75+
76+
print("🌐 Navigating to IP check service...")
77+
browser.page.goto("https://api.ipify.org?format=json")
78+
browser.page.wait_for_load_state("networkidle")
79+
80+
# Verify proxy is working by checking IP
81+
print("\n✅ Browser started successfully with proxy!")
82+
print(" You should see the proxy's IP address in the browser\n")
83+
84+
# Example: Use agent to interact with pages through proxy
85+
browser.page.goto("https://www.google.com")
86+
browser.page.wait_for_load_state("networkidle")
87+
88+
agent.act("Click the search box")
89+
agent.act('Type "my ip address" into the search field')
90+
agent.act("Press Enter key")
91+
92+
import time
93+
94+
time.sleep(2)
95+
96+
print("\n✅ Agent execution complete!")
97+
print(" All traffic was routed through the proxy")
98+
99+
except Exception as e:
100+
print(f"\n❌ Error: {e}")
101+
raise
102+
103+
finally:
104+
browser.close()
105+
106+
107+
def example_proxy_environment_variable():
108+
"""Example 2: Configure proxy via environment variable"""
109+
print("=" * 60)
110+
print("Example 2: Proxy via Environment Variable")
111+
print("=" * 60)
112+
113+
# Check if SENTIENCE_PROXY is set
114+
proxy_from_env = os.environ.get("SENTIENCE_PROXY")
115+
116+
if not proxy_from_env:
117+
print("\n⚠️ SENTIENCE_PROXY environment variable not set")
118+
print(" Set it with:")
119+
print(' export SENTIENCE_PROXY="http://user:pass@proxy.example.com:8080"')
120+
print("\n Skipping this example...\n")
121+
return
122+
123+
print(f"\n🔧 Using proxy from environment: {proxy_from_env.split('@')[0]}@***")
124+
125+
# Create browser without explicit proxy argument
126+
# It will automatically use SENTIENCE_PROXY from environment
127+
browser = SentienceBrowser(headless=False)
128+
129+
openai_key = os.environ.get("OPENAI_API_KEY")
130+
if not openai_key:
131+
print("❌ Error: OPENAI_API_KEY not set")
132+
return
133+
134+
llm = OpenAIProvider(api_key=openai_key, model="gpt-4o-mini")
135+
agent = SentienceAgent(browser, llm, verbose=True)
136+
137+
try:
138+
print("\n🚀 Starting browser with proxy from environment...")
139+
browser.start()
140+
141+
print("🌐 Navigating to IP check service...")
142+
browser.page.goto("https://api.ipify.org?format=json")
143+
browser.page.wait_for_load_state("networkidle")
144+
145+
print("\n✅ Browser started successfully with environment proxy!")
146+
147+
except Exception as e:
148+
print(f"\n❌ Error: {e}")
149+
raise
150+
151+
finally:
152+
browser.close()
153+
154+
155+
def example_proxy_types():
156+
"""Example 3: Different proxy types (HTTP, HTTPS, SOCKS5)"""
157+
print("=" * 60)
158+
print("Example 3: Different Proxy Types")
159+
print("=" * 60)
160+
161+
proxy_examples = {
162+
"HTTP": "http://user:pass@proxy.example.com:8080",
163+
"HTTPS": "https://user:pass@secure-proxy.example.com:8443",
164+
"SOCKS5": "socks5://user:pass@socks-proxy.example.com:1080",
165+
"No Auth": "http://proxy.example.com:8080", # Without credentials
166+
}
167+
168+
print("\n📋 Supported proxy formats:")
169+
for proxy_type, example_url in proxy_examples.items():
170+
# Hide credentials in output
171+
display_url = example_url.replace("user:pass@", "user:***@")
172+
print(f" {proxy_type:10s}: {display_url}")
173+
174+
print("\n💡 To use a specific proxy type, pass it to SentienceBrowser:")
175+
print(' browser = SentienceBrowser(proxy="socks5://user:pass@proxy.com:1080")')
176+
177+
178+
def example_webrtc_leak_protection():
179+
"""Example 4: WebRTC leak protection (automatic)"""
180+
print("=" * 60)
181+
print("Example 4: WebRTC Leak Protection (Automatic)")
182+
print("=" * 60)
183+
184+
print("\n🔒 WebRTC leak protection is AUTOMATICALLY enabled for all users!")
185+
print(" This prevents your real IP from leaking via WebRTC when using proxies.")
186+
print("\n Browser flags applied:")
187+
print(" - --disable-features=WebRtcHideLocalIpsWithMdns")
188+
print(" - --force-webrtc-ip-handling-policy=disable_non_proxied_udp")
189+
print("\n🔒 HTTPS certificate handling (when using proxy):")
190+
print(" - ignore_https_errors=True (automatically set)")
191+
print(" - Handles residential proxies with self-signed SSL certificates")
192+
print(" - Prevents ERR_CERT_AUTHORITY_INVALID errors")
193+
print(" - Only active when proxy is configured (normal browsing unaffected)")
194+
print("\n No additional configuration needed - it just works!\n")
195+
196+
197+
def example_proxy_with_cloud_tracing():
198+
"""Example 5: Combine proxy with cloud tracing"""
199+
print("=" * 60)
200+
print("Example 5: Proxy + Cloud Tracing")
201+
print("=" * 60)
202+
203+
sentience_key = os.environ.get("SENTIENCE_API_KEY")
204+
openai_key = os.environ.get("OPENAI_API_KEY")
205+
206+
if not sentience_key:
207+
print("\n⚠️ SENTIENCE_API_KEY not set")
208+
print(" Cloud tracing requires Pro or Enterprise tier")
209+
print(" Get your API key at: https://sentience.studio")
210+
print("\n Skipping this example...\n")
211+
return
212+
213+
if not openai_key:
214+
print("❌ Error: OPENAI_API_KEY not set")
215+
return
216+
217+
from sentience.tracer_factory import create_tracer
218+
219+
# Create tracer for cloud upload
220+
run_id = "proxy-with-tracing-demo"
221+
tracer = create_tracer(api_key=sentience_key, run_id=run_id)
222+
223+
# Configure proxy
224+
proxy_url = "http://user:pass@proxy.example.com:8080"
225+
226+
# Create browser with BOTH proxy and API key
227+
browser = SentienceBrowser(api_key=sentience_key, proxy=proxy_url, headless=False)
228+
229+
llm = OpenAIProvider(api_key=openai_key, model="gpt-4o-mini")
230+
231+
# Configure agent with screenshots + tracer
232+
config = AgentConfig(
233+
snapshot_limit=50,
234+
capture_screenshots=True,
235+
screenshot_format="jpeg",
236+
screenshot_quality=80,
237+
)
238+
239+
agent = SentienceAgent(browser, llm, tracer=tracer, config=config)
240+
241+
try:
242+
print("\n🚀 Starting browser with proxy + cloud tracing...")
243+
browser.start()
244+
245+
print("🌐 Executing agent actions (all traced)...")
246+
browser.page.goto("https://www.google.com")
247+
browser.page.wait_for_load_state("networkidle")
248+
249+
agent.act("Click the search box")
250+
agent.act('Type "sentience AI SDK" into the search field')
251+
agent.act("Press Enter key")
252+
253+
print("\n✅ Agent execution complete!")
254+
255+
except Exception as e:
256+
print(f"\n❌ Error: {e}")
257+
raise
258+
259+
finally:
260+
# Upload trace to cloud
261+
print("\n📤 Uploading trace to cloud...")
262+
try:
263+
tracer.close(blocking=True)
264+
print("✅ Trace uploaded successfully!")
265+
print(f" View at: https://studio.sentienceapi.com (run_id: {run_id})")
266+
except Exception as e:
267+
print(f"⚠️ Upload failed: {e}")
268+
269+
browser.close()
270+
271+
272+
def example_no_proxy_baseline():
273+
"""Example 0: Baseline - Run agent without proxy to show it works"""
274+
print("=" * 60)
275+
print("Example 0: Baseline (No Proxy)")
276+
print("=" * 60)
277+
278+
openai_key = os.environ.get("OPENAI_API_KEY")
279+
if not openai_key:
280+
print("❌ Error: OPENAI_API_KEY not set")
281+
print(" Set it with: export OPENAI_API_KEY='your-key-here'")
282+
return
283+
284+
# Create browser WITHOUT proxy (baseline)
285+
browser = SentienceBrowser(headless=False)
286+
llm = OpenAIProvider(api_key=openai_key, model="gpt-4o-mini")
287+
agent = SentienceAgent(browser, llm, verbose=True)
288+
289+
try:
290+
print("\n🚀 Starting browser (without proxy)...")
291+
browser.start()
292+
293+
print("🌐 Navigating to Google...")
294+
browser.page.goto("https://www.google.com")
295+
browser.page.wait_for_load_state("networkidle")
296+
297+
print("\n🤖 Running agent actions...")
298+
agent.act("Click the search box")
299+
agent.act('Type "what is my ip" into the search field')
300+
agent.act("Press Enter key")
301+
302+
import time
303+
304+
time.sleep(3)
305+
306+
print("\n✅ Agent execution complete!")
307+
print(" This shows your REAL IP address (no proxy)")
308+
print("\n💡 To use a proxy:")
309+
print(" 1. Get proxy credentials from your provider")
310+
print(" 2. Run example_proxy_direct_argument() or set SENTIENCE_PROXY")
311+
print(" You should see a DIFFERENT IP (the proxy's IP)")
312+
313+
stats = agent.get_token_stats()
314+
print("\n📊 Token Usage:")
315+
print(f" Prompt tokens: {stats.total_prompt_tokens}")
316+
print(f" Completion tokens: {stats.total_completion_tokens}")
317+
print(f" Total tokens: {stats.total_tokens}")
318+
319+
except Exception as e:
320+
print(f"\n❌ Error: {e}")
321+
raise
322+
323+
finally:
324+
browser.close()
325+
326+
327+
def main():
328+
"""Run all proxy examples"""
329+
print("\n" + "=" * 60)
330+
print("Sentience SDK - Residential Proxy Examples")
331+
print("=" * 60 + "\n")
332+
333+
# Run examples
334+
# Note: Uncomment the examples you want to run
335+
336+
# Example 0: Baseline (no proxy) - WORKS OUT OF THE BOX
337+
example_no_proxy_baseline()
338+
339+
# Example 1: Direct argument (configure proxy_url first)
340+
# example_proxy_direct_argument()
341+
342+
# Example 2: Environment variable (set SENTIENCE_PROXY first)
343+
# example_proxy_environment_variable()
344+
345+
# Example 3: Show supported proxy types
346+
# example_proxy_types()
347+
348+
# Example 4: WebRTC leak protection info
349+
# example_webrtc_leak_protection()
350+
351+
# Example 5: Proxy + Cloud Tracing (requires API key)
352+
# example_proxy_with_cloud_tracing()
353+
354+
print("\n" + "=" * 60)
355+
print("Examples Complete!")
356+
print("=" * 60)
357+
print("\n💡 Tips:")
358+
print(" - Example 0 shows baseline (no proxy) - works immediately")
359+
print(" - Uncomment other examples after configuring proxy credentials")
360+
print(" - Set SENTIENCE_PROXY environment variable for easy configuration")
361+
print(" - WebRTC leak protection is automatic - no configuration needed")
362+
print(" - Combine with cloud tracing for full visibility\n")
363+
364+
365+
if __name__ == "__main__":
366+
main()

screenshot.png

-91.1 KB
Loading

0 commit comments

Comments
 (0)