Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy Usage #31

Closed
dinosaurtirex opened this issue Dec 17, 2024 · 6 comments
Closed

Proxy Usage #31

dinosaurtirex opened this issue Dec 17, 2024 · 6 comments

Comments

@dinosaurtirex
Copy link

How is there any way to use proxy with ZenDriver?

@slimshreydy
Copy link
Contributor

slimshreydy commented Dec 17, 2024

@dinosaurtirex did you figure out how to do this? Still struggling here with getting an auth proxy set up.

@therealpurplemana
Copy link

therealpurplemana commented Dec 19, 2024

code sample, might work out of the box with some minimal changes. Includes built in image and font blocking. I'd suggest building your proxy setup method and blockers for simplicity:

    async def setup_proxy(self, username, password, tab, block_images=False):
        self.logger.info(f"PMXDriver '{self.instance_name}': Setting up proxy with username: {username}")
        try:
            self.current_tab = tab

            # Simple auth handler that just handles credentials
            def auth_challenge_handler(event):
                tab.feed_cdp(
                    cdp.fetch.continue_with_auth(
                        request_id=event.request_id,
                        auth_challenge_response=cdp.fetch.AuthChallengeResponse(
                            response="ProvideCredentials",
                            username=username,
                            password=password,
                        ),
                    )
                )

            # Simple request handler that only blocks images/fonts if needed
            async def request_paused_handler(event: cdp.fetch.RequestPaused, tab: Tab):
                request_id = event.request_id
                
                if block_images and event.resource_type in [cdp.network.ResourceType.IMAGE, cdp.network.ResourceType.FONT]:
                    tab.feed_cdp(cdp.fetch.fail_request(
                        request_id=request_id,
                        error_reason=cdp.network.ErrorReason.BLOCKED_BY_CLIENT
                    ))
                    return

                # Continue all other requests immediately
                tab.feed_cdp(cdp.fetch.continue_request(request_id=request_id))

            # Add basic handlers
            tab.add_handler(cdp.fetch.AuthRequired, auth_challenge_handler)
            tab.add_handler(cdp.fetch.RequestPaused, request_paused_handler)

            # Enable fetch with minimal patterns
            patterns = []
            if block_images:
                patterns.extend([
                    cdp.fetch.RequestPattern(
                        url_pattern="*",
                        resource_type=cdp.network.ResourceType.IMAGE,
                        request_stage=cdp.fetch.RequestStage.REQUEST
                    ),
                    cdp.fetch.RequestPattern(
                        url_pattern="*",
                        resource_type=cdp.network.ResourceType.FONT,
                        request_stage=cdp.fetch.RequestStage.REQUEST
                    )
                ])

            # Always add auth pattern
            patterns.append(
                cdp.fetch.RequestPattern(
                    url_pattern="*",
                    request_stage=cdp.fetch.RequestStage.REQUEST
                )
            )

            await tab.send(cdp.fetch.enable(
                patterns=patterns,
                handle_auth_requests=True
            ))

            #self.logger.info("Proxy setup complete")

        except Exception as e:
            self.logger.error(f"Error during proxy setup: {e}")
            raise

@dinosaurtirex
Copy link
Author

