This commit is contained in:
Baobhan Sith
2025-04-27 22:55:17 +08:00
parent 046fe72d4f
commit 855289679e
2 changed files with 20 additions and 33 deletions
@@ -7,11 +7,11 @@ import {
getConnectionById, getConnectionById,
updateConnection, updateConnection,
deleteConnection, deleteConnection,
testConnection, testConnection,
testUnsavedConnection, testUnsavedConnection,
exportConnections, exportConnections,
importConnections, // <-- Add comma here importConnections,
getRdpSessionToken // +++ Import getRdpSessionToken +++ getRdpSessionToken // Import the new controller function
} from './connections.controller'; } from './connections.controller';
const router = Router(); const router = Router();
@@ -77,6 +77,7 @@ router.post('/:id/test', testConnection);
// POST /api/v1/connections/test-unsaved - 测试未保存的连接信息 // POST /api/v1/connections/test-unsaved - 测试未保存的连接信息
router.post('/test-unsaved', testUnsavedConnection); router.post('/test-unsaved', testUnsavedConnection);
// Removed GET /:id/rdp-token route // POST /api/v1/connections/:id/rdp-session - Get RDP session token via backend
router.post('/:id/rdp-session', getRdpSessionToken);
export default router; export default router;
@@ -50,39 +50,25 @@ const connectRdp = async () => {
statusMessage.value = t('remoteDesktopModal.status.fetchingToken'); statusMessage.value = t('remoteDesktopModal.status.fetchingToken');
try { try {
// 1. 从独立的 RDP 后端获取 Token // 1. 从主后端获取 RDP 会话的 Guacamole Token
// WARNING: Sending credentials directly like this is insecure if the API is not properly secured (e.g., HTTPS, network isolation). // Construct the path relative to the apiClient's baseURL ('/api/v1')
// WARNING: props.connection likely does NOT contain the password. Using a placeholder. const apiUrl = `connections/${props.connection.id}/rdp-session`;
// You MUST implement a secure way to get the password here. console.log(`[RDP Modal] Fetching token from main backend: POST /api/v1/${apiUrl}`); // Log the expected full path
const connectionParams = new URLSearchParams({
hostname: props.connection.host,
port: props.connection.port.toString(),
username: props.connection.username,
// !!! SECURITY RISK: Password should not be handled like this !!!
// Replace this with a secure method (e.g., prompt user, fetch securely)
password: (props.connection as any).password || 'PASSWORD_PLACEHOLDER', // Assuming password might exist, otherwise use placeholder
security: (props.connection as any).rdp_security || 'any', // Use RDP specific fields if available
ignoreCert: String((props.connection as any).rdp_ignore_cert ?? true),
// Add other necessary params supported by the rdp backend API
});
const apiUrl = `${RDP_BACKEND_API_BASE}/api/get-token?${connectionParams.toString()}`;
console.log(`[RDP Modal] Fetching token from RDP backend: ${RDP_BACKEND_API_BASE}/api/get-token?...`);
// Use fetch directly as apiClient might be configured for the main backend // Use apiClient configured for the main backend
const response = await fetch(apiUrl); const response = await apiClient.post<{ token: string }>(apiUrl);
if (!response.ok) {
const errorData = await response.json().catch(() => ({ error: 'Failed to parse error response' })); // apiClient should handle non-2xx responses by throwing an error
throw new Error(`RDP API Error (${response.status}): ${errorData.error || response.statusText}`); // We just need to check if the token exists in the successful response data
} const token = response.data?.token;
const data = await response.json();
const token = data.token;
if (!token) { if (!token) {
throw new Error('Token not found in RDP API response'); console.error('[RDP Modal] Token not found in main backend response:', response.data);
throw new Error('Token not found in API response');
} }
console.log('[RDP Modal] Received token.'); console.log('[RDP Modal] Received token from main backend.');
statusMessage.value = t('remoteDesktopModal.status.connectingWs'); statusMessage.value = t('remoteDesktopModal.status.connectingWs');
// 2. 连接 WebSocket (to RDP backend's WebSocket server) // 2. 连接 WebSocket (仍然连接到独立的 RDP 后端的 WebSocket 服务器)
const tunnelUrl = `${RDP_BACKEND_WEBSOCKET_URL}/?token=${encodeURIComponent(token)}`; const tunnelUrl = `${RDP_BACKEND_WEBSOCKET_URL}/?token=${encodeURIComponent(token)}`;
console.log(`[RDP Modal] Connecting WebSocket to: ${RDP_BACKEND_WEBSOCKET_URL}/?token=...`); console.log(`[RDP Modal] Connecting WebSocket to: ${RDP_BACKEND_WEBSOCKET_URL}/?token=...`);
// @ts-ignore // @ts-ignore