Skip to content

Instantly share code, notes, and snippets.

@JokerCatz
Last active January 2, 2022 22:08
Show Gist options
  • Save JokerCatz/4ab8b8088aaa88d7f0680e26b92a9df8 to your computer and use it in GitHub Desktop.
Save JokerCatz/4ab8b8088aaa88d7f0680e26b92a9df8 to your computer and use it in GitHub Desktop.
Redpill of Golang disable http2
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
resty "gopkg.in/resty.v1"
)
var _httpClient = &http.Client{
Transport: &http.Transport{
TLSClientConfig: nil, // will make client use http2
},
}
func simpleRPC(nodeURL string, body string) (*[]byte, *string, *int, error) {
req, err := http.NewRequest("POST", nodeURL, strings.NewReader(body))
if err != nil {
return nil, nil, nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := _httpClient.Do(req)
if err != nil {
return nil, nil, nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, nil, nil, fmt.Errorf("!StatusOK")
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, nil, err
}
return &respBody, &resp.Proto, &resp.StatusCode, nil
}
func ForceDisableHttp2() {
goDebugEnvSet := []string{"http2client=0"}
for _, tEnv := range strings.Split(os.Getenv("GODEBUG"), ",") {
if strings.Contains(tEnv, "http2client") {
continue
}
goDebugEnvSet = append(goDebugEnvSet, strings.TrimSpace(tEnv))
}
os.Setenv("GODEBUG", strings.Join(goDebugEnvSet, ","))
}
func init() {
ForceDisableHttp2() // enable / disable to test me
}
func main() {
url := `https://WTF`
payload := `{"WTF":true}`
body, proto, statusCode, err := simpleRPC(url, payload)
if err != nil {
fmt.Println("normal failed", err)
} else {
fmt.Println("normal success", *proto, *statusCode, string(*body))
}
resp, err := resty.
R().
SetHeader("Content-Type", "application/json").
SetBody(payload).
Post(url)
if err != nil {
fmt.Println("resty failed", err)
} else {
fmt.Println("resty success", resp.RawResponse.Proto, resp.RawResponse.StatusCode, string(resp.Body()))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment