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

@ -8,8 +8,8 @@ import (
"strings"
"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/stat"
"github.com/xtls/xray-core/transport/internet/tls"
@ -34,7 +34,7 @@ func (c *ConnRF) Read(b []byte) (int, error) {
if resp.Status != "101 Switching Protocols" ||
strings.ToLower(resp.Header.Get("Upgrade")) != "websocket" ||
strings.ToLower(resp.Header.Get("Connection")) != "upgrade" {
return 0, newError("unrecognized reply")
return 0, errors.New("unrecognized reply")
}
// drain remaining bufreader
return reader.Read(b[:reader.Buffered()])
@ -47,7 +47,7 @@ func dialhttpUpgrade(ctx context.Context, dest net.Destination, streamSettings *
pconn, err := internet.DialSystem(ctx, dest, streamSettings.SocketSettings)
if err != nil {
newError("failed to dial to ", dest).Base(err).AtError().WriteToLog()
errors.LogErrorInner(ctx, err, "failed to dial to ", dest)
return nil, err
}
@ -104,19 +104,19 @@ func dialhttpUpgrade(ctx context.Context, dest net.Destination, streamSettings *
return connRF, nil
}
//http.Header.Add() will convert headers to MIME header format.
//Some people don't like this because they want to send "Web*S*ocket".
//So we add a simple function to replace that method.
// http.Header.Add() will convert headers to MIME header format.
// Some people don't like this because they want to send "Web*S*ocket".
// So we add a simple function to replace that method.
func AddHeader(header http.Header, key, value string) {
header[key] = append(header[key], value)
}
func Dial(ctx context.Context, dest net.Destination, streamSettings *internet.MemoryStreamConfig) (stat.Connection, error) {
newError("creating connection to ", dest).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "creating connection to ", dest)
conn, err := dialhttpUpgrade(ctx, dest, streamSettings)
if err != nil {
return nil, newError("failed to dial request to ", dest).Base(err)
return nil, errors.New("failed to dial request to ", dest).Base(err)
}
return stat.Connection(conn), nil
}

View file

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

@ -4,6 +4,7 @@ import (
"context"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
)
//go:generate go run github.com/xtls/xray-core/common/errors/errorgen
@ -12,6 +13,6 @@ const protocolName = "httpupgrade"
func init() {
common.Must(common.RegisterConfig((*Config)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
return nil, newError("httpupgrade is a transport protocol.")
return nil, errors.New("httpupgrade is a transport protocol.")
}))
}

View file

@ -8,9 +8,9 @@ import (
"strings"
"github.com/xtls/xray-core/common"
"github.com/xtls/xray-core/common/errors"
"github.com/xtls/xray-core/common/net"
http_proto "github.com/xtls/xray-core/common/protocol/http"
"github.com/xtls/xray-core/common/session"
"github.com/xtls/xray-core/transport/internet"
"github.com/xtls/xray-core/transport/internet/stat"
v2tls "github.com/xtls/xray-core/transport/internet/tls"
@ -40,11 +40,11 @@ func (s *server) Handle(conn net.Conn) (stat.Connection, error) {
if s.config != nil {
host := req.Host
if len(s.config.Host) > 0 && host != s.config.Host {
return nil, newError("bad host: ", host)
return nil, errors.New("bad host: ", host)
}
path := s.config.GetNormalizedPath()
if req.URL.Path != path {
return nil, newError("bad path: ", req.URL.Path)
return nil, errors.New("bad path: ", req.URL.Path)
}
}
@ -52,7 +52,7 @@ func (s *server) Handle(conn net.Conn) (stat.Connection, error) {
upgrade := strings.ToLower(req.Header.Get("Upgrade"))
if connection != "upgrade" || upgrade != "websocket" {
_ = conn.Close()
return nil, newError("unrecognized request")
return nil, errors.New("unrecognized request")
}
resp := &http.Response{
Status: "101 Switching Protocols",
@ -90,7 +90,7 @@ func (s *server) keepAccepting() {
}
handledConn, err := s.Handle(conn)
if err != nil {
newError("failed to handle request").Base(err).WriteToLog()
errors.LogInfoInner(context.Background(), err, "failed to handle request")
continue
}
s.addConn(handledConn)
@ -113,22 +113,22 @@ func ListenHTTPUpgrade(ctx context.Context, address net.Address, port net.Port,
Net: "unix",
}, streamSettings.SocketSettings)
if err != nil {
return nil, newError("failed to listen unix domain socket(for HttpUpgrade) on ", address).Base(err)
return nil, errors.New("failed to listen unix domain socket(for HttpUpgrade) on ", address).Base(err)
}
newError("listening unix domain socket(for HttpUpgrade) on ", address).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "listening unix domain socket(for HttpUpgrade) on ", address)
} else { // tcp
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(for HttpUpgrade) on ", address, ":", port).Base(err)
return nil, errors.New("failed to listen TCP(for HttpUpgrade) on ", address, ":", port).Base(err)
}
newError("listening TCP(for HttpUpgrade) on ", address, ":", port).WriteToLog(session.ExportIDToError(ctx))
errors.LogInfo(ctx, "listening TCP(for HttpUpgrade) 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")
}
if config := v2tls.ConfigFromStreamSettings(streamSettings); config != nil {