猿问

如何从状态为 101 切换协议的响应中读取正文

我连接到 Kubernetes 集群中的服务器,使用 POST 请求和标头来升级请求。我正在使用以下功能:


func PostRequest(client *http.Client, url string, bodyData []byte) (*http.Response, error){

    req, _ := http.NewRequest("POST", url, bytes.NewBuffer(bodyData))

    //req.Header.Set("Authorization", "Bearer " + BEARER_TOKEN)

    req.Header.Set("X-Stream-Protocol-Version", "v2.channel.k8s.io")

    req.Header.Set("X-Stream-Protocol-Version", "channel.k8s.io")

    req.Header.Set("Upgrade", "SPDY/3.1")

    req.Header.Set("Connection","upgrade")

    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")


    resp, err := (*client).Do(req)


    return resp, err

}

收到回复后,我试图阅读它,但当我阅读正文时它卡住了:


url2 := "https://<serveri_ip>:10250/exec/default/mypod/mycontainer?command=ls&command=/&input=1&output=1&tty=1"


resp, err := PostRequest(api.GlobalClient, url2, []byte(""))

fmt.Println(r.Status)

fmt.Println(r.Header)

bodyBytes, err := ioutil.ReadAll(r.Body) // -> it stuck here

fmt.Println(string(bodyBytes))  

我想它试图打开 websocket,所以我尝试像这样使用gorilla websocket 库:


u := url.URL{Scheme: "ws", Host: "<node_ip>:10250", Path: "/exec/default/mypod/mycontainer?command=ls&command=/&input=1&output=1&tty=1"}


interrupt := make(chan os.Signal, 1)

signal.Notify(interrupt, os.Interrupt)


c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)

if err != nil {

  log.Fatal("dial:", err)

}

defer c.Close()

但是它打印了一个错误:


2020/04/04 20:51:25 拨号:websocket:握手错误


我做错了什么?

如何从状态“切换协议”中读取响应正文


蛊毒传说
浏览 117回答 1
1回答

慕虎7371278

我设法用 Kubernetes go-client库做到了:package mainimport (&nbsp; &nbsp; "crypto/tls"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "net/http"&nbsp; &nbsp; "net/url"&nbsp; &nbsp; "os"&nbsp; &nbsp; "time"&nbsp; &nbsp; restclient "k8s.io/client-go/rest"&nbsp; &nbsp; "k8s.io/client-go/tools/remotecommand")func main(){&nbsp; &nbsp; /*req.Header.Add("X-Stream-Protocol-Version", "v4.channel.k8s.io")&nbsp; &nbsp; req.Header.Add("X-Stream-Protocol-Version", "v3.channel.k8s.io")&nbsp; &nbsp; req.Header.Add("X-Stream-Protocol-Version", "v2.channel.k8s.io")&nbsp; &nbsp; req.Header.Add("X-Stream-Protocol-Version", "channel.k8s.io")&nbsp; &nbsp; req.Header.Add("Connection", "upgrade")&nbsp; &nbsp; req.Header.Add("Upgrade", "SPDY/3.1")*/&nbsp; &nbsp; //url2 := "https://123.123.123.123:10250/exec/default/my-pod/nginx?command=ls&command=/&input=1&output=1&tty=1"&nbsp; &nbsp; tr := &http.Transport{&nbsp; &nbsp; &nbsp; &nbsp; MaxIdleConns:&nbsp; &nbsp; &nbsp; &nbsp;10,&nbsp; &nbsp; &nbsp; &nbsp; IdleConnTimeout:&nbsp; &nbsp; 30 * time.Second,&nbsp; &nbsp; &nbsp; &nbsp; DisableCompression: true,&nbsp; &nbsp; &nbsp; &nbsp; TLSClientConfig: &tls.Config{InsecureSkipVerify: true},&nbsp; &nbsp; }&nbsp; &nbsp; config := &restclient.Config{&nbsp; &nbsp; &nbsp; &nbsp; Host:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "https://123.123.123.123:10250",&nbsp; &nbsp; &nbsp; &nbsp; APIPath:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"/exec/default/my-pod/nginx",&nbsp; &nbsp; &nbsp; &nbsp; TLSClientConfig:&nbsp; &nbsp; &nbsp;restclient.TLSClientConfig{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Insecure: true,&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; Transport:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tr,&nbsp; &nbsp; }&nbsp; &nbsp; url3 := &url.URL{&nbsp; &nbsp; &nbsp; &nbsp; Scheme:&nbsp; &nbsp; &nbsp;"https",&nbsp; &nbsp; &nbsp; &nbsp; Opaque:&nbsp; &nbsp; &nbsp;"",&nbsp; &nbsp; &nbsp; &nbsp; User:&nbsp; &nbsp; &nbsp; &nbsp;nil,&nbsp; &nbsp; &nbsp; &nbsp; Host:&nbsp; &nbsp; &nbsp; &nbsp;"123.123.123.123:10250",&nbsp; &nbsp; &nbsp; &nbsp; Path:&nbsp; &nbsp; &nbsp; &nbsp;"/exec/default/my-pod/nginx",&nbsp; &nbsp; &nbsp; &nbsp; RawPath:&nbsp; &nbsp; "",&nbsp; &nbsp; &nbsp; &nbsp; RawQuery:&nbsp; &nbsp;"command=ls&command=/&input=1&output=1&tty=1",&nbsp; &nbsp; }&nbsp; &nbsp; exec, err := remotecommand.NewSPDYExecutor(config, "POST", url3)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(err)&nbsp; &nbsp; }&nbsp; &nbsp; // Thanks for this blog post https://www.henryxieblogs.com/2019/05/&nbsp; &nbsp; err = exec.Stream(remotecommand.StreamOptions{&nbsp; &nbsp; &nbsp; &nbsp; Stdin:&nbsp; os.Stdin,&nbsp; &nbsp; &nbsp; &nbsp; Stdout: os.Stdout,&nbsp; &nbsp; &nbsp; &nbsp; Stderr: os.Stderr,&nbsp; &nbsp; &nbsp; &nbsp; Tty:true,&nbsp; &nbsp; })&nbsp; &nbsp; fmt.Println(err)}
随时随地看视频慕课网APP

相关分类

Go
我要回答