优化代码

This commit is contained in:
刘祥超
2022-08-04 11:41:42 +08:00
parent b8babaae39
commit 0414cc02e8
16 changed files with 38 additions and 44 deletions

View File

@@ -10,7 +10,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/utils" "github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
"io/ioutil" "io"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@@ -118,7 +118,7 @@ func (this *ESStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
}() }()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
bodyData, _ := ioutil.ReadAll(resp.Body) bodyData, _ := io.ReadAll(resp.Body)
return errors.New("ElasticSearch response status code: " + fmt.Sprintf("%d", resp.StatusCode) + " content: " + string(bodyData)) return errors.New("ElasticSearch response status code: " + fmt.Sprintf("%d", resp.StatusCode) + " content: " + string(bodyData))
} }

View File

@@ -10,7 +10,7 @@ import (
"github.com/go-acme/lego/v4/challenge/dns01" "github.com/go-acme/lego/v4/challenge/dns01"
"github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/lego"
acmelog "github.com/go-acme/lego/v4/log" acmelog "github.com/go-acme/lego/v4/log"
"io/ioutil" "io"
"log" "log"
"testing" "testing"
@@ -50,7 +50,7 @@ func (this *MyProvider) CleanUp(domain, token, keyAuth string) error {
// 参考 https://go-acme.github.io/lego/usage/library/ // 参考 https://go-acme.github.io/lego/usage/library/
func TestGenerate(t *testing.T) { func TestGenerate(t *testing.T) {
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags) acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
// 生成私钥 // 生成私钥
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
@@ -94,7 +94,7 @@ func TestGenerate(t *testing.T) {
} }
func TestGenerate_EAB(t *testing.T) { func TestGenerate_EAB(t *testing.T) {
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags) acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
// 生成私钥 // 生成私钥
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)

View File

@@ -8,7 +8,7 @@ import (
"github.com/go-acme/lego/v4/lego" "github.com/go-acme/lego/v4/lego"
acmelog "github.com/go-acme/lego/v4/log" acmelog "github.com/go-acme/lego/v4/log"
"github.com/go-acme/lego/v4/registration" "github.com/go-acme/lego/v4/registration"
"io/ioutil" "io"
"log" "log"
) )
@@ -55,7 +55,7 @@ func (this *Request) Run() (certData []byte, keyData []byte, err error) {
func (this *Request) runDNS() (certData []byte, keyData []byte, err error) { func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
if !this.debug { if !this.debug {
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags) acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
} }
if this.task.User == nil { if this.task.User == nil {
@@ -138,7 +138,7 @@ func (this *Request) runDNS() (certData []byte, keyData []byte, err error) {
func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) { func (this *Request) runHTTP() (certData []byte, keyData []byte, err error) {
if !this.debug { if !this.debug {
acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags) acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
} }
if this.task.User == nil { if this.task.User == nil {

View File

@@ -4,7 +4,6 @@ import (
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const" teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"io/ioutil"
"os" "os"
"path/filepath" "path/filepath"
) )
@@ -42,7 +41,7 @@ func SharedAPIConfig() (*APIConfig, error) {
var data []byte var data []byte
var err error var err error
for _, path := range paths { for _, path := range paths {
data, err = ioutil.ReadFile(path) data, err = os.ReadFile(path)
if err == nil { if err == nil {
if path == localFile { if path == localFile {
isFromLocal = true isFromLocal = true
@@ -63,7 +62,7 @@ func SharedAPIConfig() (*APIConfig, error) {
if !isFromLocal { if !isFromLocal {
// 恢复文件 // 恢复文件
_ = ioutil.WriteFile(localFile, data, 0666) _ = os.WriteFile(localFile, data, 0666)
} }
// 恢复数据库文件 // 恢复数据库文件
@@ -80,9 +79,9 @@ func SharedAPIConfig() (*APIConfig, error) {
for _, path := range paths { for _, path := range paths {
_, err := os.Stat(path) _, err := os.Stat(path)
if err == nil { if err == nil {
data, err := ioutil.ReadFile(path) data, err := os.ReadFile(path)
if err == nil { if err == nil {
_ = ioutil.WriteFile(dbConfigFile, data, 0666) _ = os.WriteFile(dbConfigFile, data, 0666)
break break
} }
} }
@@ -122,14 +121,14 @@ func (this *APIConfig) WriteFile(path string) error {
for _, backupDir := range backupDirs { for _, backupDir := range backupDirs {
stat, err := os.Stat(backupDir) stat, err := os.Stat(backupDir)
if err == nil && stat.IsDir() { if err == nil && stat.IsDir() {
_ = ioutil.WriteFile(backupDir+"/"+filename, data, 0666) _ = os.WriteFile(backupDir+"/"+filename, data, 0666)
} else if err != nil && os.IsNotExist(err) { } else if err != nil && os.IsNotExist(err) {
err = os.Mkdir(backupDir, 0777) err = os.Mkdir(backupDir, 0777)
if err == nil { if err == nil {
_ = ioutil.WriteFile(backupDir+"/"+filename, data, 0666) _ = os.WriteFile(backupDir+"/"+filename, data, 0666)
} }
} }
} }
return ioutil.WriteFile(path, data, 0666) return os.WriteFile(path, data, 0666)
} }

View File

@@ -13,7 +13,6 @@ import (
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@@ -270,7 +269,7 @@ func (this *CloudFlareProvider) doAPI(method string, apiPath string, args map[st
_ = resp.Body.Close() _ = resp.Body.Close()
}() }()
data, err := ioutil.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -10,7 +10,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes" "github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
"github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/errors"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"io/ioutil" "io"
"net/http" "net/http"
"strconv" "strconv"
"time" "time"
@@ -180,5 +180,5 @@ func (this *CustomHTTPProvider) post(params maps.Map) (respData []byte, err erro
if resp.StatusCode != 200 { if resp.StatusCode != 200 {
return nil, errors.New("status should be 200, but got '" + strconv.Itoa(resp.StatusCode) + "'") return nil, errors.New("status should be 200, but got '" + strconv.Itoa(resp.StatusCode) + "'")
} }
return ioutil.ReadAll(resp.Body) return io.ReadAll(resp.Body)
} }

View File

@@ -8,7 +8,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils" "github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"io/ioutil" "io"
"net/http" "net/http"
"net/url" "net/url"
"strings" "strings"
@@ -276,7 +276,7 @@ func (this *DNSPodProvider) post(path string, params map[string]string) (maps.Ma
defer func() { defer func() {
_ = resp.Body.Close() _ = resp.Body.Close()
}() }()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -15,7 +15,6 @@ import (
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"io" "io"
"io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
"sort" "sort"
@@ -1502,7 +1501,7 @@ func (this *HuaweiDNSProvider) doAPI(method string, apiPath string, args map[str
_ = resp.Body.Close() _ = resp.Body.Close()
}() }()
data, err := ioutil.ReadAll(resp.Body) data, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
return err return err
} }

View File

@@ -4,7 +4,7 @@ package iplibrary
import ( import (
"errors" "errors"
"io/ioutil" "os"
"strconv" "strconv"
"strings" "strings"
) )
@@ -62,7 +62,7 @@ func getIpInfo(cityId int64, line []byte) *IpInfo {
func NewIP2Region(path string) (*IP2Region, error) { func NewIP2Region(path string) (*IP2Region, error) {
var region = &IP2Region{} var region = &IP2Region{}
region.dbData, err = ioutil.ReadFile(path) region.dbData, err = os.ReadFile(path)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@@ -27,7 +27,6 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/credentials" "google.golang.org/grpc/credentials"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"io/ioutil"
"log" "log"
"net" "net"
"os" "os"
@@ -313,7 +312,7 @@ func (this *APINode) autoUpgrade() error {
// 执行SQL // 执行SQL
var config = &dbs.Config{} var config = &dbs.Config{}
configData, err := ioutil.ReadFile(Tea.ConfigFile("db.yaml")) configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
if err != nil { if err != nil {
return errors.New("read database config file failed: " + err.Error()) return errors.New("read database config file failed: " + err.Error())
} }
@@ -785,6 +784,6 @@ func (this *APINode) dbIssueSuggestion(errString string) string {
func (this *APINode) saveIssues() { func (this *APINode) saveIssues() {
issuesJSON, err := json.Marshal(this.issues) issuesJSON, err := json.Marshal(this.issues)
if err == nil { if err == nil {
_ = ioutil.WriteFile(this.issuesFile, issuesJSON, 0666) _ = os.WriteFile(this.issuesFile, issuesJSON, 0666)
} }
} }

View File

@@ -8,7 +8,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/rpc/services" "github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
"github.com/iwind/TeaGo/maps" "github.com/iwind/TeaGo/maps"
"io/ioutil" "io"
"net" "net"
"net/http" "net/http"
"reflect" "reflect"
@@ -152,7 +152,7 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) {
} }
// TODO 需要防止BODY过大攻击 // TODO 需要防止BODY过大攻击
body, err := ioutil.ReadAll(req.Body) body, err := io.ReadAll(req.Body)
if err != nil { if err != nil {
writer.WriteHeader(http.StatusBadRequest) writer.WriteHeader(http.StatusBadRequest)
_, _ = writer.Write([]byte(err.Error())) _, _ = writer.Write([]byte(err.Error()))

View File

@@ -11,7 +11,6 @@ import (
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"io/ioutil"
"os" "os"
"strconv" "strconv"
"strings" "strings"
@@ -75,7 +74,7 @@ func (this *Setup) Run() error {
// 执行SQL // 执行SQL
config := &dbs.Config{} config := &dbs.Config{}
configData, err := ioutil.ReadFile(Tea.ConfigFile("db.yaml")) configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
if err != nil { if err != nil {
return err return err
} }

View File

@@ -15,7 +15,7 @@ import (
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
stringutil "github.com/iwind/TeaGo/utils/string" stringutil "github.com/iwind/TeaGo/utils/string"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
"io/ioutil" "os"
"time" "time"
) )
@@ -35,7 +35,7 @@ func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor {
func NewSQLExecutorFromCmd() (*SQLExecutor, error) { func NewSQLExecutorFromCmd() (*SQLExecutor, error) {
// 执行SQL // 执行SQL
config := &dbs.Config{} config := &dbs.Config{}
configData, err := ioutil.ReadFile(Tea.ConfigFile("db.yaml")) configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@@ -15,7 +15,7 @@ import (
"github.com/TeaOSLab/EdgeAPI/internal/utils" "github.com/TeaOSLab/EdgeAPI/internal/utils"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"golang.org/x/crypto/ocsp" "golang.org/x/crypto/ocsp"
"io/ioutil" "io"
"net/http" "net/http"
"time" "time"
) )
@@ -147,7 +147,7 @@ func (this *SSLCertUpdateOCSPTask) UpdateCertOCSP(certOne *models.SSLCert) (ocsp
_ = issuerResp.Body.Close() _ = issuerResp.Body.Close()
}() }()
issuerData, err := ioutil.ReadAll(issuerResp.Body) issuerData, err := io.ReadAll(issuerResp.Body)
if err != nil { if err != nil {
return nil, 0, errors.New("read issuer certificate failed: '" + issuerURL + "': " + err.Error()) return nil, 0, errors.New("read issuer certificate failed: '" + issuerURL + "': " + err.Error())
} }
@@ -178,7 +178,7 @@ func (this *SSLCertUpdateOCSPTask) UpdateCertOCSP(certOne *models.SSLCert) (ocsp
_ = ocspResp.Body.Close() _ = ocspResp.Body.Close()
}() }()
respData, err := ioutil.ReadAll(ocspResp.Body) respData, err := io.ReadAll(ocspResp.Body)
if err != nil { if err != nil {
return nil, 0, errors.New("read ocsp failed: '" + ocspServerURL + "': " + err.Error()) return nil, 0, errors.New("read ocsp failed: '" + ocspServerURL + "': " + err.Error())
} }

View File

@@ -2,7 +2,7 @@ package utils
import ( import (
"crypto/tls" "crypto/tls"
"io/ioutil" "io"
"net/http" "net/http"
"net/http/httputil" "net/http/httputil"
"sync" "sync"
@@ -19,7 +19,7 @@ func DumpResponse(resp *http.Response) (header []byte, body []byte, err error) {
if err != nil { if err != nil {
return return
} }
body, err = ioutil.ReadAll(resp.Body) body, err = io.ReadAll(resp.Body)
return return
} }

View File

@@ -7,7 +7,6 @@ import (
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const" teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/iwind/TeaGo/Tea" "github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files" "github.com/iwind/TeaGo/files"
"io/ioutil"
"os" "os"
"os/exec" "os/exec"
"regexp" "regexp"
@@ -83,13 +82,13 @@ func (this *ServiceManager) installInitService(exePath string, args []string) er
return errors.New("'scripts/" + shortName + "' file not exists") return errors.New("'scripts/" + shortName + "' file not exists")
} }
data, err := ioutil.ReadFile(scriptFile) data, err := os.ReadFile(scriptFile)
if err != nil { if err != nil {
return err return err
} }
data = regexp.MustCompile("INSTALL_DIR=.+").ReplaceAll(data, []byte("INSTALL_DIR="+Tea.Root)) data = regexp.MustCompile("INSTALL_DIR=.+").ReplaceAll(data, []byte("INSTALL_DIR="+Tea.Root))
err = ioutil.WriteFile(initServiceFile, data, 0777) err = os.WriteFile(initServiceFile, data, 0777)
if err != nil { if err != nil {
return err return err
} }
@@ -137,7 +136,7 @@ ExecReload=` + exePath + ` reload
WantedBy=multi-user.target` WantedBy=multi-user.target`
// write file // write file
err := ioutil.WriteFile(systemdServiceFile, []byte(desc), 0777) err := os.WriteFile(systemdServiceFile, []byte(desc), 0777)
if err != nil { if err != nil {
return err return err
} }