syed commented on a change in pull request #2204: [CLOUDSTACK-10025] Adding Support for NoVNC Console for KVM URL: https://github.com/apache/cloudstack/pull/2204#discussion_r131290366
########## File path: services/console-proxy/server/src/com/cloud/consoleproxy/WebSocketHandlerForNovnc.java ########## @@ -0,0 +1,505 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.consoleproxy; + +import com.cloud.consoleproxy.vnc.RfbConstants; +import org.apache.log4j.Logger; +import org.eclipse.jetty.server.Request; +import org.eclipse.jetty.websocket.api.Session; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError; +import org.eclipse.jetty.websocket.api.annotations.WebSocket; +import org.eclipse.jetty.websocket.api.extensions.Frame; +import org.eclipse.jetty.websocket.common.WebSocketSession; +import org.eclipse.jetty.websocket.server.WebSocketHandler; +import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.nio.ByteBuffer; +import java.security.spec.KeySpec; +import java.util.Map; + +/** + * Created by root on 18/6/17. + */ + +@WebSocket +public class WebSocketHandlerForNovnc extends WebSocketHandler { + + + public static final Logger s_logger = Logger.getLogger(WebSocketHandlerForNovnc.class.getSimpleName()); + private Socket vncSocket; + private DataInputStream is; + private DataOutputStream os; + private Session session; + private boolean isConnectionStart = false; + private int frameCount; + private String hostPassword; + private int authType; + + private static enum VncState { + SERVER_VERSION_SENT, AUTH_TYPES_SENT, AUTH_RESULT_SENT, UNKNOWN + } + private static final byte[] M_VNC_AUTH_OK = new byte[]{0, 0, 0, 0}; + private static final byte[] M_VNC_AUTH_TYE_NOAUTH = new byte[]{01, 01}; + private VncState clientState; + + /** + * Reverse bits in byte, so least significant bit will be most significant + * bit. E.g. 01001100 will become 00110010. + * <p> + * See also: http://www.vidarholen.net/contents/junk/vnc.html , + * http://bytecrafter + * .blogspot.com/2010/09/des-encryption-as-used-in-vnc.html + * + * @param b a byte + * @return byte in reverse order + */ + private static byte flipByte(byte b) { + int b1_8 = (b & 0x1) << 7; + int b2_7 = (b & 0x2) << 5; + int b3_6 = (b & 0x4) << 3; + int b4_5 = (b & 0x8) << 1; + int b5_4 = (b & 0x10) >>> 1; + int b6_3 = (b & 0x20) >>> 3; + int b7_2 = (b & 0x40) >>> 5; + int b8_1 = (b & 0x80) >>> 7; + byte c = (byte) (b1_8 | b2_7 | b3_6 | b4_5 | b5_4 | b6_3 | b7_2 | b8_1); + return c; + } + + @Override + public void configure(WebSocketServletFactory webSocketServletFactory) { + webSocketServletFactory.register(WebSocketHandlerForNovnc.class); + } + + @Override + public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { + if (this.getWebSocketFactory().isUpgradeRequest(request, response)) { + response.addHeader("Sec-WebSocket-Protocol", "binary"); + if (this.getWebSocketFactory().acceptWebSocket(request, response)) { + baseRequest.setHandled(true); + return; + } + + if (response.isCommitted()) { + return; + } + } + } + + @OnWebSocketConnect + public void onConnect(final Session session) throws IOException, InterruptedException { + + s_logger.info("Connect: " + session.getRemoteAddress().getAddress()); + s_logger.info(session.getUpgradeRequest().getRequestURI()); + System.out.println("Responding to client"); Review comment: Remove all `System.out.println` and replace it with appropriate logger messages ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
