feat/chore: add log file/refine code/add tip for no-load (#219)

This commit is contained in:
mzz 2023-07-15 12:23:36 +08:00 committed by GitHub
parent c58c626050
commit 7ea40151ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 51 additions and 19 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ dae
outline.json
go-mod/
node_modules/
*.log

View File

@ -18,6 +18,7 @@ import (
"github.com/mzz2017/softwind/netproxy"
"github.com/mzz2017/softwind/pkg/fastrand"
"github.com/mzz2017/softwind/protocol/direct"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/daeuniverse/dae/cmd/internal"
"github.com/daeuniverse/dae/common"
@ -45,9 +46,12 @@ var (
)
func init() {
runCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "disable timestamp")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-pidfile", "", false, "not generate /var/run/dae.pid")
runCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "Config file of dae.")
runCmd.PersistentFlags().StringVar(&logFile, "logfile", "", "Log file to write. Empty means writing to stdout and stderr.")
runCmd.PersistentFlags().IntVar(&logFileMaxSize, "logfile-maxsize", 30, "Unit: MB. The maximum size in megabytes of the log file before it gets rotated.")
runCmd.PersistentFlags().IntVar(&logFileMaxBackups, "logfile-maxbackups", 3, "The maximum number of old log files to retain.")
runCmd.PersistentFlags().BoolVarP(&disableTimestamp, "disable-timestamp", "", false, "Disable timestamp.")
runCmd.PersistentFlags().BoolVarP(&disablePidFile, "disable-pidfile", "", false, "Not generate /var/run/dae.pid.")
fastrand.Rand().Shuffle(len(CheckNetworkLinks), func(i, j int) {
CheckNetworkLinks[i], CheckNetworkLinks[j] = CheckNetworkLinks[j], CheckNetworkLinks[i]
@ -56,6 +60,9 @@ func init() {
var (
cfgFile string
logFile string
logFileMaxSize int
logFileMaxBackups int
disableTimestamp bool
disablePidFile bool
@ -78,7 +85,18 @@ var (
}).Fatalln("Failed to read config")
}
log := logger.NewLogger(conf.Global.LogLevel, disableTimestamp)
var logOpts *lumberjack.Logger
if logFile != "" {
logOpts = &lumberjack.Logger{
Filename: logFile,
MaxSize: logFileMaxSize,
MaxAge: 0,
MaxBackups: logFileMaxBackups,
LocalTime: true,
Compress: true,
}
}
log := logger.NewLogger(conf.Global.LogLevel, disableTimestamp, logOpts)
logrus.SetLevel(log.Level)
log.Infof("Include config files: [%v]", strings.Join(includes, ", "))
@ -181,7 +199,9 @@ loop:
log.Infof("Include config files: [%v]", strings.Join(includes, ", "))
}
// New logger.
log = logger.NewLogger(newConf.Global.LogLevel, disableTimestamp)
oldLogOutput := log.Out
log = logger.NewLogger(newConf.Global.LogLevel, disableTimestamp, nil)
log.SetOutput(oldLogOutput) // FIXME: THIS IS A HACK.
logrus.SetLevel(log.Level)
// New control plane.

View File

@ -65,6 +65,8 @@ const (
OutboundDirect OutboundIndex = iota
OutboundBlock
OutboundUserDefinedMin
OutboundMustRules OutboundIndex = 0xFC
OutboundControlPlaneRouting OutboundIndex = 0xFD
OutboundLogicalOr OutboundIndex = 0xFE

View File

@ -26,8 +26,9 @@ var TestNetworkType = &dialer.NetworkType{
IsDns: false,
}
var log = logger.NewLogger("trace", false, nil)
func TestDialerGroup_Select_Fixed(t *testing.T) {
log := logger.NewLogger("trace", false)
option := &dialer.GlobalOption{
Log: log,
TcpCheckOptionRaw: dialer.TcpCheckOptionRaw{Raw: []string{testTcpCheckUrl}},
@ -69,7 +70,6 @@ func TestDialerGroup_Select_Fixed(t *testing.T) {
}
func TestDialerGroup_Select_MinLastLatency(t *testing.T) {
log := logger.NewLogger("trace", false)
option := &dialer.GlobalOption{
Log: log,
@ -139,7 +139,6 @@ func TestDialerGroup_Select_MinLastLatency(t *testing.T) {
}
func TestDialerGroup_Select_Random(t *testing.T) {
log := logger.NewLogger("trace", false)
option := &dialer.GlobalOption{
Log: log,
@ -179,7 +178,6 @@ func TestDialerGroup_Select_Random(t *testing.T) {
}
func TestDialerGroup_SetAlive(t *testing.T) {
log := logger.NewLogger("trace", false)
option := &dialer.GlobalOption{
Log: log,

View File

@ -565,6 +565,9 @@ func (c *ControlPlane) ChooseDialTarget(outbound consts.OutboundIndex, dst netip
if c.realDomainSet.TestString(domain) {
c.muRealDomainSet.Unlock()
dialMode = consts.DialMode_Domain
// Should use this domain to reroute
shouldReroute = true
} else {
c.muRealDomainSet.Unlock()
// Lookup A/AAAA to make sure it is a real domain.
@ -589,6 +592,7 @@ func (c *ControlPlane) ChooseDialTarget(outbound consts.OutboundIndex, dst netip
}
case consts.DialMode_DomainCao:
shouldReroute = true
fallthrough
case consts.DialMode_DomainPlus:
dialMode = consts.DialMode_Domain

View File

@ -127,9 +127,6 @@ func (c *ControlPlane) RouteDialTcp(p *RouteDialParam) (conn netproxy.Conn, err
domain := p.Domain
src := p.Src
dst := p.Dest
if c.dialMode == consts.DialMode_DomainCao && domain != "" {
outboundIndex = consts.OutboundControlPlaneRouting
}
dialTarget, shouldReroute, dialIp := c.ChooseDialTarget(outboundIndex, dst, domain)
if shouldReroute {
@ -159,6 +156,9 @@ func (c *ControlPlane) RouteDialTcp(p *RouteDialParam) (conn netproxy.Conn, err
}
// TODO: Set-up ip to domain mapping and show domain if possible.
if int(outboundIndex) >= len(c.outbounds) {
if len(c.outbounds) == int(consts.OutboundUserDefinedMin) {
return nil, fmt.Errorf("traffic was dropped due to no-load configuration")
}
return nil, fmt.Errorf("outbound id from bpf is out of range: %v not in [0, %v]", outboundIndex, len(c.outbounds)-1)
}
outbound := c.outbounds[outboundIndex]

View File

@ -179,9 +179,6 @@ func (c *ControlPlane) handlePkt(lConn *net.UDPConn, data []byte, src, pktDst, r
}
// Get outbound.
outboundIndex := consts.OutboundIndex(routingResult.Outbound)
if c.dialMode == consts.DialMode_DomainCao && domain != "" {
outboundIndex = consts.OutboundControlPlaneRouting
}
dialTarget, shouldReroute, dialIp := c.ChooseDialTarget(outboundIndex, realDst, domain)
getNew:
if retry > MaxRetry {
@ -229,6 +226,9 @@ getNew:
}
if int(outboundIndex) >= len(c.outbounds) {
if len(c.outbounds) == int(consts.OutboundUserDefinedMin) {
return nil, fmt.Errorf("traffic was dropped due to no-load configuration")
}
return nil, fmt.Errorf("outbound %v out of range [0, %v]", outboundIndex, len(c.outbounds)-1)
}
outbound := c.outbounds[outboundIndex]

1
go.mod
View File

@ -41,6 +41,7 @@ require (
golang.org/x/net v0.12.0 // indirect
golang.org/x/tools v0.11.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230706204954-ccb25ca9f130 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
)
require (

2
go.sum
View File

@ -231,6 +231,8 @@ google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View File

@ -8,9 +8,10 @@ package logger
import (
"github.com/sirupsen/logrus"
prefixed "github.com/x-cray/logrus-prefixed-formatter"
"gopkg.in/natefinch/lumberjack.v2"
)
func NewLogger(logLevel string, disableTimestamp bool) *logrus.Logger {
func NewLogger(logLevel string, disableTimestamp bool, logFileOpt *lumberjack.Logger) *logrus.Logger {
log := logrus.New()
level, err := logrus.ParseLevel(logLevel)
@ -24,6 +25,9 @@ func NewLogger(logLevel string, disableTimestamp bool) *logrus.Logger {
FullTimestamp: true,
TimestampFormat: "Jan 02 15:04:05",
})
if logFileOpt != nil {
log.SetOutput(logFileOpt)
}
return log
}