From 75383a95b360d3ed05b946800c627488e6a1fefc Mon Sep 17 00:00:00 2001 From: Jiajun Huang Date: Thu, 30 May 2019 12:11:59 +0800 Subject: [PATCH] resp.Body must be closed after function return whether it's success or fail, otherwise it will cause memory leak ref: https://golang.org/pkg/net/http/ --- cmd/frpc/sub/reload.go | 23 ++++--- cmd/frpc/sub/status.go | 141 +++++++++++++++++++++-------------------- tests/util/util.go | 98 ++++++++++++++-------------- 3 files changed, 131 insertions(+), 131 deletions(-) diff --git a/cmd/frpc/sub/reload.go b/cmd/frpc/sub/reload.go index 647bf1e2..46118c3e 100644 --- a/cmd/frpc/sub/reload.go +++ b/cmd/frpc/sub/reload.go @@ -76,17 +76,16 @@ func reload() error { resp, err := http.DefaultClient.Do(req) if err != nil { return err - } else { - if resp.StatusCode == 200 { - return nil - } - - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - return fmt.Errorf("code [%d], %s", resp.StatusCode, strings.TrimSpace(string(body))) } - return nil + defer resp.Body.Close() + + if resp.StatusCode == 200 { + return nil + } + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + return fmt.Errorf("code [%d], %s", resp.StatusCode, strings.TrimSpace(string(body))) } diff --git a/cmd/frpc/sub/status.go b/cmd/frpc/sub/status.go index 883c7a7f..91887ef1 100644 --- a/cmd/frpc/sub/status.go +++ b/cmd/frpc/sub/status.go @@ -78,76 +78,77 @@ func status() error { resp, err := http.DefaultClient.Do(req) if err != nil { return err - } else { - if resp.StatusCode != 200 { - return fmt.Errorf("admin api status code [%d]", resp.StatusCode) - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return err - } - res := &client.StatusResp{} - err = json.Unmarshal(body, &res) - if err != nil { - return fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body))) - } - - fmt.Println("Proxy Status...") - if len(res.Tcp) > 0 { - fmt.Printf("TCP") - tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") - for _, ps := range res.Tcp { - tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) - } - tbl.Print() - fmt.Println("") - } - if len(res.Udp) > 0 { - fmt.Printf("UDP") - tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") - for _, ps := range res.Udp { - tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) - } - tbl.Print() - fmt.Println("") - } - if len(res.Http) > 0 { - fmt.Printf("HTTP") - tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") - for _, ps := range res.Http { - tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) - } - tbl.Print() - fmt.Println("") - } - if len(res.Https) > 0 { - fmt.Printf("HTTPS") - tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") - for _, ps := range res.Https { - tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) - } - tbl.Print() - fmt.Println("") - } - if len(res.Stcp) > 0 { - fmt.Printf("STCP") - tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") - for _, ps := range res.Stcp { - tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) - } - tbl.Print() - fmt.Println("") - } - if len(res.Xtcp) > 0 { - fmt.Printf("XTCP") - tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") - for _, ps := range res.Xtcp { - tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) - } - tbl.Print() - fmt.Println("") - } } + if resp.StatusCode != 200 { + return fmt.Errorf("admin api status code [%d]", resp.StatusCode) + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + res := &client.StatusResp{} + err = json.Unmarshal(body, &res) + if err != nil { + return fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body))) + } + + fmt.Println("Proxy Status...") + if len(res.Tcp) > 0 { + fmt.Printf("TCP") + tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") + for _, ps := range res.Tcp { + tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) + } + tbl.Print() + fmt.Println("") + } + if len(res.Udp) > 0 { + fmt.Printf("UDP") + tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") + for _, ps := range res.Udp { + tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) + } + tbl.Print() + fmt.Println("") + } + if len(res.Http) > 0 { + fmt.Printf("HTTP") + tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") + for _, ps := range res.Http { + tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) + } + tbl.Print() + fmt.Println("") + } + if len(res.Https) > 0 { + fmt.Printf("HTTPS") + tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") + for _, ps := range res.Https { + tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) + } + tbl.Print() + fmt.Println("") + } + if len(res.Stcp) > 0 { + fmt.Printf("STCP") + tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") + for _, ps := range res.Stcp { + tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) + } + tbl.Print() + fmt.Println("") + } + if len(res.Xtcp) > 0 { + fmt.Printf("XTCP") + tbl := table.New("Name", "Status", "LocalAddr", "Plugin", "RemoteAddr", "Error") + for _, ps := range res.Xtcp { + tbl.AddRow(ps.Name, ps.Status, ps.LocalAddr, ps.Plugin, ps.RemoteAddr, ps.Err) + } + tbl.Print() + fmt.Println("") + } + return nil } diff --git a/tests/util/util.go b/tests/util/util.go index 2070ce31..f4e40111 100644 --- a/tests/util/util.go +++ b/tests/util/util.go @@ -28,51 +28,51 @@ func GetProxyStatus(statusAddr string, user string, passwd string, name string) resp, err := http.DefaultClient.Do(req) if err != nil { return status, err - } else { - if resp.StatusCode != 200 { - return status, fmt.Errorf("admin api status code [%d]", resp.StatusCode) - } - defer resp.Body.Close() - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return status, err - } - allStatus := &client.StatusResp{} - err = json.Unmarshal(body, &allStatus) - if err != nil { - return status, fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body))) - } - for _, s := range allStatus.Tcp { - if s.Name == name { - return &s, nil - } - } - for _, s := range allStatus.Udp { - if s.Name == name { - return &s, nil - } - } - for _, s := range allStatus.Http { - if s.Name == name { - return &s, nil - } - } - for _, s := range allStatus.Https { - if s.Name == name { - return &s, nil - } - } - for _, s := range allStatus.Stcp { - if s.Name == name { - return &s, nil - } - } - for _, s := range allStatus.Xtcp { - if s.Name == name { - return &s, nil - } + } + if resp.StatusCode != 200 { + return status, fmt.Errorf("admin api status code [%d]", resp.StatusCode) + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return status, err + } + allStatus := &client.StatusResp{} + err = json.Unmarshal(body, &allStatus) + if err != nil { + return status, fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body))) + } + for _, s := range allStatus.Tcp { + if s.Name == name { + return &s, nil } } + for _, s := range allStatus.Udp { + if s.Name == name { + return &s, nil + } + } + for _, s := range allStatus.Http { + if s.Name == name { + return &s, nil + } + } + for _, s := range allStatus.Https { + if s.Name == name { + return &s, nil + } + } + for _, s := range allStatus.Stcp { + if s.Name == name { + return &s, nil + } + } + for _, s := range allStatus.Xtcp { + if s.Name == name { + return &s, nil + } + } + return status, errors.New("no proxy status found") } @@ -87,13 +87,13 @@ func ReloadConf(reloadAddr string, user string, passwd string) error { resp, err := http.DefaultClient.Do(req) if err != nil { return err - } else { - if resp.StatusCode != 200 { - return fmt.Errorf("admin api status code [%d]", resp.StatusCode) - } - defer resp.Body.Close() - io.Copy(ioutil.Discard, resp.Body) } + defer resp.Body.Close() + + if resp.StatusCode != 200 { + return fmt.Errorf("admin api status code [%d]", resp.StatusCode) + } + io.Copy(ioutil.Discard, resp.Body) return nil }