Skip to content

Commit

Permalink
尝试优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
wyx2685 committed Jan 26, 2024
1 parent 91e78fb commit f92c5b3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 50 deletions.
12 changes: 6 additions & 6 deletions conf/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package conf

import (
"fmt"
"github.com/fsnotify/fsnotify"
"log"
"path"
"path/filepath"
"strings"
"time"

"github.com/fsnotify/fsnotify"
)

func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func()) error {
Expand All @@ -34,7 +34,7 @@ func (p *Conf) Watch(filePath, xDnsPath string, sDnsPath string, reload func())
case filepath.Base(xDnsPath), filepath.Base(sDnsPath):
log.Println("DNS file changed, reloading...")
default:
log.Println("config dir changed, reloading...")
log.Println("config file changed, reloading...")
}
*p = *New()
err := p.LoadFromPath(filePath)
Expand All @@ -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 {
return fmt.Errorf("watch file error: %s", err)
}
if xDnsPath != "" {
err = watcher.Add(path.Dir(xDnsPath))
err = watcher.Add(xDnsPath)
if err != nil {
return fmt.Errorf("watch dns file error: %s", err)
}
}
if sDnsPath != "" {
err = watcher.Add(path.Dir(sDnsPath))
err = watcher.Add(sDnsPath)
if err != nil {
return fmt.Errorf("watch dns file error: %s", err)
}
Expand Down
41 changes: 23 additions & 18 deletions core/xray/xray.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,24 @@ func parseConnectionConfig(c *conf.XrayConnectionConfig) (policy *coreConf.Polic
func getCore(c *conf.XrayConfig) *core.Instance {
os.Setenv("XRAY_LOCATION_ASSET", c.AssetPath)
// Log Config
coreLogConfig := &coreConf.LogConfig{}
coreLogConfig.LogLevel = c.LogConfig.Level
coreLogConfig.AccessLog = c.LogConfig.AccessPath
coreLogConfig.ErrorLog = c.LogConfig.ErrorPath
coreLogConfig := &coreConf.LogConfig{
LogLevel: c.LogConfig.Level,
AccessLog: c.LogConfig.AccessPath,
ErrorLog: c.LogConfig.ErrorPath,
}
// DNS config
coreDnsConfig := &coreConf.DNSConfig{}
os.Setenv("XRAY_DNS_PATH", "")
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 {
log.Error("Failed to open or create 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))
log.Error(fmt.Sprintf("Failed to read xray dns config file: %v", err))
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)
}
Expand All @@ -84,25 +86,27 @@ func getCore(c *conf.XrayConfig) *core.Instance {
// Routing config
coreRouterConfig := &coreConf.RouterConfig{}
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")
} 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")
}
}
}
routeConfig, err := coreRouterConfig.Build()
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
var coreCustomInboundConfig []coreConf.InboundDetourConfig
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")
} 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")
}
}
Expand All @@ -111,17 +115,18 @@ func getCore(c *conf.XrayConfig) *core.Instance {
for _, config := range coreCustomInboundConfig {
oc, err := config.Build()
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)
}
// Custom Outbound config
var coreCustomOutboundConfig []coreConf.OutboundDetourConfig
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")
} 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")
}
}
Expand Down
6 changes: 0 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package main

import (
_ "net/http/pprof"

"github.com/InazumaV/V2bX/cmd"
)

func main() {
//内存泄漏排查
// go func() {
// http.ListenAndServe("127.0.0.1:6060", nil)
// }()
cmd.Run()
}
35 changes: 15 additions & 20 deletions node/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,24 @@ func (c *Controller) reportUserTrafficTask() (err error) {
}

func compareUserList(old, new []panel.UserInfo) (deleted, added []panel.UserInfo) {
tmp := map[string]struct{}{}
tmp2 := map[string]struct{}{}
for i := range old {
tmp[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{}
oldMap := make(map[string]int)
for i, user := range old {
key := user.Uuid + strconv.Itoa(user.SpeedLimit)
oldMap[key] = i
}
l := len(tmp)
for i := range new {
e := new[i].Uuid + strconv.Itoa(new[i].SpeedLimit)
tmp[e] = struct{}{}
tmp2[e] = struct{}{}
if l != len(tmp) {
added = append(added, new[i])
l++

for _, user := range new {
key := user.Uuid + strconv.Itoa(user.SpeedLimit)
if _, exists := oldMap[key]; !exists {
added = append(added, user)
} else {
delete(oldMap, key)
}
}
tmp = nil
l = len(tmp2)
for i := range old {
tmp2[old[i].Uuid+strconv.Itoa(old[i].SpeedLimit)] = struct{}{}
if l != len(tmp2) {
deleted = append(deleted, old[i])
l++
}

for _, index := range oldMap {
deleted = append(deleted, old[index])
}

return deleted, added
}

0 comments on commit f92c5b3

Please sign in to comment.