go-resty / resty

Simple HTTP and REST client library for Go
MIT License
9.89k stars 696 forks source link

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

Open rew1nter opened 10 months ago

rew1nter commented 10 months ago

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 commented 9 months ago

我目前正在做这样的 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 commented 6 months ago

@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 commented 6 months ago

我目前正在做这样的 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