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

@ -11,6 +11,8 @@ import (
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/buf"
c "github.com/xtls/xray-core/common/ctx"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
"github.com/xtls/xray-core/common/net/cnc"
"github.com/xtls/xray-core/common/session"
@ -44,7 +46,7 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
tlsConfigs := tls.ConfigFromStreamSettings(streamSettings)
realityConfigs := reality.ConfigFromStreamSettings(streamSettings)
if tlsConfigs == nil && realityConfigs == nil {
return nil, newError("TLS or REALITY must be enabled for http transport.").AtWarning()
return nil, errors.New("TLS or REALITY must be enabled for http transport.").AtWarning()
}
sockopt := streamSettings.SocketSettings
@ -67,13 +69,13 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
}
address := net.ParseAddress(rawHost)
hctx = session.ContextWithID(hctx, session.IDFromContext(ctx))
hctx = c.ContextWithID(hctx, c.IDFromContext(ctx))
hctx = session.ContextWithOutbounds(hctx, session.OutboundsFromContext(ctx))
hctx = session.ContextWithTimeoutOnly(hctx, true)
pconn, err := internet.DialSystem(hctx, net.TCPDestination(address, port), sockopt)
if err != nil {
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
errors.LogErrorInner(ctx, err, "failed to dial to " + addr)
return nil, err
}
@ -88,18 +90,18 @@ func getHTTPClient(ctx context.Context, dest net.Destination, streamSettings *in
cn = tls.Client(pconn, tlsConfig).(*tls.Conn)
}
if err := cn.HandshakeContext(ctx); err != nil {
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
errors.LogErrorInner(ctx, err, "failed to dial to " + addr)
return nil, err
}
if !tlsConfig.InsecureSkipVerify {
if err := cn.VerifyHostname(tlsConfig.ServerName); err != nil {
newError("failed to dial to " + addr).Base(err).AtError().WriteToLog()
errors.LogErrorInner(ctx, err, "failed to dial to " + addr)
return nil, err
}
}
negotiatedProtocol := cn.NegotiatedProtocol()
if negotiatedProtocol != http2.NextProtoTLS {
return nil, newError("http2: unexpected ALPN protocol " + negotiatedProtocol + "; want q" + http2.NextProtoTLS).AtError()
return nil, errors.New("http2: unexpected ALPN protocol " + negotiatedProtocol + "; want q" + http2.NextProtoTLS).AtError()
}
return cn, nil
},
@ -168,7 +170,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
go func() {
response, err := client.Do(request)
if err != nil {
newError("failed to dial to ", dest).Base(err).AtWarning().WriteToLog(session.ExportIDToError(ctx))
errors.LogWarningInner(ctx, err, "failed to dial to ", dest)
wrc.Close()
{
// Abandon `client` if `client.Do(request)` failed
@ -182,7 +184,7 @@ func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.Me
return
}
if response.StatusCode != 200 {
newError("unexpected status", response.StatusCode).AtWarning().WriteToLog(session.ExportIDToError(ctx))
errors.LogWarning(ctx, "unexpected status", response.StatusCode)
wrc.Close()
return
}

View file

@ -1,9 +0,0 @@
package http
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

@ -9,11 +9,11 @@ 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/net/cnc"
http_proto "github.com/xtls/xray-core/common/protocol/http"
"github.com/xtls/xray-core/common/serial"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/common/signal/done"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/reality"
@ -89,7 +89,7 @@ func (l *Listener) ServeHTTP(writer http.ResponseWriter, request *http.Request)
remoteAddr := l.Addr()
dest, err := net.ParseDestination(request.RemoteAddr)
if err != nil {
newError("failed to parse request remote addr: ", request.RemoteAddr).Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to parse request remote addr: ", request.RemoteAddr)
} else {
remoteAddr = &net.TCPAddr{
IP: dest.Address.IP(),
@ -160,7 +160,7 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
}
if streamSettings.SocketSettings != nil && streamSettings.SocketSettings.AcceptProxyProtocol {
newError("accepting PROXY protocol").AtWarning().WriteToLog(session.ExportIDToError(ctx))
errors.LogWarning(ctx, "accepting PROXY protocol")
}
listener.server = server
@ -173,7 +173,7 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
Net: "unix",
}, streamSettings.SocketSettings)
if err != nil {
newError("failed to listen on ", address).Base(err).AtError().WriteToLog(session.ExportIDToError(ctx))
errors.LogErrorInner(ctx, err, "failed to listen on ", address)
return
}
} else { // tcp
@ -182,7 +182,7 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
Port: int(port),
}, streamSettings.SocketSettings)
if err != nil {
newError("failed to listen on ", address, ":", port).Base(err).AtError().WriteToLog(session.ExportIDToError(ctx))
errors.LogErrorInner(ctx, err, "failed to listen on ", address, ":", port)
return
}
}
@ -193,12 +193,12 @@ func Listen(ctx context.Context, address net.Address, port net.Port, streamSetti
}
err = server.Serve(streamListener)
if err != nil {
newError("stopping serving H2C or REALITY H2").Base(err).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfoInner(ctx, err, "stopping serving H2C or REALITY H2")
}
} else {
err = server.ServeTLS(streamListener, "", "")
if err != nil {
newError("stopping serving TLS H2").Base(err).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfoInner(ctx, err, "stopping serving TLS H2")
}
}
}()