feat: 完善堡垒机与 Agent 智能运维能力

- 支持 Windows Agent 构建、安装脚本和统一上报协议

- 增强 RDP 剪贴板、虚拟盘和文件传输能力

- 新增 AIOps 上下文工具、告警 RCA trace 落库和回放接口

- 重构 docs 模块文档并补充终端、堡垒机、监控、AI Agent 设计
This commit is contained in:
Logan.Li committed 2026-07-05 23:49:36 +08:00
1 parent a9513c2056
commit e0d652d211
169 files changed
+12192 -9116

No files matched your search

+422
View File
@@ -0,0 +1,422 @@
package cliprdr
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"strings"
"sync"
"unicode/utf16"
)
// Sender 负责把 CLIPRDR PDU 通过 MCS SendDataRequest 发往指定 channel ID。
// 由协议栈注入(通常封装 buildMCSSendDataRequest + transport.WriteX224Data + writeMu)。// Logan.Li
type Sender interface {
SendChannelPDU(channelID uint16, data []byte) error
}
// Handler 实现 RDP 剪贴板虚拟通道(CF_UNICODETEXT / CF_TEXT 文本格式)。
//
// - onRemote: 远端剪贴板文本到达时回调(协议栈应转成 WS type=clipboard 帧推给浏览器)
// - getLocal: 远端请求本地剪贴板时回调取值(协议栈从 inputLoop 维护的本地剪贴板缓存读取)
//
// 两个回调都是线程安全的调用点:Handler 内部用 mu 保护自身状态,
// 调用方负责 Sender 的并发写(writeMu)。
type Handler struct {
sender Sender
channelID uint16
mu sync.Mutex
useLongFormatNames bool
serverCapsReceived bool
monitorReady bool
localText string // 本地剪贴板文本(由 SetLocalText 更新,供远端请求时回填)
suppressNextChange bool // 抑制 echo 回环:远端→本地 触发的本地变更不再回推远端
onRemote func(text string) // 远端文本到达回调(可能为 nil)
initialFormatListSent bool // 初始化阶段只允许发送一次空 FormatList,匹配 FreeRDP/IronRDP 时序。// Logan.Li
}
// NewHandler 创建剪贴板处理器。
// - onRemote: 远端剪贴板内容到达时调用(转成 WS clipboard 帧推送浏览器)
// - getLocal: 已废弃,本地文本请用 SetLocalText 主动设置(浏览器同步剪贴板时写入)
func NewHandler(onRemote func(text string)) *Handler {
return &Handler{onRemote: onRemote}
}
// Attach 绑定发送通道与会话分配的 channel ID(在 MCS ChannelJoin 成功后调用)。
// 附加后主动发 CB_CLIP_CAPS 声明客户端能力(必须用 Long Format Names),
// 否则 Windows 不知道格式协商方式,会忽略后续 FormatList。 // Logan.Li
func (h *Handler) Attach(sender Sender, channelID uint16) {
h.mu.Lock()
h.sender = sender
h.channelID = channelID
h.useLongFormatNames = true // 客户端强制 Long Format Names,与发送的 caps 一致
h.mu.Unlock()
log.Printf("[cliprdr] attached channelID=%d", channelID)
}
// Init 在会话激活后主动发起剪贴板握手。
// MS-RDPECLIP 规定服务端先发 CB_CLIP_CAPS + CB_MONITOR_READY,但部分 Windows 版本
// (尤其开启了剪贴板重定向策略的)会等客户端先声明能力。主动发 caps + formatList
// 可兼容两种情况,不会产生冲突(服务端收到多余的 caps 会忽略)。// Logan.Li
func (h *Handler) Init() {
h.mu.Lock()
if h.sender == nil {
h.mu.Unlock()
return
}
monitorReady := h.monitorReady
needInitial := !h.initialFormatListSent
if needInitial {
h.initialFormatListSent = true
}
h.mu.Unlock()
if monitorReady {
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr init: server MonitorReady already handled"))
return
}
// 部分 Windows 会先等客户端声明能力;延迟主动初始化兼顾标准时序和真实服务器兼容性。// Logan.Li
h.sendClipCaps()
if needInitial {
h.sendEmptyFormatList()
}
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr init: proactive caps and initial FormatList sent"))
}
// SetLocalText 更新本地剪贴板文本(浏览器把本地剪贴板同步过来时调用)。
// 设置后会向远端发送 FormatList 通知远端"本地剪贴板有新内容",
// 除非 suppress 为 true(用于抑制远端→本地 echo 回环)。
func (h *Handler) SetLocalText(text string) {
h.mu.Lock()
h.localText = text
suppress := h.suppressNextChange
h.suppressNextChange = false
sender := h.sender
h.mu.Unlock()
if suppress {
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: local text set with suppress (echo from remote), skip FormatList"))
return
}
if sender != nil {
h.sendFormatList()
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: local text changed, sent FormatList", "len", len(text)))
}
}
// ChannelID 返回绑定的通道 ID(未 Attach 返回 0)。
func (h *Handler) ChannelID() uint16 {
h.mu.Lock()
defer h.mu.Unlock()
return h.channelID
}
// RequestRemoteText 在用户触发远端 Ctrl+C/Ctrl+X 后主动请求文本内容,
// 兼容部分 Windows 不主动发送 CB_FORMAT_LIST 的场景。// Logan.Li
func (h *Handler) RequestRemoteText() {
h.mu.Lock()
ready := h.sender != nil
h.mu.Unlock()
if !ready {
return
}
h.sendFormatDataRequest(cfUnicodeText)
}
// Process 处理从 MCS SendDataIndication 收到的 cliprdr PDU(已去掉 MCS/SEC 头的原始通道数据)。
// 由协议栈 readLoop 在识别到 cliprdr channel ID 后调用。
func (h *Handler) Process(data []byte) {
if len(data) < 8 {
return
}
msgType := binary.LittleEndian.Uint16(data[0:2])
msgFlags := binary.LittleEndian.Uint16(data[2:4])
dataLen := binary.LittleEndian.Uint32(data[4:8])
if int(dataLen) > len(data)-8 {
log.Printf("[cliprdr] Warn: %s", fmt.Sprintln("cliprdr: PDU dataLen exceeds payload", "dataLen", dataLen, "have", len(data)-8))
return
}
body := data[8 : 8+dataLen]
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr recv", "msgType", msgType, "msgFlags", msgFlags, "dataLen", dataLen))
switch msgType {
case msgClipCaps:
h.processClipCaps(body)
case msgMonitorReady:
h.processMonitorReady()
case msgFormatList:
h.processFormatList(body)
case msgFormatListResponse:
// 远端确认收到本地的 FormatList,无需动作
if msgFlags&flagResponseOK == 0 {
log.Printf("[cliprdr] Warn: %s", fmt.Sprintln("cliprdr: FormatListResponse FAIL"))
}
case msgFormatDataRequest:
h.processFormatDataRequest(body)
case msgFormatDataResponse:
h.processFormatDataResponse(body, msgFlags)
case msgLockClipdata, msgUnlockClipdata:
// 文本模式不处理锁
default:
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: unhandled msgType", "msgType", msgType))
}
}
// --- CB_CLIP_CAPS(2.2.2.1)-------------------------------------------------
func (h *Handler) processClipCaps(body []byte) {
if len(body) < 4 {
return
}
cCapSets := binary.LittleEndian.Uint16(body[0:2])
offset := 4 // cCapabilitySets(2) + pad1(2)
for i := 0; i < int(cCapSets); i++ {
if offset+4 > len(body) {
break
}
capType := binary.LittleEndian.Uint16(body[offset:])
capLen := binary.LittleEndian.Uint16(body[offset+2:])
if capType == capsTypeGeneral && capLen >= 12 && offset+12 <= len(body) {
generalFlags := binary.LittleEndian.Uint32(body[offset+8:])
h.mu.Lock()
h.useLongFormatNames = generalFlags&flagUseLongFormatNames != 0
useLong := h.useLongFormatNames
h.mu.Unlock()
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: server caps", "generalFlags", generalFlags, "longNames", useLong))
}
offset += int(capLen)
}
h.mu.Lock()
h.serverCapsReceived = true
localReady := h.localText != ""
ready := h.monitorReady
h.mu.Unlock()
// 非标准顺序:MonitorReady 先到时,在拿到 caps 后补发 FormatList
if ready && localReady {
h.sendFormatList()
}
}
// --- CB_MONITOR_READY(2.2.2.2)--------------------------------------------
func (h *Handler) processMonitorReady() {
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: server Monitor Ready"))
h.mu.Lock()
h.monitorReady = true
capsReady := h.serverCapsReceived
localReady := h.localText != ""
needInitial := !h.initialFormatListSent
if needInitial {
h.initialFormatListSent = true
}
h.mu.Unlock()
// MS-RDPECLIP §1.3.2.1: server 先发 CB_CLIP_CAPS 再发 CB_MONITOR_READY。
// 拿到 caps 才知道是否用 Long Format Names,故 FormatList 等 caps 就绪后再发。
h.sendClipCaps()
if needInitial && !localReady {
h.sendEmptyFormatList()
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: sent initial empty FormatList"))
}
if capsReady && localReady {
h.sendFormatList()
}
}
// --- Format List(2.2.3)---------------------------------------------------
func (h *Handler) sendFormatList() {
h.mu.Lock()
useLong := h.useLongFormatNames
h.initialFormatListSent = true
h.mu.Unlock()
body := &bytes.Buffer{}
if useLong {
// Long Format Name: formatId(4) + null-terminated UTF-16LE name
binary.Write(body, binary.LittleEndian, uint32(cfUnicodeText))
binary.Write(body, binary.LittleEndian, uint16(0)) // 空名 = 标准格式
binary.Write(body, binary.LittleEndian, uint32(cfText))
binary.Write(body, binary.LittleEndian, uint16(0))
} else {
// Short Format Name: formatId(4) + name[32]
binary.Write(body, binary.LittleEndian, uint32(cfUnicodeText))
body.Write(make([]byte, 32))
binary.Write(body, binary.LittleEndian, uint32(cfText))
body.Write(make([]byte, 32))
}
h.sendPDU(msgFormatList, 0, body.Bytes())
}
func (h *Handler) sendEmptyFormatList() {
// 初始化空 FormatList 是 FreeRDP 允许的首包,用来完成剪贴板客户端初始化。// Logan.Li
h.sendPDU(msgFormatList, 0, nil)
}
func (h *Handler) processFormatList(body []byte) {
formats := h.parseFormatList(body)
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: server FormatList", "formats", formats))
// 始终回复 OK
h.sendPDU(msgFormatListResponse, flagResponseOK, nil)
// 优先请求 Unicode 文本,其次 ANSI
for _, f := range formats {
if f == cfUnicodeText {
h.sendFormatDataRequest(cfUnicodeText)
return
}
}
for _, f := range formats {
if f == cfText {
h.sendFormatDataRequest(cfText)
return
}
}
}
func (h *Handler) parseFormatList(body []byte) []uint32 {
h.mu.Lock()
useLong := h.useLongFormatNames
h.mu.Unlock()
var formats []uint32
if useLong {
// Long: formatId(4) + null-term UTF-16LE name
offset := 0
for offset+4 <= len(body) {
formats = append(formats, binary.LittleEndian.Uint32(body[offset:]))
offset += 4
// 跳过 name(null-terminated UTF-16LE)
for offset+1 < len(body) {
if body[offset] == 0 && body[offset+1] == 0 {
offset += 2
break
}
offset += 2
}
}
} else {
// Short: formatId(4) + name[32] = 36 字节/项
for offset := 0; offset+36 <= len(body); offset += 36 {
formats = append(formats, binary.LittleEndian.Uint32(body[offset:]))
}
}
return formats
}
// --- Format Data Request / Response(2.2.5)-------------------------------
func (h *Handler) sendFormatDataRequest(formatID uint32) {
b := make([]byte, 4)
binary.LittleEndian.PutUint32(b, formatID)
h.sendPDU(msgFormatDataRequest, 0, b)
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: sent FormatDataRequest", "formatID", formatID))
}
func (h *Handler) processFormatDataRequest(body []byte) {
if len(body) < 4 {
h.sendPDU(msgFormatDataResponse, flagResponseFail, nil)
return
}
requestedFormat := binary.LittleEndian.Uint32(body[0:4])
h.mu.Lock()
text := h.localText
h.mu.Unlock()
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: server requests format", "formatID", requestedFormat, "localLen", len(text)))
switch requestedFormat {
case cfUnicodeText:
encoded := encodeUTF16LE(text + "\x00")
h.sendPDU(msgFormatDataResponse, flagResponseOK, encoded)
case cfText:
h.sendPDU(msgFormatDataResponse, flagResponseOK, []byte(text+"\x00"))
default:
h.sendPDU(msgFormatDataResponse, flagResponseFail, nil)
}
}
func (h *Handler) processFormatDataResponse(body []byte, msgFlags uint16) {
if msgFlags&flagResponseOK == 0 {
log.Printf("[cliprdr] Warn: %s", fmt.Sprintln("cliprdr: FormatDataResponse FAIL"))
return
}
// 尝试按 UTF-16LE 解码(CF_UNICODETEXT)
text := decodeUTF16LE(body)
text = strings.TrimRight(text, "\x00")
if text == "" {
return
}
log.Printf("[cliprdr] Debug: %s", fmt.Sprintln("cliprdr: remote clipboard text", "len", len(text)))
// 标记抑制下一次本地变更(避免 echo 回环:远端→本地触发的本地变更不再推回远端)
h.mu.Lock()
h.suppressNextChange = true
cb := h.onRemote
h.mu.Unlock()
if cb != nil {
cb(text)
}
}
// --- Send helpers --------------------------------------------------------
func (h *Handler) sendClipCaps() {
// General capability set: type(2) + length(2) + version(4) + flags(4) = 12
cap := &bytes.Buffer{}
binary.Write(cap, binary.LittleEndian, uint16(capsTypeGeneral))
binary.Write(cap, binary.LittleEndian, uint16(12))
binary.Write(cap, binary.LittleEndian, uint32(capsVersion2))
binary.Write(cap, binary.LittleEndian, uint32(flagUseLongFormatNames))
body := &bytes.Buffer{}
binary.Write(body, binary.LittleEndian, uint16(1)) // cCapabilitySets
binary.Write(body, binary.LittleEndian, uint16(0)) // pad1
body.Write(cap.Bytes())
h.sendPDU(msgClipCaps, 0, body.Bytes())
}
func (h *Handler) sendPDU(msgType, msgFlags uint16, body []byte) {
h.mu.Lock()
sender := h.sender
chID := h.channelID
h.mu.Unlock()
if sender == nil {
return
}
buf := &bytes.Buffer{}
binary.Write(buf, binary.LittleEndian, msgType)
binary.Write(buf, binary.LittleEndian, msgFlags)
binary.Write(buf, binary.LittleEndian, uint32(len(body)))
buf.Write(body)
if err := sender.SendChannelPDU(chID, buf.Bytes()); err != nil {
log.Printf("[cliprdr] Warn: %s", fmt.Sprintln("cliprdr: send PDU failed", "msgType", msgType, "err", err))
}
}
// --- UTF-16LE helpers ----------------------------------------------------
func decodeUTF16LE(b []byte) string {
if len(b) < 2 {
return ""
}
if len(b)%2 != 0 {
b = b[:len(b)-1]
}
u16 := make([]uint16, len(b)/2)
for i := range u16 {
u16[i] = binary.LittleEndian.Uint16(b[i*2:])
}
return string(utf16.Decode(u16))
}
func encodeUTF16LE(s string) []byte {
u16 := utf16.Encode([]rune(s))
b := make([]byte, len(u16)*2)
for i, v := range u16 {
binary.LittleEndian.PutUint16(b[i*2:], v)
}
return b
}
+65
View File
@@ -0,0 +1,65 @@
// Package cliprdr 实现 RDP 剪贴板虚拟通道扩展(MS-RDPECLIP)的客户端侧,
// 支持浏览器与远端 Windows 之间的双向文本剪贴板同步。// Logan.Li
//
// 协议序列(MS-RDPECLIP §1.3.2):
//
// 初始化:
// Server ── CB_CLIP_CAPS ──► Client
// Server ── CB_MONITOR_READY ──► Client
// Client ── CB_CLIP_CAPS ──► Server
// Client ── CB_FORMAT_LIST ──► Server
// Server ── CB_FORMAT_LIST_RESPONSE ──► Client
//
// 远端→本地(远端复制,本地粘贴):
// Server ── CB_FORMAT_LIST ──► Client (远端声明可用格式)
// Client ── CB_FORMAT_LIST_RESPONSE ──► Server
// Client ── CB_FORMAT_DATA_REQUEST ──► Server (本地请求文本数据)
// Server ── CB_FORMAT_DATA_RESPONSE ──► Client (远端返回剪贴板内容)
//
// 本地→远端(本地复制,远端粘贴):
// Client ── CB_FORMAT_LIST ──► Server (本地声明可用格式)
// Server ── CB_FORMAT_DATA_REQUEST ──► Client (远端请求文本数据)
// Client ── CB_FORMAT_DATA_RESPONSE ──► Server (本地返回剪贴板内容)
package cliprdr
// CLIPRDR PDU msgType(MS-RDPECLIP 2.2.1)
const (
msgMonitorReady = 0x0001
msgFormatList = 0x0002
msgFormatListResponse = 0x0003
msgFormatDataRequest = 0x0004
msgFormatDataResponse = 0x0005
msgTempDirectory = 0x0006
msgClipCaps = 0x0007
msgFilecontentsRequest = 0x0008
msgFilecontentsResponse = 0x0009
msgLockClipdata = 0x000A
msgUnlockClipdata = 0x000B
)
// msgFlags(MS-RDPECLIP 2.2.2)
const (
flagResponseOK = 0x0001
flagResponseFail = 0x0002
flagAsciiNames = 0x0004
)
// 通用能力集 GeneralFlags(MS-RDPECLIP 2.2.2.1.1.1.1)
const (
capsTypeGeneral = 0x0001
capsVersion2 = 0x00000002
flagUseLongFormatNames = 0x00000002
)
// Windows 标准剪贴板格式 ID(WinUser.h)
const (
cfText = 1 // CF_TEXT ANSI 文本
cfUnicodeText = 13 // CF_UNICODETEXT UTF-16 文本
)
// 通道声明常量(供协议栈在 MCS Connect-Initial 里声明通道时复用,避免魔法值分散)
const (
ChannelName = "cliprdr"
// ChannelOptions 与 grdp/FreeRDP 保持一致:INITIALIZED | ENCRYPT_RDP | SHOW_PROTOCOL
ChannelOptions = 0x80000000 | 0x40000000 | 0x00200000
)
+602
View File
@@ -0,0 +1,602 @@
package rdpdr
import (
"encoding/binary"
"log"
"path"
"strings"
"time"
"unicode/utf16"
)
const (
irpCreate uint32 = 0x00000000
irpClose uint32 = 0x00000002
irpRead uint32 = 0x00000003
irpWrite uint32 = 0x00000004
irpQueryInfo uint32 = 0x00000005
irpSetInfo uint32 = 0x00000006
irpDeviceCtrl uint32 = 0x0000000e
irpQueryVolume uint32 = 0x0000000a
irpDirControl uint32 = 0x0000000c
irpLockControl uint32 = 0x00000011
irpMnQueryDir uint32 = 0x00000001
irpMnNotifyChangeDir uint32 = 0x00000002
statusSuccess uint32 = 0x00000000
statusNoMoreFiles uint32 = 0x80000006
statusAccessDenied uint32 = 0xc0000022
statusObjectNameNotFound uint32 = 0xc0000034
statusNoSuchFile uint32 = 0xc000000f
statusFileIsDirectory uint32 = 0xc00000ba
statusNotSupported uint32 = 0xc00000bb
statusInvalidDeviceReq uint32 = 0xc0000010
statusInvalidParameter uint32 = 0xc000000d
statusEndOfFile uint32 = 0xc0000011
fileOpen uint32 = 0x00000001
fileCreate uint32 = 0x00000002
fileOpenIf uint32 = 0x00000003
fileOverwrite uint32 = 0x00000004
fileOverwriteIf uint32 = 0x00000005
fileDirectoryFile uint32 = 0x00000001
fileNonDirectoryFile uint32 = 0x00000040
fileDirectoryInformation uint32 = 1
fileFullDirectoryInformation uint32 = 2
fileBothDirectoryInformation uint32 = 3
fileBasicInformation uint32 = 4
fileStandardInformation uint32 = 5
fileInternalInformation uint32 = 6
fileEaInformation uint32 = 7
fileAccessInformation uint32 = 8
fileNameInformation uint32 = 9
fileNamesInformation uint32 = 12
filePositionInformation uint32 = 14
fileModeInformation uint32 = 16
fileAlignmentInformation uint32 = 17
fileEndOfFileInformation uint32 = 20
fileNetworkOpenInformation uint32 = 34
fileAttributeTagInformation uint32 = 35
fileFsVolumeInformation uint32 = 1
fileFsSizeInformation uint32 = 3
fileFsDeviceInformation uint32 = 4
fileFsAttributeInformation uint32 = 5
fileFsFullSizeInformation uint32 = 7
fileAttrDirectory uint32 = 0x00000010
fileAttrNormal uint32 = 0x00000080
fileAttrArchive uint32 = 0x00000020
fileOpened byte = 0x01
fileCreated byte = 0x02
fileOverwritten byte = 0x03
downloadDirName = "Download"
uploadDirName = "Upload"
)
const windowsFiletimeOffset = 116444736000000000
type ioRequest struct {
deviceID uint32
fileID uint32
completionID uint32
majorFn uint32
minorFn uint32
payload []byte
}
type openFile struct {
path string
name string
isDir bool
data []byte
written bool
dirEntries []dirEntry
dirIndex int
}
type dirEntry struct {
name string
isDir bool
size uint64
}
func (h *Handler) handleIORequest(body []byte) {
if len(body) < 20 {
return
}
req := ioRequest{
deviceID: binary.LittleEndian.Uint32(body[0:4]),
fileID: binary.LittleEndian.Uint32(body[4:8]),
completionID: binary.LittleEndian.Uint32(body[8:12]),
majorFn: binary.LittleEndian.Uint32(body[12:16]),
minorFn: binary.LittleEndian.Uint32(body[16:20]),
payload: body[20:],
}
log.Printf("[rdpdr] io request device=%d file=%d completion=%d major=0x%x minor=0x%x payload=%d", req.deviceID, req.fileID, req.completionID, req.majorFn, req.minorFn, len(req.payload))
switch req.majorFn {
case irpCreate:
h.handleCreate(req)
case irpClose:
h.handleClose(req)
case irpRead:
h.handleRead(req)
case irpWrite:
h.handleWrite(req)
case irpQueryInfo:
h.handleQueryInfo(req)
case irpSetInfo:
h.handleSetInfo(req)
case irpDeviceCtrl:
h.handleDeviceControl(req)
case irpQueryVolume:
h.handleQueryVolume(req)
case irpDirControl:
if req.minorFn == irpMnQueryDir {
h.handleQueryDirectory(req)
return
}
if req.minorFn == irpMnNotifyChangeDir {
h.handleNotifyChangeDirectory(req)
return
}
h.sendIOCompletion(req.deviceID, req.completionID, statusNotSupported, nil)
case irpLockControl:
h.handleLockControl(req)
default:
h.sendIOCompletion(req.deviceID, req.completionID, statusNotSupported, nil)
}
}
func (h *Handler) handleCreate(req ioRequest) {
var out [5]byte
if len(req.payload) < 32 {
h.sendIOCompletion(req.deviceID, req.completionID, statusInvalidParameter, out[:])
return
}
disposition := binary.LittleEndian.Uint32(req.payload[20:24])
options := binary.LittleEndian.Uint32(req.payload[24:28])
pathLen := binary.LittleEndian.Uint32(req.payload[28:32])
if uint32(len(req.payload)) < 32+pathLen {
h.sendIOCompletion(req.deviceID, req.completionID, statusInvalidParameter, out[:])
return
}
vpath := normalizeVirtualPath(decodeUTF16(req.payload[32 : 32+pathLen]))
isDir := options&fileDirectoryFile != 0
kind, name := classifyVirtualPath(vpath)
if kind == "missing" {
h.sendIOCompletion(req.deviceID, req.completionID, statusObjectNameNotFound, out[:])
return
}
switch kind {
case "root", "download", "upload":
isDir = true
case "file", "upload_file":
// Explorer 拖拽复制时可能带上目录相关 CreateOptions,虚拟盘文件必须按真实文件打开。// Logan.Li
isDir = false
}
var initialData []byte
if kind == "upload_file" {
data, ok := h.getUpload(name)
if !ok {
h.sendIOCompletion(req.deviceID, req.completionID, statusObjectNameNotFound, out[:])
return
}
initialData = data
}
if isDir && options&fileNonDirectoryFile != 0 {
h.sendIOCompletion(req.deviceID, req.completionID, statusFileIsDirectory, out[:])
return
}
if kind == "file" && disposition == fileOpen {
// 虚拟 Download 目录只负责接收远端写入,读取历史文件留给上传目录实现。// Logan.Li
h.sendIOCompletion(req.deviceID, req.completionID, statusObjectNameNotFound, out[:])
return
}
info := fileOpened
if disposition == fileCreate {
info = fileCreated
}
if disposition == fileOverwrite || disposition == fileOverwriteIf {
info = fileOverwritten
}
fileID := h.storeOpenFile(&openFile{path: vpath, name: name, isDir: isDir, data: initialData})
binary.LittleEndian.PutUint32(out[0:4], fileID)
out[4] = info
log.Printf("[rdpdr] create path=%q fileID=%d dir=%t", vpath, fileID, isDir)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out[:])
}
func (h *Handler) handleClose(req ioRequest) {
file, ok := h.removeOpenFile(req.fileID)
if ok && !file.isDir && file.written && len(file.data) > 0 && h.onDownload != nil {
data := append([]byte(nil), file.data...)
h.onDownload(Download{Name: file.name, Data: data})
log.Printf("[rdpdr] download captured name=%s size=%d", file.name, len(data))
}
var out [5]byte
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out[:])
}
func (h *Handler) handleRead(req ioRequest) {
if len(req.payload) < 20 {
h.sendIOCompletion(req.deviceID, req.completionID, statusInvalidParameter, nil)
return
}
length := int(binary.LittleEndian.Uint32(req.payload[0:4]))
offset := int(binary.LittleEndian.Uint64(req.payload[4:12]))
file, ok := h.getOpenFile(req.fileID)
if !ok || file.isDir {
log.Printf("[rdpdr] read missing file=%d offset=%d length=%d", req.fileID, offset, length)
h.sendIOCompletion(req.deviceID, req.completionID, statusNoSuchFile, nil)
return
}
if length <= 0 {
var out [4]byte
log.Printf("[rdpdr] read zero file=%d path=%q offset=%d", req.fileID, file.path, offset)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out[:])
return
}
if offset >= len(file.data) {
var out [4]byte
// Explorer 复制虚拟盘文件时更稳定地接受 0 字节成功读作为 EOF。// Logan.Li
log.Printf("[rdpdr] read eof file=%d path=%q offset=%d size=%d", req.fileID, file.path, offset, len(file.data))
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out[:])
return
}
end := offset + length
if end > len(file.data) {
end = len(file.data)
}
out := make([]byte, 4+end-offset)
binary.LittleEndian.PutUint32(out[0:4], uint32(end-offset))
copy(out[4:], file.data[offset:end])
log.Printf("[rdpdr] read file=%d path=%q offset=%d requested=%d returned=%d size=%d", req.fileID, file.path, offset, length, end-offset, len(file.data))
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
}
func (h *Handler) handleWrite(req ioRequest) {
if len(req.payload) < 32 {
h.sendIOCompletion(req.deviceID, req.completionID, statusInvalidParameter, nil)
return
}
length := int(binary.LittleEndian.Uint32(req.payload[0:4]))
offset := int(binary.LittleEndian.Uint64(req.payload[4:12]))
chunk := req.payload[32:]
if length > len(chunk) {
length = len(chunk)
}
file, ok := h.getOpenFile(req.fileID)
if !ok || file.isDir {
h.sendIOCompletion(req.deviceID, req.completionID, statusNoSuchFile, nil)
return
}
need := offset + length
if need > len(file.data) {
next := make([]byte, need)
copy(next, file.data)
file.data = next
}
copy(file.data[offset:need], chunk[:length])
file.written = true
var out [5]byte
binary.LittleEndian.PutUint32(out[0:4], uint32(length))
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out[:])
}
func (h *Handler) handleQueryInfo(req ioRequest) {
if len(req.payload) < 4 {
h.sendIOCompletion(req.deviceID, req.completionID, statusInvalidParameter, nil)
return
}
infoClass := binary.LittleEndian.Uint32(req.payload[0:4])
file, ok := h.getOpenFile(req.fileID)
if !ok {
h.sendIOCompletion(req.deviceID, req.completionID, statusNoSuchFile, nil)
return
}
attrs := fileAttrArchive
if file.isDir {
attrs = fileAttrDirectory
}
size := uint64(len(file.data))
log.Printf("[rdpdr] query info file=%d class=%d path=%q dir=%t size=%d", req.fileID, infoClass, file.path, file.isDir, size)
switch infoClass {
case fileBasicInformation:
out := make([]byte, 40)
binary.LittleEndian.PutUint32(out[0:4], 36)
writeFileTimes(out[4:36])
binary.LittleEndian.PutUint32(out[36:40], attrs)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileStandardInformation:
out := make([]byte, 26)
binary.LittleEndian.PutUint32(out[0:4], 22)
binary.LittleEndian.PutUint64(out[4:12], align4096(size))
binary.LittleEndian.PutUint64(out[12:20], size)
binary.LittleEndian.PutUint32(out[20:24], 1)
if file.isDir {
out[25] = 1
}
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileAttributeTagInformation:
out := make([]byte, 12)
binary.LittleEndian.PutUint32(out[0:4], 8)
binary.LittleEndian.PutUint32(out[4:8], attrs)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileInternalInformation:
out := make([]byte, 12)
binary.LittleEndian.PutUint32(out[0:4], 8)
binary.LittleEndian.PutUint64(out[4:12], uint64(req.fileID))
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileEaInformation:
out := make([]byte, 8)
binary.LittleEndian.PutUint32(out[0:4], 4)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileAccessInformation, fileModeInformation, fileAlignmentInformation:
out := make([]byte, 8)
binary.LittleEndian.PutUint32(out[0:4], 4)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileNameInformation:
name := encodeUTF16(fileDisplayName(file))
out := make([]byte, 8+len(name))
binary.LittleEndian.PutUint32(out[0:4], uint32(4+len(name)))
binary.LittleEndian.PutUint32(out[4:8], uint32(len(name)))
copy(out[8:], name)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case filePositionInformation:
out := make([]byte, 12)
binary.LittleEndian.PutUint32(out[0:4], 8)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileNetworkOpenInformation:
out := make([]byte, 60)
binary.LittleEndian.PutUint32(out[0:4], 56)
writeFileTimes(out[4:36])
binary.LittleEndian.PutUint64(out[36:44], align4096(size))
binary.LittleEndian.PutUint64(out[44:52], size)
binary.LittleEndian.PutUint32(out[52:56], attrs)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
default:
log.Printf("[rdpdr] query info unsupported class=%d file=%d path=%q", infoClass, req.fileID, file.path)
h.sendIOCompletion(req.deviceID, req.completionID, statusNotSupported, nil)
}
}
func (h *Handler) handleSetInfo(req ioRequest) {
var out [4]byte
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out[:])
}
func (h *Handler) handleQueryVolume(req ioRequest) {
if len(req.payload) < 4 {
h.sendIOCompletion(req.deviceID, req.completionID, statusInvalidParameter, nil)
return
}
infoClass := binary.LittleEndian.Uint32(req.payload[0:4])
switch infoClass {
case fileFsVolumeInformation:
label := encodeUTF16(h.driveName)
out := make([]byte, 21+len(label))
binary.LittleEndian.PutUint32(out[0:4], uint32(17+len(label)))
binary.LittleEndian.PutUint64(out[4:12], filetimeNow())
binary.LittleEndian.PutUint32(out[12:16], 0x41494f50)
binary.LittleEndian.PutUint32(out[16:20], uint32(len(label)))
copy(out[21:], label)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileFsSizeInformation:
out := make([]byte, 28)
binary.LittleEndian.PutUint32(out[0:4], 24)
binary.LittleEndian.PutUint64(out[4:12], 1024*1024)
binary.LittleEndian.PutUint64(out[12:20], 1024*1024)
binary.LittleEndian.PutUint32(out[20:24], 8)
binary.LittleEndian.PutUint32(out[24:28], 512)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileFsDeviceInformation:
out := make([]byte, 12)
binary.LittleEndian.PutUint32(out[0:4], 8)
binary.LittleEndian.PutUint32(out[4:8], 0x00000007)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileFsAttributeInformation:
fsName := encodeUTF16("AIOPS")
out := make([]byte, 16+len(fsName))
binary.LittleEndian.PutUint32(out[0:4], uint32(12+len(fsName)))
binary.LittleEndian.PutUint32(out[4:8], 0x00000007)
binary.LittleEndian.PutUint32(out[8:12], 255)
binary.LittleEndian.PutUint32(out[12:16], uint32(len(fsName)))
copy(out[16:], fsName)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
case fileFsFullSizeInformation:
out := make([]byte, 36)
binary.LittleEndian.PutUint32(out[0:4], 32)
binary.LittleEndian.PutUint64(out[4:12], 1024*1024)
binary.LittleEndian.PutUint64(out[12:20], 1024*1024)
binary.LittleEndian.PutUint64(out[20:28], 1024*1024)
binary.LittleEndian.PutUint32(out[28:32], 8)
binary.LittleEndian.PutUint32(out[32:36], 512)
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out)
default:
h.sendIOCompletion(req.deviceID, req.completionID, statusNotSupported, nil)
}
}
func (h *Handler) handleDeviceControl(req ioRequest) {
var out [4]byte
ioctl := uint32(0)
if len(req.payload) >= 12 {
ioctl = binary.LittleEndian.Uint32(req.payload[8:12])
}
// 资源管理器复制时会探测若干 FSCTL;明确返回“不支持”比 invalid parameter 更接近本地盘行为。// Logan.Li
log.Printf("[rdpdr] device control file=%d ioctl=0x%x payload=%d", req.fileID, ioctl, len(req.payload))
h.sendIOCompletion(req.deviceID, req.completionID, statusInvalidDeviceReq, out[:])
}
func (h *Handler) handleNotifyChangeDirectory(req ioRequest) {
// Windows 资源管理器会长期挂起目录变更通知;立即成功返回会触发重复刷新和“地址无效”。// Logan.Li
log.Printf("[rdpdr] notify change pending file=%d", req.fileID)
}
func (h *Handler) handleLockControl(req ioRequest) {
var out [4]byte
// 上传暂存文件是只读内存文件,锁请求直接成功,避免 Explorer 复制流程中断。// Logan.Li
log.Printf("[rdpdr] lock control file=%d payload=%d", req.fileID, len(req.payload))
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, out[:])
}
func (h *Handler) handleQueryDirectory(req ioRequest) {
if len(req.payload) < 32 {
h.sendIOCompletion(req.deviceID, req.completionID, statusInvalidParameter, nil)
return
}
infoClass := binary.LittleEndian.Uint32(req.payload[0:4])
initial := req.payload[4] != 0
file, ok := h.getOpenFile(req.fileID)
if !ok || !file.isDir {
h.sendIOCompletion(req.deviceID, req.completionID, statusNoSuchFile, nil)
return
}
if initial {
file.dirIndex = 0
if file.path == "" {
file.dirEntries = []dirEntry{{name: "."}, {name: ".."}, {name: downloadDirName, isDir: true}, {name: uploadDirName, isDir: true}}
} else if strings.EqualFold(file.path, uploadDirName) {
file.dirEntries = append([]dirEntry{{name: "."}, {name: ".."}}, h.listUploads()...)
} else {
file.dirEntries = []dirEntry{{name: "."}, {name: ".."}}
}
}
log.Printf("[rdpdr] query dir file=%d class=%d path=%q initial=%t index=%d entries=%d", req.fileID, infoClass, file.path, initial, file.dirIndex, len(file.dirEntries))
if file.dirIndex >= len(file.dirEntries) {
var out [5]byte
h.sendIOCompletion(req.deviceID, req.completionID, statusNoMoreFiles, out[:])
return
}
entry := file.dirEntries[file.dirIndex]
file.dirIndex++
h.sendIOCompletion(req.deviceID, req.completionID, statusSuccess, buildDirInfo(infoClass, entry))
}
func buildDirInfo(infoClass uint32, entry dirEntry) []byte {
name := encodeUTF16(entry.name)
nameWithNull := append(append([]byte(nil), name...), 0, 0)
attrs := fileAttrNormal
if entry.isDir || entry.name == "." || entry.name == ".." {
attrs = fileAttrDirectory
}
if infoClass == fileNamesInformation {
out := make([]byte, 16+len(nameWithNull))
binary.LittleEndian.PutUint32(out[0:4], uint32(12+len(nameWithNull)))
binary.LittleEndian.PutUint32(out[12:16], uint32(len(nameWithNull)))
copy(out[16:], nameWithNull)
return out
}
fixed := 64
if infoClass == fileFullDirectoryInformation {
fixed = 68
}
if infoClass == fileBothDirectoryInformation {
fixed = 93
}
out := make([]byte, 4+fixed+len(nameWithNull))
binary.LittleEndian.PutUint32(out[0:4], uint32(fixed+len(nameWithNull)))
writeFileTimes(out[12:44])
binary.LittleEndian.PutUint64(out[44:52], entry.size)
binary.LittleEndian.PutUint64(out[52:60], align4096(entry.size))
binary.LittleEndian.PutUint32(out[60:64], attrs)
binary.LittleEndian.PutUint32(out[64:68], uint32(len(nameWithNull)))
copy(out[4+fixed:], nameWithNull)
return out
}
func fileDisplayName(file *openFile) string {
if file.name != "" {
return file.name
}
if file.path != "" {
parts := strings.Split(file.path, "\\")
return parts[len(parts)-1]
}
return "\\"
}
func classifyVirtualPath(vpath string) (string, string) {
if vpath == "" {
return "root", ""
}
parts := strings.Split(vpath, "\\")
if len(parts) == 1 && strings.EqualFold(parts[0], downloadDirName) {
return "download", downloadDirName
}
if len(parts) == 1 && strings.EqualFold(parts[0], uploadDirName) {
return "upload", uploadDirName
}
if len(parts) >= 2 && strings.EqualFold(parts[0], downloadDirName) {
name := parts[len(parts)-1]
if name == "" || name == "." || name == ".." {
return "missing", ""
}
return "file", name
}
if len(parts) >= 2 && strings.EqualFold(parts[0], uploadDirName) {
name := parts[len(parts)-1]
if name == "" || name == "." || name == ".." {
return "missing", ""
}
return "upload_file", name
}
return "missing", ""
}
func normalizeVirtualPath(raw string) string {
clean := strings.Trim(strings.ReplaceAll(raw, "/", "\\"), "\\\x00 ")
if clean == "" || clean == "." {
return ""
}
parts := strings.FieldsFunc(clean, func(r rune) bool { return r == '\\' })
safe := make([]string, 0, len(parts))
for _, part := range parts {
if part == "" || part == "." || part == ".." {
continue
}
safe = append(safe, part)
}
return strings.ReplaceAll(path.Clean(strings.Join(safe, "/")), "/", "\\")
}
func encodeUTF16(s string) []byte {
u16 := utf16.Encode([]rune(s))
out := make([]byte, len(u16)*2)
for i, v := range u16 {
binary.LittleEndian.PutUint16(out[i*2:], v)
}
return out
}
func decodeUTF16(data []byte) string {
u16 := make([]uint16, len(data)/2)
for i := range u16 {
u16[i] = binary.LittleEndian.Uint16(data[i*2:])
}
for len(u16) > 0 && u16[len(u16)-1] == 0 {
u16 = u16[:len(u16)-1]
}
return string(utf16.Decode(u16))
}
func writeFileTimes(out []byte) {
ft := filetimeNow()
for i := 0; i+8 <= len(out); i += 8 {
binary.LittleEndian.PutUint64(out[i:i+8], ft)
}
}
func filetimeNow() uint64 {
return uint64(time.Now().UnixNano()/100) + windowsFiletimeOffset
}
func align4096(v uint64) uint64 {
return (v + 4095) &^ 4095
}
@@ -0,0 +1,109 @@
package rdpdr
import (
"encoding/binary"
"testing"
)
type captureSender struct {
data [][]byte
}
func (s *captureSender) SendChannelPDU(_ uint16, data []byte) error {
s.data = append(s.data, append([]byte(nil), data...))
return nil
}
func TestClassifyVirtualPathUploadFile(t *testing.T) {
kind, name := classifyVirtualPath(`Upload\php.ini`)
if kind != "upload_file" || name != "php.ini" {
t.Fatalf("Upload file should be classified as upload_file, got kind=%q name=%q", kind, name)
}
}
func TestHandleCreateUploadFileOpensAsFile(t *testing.T) {
sender := &captureSender{}
handler := NewHandler("goclient", "AIOps")
handler.Attach(sender, 1004)
handler.AddUpload("php.ini", []byte("ok"))
encodedPath := encodeUTF16(`Upload\php.ini`)
payload := make([]byte, 32+len(encodedPath))
binary.LittleEndian.PutUint32(payload[20:24], fileOpen)
binary.LittleEndian.PutUint32(payload[24:28], fileDirectoryFile)
binary.LittleEndian.PutUint32(payload[28:32], uint32(len(encodedPath)))
copy(payload[32:], encodedPath)
handler.handleCreate(ioRequest{
deviceID: 1,
completionID: 7,
majorFn: irpCreate,
payload: payload,
})
if len(sender.data) == 0 {
t.Fatal("expected create completion")
}
completion := sender.data[len(sender.data)-1]
if len(completion) < 21 {
t.Fatalf("completion too short: %d", len(completion))
}
if status := binary.LittleEndian.Uint32(completion[12:16]); status != statusSuccess {
t.Fatalf("expected success, got status=0x%08x", status)
}
fileID := binary.LittleEndian.Uint32(completion[16:20])
file, ok := handler.getOpenFile(fileID)
if !ok {
t.Fatalf("open file %d not stored", fileID)
}
if file.isDir {
t.Fatalf("upload file opened as directory: %#v", file)
}
}
func TestHandleReadEOFReturnsZeroLengthSuccess(t *testing.T) {
sender := &captureSender{}
handler := NewHandler("goclient", "AIOps")
handler.Attach(sender, 1004)
fileID := handler.storeOpenFile(&openFile{path: `Upload\x.yaml`, name: "x.yaml", data: []byte("abc")})
payload := make([]byte, 20)
binary.LittleEndian.PutUint32(payload[0:4], 4096)
binary.LittleEndian.PutUint64(payload[4:12], 3)
handler.handleRead(ioRequest{
deviceID: 1,
fileID: fileID,
completionID: 9,
payload: payload,
})
completion := sender.data[len(sender.data)-1]
if status := binary.LittleEndian.Uint32(completion[12:16]); status != statusSuccess {
t.Fatalf("expected EOF success, got status=0x%08x", status)
}
if got := binary.LittleEndian.Uint32(completion[16:20]); got != 0 {
t.Fatalf("expected zero read length at EOF, got %d", got)
}
}
func TestHandleDeviceControlReturnsInvalidDeviceRequest(t *testing.T) {
sender := &captureSender{}
handler := NewHandler("goclient", "AIOps")
handler.Attach(sender, 1004)
payload := make([]byte, 32)
binary.LittleEndian.PutUint32(payload[8:12], 0x000900a8)
handler.handleDeviceControl(ioRequest{
deviceID: 1,
fileID: 2,
completionID: 10,
payload: payload,
})
completion := sender.data[len(sender.data)-1]
if status := binary.LittleEndian.Uint32(completion[12:16]); status != statusInvalidDeviceReq {
t.Fatalf("expected invalid device request, got status=0x%08x", status)
}
if got := binary.LittleEndian.Uint32(completion[16:20]); got != 0 {
t.Fatalf("expected empty device control output, got %d", got)
}
}
+388
View File
@@ -0,0 +1,388 @@
// Package rdpdr 实现 RDP 设备重定向静态虚拟通道的最小握手。// Logan.Li
package rdpdr
import (
"bytes"
"encoding/binary"
"fmt"
"log"
"sync"
"unicode/utf16"
)
type Sender interface {
SendChannelPDU(channelID uint16, data []byte) error
}
const ChannelName = "rdpdr"
const (
componentRDPDR = 0x4472
pakIDServerAnnounce = 0x496e
pakIDClientIDConfirm = 0x4343
pakIDClientName = 0x434e
pakIDServerCoreCap = 0x5350
pakIDClientCoreCap = 0x4350
pakIDDeviceListAnnounce = 0x4441
pakIDDeviceReply = 0x6472
pakIDDeviceIORequest = 0x4952
pakIDDeviceIOCompletion = 0x4943
pakIDUserLoggedOn = 0x554c
capGeneralType = 0x0001
capDriveType = 0x0004
deviceTypeDisk = 0x00000008
)
type Download struct {
Name string
Data []byte
}
type Handler struct {
sender Sender
channelID uint16
clientID uint32
gotServerCap bool
gotIDConfirm bool
serverIOCode1 uint32
computerName string
driveName string
devicePreannounced bool
deviceAnnounced bool
mu sync.Mutex
nextFileID uint32
files map[uint32]*openFile
uploads map[string][]byte
onDownload func(Download)
}
func NewHandler(computerName string, driveName string) *Handler {
if computerName == "" {
computerName = "goclient"
}
if driveName == "" {
driveName = "AIOPS"
}
return &Handler{computerName: computerName, driveName: driveName, nextFileID: 1, files: make(map[uint32]*openFile), uploads: make(map[string][]byte)}
}
func (h *Handler) SetDownloadHandler(fn func(Download)) {
h.onDownload = fn
}
func (h *Handler) AddUpload(name string, data []byte) {
if name == "" || len(data) == 0 {
return
}
h.mu.Lock()
h.uploads[name] = append([]byte(nil), data...)
h.mu.Unlock()
log.Printf("[rdpdr] upload staged name=%s size=%d", name, len(data))
}
func (h *Handler) listUploads() []dirEntry {
h.mu.Lock()
defer h.mu.Unlock()
out := make([]dirEntry, 0, len(h.uploads))
for name, data := range h.uploads {
out = append(out, dirEntry{name: name, size: uint64(len(data))})
}
return out
}
func (h *Handler) getUpload(name string) ([]byte, bool) {
h.mu.Lock()
defer h.mu.Unlock()
data, ok := h.uploads[name]
if !ok {
return nil, false
}
return append([]byte(nil), data...), true
}
func (h *Handler) Attach(sender Sender, channelID uint16) {
h.sender = sender
h.channelID = channelID
log.Printf("[rdpdr] attached channelID=%d", channelID)
}
func (h *Handler) PreannounceDevices() {
h.mu.Lock()
if h.devicePreannounced || h.sender == nil || h.channelID == 0 {
h.mu.Unlock()
return
}
h.devicePreannounced = true
h.mu.Unlock()
// 部分 Windows 会在图形首帧后才启动 rdpdr;预公告客户端能力和磁盘设备,尽早完成驱动器重定向。// Logan.Li
log.Printf("[rdpdr] preannounce devices client=%s drive=%s", h.computerName, h.driveName)
h.sendClientName()
h.sendCoreCapResponse()
h.sendDeviceList(true)
}
func (h *Handler) Process(data []byte) {
if len(data) < 4 {
return
}
component := binary.LittleEndian.Uint16(data[0:2])
packetID := binary.LittleEndian.Uint16(data[2:4])
body := data[4:]
if component != componentRDPDR {
log.Printf("[rdpdr] ignore component=0x%x packet=0x%x len=%d", component, packetID, len(data))
return
}
log.Printf("[rdpdr] recv packet=0x%x len=%d", packetID, len(body))
switch packetID {
case pakIDServerAnnounce:
h.handleServerAnnounce(body)
case pakIDServerCoreCap:
h.handleServerCoreCap(body)
case pakIDClientIDConfirm:
h.handleClientIDConfirm(body)
case pakIDDeviceReply:
h.handleDeviceReply(body)
case pakIDDeviceIORequest:
h.handleIORequest(body)
case pakIDUserLoggedOn:
h.sendDeviceListOnce()
default:
log.Printf("[rdpdr] unhandled packet=0x%x len=%d", packetID, len(body))
}
}
func (h *Handler) handleServerAnnounce(body []byte) {
if len(body) < 8 {
return
}
h.clientID = binary.LittleEndian.Uint32(body[4:8])
log.Printf("[rdpdr] server announce clientID=%d version=%d.%d", h.clientID, binary.LittleEndian.Uint16(body[0:2]), binary.LittleEndian.Uint16(body[2:4]))
buf := make([]byte, 12)
binary.LittleEndian.PutUint16(buf[0:2], componentRDPDR)
binary.LittleEndian.PutUint16(buf[2:4], pakIDClientIDConfirm)
binary.LittleEndian.PutUint16(buf[4:6], 1)
binary.LittleEndian.PutUint16(buf[6:8], 0x000c)
binary.LittleEndian.PutUint32(buf[8:12], h.clientID)
h.send(buf)
h.sendClientName()
}
func (h *Handler) sendClientName() {
runes := utf16.Encode([]rune(h.computerName + "\x00"))
name := make([]byte, len(runes)*2)
for i, r := range runes {
binary.LittleEndian.PutUint16(name[i*2:], r)
}
buf := make([]byte, 16+len(name))
binary.LittleEndian.PutUint16(buf[0:2], componentRDPDR)
binary.LittleEndian.PutUint16(buf[2:4], pakIDClientName)
binary.LittleEndian.PutUint32(buf[4:8], 1)
binary.LittleEndian.PutUint32(buf[12:16], uint32(len(name)))
copy(buf[16:], name)
h.send(buf)
}
func (h *Handler) handleServerCoreCap(body []byte) {
if len(body) < 4 {
return
}
count := binary.LittleEndian.Uint16(body[0:2])
offset := 4
for i := 0; i < int(count) && offset+8 <= len(body); i++ {
capType := binary.LittleEndian.Uint16(body[offset:])
capLen := int(binary.LittleEndian.Uint16(body[offset+2:]))
if capLen < 8 || offset+capLen > len(body) {
break
}
if capType == capGeneralType && capLen >= 24 {
h.serverIOCode1 = binary.LittleEndian.Uint32(body[offset+20 : offset+24])
}
offset += capLen
}
h.gotServerCap = true
h.trySendReady()
}
func (h *Handler) handleClientIDConfirm(body []byte) {
if len(body) >= 8 {
h.clientID = binary.LittleEndian.Uint32(body[4:8])
}
h.gotIDConfirm = true
h.trySendReady()
}
func (h *Handler) trySendReady() {
if !h.gotServerCap || !h.gotIDConfirm {
return
}
h.sendCoreCapResponse()
// 能力协商完成后立即公告虚拟盘,避免首次登录后资源管理器等待网络位置刷新。// Logan.Li
h.sendDeviceListOnce()
}
func (h *Handler) sendCoreCapResponse() {
const generalCapLen = 44
const driveCapLen = 8
buf := bytes.Buffer{}
writeLE16(&buf, componentRDPDR)
writeLE16(&buf, pakIDClientCoreCap)
writeLE16(&buf, 2)
writeLE16(&buf, 0)
writeLE16(&buf, capGeneralType)
writeLE16(&buf, generalCapLen)
writeLE32(&buf, 2)
writeLE32(&buf, 0)
writeLE32(&buf, 0)
writeLE16(&buf, 1)
writeLE16(&buf, 0x000c)
ioCode1 := h.serverIOCode1 & 0x0000ffff
if ioCode1 == 0 {
ioCode1 = 0x0000ffff
}
writeLE32(&buf, ioCode1)
writeLE32(&buf, 0)
writeLE32(&buf, 0x00000007)
writeLE32(&buf, 0x00000001)
writeLE32(&buf, 0)
writeLE32(&buf, 0)
writeLE16(&buf, capDriveType)
writeLE16(&buf, driveCapLen)
writeLE32(&buf, 2)
h.send(buf.Bytes())
}
func (h *Handler) sendDeviceList(includeDevices bool) {
deviceData := []byte(nil)
deviceCount := uint32(0)
totalLen := 8
if includeDevices {
deviceCount = 1
deviceData = []byte(h.driveName)
totalLen += 20 + len(deviceData)
}
if deviceCount == 0 {
// FreeRDP 在没有预登录设备时直接跳过空公告,避免 Windows 记录一个无设备列表。// Logan.Li
log.Printf("[rdpdr] skip empty device list announce")
return
}
buf := make([]byte, totalLen)
binary.LittleEndian.PutUint16(buf[0:2], componentRDPDR)
binary.LittleEndian.PutUint16(buf[2:4], pakIDDeviceListAnnounce)
binary.LittleEndian.PutUint32(buf[4:8], deviceCount)
if includeDevices {
// 公告一个磁盘设备,Windows 里显示为 \\goclient\AIOPS。// Logan.Li
binary.LittleEndian.PutUint32(buf[8:12], deviceTypeDisk)
binary.LittleEndian.PutUint32(buf[12:16], 1)
copy(buf[16:24], []byte(dosDeviceName(h.driveName)))
binary.LittleEndian.PutUint32(buf[24:28], uint32(len(deviceData)))
copy(buf[28:], deviceData)
}
h.send(buf)
log.Printf("[rdpdr] device list announced devices=%d name=%s", deviceCount, h.driveName)
}
func (h *Handler) sendDeviceListOnce() {
h.mu.Lock()
if h.deviceAnnounced {
h.mu.Unlock()
return
}
h.deviceAnnounced = true
h.mu.Unlock()
h.sendDeviceList(true)
}
func dosDeviceName(name string) string {
if name == "" {
return "AIOPS"
}
if len(name) > 8 {
return name[:8]
}
return name
}
func (h *Handler) send(data []byte) {
if h.sender == nil || h.channelID == 0 {
return
}
if err := h.sender.SendChannelPDU(h.channelID, data); err != nil {
log.Printf("[rdpdr] send failed: %v", err)
}
}
func (h *Handler) sendIOCompletion(deviceID, completionID, status uint32, output []byte) {
buf := make([]byte, 16+len(output))
binary.LittleEndian.PutUint16(buf[0:2], componentRDPDR)
binary.LittleEndian.PutUint16(buf[2:4], pakIDDeviceIOCompletion)
binary.LittleEndian.PutUint32(buf[4:8], deviceID)
binary.LittleEndian.PutUint32(buf[8:12], completionID)
binary.LittleEndian.PutUint32(buf[12:16], status)
copy(buf[16:], output)
h.send(buf)
}
func (h *Handler) allocFileID() uint32 {
h.mu.Lock()
defer h.mu.Unlock()
id := h.nextFileID
h.nextFileID++
return id
}
func (h *Handler) storeOpenFile(file *openFile) uint32 {
id := h.allocFileID()
h.mu.Lock()
h.files[id] = file
h.mu.Unlock()
return id
}
func (h *Handler) getOpenFile(id uint32) (*openFile, bool) {
h.mu.Lock()
defer h.mu.Unlock()
file, ok := h.files[id]
return file, ok
}
func (h *Handler) removeOpenFile(id uint32) (*openFile, bool) {
h.mu.Lock()
defer h.mu.Unlock()
file, ok := h.files[id]
if ok {
delete(h.files, id)
}
return file, ok
}
func (h *Handler) handleDeviceReply(body []byte) {
if len(body) < 8 {
return
}
deviceID := binary.LittleEndian.Uint32(body[0:4])
status := binary.LittleEndian.Uint32(body[4:8])
if status == statusSuccess {
h.mu.Lock()
h.deviceAnnounced = true
h.mu.Unlock()
}
log.Printf("[rdpdr] device reply deviceID=%d status=0x%08x", deviceID, status)
}
func writeLE16(w *bytes.Buffer, v uint16) {
_ = binary.Write(w, binary.LittleEndian, v)
}
func writeLE32(w *bytes.Buffer, v uint32) {
_ = binary.Write(w, binary.LittleEndian, v)
}
func (h *Handler) String() string {
return fmt.Sprintf("rdpdr(channel=%d)", h.channelID)
}