diff --git a/cmd/cmd.go b/cmd/cmd.go index 8bf7d47..26f9130 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -1,7 +1,7 @@ package cmd import ( - "log" + log "github.com/sirupsen/logrus" _ "github.com/Yuzuki616/V2bX/core/imports" "github.com/spf13/cobra" @@ -14,6 +14,6 @@ var command = &cobra.Command{ func Run() { err := command.Execute() if err != nil { - log.Println("execute failed, error:", err) + log.WithField("err", err).Error("Execute command failed") } } diff --git a/cmd/server.go b/cmd/server.go index 1542389..34ceeae 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -1,7 +1,7 @@ package cmd import ( - "log" + log "github.com/sirupsen/logrus" "os" "os/signal" "runtime" @@ -42,23 +42,26 @@ func serverHandle(_ *cobra.Command, _ []string) { c := conf.New() err := c.LoadFromPath(config) if err != nil { - log.Fatalf("can't unmarshal config file: %s \n", err) + log.WithField("err", err).Error("Load config file failed") + return } limiter.Init() - log.Println("Start V2bX...") + log.Info("Start V2bX...") vc, err := vCore.NewCore(&c.CoreConfig) if err != nil { - log.Fatalf("New core error: %s", err) + log.WithField("err", err).Error("new core failed") + return } err = vc.Start() if err != nil { - log.Fatalf("Start core error: %s", err) + log.WithField("err", err).Error("Start core failed") + return } defer vc.Close() nodes := node.New() err = nodes.Start(c.NodesConfig, vc) if err != nil { - log.Fatalf("Run nodes error: %s", err) + log.WithField("err", err).Error("Run nodes failed") return } if watch { @@ -66,24 +69,29 @@ func serverHandle(_ *cobra.Command, _ []string) { nodes.Close() err = vc.Close() if err != nil { - log.Fatalf("Failed to restart xray-core: %s", err) + log.WithField("err", err).Error("Restart node failed") + return } vc, err = vCore.NewCore(&c.CoreConfig) if err != nil { - log.Fatalf("New core error: %s", err) + log.WithField("err", err).Error("New core failed") + return } err = vc.Start() if err != nil { - log.Fatalf("Start core error: %s", err) + log.WithField("err", err).Error("Start core failed") + return } err = nodes.Start(c.NodesConfig, vc) if err != nil { - log.Fatalf("Run nodes error: %s", err) + log.WithField("err", err).Error("Run nodes failed") + return } runtime.GC() }) if err != nil { - log.Fatalf("Watch config file error: %s", err) + log.WithField("err", err).Error("start watch failed") + return } } // clear memory diff --git a/core/xray/user.go b/core/xray/user.go index b5cc330..b2a51e4 100644 --- a/core/xray/user.go +++ b/core/xray/user.go @@ -73,10 +73,8 @@ func (c *Core) AddUsers(p *vCore.AddUsersParams) (added int, err error) { users := make([]*protocol.User, 0, len(p.UserInfo)) switch p.NodeInfo.Type { case "v2ray": - if p.Config.XrayOptions.EnableVless || p.NodeInfo.ExtraConfig.EnableVless { - if p.Config.XrayOptions.VlessFlow != "" { if p.Config.XrayOptions.VlessFlow == p.NodeInfo.ExtraConfig.VlessFlow { users = builder.BuildVlessUsers(p.Tag, p.UserInfo, p.Config.XrayOptions.VlessFlow) diff --git a/core/xray/xray.go b/core/xray/xray.go index 280eb10..2520e46 100644 --- a/core/xray/xray.go +++ b/core/xray/xray.go @@ -1,7 +1,6 @@ package xray import ( - "log" "os" "sync" @@ -10,6 +9,7 @@ import ( "github.com/Yuzuki616/V2bX/core/xray/app/dispatcher" _ "github.com/Yuzuki616/V2bX/core/xray/distro/all" "github.com/goccy/go-json" + log "github.com/sirupsen/logrus" "github.com/xtls/xray-core/app/proxyman" "github.com/xtls/xray-core/app/stats" "github.com/xtls/xray-core/common/serial" @@ -63,40 +63,40 @@ func getCore(c *conf.XrayConfig) *core.Instance { coreDnsConfig := &coreConf.DNSConfig{} if c.DnsConfigPath != "" { if f, err := os.Open(c.DnsConfigPath); err != nil { - log.Panicf("Failed to read DNS config file at: %s", c.DnsConfigPath) + log.WithField("err", err).Panic("Failed to read DNS config file") } else { if err = json.NewDecoder(f).Decode(coreDnsConfig); err != nil { - log.Panicf("Failed to unmarshal DNS config: %s", c.DnsConfigPath) + log.WithField("err", err).Panic("Failed to unmarshal DNS config") } } } dnsConfig, err := coreDnsConfig.Build() if err != nil { - log.Panicf("Failed to understand DNS config, Please check: https://xtls.github.io/config/dns.html for help: %s", err) + log.WithField("err", err).Panic("Failed to understand DNS config, Please check: https://xtls.github.io/config/dns.html for help") } // Routing config coreRouterConfig := &coreConf.RouterConfig{} if c.RouteConfigPath != "" { if f, err := os.Open(c.RouteConfigPath); err != nil { - log.Panicf("Failed to read Routing config file at: %s", c.RouteConfigPath) + log.WithField("err", err).Panic("Failed to read Routing config file") } else { if err = json.NewDecoder(f).Decode(coreRouterConfig); err != nil { - log.Panicf("Failed to unmarshal Routing config: %s", c.RouteConfigPath) + log.WithField("err", err).Panic("Failed to unmarshal Routing config") } } } routeConfig, err := coreRouterConfig.Build() if err != nil { - log.Panicf("Failed to understand Routing config Please check: https://xtls.github.io/config/routing.html for help: %s", err) + log.WithField("err", err).Panic("Failed to understand Routing config Please check: https://xtls.github.io/config/routing.html") } // Custom Inbound config var coreCustomInboundConfig []coreConf.InboundDetourConfig if c.InboundConfigPath != "" { if f, err := os.Open(c.InboundConfigPath); err != nil { - log.Panicf("Failed to read Custom Inbound config file at: %s", c.OutboundConfigPath) + log.WithField("err", err).Panic("Failed to read Custom Inbound config file") } else { if err = json.NewDecoder(f).Decode(&coreCustomInboundConfig); err != nil { - log.Panicf("Failed to unmarshal Custom Inbound config: %s", c.OutboundConfigPath) + log.WithField("err", err).Panic("Failed to unmarshal Custom Inbound config") } } } @@ -104,7 +104,7 @@ func getCore(c *conf.XrayConfig) *core.Instance { for _, config := range coreCustomInboundConfig { oc, err := config.Build() if err != nil { - log.Panicf("Failed to understand Inbound config, Please check: https://xtls.github.io/config/inbound.html for help: %s", err) + 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) } @@ -112,10 +112,10 @@ func getCore(c *conf.XrayConfig) *core.Instance { var coreCustomOutboundConfig []coreConf.OutboundDetourConfig if c.OutboundConfigPath != "" { if f, err := os.Open(c.OutboundConfigPath); err != nil { - log.Panicf("Failed to read Custom Outbound config file at: %s", c.OutboundConfigPath) + log.WithField("err", err).Panic("Failed to read Custom Outbound config file") } else { if err = json.NewDecoder(f).Decode(&coreCustomOutboundConfig); err != nil { - log.Panicf("Failed to unmarshal Custom Outbound config: %s", c.OutboundConfigPath) + log.WithField("err", err).Panic("Failed to unmarshal Custom Outbound config") } } } @@ -123,7 +123,7 @@ func getCore(c *conf.XrayConfig) *core.Instance { for _, config := range coreCustomOutboundConfig { oc, err := config.Build() if err != nil { - log.Panicf("Failed to understand Outbound config, Please check: https://xtls.github.io/config/outbound.html for help: %s", err) + log.WithField("err", err).Panic("Failed to understand Outbound config, Please check: https://xtls.github.io/config/outbound.html for help") } outBoundConfig = append(outBoundConfig, oc) } @@ -149,10 +149,9 @@ func getCore(c *conf.XrayConfig) *core.Instance { } server, err := core.New(config) if err != nil { - log.Panicf("failed to create instance: %s", err) + log.WithField("err", err).Panic("failed to create instance") } - log.Printf("Core Version: %s", core.Version()) - + log.Info("Xray Core Version: ", core.Version()) return server } diff --git a/limiter/clear.go b/limiter/clear.go index 47db76f..a926854 100644 --- a/limiter/clear.go +++ b/limiter/clear.go @@ -1,14 +1,16 @@ package limiter -import "log" +import log "github.com/sirupsen/logrus" func ClearOnlineIP() error { - log.Println("Limiter: Clear online ip...") + log.WithField("Type", "Limiter"). + Debug("Clear online ip...") limitLock.RLock() for _, l := range limiter { l.ConnLimiter.ClearOnlineIP() } limitLock.RUnlock() - log.Println("Limiter: Clear online ip done") + log.WithField("Type", "Limiter"). + Debug("Clear online ip done") return nil } diff --git a/limiter/limiter.go b/limiter/limiter.go index f8faf07..45c4583 100644 --- a/limiter/limiter.go +++ b/limiter/limiter.go @@ -7,8 +7,8 @@ import ( "github.com/Yuzuki616/V2bX/common/builder" "github.com/Yuzuki616/V2bX/conf" "github.com/juju/ratelimit" + log "github.com/sirupsen/logrus" "github.com/xtls/xray-core/common/task" - "log" "regexp" "sync" "time" @@ -24,7 +24,8 @@ func Init() { Execute: ClearOnlineIP, } go func() { - log.Println("Limiter: ClearOnlineIP started") + log.WithField("Type", "Limiter"). + Debug("ClearOnlineIP started") time.Sleep(time.Minute * 2) _ = c.Start() }() diff --git a/node/controller.go b/node/controller.go index 85b0b4a..725b2ad 100644 --- a/node/controller.go +++ b/node/controller.go @@ -9,7 +9,7 @@ import ( "github.com/Yuzuki616/V2bX/conf" vCore "github.com/Yuzuki616/V2bX/core" "github.com/Yuzuki616/V2bX/limiter" - "log" + log "github.com/sirupsen/logrus" ) type Controller struct { @@ -81,7 +81,7 @@ func (c *Controller) Start() error { if err != nil { return fmt.Errorf("add users error: %s", err) } - log.Printf("[%s] Added %d new users", c.Tag, added) + log.WithField("tag", c.Tag).Infof("Added %d new users", added) c.initTask() return nil } diff --git a/node/task.go b/node/task.go index 0cc05de..d87b380 100644 --- a/node/task.go +++ b/node/task.go @@ -4,7 +4,7 @@ import ( "github.com/Yuzuki616/V2bX/common/task" vCore "github.com/Yuzuki616/V2bX/core" "github.com/Yuzuki616/V2bX/limiter" - "log" + log "github.com/sirupsen/logrus" "time" ) @@ -19,10 +19,10 @@ func (c *Controller) initTask() { Interval: c.nodeInfo.PushInterval, Execute: c.reportUserTrafficTask, } - log.Printf("[%s] Start monitor node status", c.Tag) + log.WithField("tag", c.Tag).Info("Start monitor node status") // delay to start nodeInfoMonitor _ = c.nodeInfoMonitorPeriodic.Start(false) - log.Printf("[%s] Start report node status", c.Tag) + log.WithField("tag", c.Tag).Info("Start report node status") _ = c.userReportPeriodic.Start(false) if c.nodeInfo.Tls { switch c.CertConfig.CertMode { @@ -32,7 +32,7 @@ func (c *Controller) initTask() { Interval: time.Hour * 24, Execute: c.reportUserTrafficTask, } - log.Printf("[%s] Start renew cert", c.Tag) + log.WithField("tag", c.Tag).Info("Start renew cert") // delay to start renewCert _ = c.renewCertPeriodic.Start(true) } @@ -43,22 +43,31 @@ func (c *Controller) nodeInfoMonitor() (err error) { // get node info newNodeInfo, err := c.apiClient.GetNodeInfo() if err != nil { - log.Printf("[%s] Get node info error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Get node info failed") return nil } // get user info newUserInfo, err := c.apiClient.GetUserList() if err != nil { - log.Printf("[%s] Get user list error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Get user list failed") return nil } if newNodeInfo != nil { // nodeInfo changed // Remove old tag - log.Printf("[%s] Node changed, reload...", c.Tag) + log.WithField("tag", c.Tag).Info("Node changed, reload") err = c.server.DelNode(c.Tag) if err != nil { - log.Printf("[%s] Del node error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Delete node failed") return nil } // Remove Old limiter @@ -70,13 +79,20 @@ func (c *Controller) nodeInfoMonitor() (err error) { if newNodeInfo.Tls || newNodeInfo.Type == "hysteria" { err = c.requestCert() if err != nil { - log.Printf("[%s] Request cert error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Request cert failed") + return nil } } // add new node err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig) if err != nil { - log.Printf("[%s] Add node error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Add node failed") return nil } _, err = c.server.AddUsers(&vCore.AddUsersParams{ @@ -86,12 +102,19 @@ func (c *Controller) nodeInfoMonitor() (err error) { NodeInfo: newNodeInfo, }) if err != nil { - log.Printf("[%s] Add users error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Add users failed") return nil } err = l.UpdateRule(newNodeInfo.Rules) if err != nil { - log.Printf("[%s] Update Rule error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Update Rule failed") + return nil } // Check interval if c.nodeInfoMonitorPeriodic.Interval != newNodeInfo.PullInterval && @@ -108,7 +131,7 @@ func (c *Controller) nodeInfoMonitor() (err error) { } c.nodeInfo = newNodeInfo c.userList = newUserInfo - log.Printf("[%s] Added %d new users", c.Tag, len(newUserInfo)) + log.WithField("tag", c.Tag).Infof("Added %d new users", len(newUserInfo)) // exit return nil } @@ -119,7 +142,11 @@ func (c *Controller) nodeInfoMonitor() (err error) { // have deleted users err = c.server.DelUsers(deleted, c.Tag) if err != nil { - log.Printf("[%s] Del users error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Delete users failed") + return nil } } if len(added) > 0 { @@ -131,18 +158,26 @@ func (c *Controller) nodeInfoMonitor() (err error) { NodeInfo: c.nodeInfo, }) if err != nil { - log.Printf("[%s] Add users error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Add users failed") + return nil } } if len(added) > 0 || len(deleted) > 0 { // update Limiter err = limiter.UpdateLimiter(c.Tag, added, deleted) if err != nil { - log.Printf("[%s] Update limiter error: %s", c.Tag, err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("limiter users failed") + return nil } } c.userList = newUserInfo - log.Printf("[%s] %d user deleted, %d user added", c.Tag, - len(deleted), len(added)) + log.WithField("tag", c.Tag). + Errorf("%d user deleted, %d user added", len(deleted), len(added)) return nil } diff --git a/node/user.go b/node/user.go index d832b09..b8e95cc 100644 --- a/node/user.go +++ b/node/user.go @@ -2,7 +2,7 @@ package node import ( "github.com/Yuzuki616/V2bX/api/panel" - "log" + log "github.com/sirupsen/logrus" "runtime" "strconv" ) @@ -25,9 +25,12 @@ func (c *Controller) reportUserTrafficTask() (err error) { if len(userTraffic) > 0 { err = c.apiClient.ReportUserTraffic(userTraffic) if err != nil { - log.Printf("Report user traffic faild: %s", err) + log.WithFields(log.Fields{ + "tag": c.Tag, + "err": err, + }).Error("Report user traffic faild") } else { - log.Printf("[%s] Report %d online users", c.Tag, len(userTraffic)) + log.WithField("tag", err).Errorf("Report %d online users", len(userTraffic)) } } userTraffic = nil