add LogDuration

This commit is contained in:
berlin2123 2025-03-04 16:24:46 +08:00 committed by GitHub
parent 2a7aa69890
commit c20d7dff51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 42 additions and 1 deletions

View File

@ -89,6 +89,8 @@ log.level = "info"
log.maxDays = 3 log.maxDays = 3
# disable log colors when log.to is console, default is false # disable log colors when log.to is console, default is false
log.disablePrintColor = false log.disablePrintColor = false
# log the duration of each connection name has string 'ssh' or 'rdp'.
log_duration_types = ssh,rdp
# DetailedErrorsToClient defines whether to send the specific error (with debug info) to frpc. By default, this value is true. # DetailedErrorsToClient defines whether to send the specific error (with debug info) to frpc. By default, this value is true.
detailedErrorsToClient = true detailedErrorsToClient = true

View File

@ -70,6 +70,12 @@ log_max_days = 3
# disable log colors when log_file is console, default is false # disable log colors when log_file is console, default is false
disable_log_color = false disable_log_color = false
# log the duration of each connection for the connection type identified by its name
# examples,
# 'log_duration_types = ssh,rdp', it will log the duration for connection name has string 'ssh' or 'rdp'.
# 'log_duration_types = all', it will log the duration for all connections.
log_duration_types = ssh,rdp
# DetailedErrorsToClient defines whether to send the specific error (with debug info) to frpc. By default, this value is true. # DetailedErrorsToClient defines whether to send the specific error (with debug info) to frpc. By default, this value is true.
detailed_errors_to_client = true detailed_errors_to_client = true

View File

