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

What's the idiomatic way of changing proxies for the client? #740

Open
rew1nter opened this issue Oct 25, 2023 · 3 comments
Open

What's the idiomatic way of changing proxies for the client? #740

rew1nter opened this issue Oct 25, 2023 · 3 comments

Comments

@rew1nter
Copy link

I'm currently doing smth like this which I don't think is the best way to change proxies. How's this intended to be done?

package main

import (
	"crypto/tls"
	"fmt"
	"time"

	"github.com/go-resty/resty/v2"
)

var gproxy string = "http://127.0.0.1:8081"

func main() {
	c := resty.New().
		SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
		SetRedirectPolicy(resty.FlexibleRedirectPolicy(4))

	// change proxy in a single go routine
	go func() {
		for {
			time.Sleep(2 * time.Second)
			gproxy = "http://127.0.0.1:8080"
		}
	}()

	for {
		time.Sleep(1 * time.Second)
		r, _ := c.
			SetProxy(gproxy).
			R().Get("https://google.com/")
		fmt.Println(r.StatusCode())
	}
}
@gospider007
Copy link

我目前正在做这样的 smth,我认为这不是更改代理的最佳方式。这是如何做到的?

package main

import (
	"crypto/tls"
	"fmt"
	"time"

	"github.com/go-resty/resty/v2"
)

var gproxy string = "http://127.0.0.1:8081"

func main() {
	c := resty.New().
		SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
		SetRedirectPolicy(resty.FlexibleRedirectPolicy(4))

	// change proxy in a single go routine
	go func() {
		for {
			time.Sleep(2 * time.Second)
			gproxy = "http://127.0.0.1:8080"
		}
	}()

	for {
		time.Sleep(1 * time.Second)
		r, _ := c.
			SetProxy(gproxy).
			R().Get("https://google.com/")
		fmt.Println(r.StatusCode())
	}
}

this can help you

package main

import (
	"log"

	"github.com/gospider007/requests"
)

func main() {
	resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
		Proxy: "", //set proxy,ex:"http://127.0.0.1:8080","https://127.0.0.1:8080","socks5://127.0.0.1:8080"
	})
	if err != nil {
		log.Panic(err)
	}
	if resp.StatusCode() != 200 {
		log.Panic("status code is not 200")
	}
}

@jeevatkm
Copy link
Member

jeevatkm commented Mar 2, 2024

@rew1nter Thanks for reaching out; I'm sorry for the delay.
You could change the Proxy URL anytime by using client.SetProxy method. However, currently, Resty does not provide a lock for client properties. I recommend you use RWMutex at your end.

Note: Resty v3 comes with a lock and many new features.

@jeevatkm
Copy link
Member

jeevatkm commented Mar 3, 2024

我目前正在做这样的 smth,我认为这不是更改代理的最佳方式。这是如何做到的?

package main

import (
	"crypto/tls"
	"fmt"
	"time"

	"github.com/go-resty/resty/v2"
)

var gproxy string = "http://127.0.0.1:8081"

func main() {
	c := resty.New().
		SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}).
		SetRedirectPolicy(resty.FlexibleRedirectPolicy(4))

	// change proxy in a single go routine
	go func() {
		for {
			time.Sleep(2 * time.Second)
			gproxy = "http://127.0.0.1:8080"
		}
	}()

	for {
		time.Sleep(1 * time.Second)
		r, _ := c.
			SetProxy(gproxy).
			R().Get("https://google.com/")
		fmt.Println(r.StatusCode())
	}
}

this can help you

package main

import (
	"log"

	"github.com/gospider007/requests"
)

func main() {
	resp, err := requests.Get(nil, "https://httpbin.org/anything", requests.RequestOption{
		Proxy: "", //set proxy,ex:"http://127.0.0.1:8080","https://127.0.0.1:8080","socks5://127.0.0.1:8080"
	})
	if err != nil {
		log.Panic(err)
	}
	if resp.StatusCode() != 200 {
		log.Panic("status code is not 200")
	}
}

Hello @gospider007 - I'm glad to see a new open-source developer on GitHub. Welcome to the Open source community!
However, promoting your new library in another open-source library repository seems inappropriate.

https://github.com/search?q=repo%3Ago-resty%2Fresty+gospider007&type=issues

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

No branches or pull requests

3 participants