mirror of
				https://github.com/TeaOSLab/EdgeAPI.git
				synced 2025-11-04 16:00:24 +08:00 
			
		
		
		
	优化代码
This commit is contained in:
		@@ -10,7 +10,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/serverconfigs"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
@@ -118,7 +118,7 @@ func (this *ESStorage) Write(accessLogs []*pb.HTTPAccessLog) error {
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	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))
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@ import (
 | 
			
		||||
	"github.com/go-acme/lego/v4/challenge/dns01"
 | 
			
		||||
	"github.com/go-acme/lego/v4/lego"
 | 
			
		||||
	acmelog "github.com/go-acme/lego/v4/log"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
@@ -50,7 +50,7 @@ func (this *MyProvider) CleanUp(domain, token, keyAuth string) error {
 | 
			
		||||
 | 
			
		||||
// 参考  https://go-acme.github.io/lego/usage/library/
 | 
			
		||||
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)
 | 
			
		||||
@@ -94,7 +94,7 @@ func TestGenerate(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)
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ import (
 | 
			
		||||
	"github.com/go-acme/lego/v4/lego"
 | 
			
		||||
	acmelog "github.com/go-acme/lego/v4/log"
 | 
			
		||||
	"github.com/go-acme/lego/v4/registration"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"io"
 | 
			
		||||
	"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) {
 | 
			
		||||
	if !this.debug {
 | 
			
		||||
		acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
 | 
			
		||||
		acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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) {
 | 
			
		||||
	if !this.debug {
 | 
			
		||||
		acmelog.Logger = log.New(ioutil.Discard, "", log.LstdFlags)
 | 
			
		||||
		acmelog.Logger = log.New(io.Discard, "", log.LstdFlags)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if this.task.User == nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,6 @@ import (
 | 
			
		||||
	teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"gopkg.in/yaml.v3"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
)
 | 
			
		||||
@@ -42,7 +41,7 @@ func SharedAPIConfig() (*APIConfig, error) {
 | 
			
		||||
	var data []byte
 | 
			
		||||
	var err error
 | 
			
		||||
	for _, path := range paths {
 | 
			
		||||
		data, err = ioutil.ReadFile(path)
 | 
			
		||||
		data, err = os.ReadFile(path)
 | 
			
		||||
		if err == nil {
 | 
			
		||||
			if path == localFile {
 | 
			
		||||
				isFromLocal = true
 | 
			
		||||
@@ -63,7 +62,7 @@ func SharedAPIConfig() (*APIConfig, error) {
 | 
			
		||||
 | 
			
		||||
	if !isFromLocal {
 | 
			
		||||
		// 恢复文件
 | 
			
		||||
		_ = ioutil.WriteFile(localFile, data, 0666)
 | 
			
		||||
		_ = os.WriteFile(localFile, data, 0666)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 恢复数据库文件
 | 
			
		||||
@@ -80,9 +79,9 @@ func SharedAPIConfig() (*APIConfig, error) {
 | 
			
		||||
			for _, path := range paths {
 | 
			
		||||
				_, err := os.Stat(path)
 | 
			
		||||
				if err == nil {
 | 
			
		||||
					data, err := ioutil.ReadFile(path)
 | 
			
		||||
					data, err := os.ReadFile(path)
 | 
			
		||||
					if err == nil {
 | 
			
		||||
						_ = ioutil.WriteFile(dbConfigFile, data, 0666)
 | 
			
		||||
						_ = os.WriteFile(dbConfigFile, data, 0666)
 | 
			
		||||
						break
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
@@ -122,14 +121,14 @@ func (this *APIConfig) WriteFile(path string) error {
 | 
			
		||||
	for _, backupDir := range backupDirs {
 | 
			
		||||
		stat, err := os.Stat(backupDir)
 | 
			
		||||
		if err == nil && stat.IsDir() {
 | 
			
		||||
			_ = ioutil.WriteFile(backupDir+"/"+filename, data, 0666)
 | 
			
		||||
			_ = os.WriteFile(backupDir+"/"+filename, data, 0666)
 | 
			
		||||
		} else if err != nil && os.IsNotExist(err) {
 | 
			
		||||
			err = os.Mkdir(backupDir, 0777)
 | 
			
		||||
			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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,6 @@ import (
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"strconv"
 | 
			
		||||
@@ -270,7 +269,7 @@ func (this *CloudFlareProvider) doAPI(method string, apiPath string, args map[st
 | 
			
		||||
		_ = resp.Body.Close()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	data, err := ioutil.ReadAll(resp.Body)
 | 
			
		||||
	data, err := io.ReadAll(resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -10,7 +10,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/dnsclients/dnstypes"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/errors"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"time"
 | 
			
		||||
@@ -180,5 +180,5 @@ func (this *CustomHTTPProvider) post(params maps.Map) (respData []byte, err erro
 | 
			
		||||
	if resp.StatusCode != 200 {
 | 
			
		||||
		return nil, errors.New("status should be 200, but got '" + strconv.Itoa(resp.StatusCode) + "'")
 | 
			
		||||
	}
 | 
			
		||||
	return ioutil.ReadAll(resp.Body)
 | 
			
		||||
	return io.ReadAll(resp.Body)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"strings"
 | 
			
		||||
@@ -276,7 +276,7 @@ func (this *DNSPodProvider) post(path string, params map[string]string) (maps.Ma
 | 
			
		||||
	defer func() {
 | 
			
		||||
		_ = resp.Body.Close()
 | 
			
		||||
	}()
 | 
			
		||||
	body, err := ioutil.ReadAll(resp.Body)
 | 
			
		||||
	body, err := io.ReadAll(resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,6 @@ import (
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"io"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/url"
 | 
			
		||||
	"sort"
 | 
			
		||||
@@ -1502,7 +1501,7 @@ func (this *HuaweiDNSProvider) doAPI(method string, apiPath string, args map[str
 | 
			
		||||
		_ = resp.Body.Close()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	data, err := ioutil.ReadAll(resp.Body)
 | 
			
		||||
	data, err := io.ReadAll(resp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@ package iplibrary
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"errors"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
)
 | 
			
		||||
@@ -62,7 +62,7 @@ func getIpInfo(cityId int64, line []byte) *IpInfo {
 | 
			
		||||
 | 
			
		||||
func NewIP2Region(path string) (*IP2Region, error) {
 | 
			
		||||
	var region = &IP2Region{}
 | 
			
		||||
	region.dbData, err = ioutil.ReadFile(path)
 | 
			
		||||
	region.dbData, err = os.ReadFile(path)
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
 
 | 
			
		||||
@@ -27,7 +27,6 @@ import (
 | 
			
		||||
	"google.golang.org/grpc"
 | 
			
		||||
	"google.golang.org/grpc/credentials"
 | 
			
		||||
	"gopkg.in/yaml.v3"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net"
 | 
			
		||||
	"os"
 | 
			
		||||
@@ -313,7 +312,7 @@ func (this *APINode) autoUpgrade() error {
 | 
			
		||||
 | 
			
		||||
	// 执行SQL
 | 
			
		||||
	var config = &dbs.Config{}
 | 
			
		||||
	configData, err := ioutil.ReadFile(Tea.ConfigFile("db.yaml"))
 | 
			
		||||
	configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		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() {
 | 
			
		||||
	issuesJSON, err := json.Marshal(this.issues)
 | 
			
		||||
	if err == nil {
 | 
			
		||||
		_ = ioutil.WriteFile(this.issuesFile, issuesJSON, 0666)
 | 
			
		||||
		_ = os.WriteFile(this.issuesFile, issuesJSON, 0666)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -8,7 +8,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/rpc/services"
 | 
			
		||||
	rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
 | 
			
		||||
	"github.com/iwind/TeaGo/maps"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"reflect"
 | 
			
		||||
@@ -152,7 +152,7 @@ func (this *RestServer) handle(writer http.ResponseWriter, req *http.Request) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// TODO 需要防止BODY过大攻击
 | 
			
		||||
	body, err := ioutil.ReadAll(req.Body)
 | 
			
		||||
	body, err := io.ReadAll(req.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		writer.WriteHeader(http.StatusBadRequest)
 | 
			
		||||
		_, _ = writer.Write([]byte(err.Error()))
 | 
			
		||||
 
 | 
			
		||||
@@ -11,7 +11,6 @@ import (
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	"gopkg.in/yaml.v3"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
@@ -75,7 +74,7 @@ func (this *Setup) Run() error {
 | 
			
		||||
 | 
			
		||||
	// 执行SQL
 | 
			
		||||
	config := &dbs.Config{}
 | 
			
		||||
	configData, err := ioutil.ReadFile(Tea.ConfigFile("db.yaml"))
 | 
			
		||||
	configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ import (
 | 
			
		||||
	"github.com/iwind/TeaGo/types"
 | 
			
		||||
	stringutil "github.com/iwind/TeaGo/utils/string"
 | 
			
		||||
	"gopkg.in/yaml.v3"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -35,7 +35,7 @@ func NewSQLExecutor(dbConfig *dbs.DBConfig) *SQLExecutor {
 | 
			
		||||
func NewSQLExecutorFromCmd() (*SQLExecutor, error) {
 | 
			
		||||
	// 执行SQL
 | 
			
		||||
	config := &dbs.Config{}
 | 
			
		||||
	configData, err := ioutil.ReadFile(Tea.ConfigFile("db.yaml"))
 | 
			
		||||
	configData, err := os.ReadFile(Tea.ConfigFile("db.yaml"))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -15,7 +15,7 @@ import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAPI/internal/utils"
 | 
			
		||||
	"github.com/iwind/TeaGo/dbs"
 | 
			
		||||
	"golang.org/x/crypto/ocsp"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
@@ -147,7 +147,7 @@ func (this *SSLCertUpdateOCSPTask) UpdateCertOCSP(certOne *models.SSLCert) (ocsp
 | 
			
		||||
		_ = issuerResp.Body.Close()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	issuerData, err := ioutil.ReadAll(issuerResp.Body)
 | 
			
		||||
	issuerData, err := io.ReadAll(issuerResp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		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()
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	respData, err := ioutil.ReadAll(ocspResp.Body)
 | 
			
		||||
	respData, err := io.ReadAll(ocspResp.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, 0, errors.New("read ocsp failed: '" + ocspServerURL + "': " + err.Error())
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,7 +2,7 @@ package utils
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"crypto/tls"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"io"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/http/httputil"
 | 
			
		||||
	"sync"
 | 
			
		||||
@@ -19,7 +19,7 @@ func DumpResponse(resp *http.Response) (header []byte, body []byte, err error) {
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	body, err = ioutil.ReadAll(resp.Body)
 | 
			
		||||
	body, err = io.ReadAll(resp.Body)
 | 
			
		||||
	return
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -7,7 +7,6 @@ import (
 | 
			
		||||
	teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/files"
 | 
			
		||||
	"io/ioutil"
 | 
			
		||||
	"os"
 | 
			
		||||
	"os/exec"
 | 
			
		||||
	"regexp"
 | 
			
		||||
@@ -83,13 +82,13 @@ func (this *ServiceManager) installInitService(exePath string, args []string) er
 | 
			
		||||
		return errors.New("'scripts/" + shortName + "' file not exists")
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	data, err := ioutil.ReadFile(scriptFile)
 | 
			
		||||
	data, err := os.ReadFile(scriptFile)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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 {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
@@ -137,7 +136,7 @@ ExecReload=` + exePath + ` reload
 | 
			
		||||
WantedBy=multi-user.target`
 | 
			
		||||
 | 
			
		||||
	// write file
 | 
			
		||||
	err := ioutil.WriteFile(systemdServiceFile, []byte(desc), 0777)
 | 
			
		||||
	err := os.WriteFile(systemdServiceFile, []byte(desc), 0777)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user