refactor: move from io/ioutil to io and os package (#2592)

This commit is contained in:
kekeimiku 2021-09-29 10:33:57 +08:00 committed by GitHub
parent 72a7fd948e
commit 0cee1877e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 38 additions and 44 deletions

View File

@ -17,8 +17,9 @@ package client
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os"
"sort" "sort"
"strings" "strings"
@ -256,7 +257,7 @@ func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
}() }()
// get new config content // get new config content
body, err := ioutil.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
res.Code = 400 res.Code = 400
res.Msg = fmt.Sprintf("read request body error: %v", err) res.Msg = fmt.Sprintf("read request body error: %v", err)
@ -273,7 +274,7 @@ func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
// get token from origin content // get token from origin content
token := "" token := ""
b, err := ioutil.ReadFile(svr.cfgFile) b, err := os.ReadFile(svr.cfgFile)
if err != nil { if err != nil {
res.Code = 400 res.Code = 400
res.Msg = err.Error() res.Msg = err.Error()
@ -312,7 +313,7 @@ func (svr *Service) apiPutConfig(w http.ResponseWriter, r *http.Request) {
} }
content = strings.Join(newRows, "\n") content = strings.Join(newRows, "\n")
err = ioutil.WriteFile(svr.cfgFile, []byte(content), 0644) err = os.WriteFile(svr.cfgFile, []byte(content), 0644)
if err != nil { if err != nil {
res.Code = 500 res.Code = 500
res.Msg = fmt.Sprintf("write content to frpc config file error: %v", err) res.Msg = fmt.Sprintf("write content to frpc config file error: %v", err)

View File

@ -19,7 +19,6 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"time" "time"
@ -162,7 +161,7 @@ func (monitor *Monitor) doHTTPCheck(ctx context.Context) error {
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body) io.Copy(io.Discard, resp.Body)
if resp.StatusCode/100 != 2 { if resp.StatusCode/100 != 2 {
return fmt.Errorf("do http health check, StatusCode is [%d] not 2xx", resp.StatusCode) return fmt.Errorf("do http health check, StatusCode is [%d] not 2xx", resp.StatusCode)

View File

@ -19,7 +19,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"strconv" "strconv"
"strings" "strings"
@ -401,7 +400,7 @@ func (pxy *XTCPProxy) InWorkConn(conn net.Conn, m *msg.StartWorkConn) {
fmuxCfg := fmux.DefaultConfig() fmuxCfg := fmux.DefaultConfig()
fmuxCfg.KeepAliveInterval = 5 * time.Second fmuxCfg.KeepAliveInterval = 5 * time.Second
fmuxCfg.LogOutput = ioutil.Discard fmuxCfg.LogOutput = io.Discard
sess, err := fmux.Server(kcpConn, fmuxCfg) sess, err := fmux.Server(kcpConn, fmuxCfg)
if err != nil { if err != nil {
xl.Error("create yamux server from kcp connection error: %v", err) xl.Error("create yamux server from kcp connection error: %v", err)

View File

@ -19,7 +19,7 @@ import (
"crypto/tls" "crypto/tls"
"errors" "errors"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"runtime" "runtime"
"strconv" "strconv"
@ -246,7 +246,7 @@ func (svr *Service) login() (conn net.Conn, session *fmux.Session, err error) {
if svr.cfg.TCPMux { if svr.cfg.TCPMux {
fmuxCfg := fmux.DefaultConfig() fmuxCfg := fmux.DefaultConfig()
fmuxCfg.KeepAliveInterval = 20 * time.Second fmuxCfg.KeepAliveInterval = 20 * time.Second
fmuxCfg.LogOutput = ioutil.Discard fmuxCfg.LogOutput = io.Discard
session, err = fmux.Client(conn, fmuxCfg) session, err = fmux.Client(conn, fmuxCfg)
if err != nil { if err != nil {
return return

View File

@ -19,7 +19,6 @@ import (
"context" "context"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"sync" "sync"
"time" "time"
@ -308,7 +307,7 @@ func (sv *XTCPVisitor) handleConn(userConn net.Conn) {
fmuxCfg := fmux.DefaultConfig() fmuxCfg := fmux.DefaultConfig()
fmuxCfg.KeepAliveInterval = 5 * time.Second fmuxCfg.KeepAliveInterval = 5 * time.Second
fmuxCfg.LogOutput = ioutil.Discard fmuxCfg.LogOutput = io.Discard
sess, err := fmux.Client(remote, fmuxCfg) sess, err := fmux.Client(remote, fmuxCfg)
if err != nil { if err != nil {
xl.Error("create yamux session error: %v", err) xl.Error("create yamux session error: %v", err)

View File

@ -17,7 +17,7 @@ package sub
import ( import (
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -76,7 +76,7 @@ func reload(clientCfg config.ClientCommonConf) error {
return nil return nil
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }

View File

@ -18,7 +18,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@ -77,7 +77,7 @@ func status(clientCfg config.ClientCommonConf) error {
return fmt.Errorf("admin api status code [%d]", resp.StatusCode) return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
} }
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }

View File

@ -17,7 +17,6 @@ package config
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@ -77,7 +76,7 @@ func getIncludeContents(paths []string) ([]byte, error) {
if _, err := os.Stat(absDir); os.IsNotExist(err) { if _, err := os.Stat(absDir); os.IsNotExist(err) {
return nil, err return nil, err
} }
files, err := ioutil.ReadDir(absDir) files, err := os.ReadDir(absDir)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -16,7 +16,6 @@ package config
import ( import (
"bytes" "bytes"
"io/ioutil"
"os" "os"
"strings" "strings"
"text/template" "text/template"
@ -67,7 +66,7 @@ func RenderContent(in []byte) (out []byte, err error) {
func GetRenderedConfFromFile(path string) (out []byte, err error) { func GetRenderedConfFromFile(path string) (out []byte, err error) {
var b []byte var b []byte
b, err = ioutil.ReadFile(path) b, err = os.ReadFile(path)
if err != nil { if err != nil {
return return
} }

View File

@ -16,7 +16,6 @@ package plugin
import ( import (
"io" "io"
"io/ioutil"
"log" "log"
"net" "net"
@ -43,7 +42,7 @@ func NewSocks5Plugin(params map[string]string) (p Plugin, err error) {
passwd := params["plugin_passwd"] passwd := params["plugin_passwd"]
cfg := &gosocks5.Config{ cfg := &gosocks5.Config{
Logger: log.New(ioutil.Discard, "", log.LstdFlags), Logger: log.New(io.Discard, "", log.LstdFlags),
} }
if user != "" || passwd != "" { if user != "" || passwd != "" {
cfg.Credentials = gosocks5.StaticCredentials(map[string]string{user: passwd}) cfg.Credentials = gosocks5.StaticCredentials(map[string]string{user: passwd})

View File

@ -20,7 +20,7 @@ import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"reflect" "reflect"
@ -116,7 +116,7 @@ func (p *httpPlugin) do(ctx context.Context, r *Request, res *Response) error {
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("do http request error code: %d", resp.StatusCode) return fmt.Errorf("do http request error code: %d", resp.StatusCode)
} }
buf, err = ioutil.ReadAll(resp.Body) buf, err = io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }

View File

@ -6,8 +6,8 @@ import (
"crypto/tls" "crypto/tls"
"crypto/x509" "crypto/x509"
"encoding/pem" "encoding/pem"
"io/ioutil"
"math/big" "math/big"
"os"
) )
func newCustomTLSKeyPair(certfile, keyfile string) (*tls.Certificate, error) { func newCustomTLSKeyPair(certfile, keyfile string) (*tls.Certificate, error) {
@ -47,7 +47,7 @@ func newRandomTLSKeyPair() *tls.Certificate {
func newCertPool(caPath string) (*x509.CertPool, error) { func newCertPool(caPath string) (*x509.CertPool, error) {
pool := x509.NewCertPool() pool := x509.NewCertPool()
caCrt, err := ioutil.ReadFile(caPath) caCrt, err := os.ReadFile(caPath)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -16,8 +16,9 @@ package vhost
import ( import (
"bytes" "bytes"
"io/ioutil" "io"
"net/http" "net/http"
"os"
frpLog "github.com/fatedier/frp/pkg/util/log" frpLog "github.com/fatedier/frp/pkg/util/log"
"github.com/fatedier/frp/pkg/util/version" "github.com/fatedier/frp/pkg/util/version"
@ -57,7 +58,7 @@ func getNotFoundPageContent() []byte {
err error err error
) )
if NotFoundPagePath != "" { if NotFoundPagePath != "" {
buf, err = ioutil.ReadFile(NotFoundPagePath) buf, err = os.ReadFile(NotFoundPagePath)
if err != nil { if err != nil {
frpLog.Warn("read custom 404 page error: %v", err) frpLog.Warn("read custom 404 page error: %v", err)
buf = []byte(NotFound) buf = []byte(NotFound)
@ -80,7 +81,7 @@ func notFoundResponse() *http.Response {
ProtoMajor: 1, ProtoMajor: 1,
ProtoMinor: 0, ProtoMinor: 0,
Header: header, Header: header,
Body: ioutil.NopCloser(bytes.NewReader(getNotFoundPageContent())), Body: io.NopCloser(bytes.NewReader(getNotFoundPageContent())),
} }
return res return res
} }

View File

@ -19,7 +19,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"sort" "sort"
@ -406,7 +406,7 @@ func (svr *Service) HandleListener(l net.Listener) {
if svr.cfg.TCPMux { if svr.cfg.TCPMux {
fmuxCfg := fmux.DefaultConfig() fmuxCfg := fmux.DefaultConfig()
fmuxCfg.KeepAliveInterval = 20 * time.Second fmuxCfg.KeepAliveInterval = 20 * time.Second
fmuxCfg.LogOutput = ioutil.Discard fmuxCfg.LogOutput = io.Discard
session, err := fmux.Server(frpConn, fmuxCfg) session, err := fmux.Server(frpConn, fmuxCfg)
if err != nil { if err != nil {
log.Warn("Failed to create mux connection: %v", err) log.Warn("Failed to create mux connection: %v", err)

View File

@ -3,7 +3,6 @@ package framework
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -90,7 +89,7 @@ func (f *Framework) BeforeEach() {
f.cleanupHandle = AddCleanupAction(f.AfterEach) f.cleanupHandle = AddCleanupAction(f.AfterEach)
dir, err := ioutil.TempDir(os.TempDir(), "frp-e2e-test-*") dir, err := os.MkdirTemp(os.TempDir(), "frp-e2e-test-*")
ExpectNoError(err) ExpectNoError(err)
f.TempDirectory = dir f.TempDirectory = dir
@ -260,7 +259,7 @@ func (f *Framework) SetEnvs(envs []string) {
func (f *Framework) WriteTempFile(name string, content string) string { func (f *Framework) WriteTempFile(name string, content string) string {
filePath := filepath.Join(f.TempDirectory, name) filePath := filepath.Join(f.TempDirectory, name)
err := ioutil.WriteFile(filePath, []byte(content), 0766) err := os.WriteFile(filePath, []byte(content), 0766)
ExpectNoError(err) ExpectNoError(err)
return filePath return filePath
} }

View File

@ -2,7 +2,7 @@ package framework
import ( import (
"fmt" "fmt"
"io/ioutil" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -30,7 +30,7 @@ func (f *Framework) RunProcesses(serverTemplates []string, clientTemplates []str
for i := range serverTemplates { for i := range serverTemplates {
path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-server-%d", i)) path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-server-%d", i))
err = ioutil.WriteFile(path, []byte(outs[i]), 0666) err = os.WriteFile(path, []byte(outs[i]), 0666)
ExpectNoError(err) ExpectNoError(err)
flog.Trace("[%s] %s", path, outs[i]) flog.Trace("[%s] %s", path, outs[i])
@ -45,7 +45,7 @@ func (f *Framework) RunProcesses(serverTemplates []string, clientTemplates []str
for i := range clientTemplates { for i := range clientTemplates {
index := i + len(serverTemplates) index := i + len(serverTemplates)
path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-client-%d", i)) path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-client-%d", i))
err = ioutil.WriteFile(path, []byte(outs[index]), 0666) err = os.WriteFile(path, []byte(outs[index]), 0666)
ExpectNoError(err) ExpectNoError(err)
flog.Trace("[%s] %s", path, outs[index]) flog.Trace("[%s] %s", path, outs[index])
@ -85,7 +85,7 @@ func (f *Framework) RunFrpc(args ...string) (*process.Process, string, error) {
func (f *Framework) GenerateConfigFile(content string) string { func (f *Framework) GenerateConfigFile(content string) string {
f.configFileIndex++ f.configFileIndex++
path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-config-%d", f.configFileIndex)) path := filepath.Join(f.TempDirectory, fmt.Sprintf("frp-e2e-config-%d", f.configFileIndex))
err := ioutil.WriteFile(path, []byte(content), 0666) err := os.WriteFile(path, []byte(content), 0666)
ExpectNoError(err) ExpectNoError(err)
return path return path
} }

View File

@ -6,7 +6,6 @@ import (
"crypto/tls" "crypto/tls"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"net" "net"
"net/http" "net/http"
"net/url" "net/url"
@ -219,7 +218,7 @@ func (r *Request) sendHTTPRequest(method, urlstr string, host string, headers ma
} }
ret := &Response{Code: resp.StatusCode, Header: resp.Header} ret := &Response{Code: resp.StatusCode, Header: resp.Header}
buf, err := ioutil.ReadAll(resp.Body) buf, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -3,7 +3,7 @@ package client
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"strconv" "strconv"
@ -125,7 +125,7 @@ func (c *Client) do(req *http.Request) (string, error) {
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return "", fmt.Errorf("api status code [%d]", resp.StatusCode) return "", fmt.Errorf("api status code [%d]", resp.StatusCode)
} }
buf, err := ioutil.ReadAll(resp.Body) buf, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return "", err return "", err
} }

View File

@ -3,7 +3,7 @@ package plugin
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"io/ioutil" "io"
"net/http" "net/http"
plugin "github.com/fatedier/frp/pkg/plugin/server" plugin "github.com/fatedier/frp/pkg/plugin/server"
@ -21,7 +21,7 @@ func NewHTTPPluginServer(port int, newFunc NewPluginRequest, handler PluginHandl
httpserver.WithTlsConfig(tlsConfig), httpserver.WithTlsConfig(tlsConfig),
httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { httpserver.WithHandler(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
r := newFunc() r := newFunc()
buf, err := ioutil.ReadAll(req.Body) buf, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
w.WriteHeader(500) w.WriteHeader(500)
return return