code sample, might work out of the box with some minimal changes. Includes built in image and font blocking. I'd suggest building your proxy setup method and blockers for simplicity:

    async def setup_proxy(self, username, password, tab, block_images=False):
        self.logger.info(f"PMXDriver '{self.instance_name}': Setting up proxy with username: {username}")
        try:
            self.current_tab = tab

            # Simple auth handler that just handles credentials
            def auth_challenge_handler(event):
                tab.feed_cdp(
                    cdp.fetch.continue_with_auth(
                        request_id=event.request_id,
                        auth_challenge_response=cdp.fetch.AuthChallengeResponse(
                            response="ProvideCredentials",
                            username=username,
                            password=password,
                        ),
                    )
                )

            # Simple request handler that only blocks images/fonts if needed
            async def request_paused_handler(event: cdp.fetch.RequestPaused, tab: Tab):
                request_id = event.request_id
                
                if block_images and event.resource_type in [cdp.network.ResourceType.IMAGE, cdp.network.ResourceType.FONT]:
                    tab.feed_cdp(cdp.fetch.fail_request(
                        request_id=request_id,
                        error_reason=cdp.network.ErrorReason.BLOCKED_BY_CLIENT
                    ))
                    return

                # Continue all other requests immediately
                tab.feed_cdp(cdp.fetch.continue_request(request_id=request_id))

            # Add basic handlers
            tab.add_handler(cdp.fetch.AuthRequired, auth_challenge_handler)
            tab.add_handler(cdp.fetch.RequestPaused, request_paused_handler)

            # Enable fetch with minimal patterns
            patterns = []
            if block_images:
                patterns.extend([
                    cdp.fetch.RequestPattern(
                        url_pattern="*",
                        resource_type=cdp.network.ResourceType.IMAGE,
                        request_stage=cdp.fetch.RequestStage.REQUEST
                    ),
                    cdp.fetch.RequestPattern(
                        url_pattern="*",
                        resource_type=cdp.network.ResourceType.FONT,
                        request_stage=cdp.fetch.RequestStage.REQUEST
                    )
                ])

            # Always add auth pattern
            patterns.append(
                cdp.fetch.RequestPattern(
                    url_pattern="*",
                    request_stage=cdp.fetch.RequestStage.REQUEST
                )
            )

            await tab.send(cdp.fetch.enable(
                patterns=patterns,
                handle_auth_requests=True
            ))

            #self.logger.info("Proxy setup complete")

        except Exception as e:
            self.logger.error(f"Error during proxy setup: {e}")
            raise

Hello! Thanks

@dinosaurtirex
Copy link
Author

@dinosaurtirex did you figure out how to do this? Still struggling here with getting an auth proxy set up.

I did it my way, just using browser extension for proxy on start.

@dinosaurtirex
Copy link
Author

dinosaurtirex commented Dec 23, 2024

@dinosaurtirex did you figure out how to do this? Still struggling here with getting an auth proxy set up.

Just create this 2 files and add arguments on start. I did it via _default_browser_args cuz don't have enough time, it way more simple for me

    cfg = Config()
    cfg._default_browser_args = [
        "--remote-allow-origins=*",
        "--no-first-run",
        "--no-service-autorun",
        "--no-default-browser-check",
        "--homepage=about:blank",
        "--no-pings",
        "--password-store=basic",
        "--disable-infobars",
        "--disable-breakpad",
        "--disable-component-update",
        "--disable-backgrounding-occluded-windows",
        "--disable-renderer-backgrounding",
        "--disable-background-networking",
        "--disable-dev-shm-usage",
        "--disable-features=IsolateOrigins,site-per-process",
        "--disable-session-crashed-bubble",
        "--disable-search-engine-choice-screen",
        "--load-extension=C:/Users/kirill/Desktop/CODE/Parser/proxy_extension"
    ]
    browser = await zd.start(config=cfg)

background.js

var config = {
    mode: "fixed_servers",
    rules: {
      singleProxy: {
        scheme: "http",
        host: "",
        port: 0000 
      },
      bypassList: ["<local>"]
    }
  };
  
  chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
  
  function callbackFn() {
    return {
      authCredentials: {
        username: "",
        password: ""
      }
    };
  }
  
  chrome.webRequest.onAuthRequired.addListener(
    callbackFn,
    {urls: ["<all_urls>"]},
    ["blocking"]
  );
 

manifest.json

{
    "manifest_version": 2,
    "name": "proxy",
    "version": "1.0",
    "permissions": [
      "proxy",
      "tabs",
      "unlimitedStorage",
      "storage",
      "<all_urls>",
      "webRequest",
      "webRequestBlocking"
    ],
    "background": {
      "scripts": ["background.js"]
    }
  }
  

@therealpurplemana
Copy link

therealpurplemana commented Dec 23, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants