|
2 | 2 | from app.utils.crypto import decrypt_secret |
3 | 3 | from app.utils.auth import decode_access_token, user_has_policy |
4 | 4 | from app.services.guacamole import build_instruction, guacd_handshake, read_instruction |
| 5 | +from app.services.vm_service import VmService |
5 | 6 | from app.orm.vm_credential import VmCredentialORM |
6 | 7 | from app.orm.user import UserORM |
7 | 8 | from app.core.config import engine, GUACD_HOST, GUACD_PORT, VNC_HOST |
@@ -144,47 +145,65 @@ async def vm_tunnel( |
144 | 145 | await websocket.close(code=4001, reason="Provide credential or vm_id+token") |
145 | 146 | return |
146 | 147 |
|
147 | | - try: |
148 | | - vnc_port = await asyncio.to_thread(get_vnc_port, resolved_vm_id) |
149 | | - except Exception as exc: |
150 | | - detail = getattr(exc, "detail", str(exc)) |
151 | | - await websocket.close(code=4002, reason=detail) |
152 | | - return |
| 148 | + slave = await asyncio.to_thread(VmService._get_slave_for_vm, resolved_vm_id) |
| 149 | + |
| 150 | + if slave: |
| 151 | + from app.services.slave_client import slave_get_vnc_port |
| 152 | + try: |
| 153 | + vnc_port = await asyncio.to_thread(slave_get_vnc_port, slave, resolved_vm_id) |
| 154 | + except Exception as exc: |
| 155 | + detail = getattr(exc, "detail", str(exc)) |
| 156 | + await websocket.close(code=4002, reason=detail) |
| 157 | + return |
| 158 | + guacd_host = slave.hostname |
| 159 | + guacd_port = GUACD_PORT |
| 160 | + vnc_host = "127.0.0.1" |
| 161 | + else: |
| 162 | + try: |
| 163 | + vnc_port = await asyncio.to_thread(get_vnc_port, resolved_vm_id) |
| 164 | + except Exception as exc: |
| 165 | + detail = getattr(exc, "detail", str(exc)) |
| 166 | + await websocket.close(code=4002, reason=detail) |
| 167 | + return |
| 168 | + guacd_host = GUACD_HOST |
| 169 | + guacd_port = GUACD_PORT |
| 170 | + vnc_host = VNC_HOST |
153 | 171 |
|
154 | 172 | logger.warning( |
155 | | - "Tunnel: vm_id=%s vnc=%s:%s guacd=%s:%s", |
156 | | - resolved_vm_id, VNC_HOST, vnc_port, GUACD_HOST, GUACD_PORT, |
| 173 | + "Tunnel: vm_id=%s vnc=%s:%s guacd=%s:%s slave=%s", |
| 174 | + resolved_vm_id, vnc_host, vnc_port, guacd_host, guacd_port, |
| 175 | + slave.hostname if slave else None, |
157 | 176 | ) |
158 | 177 |
|
159 | 178 | await websocket.accept(subprotocol="guacamole") |
160 | 179 |
|
161 | 180 | try: |
162 | | - reader, writer = await asyncio.open_connection(GUACD_HOST, GUACD_PORT) |
| 181 | + reader, writer = await asyncio.open_connection(guacd_host, guacd_port) |
163 | 182 | except Exception as exc: |
164 | 183 | await websocket.close( |
165 | 184 | code=1011, |
166 | | - reason=f"Cannot connect to guacd {GUACD_HOST}:{GUACD_PORT}: {exc}", |
| 185 | + reason=f"Cannot connect to guacd {guacd_host}:{guacd_port}: {exc}", |
167 | 186 | ) |
168 | 187 | return |
169 | 188 |
|
170 | 189 | try: |
171 | 190 | first_instruction = await guacd_handshake( |
172 | 191 | reader, |
173 | 192 | writer, |
174 | | - vnc_host=VNC_HOST, |
| 193 | + vnc_host=vnc_host, |
175 | 194 | vnc_port=vnc_port, |
176 | 195 | width=width, |
177 | 196 | height=height, |
178 | 197 | ) |
179 | 198 | logger.warning( |
180 | | - "Tunnel connected via configured vnc host=%s port=%s", |
181 | | - VNC_HOST, |
| 199 | + "Tunnel connected via vnc host=%s port=%s", |
| 200 | + vnc_host, |
182 | 201 | vnc_port, |
183 | 202 | ) |
184 | 203 | except Exception as exc: |
185 | 204 | logger.warning( |
186 | | - "Tunnel VNC connect failed for configured host=%s port=%s: %s", |
187 | | - VNC_HOST, |
| 205 | + "Tunnel VNC connect failed for host=%s port=%s: %s", |
| 206 | + vnc_host, |
188 | 207 | vnc_port, |
189 | 208 | exc, |
190 | 209 | ) |
|
0 commit comments