change log to logrus

This commit is contained in:
yuzuki999 2023-06-30 11:07:27 +08:00
parent 07b7ec9b3e
commit 65629c0dfb
9 changed files with 105 additions and 59 deletions

View File

@ -1,7 +1,7 @@
package cmd package cmd
import ( import (
"log" log "github.com/sirupsen/logrus"
_ "github.com/Yuzuki616/V2bX/core/imports" _ "github.com/Yuzuki616/V2bX/core/imports"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -14,6 +14,6 @@ var command = &cobra.Command{
func Run() { func Run() {
err := command.Execute() err := command.Execute()
if err != nil { if err != nil {
log.Println("execute failed, error:", err) log.WithField("err", err).Error("Execute command failed")
} }
} }

View File

@ -1,7 +1,7 @@
package cmd package cmd
import ( import (
"log" log "github.com/sirupsen/logrus"
"os" "os"
"os/signal" "os/signal"
"runtime" "runtime"
@ -42,23 +42,26 @@ func serverHandle(_ *cobra.Command, _ []string) {
c := conf.New() c := conf.New()
err := c.LoadFromPath(config) err := c.LoadFromPath(config)
if err != nil { 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() limiter.Init()
log.Println("Start V2bX...") log.Info("Start V2bX...")
vc, err := vCore.NewCore(&c.CoreConfig) vc, err := vCore.NewCore(&c.CoreConfig)
if err != nil { if err != nil {
log.Fatalf("New core error: %s", err) log.WithField("err", err).Error("new core failed")
return
} }
err = vc.Start() err = vc.Start()
if err != nil { if err != nil {
log.Fatalf("Start core error: %s", err) log.WithField("err", err).Error("Start core failed")
return
} }
defer vc.Close() defer vc.Close()
nodes := node.New() nodes := node.New()
err = nodes.Start(c.NodesConfig, vc) err = nodes.Start(c.NodesConfig, vc)
if err != nil { if err != nil {
log.Fatalf("Run nodes error: %s", err) log.WithField("err", err).Error("Run nodes failed")
return return
} }
if watch { if watch {
@ -66,24 +69,29 @@ func serverHandle(_ *cobra.Command, _ []string) {
nodes.Close() nodes.Close()
err = vc.Close() err = vc.Close()
if err != nil { 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) vc, err = vCore.NewCore(&c.CoreConfig)
if err != nil { if err != nil {
log.Fatalf("New core error: %s", err) log.WithField("err", err).Error("New core failed")
return
} }
err = vc.Start() err = vc.Start()
if err != nil { 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) err = nodes.Start(c.NodesConfig, vc)
if err != nil { if err != nil {
log.Fatalf("Run nodes error: %s", err) log.WithField("err", err).Error("Run nodes failed")
return
} }
runtime.GC() runtime.GC()
}) })
if err != nil { if err != nil {
log.Fatalf("Watch config file error: %s", err) log.WithField("err", err).Error("start watch failed")
return
} }
} }
// clear memory // clear memory

View File

@ -73,10 +73,8 @@ func (c *Core) AddUsers(p *vCore.AddUsersParams) (added int, err error) {
users := make([]*protocol.User, 0, len(p.UserInfo)) users := make([]*protocol.User, 0, len(p.UserInfo))
switch p.NodeInfo.Type { switch p.NodeInfo.Type {
case "v2ray": case "v2ray":
if p.Config.XrayOptions.EnableVless || if p.Config.XrayOptions.EnableVless ||
p.NodeInfo.ExtraConfig.EnableVless { p.NodeInfo.ExtraConfig.EnableVless {
if p.Config.XrayOptions.VlessFlow != "" { if p.Config.XrayOptions.VlessFlow != "" {
if p.Config.XrayOptions.VlessFlow == p.NodeInfo.ExtraConfig.VlessFlow { if p.Config.XrayOptions.VlessFlow == p.NodeInfo.ExtraConfig.VlessFlow {
users = builder.BuildVlessUsers(p.Tag, p.UserInfo, p.Config.XrayOptions.VlessFlow) users = builder.BuildVlessUsers(p.Tag, p.UserInfo, p.Config.XrayOptions.VlessFlow)

View File

@ -1,7 +1,6 @@
package xray package xray
import ( import (
"log"
"os" "os"
"sync" "sync"
@ -10,6 +9,7 @@ import (
"github.com/Yuzuki616/V2bX/core/xray/app/dispatcher" "github.com/Yuzuki616/V2bX/core/xray/app/dispatcher"
_ "github.com/Yuzuki616/V2bX/core/xray/distro/all" _ "github.com/Yuzuki616/V2bX/core/xray/distro/all"
"github.com/goccy/go-json" "github.com/goccy/go-json"
log "github.com/sirupsen/logrus"
"github.com/xtls/xray-core/app/proxyman" "github.com/xtls/xray-core/app/proxyman"
"github.com/xtls/xray-core/app/stats" "github.com/xtls/xray-core/app/stats"
"github.com/xtls/xray-core/common/serial" "github.com/xtls/xray-core/common/serial"
@ -63,40 +63,40 @@ func getCore(c *conf.XrayConfig) *core.Instance {
coreDnsConfig := &coreConf.DNSConfig{} coreDnsConfig := &coreConf.DNSConfig{}
if c.DnsConfigPath != "" { if c.DnsConfigPath != "" {
if f, err := os.Open(c.DnsConfigPath); err != nil { 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 { } else {
if err = json.NewDecoder(f).Decode(coreDnsConfig); err != nil { 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() dnsConfig, err := coreDnsConfig.Build()
if err != nil { 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 // Routing config
coreRouterConfig := &coreConf.RouterConfig{} coreRouterConfig := &coreConf.RouterConfig{}
if c.RouteConfigPath != "" { if c.RouteConfigPath != "" {
if f, err := os.Open(c.RouteConfigPath); err != nil { 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 { } else {
if err = json.NewDecoder(f).Decode(coreRouterConfig); err != nil { 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() routeConfig, err := coreRouterConfig.Build()
if err != nil { 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 // 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 { 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 { } else {
if err = json.NewDecoder(f).Decode(&coreCustomInboundConfig); err != nil { 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 { for _, config := range coreCustomInboundConfig {
oc, err := config.Build() oc, err := config.Build()
if err != nil { 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) inBoundConfig = append(inBoundConfig, oc)
} }
@ -112,10 +112,10 @@ func getCore(c *conf.XrayConfig) *core.Instance {
var coreCustomOutboundConfig []coreConf.OutboundDetourConfig var coreCustomOutboundConfig []coreConf.OutboundDetourConfig
if c.OutboundConfigPath != "" { if c.OutboundConfigPath != "" {
if f, err := os.Open(c.OutboundConfigPath); err != nil { 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 { } else {
if err = json.NewDecoder(f).Decode(&coreCustomOutboundConfig); err != nil { 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 { for _, config := range coreCustomOutboundConfig {
oc, err := config.Build() oc, err := config.Build()
if err != nil { 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) outBoundConfig = append(outBoundConfig, oc)
} }
@ -149,10 +149,9 @@ func getCore(c *conf.XrayConfig) *core.Instance {
} }
server, err := core.New(config) server, err := core.New(config)
if err != nil { 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 return server
} }

View File

@ -1,14 +1,16 @@
package limiter package limiter
import "log" import log "github.com/sirupsen/logrus"
func ClearOnlineIP() error { func ClearOnlineIP() error {
log.Println("Limiter: Clear online ip...") log.WithField("Type", "Limiter").
Debug("Clear online ip...")
limitLock.RLock() limitLock.RLock()
for _, l := range limiter { for _, l := range limiter {
l.ConnLimiter.ClearOnlineIP() l.ConnLimiter.ClearOnlineIP()
} }
limitLock.RUnlock() limitLock.RUnlock()
log.Println("Limiter: Clear online ip done") log.WithField("Type", "Limiter").
Debug("Clear online ip done")
return nil return nil
} }

View File

@ -7,8 +7,8 @@ import (
"github.com/Yuzuki616/V2bX/common/builder" "github.com/Yuzuki616/V2bX/common/builder"
"github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/conf"
"github.com/juju/ratelimit" "github.com/juju/ratelimit"
log "github.com/sirupsen/logrus"
"github.com/xtls/xray-core/common/task" "github.com/xtls/xray-core/common/task"
"log"
"regexp" "regexp"
"sync" "sync"
"time" "time"
@ -24,7 +24,8 @@ func Init() {
Execute: ClearOnlineIP, Execute: ClearOnlineIP,
} }
go func() { go func() {
log.Println("Limiter: ClearOnlineIP started") log.WithField("Type", "Limiter").
Debug("ClearOnlineIP started")
time.Sleep(time.Minute * 2) time.Sleep(time.Minute * 2)
_ = c.Start() _ = c.Start()
}() }()

View File

@ -9,7 +9,7 @@ import (
"github.com/Yuzuki616/V2bX/conf" "github.com/Yuzuki616/V2bX/conf"
vCore "github.com/Yuzuki616/V2bX/core" vCore "github.com/Yuzuki616/V2bX/core"
"github.com/Yuzuki616/V2bX/limiter" "github.com/Yuzuki616/V2bX/limiter"
"log" log "github.com/sirupsen/logrus"
) )
type Controller struct { type Controller struct {
@ -81,7 +81,7 @@ func (c *Controller) Start() error {
if err != nil { if err != nil {
return fmt.Errorf("add users error: %s", err) 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() c.initTask()
return nil return nil
} }

View File

@ -4,7 +4,7 @@ import (
"github.com/Yuzuki616/V2bX/common/task" "github.com/Yuzuki616/V2bX/common/task"
vCore "github.com/Yuzuki616/V2bX/core" vCore "github.com/Yuzuki616/V2bX/core"
"github.com/Yuzuki616/V2bX/limiter" "github.com/Yuzuki616/V2bX/limiter"
"log" log "github.com/sirupsen/logrus"
"time" "time"
) )
@ -19,10 +19,10 @@ func (c *Controller) initTask() {
Interval: c.nodeInfo.PushInterval, Interval: c.nodeInfo.PushInterval,
Execute: c.reportUserTrafficTask, 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 // delay to start nodeInfoMonitor
_ = c.nodeInfoMonitorPeriodic.Start(false) _ = 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) _ = c.userReportPeriodic.Start(false)
if c.nodeInfo.Tls { if c.nodeInfo.Tls {
switch c.CertConfig.CertMode { switch c.CertConfig.CertMode {
@ -32,7 +32,7 @@ func (c *Controller) initTask() {
Interval: time.Hour * 24, Interval: time.Hour * 24,
Execute: c.reportUserTrafficTask, Execute: c.reportUserTrafficTask,
} }
log.Printf("[%s] Start renew cert", c.Tag) log.WithField("tag", c.Tag).Info("Start renew cert")
// delay to start renewCert // delay to start renewCert
_ = c.renewCertPeriodic.Start(true) _ = c.renewCertPeriodic.Start(true)
} }
@ -43,22 +43,31 @@ func (c *Controller) nodeInfoMonitor() (err error) {
// get node info // get node info
newNodeInfo, err := c.apiClient.GetNodeInfo() newNodeInfo, err := c.apiClient.GetNodeInfo()
if err != nil { 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 return nil
} }
// get user info // get user info
newUserInfo, err := c.apiClient.GetUserList() newUserInfo, err := c.apiClient.GetUserList()
if err != nil { 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 return nil
} }
if newNodeInfo != nil { if newNodeInfo != nil {
// nodeInfo changed // nodeInfo changed
// Remove old tag // 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) err = c.server.DelNode(c.Tag)
if err != nil { 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 return nil
} }
// Remove Old limiter // Remove Old limiter
@ -70,13 +79,20 @@ func (c *Controller) nodeInfoMonitor() (err error) {
if newNodeInfo.Tls || newNodeInfo.Type == "hysteria" { if newNodeInfo.Tls || newNodeInfo.Type == "hysteria" {
err = c.requestCert() err = c.requestCert()
if err != nil { 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 // add new node
err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig) err = c.server.AddNode(c.Tag, newNodeInfo, c.ControllerConfig)
if err != nil { 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 return nil
} }
_, err = c.server.AddUsers(&vCore.AddUsersParams{ _, err = c.server.AddUsers(&vCore.AddUsersParams{
@ -86,12 +102,19 @@ func (c *Controller) nodeInfoMonitor() (err error) {
NodeInfo: newNodeInfo, NodeInfo: newNodeInfo,
}) })
if err != nil { 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 return nil
} }
err = l.UpdateRule(newNodeInfo.Rules) err = l.UpdateRule(newNodeInfo.Rules)
if err != nil { 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 // Check interval
if c.nodeInfoMonitorPeriodic.Interval != newNodeInfo.PullInterval && if c.nodeInfoMonitorPeriodic.Interval != newNodeInfo.PullInterval &&
@ -108,7 +131,7 @@ func (c *Controller) nodeInfoMonitor() (err error) {
} }
c.nodeInfo = newNodeInfo c.nodeInfo = newNodeInfo
c.userList = newUserInfo 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 // exit
return nil return nil
} }
@ -119,7 +142,11 @@ func (c *Controller) nodeInfoMonitor() (err error) {
// have deleted users // have deleted users
err = c.server.DelUsers(deleted, c.Tag) err = c.server.DelUsers(deleted, c.Tag)
if err != nil { 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 { if len(added) > 0 {
@ -131,18 +158,26 @@ func (c *Controller) nodeInfoMonitor() (err error) {
NodeInfo: c.nodeInfo, NodeInfo: c.nodeInfo,
}) })
if err != nil { 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 { if len(added) > 0 || len(deleted) > 0 {
// update Limiter // update Limiter
err = limiter.UpdateLimiter(c.Tag, added, deleted) err = limiter.UpdateLimiter(c.Tag, added, deleted)
if err != nil { 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 c.userList = newUserInfo
log.Printf("[%s] %d user deleted, %d user added", c.Tag, log.WithField("tag", c.Tag).
len(deleted), len(added)) Errorf("%d user deleted, %d user added", len(deleted), len(added))
return nil return nil
} }

View File

@ -2,7 +2,7 @@ package node
import ( import (
"github.com/Yuzuki616/V2bX/api/panel" "github.com/Yuzuki616/V2bX/api/panel"
"log" log "github.com/sirupsen/logrus"
"runtime" "runtime"
"strconv" "strconv"
) )
@ -25,9 +25,12 @@ func (c *Controller) reportUserTrafficTask() (err error) {
if len(userTraffic) > 0 { if len(userTraffic) > 0 {
err = c.apiClient.ReportUserTraffic(userTraffic) err = c.apiClient.ReportUserTraffic(userTraffic)
if err != nil { 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 { } 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 userTraffic = nil