Home > Back-end >  How to overwrite a returning value?
How to overwrite a returning value?

Time:10-16

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
                    }
                }
            }
        }
    }
}

CodePudding user response:

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.

  •  Tags:  
  • go
  • Related