diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/data.go | 33 | ||||
| -rw-r--r-- | core/errors.go | 13 | ||||
| -rw-r--r-- | core/payload.go | 34 |
3 files changed, 57 insertions, 23 deletions
diff --git a/core/data.go b/core/data.go index a96b56f..dd2777f 100644 --- a/core/data.go +++ b/core/data.go @@ -16,13 +16,14 @@ type Wrapper interface { type PayloadType uint8 const ( - PayloadUnknown PayloadType = 0x00 - PayloadSuccess = 0x01 - PayloadError = 0x02 - PayloadHandshake = 0x03 - PayloadAuth = 0x04 - PayloadMessage = 0x05 - PayloadTest = 0xFF + PayloadUnknown PayloadType = 0x00 + PayloadSuccess = 0x01 + PayloadError = 0x02 + PayloadHandshake = 0x03 + PayloadUserAuth = 0x04 + PayloadMessage = 0x05 + PayloadServerAuth = 0x06 + PayloadTest = 0xFF ) type ErrorType uint8 @@ -33,6 +34,14 @@ const ( ErrorNotFound = 0x02 ) +type ConnType uint8 + +const ( + ConnTypeUnknown ConnType = 0x00 + ConnTypeUser = 0x01 + ConnTypeServer = 0x02 +) + type Frame struct { Length uint16 Type PayloadType @@ -99,7 +108,7 @@ func Decode(buf io.Reader) (any, error) { err := binary.Read(buf, binary.BigEndian, &pTypeByte) if err != nil { - return nil, fmt.Errorf("cannot read payload type: %v", err) + return nil, fmt.Errorf("cannot read payload type: %w", err) } pType := PayloadType(pTypeByte) @@ -107,7 +116,7 @@ func Decode(buf io.Reader) (any, error) { var pLen uint16 err = binary.Read(buf, binary.BigEndian, &pLen) if err != nil { - return nil, fmt.Errorf("cannot read payload length: %v", err) + return nil, fmt.Errorf("cannot read payload length: %w", err) } switch pType { @@ -117,8 +126,10 @@ func Decode(buf io.Reader) (any, error) { return DecodeError(buf) case PayloadHandshake: return DecodeHandshake(buf) - case PayloadAuth: - return DecodeAuth(buf) + case PayloadUserAuth: + return DecodeUserAuth(buf) + case PayloadServerAuth: + return DecodeServerAuth(buf) case PayloadMessage: return DecodeMessage(buf) case PayloadTest: diff --git a/core/errors.go b/core/errors.go index a50fbf7..ed6702d 100644 --- a/core/errors.go +++ b/core/errors.go @@ -3,10 +3,11 @@ package core import "errors" var ( - ErrUnexpectedPayloadType = errors.New("unexpected payload type") - ErrAuthInvalidUser = errors.New("invalid user") - ErrAuthInvalidPassword = errors.New("invalid password") - ErrInvalidAddress = errors.New("invalid address") - ErrNotSupported = errors.New("not supported") - ErrDisconnected = errors.New("disconnected") + ErrUnexpectedPayloadType = errors.New("unexpected payload type") + ErrAuthInvalidUser = errors.New("invalid user") + ErrAuthInvalidPassword = errors.New("invalid password") + ErrAuthReverseLookupFailed = errors.New("declared name does not match with DNS name") + ErrInvalidAddress = errors.New("invalid address") + ErrNotSupported = errors.New("not supported") + ErrDisconnected = errors.New("disconnected") ) diff --git a/core/payload.go b/core/payload.go index b8e8b9f..e943647 100644 --- a/core/payload.go +++ b/core/payload.go @@ -36,11 +36,12 @@ func DecodeError(buf io.Reader) (Error, error) { type Handshake struct { Major, Minor uint8 + ConnType ConnType } func (h Handshake) Wrap() (PayloadType, []any) { return PayloadHandshake, []any{ - h.Major, h.Minor, + h.Major, h.Minor, h.ConnType, } } @@ -57,22 +58,27 @@ func DecodeHandshake(buf io.Reader) (Handshake, error) { return h, err } + err = decodeNumeric(buf, &h.ConnType) + if err != nil { + return h, err + } + return h, nil } -type Auth struct { +type UserAuth struct { Name string Pass string } -func (a Auth) Wrap() (PayloadType, []any) { - return PayloadAuth, []any{ +func (a UserAuth) Wrap() (PayloadType, []any) { + return PayloadUserAuth, []any{ a.Name, a.Pass, } } -func DecodeAuth(buf io.Reader) (Auth, error) { - var a Auth +func DecodeUserAuth(buf io.Reader) (UserAuth, error) { + var a UserAuth err := decodeString(buf, &a.Name) if err != nil { return a, err @@ -86,6 +92,22 @@ func DecodeAuth(buf io.Reader) (Auth, error) { return a, nil } +type ServerAuth struct { + Name string +} + +func (a ServerAuth) Wrap() (PayloadType, []any) { + return PayloadServerAuth, []any{ + a.Name, + } +} + +func DecodeServerAuth(buf io.Reader) (ServerAuth, error) { + var a ServerAuth + err := decodeString(buf, &a.Name) + return a, err +} + type Message struct { Source string Target string |
