英文:
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]
。
> 即使在 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
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论