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

How to use http proxy, it is best to give a sample code #857

Closed
twolao opened this issue Apr 8, 2023 · 2 comments
Closed

How to use http proxy, it is best to give a sample code #857

twolao opened this issue Apr 8, 2023 · 2 comments
Labels
question Questions related to rod

Comments

@twolao
Copy link

twolao commented Apr 8, 2023

Rod Version: v0.108.1

The code to demonstrate your question

package main

import (
	"log"
	"time"
	"github.com/go-rod/rod"
	"github.com/go-rod/rod/lib/launcher"
	"github.com/go-rod/rod/lib/launcher/flags"
)

func main() {
	http_proxy_url := "http://user:password@ip:port"
	l := launcher.New().Headless(false)
	//l = l.Set(flags.ProxyServer, "socks5://127.0.0.1:9150") 
	l = l.Set(flags.ProxyServer, http_proxy_url)

	controlURL, _ := l.Launch()
	browser := rod.New().ControlURL(controlURL).MustConnect()
	go browser.MustHandleAuth("username", "password")() 
	browser.MustIgnoreCertErrors(true)

	page := browser.MustPage()
	defer page.MustClose()
	err := page.Navigate("https://api.ipify.org")
	if err != nil {
		log.Println(err)
	}
	err = page.WaitLoad()
	if err != nil {
		log.Println(err)
	}

	println(page.MustElement("html").MustText())
	time.Sleep(30*time.Second)
}

What you got

2023/04/08 15:57:47 navigation failed: net::ERR_NO_SUPPORTED_PROXIES
无法访问此网站

网址为 https://api.ipify.org/ 的网页可能暂时无法连接,或者它已永久性地移动到了新网址。

ERR_NO_SUPPORTED_PROXIES

What you expected to see

ip address

What have you tried to solve the question

How to use http proxy, it is best to give a sample code

@twolao twolao added the question Questions related to rod label Apr 8, 2023
@ysmood
Copy link
Collaborator

ysmood commented Apr 8, 2023

rod/examples_test.go

Lines 347 to 369 in e24573b

// Shows how we can further customize the browser with the launcher library.
// Usually you use launcher lib to set the browser's command line flags (switches).
// Doc for flags: https://peter.sh/experiments/chromium-command-line-switches
func Example_customize_browser_launch() {
url := launcher.New().
Proxy("127.0.0.1:8080"). // set flag "--proxy-server=127.0.0.1:8080"
Delete("use-mock-keychain"). // delete flag "--use-mock-keychain"
MustLaunch()
browser := rod.New().ControlURL(url).MustConnect()
defer browser.MustClose()
// So that we don't have to self issue certs for MITM
browser.MustIgnoreCertErrors(true)
// Adding authentication to the proxy, for the next auth request.
// We use CLI tool "mitmproxy --proxyauth user:pass" as an example.
go browser.MustHandleAuth("user", "pass")()
// mitmproxy needs a cert config to support https. We use http here instead,
// for example
fmt.Println(browser.MustPage("https://mdn.dev/").MustElement("title").MustText())
}

Please google and learn how to use mitmproxy

// We use CLI tool "mitmproxy --proxyauth user:pass" as an example.

@twolao
Copy link
Author

twolao commented Apr 9, 2023

rod/examples_test.go

Lines 347 to 369 in e24573b

// Shows how we can further customize the browser with the launcher library.
// Usually you use launcher lib to set the browser's command line flags (switches).
// Doc for flags: https://peter.sh/experiments/chromium-command-line-switches
func Example_customize_browser_launch() {
url := launcher.New().
Proxy("127.0.0.1:8080"). // set flag "--proxy-server=127.0.0.1:8080"
Delete("use-mock-keychain"). // delete flag "--use-mock-keychain"
MustLaunch()
browser := rod.New().ControlURL(url).MustConnect()
defer browser.MustClose()
// So that we don't have to self issue certs for MITM
browser.MustIgnoreCertErrors(true)
// Adding authentication to the proxy, for the next auth request.
// We use CLI tool "mitmproxy --proxyauth user:pass" as an example.
go browser.MustHandleAuth("user", "pass")()
// mitmproxy needs a cert config to support https. We use http here instead,
// for example
fmt.Println(browser.MustPage("https://mdn.dev/").MustElement("title").MustText())
}

Please google and learn how to use mitmproxy

// We use CLI tool "mitmproxy --proxyauth user:pass" as an example.

thanx

@ysmood ysmood closed this as completed Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions related to rod
Projects
None yet
Development

No branches or pull requests

2 participants