Compare commits

..

4 Commits

Author SHA1 Message Date
wyx2685
77ec03016c 尝试解决内存泄漏问题 2024-01-28 01:04:47 +09:00
wyx2685
1d4945af8d 更新内核,尝试优化代码 2024-01-27 13:19:55 +09:00
wyx2685
f92c5b37d5 尝试优化代码 2024-01-27 01:55:43 +09:00
wyx2685
91e78fbc20 尝试优化代码 2024-01-25 23:06:37 +09:00
13 changed files with 136 additions and 88 deletions

4
.gitignore vendored
View File

@@ -12,4 +12,6 @@ app/legocmd/.lego/
example/.lego
example/cert
./vscode
.idea/*
output/*
.idea/*
newV2bX.sh

View File

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

View File

@@ -29,18 +29,29 @@ func (c *Client) GetUserList() (UserList []UserInfo, err error) {
const path = "/api/v1/server/UniProxy/user"
r, err := c.client.R().
SetHeader("If-None-Match", c.userEtag).
ForceContentType("application/json").
Get(path)
err = c.checkResponse(r, path, err)
if err != nil {
if err = c.checkResponse(r, path, err); err != nil {
return nil, err
}
if r.StatusCode() == 304 {
return nil, nil
if r != nil {
defer func() {
if r.RawBody() != nil {
r.RawBody().Close()
}
}()
if r.StatusCode() == 304 {
return nil, nil
}
} else {
return nil, fmt.Errorf("received nil response")
}
var userList *UserListBody
err = json.Unmarshal(r.Body(), &userList)
if err != nil {
return nil, fmt.Errorf("read body error: %s", err)
}
if err := json.Unmarshal(r.Body(), &userList); err != nil {
return nil, fmt.Errorf("unmarshal userlist error: %s", err)
}
c.userEtag = r.Header().Get("ETag")

View File

@@ -2,9 +2,11 @@ package conf
import (
"fmt"
"github.com/InazumaV/V2bX/common/json5"
"io"
"os"
"github.com/InazumaV/V2bX/common/json5"
"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)
}
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

@@ -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 {
@@ -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)
@@ -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)
}

View File

@@ -3,6 +3,7 @@ package sing
import (
"context"
"fmt"
"io"
"os"
"github.com/sagernet/sing-box/log"
@@ -38,14 +39,13 @@ func init() {
func New(c *conf.CoreConfig) (vCore.Core, error) {
options := option.Options{}
if len(c.SingConfig.OriginalPath) != 0 {
f, err := os.Open(c.SingConfig.OriginalPath)
data, err := os.ReadFile(c.SingConfig.OriginalPath)
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.NewDecoder(f).Decode(&options)
err = json.Unmarshal(data, &options)
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{
@@ -68,11 +68,19 @@ func New(c *conf.CoreConfig) (vCore.Core, error) {
return nil, fmt.Errorf("failed to open or create sing dns config file: %s", err)
}
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 unmarshal sing dns config from file '%v': %v. Using default DNS options",
"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(
"Failed to unmarshal sing dns config from file '%v': %v. Using default DNS options",
f.Name(), err))
options.DNS = &option.DNSOptions{}
}
}
os.Setenv("SING_DNS_PATH", c.SingConfig.DnsConfigPath)
}

View File

@@ -2,14 +2,15 @@ package xray
import (
"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"
"os"
"strconv"
"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) {
@@ -62,7 +63,7 @@ func saveDnsConfig(dns []byte, dnsPath string) (err error) {
}
if !bytes.Equal(currentData, dns) {
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")
}
_, err := coreDnsConfig.Build()

View File

@@ -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)
}
@@ -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")
}
}
@@ -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")
}
}

10
go.mod
View File

@@ -14,7 +14,7 @@ require (
github.com/sagernet/sing-box v1.8.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
github.com/xtls/xray-core v1.8.8-0.20240120214034-53de58fad3a0
github.com/xtls/xray-core v1.8.8-0.20240125151013-25c531c6c358
golang.org/x/crypto v0.18.0
golang.org/x/sys v0.16.0
google.golang.org/protobuf v1.32.0
@@ -154,7 +154,7 @@ require (
github.com/sagernet/cloudflare-tls v0.0.0-20231208171750-a4483c1b7cd1 // indirect
github.com/sagernet/gvisor v0.0.0-20231209105102-8d27a30e436e // indirect
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 // indirect
github.com/sagernet/quic-go v0.40.1-beta.2 // indirect
github.com/sagernet/quic-go v0.40.1 // indirect
github.com/sagernet/sing-dns v0.1.12 // indirect
github.com/sagernet/sing-mux v0.2.0 // indirect
github.com/sagernet/sing-quic v0.1.7 // indirect
@@ -201,7 +201,7 @@ require (
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.13.0 // indirect
golang.org/x/oauth2 v0.14.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
golang.org/x/tools v0.17.0 // indirect
@@ -212,7 +212,7 @@ require (
google.golang.org/genproto v0.0.0-20231212172506-995d672761c0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20231211222908-989df2bf70f3 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240102182953-50ed04b92917 // indirect
google.golang.org/grpc v1.60.1 // indirect
google.golang.org/grpc v1.61.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/ns1/ns1-go.v2 v2.7.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
@@ -221,4 +221,4 @@ require (
lukechampine.com/blake3 v1.2.1 // indirect
)
replace github.com/sagernet/sing-box v1.8.2 => github.com/wyx2685/sing-box_mod v0.0.0-20240122043647-0854bf9053ab
replace github.com/sagernet/sing-box v1.8.2 => github.com/wyx2685/sing-box_mod v0.0.0

20
go.sum
View File

@@ -640,8 +640,8 @@ github.com/sagernet/gvisor v0.0.0-20231209105102-8d27a30e436e h1:DOkjByVeAR56dks
github.com/sagernet/gvisor v0.0.0-20231209105102-8d27a30e436e/go.mod h1:fLxq/gtp0qzkaEwywlRRiGmjOK5ES/xUzyIKIFP2Asw=
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97 h1:iL5gZI3uFp0X6EslacyapiRz7LLSJyr4RajF/BhMVyE=
github.com/sagernet/netlink v0.0.0-20220905062125-8043b4a9aa97/go.mod h1:xLnfdiJbSp8rNqYEdIW/6eDO4mVoogml14Bh2hSiFpM=
github.com/sagernet/quic-go v0.40.1-beta.2 h1:USRwm36XuAFdcrmv4vDRD+YUOO08DfvLNruXThrVHZU=
github.com/sagernet/quic-go v0.40.1-beta.2/go.mod h1:CcKTpzTAISxrM4PA5M20/wYuz9Tj6Tx4DwGbNl9UQrU=
github.com/sagernet/quic-go v0.40.1 h1:qLeTIJR0d0JWRmDWo346nLsVN6EWihd1kalJYPEd0TM=
github.com/sagernet/quic-go v0.40.1/go.mod h1:CcKTpzTAISxrM4PA5M20/wYuz9Tj6Tx4DwGbNl9UQrU=
github.com/sagernet/sing v0.2.18/go.mod h1:OL6k2F0vHmEzXz2KW19qQzu172FDgSbUSODylighuVo=
github.com/sagernet/sing v0.3.1-0.20240105061852-782bc05c5573 h1:1wGN3eNanp8r+Y3bNBys3ZnAVF5gdtDoDwtosMZEbgA=
github.com/sagernet/sing v0.3.1-0.20240105061852-782bc05c5573/go.mod h1:9pfuAH6mZfgnz/YjP6xu5sxx882rfyjpcrTdUpd6w3g=
@@ -785,16 +785,16 @@ github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1Y
github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
github.com/vultr/govultr/v2 v2.17.2 h1:gej/rwr91Puc/tgh+j33p/BLR16UrIPnSr+AIwYWZQs=
github.com/vultr/govultr/v2 v2.17.2/go.mod h1:ZFOKGWmgjytfyjeyAdhQlSWwTjh2ig+X49cAp50dzXI=
github.com/wyx2685/sing-box_mod v0.0.0-20240122043647-0854bf9053ab h1:8nEgSATWz3rd8Slefbcby6jTsmPmY8r6fhbETjbyYZQ=
github.com/wyx2685/sing-box_mod v0.0.0-20240122043647-0854bf9053ab/go.mod h1:YZrQibfEgKg7P4qQbcc7ZMFCFJk3WyExsh1QVN+EV04=
github.com/wyx2685/sing-box_mod v0.0.0 h1:IuJekC27gcCx8NkemLfMdcI6LK64tDh6OwLaOHQX2Ng=
github.com/wyx2685/sing-box_mod v0.0.0/go.mod h1:aSBnZ9maXq2QvQ1Ij5+5Hhn0/RBO0zYkjtWfr9GFbLo=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xtls/reality v0.0.0-20231112171332-de1173cf2b19 h1:capMfFYRgH9BCLd6A3Er/cH3A9Nz3CU2KwxwOQZIePI=
github.com/xtls/reality v0.0.0-20231112171332-de1173cf2b19/go.mod h1:dm4y/1QwzjGaK17ofi0Vs6NpKAHegZky8qk6J2JJZAE=
github.com/xtls/xray-core v1.8.8-0.20240120214034-53de58fad3a0 h1:IfUeBSlGlxy9BachfXhZu1nwNwrwLuuE/ZXjzUB0KeU=
github.com/xtls/xray-core v1.8.8-0.20240120214034-53de58fad3a0/go.mod h1:gAw8EAQONCedbAfKz3j9kaaCIl1RhE33vIOY2ePecg0=
github.com/xtls/xray-core v1.8.8-0.20240125151013-25c531c6c358 h1:DU3gK47iZoVIucsfhta5wsrD0QZUe/HDRD0n7OeozMA=
github.com/xtls/xray-core v1.8.8-0.20240125151013-25c531c6c358/go.mod h1:sSYVaxAyZEHQ/21IQQgsXnupu9tw4Ye0b8BmvQAG5O4=
github.com/yandex-cloud/go-genproto v0.0.0-20220805142335-27b56ddae16f h1:cG+ehPRJSlqljSufLf1KXeXpUd1dLNjnzA18mZcB/O0=
github.com/yandex-cloud/go-genproto v0.0.0-20220805142335-27b56ddae16f/go.mod h1:HEUYX/p8966tMUHHT+TsS0hF/Ca/NYwqprC5WXSDMfE=
github.com/yandex-cloud/go-sdk v0.0.0-20220805164847-cf028e604997 h1:2wzke3JH7OtN20WsNDZx2VH/TCmsbqtDEbXzjF+i05E=
@@ -931,8 +931,8 @@ golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAG
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.13.0 h1:jDDenyj+WgFtmV3zYVoi8aE2BwtXFLWOA67ZfNWftiY=
golang.org/x/oauth2 v0.13.0/go.mod h1:/JMhi4ZRXAf4HG9LiNmxvk+45+96RUlVThiH8FzNBn0=
golang.org/x/oauth2 v0.14.0 h1:P0Vrf/2538nmC0H+pEQ3MNFRRnVR7RlqyVw+bvm26z0=
golang.org/x/oauth2 v0.14.0/go.mod h1:lAtNWgaWfL4cm7j2OV8TxGi9Qb7ECORx8DktCY74OwM=
golang.org/x/perf v0.0.0-20180704124530-6e6d33e29852/go.mod h1:JLpeXjPJfIyPr5TlbXLkXWLhP8nz10XfvxElABhCtcw=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
@@ -1128,8 +1128,8 @@ google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv
google.golang.org/grpc v1.36.0/go.mod h1:qjiiYl8FncCW8feJPdyg3v6XW24KsRHe+dy9BAGRRjU=
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
google.golang.org/grpc v1.41.0/go.mod h1:U3l9uK9J0sini8mHphKoXyaqDA/8VyGnDee1zzIUK6k=
google.golang.org/grpc v1.60.1 h1:26+wFr+cNqSGFcOXcabYC0lUVJVRa2Sb2ortSK7VrEU=
google.golang.org/grpc v1.60.1/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM=
google.golang.org/grpc v1.61.0 h1:TOvOcuXn30kRao+gfcvsebNEa5iZIiLkisYEkf7R7o0=
google.golang.org/grpc v1.61.0/go.mod h1:VUbo7IFqmF1QtCAstipjG0GIoq49KvMe9+h1jFLBNJs=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=

10
main.go
View File

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

View File

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

View File

@@ -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
}