@ -241,6 +241,7 @@ func RegisterServerConfigFlags(cmd *cobra.Command, c *v1.ServerConfig, opts ...R
cmd.PersistentFlags().StringVarP(&c.Log.Level, "log_level", "", "info", "log level") cmd.PersistentFlags().StringVarP(&c.Log.Level, "log_level", "", "info", "log level")
cmd.PersistentFlags().Int64VarP(&c.Log.MaxDays, "log_max_days", "", 3, "log max days") cmd.PersistentFlags().Int64VarP(&c.Log.MaxDays, "log_max_days", "", 3, "log max days")
cmd.PersistentFlags().BoolVarP(&c.Log.DisablePrintColor, "disable_log_color", "", false, "disable log color in console") cmd.PersistentFlags().BoolVarP(&c.Log.DisablePrintColor, "disable_log_color", "", false, "disable log color in console")
cmd.PersistentFlags().StringVarP(&c.Log.DurationTypes, "log_duration_types", "", "ssh,rdp", "log duration types")
cmd.PersistentFlags().StringVarP(&c.Auth.Token, "token", "t", "", "auth token") cmd.PersistentFlags().StringVarP(&c.Auth.Token, "token", "t", "", "auth token")
cmd.PersistentFlags().StringVarP(&c.SubDomainHost, "subdomain_host", "", "", "subdomain host") cmd.PersistentFlags().StringVarP(&c.SubDomainHost, "subdomain_host", "", "", "subdomain host")
cmd.PersistentFlags().VarP(&PortsRangeSliceFlag{V: &c.AllowPorts}, "allow_ports", "", "allow ports") cmd.PersistentFlags().VarP(&PortsRangeSliceFlag{V: &c.AllowPorts}, "allow_ports", "", "allow ports")

View File

@ -133,6 +133,7 @@ func Convert_ServerCommonConf_To_v1(conf *ServerCommonConf) *v1.ServerConfig {
out.Log.Level = conf.LogLevel out.Log.Level = conf.LogLevel
out.Log.MaxDays = conf.LogMaxDays out.Log.MaxDays = conf.LogMaxDays
out.Log.DisablePrintColor = conf.DisableLogColor out.Log.DisablePrintColor = conf.DisableLogColor
out.Log.DurationType = conf.LogDurationType
out.DetailedErrorsToClient = lo.ToPtr(conf.DetailedErrorsToClient) out.DetailedErrorsToClient = lo.ToPtr(conf.DetailedErrorsToClient)
out.SubDomainHost = conf.SubDomainHost out.SubDomainHost = conf.SubDomainHost

View File

@ -125,6 +125,11 @@ type ServerCommonConf struct {
// DisableLogColor disables log colors when LogWay == "console" when set to // DisableLogColor disables log colors when LogWay == "console" when set to
// true. By default, this value is false. // true. By default, this value is false.
DisableLogColor bool `ini:"disable_log_color" json:"disable_log_color"` DisableLogColor bool `ini:"disable_log_color" json:"disable_log_color"`
// LogDurationTypes specifies the type of connection name, which will log
// the duration of those connections. if it is setting as 'ssh,rdp', it
// will log the duration of the name as 'ssh', 'ssh_1', 'sshname', 'rdp',
// 'rdp_test1' or 'web_my_rdp' . By default, this value is "ssh,rdp".
LogDurationTypes string `ini:"log_duration_types" json:"log_duration_types"`
// DetailedErrorsToClient defines whether to send the specific error (with // DetailedErrorsToClient defines whether to send the specific error (with
// debug info) to frpc. By default, this value is true. // debug info) to frpc. By default, this value is true.
DetailedErrorsToClient bool `ini:"detailed_errors_to_client" json:"detailed_errors_to_client"` DetailedErrorsToClient bool `ini:"detailed_errors_to_client" json:"detailed_errors_to_client"`
@ -210,6 +215,7 @@ func GetDefaultServerConf() ServerCommonConf {
DashboardAddr: "0.0.0.0", DashboardAddr: "0.0.0.0",
LogFile: "console", LogFile: "console",
LogWay: "console", LogWay: "console",
LogDurationTypes: "ssh,rdp",
DetailedErrorsToClient: true, DetailedErrorsToClient: true,
TCPMux: true, TCPMux: true,
AllowPorts: make(map[int]struct{}), AllowPorts: make(map[int]struct{}),

View File

@ -21,6 +21,7 @@ import (
"net" "net"
"reflect" "reflect"
"strconv" "strconv"
"strings"
"sync" "sync"
"time" "time"
@ -258,6 +259,7 @@ func (pxy *BaseProxy) handleUserTCPConnection(userConn net.Conn) {
}) })
} }
startime := time.Now().UnixNano() / 1000000 // time in microseconds
xl.Debugf("join connections, workConn(l[%s] r[%s]) userConn(l[%s] r[%s])", workConn.LocalAddr().String(), xl.Debugf("join connections, workConn(l[%s] r[%s]) userConn(l[%s] r[%s])", workConn.LocalAddr().String(),
workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String()) workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
@ -268,7 +270,30 @@ func (pxy *BaseProxy) handleUserTCPConnection(userConn net.Conn) {
metrics.Server.CloseConnection(name, proxyType) metrics.Server.CloseConnection(name, proxyType)
metrics.Server.AddTrafficIn(name, proxyType, inCount) metrics.Server.AddTrafficIn(name, proxyType, inCount)
metrics.Server.AddTrafficOut(name, proxyType, outCount) metrics.Server.AddTrafficOut(name, proxyType, outCount)
xl.Debugf("join connections closed")
if IsTheTypeToLog(serverCfg.Log.DurationTypes, name) {
endtime := time.Now().UnixNano() / 1000000 // time in microseconds
connectionDuration := endtime - startime
xl.Debugf("join connection closed, it remains [%d]ms, workConn(l[%s] r[%s]) userConn(l[%s] r[%s])", connectionDuration,
workConn.LocalAddr().String(), workConn.RemoteAddr().String(), userConn.LocalAddr().String(), userConn.RemoteAddr().String())
xl.Info("connection closed, it remains [%d]ms, userConn(l[%s] r[%s])", connectionDuration,
userConn.LocalAddr().String(), userConn.RemoteAddr().String())
} else {
xl.Debugf("join connection closed, userConn(l[%s] r[%s])", userConn.LocalAddr().String(), userConn.RemoteAddr().String())
}
}
func IsTheTypeToLog(logDurationTypes string, name string) bool {
if strings.Contains(logDurationTypes, "all") {
return true
}
thestrlist := strings.Split(logDurationTypes, ",")
for i := 0; i < len(thestrlist); i++ {
if strings.Contains(name, thestrlist[i]) {
return true
}
}
return false
} }
type Options struct { type Options struct {