Upload test 1

This commit is contained in:
Penguinehis
2026-09-29 10:05:43 -03:00
commit b4a8a70e54
96 changed files with 41273 additions and 0 deletions
File diff suppressed because it is too large Load Diff
+84
View File
@@ -0,0 +1,84 @@
package hcr
import "encoding/binary"
// Verdict is the result of inspecting the first bytes of a connection.
type Verdict int
const (
// VerdictNo means the bytes cannot begin an HCR request.
VerdictNo Verdict = iota
// VerdictMore means the bytes so far are consistent with HCR but more are
// needed to decide. The caller should peek further and ask again.
VerdictMore
// VerdictYes means the bytes begin a valid HCR request header.
VerdictYes
)
// SniffLimit is the largest number of bytes SniffFrame ever needs: the full
// fixed request header.
const SniffLimit = RequestHeaderSize
// IsHeader reports whether buf is a complete, valid 62-byte HCR request header.
// This is the strict check the coexistence design relies on: only a full header
// with the fixed protocol version and an in-range payload length counts.
func IsHeader(buf []byte) bool {
if len(buf) < RequestHeaderSize {
return false
}
code := buf[0]
if code < 1 || code > 6 {
return false
}
if buf[1] != ProtoVersion {
return false
}
if binary.BigEndian.Uint32(buf[58:62]) > MaxPayloadLimit {
return false
}
return true
}
// SniffFrame reports whether data begins an HCR request, so a port shared with
// BHTTP (and SSH, HTTP injection) can route the connection without consuming
// anything.
//
// HCR and BHTTP deliberately overlap on the first byte (opcodes 1..4 mean
// upload/download/… in both). The discriminator is HCR's fixed protocol version
// at byte [1] (always 0x01) and its 62-byte header layout, checked in full:
//
// - byte 0 must be an HCR opcode (1..6); anything else (including BHTTP's
// probe 0x00) is not HCR.
// - byte 1 must be 0x01. A BHTTP frame's byte [1] is the first byte of its
// random session UUID, so it is 0x01 only by chance — and that is the only
// case where HCR waits for the full 62 bytes. A "clean" BHTTP frame is
// rejected here at two bytes, with no stall, and falls through to BHTTP.
// - the full 62-byte header must pass IsHeader. A BHTTP frame whose UUID
// happens to start 0x01 almost never has a valid HCR payload length at
// [58:62], so it is rejected and BHTTP still handles it.
//
// The caller must try HCR before BHTTP, exactly because a real HCR Connect
// (opcode 1) also looks like a BHTTP upload on the first byte.
func SniffFrame(data []byte) Verdict {
if len(data) == 0 {
return VerdictMore
}
if data[0] < 1 || data[0] > 6 {
return VerdictNo
}
if len(data) < 2 {
return VerdictMore
}
if data[1] != ProtoVersion {
// Not HCR: a BHTTP frame or junk. Reject now so BHTTP gets its fast
// 29-byte path with no wait for 62 bytes.
return VerdictNo
}
if len(data) < RequestHeaderSize {
return VerdictMore
}
if IsHeader(data[:RequestHeaderSize]) {
return VerdictYes
}
return VerdictNo
}
+83
View File
@@ -0,0 +1,83 @@
package hcr
import (
"encoding/binary"
"testing"
)
// hcrHeader builds a valid 62-byte request header.
func hcrHeader(code, version byte, payloadLen uint32) []byte {
b := make([]byte, RequestHeaderSize)
b[0] = code
b[1] = version
b[2] = 0xAB // session id byte
binary.BigEndian.PutUint32(b[58:62], payloadLen)
return b
}
func TestSniffRejectsOtherProtocols(t *testing.T) {
cases := map[string]string{
"SSH": "SSH-2.0-x\r\n",
"HTTP GET": "GET / HTTP/1.1\r\n",
"BTUN": "DTUNNEL/1.1 CLIENT_HELLO\r\n",
"BHTTP probe": "\x00\x11\x22\x33",
"TLS hello": "\x16\x03\x01\x00",
}
for name, in := range cases {
if got := SniffFrame([]byte(in)); got != VerdictNo {
t.Fatalf("%s: SniffFrame = %v, want VerdictNo", name, got)
}
}
}
func TestSniffAcceptsValidHeaders(t *testing.T) {
for code := byte(1); code <= 6; code++ {
if got := SniffFrame(hcrHeader(code, ProtoVersion, 100)); got != VerdictYes {
t.Fatalf("code %d: SniffFrame = %v, want VerdictYes", code, got)
}
}
}
// A BHTTP frame whose session UUID happens to start 0x01 must NOT be stolen:
// its bytes at [58:62] are not a valid HCR payload length.
func TestSniffRejectsBhttpLookalike(t *testing.T) {
b := make([]byte, RequestHeaderSize)
b[0] = 0x01 // BHTTP upload / HCR connect
b[1] = 0x01 // UUID happens to start 0x01
binary.BigEndian.PutUint32(b[58:62], MaxPayloadLimit+1) // invalid HCR len
if got := SniffFrame(b); got != VerdictNo {
t.Fatalf("lookalike: SniffFrame = %v, want VerdictNo", got)
}
}
// A clean BHTTP frame (byte1 != 0x01) is rejected at two bytes — no 62-byte wait.
func TestSniffFastRejectsCleanBhttp(t *testing.T) {
if got := SniffFrame([]byte{0x01, 0x7A}); got != VerdictNo {
t.Fatalf("clean BHTTP: SniffFrame(2B) = %v, want VerdictNo", got)
}
}
func TestSniffProgressive(t *testing.T) {
full := hcrHeader(1, ProtoVersion, 10)
if got := SniffFrame(nil); got != VerdictMore {
t.Fatalf("empty = %v, want More", got)
}
if got := SniffFrame(full[:1]); got != VerdictMore {
t.Fatalf("1 byte = %v, want More", got)
}
if got := SniffFrame(full[:2]); got != VerdictMore {
t.Fatalf("2 bytes (ver=1) = %v, want More", got)
}
if got := SniffFrame(full[:RequestHeaderSize-1]); got != VerdictMore {
t.Fatalf("61 bytes = %v, want More", got)
}
if got := SniffFrame(full); got != VerdictYes {
t.Fatalf("full = %v, want Yes", got)
}
}
func TestSniffRejectsWrongVersion(t *testing.T) {
if got := SniffFrame(hcrHeader(1, 0x02, 10)); got != VerdictNo {
t.Fatalf("version 2 = %v, want No", got)
}
}