mirror of
https://github.com/e1732a364fed/v2ray_simple.git
synced 2025-12-24 13:27:56 +08:00
miekg/dns -> v1.1.50;修订文档;添加-llf参数单独配置日志文件的日志等级
现状控制台的日志等级和日志文件的日志等级可以不同
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
|
||||
"github.com/pkg/profile"
|
||||
"go.uber.org/zap"
|
||||
"go.uber.org/zap/zapcore"
|
||||
|
||||
"github.com/e1732a364fed/v2ray_simple/machine"
|
||||
"github.com/e1732a364fed/v2ray_simple/netLayer"
|
||||
@@ -52,6 +53,8 @@ func init() {
|
||||
|
||||
flag.IntVar(&utils.LogLevel, "ll", utils.DefaultLL, "log level,0=debug, 1=info, 2=warning, 3=error, 4=dpanic, 5=panic, 6=fatal")
|
||||
|
||||
flag.IntVar(&utils.LogLevelForFile, "llf", -1, "log level for log file,if negative, it will be the same as ll. 0=debug, 1=info, 2=warning, 3=error, 4=dpanic, 5=panic, 6=fatal")
|
||||
|
||||
//有时发现在某些情况下,dns查询或者tcp链接的建立很慢,甚至超过8秒, 所以开放自定义超时时间,便于在不同环境下测试
|
||||
flag.IntVar(&dialTimeoutSecond, "dt", int(netLayer.DialTimeout/time.Second), "dial timeout, in second")
|
||||
flag.BoolVar(&mainM.EnableApiServer, "ea", false, "enable api server")
|
||||
@@ -182,9 +185,16 @@ func mainFunc() (result int) {
|
||||
fmt.Printf("Log Level:%d %s\n", utils.LogLevel, utils.LogLevelStr(utils.LogLevel))
|
||||
|
||||
if ce := utils.CanLogInfo("Options"); ce != nil {
|
||||
ce.Write(
|
||||
fields := []zapcore.Field{
|
||||
zap.String("Log Level", utils.LogLevelStr(utils.LogLevel)),
|
||||
zap.Bool("UseReadv", netLayer.UseReadv),
|
||||
}
|
||||
|
||||
if utils.LogLevelForFile >= 0 {
|
||||
fields = append(fields, zap.String("Log Level For File", utils.LogLevelStr(utils.LogLevelForFile)))
|
||||
}
|
||||
ce.Write(
|
||||
fields...,
|
||||
)
|
||||
} else {
|
||||
fmt.Printf("UseReadv:%t\n", netLayer.UseReadv)
|
||||
|
||||
2
go.mod
2
go.mod
@@ -12,7 +12,7 @@ require (
|
||||
github.com/lucas-clemente/quic-go v0.0.0-00010101000000-000000000000
|
||||
github.com/manifoldco/promptui v0.9.0
|
||||
github.com/marten-seemann/qtls v0.10.0
|
||||
github.com/miekg/dns v1.1.48
|
||||
github.com/miekg/dns v1.1.50
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible
|
||||
github.com/oschwald/maxminddb-golang v1.9.0
|
||||
github.com/pires/go-proxyproto v0.6.2
|
||||
|
||||
4
go.sum
4
go.sum
@@ -54,8 +54,8 @@ github.com/marten-seemann/qtls-go1-18 v0.1.3 h1:R4H2Ks8P6pAtUagjFty2p7BVHn3XiwDA
|
||||
github.com/marten-seemann/qtls-go1-18 v0.1.3/go.mod h1:mJttiymBAByA49mhlNZZGrH5u1uXYZJ+RW28Py7f4m4=
|
||||
github.com/marten-seemann/qtls-go1-19 v0.1.1 h1:mnbxeq3oEyQxQXwI4ReCgW9DPoPR94sNlqWoDZnjRIE=
|
||||
github.com/marten-seemann/qtls-go1-19 v0.1.1/go.mod h1:5HTDWtVudo/WFsHKRNuOhWlbdjrfs5JHrYb0wIJqGpI=
|
||||
github.com/miekg/dns v1.1.48 h1:Ucfr7IIVyMBz4lRE8qmGUuZ4Wt3/ZGu9hmcMT3Uu4tQ=
|
||||
github.com/miekg/dns v1.1.48/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
|
||||
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
|
||||
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible h1:4QJd3OLAMgj7ph+yZTuX13Ld4UpgHp07nNdFX7mqFfM=
|
||||
github.com/natefinch/lumberjack v2.0.0+incompatible/go.mod h1:Wi9p2TTF5DG5oU+6YfsmYQpsTIOm0B1VNzQg9Mw6nPk=
|
||||
github.com/onsi/ginkgo/v2 v2.2.0 h1:3ZNA3L1c5FYDFTTxbFeVGGD8jYvjYauHD30YgLxVsNI=
|
||||
|
||||
@@ -31,7 +31,7 @@ func Is_DNSQuery_returnType_ReadErr(err error) bool {
|
||||
}
|
||||
}
|
||||
|
||||
//筛除掉 Is_DNSQuery_returnType_ReadErr 时,err 为 net.Error.Timeout() 的情况
|
||||
// 筛除掉 Is_DNSQuery_returnType_ReadErr 时,err 为 net.Error.Timeout() 的情况
|
||||
func Is_DNSQuery_returnType_ReadFatalErr(err error) bool {
|
||||
if !Is_DNSQuery_returnType_ReadErr(err) {
|
||||
return false
|
||||
@@ -45,7 +45,7 @@ func Is_DNSQuery_returnType_ReadFatalErr(err error) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
//domain必须是 dns.Fqdn 函数 包过的, 本函数不检查是否包过。如果不包过就传入,会报错。
|
||||
// domain必须是 dns.Fqdn 函数 包过的, 本函数不检查是否包过。如果不包过就传入,会报错。
|
||||
// dns_type 为 miekg/dns 包中定义的类型, 如 TypeA, TypeAAAA, TypeCNAME.
|
||||
// conn是一个建立好的 dns.Conn, 必须非空, 本函数不检查.
|
||||
// theMux是与 conn相匹配的mutex, 这是为了防止同时有多个请求导致无法对口;内部若判断为nil,会主动使用一个全局mux.
|
||||
@@ -128,12 +128,13 @@ func DNSQuery(domain string, dns_type uint16, conn *dns.Conn, theMux *sync.Mutex
|
||||
return
|
||||
}
|
||||
|
||||
// 给 miekg/dns.Conn 加一个互斥锁, 可保证同一时间仅有一个请求发生.
|
||||
// 这样就不会造成并发时的混乱
|
||||
type DnsConn struct {
|
||||
*dns.Conn
|
||||
Name string //我们这里惯例,直接使用配置文件中配置的url字符串作为Name
|
||||
raddr *Addr //这个用于在Conn出故障后, 重新拨号时所使用
|
||||
|
||||
// 加一个互斥锁, 可保证同一时间仅有一个 对 dns.Conn 的使用。
|
||||
// 这样就不会造成并发时的混乱
|
||||
mutex sync.Mutex
|
||||
|
||||
garbageMark bool
|
||||
@@ -145,7 +146,7 @@ type IPRecord struct {
|
||||
RecordTime time.Time
|
||||
}
|
||||
|
||||
//dns machine维持与多个dns服务器的连接(最好是udp这种无状态的),并可以发起dns请求。
|
||||
// dns machine维持与多个dns服务器的连接(最好是udp这种无状态的),并可以发起dns请求。
|
||||
// 会缓存dns记录; 该设施是一个状态机, 所以叫 DNSMachine。
|
||||
// SpecialIPPollicy 用于指定特殊的 域名-ip 映射,这样遇到这种域名时,不经过dns查询,直接返回预设ip。
|
||||
// SpecialServerPollicy 用于为特殊的 域名指定特殊的 dns服务器,这样遇到这种域名时,会通过该特定服务器查询。
|
||||
@@ -174,7 +175,7 @@ func (c *DnsConn) Dial() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//建立一个与dns服务器连接, 可为纯udp dns or DoT. if DoT, 则要求 addr.Network == "tls",
|
||||
// 建立一个与dns服务器连接, 可为纯udp dns or DoT. if DoT, 则要求 addr.Network == "tls",
|
||||
// 如果是纯udp的,要求 addr.IsUDP() == true
|
||||
func DialDnsAddr(addr *Addr) (conn net.Conn, err error) {
|
||||
|
||||
@@ -265,7 +266,7 @@ func (dm *DNSMachine) Query(domain string) (ip net.IP) {
|
||||
return
|
||||
}
|
||||
|
||||
//传入的domain必须是不带尾缀点号的domain, 即没有包过 Fqdn
|
||||
// 传入的domain必须是不带尾缀点号的domain, 即没有包过 Fqdn
|
||||
func (dm *DNSMachine) QueryType(domain string, dns_type uint16) (ip net.IP) {
|
||||
var generalCacheHit bool // 若读到了 cache 或 SpecialIPPollicy 的项, 则 generalCacheHit 为 true
|
||||
|
||||
|
||||
32
utils/log.go
32
utils/log.go
@@ -24,10 +24,11 @@ const (
|
||||
|
||||
// LogLevel 值越小越唠叨, 废话越多,值越大打印的越少,见log_开头的常量;
|
||||
//
|
||||
//我们的loglevel具体值 与 zap的 loglevel+1 的含义等价
|
||||
// 我们的loglevel具体值 与 zap的 loglevel+1 的含义等价
|
||||
var (
|
||||
LogLevel int
|
||||
ZapLogger *zap.Logger
|
||||
LogLevel int
|
||||
LogLevelForFile int = -1 //这一项如果非负,则控制台的日志等级按 LogLevel 走,日志文件登记按 LogLevelForFile 走, 否则都按 LogLevel 走
|
||||
ZapLogger *zap.Logger
|
||||
|
||||
//日志输出文件名称
|
||||
LogOutFileName string
|
||||
@@ -55,7 +56,7 @@ func getZapLogFileWriteSyncer(fn string) zapcore.WriteSyncer {
|
||||
})
|
||||
}
|
||||
|
||||
//为了输出日志保持整齐, 统一使用5字节长度的字符串, 少的加尾缀空格, 多的以 点号 进行缩写。
|
||||
// 为了输出日志保持整齐, 统一使用5字节长度的字符串, 少的加尾缀空格, 多的以 点号 进行缩写。
|
||||
func levelCapitalStrWith5Chars(l zapcore.Level) string {
|
||||
switch l {
|
||||
case zapcore.DebugLevel:
|
||||
@@ -80,7 +81,7 @@ func capitalLevelEncoderWith5Chars(l zapcore.Level, enc zapcore.PrimitiveArrayEn
|
||||
enc.AppendString(levelCapitalStrWith5Chars(l))
|
||||
}
|
||||
|
||||
//本作大量用到zap打印输出, 所以必须调用InitLog函数来初始化,否则就会闪退
|
||||
// 本作大量用到zap打印输出, 所以必须调用InitLog函数来初始化,否则就会闪退
|
||||
func InitLog(firstMsg string) {
|
||||
atomicLevel := zap.NewAtomicLevel()
|
||||
atomicLevel.SetLevel(zapcore.Level(LogLevel - 1))
|
||||
@@ -103,7 +104,17 @@ func InitLog(firstMsg string) {
|
||||
jsonConf.TimeKey = "T"
|
||||
jsonConf.MessageKey = "M"
|
||||
jsonConf.EncodeLevel = capitalLevelEncoderWith5Chars
|
||||
jsonCore := zapcore.NewCore(zapcore.NewJSONEncoder(jsonConf), getZapLogFileWriteSyncer(LogOutFileName), atomicLevel)
|
||||
|
||||
var logfileLevel zapcore.LevelEnabler = atomicLevel
|
||||
|
||||
if LogLevelForFile >= 0 {
|
||||
lff := zapcore.Level(LogLevelForFile - 1)
|
||||
logfileLevel = zap.LevelEnablerFunc(func(lev zapcore.Level) bool {
|
||||
return lev >= lff
|
||||
})
|
||||
}
|
||||
|
||||
jsonCore := zapcore.NewCore(zapcore.NewJSONEncoder(jsonConf), getZapLogFileWriteSyncer(LogOutFileName), logfileLevel)
|
||||
|
||||
ZapLogger = zap.New(zapcore.NewTee(consoleCore, jsonCore))
|
||||
|
||||
@@ -168,18 +179,27 @@ func CanLogFatal(msg string) *zapcore.CheckedEntry {
|
||||
|
||||
}
|
||||
|
||||
// assume ZapLogger is not nil.
|
||||
func Debug(msg string) {
|
||||
ZapLogger.Debug(msg)
|
||||
}
|
||||
|
||||
// assume ZapLogger is not nil.
|
||||
func Info(msg string) {
|
||||
ZapLogger.Info(msg)
|
||||
}
|
||||
|
||||
// assume ZapLogger is not nil.
|
||||
func Warn(msg string) {
|
||||
ZapLogger.Warn(msg)
|
||||
}
|
||||
|
||||
// assume ZapLogger is not nil.
|
||||
func Error(msg string) {
|
||||
ZapLogger.Error(msg)
|
||||
}
|
||||
|
||||
// assume ZapLogger is not nil.
|
||||
func Fatal(msg string) {
|
||||
ZapLogger.Fatal(msg)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user