Files
aiops/backend/pkg/rdp/session_connect.go
T
LoganandClaude 76ec9387f0 feat(rdp): 新增 RDP 远程桌面/SMB/账单同步能力 + 清理编译产物
主要变更:
- RDP 远程桌面:新增 backend/pkg/rdp(RDP 引擎 + credssp/display/rle/trace)、
  backend/controller/rdp*(连接/凭证/文件/会话/实时)、frontend RDP 组件与 API
- Windows SMB 文件传输:backend/pkg/winsmb
- 云账单同步:backend/pkg/billingsync + billing_sync/aliyun_billing,前端账单详情
- Agent CLI Kubernetes 工具:kubernetes_action/get、pod_execute/logs
- 服务器导入助手:server_import_helpers、ImportCredentialSettings
- 国际化与组件重构(cloud/deploy/monitor/server/system 等多模块)
- 清理编译产物二进制:移除 backend/server、backend/agent/bin/aiops-agent-*
- .gitignore:新增 backend/agent/ 与 .cache/(避免 334MB go-build 缓存误入库)

Co-Authored-By: Claude <noreply@anthropic.com>
2026-07-04 01:50:36 +08:00

170 lines
4.2 KiB
Go

package rdp
import (
"context"
"fmt"
"io"
"os"
"strings"
"time"
"ezops/pkg/rdp/credssp"
"ezops/pkg/rdp/transport"
)
// Session 保存已建立的 RDP 图形会话状态 // Logan.Li
type Session struct {
Conn io.ReadWriter
UserID uint16
SharedID uint32
Width int
Height int
}
// OpenSession 完成 CredSSP + MCS/GCC 握手并进入可收帧状态 // Logan.Li
func OpenSession(ctx context.Context, cfg Config) (*Session, error) {
port := cfg.Port
if port == 0 {
port = 3389
}
if cfg.MCSMode == "" {
cfg.MCSMode = strings.TrimSpace(os.Getenv("RDP_MCS_MODE"))
}
if cfg.MCSMode == "" {
cfg.MCSMode = "grdp-modern"
}
address := fmt.Sprintf("%s:%d", cfg.Host, port)
strategies := []credssp.Options{
{Version: 6, PubKeyStyle: "binding"},
{Version: 6, PubKeyStyle: "binding", UseCBT: true},
{Version: 6, PubKeyStyle: "binding", ForceMIC: true},
{Version: 2, PubKeyStyle: "raw"},
}
var lastErr error
for _, opts := range strategies {
sess, err := openSessionOnce(ctx, cfg, address, opts)
if err == nil {
return sess, nil
}
lastErr = err
}
if lastErr == nil {
lastErr = fmt.Errorf("RDP 会话建立失败")
}
return nil, lastErr
}
func openSessionOnce(ctx context.Context, cfg Config, address string, opts credssp.Options) (*Session, error) {
tlsConn, _, err := dialCredSSPOnce(ctx, cfg, address, opts)
if err != nil {
return nil, err
}
_ = tlsConn.SetDeadline(time.Now().Add(45 * time.Second))
if err := transport.WriteX224Data(tlsConn, buildMCSConnectInitial(cfg)); err != nil {
_ = tlsConn.Close()
return nil, err
}
payload, err := transport.ReadX224Data(tlsConn)
if err != nil {
_ = tlsConn.Close()
return nil, err
}
if _, err := parseMCSConnectResponse(payload); err != nil {
_ = tlsConn.Close()
return nil, err
}
userID, err := attachMCSUser(tlsConn)
if err != nil {
_ = tlsConn.Close()
return nil, err
}
mcsInfo, _ := parseMCSConnectResponse(payload)
joinIDs := append([]uint16{mcsGlobalChannelID, userID}, mcsInfo.ServerChannelIDs...)
for _, channelID := range joinIDs {
if _, err := joinMCSChannel(tlsConn, userID, channelID); err != nil {
_ = tlsConn.Close()
return nil, err
}
}
_, demand, err := sendClientInfoAndReadDemand(tlsConn, cfg, userID)
if err != nil {
_ = tlsConn.Close()
return nil, err
}
if _, err := sendConfirmActiveAndFinalize(tlsConn, cfg, userID, demand); err != nil {
_ = tlsConn.Close()
return nil, err
}
if err := requestDisplayUpdates(tlsConn, cfg, userID, demand.sharedID); err != nil {
_ = tlsConn.Close()
return nil, err
}
width := cfg.Width
if width <= 0 {
width = 1280
}
height := cfg.Height
if height <= 0 {
height = 800
}
_ = tlsConn.SetDeadline(time.Time{})
return &Session{
Conn: tlsConn,
UserID: userID,
SharedID: demand.sharedID,
Width: width,
Height: height,
}, nil
}
func requestDisplayUpdates(rw io.ReadWriter, cfg Config, userID uint16, sharedID uint32) error {
if conn, ok := rw.(interface{ SetReadDeadline(time.Time) error }); ok {
_ = conn.SetReadDeadline(time.Now().Add(3 * time.Second))
pkt, err := ReadWirePacket(rw)
_ = conn.SetReadDeadline(time.Time{})
if err == nil && !pkt.FastPath {
_, _ = parseMCSDataIndication(pkt.Payload)
}
}
body := buildSuppressOutputBody(cfg, true)
pdu := buildDataPDU(userID, sharedID, pduType2SuppressOutput, body)
if err := transport.WriteX224Data(rw, buildMCSSendDataRequest(userID, mcsGlobalChannelID, pdu)); err != nil {
return fmt.Errorf("发送 SuppressOutput 失败: %w", err)
}
return nil
}
func buildSuppressOutputBody(cfg Config, allow bool) []byte {
width := cfg.Width
if width <= 0 {
width = 1280
}
height := cfg.Height
if height <= 0 {
height = 800
}
out := make([]byte, 0, 12)
if allow {
out = append(out, 1, 0, 0, 0)
} else {
out = append(out, 0, 0, 0, 0)
}
out = append(out, 0, 0, 0, 0, byte(width-1), byte((width-1)>>8), byte(height-1), byte((height-1)>>8))
return out
}
func buildRefreshRectBody(cfg Config) []byte {
width := cfg.Width
if width <= 0 {
width = 1280
}
height := cfg.Height
if height <= 0 {
height = 800
}
out := make([]byte, 0, 10)
out = append(out, 1, 0)
out = append(out, 0, 0, 0, 0, byte(width-1), byte((width-1)>>8), byte(height-1), byte((height-1)>>8))
return out
}