diff --git a/pkg/gotd/mtproxy/faketls/server_hello.go b/pkg/gotd/mtproxy/faketls/server_hello.go index ff148dad..abda1f48 100644 --- a/pkg/gotd/mtproxy/faketls/server_hello.go +++ b/pkg/gotd/mtproxy/faketls/server_hello.go @@ -4,11 +4,20 @@ import ( "bytes" "crypto/hmac" "crypto/sha256" + "encoding/hex" "io" "github.com/go-faster/errors" ) +// peekDump returns up to n bytes from the start of buf as a hex string for diagnostics. +func peekDump(buf []byte, n int) string { + if len(buf) < n { + n = len(buf) + } + return hex.EncodeToString(buf[:n]) +} + // readServerHello reads faketls ServerHello. func readServerHello(r io.Reader, clientRandom [32]byte, secret []byte) error { packetBuf := bytes.NewBuffer(nil) @@ -16,10 +25,11 @@ func readServerHello(r io.Reader, clientRandom [32]byte, secret []byte) error { handshake, err := readRecord(r) if err != nil { - return errors.Wrap(err, "handshake record") + return errors.Wrapf(err, "handshake record (peek=%s)", peekDump(packetBuf.Bytes(), 32)) } if handshake.Type != RecordTypeHandshake { - return errors.Wrap(err, "unexpected record type") + return errors.Errorf("unexpected handshake record type: got 0x%02x, want 0x%02x (peek=%s)", + byte(handshake.Type), byte(RecordTypeHandshake), peekDump(packetBuf.Bytes(), 32)) } changeCipher, err := readRecord(r) @@ -27,7 +37,8 @@ func readServerHello(r io.Reader, clientRandom [32]byte, secret []byte) error { return errors.Wrap(err, "change cipher record") } if changeCipher.Type != RecordTypeChangeCipherSpec { - return errors.Wrap(err, "unexpected record type") + return errors.Errorf("unexpected change cipher record type: got 0x%02x, want 0x%02x", + byte(changeCipher.Type), byte(RecordTypeChangeCipherSpec)) } cert, err := readRecord(r) @@ -35,7 +46,8 @@ func readServerHello(r io.Reader, clientRandom [32]byte, secret []byte) error { return errors.Wrap(err, "cert record") } if cert.Type != RecordTypeApplication { - return errors.Wrap(err, "unexpected record type") + return errors.Errorf("unexpected application record type: got 0x%02x, want 0x%02x", + byte(cert.Type), byte(RecordTypeApplication)) } // `$record_header = type 1 byte + version 2 bytes + payload_length 2 bytes = 5 bytes`