如何覆盖一个返回值?

huangapple go评论76阅读模式
英文:

How to overwrite a returning value?

问题

目标:当工具进入else语句时,我想更新返回值的值。我尝试了很多次,但没有成功。

故事:基本上,我编写了一个小工具,模拟一个HTTP客户端,它在循环中尝试联系服务器。首先,它尝试不使用代理,如果失败,则尝试使用代理设置。我要寻找的返回值只是设置未来的HTTP请求将如何进行。一旦返回,循环停止,工具执行其余的代码。

代码:

func SetupClient() *http.Client {
	for {
		tlsConfig := &tls.Config{InsecureSkipVerify: true}
		var PTransport http.RoundTripper = &http.Transport{TLSClientConfig: tlsConfig,
			Proxy: http.ProxyFromEnvironment}
		transport := &http.Transport{TLSClientConfig: tlsConfig}
		client := &http.Client{Transport: transport}
		fmt.Printf(" trying direct connection \n")

		req, err := http.NewRequest("GET", "https://192.168.1.98:4443/api/endpointx", nil)
		if err != nil {
			fmt.Println(err)
		}

		req.Header.Set("Cookie", "JSESSIONID=shszrhdrhdrhjdhdr")
		req.Header.Set("Referer", "https://aaegesgsegesgegeg")
		req.Header.Set("Upgrade-Insecure-Requests", "1")
		req.Header.Set("Connection", "close")
		req.Header.Set("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3")
		req.Header.Set("Accept", "*/*")
		req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0")
		time.Sleep(5 * time.Second)
		re := regexp.MustCompile(`:"(.*)"`)
		resp, err := client.Do(req)

		if err == nil {
			body, _ := ioutil.ReadAll(resp.Body)

			match := re.FindStringSubmatch(string(body))
			fmt.Println("quello che viene matchato è:", match[1])

			// 我知道这个匹配看起来不像else,我只是为了进入else
			if strings.Contains(string(body), "aaaaaaaa") {
				fmt.Printf(" inside IF\n ")
				return client
			} else {
				fmt.Printf(" inside else\n ")
				clientP := &http.Client{Transport: PTransport}
				resp, err := clientP.Do(req)
				if err == nil {
					body, _ := ioutil.ReadAll(resp.Body)

					if strings.Contains(string(body), match[1]) {
						client := clientP
						// 在这里,这个返回值应该覆盖旧的client变量
						// 这样下一个http请求将使用代理设置
						return client
					}
				}
			}
		}
	}
}
英文:

Goal: I would like to update the value of the return when the tool gets inside the else statement. I tried many times but without success

Story: basically, I coded a small tool that mimics an HTTP client which tries in a loop to contact a server. First, it tries without proxy, if it fails, it tries using proxy settings. The returning value I'm looking for just sets how the future HTTP requests will be made. Once returned, the loop stops and the tool executes the rest of the code

Code:

func SetupClient() *http.Client {
	for {
		tlsConfig := &tls.Config{InsecureSkipVerify: true}
		var PTransport http.RoundTripper = &http.Transport{TLSClientConfig: tlsConfig,
			Proxy: http.ProxyFromEnvironment}
		transport := &http.Transport{TLSClientConfig: tlsConfig}
		client := &http.Client{Transport: transport}
		fmt.Printf(" trying direct connection \n")

		req, err := http.NewRequest("GET", "https://192.168.1.98:4443/api/endpointx", nil)
		if err != nil {
			fmt.Println(err)
		}

		req.Header.Set("Cookie", "JSESSIONID=shszrhdrhdrhjdhdr")
		req.Header.Set("Referer", "https://aaegesgsegesgegeg")
		req.Header.Set("Upgrade-Insecure-Requests", "1")
		req.Header.Set("Connection", "close")
		req.Header.Set("Accept-Language", "it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3")
		req.Header.Set("Accept", "*/*")
		req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0")
		time.Sleep(5 * time.Second)
		re := regexp.MustCompile(`:"(.*)"`)
		resp, err := client.Do(req)

		if err == nil {
			body, _ := ioutil.ReadAll(resp.Body)

			match := re.FindStringSubmatch(string(body))
			fmt.Println("quello che viene matchato è:", match[1])

			// I know this match does not look like the else, I made it just to get
			//inside the else
			if strings.Contains(string(body), "aaaaaaaa") {
				fmt.Printf(" inside IF\n ")
				return client
			} else {
				fmt.Printf(" inside else\n ")
				clientP := &http.Client{Transport: PTransport}
				resp, err := clientP.Do(req)
				if err == nil {
					body, _ := ioutil.ReadAll(resp.Body)

					if strings.Contains(string(body), match[1]) {
						client := clientP
						// here. this return should overwrite the old client variable
						// so that the next http requests will use the proxy settings
						return client
					}
				}
			}
		}
	}
}

答案1

得分: 0

> 但它仍然没有使用代理设置

这意味着它从不进入以下代码块:

                    if strings.Contains(string(body), match[1]) {
                        client = clientP
                        // 在这里,这个返回语句应该覆盖旧的 client 变量
                        // 这样下一个 HTTP 请求就会使用代理设置
                        return client
                    }

你需要确保 string(body) 实际上包含 match[1]

OP ryout评论中确认:

> 即使在 client = clientP 之后,问题仍未解决,因为服务器系统的变量。
在 Linux 中,代理设置为 https_proxy='httpS://...' 而不是 http

英文:

> but it still does not use the proxy settings

That should mean it never goes into

                    if strings.Contains(string(body), match[1]) {
                        client = clientP
                        // here. this return should overwrite the old client variable
                        // so that the next http requests will use the proxy settings
                        return client
                    }

You need to make sure string(body) actually contains match[1].

The OP ryout confirms in the comments:

> Even after client = clientP, the problem was not solved, because of the server system's variable.
In Linux, the proxy was set to https_proxy='httpS://...' rather than http.

huangapple
  • 本文由 发表于 2021年10月14日 15:33:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/69566722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定