mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-04-29 16:58:34 +00:00
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:
parent
8320732743
commit
079d0bd8a9
291 changed files with 1837 additions and 2368 deletions
|
@ -17,6 +17,7 @@ import (
|
|||
"github.com/xtls/xray-core/common/crypto"
|
||||
"github.com/xtls/xray-core/common/dice"
|
||||
"github.com/xtls/xray-core/common/drain"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/protocol"
|
||||
"github.com/xtls/xray-core/proxy/vmess"
|
||||
vmessaead "github.com/xtls/xray-core/proxy/vmess/aead"
|
||||
|
@ -53,7 +54,7 @@ func NewClientSession(ctx context.Context, behaviorSeed int64) *ClientSession {
|
|||
var err error
|
||||
session.readDrainer, err = drain.NewBehaviorSeedLimitedDrainer(behaviorSeed, 18, 3266, 64)
|
||||
if err != nil {
|
||||
newError("unable to initialize drainer").Base(err).WriteToLog()
|
||||
errors.LogInfoInner(ctx, err, "unable to initialize drainer")
|
||||
session.readDrainer = drain.NewNopDrainer()
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +80,7 @@ func (c *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
|
|||
|
||||
if header.Command != protocol.RequestCommandMux {
|
||||
if err := addrParser.WriteAddressPort(buffer, header.Address, header.Port); err != nil {
|
||||
return newError("failed to writer address and port").Base(err)
|
||||
return errors.New("failed to writer address and port").Base(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -112,7 +113,7 @@ func (c *ClientSession) EncodeRequestBody(request *protocol.RequestHeader, write
|
|||
var ok bool
|
||||
padding, ok = sizeParser.(crypto.PaddingLengthGenerator)
|
||||
if !ok {
|
||||
return nil, newError("invalid option: RequestOptionGlobalPadding")
|
||||
return nil, errors.New("invalid option: RequestOptionGlobalPadding")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -173,7 +174,7 @@ func (c *ClientSession) EncodeRequestBody(request *protocol.RequestHeader, write
|
|||
}
|
||||
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, request.Command.TransferType(), padding), nil
|
||||
default:
|
||||
return nil, newError("invalid option: Security")
|
||||
return nil, errors.New("invalid option: Security")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -190,12 +191,12 @@ func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
|
|||
|
||||
if n, err := io.ReadFull(reader, aeadEncryptedResponseHeaderLength[:]); err != nil {
|
||||
c.readDrainer.AcknowledgeReceive(n)
|
||||
return nil, drain.WithError(c.readDrainer, reader, newError("Unable to Read Header Len").Base(err))
|
||||
return nil, drain.WithError(c.readDrainer, reader, errors.New("Unable to Read Header Len").Base(err))
|
||||
} else { // nolint: golint
|
||||
c.readDrainer.AcknowledgeReceive(n)
|
||||
}
|
||||
if decryptedResponseHeaderLengthBinaryBuffer, err := aeadResponseHeaderLengthEncryptionAEAD.Open(nil, aeadResponseHeaderLengthEncryptionIV, aeadEncryptedResponseHeaderLength[:], nil); err != nil {
|
||||
return nil, drain.WithError(c.readDrainer, reader, newError("Failed To Decrypt Length").Base(err))
|
||||
return nil, drain.WithError(c.readDrainer, reader, errors.New("Failed To Decrypt Length").Base(err))
|
||||
} else { // nolint: golint
|
||||
common.Must(binary.Read(bytes.NewReader(decryptedResponseHeaderLengthBinaryBuffer), binary.BigEndian, &decryptedResponseHeaderLengthBinaryDeserializeBuffer))
|
||||
decryptedResponseHeaderLength = int(decryptedResponseHeaderLengthBinaryDeserializeBuffer)
|
||||
|
@ -211,13 +212,13 @@ func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
|
|||
|
||||
if n, err := io.ReadFull(reader, encryptedResponseHeaderBuffer); err != nil {
|
||||
c.readDrainer.AcknowledgeReceive(n)
|
||||
return nil, drain.WithError(c.readDrainer, reader, newError("Unable to Read Header Data").Base(err))
|
||||
return nil, drain.WithError(c.readDrainer, reader, errors.New("Unable to Read Header Data").Base(err))
|
||||
} else { // nolint: golint
|
||||
c.readDrainer.AcknowledgeReceive(n)
|
||||
}
|
||||
|
||||
if decryptedResponseHeaderBuffer, err := aeadResponseHeaderPayloadEncryptionAEAD.Open(nil, aeadResponseHeaderPayloadEncryptionIV, encryptedResponseHeaderBuffer, nil); err != nil {
|
||||
return nil, drain.WithError(c.readDrainer, reader, newError("Failed To Decrypt Payload").Base(err))
|
||||
return nil, drain.WithError(c.readDrainer, reader, errors.New("Failed To Decrypt Payload").Base(err))
|
||||
} else { // nolint: golint
|
||||
c.responseReader = bytes.NewReader(decryptedResponseHeaderBuffer)
|
||||
}
|
||||
|
@ -226,11 +227,11 @@ func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
|
|||
defer buffer.Release()
|
||||
|
||||
if _, err := buffer.ReadFullFrom(c.responseReader, 4); err != nil {
|
||||
return nil, newError("failed to read response header").Base(err).AtWarning()
|
||||
return nil, errors.New("failed to read response header").Base(err).AtWarning()
|
||||
}
|
||||
|
||||
if buffer.Byte(0) != c.responseHeader {
|
||||
return nil, newError("unexpected response header. Expecting ", int(c.responseHeader), " but actually ", int(buffer.Byte(0)))
|
||||
return nil, errors.New("unexpected response header. Expecting ", int(c.responseHeader), " but actually ", int(buffer.Byte(0)))
|
||||
}
|
||||
|
||||
header := &protocol.ResponseHeader{
|
||||
|
@ -243,7 +244,7 @@ func (c *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
|
|||
|
||||
buffer.Clear()
|
||||
if _, err := buffer.ReadFullFrom(c.responseReader, dataLen); err != nil {
|
||||
return nil, newError("failed to read response command").Base(err)
|
||||
return nil, errors.New("failed to read response command").Base(err)
|
||||
}
|
||||
command, err := UnmarshalCommand(cmdID, buffer.Bytes())
|
||||
if err == nil {
|
||||
|
@ -265,7 +266,7 @@ func (c *ClientSession) DecodeResponseBody(request *protocol.RequestHeader, read
|
|||
var ok bool
|
||||
padding, ok = sizeParser.(crypto.PaddingLengthGenerator)
|
||||
if !ok {
|
||||
return nil, newError("invalid option: RequestOptionGlobalPadding")
|
||||
return nil, errors.New("invalid option: RequestOptionGlobalPadding")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -328,7 +329,7 @@ func (c *ClientSession) DecodeResponseBody(request *protocol.RequestHeader, read
|
|||
}
|
||||
return crypto.NewAuthenticationReader(auth, sizeParser, reader, request.Command.TransferType(), padding), nil
|
||||
default:
|
||||
return nil, newError("invalid option: Security")
|
||||
return nil, errors.New("invalid option: Security")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/xtls/xray-core/common"
|
||||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/protocol"
|
||||
"github.com/xtls/xray-core/common/serial"
|
||||
|
@ -13,11 +14,11 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
ErrCommandTooLarge = newError("Command too large.")
|
||||
ErrCommandTypeMismatch = newError("Command type mismatch.")
|
||||
ErrInvalidAuth = newError("Invalid auth.")
|
||||
ErrInsufficientLength = newError("Insufficient length.")
|
||||
ErrUnknownCommand = newError("Unknown command.")
|
||||
ErrCommandTooLarge = errors.New("Command too large.")
|
||||
ErrCommandTypeMismatch = errors.New("Command type mismatch.")
|
||||
ErrInvalidAuth = errors.New("Invalid auth.")
|
||||
ErrInsufficientLength = errors.New("Insufficient length.")
|
||||
ErrUnknownCommand = errors.New("Unknown command.")
|
||||
)
|
||||
|
||||
func MarshalCommand(command interface{}, writer io.Writer) error {
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package encoding
|
||||
|
||||
import "github.com/xtls/xray-core/common/errors"
|
||||
|
||||
type errPathObjHolder struct{}
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).WithPathObj(errPathObjHolder{})
|
||||
}
|
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/xtls/xray-core/common/buf"
|
||||
"github.com/xtls/xray-core/common/crypto"
|
||||
"github.com/xtls/xray-core/common/drain"
|
||||
"github.com/xtls/xray-core/common/errors"
|
||||
"github.com/xtls/xray-core/common/net"
|
||||
"github.com/xtls/xray-core/common/protocol"
|
||||
"github.com/xtls/xray-core/common/task"
|
||||
|
@ -75,7 +76,7 @@ func (h *SessionHistory) removeExpiredEntries() error {
|
|||
defer h.Unlock()
|
||||
|
||||
if len(h.cache) == 0 {
|
||||
return newError("nothing to do")
|
||||
return errors.New("nothing to do")
|
||||
}
|
||||
|
||||
for session, expire := range h.cache {
|
||||
|
@ -130,7 +131,7 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader, isDrain bool) (*pr
|
|||
|
||||
drainer, err := drain.NewBehaviorSeedLimitedDrainer(int64(s.userValidator.GetBehaviorSeed()), 16+38, 3266, 64)
|
||||
if err != nil {
|
||||
return nil, newError("failed to initialize drainer").Base(err)
|
||||
return nil, errors.New("failed to initialize drainer").Base(err)
|
||||
}
|
||||
|
||||
drainConnection := func(e error) error {
|
||||
|
@ -147,7 +148,7 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader, isDrain bool) (*pr
|
|||
}()
|
||||
|
||||
if _, err := buffer.ReadFullFrom(reader, protocol.IDBytesLen); err != nil {
|
||||
return nil, newError("failed to read request header").Base(err)
|
||||
return nil, errors.New("failed to read request header").Base(err)
|
||||
}
|
||||
|
||||
var decryptor io.Reader
|
||||
|
@ -167,20 +168,20 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader, isDrain bool) (*pr
|
|||
if errorReason != nil {
|
||||
if shouldDrain {
|
||||
drainer.AcknowledgeReceive(bytesRead)
|
||||
return nil, drainConnection(newError("AEAD read failed").Base(errorReason))
|
||||
return nil, drainConnection(errors.New("AEAD read failed").Base(errorReason))
|
||||
} else {
|
||||
return nil, drainConnection(newError("AEAD read failed, drain skipped").Base(errorReason))
|
||||
return nil, drainConnection(errors.New("AEAD read failed, drain skipped").Base(errorReason))
|
||||
}
|
||||
}
|
||||
decryptor = bytes.NewReader(aeadData)
|
||||
default:
|
||||
return nil, drainConnection(newError("invalid user").Base(errorAEAD))
|
||||
return nil, drainConnection(errors.New("invalid user").Base(errorAEAD))
|
||||
}
|
||||
|
||||
drainer.AcknowledgeReceive(int(buffer.Len()))
|
||||
buffer.Clear()
|
||||
if _, err := buffer.ReadFullFrom(decryptor, 38); err != nil {
|
||||
return nil, newError("failed to read request header").Base(err)
|
||||
return nil, errors.New("failed to read request header").Base(err)
|
||||
}
|
||||
|
||||
request := &protocol.RequestHeader{
|
||||
|
@ -195,7 +196,7 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader, isDrain bool) (*pr
|
|||
sid.key = s.requestBodyKey
|
||||
sid.nonce = s.requestBodyIV
|
||||
if !s.sessionHistory.addIfNotExits(sid) {
|
||||
return nil, newError("duplicated session id, possibly under replay attack, but this is a AEAD request")
|
||||
return nil, errors.New("duplicated session id, possibly under replay attack, but this is a AEAD request")
|
||||
}
|
||||
|
||||
s.responseHeader = buffer.Byte(33) // 1 byte
|
||||
|
@ -219,12 +220,12 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader, isDrain bool) (*pr
|
|||
|
||||
if paddingLen > 0 {
|
||||
if _, err := buffer.ReadFullFrom(decryptor, int32(paddingLen)); err != nil {
|
||||
return nil, newError("failed to read padding").Base(err)
|
||||
return nil, errors.New("failed to read padding").Base(err)
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := buffer.ReadFullFrom(decryptor, 4); err != nil {
|
||||
return nil, newError("failed to read checksum").Base(err)
|
||||
return nil, errors.New("failed to read checksum").Base(err)
|
||||
}
|
||||
|
||||
fnv1a := fnv.New32a()
|
||||
|
@ -233,15 +234,15 @@ func (s *ServerSession) DecodeRequestHeader(reader io.Reader, isDrain bool) (*pr
|
|||
expectedHash := binary.BigEndian.Uint32(buffer.BytesFrom(-4))
|
||||
|
||||
if actualHash != expectedHash {
|
||||
return nil, newError("invalid auth, but this is a AEAD request")
|
||||
return nil, errors.New("invalid auth, but this is a AEAD request")
|
||||
}
|
||||
|
||||
if request.Address == nil {
|
||||
return nil, newError("invalid remote address")
|
||||
return nil, errors.New("invalid remote address")
|
||||
}
|
||||
|
||||
if request.Security == protocol.SecurityType_UNKNOWN || request.Security == protocol.SecurityType_AUTO {
|
||||
return nil, newError("unknown security type: ", request.Security)
|
||||
return nil, errors.New("unknown security type: ", request.Security)
|
||||
}
|
||||
|
||||
return request, nil
|
||||
|
@ -258,7 +259,7 @@ func (s *ServerSession) DecodeRequestBody(request *protocol.RequestHeader, reade
|
|||
var ok bool
|
||||
padding, ok = sizeParser.(crypto.PaddingLengthGenerator)
|
||||
if !ok {
|
||||
return nil, newError("invalid option: RequestOptionGlobalPadding")
|
||||
return nil, errors.New("invalid option: RequestOptionGlobalPadding")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -321,7 +322,7 @@ func (s *ServerSession) DecodeRequestBody(request *protocol.RequestHeader, reade
|
|||
return crypto.NewAuthenticationReader(auth, sizeParser, reader, request.Command.TransferType(), padding), nil
|
||||
|
||||
default:
|
||||
return nil, newError("invalid option: Security")
|
||||
return nil, errors.New("invalid option: Security")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -382,7 +383,7 @@ func (s *ServerSession) EncodeResponseBody(request *protocol.RequestHeader, writ
|
|||
var ok bool
|
||||
padding, ok = sizeParser.(crypto.PaddingLengthGenerator)
|
||||
if !ok {
|
||||
return nil, newError("invalid option: RequestOptionGlobalPadding")
|
||||
return nil, errors.New("invalid option: RequestOptionGlobalPadding")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -445,6 +446,6 @@ func (s *ServerSession) EncodeResponseBody(request *protocol.RequestHeader, writ
|
|||
return crypto.NewAuthenticationWriter(auth, sizeParser, writer, request.Command.TransferType(), padding), nil
|
||||
|
||||
default:
|
||||
return nil, newError("invalid option: Security")
|
||||
return nil, errors.New("invalid option: Security")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue