|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.consoleproxy; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import javax.servlet.ServletException; |
| 23 | +import javax.servlet.http.HttpServletRequest; |
| 24 | +import javax.servlet.http.HttpServletResponse; |
| 25 | + |
| 26 | +import com.cloud.consoleproxy.util.Logger; |
| 27 | + |
| 28 | +import org.eclipse.jetty.server.Request; |
| 29 | +import org.eclipse.jetty.websocket.api.Session; |
| 30 | +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; |
| 31 | +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; |
| 32 | +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame; |
| 33 | +import org.eclipse.jetty.websocket.api.annotations.WebSocket; |
| 34 | +import org.eclipse.jetty.websocket.api.extensions.Frame; |
| 35 | +import org.eclipse.jetty.websocket.server.WebSocketHandler; |
| 36 | +import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; |
| 37 | + |
| 38 | +@WebSocket |
| 39 | +public class ConsoleProxyNoVNCHandler extends WebSocketHandler { |
| 40 | + |
| 41 | + private ConsoleProxyNoVncClient viewer; |
| 42 | + private static final Logger s_logger = Logger.getLogger(ConsoleProxyNoVNCHandler.class); |
| 43 | + |
| 44 | + public ConsoleProxyNoVNCHandler() { |
| 45 | + super(); |
| 46 | + s_logger.info("Default constructor"); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public void configure(WebSocketServletFactory webSocketServletFactory) { |
| 51 | + webSocketServletFactory.register(ConsoleProxyNoVNCHandler.class); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) |
| 56 | + throws IOException, ServletException { |
| 57 | + s_logger.info("Hnadling : " + request.getRequestURI()); |
| 58 | + |
| 59 | + if (this.getWebSocketFactory().isUpgradeRequest(request, response)) { |
| 60 | + s_logger.info("SocketRequest : " + request.getRequestURI()); |
| 61 | + response.addHeader("Sec-WebSocket-Protocol", "binary"); |
| 62 | + if (this.getWebSocketFactory().acceptWebSocket(request, response)) { |
| 63 | + baseRequest.setHandled(true); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + if (response.isCommitted()) { |
| 68 | + return; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + super.handle(target, baseRequest, request, response); |
| 73 | + } |
| 74 | + |
| 75 | + @OnWebSocketConnect |
| 76 | + public void onConnect(final Session session) throws IOException, InterruptedException { |
| 77 | + s_logger.info("Request : " + session.getUpgradeRequest().getRequestURI() + " : " |
| 78 | + + session.getUpgradeRequest().getParameterMap()); |
| 79 | + |
| 80 | + String queries = session.getUpgradeRequest().getQueryString(); |
| 81 | + Map<String, String> queryMap = ConsoleProxyHttpHandlerHelper.getQueryMap(queries); |
| 82 | + |
| 83 | + String host = queryMap.get("host"); |
| 84 | + String portStr = queryMap.get("port"); |
| 85 | + String sid = queryMap.get("sid"); |
| 86 | + String tag = queryMap.get("tag"); |
| 87 | + String ticket = queryMap.get("ticket"); |
| 88 | + String ajaxSessionIdStr = queryMap.get("sess"); |
| 89 | + String console_url = queryMap.get("consoleurl"); |
| 90 | + String console_host_session = queryMap.get("sessionref"); |
| 91 | + String vm_locale = queryMap.get("locale"); |
| 92 | + String hypervHost = queryMap.get("hypervHost"); |
| 93 | + String username = queryMap.get("username"); |
| 94 | + String password = queryMap.get("password"); |
| 95 | + |
| 96 | + if (tag == null) |
| 97 | + tag = ""; |
| 98 | + |
| 99 | + long ajaxSessionId = 0; |
| 100 | + int port; |
| 101 | + |
| 102 | + if (host == null || portStr == null || sid == null) |
| 103 | + throw new IllegalArgumentException(); |
| 104 | + |
| 105 | + try { |
| 106 | + port = Integer.parseInt(portStr); |
| 107 | + } catch (NumberFormatException e) { |
| 108 | + s_logger.warn("Invalid number parameter in query string: " + portStr); |
| 109 | + throw new IllegalArgumentException(e); |
| 110 | + } |
| 111 | + |
| 112 | + if (ajaxSessionIdStr != null) { |
| 113 | + try { |
| 114 | + ajaxSessionId = Long.parseLong(ajaxSessionIdStr); |
| 115 | + } catch (NumberFormatException e) { |
| 116 | + s_logger.warn("Invalid number parameter in query string: " + ajaxSessionIdStr); |
| 117 | + throw new IllegalArgumentException(e); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + try { |
| 122 | + ConsoleProxyClientParam param = new ConsoleProxyClientParam(); |
| 123 | + param.setClientHostAddress(host); |
| 124 | + param.setClientHostPort(port); |
| 125 | + param.setClientHostPassword(sid); |
| 126 | + param.setClientTag(tag); |
| 127 | + param.setTicket(ticket); |
| 128 | + param.setClientTunnelUrl(console_url); |
| 129 | + param.setClientTunnelSession(console_host_session); |
| 130 | + param.setLocale(vm_locale); |
| 131 | + param.setHypervHost(hypervHost); |
| 132 | + param.setUsername(username); |
| 133 | + param.setPassword(password); |
| 134 | + |
| 135 | + s_logger.info("Getting viewer"); |
| 136 | + viewer = ConsoleProxy.getNoVncViewer(param, ajaxSessionIdStr, session); |
| 137 | + } catch (Exception e) { |
| 138 | + s_logger.warn("Failed to create viewer due to " + e.getMessage(), e); |
| 139 | + return; |
| 140 | + } |
| 141 | + |
| 142 | + // if (ajaxSessionId == 0 || ajaxSessionId != viewer.getAjaxSessionId()) { |
| 143 | + // if (s_logger.isDebugEnabled()) |
| 144 | + // s_logger.debug("Ajax request comes from a different session, id in request: " + ajaxSessionId |
| 145 | + // + ", id in viewer: " + viewer.getAjaxSessionId()); |
| 146 | + // session.getRemote().sendBytes(ByteBuffer.wrap("Invalid ajax client session id".getBytes())); |
| 147 | + // } |
| 148 | + |
| 149 | + s_logger.info("Got viewer"); |
| 150 | + } |
| 151 | + |
| 152 | + @OnWebSocketClose |
| 153 | + public void onClose(Session session, int statusCode, String reason) throws IOException, InterruptedException { |
| 154 | + s_logger.info("Closing"); |
| 155 | + ConsoleProxy.removeViewer(viewer); |
| 156 | + } |
| 157 | + |
| 158 | + @OnWebSocketFrame |
| 159 | + public void onFrame(Frame f) throws IOException { |
| 160 | + viewer.sendClientFrame(f); |
| 161 | + } |
| 162 | +} |
0 commit comments