Refactor log (#3446)

* Refactor log

* Add new log methods

* Fix logger test

* Change all logging code

* Clean up pathObj

* Rebase to latest main

* Remove invoking method name after the dot
This commit is contained in:
yuhan6665 2024-06-29 14:32:57 -04:00 committed by GitHub
parent 8320732743
commit 079d0bd8a9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
291 changed files with 1837 additions and 2368 deletions

View file

@ -4,8 +4,8 @@ import (
"context"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/reality"
"github.com/xtls/xray-core/transport/internet/stat"
@ -14,7 +14,7 @@ import (
// Dial dials a new TCP connection to the given destination.
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
newError("dialing TCP to ", dest).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "dialing TCP to ", dest)
conn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
if err != nil {
return nil, err
@ -40,11 +40,11 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
return nil, newError("failed to get header settings").Base(err).AtError()
return nil, errors.New("failed to get header settings").Base(err).AtError()
}
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil {
return nil, newError("failed to create header authenticator").Base(err).AtError()
return nil, errors.New("failed to create header authenticator").Base(err).AtError()
}
conn = auth.Client(conn)
}

View file

@ -1,9 +0,0 @@
package tcp
import "github.com/xtls/xray-core/common/errors"
type errPathObjHolder struct{}
func newError(values ...interface{}) *errors.Error {
return errors.New(values...).WithPathObj(errPathObjHolder{})
}

View file

@ -8,8 +8,8 @@ import (
goreality "github.com/xtls/reality"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/reality"
"github.com/xtls/xray-core/transport/internet/stat"
@ -47,22 +47,22 @@ func ListenTCP(ctx context.Context, address net.Address, port net.Port, streamSe
Net: "unix",
}, streamSettings.SocketSettings)
if err != nil {
return nil, newError("failed to listen Unix Domain Socket on ", address).Base(err)
return nil, errors.New("failed to listen Unix Domain Socket on ", address).Base(err)
}
newError("listening Unix Domain Socket on ", address).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "listening Unix Domain Socket on ", address)
} else {
listener, err = internet.ListenSystem(ctx, &net.TCPAddr{
IP: address.IP(),
Port: int(port),
}, streamSettings.SocketSettings)
if err != nil {
return nil, newError("failed to listen TCP on ", address, ":", port).Base(err)
return nil, errors.New("failed to listen TCP on ", address, ":", port).Base(err)
}
newError("listening TCP on ", address, ":", port).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "listening TCP on ", address, ":", port)
}
if streamSettings.SocketSettings != nil && streamSettings.SocketSettings.AcceptProxyProtocol {
newError("accepting PROXY protocol").AtWarning().WriteToLog(session.ExportIDToError(ctx))
errors.LogWarning(ctx, "accepting PROXY protocol")
}
l.listener = listener
@ -77,11 +77,11 @@ func ListenTCP(ctx context.Context, address net.Address, port net.Port, streamSe
if tcpSettings.HeaderSettings != nil {
headerConfig, err := tcpSettings.HeaderSettings.GetInstance()
if err != nil {
return nil, newError("invalid header settings").Base(err).AtError()
return nil, errors.New("invalid header settings").Base(err).AtError()
}
auth, err := internet.CreateConnectionAuthenticator(headerConfig)
if err != nil {
return nil, newError("invalid header settings.").Base(err).AtError()
return nil, errors.New("invalid header settings.").Base(err).AtError()
}
l.authConfig = auth
}
@ -98,7 +98,7 @@ func (v *Listener) keepAccepting() {
if strings.Contains(errStr, "closed") {
break
}
newError("failed to accepted raw connections").Base(err).AtWarning().WriteToLog()
errors.LogWarningInner(context.Background(), err, "failed to accepted raw connections")
if strings.Contains(errStr, "too many") {
time.Sleep(time.Millisecond * 500)
}
@ -109,7 +109,7 @@ func (v *Listener) keepAccepting() {
conn = tls.Server(conn, v.tlsConfig)
} else if v.realityConfig != nil {
if conn, err = reality.Server(conn, v.realityConfig); err != nil {
newError(err).AtInfo().WriteToLog()
errors.LogInfo(context.Background(), err.Error())
return
}
}

View file

@ -4,6 +4,7 @@
package tcp
import (
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat"
@ -15,11 +16,11 @@ func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
ra := conn.RemoteAddr()
ip, port, err := internet.OriginalDst(la, ra)
if err != nil {
return net.Destination{}, newError("failed to get destination").Base(err)
return net.Destination{}, errors.New("failed to get destination").Base(err)
}
dest := net.TCPDestination(net.IPAddress(ip), net.Port(port))
if !dest.IsValid() {
return net.Destination{}, newError("failed to parse destination.")
return net.Destination{}, errors.New("failed to parse destination.")
}
return dest, nil
}

View file

@ -4,6 +4,7 @@
package tcp
import (
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat"
@ -15,11 +16,11 @@ func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
ra := conn.RemoteAddr()
ip, port, err := internet.OriginalDst(la, ra)
if err != nil {
return net.Destination{}, newError("failed to get destination").Base(err)
return net.Destination{}, errors.New("failed to get destination").Base(err)
}
dest := net.TCPDestination(net.IPAddress(ip), net.Port(port))
if !dest.IsValid() {
return net.Destination{}, newError("failed to parse destination.")
return net.Destination{}, errors.New("failed to parse destination.")
}
return dest, nil
}

View file

@ -4,9 +4,11 @@
package tcp
import (
"context"
"syscall"
"unsafe"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/transport/internet/stat"
)
@ -16,11 +18,11 @@ const SO_ORIGINAL_DST = 80
func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
sysrawconn, f := conn.(syscall.Conn)
if !f {
return net.Destination{}, newError("unable to get syscall.Conn")
return net.Destination{}, errors.New("unable to get syscall.Conn")
}
rawConn, err := sysrawconn.SyscallConn()
if err != nil {
return net.Destination{}, newError("failed to get sys fd").Base(err)
return net.Destination{}, errors.New("failed to get sys fd").Base(err)
}
var dest net.Destination
err = rawConn.Control(func(fd uintptr) {
@ -30,7 +32,7 @@ func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
}
addr, err := syscall.GetsockoptIPv6MTUInfo(int(fd), level, SO_ORIGINAL_DST)
if err != nil {
newError("failed to call getsockopt").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to call getsockopt")
return
}
ip := (*[4]byte)(unsafe.Pointer(&addr.Addr.Flowinfo))[:4]
@ -41,10 +43,10 @@ func GetOriginalDestination(conn stat.Connection) (net.Destination, error) {
dest = net.TCPDestination(net.IPAddress(ip), net.PortFromBytes(port))
})
if err != nil {
return net.Destination{}, newError("failed to control connection").Base(err)
return net.Destination{}, errors.New("failed to control connection").Base(err)
}
if !dest.IsValid() {
return net.Destination{}, newError("failed to call getsockopt")
return net.Destination{}, errors.New("failed to call getsockopt")
}
return dest, nil
}