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

请教个问题,关于setProxy或者socks的 #373

Closed
Flipped199 opened this issue Jul 10, 2023 · 9 comments
Closed

请教个问题,关于setProxy或者socks的 #373

Flipped199 opened this issue Jul 10, 2023 · 9 comments

Comments

@Flipped199
Copy link

既然以上两个方法影响了全局的设置,那么为什么这俩方法挂载在dataflow上而不是在client上呢?

比如说我现在需要创建一个gout.client 并为它设置proxy我只能调用get()等一些方法后再设置,而当我第二次用到这个client的时候,我并不需要再次设置proxy
如果是我哪里没有理解 请帮我指正,谢谢

@guonaihong
Copy link
Owner

默认是影响全局的配置(gout里面有一个全局的http.Client),SetProxy和SetSocks5是影响http.Client这个对象。只要自己传递一个http.Client就是局部的。

package main

import (
	"fmt"
	"github.com/guonaihong/gout"
	"log"
)

func main() {
	c := &http.Client{}
	s := ""
	err := gout.
		New(c).
		GET("www.qq.com").
		// 设置proxy服务地址
		SetProxy("http://127.0.0.1:7000").
		// 绑定返回数据到s里面
		BindBody(&s).
		Do()

	if err != nil {
		log.Println(err)
		return
	}

	fmt.Println(s)
}

@Flipped199
Copy link
Author

是的,但是如果我现在的需求是这样的,请求a和b不确定先调用哪个,但都需要使用代理,这种情况我貌似不太懂应该怎么解决

func x() {
	c := &http.Client{}
	// 请求a
	err := gout.New(c).GET("a").SetSOCKS5("xx").Do()
	if err != nil {
		// handle err
	}

	// 请求b
	err = gout.New(c).GET("b").Do()
	if err != nil {
		// handle err
	}
}

@guonaihong
Copy link
Owner

http.Client里面有个连接池,一般先全局初始化,后面直接使用。如果所有的请求都要跑SetSOCKS5,可以

err := gout.New(c).GET("a").SetSOCKS5("xx").Do()

设置下。
后面都传递这个http.Client就行。

@Flipped199
Copy link
Author

首先很感谢您的回复。但是,我这边碰到的问题是,a和b调用顺序不确定,可能先调a,也可能先调b,所以就无法确定setsocks放在哪行,每个都写的话又感觉不太优雅(虽然确实能解决问题)貌似也可以给client设置一个transport来解决问题。再次感谢大佬的回复。谢谢

@guonaihong
Copy link
Owner

我上面的例子写得可能含糊,你没get到点。稍等一下。。

@guonaihong
Copy link
Owner

guonaihong commented Jul 11, 2023

package main

import (
	"net/http"

	"github.com/guonaihong/gout"
)

func main() {
	var c http.Client
	// 通过SetSOCKS5设置socks5代理, 这时候c就是socks5代理的http client
	// 这只要初始化一次
	err := gout.New(&c).SetSOCKS5("").Do()
	if err != nil {
		// handle err
	}

	// 请求
	err = gout.New(&c).GET("a").Do()
	if err != nil {
		// handle err
	}

	// b请求
	err = gout.New(&c).GET("a").Do()
	if err != nil {
		// handle err
	}
}

@Flipped199看下这个例子呢。

@guonaihong
Copy link
Owner

guonaihong commented Jul 11, 2023

你上面提的给http.Client加个辅助函数也可以。比如伪代码这么设计

var c http.Client
hcutil.SetSOCKS5(&c, "代理的地址")

。我明天中午加下。

@Flipped199
Copy link
Author

非常感谢大佬,我一直在尝试NewWithOpt的方法,先入为主的想到它和New返回的应该是同样的对象,然后导致一直没注意到这个New返回的是DataFlow的对象,抱歉浪费您这么多时间。💖

@guonaihong guonaihong mentioned this issue Jul 13, 2023
@guonaihong
Copy link
Owner

我在hcutil包和NewWithOpt都加上socks5, proxy, unix socket函数的设置,感兴趣可使用下。

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

2 participants