More server error messages

This commit is contained in:
世界 2022-05-23 20:01:29 +08:00
parent 1843d16569
commit 8697b84d59
No known key found for this signature in database
GPG key ID: CD109927C34A63C4
4 changed files with 35 additions and 19 deletions

View file

@ -9,24 +9,36 @@ import (
"syscall" "syscall"
) )
type exception struct { type causeError struct {
message string message string
cause error cause error
} }
func (e exception) Error() string { func (e *causeError) Error() string {
if e.cause == nil { if e.cause == nil {
return e.message return e.message
} }
return e.message + ": " + e.cause.Error() return e.message + ": " + e.cause.Error()
} }
func (e exception) Unwrap() error { func (e *causeError) Unwrap() error {
return e.cause return e.cause
} }
func (e exception) Is(err error) bool { type extendedError struct {
return e == err || errors.Is(e.cause, err) message string
cause error
}
func (e *extendedError) Error() string {
if e.cause == nil {
return e.message
}
return e.cause.Error() + e.message
}
func (e *extendedError) Unwrap() error {
return e.cause
} }
func New(message ...any) error { func New(message ...any) error {
@ -34,7 +46,11 @@ func New(message ...any) error {
} }
func Cause(cause error, message ...any) error { func Cause(cause error, message ...any) error {
return exception{fmt.Sprint(message...), cause} return &causeError{fmt.Sprint(message...), cause}
}
func Extend(cause error, message ...any) error {
return &extendedError{fmt.Sprint(message...), cause}
} }
func IsClosed(err error) bool { func IsClosed(err error) bool {

View file

@ -317,7 +317,7 @@ func (c *clientConn) readResponse() error {
return err return err
} }
if headerType != HeaderTypeServer { if headerType != HeaderTypeServer {
return ErrBadHeaderType return E.Extend(ErrBadHeaderType, "expected ", HeaderTypeServer, ", got ", headerType)
} }
var epoch uint64 var epoch uint64
@ -328,7 +328,7 @@ func (c *clientConn) readResponse() error {
diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch)))) diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch))))
if diff > 30 { if diff > 30 {
return ErrBadTimestamp return E.Extend(ErrBadTimestamp, "received ", epoch, ", diff ", diff, "s")
} }
_requestSalt := buf.Make(c.keySaltLength) _requestSalt := buf.Make(c.keySaltLength)
@ -525,7 +525,7 @@ func (c *clientPacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
return M.Socksaddr{}, err return M.Socksaddr{}, err
} }
if headerType != HeaderTypeServer { if headerType != HeaderTypeServer {
return M.Socksaddr{}, ErrBadHeaderType return M.Socksaddr{}, E.Extend(ErrBadHeaderType, "expected ", HeaderTypeServer, ", got ", headerType)
} }
var epoch uint64 var epoch uint64
@ -536,7 +536,7 @@ func (c *clientPacketConn) ReadPacket(buffer *buf.Buffer) (M.Socksaddr, error) {
diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch)))) diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch))))
if diff > 30 { if diff > 30 {
return M.Socksaddr{}, ErrBadTimestamp return M.Socksaddr{}, E.Extend(ErrBadTimestamp, "received ", epoch, ", diff ", diff, "s")
} }
if sessionId == c.session.remoteSessionId { if sessionId == c.session.remoteSessionId {

View file

@ -138,7 +138,7 @@ func (s *Service) newConnection(ctx context.Context, conn net.Conn, metadata M.M
err = reader.ReadChunk(header[s.keySaltLength:]) err = reader.ReadChunk(header[s.keySaltLength:])
if err != nil { if err != nil {
return E.Cause(err, "read request fixed length chunk") return err
} }
headerType, err := reader.ReadByte() headerType, err := reader.ReadByte()
@ -147,7 +147,7 @@ func (s *Service) newConnection(ctx context.Context, conn net.Conn, metadata M.M
} }
if headerType != HeaderTypeClient { if headerType != HeaderTypeClient {
return ErrBadHeaderType return E.Extend(ErrBadHeaderType, "expected ", HeaderTypeClient, ", got ", headerType)
} }
var epoch uint64 var epoch uint64
@ -158,7 +158,7 @@ func (s *Service) newConnection(ctx context.Context, conn net.Conn, metadata M.M
diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch)))) diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch))))
if diff > 30 { if diff > 30 {
return ErrBadTimestamp return E.Extend(ErrBadTimestamp, "received ", epoch, ", diff ", diff, "s")
} }
var length uint16 var length uint16
@ -356,7 +356,7 @@ process:
goto returnErr goto returnErr
} }
if headerType != HeaderTypeClient { if headerType != HeaderTypeClient {
err = ErrBadHeaderType err = E.Extend(ErrBadHeaderType, "expected ", HeaderTypeClient, ", got ", headerType)
goto returnErr goto returnErr
} }
@ -367,7 +367,7 @@ process:
} }
diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch)))) diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch))))
if diff > 30 { if diff > 30 {
err = ErrBadTimestamp err = E.Extend(ErrBadTimestamp, "received ", epoch, ", diff ", diff, "s")
goto returnErr goto returnErr
} }

View file

@ -145,7 +145,7 @@ func (s *MultiService[U]) newConnection(ctx context.Context, conn net.Conn, meta
} }
if headerType != HeaderTypeClient { if headerType != HeaderTypeClient {
return ErrBadHeaderType return E.Extend(ErrBadHeaderType, "expected ", HeaderTypeClient, ", got ", headerType)
} }
var epoch uint64 var epoch uint64
@ -155,7 +155,7 @@ func (s *MultiService[U]) newConnection(ctx context.Context, conn net.Conn, meta
} }
diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch)))) diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch))))
if diff > 30 { if diff > 30 {
return ErrBadTimestamp return E.Extend(ErrBadTimestamp, "received ", epoch, ", diff ", diff, "s")
} }
var length uint16 var length uint16
err = binary.Read(reader, binary.BigEndian, &length) err = binary.Read(reader, binary.BigEndian, &length)
@ -284,7 +284,7 @@ process:
goto returnErr goto returnErr
} }
if headerType != HeaderTypeClient { if headerType != HeaderTypeClient {
err = ErrBadHeaderType err = E.Extend(ErrBadHeaderType, "expected ", HeaderTypeClient, ", got ", headerType)
goto returnErr goto returnErr
} }
@ -295,7 +295,7 @@ process:
} }
diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch)))) diff := int(math.Abs(float64(time.Now().Unix() - int64(epoch))))
if diff > 30 { if diff > 30 {
err = ErrBadTimestamp err = E.Extend(ErrBadTimestamp, "received ", epoch, ", diff ", diff, "s")
goto returnErr goto returnErr
} }