Create creates a new RDP proxy session. It starts a TCP listener and returns the session ID and port. Callers send rdp_proxy to the agent via SendToAgent. Session is ready once the agent confirms (WaitAgentReady).
(ctx context.Context, apiID, hostID string)
| 98 | // the session ID and port. Callers send rdp_proxy to the agent via SendToAgent. |
| 99 | // Session is ready once the agent confirms (WaitAgentReady). |
| 100 | func (s *Sessions) Create(ctx context.Context, apiID, hostID string) (sessionID string, port int, err error) { |
| 101 | s.mu.Lock() |
| 102 | defer s.mu.Unlock() |
| 103 | |
| 104 | if s.maxSessions > 0 && len(s.sessions) >= s.maxSessions { |
| 105 | return "", 0, ErrMaxSessionsReached |
| 106 | } |
| 107 | |
| 108 | b := make([]byte, 16) |
| 109 | if _, err := rand.Read(b); err != nil { |
| 110 | return "", 0, err |
| 111 | } |
| 112 | sessionID = hex.EncodeToString(b) |
| 113 | |
| 114 | listener, err := net.Listen("tcp", "127.0.0.1:0") |
| 115 | if err != nil { |
| 116 | return "", 0, err |
| 117 | } |
| 118 | addr := listener.Addr().(*net.TCPAddr) |
| 119 | port = addr.Port |
| 120 | |
| 121 | sessCtx, cancel := context.WithCancel(context.Background()) |
| 122 | sid := sessionID // capture for closure |
| 123 | sess := &Session{ |
| 124 | SessionID: sessionID, |
| 125 | Port: port, |
| 126 | Listener: listener, |
| 127 | ApiID: apiID, |
| 128 | HostID: hostID, |
| 129 | cancel: cancel, |
| 130 | log: s.log, |
| 131 | sender: s.sender, |
| 132 | agentReady: make(chan AgentResult, 1), |
| 133 | removeFromMap: func() { |
| 134 | s.mu.Lock() |
| 135 | delete(s.sessions, sid) |
| 136 | s.mu.Unlock() |
| 137 | }, |
| 138 | } |
| 139 | sess.touchActivity() |
| 140 | |
| 141 | s.sessions[sessionID] = sess |
| 142 | |
| 143 | if s.log != nil { |
| 144 | s.log.Info("rdp proxy session created", "session_id", sessionID, "port", port, "host_id", hostID) |
| 145 | } |
| 146 | |
| 147 | go sess.acceptLoop(sessCtx) |
| 148 | go sess.timeoutLoop(sessCtx) |
| 149 | |
| 150 | return sessionID, port, nil |
| 151 | } |
| 152 | |
| 153 | // SendToAgent sends a message to the agent owning the named session. Writes |
| 154 | // are serialised by the registry's per-agent write mutex. |
nothing calls this directly
no test coverage detected