尝试优化代码

This commit is contained in:
wyx2685 2024-01-25 23:06:37 +09:00
parent e292b3b0e7
commit 91e78fbc20
No known key found for this signature in database
GPG Key ID: 8827A30FF1DB1379
7 changed files with 66 additions and 24 deletions

View File

@ -134,9 +134,19 @@ func (c *Client) GetNodeInfo() (node *NodeInfo, err error) {
if err = c.checkResponse(r, path, err); err != nil { if err = c.checkResponse(r, path, err); err != nil {
return nil, err return nil, err
} }
if r != nil {
defer func() {
if r.RawBody() != nil {
r.RawBody().Close()
}
}()
if r.StatusCode() == 304 { if r.StatusCode() == 304 {
return nil, nil return nil, nil
} }
} else {
return nil, fmt.Errorf("received nil response")
}
node = &NodeInfo{ node = &NodeInfo{
Id: c.NodeId, Id: c.NodeId,
Type: c.NodeType, Type: c.NodeType,

View File

@ -30,14 +30,22 @@ func (c *Client) GetUserList() (UserList []UserInfo, err error) {
r, err := c.client.R(). r, err := c.client.R().
SetHeader("If-None-Match", c.userEtag). SetHeader("If-None-Match", c.userEtag).
Get(path) Get(path)
err = c.checkResponse(r, path, err) if err = c.checkResponse(r, path, err); err != nil {
if err != nil {
return nil, err return nil, err
} }
if r != nil {
defer func() {
if r.RawBody() != nil {
r.RawBody().Close()
}
}()
if r.StatusCode() == 304 { if r.StatusCode() == 304 {
return nil, nil return nil, nil
} }
} else {
return nil, fmt.Errorf("received nil response")
}
var userList *UserListBody var userList *UserListBody
err = json.Unmarshal(r.Body(), &userList) err = json.Unmarshal(r.Body(), &userList)
if err != nil { if err != nil {

View File

@ -2,9 +2,11 @@ package conf
import ( import (
"fmt" "fmt"
"github.com/InazumaV/V2bX/common/json5" "io"
"os" "os"
"github.com/InazumaV/V2bX/common/json5"
"github.com/goccy/go-json" "github.com/goccy/go-json"
) )
@ -29,5 +31,17 @@ func (p *Conf) LoadFromPath(filePath string) error {
return fmt.Errorf("open config file error: %s", err) return fmt.Errorf("open config file error: %s", err)
} }
defer f.Close() defer f.Close()
return json.NewDecoder(json5.NewTrimNodeReader(f)).Decode(p)
reader := json5.NewTrimNodeReader(f)
data, err := io.ReadAll(reader)
if err != nil {
return fmt.Errorf("read config file error: %s", err)
}
err = json.Unmarshal(data, p)
if err != nil {
return fmt.Errorf("unmarshal config error: %s", err)
}
return nil
} }

View File

@ -3,6 +3,7 @@ package sing
import ( import (
"context" "context"
"fmt" "fmt"
"io"
"os" "os"
"github.com/sagernet/sing-box/log" "github.com/sagernet/sing-box/log"
@ -38,14 +39,13 @@ func init() {
func New(c *conf.CoreConfig) (vCore.Core, error) { func New(c *conf.CoreConfig) (vCore.Core, error) {
options := option.Options{} options := option.Options{}
if len(c.SingConfig.OriginalPath) != 0 { if len(c.SingConfig.OriginalPath) != 0 {
f, err := os.Open(c.SingConfig.OriginalPath) data, err := os.ReadFile(c.SingConfig.OriginalPath)
if err != nil { if err != nil {
return nil, fmt.Errorf("open original config error: %s", err) return nil, fmt.Errorf("read original config error: %s", err)
} }
defer f.Close() err = json.Unmarshal(data, &options)
err = json.NewDecoder(f).Decode(&options)
if err != nil { if err != nil {
return nil, fmt.Errorf("decode original config error: %s", err) return nil, fmt.Errorf("unmarshal original config error: %s", err)
} }
} }
options.Log = &option.LogOptions{ options.Log = &option.LogOptions{
@ -68,12 +68,20 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
return nil, fmt.Errorf("failed to open or create sing dns config file: %s", err) return nil, fmt.Errorf("failed to open or create sing dns config file: %s", err)
} }
defer f.Close() defer f.Close()
if err := json.NewDecoder(f).Decode(options.DNS); err != nil { data, err := io.ReadAll(f)
if err != nil {
log.Warn(fmt.Sprintf(
"Failed to read sing dns config from file '%v': %v. Using default DNS options",
f.Name(), err))
options.DNS = &option.DNSOptions{}
} else {
if err := json.Unmarshal(data, options.DNS); err != nil {
log.Warn(fmt.Sprintf( log.Warn(fmt.Sprintf(
"Failed to unmarshal sing dns config from file '%v': %v. Using default DNS options", "Failed to unmarshal sing dns config from file '%v': %v. Using default DNS options",
f.Name(), err)) f.Name(), err))
options.DNS = &option.DNSOptions{} options.DNS = &option.DNSOptions{}
} }
}
os.Setenv("SING_DNS_PATH", c.SingConfig.DnsConfigPath) os.Setenv("SING_DNS_PATH", c.SingConfig.DnsConfigPath)
} }
b, err := box.New(box.Options{ b, err := box.New(box.Options{

View File

@ -2,14 +2,15 @@ package xray
import ( import (
"bytes" "bytes"
"github.com/InazumaV/V2bX/api/panel"
"github.com/goccy/go-json"
log "github.com/sirupsen/logrus"
coreConf "github.com/xtls/xray-core/infra/conf"
"net" "net"
"os" "os"
"strconv" "strconv"
"strings" "strings"
"github.com/InazumaV/V2bX/api/panel"
"github.com/goccy/go-json"
log "github.com/sirupsen/logrus"
coreConf "github.com/xtls/xray-core/infra/conf"
) )
func updateDNSConfig(node *panel.NodeInfo) (err error) { func updateDNSConfig(node *panel.NodeInfo) (err error) {
@ -62,7 +63,7 @@ func saveDnsConfig(dns []byte, dnsPath string) (err error) {
} }
if !bytes.Equal(currentData, dns) { if !bytes.Equal(currentData, dns) {
coreDnsConfig := &coreConf.DNSConfig{} coreDnsConfig := &coreConf.DNSConfig{}
if err = json.NewDecoder(bytes.NewReader(dns)).Decode(coreDnsConfig); err != nil { if err = json.Unmarshal(dns, coreDnsConfig); err != nil {
log.WithField("err", err).Error("Failed to unmarshal DNS config") log.WithField("err", err).Error("Failed to unmarshal DNS config")
} }
_, err := coreDnsConfig.Build() _, err := coreDnsConfig.Build()

View File

@ -1,7 +1,6 @@
package main package main
import ( import (
_ "net/http"
_ "net/http/pprof" _ "net/http/pprof"
"github.com/InazumaV/V2bX/cmd" "github.com/InazumaV/V2bX/cmd"
@ -10,7 +9,7 @@ import (
func main() { func main() {
//内存泄漏排查 //内存泄漏排查
// go func() { // go func() {
// http.ListenAndServe("0.0.0.0:6060", nil) // http.ListenAndServe("127.0.0.1:6060", nil)
// }() // }()
cmd.Run() cmd.Run()
} }

View File

@ -262,12 +262,14 @@ func (u *User) DecodePrivate(pemEncodedPriv string) (*ecdsa.PrivateKey, error) {
privateKey, err := x509.ParseECPrivateKey(x509EncodedPriv) privateKey, err := x509.ParseECPrivateKey(x509EncodedPriv)
return privateKey, err return privateKey, err
} }
func (u *User) Load(path string) error { func (u *User) Load(path string) error {
f, err := os.Open(path) data, err := os.ReadFile(path)
if err != nil { if err != nil {
return fmt.Errorf("open file error: %s", err) return fmt.Errorf("open file error: %s", err)
} }
err = json.NewDecoder(f).Decode(u)
err = json.Unmarshal(data, u)
if err != nil { if err != nil {
return fmt.Errorf("unmarshal json error: %s", err) return fmt.Errorf("unmarshal json error: %s", err)
} }