尝试优化代码

This commit is contained in:
wyx2685 2024-01-27 01:55:43 +09:00
parent 91e78fbc20
commit f92c5b37d5
No known key found for this signature in database
GPG Key ID: 8827A30FF1DB1379
4 changed files with 44 additions and 50 deletions

View File

@ -2,12 +2,12 @@ package conf
import ( import (
"fmt" "fmt"
"github.com/fsnotify/fsnotify"
"log" "log"
"path"
"path/filepath" "path/filepath"
"strings" "strings"
"time" "time"
"github.com/fsnotify/fsnotify"
) )
func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func()) error { func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func()) error {
@ -34,7 +34,7 @@ func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func())
case filepath.Base(xDnsPath), filepath.Base(sDnsPath): case filepath.Base(xDnsPath), filepath.Base(sDnsPath):
log.Println("DNS file changed, reloading...") log.Println("DNS file changed, reloading...")
default: default:
log.Println("config dir changed, reloading...") log.Println("config file changed, reloading...")
} }
*p = *New() *p = *New()
err := p.LoadFromPath(filePath) err := p.LoadFromPath(filePath)
@ -51,18 +51,18 @@ func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func())
} }
} }
}() }()
err = watcher.Add(path.Dir(filePath)) err = watcher.Add(filePath)
if err != nil { if err != nil {
return fmt.Errorf("watch file error: %s", err) return fmt.Errorf("watch file error: %s", err)
} }
if xDnsPath != "" { if xDnsPath != "" {
err = watcher.Add(path.Dir(xDnsPath)) err = watcher.Add(xDnsPath)
if err != nil { if err != nil {
return fmt.Errorf("watch dns file error: %s", err) return fmt.Errorf("watch dns file error: %s", err)
} }
} }
if sDnsPath != "" { if sDnsPath != "" {
err = watcher.Add(path.Dir(sDnsPath)) err = watcher.Add(sDnsPath)
if err != nil { if err != nil {
return fmt.Errorf("watch dns file error: %s", err) return fmt.Errorf("watch dns file error: %s", err)
} }

View File

@ -58,22 +58,24 @@ func parseConnectionConfig(c *conf.XrayConnectionConfig) (policy *coreConf.Polic
func getCore(c *conf.XrayConfig) *core.Instance { func getCore(c *conf.XrayConfig) *core.Instance {
os.Setenv("XRAY_LOCATION_ASSET", c.AssetPath) os.Setenv("XRAY_LOCATION_ASSET", c.AssetPath)
// Log Config // Log Config
coreLogConfig := &coreConf.LogConfig{} coreLogConfig := &coreConf.LogConfig{
coreLogConfig.LogLevel = c.LogConfig.Level LogLevel: c.LogConfig.Level,
coreLogConfig.AccessLog = c.LogConfig.AccessPath AccessLog: c.LogConfig.AccessPath,
coreLogConfig.ErrorLog = c.LogConfig.ErrorPath ErrorLog: c.LogConfig.ErrorPath,
}
// DNS config // DNS config
coreDnsConfig := &coreConf.DNSConfig{} coreDnsConfig := &coreConf.DNSConfig{}
os.Setenv("XRAY_DNS_PATH", "") os.Setenv("XRAY_DNS_PATH", "")
if c.DnsConfigPath != "" { if c.DnsConfigPath != "" {
f, err := os.OpenFile(c.DnsConfigPath, os.O_RDWR|os.O_CREATE, 0755) data, err := os.ReadFile(c.DnsConfigPath)
if err != nil { if err != nil {
log.Error("Failed to open or create xray dns config file: %v", err) log.Error(fmt.Sprintf("Failed to read xray dns config file: %v", err))
}
defer f.Close()
if err := json.NewDecoder(f).Decode(coreDnsConfig); err != nil {
log.Error(fmt.Sprintf("Failed to unmarshal xray dns config from file '%v': %v. Using default DNS options.", f.Name(), err))
coreDnsConfig = &coreConf.DNSConfig{} coreDnsConfig = &coreConf.DNSConfig{}
} else {
if err := json.Unmarshal(data, coreDnsConfig); err != nil {
log.Error(fmt.Sprintf("Failed to unmarshal xray dns config: %v. Using default DNS options.", err))
coreDnsConfig = &coreConf.DNSConfig{}
}
} }
os.Setenv("XRAY_DNS_PATH", c.DnsConfigPath) os.Setenv("XRAY_DNS_PATH", c.DnsConfigPath)
} }
@ -84,25 +86,27 @@ func getCore(c *conf.XrayConfig) *core.Instance {
// Routing config // Routing config
coreRouterConfig := &coreConf.RouterConfig{} coreRouterConfig := &coreConf.RouterConfig{}
if c.RouteConfigPath != "" { if c.RouteConfigPath != "" {
if f, err := os.Open(c.RouteConfigPath); err != nil { data, err := os.ReadFile(c.RouteConfigPath)
if err != nil {
log.WithField("err", err).Panic("Failed to read Routing config file") log.WithField("err", err).Panic("Failed to read Routing config file")
} else { } else {
if err = json.NewDecoder(f).Decode(coreRouterConfig); err != nil { if err = json.Unmarshal(data, coreRouterConfig); err != nil {
log.WithField("err", err).Panic("Failed to unmarshal Routing config") log.WithField("err", err).Panic("Failed to unmarshal Routing config")
} }
} }
} }
routeConfig, err := coreRouterConfig.Build() routeConfig, err := coreRouterConfig.Build()
if err != nil { if err != nil {
log.WithField("err", err).Panic("Failed to understand Routing config Please check: https://xtls.github.io/config/routing.html") log.WithField("err", err).Panic("Failed to understand Routing config. Please check: https://xtls.github.io/config/routing.html for help")
} }
// Custom Inbound config // Custom Inbound config
var coreCustomInboundConfig []coreConf.InboundDetourConfig var coreCustomInboundConfig []coreConf.InboundDetourConfig
if c.InboundConfigPath != "" { if c.InboundConfigPath != "" {
if f, err := os.Open(c.InboundConfigPath); err != nil { data, err := os.ReadFile(c.InboundConfigPath)
if err != nil {
log.WithField("err", err).Panic("Failed to read Custom Inbound config file") log.WithField("err", err).Panic("Failed to read Custom Inbound config file")
} else { } else {
if err = json.NewDecoder(f).Decode(&coreCustomInboundConfig); err != nil { if err = json.Unmarshal(data, &coreCustomInboundConfig); err != nil {
log.WithField("err", err).Panic("Failed to unmarshal Custom Inbound config") log.WithField("err", err).Panic("Failed to unmarshal Custom Inbound config")
} }
} }
@ -111,17 +115,18 @@ func getCore(c *conf.XrayConfig) *core.Instance {
for _, config := range coreCustomInboundConfig { for _, config := range coreCustomInboundConfig {
oc, err := config.Build() oc, err := config.Build()
if err != nil { if err != nil {
log.WithField("err", err).Panic("Failed to understand Inbound config, Please check: https://xtls.github.io/config/inbound.html for help") log.WithField("err", err).Panic("Failed to understand Inbound config. Please check: https://xtls.github.io/config/inbound.html for help")
} }
inBoundConfig = append(inBoundConfig, oc) inBoundConfig = append(inBoundConfig, oc)
} }
// Custom Outbound config // Custom Outbound config
var coreCustomOutboundConfig []coreConf.OutboundDetourConfig var coreCustomOutboundConfig []coreConf.OutboundDetourConfig
if c.OutboundConfigPath != "" { if c.OutboundConfigPath != "" {
if f, err := os.Open(c.OutboundConfigPath); err != nil { data, err := os.ReadFile(c.OutboundConfigPath)
if err != nil {
log.WithField("err", err).Panic("Failed to read Custom Outbound config file") log.WithField("err", err).Panic("Failed to read Custom Outbound config file")
} else { } else {
if err = json.NewDecoder(f).Decode(&coreCustomOutboundConfig); err != nil { if err = json.Unmarshal(data, &coreCustomOutboundConfig); err != nil {
log.WithField("err", err).Panic("Failed to unmarshal Custom Outbound config") log.WithField("err", err).Panic("Failed to unmarshal Custom Outbound config")
} }
} }

View File

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

View File

@ -82,29 +82,24 @@ func (c *Controller) reportUserTrafficTask() (err error) {
} }
func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo) { func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo) {
tmp := map[string]struct{}{} oldMap := make(map[string]int)
tmp2 := map[string]struct{}{} for i, user := range old {
for i := range old { key := user.Uuid + strconv.Itoa(user.SpeedLimit)
tmp[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{} oldMap[key] = i
} }
l := len(tmp)
for i := range new { for _, user := range new {
e := new[i].Uuid + strconv.Itoa(new[i].SpeedLimit) key := user.Uuid + strconv.Itoa(user.SpeedLimit)
tmp[e] = struct{}{} if _, exists := oldMap[key]; !exists {
tmp2[e] = struct{}{} added = append(added, user)
if l != len(tmp) { } else {
added = append(added, new[i]) delete(oldMap, key)
l++
} }
} }
tmp = nil
l = len(tmp2) for _, index := range oldMap {
for i := range old { deleted = append(deleted, old[index])
tmp2[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{}
if l != len(tmp2) {
deleted = append(deleted, old[i])
l++
}
} }
return deleted, added return deleted, added
} }