diff --git a/services/console-proxy/plugin/pom.xml b/services/console-proxy/plugin/pom.xml new file mode 100644 index 00000000000..8cf3d76aa00 --- /dev/null +++ b/services/console-proxy/plugin/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + cloud-plugin-console-proxy + Apache CloudStack Console Proxy Plugin + pom + + org.apache.cloudstack + cloud-service-console-proxy + 4.1.0-SNAPSHOT + ../pom.xml + + + install + src + test + + diff --git a/services/console-proxy/pom.xml b/services/console-proxy/pom.xml new file mode 100644 index 00000000000..cd57526273d --- /dev/null +++ b/services/console-proxy/pom.xml @@ -0,0 +1,37 @@ + + + 4.0.0 + cloud-service-console-proxy + Apache CloudStack Console Proxy Service + pom + + org.apache.cloudstack + cloud-services + 4.1.0-SNAPSHOT + ../pom.xml + + + install + + + plugin + server + + diff --git a/services/console-proxy/server/bindir/cloud-setup-console-proxy.in b/services/console-proxy/server/bindir/cloud-setup-console-proxy.in new file mode 100755 index 00000000000..6439c0fc329 --- /dev/null +++ b/services/console-proxy/server/bindir/cloud-setup-console-proxy.in @@ -0,0 +1,220 @@ +#!/usr/bin/env python + +# 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. + + +import sys, os, subprocess, errno, re, getopt + +# ---- This snippet of code adds the sources path and the waf configured PYTHONDIR to the Python path ---- +# ---- We do this so cloud_utils can be looked up in the following order: +# ---- 1) Sources directory +# ---- 2) waf configured PYTHONDIR +# ---- 3) System Python path +for pythonpath in ( + "@PYTHONDIR@", + os.path.join(os.path.dirname(__file__),os.path.pardir,os.path.pardir,"python","lib"), + ): + if os.path.isdir(pythonpath): sys.path.insert(0,pythonpath) +# ---- End snippet of code ---- +import cloud_utils +from cloud_utils import stderr + +E_GENERIC= 1 +E_NOKVM = 2 +E_NODEFROUTE = 3 +E_DHCP = 4 +E_NOPERSISTENTNET = 5 +E_NETRECONFIGFAILED = 6 +E_VIRTRECONFIGFAILED = 7 +E_FWRECONFIGFAILED = 8 +E_CPRECONFIGFAILED = 9 +E_CPFAILEDTOSTART = 10 +E_NOFQDN = 11 + +def bail(errno=E_GENERIC,message=None,*args): + if message: stderr(message,*args) + stderr("Cloud Console Proxy setup aborted") + sys.exit(errno) + + +#---------------- boilerplate for python 2.4 support + + +# CENTOS does not have this -- we have to put this here +try: + from subprocess import check_call + from subprocess import CalledProcessError +except ImportError: + def check_call(*popenargs, **kwargs): + import subprocess + retcode = subprocess.call(*popenargs, **kwargs) + cmd = kwargs.get("args") + if cmd is None: cmd = popenargs[0] + if retcode: raise CalledProcessError(retcode, cmd) + return retcode + + class CalledProcessError(Exception): + def __init__(self, returncode, cmd): + self.returncode = returncode ; self.cmd = cmd + def __str__(self): return "Command '%s' returned non-zero exit status %d" % (self.cmd, self.returncode) + +# ------------ end boilerplate ------------------------- + +def check_hostname(): return check_call(["hostname",'--fqdn']) + +class Command: + def __init__(self,name,parent=None): + self.__name = name + self.__parent = parent + def __getattr__(self,name): + if name == "_print": name = "print" + return Command(name,self) + def __call__(self,*args): + cmd = self.__get_recursive_name() + list(args) + #print " ",cmd + popen = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE) + m = popen.communicate() + ret = popen.wait() + if ret: + e = CalledProcessError(ret,cmd) + e.stdout,e.stderr = m + raise e + class CommandOutput: + def __init__(self,stdout,stderr): + self.stdout = stdout + self.stderr = stderr + return CommandOutput(*m) + def __lt__(self,other): + cmd = self.__get_recursive_name() + #print " ",cmd,"<",other + popen = subprocess.Popen(cmd,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE) + m = popen.communicate(other) + ret = popen.wait() + if ret: + e = CalledProcessError(ret,cmd) + e.stdout,e.stderr = m + raise e + class CommandOutput: + def __init__(self,stdout,stderr): + self.stdout = stdout + self.stderr = stderr + return CommandOutput(*m) + + def __get_recursive_name(self,sep=None): + m = self + l = [] + while m is not None: + l.append(m.__name) + m = m.__parent + l.reverse() + if sep: return sep.join(l) + else: return l + def __str__(self): + return ''%self.__get_recursive_name(sep=" ") + + def __repr__(self): return self.__str__() + +ip = Command("ip") +service = Command("service") +chkconfig = Command("chkconfig") +ufw = Command("ufw") +iptables = Command("iptables") +augtool = Command("augtool") +ifconfig = Command("ifconfig") +uuidgen = Command("uuidgen") + +Fedora = os.path.exists("/etc/fedora-release") +CentOS = os.path.exists("/etc/centos-release") or ( os.path.exists("/etc/redhat-release") and not os.path.exists("/etc/fedora-release") ) + +#--------------- procedure starts here ------------ + +def main(): + # parse cmd line + opts, args = getopt.getopt(sys.argv[1:], "a", ["host=", "zone=", "pod="]) + host=None + zone=None + pod=None + autoMode=False + do_check_kvm = True + for opt, arg in opts: + if opt == "--host": + if arg != "": + host = arg + elif opt == "--zone": + if arg != "": + zone = arg + elif opt == "--pod": + if arg != "": + pod = arg + elif opt == "-a": + autoMode=True + servicename = "@PACKAGE@-console-proxy" + + if autoMode: + cloud_utils.setLogFile("/var/log/cloud/setupConsoleProxy.log") + + stderr("Welcome to the Cloud Console Proxy setup") + stderr("") + + try: + check_hostname() + stderr("The hostname of this machine is properly set up") + except CalledProcessError,e: + bail(E_NOFQDN,"This machine does not have an FQDN (fully-qualified domain name) for a hostname") + + stderr("Stopping the Cloud Console Proxy") + cloud_utils.stop_service(servicename) + stderr("Cloud Console Proxy stopped") + + ports = "8002".split() + if Fedora or CentOS: + try: + o = chkconfig("--list","iptables") + if ":on" in o.stdout and os.path.exists("/etc/sysconfig/iptables"): + stderr("Setting up firewall rules to permit traffic to Cloud services") + service.iptables.start() ; print o.stdout + o.stderr + for p in ports: iptables("-I","INPUT","1","-p","tcp","--dport",p,'-j','ACCEPT') + o = service.iptables.save() ; print o.stdout + o.stderr + except CalledProcessError,e: + print e.stdout+e.stderr + bail(E_FWRECONFIGFAILED,"Firewall rules could not be set") + else: + stderr("Setting up firewall rules to permit traffic to Cloud services") + try: + for p in ports: ufw.allow(p) + stderr("Rules set") + except CalledProcessError,e: + print e.stdout+e.stderr + bail(E_FWRECONFIGFAILED,"Firewall rules could not be set") + + stderr("We are going to enable ufw now. This may disrupt network connectivity and service availability. See the ufw documentation for information on how to manage ufw firewall policies.") + try: + o = ufw.enable < "y\n" ; print o.stdout + o.stderr + except CalledProcessError,e: + print e.stdout+e.stderr + bail(E_FWRECONFIGFAILED,"Firewall could not be enabled") + + cloud_utils.setup_consoleproxy_config("@CPSYSCONFDIR@/agent.properties", host, zone, pod) + stderr("Enabling and starting the Cloud Console Proxy") + cloud_utils.enable_service(servicename) + stderr("Cloud Console Proxy restarted") + +if __name__ == "__main__": + main() + +# FIXMES: 1) nullify networkmanager on ubuntu (asking the user first) and enable the networking service permanently diff --git a/services/console-proxy/server/certs/localhost.crt b/services/console-proxy/server/certs/localhost.crt new file mode 100644 index 00000000000..005d98b8c72 --- /dev/null +++ b/services/console-proxy/server/certs/localhost.crt @@ -0,0 +1,22 @@ +-----BEGIN CERTIFICATE----- +MIIDrTCCApWgAwIBAgIBADANBgkqhkiG9w0BAQUFADBxMQswCQYDVQQGEwJVUzEL +MAkGA1UECAwCQ0ExEjAQBgNVBAcMCUN1cGVydGlubzESMBAGA1UECgwJQ2xvdWQu +Y29tMRAwDgYDVQQLDAdEZWZhdWx0MRswGQYDVQQDDBJTZWNvbmRhcnlTdG9yYWdl +Vk0wHhcNMTAwNTI3MTgzNjI1WhcNMTMwMjIwMTgzNjI1WjBxMQswCQYDVQQGEwJV +UzELMAkGA1UECAwCQ0ExEjAQBgNVBAcMCUN1cGVydGlubzESMBAGA1UECgwJQ2xv +dWQuY29tMRAwDgYDVQQLDAdEZWZhdWx0MRswGQYDVQQDDBJTZWNvbmRhcnlTdG9y +YWdlVk0wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCbhstQ5Gn2gzrk +ZX1es+tuz4rnrcONRHUzyY/UdoT5jiVfmmS9CML/GgSpKzmnEMNZcCSh7G/GKPwD +gBZywpTVD56nYT4ZzK0GjPxcg0a+BvxxA2esQ2/kFBvtdcZ1TNExtjdOqysjK0It +M6U2891wbn+Y9oHqooTA0uaZELTpe/MCg2eBx7A4+u26novHHfOaKEEqtBscpDP8 +0+nQduNQf61haV25Lv2CDqrCIuv/FrNmgQhcUg8e1dFkk4VCsflEDuSYh9PpaD7J +t+oqmNTVw8k6u3JAYJFkcu457uYz0wrED7Cai7Y6gUy7xwmY2SSY/r2mJJHEpSpZ +NhiH47kZAgMBAAGjUDBOMB0GA1UdDgQWBBQ2hUX5Jdhn277SBisACnEABqg52zAf +BgNVHSMEGDAWgBQ2hUX5Jdhn277SBisACnEABqg52zAMBgNVHRMEBTADAQH/MA0G +CSqGSIb3DQEBBQUAA4IBAQBAVrkGGDPHDPQdZRgI1+1L87sX5xdNoet9sJUVRtz9 +ZwhGWAmca30cJGlhSFNx5y01E6T7lHDLrF9HCf9nVC10t0FwQwTVYijE8VyM7fAA +4Hv/whSKg1kkQQSTis2ZW0wMU6TnanhJy6MrxwdhRampeXjNOpNtoLrF/5LbhWxO +Gm0S5u+4q7eYnUPD4o3sb17idG62kkejHeToPnJwXtDwyq0XYNlL/OoqRaaY5f0b +IKdeqqEkdtkzfU4N1dG7bJA29gBl48gPn+CSrh9u3D0s1OYM7MWi1/TjpwCR18ir +CslVzO6kVNyQoNEYhZ9+2Sz0ceZVrYDFFzp8qAF8qbZ7 +-----END CERTIFICATE----- diff --git a/services/console-proxy/server/certs/localhost.key b/services/console-proxy/server/certs/localhost.key new file mode 100644 index 00000000000..6d95765e4f0 --- /dev/null +++ b/services/console-proxy/server/certs/localhost.key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAm4bLUORp9oM65GV9XrPrbs+K563DjUR1M8mP1HaE+Y4lX5pk +vQjC/xoEqSs5pxDDWXAkoexvxij8A4AWcsKU1Q+ep2E+GcytBoz8XINGvgb8cQNn +rENv5BQb7XXGdUzRMbY3TqsrIytCLTOlNvPdcG5/mPaB6qKEwNLmmRC06XvzAoNn +gcewOPrtup6Lxx3zmihBKrQbHKQz/NPp0HbjUH+tYWlduS79gg6qwiLr/xazZoEI +XFIPHtXRZJOFQrH5RA7kmIfT6Wg+ybfqKpjU1cPJOrtyQGCRZHLuOe7mM9MKxA+w +mou2OoFMu8cJmNkkmP69piSRxKUqWTYYh+O5GQIDAQABAoIBAQCI5S8VNtimaYBv +BX5C26+BzCECKIsWT4myWdrGMsR9PUdcTXQaiBnLncU4epm2miS5FuLHvGvuSqj5 +E8eun+ONXsBRqGCXKPer6nE/pTWhklilyU9566oTYjfq3l4fZcxFK5SnJDdGL4+C +ZhEou6LQkhKyO1jDhOXBLGJZnMEBOf+zXhgZGLDbQwCcCQ5PAZUiSf0cnVRk3rI9 +GwdsbCDNJk6awy8ANlFATemDvHwHZ7ZwmvVzsjsYlYJbY/vJYlOyxa7tzYROVAlm +m8oiPfDvGjnXcGxVQwT0pgDvwtUkUFijZORpkJOPavxqSkpOzDFrOe9UW6HJYGzH +ujhmZBVhAoGBAMnbfssRwYI5egfsxNbA62NkxRcGe5HIlnyZ1Ln0BCE8ae60frcf +4IluZzT53Ly3jUQP2uGdp6nJVq/5ymRYSySNr46DXek2qcJ2TMbtRBhjebPwi+Rb +qTNjccNSgqs0j+1qP78PoTUO5fUWGL9XqIlfPU6Vji4+SmdNMvz6z84lAoGBAMU9 +/3l2WVXykD6FNrRbtvpzU1C9LDixOaVVpGnZHO8krW440LnhIZJbkXnXDVZXc7eI +D/doecfL8rtFteqeBr0LiXGQXU5mIMjXAOAPSvDqIciYzhL8KOK5CWEgVZo6RQlU +G3ne0mk93I+w8N0SE2VKeuxepz4yw0oiKUpAgWrlAoGAS06qNRSAvxa2YjKBFSWQ +K9qydO6kNzVAf2fcpytURxoE41tPUv5/hIY91tPI+Fb6SwQnQrjQjlVhE/H7Agi2 +sAJ0FpUH+jO8jaIY7rYiC39BLlJ1vlI8A8H79UTZHwpTD93tvlgUankObas6vFf1 +tppjgufkzXfLxlJUzXC9CkkCgYA7gy9YOKtP0XZQRuVwmdOl0bIrxEhZeq/IAQUw +or+mMEzb2uyviQwWGubT+l0d1hkmITmgDrff3tuIQcpX1gJ2e8qmp0Zf51SxBJ5Q +/IxCEILNAb374HV9oxL/aUAq3rYB0IzRwrd95ZptCJhEO7X6c/SO6ShRDgP6lEAd +FUV3OQKBgQCFC0Xx/fCX1yquARgoe5pbK7IpXWaTvjBu//gbHsfR2lk3dZbESdeg +OquPDdfp+jFPGISsDhPLzcfkZIPbz5ZVs8KdmpB/YLwyJwFqjDyjwVaDnRnuycb1 +/4PlVWKp7j5SDDNCfleYvmiRn8k6P4mxVJOHKzwb/IwQcKghyqAF1w== +-----END RSA PRIVATE KEY----- diff --git a/services/console-proxy/server/certs/realhostip.crt b/services/console-proxy/server/certs/realhostip.crt new file mode 100644 index 00000000000..7520b0ca6c4 --- /dev/null +++ b/services/console-proxy/server/certs/realhostip.crt @@ -0,0 +1,31 @@ +-----BEGIN CERTIFICATE----- +MIIFZTCCBE2gAwIBAgIHKBCduBUoKDANBgkqhkiG9w0BAQUFADCByjELMAkGA1UE +BhMCVVMxEDAOBgNVBAgTB0FyaXpvbmExEzARBgNVBAcTClNjb3R0c2RhbGUxGjAY +BgNVBAoTEUdvRGFkZHkuY29tLCBJbmMuMTMwMQYDVQQLEypodHRwOi8vY2VydGlm +aWNhdGVzLmdvZGFkZHkuY29tL3JlcG9zaXRvcnkxMDAuBgNVBAMTJ0dvIERhZGR5 +IFNlY3VyZSBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTERMA8GA1UEBRMIMDc5Njky +ODcwHhcNMTIwMjAzMDMzMDQwWhcNMTcwMjA3MDUxMTIzWjBZMRkwFwYDVQQKDBAq +LnJlYWxob3N0aXAuY29tMSEwHwYDVQQLDBhEb21haW4gQ29udHJvbCBWYWxpZGF0 +ZWQxGTAXBgNVBAMMECoucmVhbGhvc3RpcC5jb20wggEiMA0GCSqGSIb3DQEBAQUA +A4IBDwAwggEKAoIBAQCDT9AtEfs+s/I8QXp6rrCw0iNJ0+GgsybNHheU+JpL39LM +TZykCrZhZnyDvwdxCoOfE38Sa32baHKNds+y2SHnMNsOkw8OcNucHEBX1FIpOBGp +h9D6xC+umx9od6xMWETUv7j6h2u+WC3OhBM8fHCBqIiAol31/IkcqDxxsHlQ8S/o +CfTlXJUY6Yn628OA1XijKdRnadV0hZ829cv/PZKljjwQUTyrd0KHQeksBH+YAYSo +2JUl8ekNLsOi8/cPtfojnltzRI1GXi0ZONs8VnDzJ0a2gqZY+uxlz+CGbLnGnlN4 +j9cBpE+MfUE+35Dq121sTpsSgF85Mz+pVhn2S633AgMBAAGjggG+MIIBujAPBgNV +HRMBAf8EBTADAQEAMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAOBgNV +HQ8BAf8EBAMCBaAwMwYDVR0fBCwwKjAooCagJIYiaHR0cDovL2NybC5nb2RhZGR5 +LmNvbS9nZHMxLTY0LmNybDBTBgNVHSAETDBKMEgGC2CGSAGG/W0BBxcBMDkwNwYI +KwYBBQUHAgEWK2h0dHA6Ly9jZXJ0aWZpY2F0ZXMuZ29kYWRkeS5jb20vcmVwb3Np +dG9yeS8wgYAGCCsGAQUFBwEBBHQwcjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3Au +Z29kYWRkeS5jb20vMEoGCCsGAQUFBzAChj5odHRwOi8vY2VydGlmaWNhdGVzLmdv +ZGFkZHkuY29tL3JlcG9zaXRvcnkvZ2RfaW50ZXJtZWRpYXRlLmNydDAfBgNVHSME +GDAWgBT9rGEyk2xF1uLuhV+auud2mWjM5zArBgNVHREEJDAighAqLnJlYWxob3N0 +aXAuY29tgg5yZWFsaG9zdGlwLmNvbTAdBgNVHQ4EFgQUZyJz9/QLy5TWIIscTXID +E8Xk47YwDQYJKoZIhvcNAQEFBQADggEBAKiUV3KK16mP0NpS92fmQkCLqm+qUWyN +BfBVgf9/M5pcT8EiTZlS5nAtzAE/eRpBeR3ubLlaAogj4rdH7YYVJcDDLLoB2qM3 +qeCHu8LFoblkb93UuFDWqRaVPmMlJRnhsRkL1oa2gM2hwQTkBDkP7w5FG1BELCgl +gZI2ij2yxjge6pOEwSyZCzzbCcg9pN+dNrYyGEtB4k+BBnPA3N4r14CWbk+uxjrQ +6j2Ip+b7wOc5IuMEMl8xwTyjuX3lsLbAZyFI9RCyofwA9NqIZ1GeB6Zd196rubQp +93cmBqGGjZUs3wMrGlm7xdjlX6GQ9UvmvkMub9+lL99A5W50QgCmFeI= +-----END CERTIFICATE----- diff --git a/services/console-proxy/server/certs/realhostip.csr b/services/console-proxy/server/certs/realhostip.csr new file mode 100644 index 00000000000..61395c9f8d9 --- /dev/null +++ b/services/console-proxy/server/certs/realhostip.csr @@ -0,0 +1,15 @@ +-----BEGIN NEW CERTIFICATE REQUEST----- +MIICsDCCAZgCAQAwazELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRIwEAYDVQQHEwlDdXBlcnRp +bm8xDjAMBgNVBAoTBVZNT3BzMRAwDgYDVQQLEwdVbmtub3duMRkwFwYDVQQDDBAqLnJlYWxob3N0 +aXAuY29tMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAg0/QLRH7PrPyPEF6eq6wsNIj +SdPhoLMmzR4XlPiaS9/SzE2cpAq2YWZ8g78HcQqDnxN/Emt9m2hyjXbPstkh5zDbDpMPDnDbnBxA +V9RSKTgRqYfQ+sQvrpsfaHesTFhE1L+4+odrvlgtzoQTPHxwgaiIgKJd9fyJHKg8cbB5UPEv6An0 +5VyVGOmJ+tvDgNV4oynUZ2nVdIWfNvXL/z2SpY48EFE8q3dCh0HpLAR/mAGEqNiVJfHpDS7DovP3 +D7X6I55bc0SNRl4tGTjbPFZw8ydGtoKmWPrsZc/ghmy5xp5TeI/XAaRPjH1BPt+Q6tdtbE6bEoBf +OTM/qVYZ9kut9wIDAQABoAAwDQYJKoZIhvcNAQEFBQADggEBAF5lhhni9dW9MqSL2ixNbViPWpFS +ecOggshYChJfZKrhsuZaDpumJ/+ebICS4zv/oxDwNLSmeAmydiaUQC9LFQEEwvPBYDTtTzwCrtwH +yyFJQSm6pyeIBP/Bih/5hLW8JPm0bDbp5ldrHCDEgKQeeyQhyYOKFODkTuMLw+FLD+V86IVHxElL +/urCRWyZEPwyMsgfsU6ywNX9XNShyk1uDHjFDE67sPhfw52ooxXrYQnBdTk+g0UXPbULzrCK/1kU +fjRq347V9Fwi5NFyGADOaA+q6mtnlb1i3uH1n1YVUzevvpnIr3/RxPSYHB47Kj9iYKeDlYdTRHJy +NpuvTfmQO2Y= +-----END NEW CERTIFICATE REQUEST----- diff --git a/services/console-proxy/server/certs/realhostip.key b/services/console-proxy/server/certs/realhostip.key new file mode 100644 index 00000000000..53bdc86e273 --- /dev/null +++ b/services/console-proxy/server/certs/realhostip.key @@ -0,0 +1,24 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCDT9AtEfs+s/I8QXp6rrCw0iNJ +0+GgsybNHheU+JpL39LMTZykCrZhZnyDvwdxCoOfE38Sa32baHKNds+y2SHnMNsOkw8OcNucHEBX +1FIpOBGph9D6xC+umx9od6xMWETUv7j6h2u+WC3OhBM8fHCBqIiAol31/IkcqDxxsHlQ8S/oCfTl +XJUY6Yn628OA1XijKdRnadV0hZ829cv/PZKljjwQUTyrd0KHQeksBH+YAYSo2JUl8ekNLsOi8/cP +tfojnltzRI1GXi0ZONs8VnDzJ0a2gqZY+uxlz+CGbLnGnlN4j9cBpE+MfUE+35Dq121sTpsSgF85 +Mz+pVhn2S633AgMBAAECggEAH/Szd9RxbVADenCA6wxKSa3KErRyq1YN8ksJeCKMAj0FIt0caruE +qO11DebWW8cwQu1Otl/cYI6pmg24/BBldMrp9IELX/tNJo+lhPpRyGAxxC0eSXinFfoASb8d+jJd +Bd1mmemM6fSxqRlxSP4LrzIhjhR1g2CiyYuTsiM9UtoVKGyHwe7KfFwirUOJo3Mr18zUVNm7YqY4 +IVhOSq59zkH3ULBlYq4bG50jpxa5mNSCZ7IpafPY/kE/CbR+FWNt30+rk69T+qb5abg6+XGm+OAm +bnQ18yZEqX6nJLk7Ch0cfA5orGgrTMOrM71wK7tBBDQ308kOxDGebx6j0qD36QKBgQDTRDr8kuhA +9sUyKr9vk2DQCMpNvEeiwI3JRMqmmxpNAtg01aJ3Ya57vX5Fc+zcuV87kP6FM1xgpHQvnw5LWo2J +s7ANwQcP8ricEW5zkZhSjI4ssMeAubmsHOloGxmLFYZqwx0JI7CWViGTLMcUlqKblmHcjeQDeDfP +P1TaCItFmwKBgQCfHZwVvIcaDs5vxVpZ4ftvflIrW8qq0uOVK6QIf9A/YTGhCXl2qxxTg2A6+0rg +ZqI7zKzUDxIbVv0KlgCbpHDC9d5+sdtDB3wW2pimuJ3p1z4/RHb4n/lDwXCACZl1S5l24yXX2pFZ +wdPCXmy5PYkHMssFLNhI24pprUIQs66M1QKBgQDQwjAjWisD3pRXESSfZRsaFkWJcM28hdbVFhPF +c6gWhwQLmTp0CuL2RPXcPUPFi6sN2iWWi3zxxi9Eyz+9uBn6AsOpo56N5MME/LiOnETO9TKb+Ib6 +rQtKhjshcv3XkIqFPo2XdVvOAgglPO7vajX91iiXXuH7h7RmJud6l0y/lwKBgE+bi90gLuPtpoEr +VzIDKz40ED5bNYHT80NNy0rpT7J2GVN9nwStRYXPBBVeZq7xCpgqpgmO5LtDAWULeZBlbHlOdBwl +NhNKKl5wzdEUKwW0yBL1WSS5PQgWPwgARYP25/ggW22sj+49WIo1neXsEKPGWObk8e050f1fTt92 +Vo1lAoGAb1gCoyBCzvi7sqFxm4V5oapnJeiQQJFjhoYWqGa26rQ+AvXXNuBcigIeDXNJPctSF0Uc +p11KbbCgiruBbckvM1vGsk6Sx4leRk+IFHRpJktFUek4o0eUg0shOsyyvyet48Dfg0a8FvcxROs0 +gD+IYds5doiob/hcm1hnNB/3vk4= +-----END PRIVATE KEY----- \ No newline at end of file diff --git a/services/console-proxy/server/certs/realhostip.keystore b/services/console-proxy/server/certs/realhostip.keystore new file mode 100644 index 00000000000..c8d54d47eba Binary files /dev/null and b/services/console-proxy/server/certs/realhostip.keystore differ diff --git a/services/console-proxy/server/conf.dom0/agent.properties.in b/services/console-proxy/server/conf.dom0/agent.properties.in new file mode 100644 index 00000000000..1920481c03f --- /dev/null +++ b/services/console-proxy/server/conf.dom0/agent.properties.in @@ -0,0 +1,46 @@ +# 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. + +# Sample configuration file for VMOPS console proxy + +instance=ConsoleProxy +consoleproxy.httpListenPort=8002 + +#resource= the java class, which agent load to execute +resource=com.cloud.agent.resource.consoleproxy.ConsoleProxyResource + +#host= The IP address of management server +host=localhost + +#port = The port management server listening on, default is 8250 +port=8250 + +#pod= The pod, which agent belonged to +pod=default + +#zone= The zone, which agent belonged to +zone=default + +#private.network.device= the private nic device +# if this is commented, it is autodetected on service startup +# private.network.device=cloudbr0 + +#public.network.device= the public nic device +# if this is commented, it is autodetected on service startup +# public.network.device=cloudbr0 + +#guid= a GUID to identify the agent diff --git a/services/console-proxy/server/conf.dom0/consoleproxy.properties.in b/services/console-proxy/server/conf.dom0/consoleproxy.properties.in new file mode 100644 index 00000000000..a3cddbcab96 --- /dev/null +++ b/services/console-proxy/server/conf.dom0/consoleproxy.properties.in @@ -0,0 +1,23 @@ +# 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. + +consoleproxy.tcpListenPort=0 +consoleproxy.httpListenPort=80 +consoleproxy.httpCmdListenPort=8001 +consoleproxy.jarDir=./applet/ +consoleproxy.viewerLinger=180 +consoleproxy.reconnectMaxRetry=5 diff --git a/services/console-proxy/server/conf.dom0/log4j-cloud.xml.in b/services/console-proxy/server/conf.dom0/log4j-cloud.xml.in new file mode 100644 index 00000000000..dde844fabe4 --- /dev/null +++ b/services/console-proxy/server/conf.dom0/log4j-cloud.xml.in @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/services/console-proxy/server/conf/agent.properties b/services/console-proxy/server/conf/agent.properties new file mode 100644 index 00000000000..4e217f21100 --- /dev/null +++ b/services/console-proxy/server/conf/agent.properties @@ -0,0 +1,19 @@ +# 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. + +instance=ConsoleProxy +resource=com.cloud.agent.resource.consoleproxy.ConsoleProxyResource diff --git a/services/console-proxy/server/conf/consoleproxy.properties b/services/console-proxy/server/conf/consoleproxy.properties new file mode 100644 index 00000000000..bb452f5823c --- /dev/null +++ b/services/console-proxy/server/conf/consoleproxy.properties @@ -0,0 +1,23 @@ +# 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. + +consoleproxy.tcpListenPort=0 +consoleproxy.httpListenPort=8088 +consoleproxy.httpCmdListenPort=8001 +consoleproxy.jarDir=./applet/ +consoleproxy.viewerLinger=180 +consoleproxy.reconnectMaxRetry=5 diff --git a/services/console-proxy/server/conf/log4j-cloud.xml b/services/console-proxy/server/conf/log4j-cloud.xml new file mode 100644 index 00000000000..5b31c9db967 --- /dev/null +++ b/services/console-proxy/server/conf/log4j-cloud.xml @@ -0,0 +1,102 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/services/console-proxy/server/css/ajaxviewer.css b/services/console-proxy/server/css/ajaxviewer.css new file mode 100644 index 00000000000..5ea552b176f --- /dev/null +++ b/services/console-proxy/server/css/ajaxviewer.css @@ -0,0 +1,144 @@ +/* +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. +*/ + +body { + margin:0 0; + text-align: center; +} + +#main_panel { + clear:both; + margin: 0 auto; + text-align: left; +} + +.canvas_tile { + cursor:crosshair; +} + +#toolbar { + font:normal 12px 'Trebuchet MS','Arial'; + margin:0 auto; + text-align: left; + padding:0 0; + height:32px; + background-image:url(/resource/images/back.gif); + background-repeat:repeat-x; +} + +#toolbar ul { + margin:0 0; + padding:0 10px 0 10px; + float:left; + display:block; + line-height:32px; + list-style:none; +} + +#toolbar li { + float:left; + display:inline; + padding:0; + height:32px; +} + +#toolbar a { + color:white; + float:left; + display:block; + padding:0 3px 0 3px; + text-decoration:none; + line-height:32px; +} + +#toolbar a span { + display:block; + float:none; + padding:0 10px 0 7px; +} + +#toolbar a span img { + border:none; + margin:8px 4px 0 0; +} + +#toolbar a:hover { + background: url(/resource/images/left.png) no-repeat left center; +} + +#toolbar a:hover span { + background:url(/resource/images/right.png) no-repeat right center; +} + + +#toolbar ul li ul { + position: absolute; + top:32; + width: 260; + height: 65; + display: block; + display: none; + border-top: 1px solid black; + background-image:url(/resource/images/back.gif); + background-repeat:repeat-x repeat-y; +} + +#toolbar ul li ul li { + display: list-item; + float:none; + padding-left: 20; +} + +#toolbar ul li ul li.current { + background: url(/resource/images/cad.gif) no-repeat left center; +} + +#toolbar ul li ul li a { + display:block; + padding:0 3px 0 3px; + text-decoration:none; + line-height:32px; + vertical-align: bottom; /* this is to fix the list gap in IE */ +} + +#toolbar ul li ul li a:hover { + background: url(/resource/images/left.png) no-repeat left center; +} + +#toolbar ul li ul li a:hover span { + background: url(/resource/images/right2.png) no-repeat right center; +} + +span.dark { + margin-right:20px; + float:right; + display:block; + width:32px; + height:30px; + background:url(/resource/images/gray-green.png) no-repeat center center; +} + +span.bright { + margin-right:20px; + float:right; + display:block; + width:32px; + height:30px; + background:url(/resource/images/bright-green.png) no-repeat center center; +} diff --git a/services/console-proxy/server/css/logger.css b/services/console-proxy/server/css/logger.css new file mode 100644 index 00000000000..42ac8071ed5 --- /dev/null +++ b/services/console-proxy/server/css/logger.css @@ -0,0 +1,139 @@ +/* +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. +*/ + +@charset "UTF-8"; +.logwin { + position: absolute; + + z-index:2147483648; + width: 800px; + border: 1px solid gray; + background: white; + text-align: left; +} + +.logwin_title{ + width:auto; + height: 23px; + background:url(../images/grid_headerbg.gif) repeat-x top left; + border: 1px sold #737373; +} + +.logwin_title_actionbox{ + width:175px; + height:16px; + float:left; + margin:4px 0 0 7px; + display:inline; +} + + +.logwin_title_actionbox .select { + background: #424242; + font: normal 10px Arial, Helvetica, sans-serif; + float:left; + border: 1px solid #6e6e6e; + height: 16px; + width: 100px; + margin-left:3px; + padding:0 0 0 3px; + color:#CCC; +} + +.logwin_title_rgtactionbox{ + width:49px; + height:15px; + float:right; + margin:4px 0 0 7px; + display:inline; +} + + +a.logwin_playbutton { + width:18px; + height:15px; + float:left; + background:url(../images/play_button.gif) no-repeat top left; + margin-right:2px; + padding:0; +} + +a:hover.logwin_playbutton { + background:url(../images/play_button_hover.gif) no-repeat top left; +} + +a.logwin_stopbutton { + width:18px; + height:15px; + float:left; + background:url(../images/stop_button.gif) no-repeat top left; + margin-right:2px; + padding:0; +} + +a:hover.logwin_stopbutton { + background:url(../images/stop_button_hover.gif) no-repeat top left; +} + +a.logwin_clrbutton { + width:28px; + height:15px; + float:left; + background:url(../images/clr_button.gif) no-repeat top left; + margin:0; + padding:0; +} + +a:hover.logwin_clrbutton { + background:url(../images/clr_button_hover.gif) no-repeat top left; +} + +a.logwin_shrinkbutton { + width:18px; + height:15px; + float:right; + background:url(../images/shrink_button.gif) no-repeat top left; + margin-right:7px; + margin-top:4px; + padding:0; +} + +a:hover.logwin_shrinkbutton { + background:url(../images/shrink_button_hover.gif) no-repeat top left; +} + +a.logwin_minimizebutton { + width:18px; + height:15px; + float:left; + background:url(../images/minimize_button.gif) no-repeat top left; + margin-right:2px; + padding:0; +} + +a:hover.logwin_minimizebutton { + background:url(../images/minimize_button_hover.gif) no-repeat top left; +} + +.logwin_content { + overflow:scroll; + height: 477px; + background: white; +} + diff --git a/services/console-proxy/server/distro/centos/SYSCONFDIR/rc.d/init.d/cloud-console-proxy.in b/services/console-proxy/server/distro/centos/SYSCONFDIR/rc.d/init.d/cloud-console-proxy.in new file mode 100644 index 00000000000..47fdaef1d96 --- /dev/null +++ b/services/console-proxy/server/distro/centos/SYSCONFDIR/rc.d/init.d/cloud-console-proxy.in @@ -0,0 +1,98 @@ +#!/bin/bash + +# chkconfig: 35 99 10 +# description: Cloud Console Proxy + +# 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. + +# WARNING: if this script is changed, then all other initscripts MUST BE changed to match it as well + +. /etc/rc.d/init.d/functions + +whatami=cloud-console-proxy + +# set environment variables + +SHORTNAME="$whatami" +PIDFILE=@PIDDIR@/"$whatami".pid +LOCKFILE=@LOCKDIR@/"$SHORTNAME" +LOGFILE=@CPLOG@ +PROGNAME="Cloud Console Proxy" + +unset OPTIONS +[ -r @SYSCONFDIR@/sysconfig/"$SHORTNAME" ] && source @SYSCONFDIR@/sysconfig/"$SHORTNAME" +DAEMONIZE=@BINDIR@/@PACKAGE@-daemonize +PROG=@LIBEXECDIR@/console-proxy-runner + +start() { + echo -n $"Starting $PROGNAME: " + if hostname --fqdn >/dev/null 2>&1 ; then + daemon --check=$SHORTNAME --pidfile=${PIDFILE} "$DAEMONIZE" \ + -n "$SHORTNAME" -p "$PIDFILE" -l "$LOGFILE" "$PROG" $OPTIONS + RETVAL=$? + echo + else + failure + echo + echo The host name does not resolve properly to an IP address. Cannot start "$PROGNAME". > /dev/stderr + RETVAL=9 + fi + [ $RETVAL = 0 ] && touch ${LOCKFILE} + return $RETVAL +} + +stop() { + echo -n $"Stopping $PROGNAME: " + killproc -p ${PIDFILE} $SHORTNAME # -d 10 $SHORTNAME + RETVAL=$? + echo + [ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE} +} + + +# See how we were called. +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status -p ${PIDFILE} $SHORTNAME + RETVAL=$? + ;; + restart) + stop + sleep 3 + start + ;; + condrestart) + if status -p ${PIDFILE} $SHORTNAME >&/dev/null; then + stop + sleep 3 + start + fi + ;; + *) + echo $"Usage: $whatami {start|stop|restart|condrestart|status|help}" + RETVAL=3 +esac + +exit $RETVAL + diff --git a/services/console-proxy/server/distro/fedora/SYSCONFDIR/rc.d/init.d/cloud-console-proxy.in b/services/console-proxy/server/distro/fedora/SYSCONFDIR/rc.d/init.d/cloud-console-proxy.in new file mode 100644 index 00000000000..47fdaef1d96 --- /dev/null +++ b/services/console-proxy/server/distro/fedora/SYSCONFDIR/rc.d/init.d/cloud-console-proxy.in @@ -0,0 +1,98 @@ +#!/bin/bash + +# chkconfig: 35 99 10 +# description: Cloud Console Proxy + +# 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. + +# WARNING: if this script is changed, then all other initscripts MUST BE changed to match it as well + +. /etc/rc.d/init.d/functions + +whatami=cloud-console-proxy + +# set environment variables + +SHORTNAME="$whatami" +PIDFILE=@PIDDIR@/"$whatami".pid +LOCKFILE=@LOCKDIR@/"$SHORTNAME" +LOGFILE=@CPLOG@ +PROGNAME="Cloud Console Proxy" + +unset OPTIONS +[ -r @SYSCONFDIR@/sysconfig/"$SHORTNAME" ] && source @SYSCONFDIR@/sysconfig/"$SHORTNAME" +DAEMONIZE=@BINDIR@/@PACKAGE@-daemonize +PROG=@LIBEXECDIR@/console-proxy-runner + +start() { + echo -n $"Starting $PROGNAME: " + if hostname --fqdn >/dev/null 2>&1 ; then + daemon --check=$SHORTNAME --pidfile=${PIDFILE} "$DAEMONIZE" \ + -n "$SHORTNAME" -p "$PIDFILE" -l "$LOGFILE" "$PROG" $OPTIONS + RETVAL=$? + echo + else + failure + echo + echo The host name does not resolve properly to an IP address. Cannot start "$PROGNAME". > /dev/stderr + RETVAL=9 + fi + [ $RETVAL = 0 ] && touch ${LOCKFILE} + return $RETVAL +} + +stop() { + echo -n $"Stopping $PROGNAME: " + killproc -p ${PIDFILE} $SHORTNAME # -d 10 $SHORTNAME + RETVAL=$? + echo + [ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE} +} + + +# See how we were called. +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status -p ${PIDFILE} $SHORTNAME + RETVAL=$? + ;; + restart) + stop + sleep 3 + start + ;; + condrestart) + if status -p ${PIDFILE} $SHORTNAME >&/dev/null; then + stop + sleep 3 + start + fi + ;; + *) + echo $"Usage: $whatami {start|stop|restart|condrestart|status|help}" + RETVAL=3 +esac + +exit $RETVAL + diff --git a/services/console-proxy/server/distro/rhel/SYSCONFDIR/rc.d/init.d/cloud-console-proxy.in b/services/console-proxy/server/distro/rhel/SYSCONFDIR/rc.d/init.d/cloud-console-proxy.in new file mode 100644 index 00000000000..47fdaef1d96 --- /dev/null +++ b/services/console-proxy/server/distro/rhel/SYSCONFDIR/rc.d/init.d/cloud-console-proxy.in @@ -0,0 +1,98 @@ +#!/bin/bash + +# chkconfig: 35 99 10 +# description: Cloud Console Proxy + +# 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. + +# WARNING: if this script is changed, then all other initscripts MUST BE changed to match it as well + +. /etc/rc.d/init.d/functions + +whatami=cloud-console-proxy + +# set environment variables + +SHORTNAME="$whatami" +PIDFILE=@PIDDIR@/"$whatami".pid +LOCKFILE=@LOCKDIR@/"$SHORTNAME" +LOGFILE=@CPLOG@ +PROGNAME="Cloud Console Proxy" + +unset OPTIONS +[ -r @SYSCONFDIR@/sysconfig/"$SHORTNAME" ] && source @SYSCONFDIR@/sysconfig/"$SHORTNAME" +DAEMONIZE=@BINDIR@/@PACKAGE@-daemonize +PROG=@LIBEXECDIR@/console-proxy-runner + +start() { + echo -n $"Starting $PROGNAME: " + if hostname --fqdn >/dev/null 2>&1 ; then + daemon --check=$SHORTNAME --pidfile=${PIDFILE} "$DAEMONIZE" \ + -n "$SHORTNAME" -p "$PIDFILE" -l "$LOGFILE" "$PROG" $OPTIONS + RETVAL=$? + echo + else + failure + echo + echo The host name does not resolve properly to an IP address. Cannot start "$PROGNAME". > /dev/stderr + RETVAL=9 + fi + [ $RETVAL = 0 ] && touch ${LOCKFILE} + return $RETVAL +} + +stop() { + echo -n $"Stopping $PROGNAME: " + killproc -p ${PIDFILE} $SHORTNAME # -d 10 $SHORTNAME + RETVAL=$? + echo + [ $RETVAL = 0 ] && rm -f ${LOCKFILE} ${PIDFILE} +} + + +# See how we were called. +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status -p ${PIDFILE} $SHORTNAME + RETVAL=$? + ;; + restart) + stop + sleep 3 + start + ;; + condrestart) + if status -p ${PIDFILE} $SHORTNAME >&/dev/null; then + stop + sleep 3 + start + fi + ;; + *) + echo $"Usage: $whatami {start|stop|restart|condrestart|status|help}" + RETVAL=3 +esac + +exit $RETVAL + diff --git a/services/console-proxy/server/distro/ubuntu/SYSCONFDIR/init.d/cloud-console-proxy.in b/services/console-proxy/server/distro/ubuntu/SYSCONFDIR/init.d/cloud-console-proxy.in new file mode 100755 index 00000000000..550f2fbedfa --- /dev/null +++ b/services/console-proxy/server/distro/ubuntu/SYSCONFDIR/init.d/cloud-console-proxy.in @@ -0,0 +1,112 @@ +#!/bin/bash + +# chkconfig: 35 99 10 +# description: Cloud Console Proxy + +# 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. + +# WARNING: if this script is changed, then all other initscripts MUST BE changed to match it as well + +. /lib/lsb/init-functions +. /etc/default/rcS + +whatami=cloud-console-proxy + +# set environment variables + +SHORTNAME="$whatami" +PIDFILE=@PIDDIR@/"$whatami".pid +LOCKFILE=@LOCKDIR@/"$SHORTNAME" +LOGFILE=@CPLOG@ +PROGNAME="Cloud Console Proxy" + +unset OPTIONS +[ -r @SYSCONFDIR@/default/"$SHORTNAME" ] && source @SYSCONFDIR@/default/"$SHORTNAME" +DAEMONIZE=@BINDIR@/@PACKAGE@-daemonize +PROG=@LIBEXECDIR@/console-proxy-runner + +start() { + log_daemon_msg $"Starting $PROGNAME" "$SHORTNAME" + if [ -s "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then + log_progress_msg "apparently already running" + log_end_msg 0 + exit 0 + fi + if hostname --fqdn >/dev/null 2>&1 ; then + true + else + log_failure_msg "The host name does not resolve properly to an IP address. Cannot start $PROGNAME" + log_end_msg 1 + exit 1 + fi + + if start-stop-daemon --start --quiet \ + --pidfile "$PIDFILE" \ + --exec "$DAEMONIZE" -- -n "$SHORTNAME" -p "$PIDFILE" -l "$LOGFILE" "$PROG" $OPTIONS + RETVAL=$? + then + rc=0 + sleep 1 + if ! kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then + log_failure_msg "$PROG failed to start" + rc=1 + fi + else + rc=1 + fi + + if [ $rc -eq 0 ]; then + log_end_msg 0 + else + log_end_msg 1 + rm -f "$PIDFILE" + fi +} + +stop() { + echo -n $"Stopping $PROGNAME" "$SHORTNAME" + start-stop-daemon --stop --quiet --oknodo --pidfile "$PIDFILE" + log_end_msg $? + rm -f "$PIDFILE" +} + + +# See how we were called. +case "$1" in + start) + start + ;; + stop) + stop + ;; + status) + status_of_proc -p "$PIDFILE" "$PROG" "$SHORTNAME" + RETVAL=$? + ;; + restart) + stop + sleep 3 + start + ;; + *) + echo $"Usage: $whatami {start|stop|restart|status|help}" + RETVAL=3 +esac + +exit $RETVAL + diff --git a/services/console-proxy/server/images/back.gif b/services/console-proxy/server/images/back.gif new file mode 100644 index 00000000000..5c61ae27719 Binary files /dev/null and b/services/console-proxy/server/images/back.gif differ diff --git a/services/console-proxy/server/images/bright-green.png b/services/console-proxy/server/images/bright-green.png new file mode 100644 index 00000000000..685dfc1f5f4 Binary files /dev/null and b/services/console-proxy/server/images/bright-green.png differ diff --git a/services/console-proxy/server/images/cad.gif b/services/console-proxy/server/images/cad.gif new file mode 100644 index 00000000000..de3492587c0 Binary files /dev/null and b/services/console-proxy/server/images/cad.gif differ diff --git a/services/console-proxy/server/images/cannotconnect.jpg b/services/console-proxy/server/images/cannotconnect.jpg new file mode 100644 index 00000000000..0599f17beb2 Binary files /dev/null and b/services/console-proxy/server/images/cannotconnect.jpg differ diff --git a/services/console-proxy/server/images/clr_button.gif b/services/console-proxy/server/images/clr_button.gif new file mode 100644 index 00000000000..f5c385829da Binary files /dev/null and b/services/console-proxy/server/images/clr_button.gif differ diff --git a/services/console-proxy/server/images/clr_button_hover.gif b/services/console-proxy/server/images/clr_button_hover.gif new file mode 100644 index 00000000000..f882fa0baef Binary files /dev/null and b/services/console-proxy/server/images/clr_button_hover.gif differ diff --git a/services/console-proxy/server/images/dot.cur b/services/console-proxy/server/images/dot.cur new file mode 100644 index 00000000000..1721801ab1f Binary files /dev/null and b/services/console-proxy/server/images/dot.cur differ diff --git a/services/console-proxy/server/images/gray-green.png b/services/console-proxy/server/images/gray-green.png new file mode 100644 index 00000000000..e785a63f83e Binary files /dev/null and b/services/console-proxy/server/images/gray-green.png differ diff --git a/services/console-proxy/server/images/grid_headerbg.gif b/services/console-proxy/server/images/grid_headerbg.gif new file mode 100644 index 00000000000..5a0f72b82fb Binary files /dev/null and b/services/console-proxy/server/images/grid_headerbg.gif differ diff --git a/services/console-proxy/server/images/left.png b/services/console-proxy/server/images/left.png new file mode 100644 index 00000000000..fb7506618d7 Binary files /dev/null and b/services/console-proxy/server/images/left.png differ diff --git a/services/console-proxy/server/images/minimize_button.gif b/services/console-proxy/server/images/minimize_button.gif new file mode 100644 index 00000000000..a4e2e267c7e Binary files /dev/null and b/services/console-proxy/server/images/minimize_button.gif differ diff --git a/services/console-proxy/server/images/minimize_button_hover.gif b/services/console-proxy/server/images/minimize_button_hover.gif new file mode 100644 index 00000000000..7837ed00baf Binary files /dev/null and b/services/console-proxy/server/images/minimize_button_hover.gif differ diff --git a/services/console-proxy/server/images/notready.jpg b/services/console-proxy/server/images/notready.jpg new file mode 100644 index 00000000000..406599cd595 Binary files /dev/null and b/services/console-proxy/server/images/notready.jpg differ diff --git a/services/console-proxy/server/images/play_button.gif b/services/console-proxy/server/images/play_button.gif new file mode 100644 index 00000000000..6f483082ecb Binary files /dev/null and b/services/console-proxy/server/images/play_button.gif differ diff --git a/services/console-proxy/server/images/play_button_hover.gif b/services/console-proxy/server/images/play_button_hover.gif new file mode 100644 index 00000000000..6689e3eec81 Binary files /dev/null and b/services/console-proxy/server/images/play_button_hover.gif differ diff --git a/services/console-proxy/server/images/right.png b/services/console-proxy/server/images/right.png new file mode 100644 index 00000000000..4abac818410 Binary files /dev/null and b/services/console-proxy/server/images/right.png differ diff --git a/services/console-proxy/server/images/right2.png b/services/console-proxy/server/images/right2.png new file mode 100644 index 00000000000..cca9379fe5b Binary files /dev/null and b/services/console-proxy/server/images/right2.png differ diff --git a/services/console-proxy/server/images/shrink_button.gif b/services/console-proxy/server/images/shrink_button.gif new file mode 100644 index 00000000000..834625a2732 Binary files /dev/null and b/services/console-proxy/server/images/shrink_button.gif differ diff --git a/services/console-proxy/server/images/shrink_button_hover.gif b/services/console-proxy/server/images/shrink_button_hover.gif new file mode 100644 index 00000000000..eed9cec54b7 Binary files /dev/null and b/services/console-proxy/server/images/shrink_button_hover.gif differ diff --git a/services/console-proxy/server/images/stop_button.gif b/services/console-proxy/server/images/stop_button.gif new file mode 100644 index 00000000000..bfec9c64a89 Binary files /dev/null and b/services/console-proxy/server/images/stop_button.gif differ diff --git a/services/console-proxy/server/images/stop_button_hover.gif b/services/console-proxy/server/images/stop_button_hover.gif new file mode 100644 index 00000000000..c7c578fc83f Binary files /dev/null and b/services/console-proxy/server/images/stop_button_hover.gif differ diff --git a/services/console-proxy/server/images/winlog.png b/services/console-proxy/server/images/winlog.png new file mode 100644 index 00000000000..c7668c52f53 Binary files /dev/null and b/services/console-proxy/server/images/winlog.png differ diff --git a/services/console-proxy/server/js/ajaxkeys.js b/services/console-proxy/server/js/ajaxkeys.js new file mode 100644 index 00000000000..2ecf5b561e0 --- /dev/null +++ b/services/console-proxy/server/js/ajaxkeys.js @@ -0,0 +1,77 @@ +/* +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. +*/ + +/* + * This var contains the limited keyboard translation tables. + * This is the table that users can modify to make special keyboard to work properly. + * They are used by the ajaxviewer.js + */ + +//client event type. corresponds to events in ajaxviewer. +X11_KEY_CIRCUMFLEX_ACCENT = 0x5e; // ^ +X11_KEY_YEN_MARK = 0xa5; +X11_KEY_OPEN_BRACKET = 0x5b; +X11_KEY_CLOSE_BRACKET = 0x5d; +X11_KEY_COLON = 0x3a; +X11_KEY_REVERSE_SOLIUS = 0x5c; // another back slash (back slash on JP keyboard) +X11_KEY_CAPSLOCK = 0xffe5; +X11_KEY_SEMI_COLON = 0x3b; +X11_KEY_SHIFT = 0xffe1; +X11_KEY_ADD = 0x2b; + +KEY_DOWN = 5; +KEY_UP = 6; + +//JP keyboard type +// +var keyboardTables = [ + {tindex: 0, keyboardType: "EN-Cooked", mappingTable: + {X11: [ {keycode: 222, entry: X11_KEY_CIRCUMFLEX_ACCENT}, + {keycode: 220, entry: X11_KEY_YEN_MARK}, + {keycode: 219, entry: X11_KEY_OPEN_BRACKET}, + {keycode: 221, entry: X11_KEY_CLOSE_BRACKET}, + {keycode: 59, entry: X11_KEY_COLON, browser: "Firefox"}, + {keycode: 186, entry: X11_KEY_COLON, browser: "Chrome"}, + {keycode: 9, entry: 9, guestos: "XenServer"}, + {keycode: 226, entry: X11_KEY_REVERSE_SOLIUS}, + {keycode: 240, entry: [ + {type: KEY_DOWN, code: X11_KEY_CAPSLOCK, modifiers: 0 }, + {type: KEY_UP, code: X11_KEY_CAPSLOCK, modifiers: 0 }, + ] + }, + ], + keyPress: [ + {keycode: 59, entry: [ + {type: KEY_DOWN, code: X11_KEY_SEMI_COLON, modifiers: 0 }, + {type: KEY_UP, code: X11_KEY_SEMI_COLON, modifiers: 0 }, + ] + }, + {keycode: 43, entry: [ + {type: KEY_DOWN, code: X11_KEY_SHIFT, modifiers: 0, shift: false }, + {type: KEY_DOWN, code: X11_KEY_ADD, modifiers: 0, shift: false }, + {type: KEY_UP, code: X11_KEY_ADD, modifiers: 0, shift: false }, + {type: KEY_UP, code: X11_KEY_SHIFT, modifiers: 0, shift: false }, + {type: KEY_DOWN, code: X11_KEY_ADD, modifiers: 0, shift: true }, + {type: KEY_UP, code: X11_KEY_ADD, modifiers: 0, shift: true }, + ] + }, + ] + } + } ] + diff --git a/services/console-proxy/server/js/ajaxviewer.js b/services/console-proxy/server/js/ajaxviewer.js new file mode 100644 index 00000000000..e95615d8946 --- /dev/null +++ b/services/console-proxy/server/js/ajaxviewer.js @@ -0,0 +1,1444 @@ +/* +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. +*/ + +var g_logger; + +///////////////////////////////////////////////////////////////////////////// +// class StringBuilder +// +function StringBuilder(initStr) { + this.strings = new Array(""); + this.append(initStr); +} + +StringBuilder.prototype = { + append : function (str) { + if (str) { + this.strings.push(str); + } + return this; + }, + + clear : function() { + this.strings.length = 1; + return this; + }, + + toString: function() { + return this.strings.join(""); + } +}; + + +function getCurrentLanguage() { + if(acceptLanguages) { + var tokens = acceptLanguages.split(','); + if(tokens.length > 0) + return tokens[0]; + + return "en-us"; + } else { + return "en-us"; + } +} + +///////////////////////////////////////////////////////////////////////////// +// class KeyboardMapper +// +function KeyboardMapper() { + this.mappedInput = []; + this.jsX11KeysymMap = []; + this.jsKeyPressX11KeysymMap = []; +} + +// +// RAW keyboard +// Primarily translates KeyDown/KeyUp event, either as is (if there is no mapping entry) +// or through mapped result. +// +// For KeyPress event, it translates it only if there exist a mapping entry +// in jsX11KeysymMap map and the entry meets the condition +// +// COOKED keyboard +// Primarily translates KeyPress event, either as is or through mapped result +// It translates KeyDown/KeyUp only there exists a mapping entry, or if there +// is no mapping entry, translate when certain modifier key is pressed (i.e., +// CTRL or ALT key +// +// Mapping entry types +// direct : will be directly mapped into the entry value with the same event type +// boolean : only valid for jsX11KeysymMap, existence of this type, no matter true or false +// in value, corresponding KeyDown/KeyUp event will be masked +// array : contains a set of conditional mapping entry +// +// Conditional mapping entry +// +// { +// type: , code: , modifiers: , +// shift : , -- match on shift state +// guestos : , -- match on guestos type +// browser: , -- match on browser +// browserVersion: -- match on browser version +// } +// +KeyboardMapper.KEYBOARD_TYPE_RAW = 0; +KeyboardMapper.KEYBOARD_TYPE_COOKED = 1; + +KeyboardMapper.prototype = { + + setKeyboardType : function(keyboardType) { + this.keyboardType = keyboardType; + + if(keyboardType == KeyboardMapper.KEYBOARD_TYPE_RAW) { + // intialize keyboard mapping for RAW keyboard + this.jsX11KeysymMap[AjaxViewer.JS_KEY_CAPSLOCK] = AjaxViewer.X11_KEY_CAPSLOCK; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_BACKSPACE] = AjaxViewer.X11_KEY_BACKSPACE; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_TAB] = AjaxViewer.X11_KEY_TAB; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_ENTER] = AjaxViewer.X11_KEY_ENTER; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_ESCAPE] = AjaxViewer.X11_KEY_ESCAPE; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_INSERT] = AjaxViewer.X11_KEY_INSERT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_DELETE] = AjaxViewer.X11_KEY_DELETE; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_HOME] = AjaxViewer.X11_KEY_HOME; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_END] = AjaxViewer.X11_KEY_END; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_PAGEUP] = AjaxViewer.X11_KEY_PAGEUP; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_PAGEDOWN] = AjaxViewer.X11_KEY_PAGEDOWN; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_LEFT] = AjaxViewer.X11_KEY_LEFT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_UP] = AjaxViewer.X11_KEY_UP; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_RIGHT] = AjaxViewer.X11_KEY_RIGHT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_DOWN] = AjaxViewer.X11_KEY_DOWN; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F1] = AjaxViewer.X11_KEY_F1; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F2] = AjaxViewer.X11_KEY_F2; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F3] = AjaxViewer.X11_KEY_F3; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F4] = AjaxViewer.X11_KEY_F4; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F5] = AjaxViewer.X11_KEY_F5; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F6] = AjaxViewer.X11_KEY_F6; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F7] = AjaxViewer.X11_KEY_F7; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F8] = AjaxViewer.X11_KEY_F8; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F9] = AjaxViewer.X11_KEY_F9; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F10] = AjaxViewer.X11_KEY_F10; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F11] = AjaxViewer.X11_KEY_F11; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F12] = AjaxViewer.X11_KEY_F12; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_SHIFT] = AjaxViewer.X11_KEY_SHIFT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_CTRL] = AjaxViewer.X11_KEY_CTRL; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_ALT] = AjaxViewer.X11_KEY_ALT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_GRAVE_ACCENT] = AjaxViewer.X11_KEY_GRAVE_ACCENT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_SUBSTRACT] = AjaxViewer.X11_KEY_SUBSTRACT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_ADD] = AjaxViewer.X11_KEY_ADD; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_OPEN_BRACKET] = AjaxViewer.X11_KEY_OPEN_BRACKET; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_CLOSE_BRACKET] = AjaxViewer.X11_KEY_CLOSE_BRACKET; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_BACK_SLASH] = AjaxViewer.X11_KEY_BACK_SLASH; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_SINGLE_QUOTE] = AjaxViewer.X11_KEY_SINGLE_QUOTE; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_COMMA] = AjaxViewer.X11_KEY_COMMA; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_PERIOD] = AjaxViewer.X11_KEY_PERIOD; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_FORWARD_SLASH] = AjaxViewer.X11_KEY_FORWARD_SLASH; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_DASH] = AjaxViewer.X11_KEY_DASH; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_SEMI_COLON] = AjaxViewer.X11_KEY_SEMI_COLON; + + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD0] = AjaxViewer.X11_KEY_NUMPAD0; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD1] = AjaxViewer.X11_KEY_NUMPAD1; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD2] = AjaxViewer.X11_KEY_NUMPAD2; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD3] = AjaxViewer.X11_KEY_NUMPAD3; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD4] = AjaxViewer.X11_KEY_NUMPAD4; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD5] = AjaxViewer.X11_KEY_NUMPAD5; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD6] = AjaxViewer.X11_KEY_NUMPAD6; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD7] = AjaxViewer.X11_KEY_NUMPAD7; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD8] = AjaxViewer.X11_KEY_NUMPAD8; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_NUMPAD9] = AjaxViewer.X11_KEY_NUMPAD9; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_DECIMAL_POINT] = AjaxViewer.X11_KEY_DECIMAL_POINT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_DIVIDE] = AjaxViewer.X11_KEY_DIVIDE; + + this.jsX11KeysymMap[AjaxViewer.JS_KEY_MULTIPLY] = [ + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_SHIFT, modifiers: 0 }, + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_ASTERISK, modifiers: 0 }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_ASTERISK, modifiers: 0 }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_SHIFT, modifiers: 0 } + ]; + + this.jsX11KeysymMap[AjaxViewer.JS_KEY_ADD] = false; + this.jsKeyPressX11KeysymMap = []; + this.jsKeyPressX11KeysymMap[61] = [ + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: false } + ]; + this.jsKeyPressX11KeysymMap[43] = [ + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_SHIFT, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_SHIFT, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: true }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: true } + ]; + } else { + // initialize mapping for COOKED keyboard + this.jsX11KeysymMap[AjaxViewer.JS_KEY_CAPSLOCK] = AjaxViewer.X11_KEY_CAPSLOCK; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_BACKSPACE] = AjaxViewer.X11_KEY_BACKSPACE; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_TAB] = AjaxViewer.X11_KEY_TAB; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_ENTER] = AjaxViewer.X11_KEY_ENTER; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_ESCAPE] = AjaxViewer.X11_KEY_ESCAPE; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_INSERT] = AjaxViewer.X11_KEY_INSERT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_DELETE] = AjaxViewer.X11_KEY_DELETE; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_HOME] = AjaxViewer.X11_KEY_HOME; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_END] = AjaxViewer.X11_KEY_END; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_PAGEUP] = AjaxViewer.X11_KEY_PAGEUP; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_PAGEDOWN] = AjaxViewer.X11_KEY_PAGEDOWN; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_LEFT] = AjaxViewer.X11_KEY_LEFT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_UP] = AjaxViewer.X11_KEY_UP; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_RIGHT] = AjaxViewer.X11_KEY_RIGHT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_DOWN] = AjaxViewer.X11_KEY_DOWN; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F1] = AjaxViewer.X11_KEY_F1; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F2] = AjaxViewer.X11_KEY_F2; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F3] = AjaxViewer.X11_KEY_F3; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F4] = AjaxViewer.X11_KEY_F4; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F5] = AjaxViewer.X11_KEY_F5; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F6] = AjaxViewer.X11_KEY_F6; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F7] = AjaxViewer.X11_KEY_F7; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F8] = AjaxViewer.X11_KEY_F8; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F9] = AjaxViewer.X11_KEY_F9; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F10] = AjaxViewer.X11_KEY_F10; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F11] = AjaxViewer.X11_KEY_F11; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_F12] = AjaxViewer.X11_KEY_F12; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_SHIFT] = AjaxViewer.X11_KEY_SHIFT; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_CTRL] = AjaxViewer.X11_KEY_CTRL; + this.jsX11KeysymMap[AjaxViewer.JS_KEY_ALT] = AjaxViewer.X11_KEY_ALT; + } + }, + RawkeyboardInputHandler : function(eventType, code, modifiers, guestos, browser, browserVersion) { + if(eventType == AjaxViewer.KEY_DOWN || eventType == AjaxViewer.KEY_UP) { + + // special handling for Alt + Ctrl + Ins, convert it into Alt-Ctrl-Del + if(code == AjaxViewer.JS_KEY_INSERT) { + if((modifiers & AjaxViewer.ALT_KEY_MASK) != 0 && (modifiers & AjaxViewer.CTRL_KEY_MASK) != 0) { + this.mappedInput.push({type : eventType, code: 0xffff, modifiers: modifiers}); + return; + } + } + + var X11Keysym = code; + if(this.jsX11KeysymMap[code] != undefined) { + X11Keysym = this.jsX11KeysymMap[code]; + if(typeof this.jsX11KeysymMap[code] == "boolean") { + return; + } else if($.isArray(X11Keysym)) { + for(var i = 0; i < X11Keysym.length; i++) { + // How to set the guestos, browser, version value??? add later + if(this.isConditionalEntryMatched(eventType, code, modifiers, X11Keysym[i], guestos, browser, browserVersion)) { + this.mappedInput.push(X11Keysym[i]); + } + } + } else { + this.mappedInput.push({type : eventType, code: X11Keysym, modifiers: modifiers}); + } + } else { + this.mappedInput.push({type : eventType, code: X11Keysym, modifiers: modifiers}); + } + + // special handling for ALT/CTRL key + if(eventType == AjaxViewer.KEY_UP && (code == AjaxViewer.JS_KEY_ALT || code == code == AjaxViewer.JS_KEY_CTRL)) + this.mappedInput.push({type : eventType, code: this.jsX11KeysymMap[code], modifiers: modifiers}); + + } else if(eventType == AjaxViewer.KEY_PRESS) { + var X11Keysym = code; + X11Keysym = this.jsKeyPressX11KeysymMap[code]; + if(X11Keysym) { + if($.isArray(X11Keysym)) { + for(var i = 0; i < X11Keysym.length; i++) { + if(this.isConditionalEntryMatched(eventType, code, modifiers, X11Keysym[i], guestos, browser)) + this.mappedInput.push(X11Keysym[i]); + } + } else { + this.mappedInput.push({type : AjaxViewer.KEY_DOWN, code: X11Keysym, modifiers: modifiers}); + this.mappedInput.push({type : AjaxViewer.KEY_UP, code: X11Keysym, modifiers: modifiers}); + } + } + } + }, + + CookedKeyboardInputHandler : function(eventType, code, modifiers, guestos, browser, browserVersion) { + if(eventType == AjaxViewer.KEY_DOWN || eventType == AjaxViewer.KEY_UP) { + + // special handling for Alt + Ctrl + Ins, convert it into Alt-Ctrl-Del + if(code == AjaxViewer.JS_KEY_INSERT) { + if((modifiers & AjaxViewer.ALT_KEY_MASK) != 0 && (modifiers & AjaxViewer.CTRL_KEY_MASK) != 0) { + this.mappedInput.push({type : eventType, code: 0xffff, modifiers: modifiers}); + return; + } + } + + var X11Keysym = code; + if(this.jsX11KeysymMap[code] != undefined) { + X11Keysym = this.jsX11KeysymMap[code]; + if(typeof this.jsX11KeysymMap[code] == "boolean") { + return; + } else if($.isArray(X11Keysym)) { + for(var i = 0; i < X11Keysym.length; i++) { + if(this.isConditionalEntryMatched(eventType, code, modifiers, X11Keysym[i], guestos, browser, browserVersion)) { + this.mappedInput.push(X11Keysym[i]); + } + } + } else { + this.mappedInput.push({type : eventType, code: X11Keysym, modifiers: modifiers}); + } + } else { + if((modifiers & (AjaxViewer.CTRL_KEY_MASK | AjaxViewer.ALT_KEY_MASK)) != 0) { + this.mappedInput.push({type : eventType, code: X11Keysym, modifiers: modifiers}); + } + } + + // special handling for ALT/CTRL key + if(eventType == AjaxViewer.KEY_UP && (code == AjaxViewer.JS_KEY_ALT || code == code == AjaxViewer.JS_KEY_CTRL)) + this.mappedInput.push({type : eventType, code: this.jsX11KeysymMap[code], modifiers: modifiers}); + + } else if(eventType == AjaxViewer.KEY_PRESS) { + // special handling for * and + key on number pad + if(code == AjaxViewer.JS_NUMPAD_MULTIPLY) { + this.mappedInput.push({type : AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_SHIFT, modifiers: modifiers}); + this.mappedInput.push({type : AjaxViewer.KEY_DOWN, code: 42, modifiers: modifiers}); + this.mappedInput.push({type : AjaxViewer.KEY_UP, code: 42, modifiers: modifiers}); + this.mappedInput.push({type : AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_SHIFT, modifiers: modifiers}); + return; + } + + if(code == AjaxViewer.JS_NUMPAD_PLUS) { + this.mappedInput.push({type : AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_SHIFT, modifiers: modifiers}); + this.mappedInput.push({type : AjaxViewer.KEY_DOWN, code: 43, modifiers: modifiers}); + this.mappedInput.push({type : AjaxViewer.KEY_UP, code: 43, modifiers: modifiers}); + this.mappedInput.push({type : AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_SHIFT, modifiers: modifiers}); + return; + } + + // ENTER/BACKSPACE key should already have been sent through KEY DOWN/KEY UP event + if(code == AjaxViewer.JS_KEY_ENTER || code == AjaxViewer.JS_KEY_BACKSPACE) + return; + + if(code > 0) { + var X11Keysym = code; + X11Keysym = this.jsKeyPressX11KeysymMap[code]; + if(X11Keysym) { + if($.isArray(X11Keysym)) { + for(var i = 0; i < X11Keysym.length; i++) { + if(this.isConditionalEntryMatched(eventType, code, modifiers, X11Keysym[i], guestos, browser)) + this.mappedInput.push(X11Keysym[i]); + } + } else { + this.mappedInput.push({type : AjaxViewer.KEY_DOWN, code: X11Keysym, modifiers: modifiers}); + this.mappedInput.push({type : AjaxViewer.KEY_UP, code: X11Keysym, modifiers: modifiers}); + } + } else { + // if there is no mappting entry, use the JS keypress code directly + this.mappedInput.push({type : AjaxViewer.KEY_DOWN, code: code, modifiers: modifiers}); + this.mappedInput.push({type : AjaxViewer.KEY_UP, code: code, modifiers: modifiers}); + } + } + } + }, + + inputFeed : function(eventType, code, modifiers, guestos, browser, browserVersion) { + if(this.keyboardType == KeyboardMapper.KEYBOARD_TYPE_RAW) + this.RawkeyboardInputHandler(eventType, code, modifiers, guestos, browser, browserVersion); + else + this.CookedKeyboardInputHandler(eventType, code, modifiers, guestos, browser, browserVersion); + }, + + getMappedInput : function() { + var mappedInput = this.mappedInput; + this.mappedInput = []; + return mappedInput; + }, + + isConditionalEntryMatched : function(eventType, code, modifiers, entry, guestos, browser, browserVersion) { + if(eventType == AjaxViewer.KEY_DOWN || eventType == AjaxViewer.KEY_UP) { + // for KeyDown/KeyUp events, we require that the type in entry should match with + // the real input + if(entry.type != eventType) + return false; + } + + // check conditional match + if(entry.shift != undefined) { + var shift = ((modifiers & AjaxViewer.SHIFT_KEY_MASK) != 0 ? true : false); + if(entry.shift ^ shift) + return false; + } + + if(entry.guestos != undefined) { + if(entry.guestos != guestos) + return false; + } + + if(entry.browser != undefined) { + if(entry.browser != browser) + return false; + } + + if(entry.browserVersion != undefined) { + if(entry.browserVersion != browserVersion) + return false; + } + + return true; + }, + + isModifierInput : function(code) { + return $.inArray(code, [AjaxViewer.ALT_KEY_MASK, AjaxViewer.SHIFT_KEY_MASK, AjaxViewer.CTRL_KEY_MASK, AjaxViewer.META_KEY_MASK]) >= 0; + } +}; + +///////////////////////////////////////////////////////////////////////////// +// class AjaxViewer +// +function AjaxViewer(panelId, imageUrl, updateUrl, tileMap, width, height, tileWidth, tileHeight) { + // logging is disabled by default so that it won't have negative impact on performance + // however, a back door key-sequence can trigger to open the logger window, it is designed to help + // trouble-shooting + g_logger = new Logger(); + + // g_logger.enable(true); + // g_logger.open(); + + var ajaxViewer = this; + this.imageLoaded = false; + this.fullImage = true; + this.imgUrl = imageUrl; + this.img = new Image(); + $(this.img).attr('src', imageUrl).load(function() { + ajaxViewer.imageLoaded = true; + }); + + this.updateUrl = updateUrl; + this.tileMap = tileMap; + this.dirty = true; + this.width = width; + this.height = height; + this.tileWidth = tileWidth; + this.tileHeight = tileHeight; + this.maxTileZIndex = 1; + + this.currentKeyboard = AjaxViewer.KEYBOARD_TYPE_ENGLISH; + this.keyboardMappers = []; + + this.timer = 0; + this.eventQueue = []; + this.sendingEventInProgress = false; + + this.lastClickEvent = { x: 0, y: 0, button: 0, modifiers: 0, time: new Date().getTime() }; + + if(window.onStatusNotify == undefined) + window.onStatusNotify = function(status) {}; + + this.panel = this.generateCanvas(panelId, width, height, tileWidth, tileHeight); +// this.setupKeyboardTranslationle(); + this.setupKeyboardTranslationTable(this.keyboardMappers); + this.setupUIController(); +} + +// client event types +AjaxViewer.MOUSE_MOVE = 1; +AjaxViewer.MOUSE_DOWN = 2; +AjaxViewer.MOUSE_UP = 3; +AjaxViewer.KEY_PRESS = 4; +AjaxViewer.KEY_DOWN = 5; +AjaxViewer.KEY_UP = 6; +AjaxViewer.EVENT_BAG = 7; +AjaxViewer.MOUSE_DBLCLK = 8; + +// use java AWT key modifier masks +AjaxViewer.SHIFT_KEY_MASK = 64; +AjaxViewer.CTRL_KEY_MASK = 128; +AjaxViewer.META_KEY_MASK = 256; +AjaxViewer.ALT_KEY_MASK = 512; +AjaxViewer.LEFT_SHIFT_MASK = 1024; +AjaxViewer.LEFT_CTRL_MASK = 2048; +AjaxViewer.LEFT_ALT_MASK = 4096; + +AjaxViewer.EVENT_QUEUE_MOUSE_EVENT = 1; +AjaxViewer.EVENT_QUEUE_KEYBOARD_EVENT = 2; + +AjaxViewer.STATUS_RECEIVING = 1; +AjaxViewer.STATUS_RECEIVED = 2; +AjaxViewer.STATUS_SENDING = 3; +AjaxViewer.STATUS_SENT = 4; + +AjaxViewer.KEYBOARD_TYPE_ENGLISH = "us"; +AjaxViewer.KEYBOARD_TYPE_JAPANESE = "jp"; + +AjaxViewer.JS_KEY_BACKSPACE = 8; +AjaxViewer.JS_KEY_TAB = 9; +AjaxViewer.JS_KEY_ENTER = 13; +AjaxViewer.JS_KEY_SHIFT = 16; +AjaxViewer.JS_KEY_CTRL = 17; +AjaxViewer.JS_KEY_ALT = 18; +AjaxViewer.JS_KEY_PAUSE = 19; +AjaxViewer.JS_KEY_CAPSLOCK = 20; +AjaxViewer.JS_KEY_ESCAPE = 27; +AjaxViewer.JS_KEY_PAGEUP = 33; +AjaxViewer.JS_KEY_PAGEDOWN = 34; +AjaxViewer.JS_KEY_END = 35; +AjaxViewer.JS_KEY_HOME = 36; +AjaxViewer.JS_KEY_LEFT = 37; +AjaxViewer.JS_KEY_UP = 38; +AjaxViewer.JS_KEY_RIGHT = 39; +AjaxViewer.JS_KEY_DOWN = 40; +AjaxViewer.JS_KEY_INSERT = 45; +AjaxViewer.JS_KEY_DELETE = 46; +AjaxViewer.JS_KEY_LEFT_WINDOW_KEY = 91; +AjaxViewer.JS_KEY_RIGHT_WINDOW_KEY = 92; +AjaxViewer.JS_KEY_SELECT_KEY = 93; +AjaxViewer.JS_KEY_NUMPAD0 = 96; +AjaxViewer.JS_KEY_NUMPAD1 = 97; +AjaxViewer.JS_KEY_NUMPAD2 = 98; +AjaxViewer.JS_KEY_NUMPAD3 = 99; +AjaxViewer.JS_KEY_NUMPAD4 = 100; +AjaxViewer.JS_KEY_NUMPAD5 = 101; +AjaxViewer.JS_KEY_NUMPAD6 = 102; +AjaxViewer.JS_KEY_NUMPAD7 = 103; +AjaxViewer.JS_KEY_NUMPAD8 = 104; +AjaxViewer.JS_KEY_NUMPAD9 = 105; +AjaxViewer.JS_KEY_MULTIPLY = 106; +AjaxViewer.JS_KEY_ADD = 107; +AjaxViewer.JS_KEY_SUBSTRACT = 109; +AjaxViewer.JS_KEY_DECIMAL_POINT = 110; +AjaxViewer.JS_KEY_DIVIDE = 111; +AjaxViewer.JS_KEY_F1 = 112; +AjaxViewer.JS_KEY_F2 = 113; +AjaxViewer.JS_KEY_F3 = 114; +AjaxViewer.JS_KEY_F4 = 115; +AjaxViewer.JS_KEY_F5 = 116; +AjaxViewer.JS_KEY_F6 = 117; +AjaxViewer.JS_KEY_F7 = 118; +AjaxViewer.JS_KEY_F8 = 119; +AjaxViewer.JS_KEY_F9 = 120; +AjaxViewer.JS_KEY_F10 = 121; +AjaxViewer.JS_KEY_F11 = 122; +AjaxViewer.JS_KEY_F12 = 123; +AjaxViewer.JS_KEY_NUMLOCK = 144; +AjaxViewer.JS_KEY_SCROLLLOCK = 145; +AjaxViewer.JS_KEY_SEMI_COLON = 186; // ; +AjaxViewer.JS_KEY_EQUAL_SIGN = 187; // = +AjaxViewer.JS_KEY_COMMA = 188; // , +AjaxViewer.JS_KEY_DASH = 189; // - +AjaxViewer.JS_KEY_PERIOD = 190; // . +AjaxViewer.JS_KEY_FORWARD_SLASH = 191; // / +AjaxViewer.JS_KEY_GRAVE_ACCENT = 192; // ` +AjaxViewer.JS_KEY_OPEN_BRACKET = 219; // [ +AjaxViewer.JS_KEY_BACK_SLASH = 220; // \ +AjaxViewer.JS_KEY_CLOSE_BRACKET = 221; // ] +AjaxViewer.JS_KEY_SINGLE_QUOTE = 222; // ' +AjaxViewer.JS_NUMPAD_PLUS = 43; +AjaxViewer.JS_NUMPAD_MULTIPLY = 42; +AjaxViewer.JS_KEY_NUM8 = 56; + +// keycode from Japanese keyboard +AjaxViewer.JS_KEY_JP_COLON = 222; // :* on JP keyboard +AjaxViewer.JS_KEY_JP_CLOSE_BRACKET = 220; // [{ on JP keyboard +AjaxViewer.JS_KEY_JP_AT_SIGN = 219; // @` on JP keyboard +AjaxViewer.JS_KEY_JP_OPEN_BRACKET = 221; // [{ on JP keyboard +AjaxViewer.JS_KEY_JP_BACK_SLASH = 193; // \| on JP keyboard +AjaxViewer.JS_KEY_JP_YEN_MARK = 255; + +AjaxViewer.JS_KEY_JP_EQUAL = 109; // -= ON JP keyboard +AjaxViewer.JS_KEY_JP_ACUTE = 107; // ^~ on JP keyboard + +// X11 keysym definitions +AjaxViewer.X11_KEY_CAPSLOCK = 0xffe5; +AjaxViewer.X11_KEY_BACKSPACE = 0xff08; +AjaxViewer.X11_KEY_TAB = 0xff09; +AjaxViewer.X11_KEY_ENTER = 0xff0d; +AjaxViewer.X11_KEY_ESCAPE = 0xff1b; +AjaxViewer.X11_KEY_INSERT = 0xff63; +AjaxViewer.X11_KEY_DELETE = 0xffff; +AjaxViewer.X11_KEY_HOME = 0xff50; +AjaxViewer.X11_KEY_END = 0xff57; +AjaxViewer.X11_KEY_PAGEUP = 0xff55; +AjaxViewer.X11_KEY_PAGEDOWN = 0xff56; +AjaxViewer.X11_KEY_LEFT = 0xff51; +AjaxViewer.X11_KEY_UP = 0xff52; +AjaxViewer.X11_KEY_RIGHT = 0xff53; +AjaxViewer.X11_KEY_DOWN = 0xff54; +AjaxViewer.X11_KEY_F1 = 0xffbe; +AjaxViewer.X11_KEY_F2 = 0xffbf; +AjaxViewer.X11_KEY_F3 = 0xffc0; +AjaxViewer.X11_KEY_F4 = 0xffc1; +AjaxViewer.X11_KEY_F5 = 0xffc2; +AjaxViewer.X11_KEY_F6 = 0xffc3; +AjaxViewer.X11_KEY_F7 = 0xffc4; +AjaxViewer.X11_KEY_F8 = 0xffc5; +AjaxViewer.X11_KEY_F9 = 0xffc6; +AjaxViewer.X11_KEY_F10 = 0xffc7; +AjaxViewer.X11_KEY_F11 = 0xffc8; +AjaxViewer.X11_KEY_F12 = 0xffc9; +AjaxViewer.X11_KEY_SHIFT = 0xffe1; +AjaxViewer.X11_KEY_CTRL = 0xffe3; +AjaxViewer.X11_KEY_ALT = 0xffe9; +AjaxViewer.X11_KEY_GRAVE_ACCENT = 0x60; +AjaxViewer.X11_KEY_SUBSTRACT = 0x2d; +AjaxViewer.X11_KEY_ADD = 0x2b; +AjaxViewer.X11_KEY_OPEN_BRACKET = 0x5b; +AjaxViewer.X11_KEY_CLOSE_BRACKET = 0x5d; +AjaxViewer.X11_KEY_BACK_SLASH = 0x7c; +AjaxViewer.X11_KEY_REVERSE_SOLIUS = 0x5c; // another back slash (back slash on JP keyboard) +AjaxViewer.X11_KEY_SINGLE_QUOTE = 0x22; +AjaxViewer.X11_KEY_COMMA = 0x3c; +AjaxViewer.X11_KEY_PERIOD = 0x3e; +AjaxViewer.X11_KEY_FORWARD_SLASH = 0x3f; +AjaxViewer.X11_KEY_DASH = 0x2d; +AjaxViewer.X11_KEY_COLON = 0x3a; +AjaxViewer.X11_KEY_SEMI_COLON = 0x3b; +AjaxViewer.X11_KEY_NUMPAD0 = 0x30; +AjaxViewer.X11_KEY_NUMPAD1 = 0x31; +AjaxViewer.X11_KEY_NUMPAD2 = 0x32; +AjaxViewer.X11_KEY_NUMPAD3 = 0x33; +AjaxViewer.X11_KEY_NUMPAD4 = 0x34; +AjaxViewer.X11_KEY_NUMPAD5 = 0x35; +AjaxViewer.X11_KEY_NUMPAD6 = 0x36; +AjaxViewer.X11_KEY_NUMPAD7 = 0x37; +AjaxViewer.X11_KEY_NUMPAD8 = 0x38; +AjaxViewer.X11_KEY_NUMPAD9 = 0x39; +AjaxViewer.X11_KEY_DECIMAL_POINT = 0x2e; +AjaxViewer.X11_KEY_DIVIDE = 0x3f; +AjaxViewer.X11_KEY_TILDE = 0x7e; // ~ +AjaxViewer.X11_KEY_CIRCUMFLEX_ACCENT = 0x5e; // ^ +AjaxViewer.X11_KEY_YEN_MARK = 0xa5; // Japanese YEN mark +AjaxViewer.X11_KEY_ASTERISK = 0x2a; + +AjaxViewer.getEventName = function(type) { + switch(type) { + case AjaxViewer.MOUSE_MOVE : + return "MOUSE_MOVE"; + + case AjaxViewer.MOUSE_DOWN : + return "MOUSE_DOWN"; + + case AjaxViewer.MOUSE_UP : + return "MOUSE_UP"; + + case AjaxViewer.KEY_PRESS : + return "KEY_PRESS"; + + case AjaxViewer.KEY_DOWN : + return "KEY_DOWN"; + + case AjaxViewer.KEY_UP : + return "KEY_UP"; + + case AjaxViewer.EVENT_BAG : + return "EVENT_BAG"; + + case AjaxViewer.MOUSE_DBLCLK : + return "MOUSE_DBLCLK"; + } + + return "N/A"; +}; + +AjaxViewer.prototype = { + setDirty: function(value) { + this.dirty = value; + }, + + isDirty: function() { + return this.dirty; + }, + + isImageLoaded: function() { + return this.imageLoaded; + }, + + refresh: function(imageUrl, tileMap, fullImage) { + var ajaxViewer = this; + var img = $(this.img); + this.fullImage = fullImage; + this.imgUrl=imageUrl; + + img.attr('src', imageUrl).load(function() { + ajaxViewer.imageLoaded = true; + }); + this.tileMap = tileMap; + }, + + resize: function(panelId, width, height, tileWidth, tileHeight) { + $(".canvas_tile", document.body).each(function() { + $(this).remove(); + }); + $("table", $("#" + panelId)).remove(); + + this.width = width; + this.height = height; + this.tileWidth = tileWidth; + this.tileHeight = tileHeight; + this.panel = this.generateCanvas(panelId, width, height, tileWidth, tileHeight); + }, + + start: function() { + var ajaxViewer = this; + this.timer = setInterval(function() { ajaxViewer.heartbeat(); }, 50); + + $(document).bind("ajaxError", function(event, XMLHttpRequest, ajaxOptions, thrownError) { + ajaxViewer.onAjaxError(event, XMLHttpRequest, ajaxOptions, thrownError); + }); + + this.eventQueue = []; // reset event queue + this.sendingEventInProgress = false; + ajaxViewer.installMouseHook(); + ajaxViewer.installKeyboardHook(); + + $(window).bind("resize", function() { + ajaxViewer.onWindowResize(); + }); + }, + + stop: function() { + clearInterval(this.timer); + this.deleteCanvas(); + + this.uninstallMouseHook(); + this.uninstallKeyboardHook(); + this.eventQueue = []; + this.sendingEventInProgress = false; + + $(document).unbind("ajaxError"); + $(window).unbind("resize"); + }, + + sendMouseEvent: function(event, x, y, whichButton, modifiers) { + this.eventQueue.push({ + type: AjaxViewer.EVENT_QUEUE_MOUSE_EVENT, + event: event, + x: x, + y: y, + code: whichButton, + modifiers: modifiers + }); + this.checkEventQueue(); + }, +//NEW insert the keyboard tables file here +// ajaxKeys.js + + setupKeyboardTranslationTable : function() { + this.keyboardMappers = []; + + var mapper = new KeyboardMapper(); + this.keyboardMappers[AjaxViewer.KEYBOARD_TYPE_ENGLISH] = mapper; + mapper.setKeyboardType(KeyboardMapper.KEYBOARD_TYPE_COOKED); + + mapper = new KeyboardMapper(); + this.keyboardMappers[AjaxViewer.KEYBOARD_TYPE_JAPANESE] = mapper; + mapper.setKeyboardType(KeyboardMapper.KEYBOARD_TYPE_RAW); + + // JP keyboard plugged in a English host OS +/* + mapper.jsX11KeysymMap[AjaxViewer.JS_KEY_JP_COLON] = AjaxViewer.X11_KEY_COLON; + mapper.jsX11KeysymMap[AjaxViewer.JS_KEY_JP_CLOSE_BRACKET] = AjaxViewer.X11_KEY_CLOSE_BRACKET; + mapper.jsX11KeysymMap[AjaxViewer.JS_KEY_JP_AT_SIGN] = AjaxViewer.X11_KEY_GRAVE_ACCENT; + mapper.jsX11KeysymMap[AjaxViewer.JS_KEY_JP_OPEN_BRACKET] = AjaxViewer.X11_KEY_OPEN_BRACKET; + mapper.jsX11KeysymMap[AjaxViewer.JS_KEY_JP_BACK_SLASH] = AjaxViewer.X11_KEY_REVERSE_SOLIUS; // X11 REVERSE SOLIDUS + mapper.jsX11KeysymMap[AjaxViewer.JS_KEY_JP_YEN_MARK] = AjaxViewer.X11_KEY_YEN_MARK; // X11 YEN SIGN + mapper.jsKeyPressX11KeysymMap[61] = [ + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_CIRCUMFLEX_ACCENT, modifiers: 0 }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_CIRCUMFLEX_ACCENT, modifiers: 0 }, + ]; + + mapper.jsKeyPressX11KeysymMap[43] = [ + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_SHIFT, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_SHIFT, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_TILDE, modifiers: 0, shift: true }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_TILDE, modifiers: 0, shift: true } + ]; +*/ + +/* Old + // JP keyboard plugged in a Japanese host OS + mapper.jsX11KeysymMap[222] = AjaxViewer.X11_KEY_CIRCUMFLEX_ACCENT; + mapper.jsX11KeysymMap[220] = AjaxViewer.X11_KEY_YEN_MARK; + mapper.jsX11KeysymMap[219] = AjaxViewer.X11_KEY_OPEN_BRACKET; + mapper.jsX11KeysymMap[221] = AjaxViewer.X11_KEY_CLOSE_BRACKET; + mapper.jsX11KeysymMap[59] = AjaxViewer.X11_KEY_COLON; // Firefox + mapper.jsX11KeysymMap[186] = AjaxViewer.X11_KEY_COLON; // Chrome + mapper.jsX11KeysymMap[226] = AjaxViewer.X11_KEY_REVERSE_SOLIUS; // \| key left to right SHIFT on JP keyboard + mapper.jsX11KeysymMap[240] = [ + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_CAPSLOCK, modifiers: 0 }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_CAPSLOCK, modifiers: 0 }, + ]; + + // for keycode 107, keypress 59 + mapper.jsKeyPressX11KeysymMap[59] = [ + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_SEMI_COLON, modifiers: 0 }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_SEMI_COLON, modifiers: 0 }, + ]; + + // for keycode 107, keypress 43 + mapper.jsKeyPressX11KeysymMap[43] = [ + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_SHIFT, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_SHIFT, modifiers: 0, shift: false }, + {type: AjaxViewer.KEY_DOWN, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: true }, + {type: AjaxViewer.KEY_UP, code: AjaxViewer.X11_KEY_ADD, modifiers: 0, shift: true }, + ]; + +*/ + // create the mapping table based on the tables input + if (keyboardTables != undefined ) { + + for(var i = 0; i < keyboardTables.length; i++) { + var mappingTbl = keyboardTables[i]; + var mappings = mappingTbl.mappingTable; + var x11Maps = mappings.X11; + for (var j = 0; j < x11Maps.length; j++) { + var code = x11Maps[j].keycode; + var mappedEntry = x11Maps[j].entry; + mapper.jsX11KeysymMap[code] = mappedEntry; + } + var keyPressMaps = mappings.keyPress; + for (var j = 0; j < keyPressMaps.length; j++) { + var code = keyPressMaps[j].keycode; + var mappedEntry = keyPressMaps[j].entry; + mapper.jsKeyPressX11KeysymMap[code] = mappedEntry; + } + + } + } + + }, // end of the setupKeyboardTranslationTable function + + getCurrentKeyboardMapper : function() { + return this.keyboardMappers[this.currentKeyboard]; + }, + + setupUIController : function() { + var ajaxViewer = this; + var pullDownElement = $("#toolbar").find(".pulldown"); + pullDownElement.hover( + function(e) { + var subMenu = pullDownElement.find("ul"); + var offset = subMenu.parent().offset(); + subMenu.css("left", offset.left); + + $("li.current").removeClass("current"); + $("li:has(a[cmd$=" + ajaxViewer.currentKeyboard + "])", subMenu).addClass("current"); + subMenu.css("z-index", "" + ajaxViewer.maxTileZIndex + 1).show(); + return false; + }, + + function(e) { + pullDownElement.find("ul").hide(); + return false; + } + ); + + $("[cmd]", "#toolbar").each(function(i, val) { + $(val).click(function(e) { + var cmd = $(e.target).attr("cmd"); + if(cmd) + ajaxViewer.onCommand(cmd); + else { + var cmdLink = $(e.target).closest("a"); + + if(cmdLink.attr("cmd")) { + var cmd = cmdLink.attr("cmd"); + ajaxViewer.onCommand(cmd); + } + } + }); + }); + }, + + onCommand : function(cmd) { + if(cmd == "keyboard_jp") { + $("#toolbar").find(".pulldown").find("ul").hide(); + this.currentKeyboard = AjaxViewer.KEYBOARD_TYPE_JAPANESE; + } else if(cmd == "keyboard_us") { + $("#toolbar").find(".pulldown").find("ul").hide(); + this.currentKeyboard = AjaxViewer.KEYBOARD_TYPE_ENGLISH; + } else if(cmd == "sendCtrlAltDel") { + this.sendKeyboardEvent(AjaxViewer.KEY_DOWN, 0xffe9, 0); // X11 Alt + this.sendKeyboardEvent(AjaxViewer.KEY_DOWN, 0xffe3, 0); // X11 Ctrl + this.sendKeyboardEvent(AjaxViewer.KEY_DOWN, 0xffff, 0); // X11 Del + this.sendKeyboardEvent(AjaxViewer.KEY_UP, 0xffff, 0); + this.sendKeyboardEvent(AjaxViewer.KEY_UP, 0xffe3, 0); + this.sendKeyboardEvent(AjaxViewer.KEY_UP, 0xffe9, 0); + } else if(cmd == "sendCtrlEsc") { + this.sendKeyboardEvent(AjaxViewer.KEY_DOWN, 0xffe3, 0); // X11 Ctrl + this.sendKeyboardEvent(AjaxViewer.KEY_DOWN, 0xff1b, 0); // X11 ESC + this.sendKeyboardEvent(AjaxViewer.KEY_UP, 0xff1b, 0); + this.sendKeyboardEvent(AjaxViewer.KEY_UP, 0xffe3, 0); + } else if(cmd == "toggle_logwin") { + if(!g_logger.isOpen()) { + g_logger.enable(true); + g_logger.open(); + g_logger.log(Logger.LEVEL_SYS, "Accept languages: " + acceptLanguages + ", current language: " + getCurrentLanguage()); + } else { + g_logger.close(); + } + } + }, + + sendKeyboardEvent: function(event, code, modifiers) { + // back door to open logger window - CTRL-ATL-SHIFT+SPACE + if(code == 32 && + (modifiers & AjaxViewer.SHIFT_KEY_MASK | AjaxViewer.CTRL_KEY_MASK | AjaxViewer.ALT_KEY_MASK) == (AjaxViewer.SHIFT_KEY_MASK | AjaxViewer.CTRL_KEY_MASK | AjaxViewer.ALT_KEY_MASK)) { + + if(!g_logger.isOpen()) { + g_logger.enable(true); + g_logger.open(); + g_logger.log(Logger.LEVEL_SYS, "Accept languages: " + acceptLanguages + ", current language: " + getCurrentLanguage()); + } else { + g_logger.close(); + } + } + + var len; + g_logger.log(Logger.LEVEL_INFO, "Keyboard event: " + AjaxViewer.getEventName(event) + ", code: " + code + ", modifiers: " + modifiers + ', char: ' + String.fromCharCode(code)); + this.eventQueue.push({ + type: AjaxViewer.EVENT_QUEUE_KEYBOARD_EVENT, + event: event, + code: code, + modifiers: modifiers + }); + + if(event != AjaxViewer.KEY_DOWN) + this.checkEventQueue(); + }, + + aggregateEvents: function() { + var ajaxViewer = this; + var aggratedQueue = []; + + var aggregating = false; + var mouseX; + var mouseY; + $.each(ajaxViewer.eventQueue, function(index, item) { + if(item.type != AjaxViewer.EVENT_QUEUE_MOUSE_EVENT) { + aggratedQueue.push(item); + } else { + if(!aggregating) { + if(item.event == AjaxViewer.MOUSE_MOVE) { + aggregating = true; + mouseX = item.x; + mouseY = item.y; + } else { + aggratedQueue.push(item); + } + } else { + if(item.event == AjaxViewer.MOUSE_MOVE) { + // continue to aggregate mouse move event + mouseX = item.x; + mouseY = item.y; + } else { + aggratedQueue.push({ + type: AjaxViewer.EVENT_QUEUE_MOUSE_EVENT, + event: AjaxViewer.MOUSE_MOVE, + x: mouseX, + y: mouseY, + code: 0, + modifiers: 0 + }); + aggregating = false; + + aggratedQueue.push(item); + } + } + } + }); + + if(aggregating) { + aggratedQueue.push({ + type: AjaxViewer.EVENT_QUEUE_MOUSE_EVENT, + event: AjaxViewer.MOUSE_MOVE, + x: mouseX, + y: mouseY, + code: 0, + modifiers: 0 + }); + } + + this.eventQueue = aggratedQueue; + }, + + checkEventQueue: function() { + var ajaxViewer = this; + + if(!this.sendingEventInProgress && this.eventQueue.length > 0) { + var sb = new StringBuilder(); + sb.append(""+this.eventQueue.length).append("|"); + $.each(this.eventQueue, function() { + var item = this; + if(item.type == AjaxViewer.EVENT_QUEUE_MOUSE_EVENT) { + sb.append(""+item.type).append("|"); + sb.append(""+item.event).append("|"); + sb.append(""+item.x).append("|"); + sb.append(""+item.y).append("|"); + sb.append(""+item.code).append("|"); + sb.append(""+item.modifiers).append("|"); + } else { + sb.append(""+item.type).append("|"); + sb.append(""+item.event).append("|"); + sb.append(""+item.code).append("|"); + sb.append(""+item.modifiers).append("|"); + } + }); + this.eventQueue.length = 0; + + var url = ajaxViewer.updateUrl + "&event=" + AjaxViewer.EVENT_BAG; + + g_logger.log(Logger.LEVEL_TRACE, "Posting client event " + sb.toString() + "..."); + + ajaxViewer.sendingEventInProgress = true; + window.onStatusNotify(AjaxViewer.STATUS_SENDING); + $.post(url, {data: sb.toString()}, function(data, textStatus) { + g_logger.log(Logger.LEVEL_TRACE, "Client event " + sb.toString() + " is posted"); + + ajaxViewer.sendingEventInProgress = false; + window.onStatusNotify(AjaxViewer.STATUS_SENT); + + ajaxViewer.checkUpdate(); + }, 'html'); + } + }, + + onAjaxError: function(event, XMLHttpRequest, ajaxOptions, thrownError) { + if(window.onClientError != undefined && jQuery.isFunction(window.onClientError)) { + window.onClientError(); + } + }, + + onWindowResize: function() { + var offset = this.panel.offset(); + + var row = $('tr:first', this.panel); + var cell = $('td:first', row); + var tile = this.getTile(cell, 'tile'); + + var tileOffset = tile.offset(); + var deltaX = offset.left - tileOffset.left; + var deltaY = offset.top - tileOffset.top; + + if(deltaX != 0 || deltaY != 0) { + $(".canvas_tile").each(function() { + var offsetFrom = $(this).offset(); + $(this).css('left', offsetFrom.left + deltaX).css('top', offsetFrom.top + deltaY); + }); + } + }, + + deleteCanvas: function() { + $('.canvas_tile', $(document.body)).each(function() { + $(this).remove(); + }); + }, + + generateCanvas: function(wrapperDivId, width, height, tileWidth, tileHeight) { + var canvasParent = $('#' + wrapperDivId); + canvasParent.width(width); + canvasParent.height(height); + + if(window.onCanvasSizeChange != undefined && jQuery.isFunction(window.onCanvasSizeChange)) + window.onCanvasSizeChange(width, height); + + var tableDef = '\r\n'; + var i = 0; + var j = 0; + for(i = 0; i < Math.ceil((height + tileHeight - 1) / tileHeight); i++) { + var rowHeight = Math.min(height - i*tileHeight, tileHeight); + tableDef += '\r\n'; + + for(j = 0; j < Math.ceil((width + tileWidth - 1) / tileWidth); j++) { + var colWidth = Math.min(width - j*tileWidth, tileWidth); + tableDef += '\r\n'; + } + tableDef += '\r\n'; + } + tableDef += '
\r\n'; + + return $(tableDef).appendTo(canvasParent); + }, + + getTile: function(cell, name) { + var clonedDiv = cell.data(name); + if(!clonedDiv) { + var offset = cell.offset(); + var divDef = "
"; + + clonedDiv = $(divDef).appendTo($(document.body)); + cell.data(name, clonedDiv); + } + + return clonedDiv; + }, + + initCell: function(cell) { + if(!cell.data("init")) { + cell.data("init", true); + + cell.data("current", 0); + this.getTile(cell, "tile2"); + this.getTile(cell, "tile"); + } + }, + + displayCell: function(cell, bg) { + var div; + var divPrev; + if(!cell.data("current")) { + cell.data("current", 1); + + divPrev = this.getTile(cell, "tile"); + div = this.getTile(cell, "tile2"); + } else { + cell.data("current", 0); + divPrev = this.getTile(cell, "tile2"); + div = this.getTile(cell, "tile"); + } + + var zIndex = parseInt(divPrev.css("z-index")) + 1; + this.maxTileZIndex = Math.max(this.maxTileZIndex, zIndex); + div.css("z-index", zIndex); + div.css("background", bg); + }, + + updateTile: function() { + if(this.dirty) { + var ajaxViewer = this; + var tileWidth = this.tileWidth; + var tileHeight = this.tileHeight; + var imgUrl = this.imgUrl; + var panel = this.panel; + + if(this.fullImage) { + $.each(this.tileMap, function() { + var i = $(this)[0]; + var j = $(this)[1]; + var row = $("TR:eq("+i+")", panel); + var cell = $("TD:eq("+j+")", row); + var attr = "url(" + imgUrl + ") -"+j*tileWidth+"px -"+i*tileHeight + "px"; + + ajaxViewer.initCell(cell); + ajaxViewer.displayCell(cell, attr); + }); + } else { + $.each(this.tileMap, function(index) { + var i = $(this)[0]; + var j = $(this)[1]; + var offset = index*tileWidth; + var attr = "url(" + imgUrl + ") no-repeat -"+offset+"px 0px"; + var row = $("TR:eq("+i+")", panel); + var cell = $("TD:eq("+j+")", row); + + ajaxViewer.initCell(cell); + ajaxViewer.displayCell(cell, attr); + }); + } + + this.dirty = false; + } + }, + + heartbeat: function() { + this.checkEventQueue(); + this.checkUpdate(); + }, + + checkUpdate: function() { + if(!this.isDirty()) + return; + + if(this.isImageLoaded()) { + this.updateTile(); + var url = this.updateUrl; + var ajaxViewer = this; + + window.onStatusNotify(AjaxViewer.STATUS_RECEIVING); + $.getScript(url, function(data, textStatus) { + if(/^/.test(data)) { + ajaxViewer.stop(); + $(document.body).html(data); + } else { + eval(data); + ajaxViewer.setDirty(true); + window.onStatusNotify(AjaxViewer.STATUS_RECEIVED); + + ajaxViewer.checkUpdate(); + } + }); + } + }, + + ptInPanel: function(pageX, pageY) { + var mainPanel = this.panel; + + var offset = mainPanel.offset(); + var x = pageX - offset.left; + var y = pageY - offset.top; + + if(x < 0 || y < 0 || x > mainPanel.width() - 1 || y > mainPanel.height() - 1) + return false; + return true; + }, + + pageToPanel: function(pageX, pageY) { + var mainPanel = this.panel; + + var offset = mainPanel.offset(); + var x = pageX - offset.left; + var y = pageY - offset.top; + + if(x < 0) + x = 0; + if(x > mainPanel.width() - 1) + x = mainPanel.width() - 1; + + if(y < 0) + y = 0; + if(y > mainPanel.height() - 1) + y = mainPanel.height() - 1; + + return { x: Math.ceil(x), y: Math.ceil(y) }; + }, + + installMouseHook: function() { + var ajaxViewer = this; + var target = $(document.body); + + target.mousemove(function(e) { + if(!ajaxViewer.ptInPanel(e.pageX, e.pageY)) + return true; + + var pt = ajaxViewer.pageToPanel(e.pageX, e.pageY); + ajaxViewer.onMouseMove(pt.x, pt.y); + + e.stopPropagation(); + return false; + }); + + target.mousedown(function(e) { + ajaxViewer.panel.parent().focus(); + + if(!ajaxViewer.ptInPanel(e.pageX, e.pageY)) + return true; + + var modifiers = ajaxViewer.getKeyModifiers(e); + var whichButton = e.button; + + var pt = ajaxViewer.pageToPanel(e.pageX, e.pageY); + ajaxViewer.onMouseDown(pt.x, pt.y, whichButton, modifiers); + + e.stopPropagation(); + return false; + }); + + target.mouseup(function(e) { + if(!ajaxViewer.ptInPanel(e.pageX, e.pageY)) + return true; + + var modifiers = ajaxViewer.getKeyModifiers(e); + var whichButton = e.button; + + var pt = ajaxViewer.pageToPanel(e.pageX, e.pageY); + + ajaxViewer.onMouseUp(pt.x, pt.y, whichButton, modifiers); + e.stopPropagation(); + return false; + }); + + // disable browser right-click context menu + target.bind("contextmenu", function() { return false; }); + }, + + uninstallMouseHook : function() { + var target = $(document); + target.unbind("mousemove"); + target.unbind("mousedown"); + target.unbind("mouseup"); + target.unbind("contextmenu"); + }, + + requiresDefaultKeyProcess : function(e) { + switch(e.which) { + case 8 : // backspace + case 9 : // TAB + case 19 : // PAUSE/BREAK + case 20 : // CAPSLOCK + case 27 : // ESCAPE + case 16 : // SHIFT key + case 17 : // CTRL key + case 18 : // ALT key + case 33 : // PGUP + case 34 : // PGDN + case 35 : // END + case 36 : // HOME + case 37 : // LEFT + case 38 : // UP + case 39 : // RIGHT + case 40 : // DOWN + return false; + } + + if(this.getKeyModifiers(e) == AjaxViewer.SHIFT_KEY_MASK) + return true; + + if(this.getKeyModifiers(e) != 0) + return false; + + return true; + }, + + installKeyboardHook: function() { + var ajaxViewer = this; + var target = $(document); + + target.keypress(function(e) { + ajaxViewer.onKeyPress(e.which, ajaxViewer.getKeyModifiers(e)); + + e.stopPropagation(); + if(ajaxViewer.requiresDefaultKeyProcess(e)) + return true; + + e.preventDefault(); + return false; + }); + + target.keydown(function(e) { + ajaxViewer.onKeyDown(e.which, ajaxViewer.getKeyModifiers(e)); + + e.stopPropagation(); + if(ajaxViewer.requiresDefaultKeyProcess(e)) + return true; + + e.preventDefault(); + return false; + }); + + target.keyup(function(e) { + ajaxViewer.onKeyUp(e.which, ajaxViewer.getKeyModifiers(e)); + + e.stopPropagation(); + if(ajaxViewer.requiresDefaultKeyProcess(e)) + return true; + + e.preventDefault(); + return false; + }); + }, + + uninstallKeyboardHook : function() { + var target = $(document); + target.unbind("keypress"); + target.unbind("keydown"); + target.unbind("keyup"); + }, + + onMouseMove: function(x, y) { + this.sendMouseEvent(AjaxViewer.MOUSE_MOVE, x, y, 0, 0); + }, + + onMouseDown: function(x, y, whichButton, modifiers) { + this.sendMouseEvent(AjaxViewer.MOUSE_DOWN, x, y, whichButton, modifiers); + }, + + onMouseUp: function(x, y, whichButton, modifiers) { + this.sendMouseEvent(AjaxViewer.MOUSE_UP, x, y, whichButton, modifiers); + + var curTick = new Date().getTime(); + if(this.lastClickEvent.time && (curTick - this.lastClickEvent.time < 300)) { + this.onMouseDblClick(this.lastClickEvent.x, this.lastClickEvent.y, + this.lastClickEvent.button, this.lastClickEvent.modifiers); + } + + this.lastClickEvent.x = x; + this.lastClickEvent.y = y; + this.lastClickEvent.button = whichButton; + this.lastClickEvent.modifiers = modifiers; + this.lastClickEvent.time = curTick; + }, + + onMouseDblClick: function(x, y, whichButton, modifiers) { + this.sendMouseEvent(AjaxViewer.MOUSE_DBLCLK, x, y, whichButton, modifiers); + }, + + onKeyPress: function(code, modifiers) { + g_logger.log(Logger.LEVEL_WARN, "RAW KEYBOARD EVENT. KEY-PRESS: " + code + ", modifers: " + modifiers); + + this.dispatchKeyboardInput(AjaxViewer.KEY_PRESS, code, modifiers); + }, + + onKeyDown: function(code, modifiers) { + g_logger.log(Logger.LEVEL_WARN, "RAW KEYBOARD EVENT. KEY-DOWN: " + code + ", modifers: " + modifiers); + + this.dispatchKeyboardInput(AjaxViewer.KEY_DOWN, code, modifiers); + }, + + onKeyUp: function(code, modifiers) { + g_logger.log(Logger.LEVEL_WARN, "RAW KEYBOARD EVENT. KEY-UP: " + code + ", modifers: " + modifiers); + + this.dispatchKeyboardInput(AjaxViewer.KEY_UP, code, modifiers); + }, + + dispatchKeyboardInput : function(event, code, modifiers) { + var keyboardMapper = ajaxViewer.getCurrentKeyboardMapper(); + keyboardMapper.inputFeed(event, code, modifiers, this.guestos, $.browser, $.browser.version); + this.dispatchMappedKeyboardInput(keyboardMapper.getMappedInput()); + }, + + dispatchMappedKeyboardInput : function(mappedInput) { + for(var i = 0; i < mappedInput.length; i++) { + switch(mappedInput[i].type) { + case AjaxViewer.KEY_DOWN : + this.sendKeyboardEvent(AjaxViewer.KEY_DOWN, mappedInput[i].code, mappedInput[i].modifiers); + break; + + case AjaxViewer.KEY_UP : + this.sendKeyboardEvent(AjaxViewer.KEY_UP, mappedInput[i].code, mappedInput[i].modifiers); + break; + + case AjaxViewer.KEY_PRESS : + this.sendKeyboardEvent(AjaxViewer.KEY_PRESS, mappedInput[i].code, mappedInput[i].modifiers); + break; + } + } + }, + + getKeyModifiers: function(e) { + var modifiers = 0; + if(e.altKey) + modifiers |= AjaxViewer.ALT_KEY_MASK; + + if(e.altLeft) + modifiers |= AjaxViewer.LEFT_ALT_MASK; + + if(e.ctrlKey) + modifiers |= AjaxViewer.CTRL_KEY_MASK; + + if(e.ctrlLeft) + modifiers |= AjaxViewer.LEFT_CTRL_MASK; + + if(e.shiftKey) + modifiers |= AjaxViewer.SHIFT_KEY_MASK; + + if(e.shiftLeft) + modifiers |= AjaxViewer.LEFT_SHIFT_MASK; + + if(e.metaKey) + modifiers |= AjaxViewer.META_KEY_MASK; + + return modifiers; + } +}; + diff --git a/services/console-proxy/server/js/cloud.logger.js b/services/console-proxy/server/js/cloud.logger.js new file mode 100644 index 00000000000..751627ed96c --- /dev/null +++ b/services/console-proxy/server/js/cloud.logger.js @@ -0,0 +1,338 @@ +/* +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. +*/ + +// Version: 1.9.1.152 + +// +// Javascript logger utility +// Author +// Kelven Yang +// 2/25/2010 +// + +function Logger() { + this.bDockEnabled = true; + + this.logWin = null; + this.logger = null; + this.header = null; + + this.bEnabled = true; + this.level = 0; + + this.bMoving = false; + this.offsetStart = {left: 0, top: 0}; + this.ptStart = {x: 0, y: 0}; +} + +Logger.DEFAULT_WIN_HEIGHT = 500; +Logger.LEVEL_TRACE = 0; +Logger.LEVEL_DEBUG = 1; +Logger.LEVEL_INFO = 2; +Logger.LEVEL_WARN = 3; +Logger.LEVEL_ERROR = 4; +Logger.LEVEL_FATAL = 5; +Logger.LEVEL_SYS = 100; + +Logger.prototype = { + + open: function() { + if(this.logWin) { + this.logWin.show(); + + this.log(Logger.LEVEL_SYS, "Logger is open in browser: " + this.objectToString($.browser)); + return; + } + + var logger = this; + var logWinMarkup = [ + '
', + '
', + '
', + '', + '', + '', + '
', + '', + '
', + '
', + '
', + '', + '', + '
', + '
', + '
', + '
' + ].join(''); + + this.logWin = $(logWinMarkup).appendTo(document.body); + this.header = $('.logwin_title:first', this.logWin); + this.logger = $('.logwin_content:first', this.logWin); + + $(".logwin_title", this.logWin).mousedown(function(e) { + if($(e.target).attr('cmd')) + return true; + + if(!logger.bMoving) { + logger.bMoving = true; + logger.offsetStart = logger.logWin.offset(); + logger.ptStart = {x: e.pageX, y: e.pageY}; + + $(document).bind("mousemove", function(e) { + if(logger.bMoving) { + logger.enableDocking(false); + + var logWinNewLeft = logger.offsetStart.left + e.pageX - logger.ptStart.x; + var logWinNewTop = logger.offsetStart.top + e.pageY - logger.ptStart.y; + + logger.logWin.css("left", logWinNewLeft + "px").css("top", logWinNewTop + "px"); + } + return false; + }); + + $(document).bind("mouseup", function(e) { + if(logger.bMoving) { + logger.bMoving = false; + $(document).unbind("mousemove", arguments.callee.name); + $(document).unbind("mouseup", arguments.callee.name); + + return false; + } + return true; + }); + } + + // prevent default handling + return false; + }).dblclick(function(e) { + logger.expand(!logger.isExpanded()); + }); + + this.logWin.click(function(e) { + if($(e.target).attr('cmd')) { + switch($(e.target).attr('cmd')) { + case '1' : + logger.enable(true); + break; + + case '2' : + logger.enable(false); + break; + + case '3' : + logger.clear(); + break; + + case '4' : + logger.enableDocking(true); + logger.dockIn(); + break; + + case '5' : + logger.expand(!logger.isExpanded()); + break; + + default : + break; + } + } + }); + + $("#template_type", this.logWin).change(function(e) { + logger.setLevel(parseInt($(this).val())); + }); + + this.logWin.css("left", (($(document.body).width() - this.logWin.width()) / 2) + "px"); + this.dockIn(); + + this.log(Logger.LEVEL_SYS, "Logger is open in browser: " + this.objectToString($.browser)); + }, + + close: function() { + if(this.logWin) + this.logWin.hide(); + }, + + isOpen: function() { + if(this.logWin) + return this.logWin.is(":visible"); + return false; + }, + + dockIn: function() { + var logger = this; + var offset = this.logWin.offset(); + var bottom = offset.top + this.logWin.height(); + var delta = bottom - 2; + + this.logWin.animate({top: (offset.top - delta) + "px"}, 200, + function() { + logger.logWin.unbind("mouseleave"); + logger.logWin.bind("mouseenter", function(e) { + if(logger.bDockEnabled) + logger.dockOut(); + }); + } + ); + }, + + dockOut: function() { + var logger = this; + this.logWin.animate({top: "0px"}, 200, + function() { + logger.logWin.unbind("mouseenter"); + logger.logWin.bind("mouseleave", function(e) { + if(logger.bDockEnabled) { + var xPosInLogWin = e.pageX - logger.logWin.offset().left; + var yPosInLogWin = e.pageY - logger.logWin.offset().top; + + if(xPosInLogWin < 0 || yPosInLogWin < 0 || + xPosInLogWin > logger.logWin.width() || yPosInLogWin > logger.logWin.height()) { + logger.dockIn(); + } + } + }); + } + ); + }, + + enableDocking: function(bEnable) { + this.bDockEnabled = bEnable; + }, + + log: function(level, message) { + // Note : LEVEL_SYS message will always be logged + if(this.logger && (level == Logger.LEVEL_SYS || this.bEnabled && level >= this.level)) { + var curTime = new Date(); + var curTimeString = [ + '', curTime.getMonth(), + '/', curTime.getDate(), + '/', curTime.getYear(), + ' ', + curTime.getHours(), + ':', curTime.getMinutes(), + ":", curTime.getSeconds(), + ".", curTime.getMilliseconds()].join(''); + + this.logger.append(this.getLevelDisplayString(level) + " - " + curTimeString + " - " + message + '
'); + } + }, + + clear: function() { + if(this.logger) { + this.logger.empty(); + this.log(Logger.LEVEL_SYS, "Logger is cleared"); + } + }, + + setLevel: function(level) { + this.level = level; + + this.log(Logger.LEVEL_SYS, "Set logger trace level to " + this.getLevelDisplayString(level)); + }, + + enable: function(bEnabled) { + this.bEnabled = bEnabled; + + if(bEnabled) + this.log(Logger.LEVEL_SYS, "Logger is enabled"); + else + this.log(Logger.LEVEL_SYS, "Logger is disabled"); + }, + + expand: function(bExpand) { + if(bExpand) { + this.logWin.height(Logger.DEFAULT_WIN_HEIGHT); + this.logger.height(Logger.DEFAULT_WIN_HEIGHT - this.header.height()); + } else { + this.logWin.height(this.header.height()); + this.logger.height(0); + } + }, + + isExpanded: function() { + return this.logWin.height() > this.header.height(); + }, + + getLevelDisplayString: function(level) { + switch(level) { + case Logger.LEVEL_TRACE : + return "TRACE"; + + case Logger.LEVEL_DEBUG : + return "DEBUG"; + + case Logger.LEVEL_INFO : + return "INFO"; + + case Logger.LEVEL_WARN : + return "WARN"; + + case Logger.LEVEL_ERROR : + return "ERROR"; + + case Logger.LEVEL_FATAL : + return "FATAL"; + + case Logger.LEVEL_SYS : + return "SYSINFO"; + } + + return "LEVEL " + level; + }, + + // this is a util function which actually can be put elsewhere instead of in this class + objectToString : function(object) { + if(object) { + if(object instanceof Object) { + var sb = ['{' ]; + + $.each(object, function(name, val) { + sb.push('' + name + ': '); + + if(val instanceof Object) { + sb.push(this.objectToString(val)); + } else { + sb.push('' + val); + } + + sb.push(','); + }); + + if(sb[sb.length - 1] == ',' ) + sb.length = sb.length - 1; + + sb.push('}'); + return sb.join(""); + } else { + return '' + object; + } + } else { + return 'N/A'; + } + } +}; + diff --git a/services/console-proxy/server/js/handler.js b/services/console-proxy/server/js/handler.js new file mode 100644 index 00000000000..d22ff079ee6 --- /dev/null +++ b/services/console-proxy/server/js/handler.js @@ -0,0 +1,72 @@ +/* +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. +*/ + +// +// Callback handlers for AJAX viewer +// Author +// Kelven Yang +// 11/18/2009 +// +function onKickoff() { + ajaxViewer.stop(); + $('#toolbar').remove(); + $('#main_panel').html('

This session is terminated because a session for the same VM has been created elsewhere.

'); +} + +function onDisconnect() { + ajaxViewer.stop(); + $('#toolbar').remove(); + $('#main_panel').html('

This session is terminated as the machine you are accessing has terminated the connection.

'); +} + +function onClientError() { + ajaxViewer.stop(); + $('#toolbar').remove(); + $('#main_panel').html('

Client communication error, please retry later.

'); +} + +function onCanvasSizeChange(width, height) { + $('#toolbar').width(width); +} + +function onStatusNotify(status) { + if(status == ajaxViewer.STATUS_SENDING || status == ajaxViewer.STATUS_RECEIVING) + $('#light').removeClass('dark').addClass('bright'); + else + $('#light').removeClass('bright').addClass('dark'); +} + +function sendCtrlAltDel() { + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_DOWN, 45, ajaxViewer.CTRL_KEY | ajaxViewer.ALT_KEY); + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_UP, 45, ajaxViewer.CTRL_KEY | ajaxViewer.ALT_KEY); +} + +function sendCtrlEsc() { + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_DOWN, 17, 0); + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_DOWN, 27, ajaxViewer.CTRL_KEY); + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_UP, 27, ajaxViewer.CTRL_KEY); + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_UP, 17, 0); +} + +function sendAltTab() { + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_DOWN, 18, 0); + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_DOWN, 9, ajaxViewer.ALT_KEY); + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_UP, 9, ajaxViewer.ALT_KEY); + ajaxViewer.sendKeyboardEvent(ajaxViewer.KEY_UP, 18, 0); +} diff --git a/services/console-proxy/server/js/jquery.js b/services/console-proxy/server/js/jquery.js new file mode 100644 index 00000000000..b1ae21d8b23 --- /dev/null +++ b/services/console-proxy/server/js/jquery.js @@ -0,0 +1,19 @@ +/* + * jQuery JavaScript Library v1.3.2 + * http://jquery.com/ + * + * Copyright (c) 2009 John Resig + * Dual licensed under the MIT and GPL licenses. + * http://docs.jquery.com/License + * + * Date: 2009-02-19 17:34:21 -0500 (Thu, 19 Feb 2009) + * Revision: 6246 + */ +(function(){var l=this,g,y=l.jQuery,p=l.$,o=l.jQuery=l.$=function(E,F){return new o.fn.init(E,F)},D=/^[^<]*(<(.|\s)+>)[^>]*$|^#([\w-]+)$/,f=/^.[^:#\[\.,]*$/;o.fn=o.prototype={init:function(E,H){E=E||document;if(E.nodeType){this[0]=E;this.length=1;this.context=E;return this}if(typeof E==="string"){var G=D.exec(E);if(G&&(G[1]||!H)){if(G[1]){E=o.clean([G[1]],H)}else{var I=document.getElementById(G[3]);if(I&&I.id!=G[3]){return o().find(E)}var F=o(I||[]);F.context=document;F.selector=E;return F}}else{return o(H).find(E)}}else{if(o.isFunction(E)){return o(document).ready(E)}}if(E.selector&&E.context){this.selector=E.selector;this.context=E.context}return this.setArray(o.isArray(E)?E:o.makeArray(E))},selector:"",jquery:"1.3.2",size:function(){return this.length},get:function(E){return E===g?Array.prototype.slice.call(this):this[E]},pushStack:function(F,H,E){var G=o(F);G.prevObject=this;G.context=this.context;if(H==="find"){G.selector=this.selector+(this.selector?" ":"")+E}else{if(H){G.selector=this.selector+"."+H+"("+E+")"}}return G},setArray:function(E){this.length=0;Array.prototype.push.apply(this,E);return this},each:function(F,E){return o.each(this,F,E)},index:function(E){return o.inArray(E&&E.jquery?E[0]:E,this)},attr:function(F,H,G){var E=F;if(typeof F==="string"){if(H===g){return this[0]&&o[G||"attr"](this[0],F)}else{E={};E[F]=H}}return this.each(function(I){for(F in E){o.attr(G?this.style:this,F,o.prop(this,E[F],G,I,F))}})},css:function(E,F){if((E=="width"||E=="height")&&parseFloat(F)<0){F=g}return this.attr(E,F,"curCSS")},text:function(F){if(typeof F!=="object"&&F!=null){return this.empty().append((this[0]&&this[0].ownerDocument||document).createTextNode(F))}var E="";o.each(F||this,function(){o.each(this.childNodes,function(){if(this.nodeType!=8){E+=this.nodeType!=1?this.nodeValue:o.fn.text([this])}})});return E},wrapAll:function(E){if(this[0]){var F=o(E,this[0].ownerDocument).clone();if(this[0].parentNode){F.insertBefore(this[0])}F.map(function(){var G=this;while(G.firstChild){G=G.firstChild}return G}).append(this)}return this},wrapInner:function(E){return this.each(function(){o(this).contents().wrapAll(E)})},wrap:function(E){return this.each(function(){o(this).wrapAll(E)})},append:function(){return this.domManip(arguments,true,function(E){if(this.nodeType==1){this.appendChild(E)}})},prepend:function(){return this.domManip(arguments,true,function(E){if(this.nodeType==1){this.insertBefore(E,this.firstChild)}})},before:function(){return this.domManip(arguments,false,function(E){this.parentNode.insertBefore(E,this)})},after:function(){return this.domManip(arguments,false,function(E){this.parentNode.insertBefore(E,this.nextSibling)})},end:function(){return this.prevObject||o([])},push:[].push,sort:[].sort,splice:[].splice,find:function(E){if(this.length===1){var F=this.pushStack([],"find",E);F.length=0;o.find(E,this[0],F);return F}else{return this.pushStack(o.unique(o.map(this,function(G){return o.find(E,G)})),"find",E)}},clone:function(G){var E=this.map(function(){if(!o.support.noCloneEvent&&!o.isXMLDoc(this)){var I=this.outerHTML;if(!I){var J=this.ownerDocument.createElement("div");J.appendChild(this.cloneNode(true));I=J.innerHTML}return o.clean([I.replace(/ jQuery\d+="(?:\d+|null)"/g,"").replace(/^\s*/,"")])[0]}else{return this.cloneNode(true)}});if(G===true){var H=this.find("*").andSelf(),F=0;E.find("*").andSelf().each(function(){if(this.nodeName!==H[F].nodeName){return}var I=o.data(H[F],"events");for(var K in I){for(var J in I[K]){o.event.add(this,K,I[K][J],I[K][J].data)}}F++})}return E},filter:function(E){return this.pushStack(o.isFunction(E)&&o.grep(this,function(G,F){return E.call(G,F)})||o.multiFilter(E,o.grep(this,function(F){return F.nodeType===1})),"filter",E)},closest:function(E){var G=o.expr.match.POS.test(E)?o(E):null,F=0;return this.map(function(){var H=this;while(H&&H.ownerDocument){if(G?G.index(H)>-1:o(H).is(E)){o.data(H,"closest",F);return H}H=H.parentNode;F++}})},not:function(E){if(typeof E==="string"){if(f.test(E)){return this.pushStack(o.multiFilter(E,this,true),"not",E)}else{E=o.multiFilter(E,this)}}var F=E.length&&E[E.length-1]!==g&&!E.nodeType;return this.filter(function(){return F?o.inArray(this,E)<0:this!=E})},add:function(E){return this.pushStack(o.unique(o.merge(this.get(),typeof E==="string"?o(E):o.makeArray(E))))},is:function(E){return !!E&&o.multiFilter(E,this).length>0},hasClass:function(E){return !!E&&this.is("."+E)},val:function(K){if(K===g){var E=this[0];if(E){if(o.nodeName(E,"option")){return(E.attributes.value||{}).specified?E.value:E.text}if(o.nodeName(E,"select")){var I=E.selectedIndex,L=[],M=E.options,H=E.type=="select-one";if(I<0){return null}for(var F=H?I:0,J=H?I+1:M.length;F=0||o.inArray(this.name,K)>=0)}else{if(o.nodeName(this,"select")){var N=o.makeArray(K);o("option",this).each(function(){this.selected=(o.inArray(this.value,N)>=0||o.inArray(this.text,N)>=0)});if(!N.length){this.selectedIndex=-1}}else{this.value=K}}})},html:function(E){return E===g?(this[0]?this[0].innerHTML.replace(/ jQuery\d+="(?:\d+|null)"/g,""):null):this.empty().append(E)},replaceWith:function(E){return this.after(E).remove()},eq:function(E){return this.slice(E,+E+1)},slice:function(){return this.pushStack(Array.prototype.slice.apply(this,arguments),"slice",Array.prototype.slice.call(arguments).join(","))},map:function(E){return this.pushStack(o.map(this,function(G,F){return E.call(G,F,G)}))},andSelf:function(){return this.add(this.prevObject)},domManip:function(J,M,L){if(this[0]){var I=(this[0].ownerDocument||this[0]).createDocumentFragment(),F=o.clean(J,(this[0].ownerDocument||this[0]),I),H=I.firstChild;if(H){for(var G=0,E=this.length;G1||G>0?I.cloneNode(true):I)}}if(F){o.each(F,z)}}return this;function K(N,O){return M&&o.nodeName(N,"table")&&o.nodeName(O,"tr")?(N.getElementsByTagName("tbody")[0]||N.appendChild(N.ownerDocument.createElement("tbody"))):N}}};o.fn.init.prototype=o.fn;function z(E,F){if(F.src){o.ajax({url:F.src,async:false,dataType:"script"})}else{o.globalEval(F.text||F.textContent||F.innerHTML||"")}if(F.parentNode){F.parentNode.removeChild(F)}}function e(){return +new Date}o.extend=o.fn.extend=function(){var J=arguments[0]||{},H=1,I=arguments.length,E=false,G;if(typeof J==="boolean"){E=J;J=arguments[1]||{};H=2}if(typeof J!=="object"&&!o.isFunction(J)){J={}}if(I==H){J=this;--H}for(;H-1}},swap:function(H,G,I){var E={};for(var F in G){E[F]=H.style[F];H.style[F]=G[F]}I.call(H);for(var F in G){H.style[F]=E[F]}},css:function(H,F,J,E){if(F=="width"||F=="height"){var L,G={position:"absolute",visibility:"hidden",display:"block"},K=F=="width"?["Left","Right"]:["Top","Bottom"];function I(){L=F=="width"?H.offsetWidth:H.offsetHeight;if(E==="border"){return}o.each(K,function(){if(!E){L-=parseFloat(o.curCSS(H,"padding"+this,true))||0}if(E==="margin"){L+=parseFloat(o.curCSS(H,"margin"+this,true))||0}else{L-=parseFloat(o.curCSS(H,"border"+this+"Width",true))||0}})}if(H.offsetWidth!==0){I()}else{o.swap(H,G,I)}return Math.max(0,Math.round(L))}return o.curCSS(H,F,J)},curCSS:function(I,F,G){var L,E=I.style;if(F=="opacity"&&!o.support.opacity){L=o.attr(E,"opacity");return L==""?"1":L}if(F.match(/float/i)){F=w}if(!G&&E&&E[F]){L=E[F]}else{if(q.getComputedStyle){if(F.match(/float/i)){F="float"}F=F.replace(/([A-Z])/g,"-$1").toLowerCase();var M=q.getComputedStyle(I,null);if(M){L=M.getPropertyValue(F)}if(F=="opacity"&&L==""){L="1"}}else{if(I.currentStyle){var J=F.replace(/\-(\w)/g,function(N,O){return O.toUpperCase()});L=I.currentStyle[F]||I.currentStyle[J];if(!/^\d+(px)?$/i.test(L)&&/^\d/.test(L)){var H=E.left,K=I.runtimeStyle.left;I.runtimeStyle.left=I.currentStyle.left;E.left=L||0;L=E.pixelLeft+"px";E.left=H;I.runtimeStyle.left=K}}}}return L},clean:function(F,K,I){K=K||document;if(typeof K.createElement==="undefined"){K=K.ownerDocument||K[0]&&K[0].ownerDocument||document}if(!I&&F.length===1&&typeof F[0]==="string"){var H=/^<(\w+)\s*\/?>$/.exec(F[0]);if(H){return[K.createElement(H[1])]}}var G=[],E=[],L=K.createElement("div");o.each(F,function(P,S){if(typeof S==="number"){S+=""}if(!S){return}if(typeof S==="string"){S=S.replace(/(<(\w+)[^>]*?)\/>/g,function(U,V,T){return T.match(/^(abbr|br|col|img|input|link|meta|param|hr|area|embed)$/i)?U:V+">"});var O=S.replace(/^\s+/,"").substring(0,10).toLowerCase();var Q=!O.indexOf("",""]||!O.indexOf("",""]||O.match(/^<(thead|tbody|tfoot|colg|cap)/)&&[1,"","
"]||!O.indexOf("",""]||(!O.indexOf("",""]||!O.indexOf("",""]||!o.support.htmlSerialize&&[1,"div
","
"]||[0,"",""];L.innerHTML=Q[1]+S+Q[2];while(Q[0]--){L=L.lastChild}if(!o.support.tbody){var R=/"&&!R?L.childNodes:[];for(var M=N.length-1;M>=0;--M){if(o.nodeName(N[M],"tbody")&&!N[M].childNodes.length){N[M].parentNode.removeChild(N[M])}}}if(!o.support.leadingWhitespace&&/^\s/.test(S)){L.insertBefore(K.createTextNode(S.match(/^\s*/)[0]),L.firstChild)}S=o.makeArray(L.childNodes)}if(S.nodeType){G.push(S)}else{G=o.merge(G,S)}});if(I){for(var J=0;G[J];J++){if(o.nodeName(G[J],"script")&&(!G[J].type||G[J].type.toLowerCase()==="text/javascript")){E.push(G[J].parentNode?G[J].parentNode.removeChild(G[J]):G[J])}else{if(G[J].nodeType===1){G.splice.apply(G,[J+1,0].concat(o.makeArray(G[J].getElementsByTagName("script"))))}I.appendChild(G[J])}}return E}return G},attr:function(J,G,K){if(!J||J.nodeType==3||J.nodeType==8){return g}var H=!o.isXMLDoc(J),L=K!==g;G=H&&o.props[G]||G;if(J.tagName){var F=/href|src|style/.test(G);if(G=="selected"&&J.parentNode){J.parentNode.selectedIndex}if(G in J&&H&&!F){if(L){if(G=="type"&&o.nodeName(J,"input")&&J.parentNode){throw"type property can't be changed"}J[G]=K}if(o.nodeName(J,"form")&&J.getAttributeNode(G)){return J.getAttributeNode(G).nodeValue}if(G=="tabIndex"){var I=J.getAttributeNode("tabIndex");return I&&I.specified?I.value:J.nodeName.match(/(button|input|object|select|textarea)/i)?0:J.nodeName.match(/^(a|area)$/i)&&J.href?0:g}return J[G]}if(!o.support.style&&H&&G=="style"){return o.attr(J.style,"cssText",K)}if(L){J.setAttribute(G,""+K)}var E=!o.support.hrefNormalized&&H&&F?J.getAttribute(G,2):J.getAttribute(G);return E===null?g:E}if(!o.support.opacity&&G=="opacity"){if(L){J.zoom=1;J.filter=(J.filter||"").replace(/alpha\([^)]*\)/,"")+(parseInt(K)+""=="NaN"?"":"alpha(opacity="+K*100+")")}return J.filter&&J.filter.indexOf("opacity=")>=0?(parseFloat(J.filter.match(/opacity=([^)]*)/)[1])/100)+"":""}G=G.replace(/-([a-z])/ig,function(M,N){return N.toUpperCase()});if(L){J[G]=K}return J[G]},trim:function(E){return(E||"").replace(/^\s+|\s+$/g,"")},makeArray:function(G){var E=[];if(G!=null){var F=G.length;if(F==null||typeof G==="string"||o.isFunction(G)||G.setInterval){E[0]=G}else{while(F){E[--F]=G[F]}}}return E},inArray:function(G,H){for(var E=0,F=H.length;E0?this.clone(true):this).get();o.fn[F].apply(o(L[K]),I);J=J.concat(I)}return this.pushStack(J,E,G)}});o.each({removeAttr:function(E){o.attr(this,E,"");if(this.nodeType==1){this.removeAttribute(E)}},addClass:function(E){o.className.add(this,E)},removeClass:function(E){o.className.remove(this,E)},toggleClass:function(F,E){if(typeof E!=="boolean"){E=!o.className.has(this,F)}o.className[E?"add":"remove"](this,F)},remove:function(E){if(!E||o.filter(E,[this]).length){o("*",this).add([this]).each(function(){o.event.remove(this);o.removeData(this)});if(this.parentNode){this.parentNode.removeChild(this)}}},empty:function(){o(this).children().remove();while(this.firstChild){this.removeChild(this.firstChild)}}},function(E,F){o.fn[E]=function(){return this.each(F,arguments)}});function j(E,F){return E[0]&&parseInt(o.curCSS(E[0],F,true),10)||0}var h="jQuery"+e(),v=0,A={};o.extend({cache:{},data:function(F,E,G){F=F==l?A:F;var H=F[h];if(!H){H=F[h]=++v}if(E&&!o.cache[H]){o.cache[H]={}}if(G!==g){o.cache[H][E]=G}return E?o.cache[H][E]:H},removeData:function(F,E){F=F==l?A:F;var H=F[h];if(E){if(o.cache[H]){delete o.cache[H][E];E="";for(E in o.cache[H]){break}if(!E){o.removeData(F)}}}else{try{delete F[h]}catch(G){if(F.removeAttribute){F.removeAttribute(h)}}delete o.cache[H]}},queue:function(F,E,H){if(F){E=(E||"fx")+"queue";var G=o.data(F,E);if(!G||o.isArray(H)){G=o.data(F,E,o.makeArray(H))}else{if(H){G.push(H)}}}return G},dequeue:function(H,G){var E=o.queue(H,G),F=E.shift();if(!G||G==="fx"){F=E[0]}if(F!==g){F.call(H)}}});o.fn.extend({data:function(E,G){var H=E.split(".");H[1]=H[1]?"."+H[1]:"";if(G===g){var F=this.triggerHandler("getData"+H[1]+"!",[H[0]]);if(F===g&&this.length){F=o.data(this[0],E)}return F===g&&H[1]?this.data(H[0]):F}else{return this.trigger("setData"+H[1]+"!",[H[0],G]).each(function(){o.data(this,E,G)})}},removeData:function(E){return this.each(function(){o.removeData(this,E)})},queue:function(E,F){if(typeof E!=="string"){F=E;E="fx"}if(F===g){return o.queue(this[0],E)}return this.each(function(){var G=o.queue(this,E,F);if(E=="fx"&&G.length==1){G[0].call(this)}})},dequeue:function(E){return this.each(function(){o.dequeue(this,E)})}}); +/* + * Sizzle CSS Selector Engine - v0.9.3 + * Copyright 2009, The Dojo Foundation + * Released under the MIT, BSD, and GPL Licenses. + * More information: http://sizzlejs.com/ + */ +(function(){var R=/((?:\((?:\([^()]+\)|[^()]+)+\)|\[(?:\[[^[\]]*\]|['"][^'"]*['"]|[^[\]'"]+)+\]|\\.|[^ >+~,(\[\\]+)+|[>+~])(\s*,\s*)?/g,L=0,H=Object.prototype.toString;var F=function(Y,U,ab,ac){ab=ab||[];U=U||document;if(U.nodeType!==1&&U.nodeType!==9){return[]}if(!Y||typeof Y!=="string"){return ab}var Z=[],W,af,ai,T,ad,V,X=true;R.lastIndex=0;while((W=R.exec(Y))!==null){Z.push(W[1]);if(W[2]){V=RegExp.rightContext;break}}if(Z.length>1&&M.exec(Y)){if(Z.length===2&&I.relative[Z[0]]){af=J(Z[0]+Z[1],U)}else{af=I.relative[Z[0]]?[U]:F(Z.shift(),U);while(Z.length){Y=Z.shift();if(I.relative[Y]){Y+=Z.shift()}af=J(Y,af)}}}else{var ae=ac?{expr:Z.pop(),set:E(ac)}:F.find(Z.pop(),Z.length===1&&U.parentNode?U.parentNode:U,Q(U));af=F.filter(ae.expr,ae.set);if(Z.length>0){ai=E(af)}else{X=false}while(Z.length){var ah=Z.pop(),ag=ah;if(!I.relative[ah]){ah=""}else{ag=Z.pop()}if(ag==null){ag=U}I.relative[ah](ai,ag,Q(U))}}if(!ai){ai=af}if(!ai){throw"Syntax error, unrecognized expression: "+(ah||Y)}if(H.call(ai)==="[object Array]"){if(!X){ab.push.apply(ab,ai)}else{if(U.nodeType===1){for(var aa=0;ai[aa]!=null;aa++){if(ai[aa]&&(ai[aa]===true||ai[aa].nodeType===1&&K(U,ai[aa]))){ab.push(af[aa])}}}else{for(var aa=0;ai[aa]!=null;aa++){if(ai[aa]&&ai[aa].nodeType===1){ab.push(af[aa])}}}}}else{E(ai,ab)}if(V){F(V,U,ab,ac);if(G){hasDuplicate=false;ab.sort(G);if(hasDuplicate){for(var aa=1;aa":function(Z,U,aa){var X=typeof U==="string";if(X&&!/\W/.test(U)){U=aa?U:U.toUpperCase();for(var V=0,T=Z.length;V=0)){if(!V){T.push(Y)}}else{if(V){U[X]=false}}}}return false},ID:function(T){return T[1].replace(/\\/g,"")},TAG:function(U,T){for(var V=0;T[V]===false;V++){}return T[V]&&Q(T[V])?U[1]:U[1].toUpperCase()},CHILD:function(T){if(T[1]=="nth"){var U=/(-?)(\d*)n((?:\+|-)?\d*)/.exec(T[2]=="even"&&"2n"||T[2]=="odd"&&"2n+1"||!/\D/.test(T[2])&&"0n+"+T[2]||T[2]);T[2]=(U[1]+(U[2]||1))-0;T[3]=U[3]-0}T[0]=L++;return T},ATTR:function(X,U,V,T,Y,Z){var W=X[1].replace(/\\/g,"");if(!Z&&I.attrMap[W]){X[1]=I.attrMap[W]}if(X[2]==="~="){X[4]=" "+X[4]+" "}return X},PSEUDO:function(X,U,V,T,Y){if(X[1]==="not"){if(X[3].match(R).length>1||/^\w/.test(X[3])){X[3]=F(X[3],null,null,U)}else{var W=F.filter(X[3],U,V,true^Y);if(!V){T.push.apply(T,W)}return false}}else{if(I.match.POS.test(X[0])||I.match.CHILD.test(X[0])){return true}}return X},POS:function(T){T.unshift(true);return T}},filters:{enabled:function(T){return T.disabled===false&&T.type!=="hidden"},disabled:function(T){return T.disabled===true},checked:function(T){return T.checked===true},selected:function(T){T.parentNode.selectedIndex;return T.selected===true},parent:function(T){return !!T.firstChild},empty:function(T){return !T.firstChild},has:function(V,U,T){return !!F(T[3],V).length},header:function(T){return/h\d/i.test(T.nodeName)},text:function(T){return"text"===T.type},radio:function(T){return"radio"===T.type},checkbox:function(T){return"checkbox"===T.type},file:function(T){return"file"===T.type},password:function(T){return"password"===T.type},submit:function(T){return"submit"===T.type},image:function(T){return"image"===T.type},reset:function(T){return"reset"===T.type},button:function(T){return"button"===T.type||T.nodeName.toUpperCase()==="BUTTON"},input:function(T){return/input|select|textarea|button/i.test(T.nodeName)}},setFilters:{first:function(U,T){return T===0},last:function(V,U,T,W){return U===W.length-1},even:function(U,T){return T%2===0},odd:function(U,T){return T%2===1},lt:function(V,U,T){return UT[3]-0},nth:function(V,U,T){return T[3]-0==U},eq:function(V,U,T){return T[3]-0==U}},filter:{PSEUDO:function(Z,V,W,aa){var U=V[1],X=I.filters[U];if(X){return X(Z,W,V,aa)}else{if(U==="contains"){return(Z.textContent||Z.innerText||"").indexOf(V[3])>=0}else{if(U==="not"){var Y=V[3];for(var W=0,T=Y.length;W=0)}}},ID:function(U,T){return U.nodeType===1&&U.getAttribute("id")===T},TAG:function(U,T){return(T==="*"&&U.nodeType===1)||U.nodeName===T},CLASS:function(U,T){return(" "+(U.className||U.getAttribute("class"))+" ").indexOf(T)>-1},ATTR:function(Y,W){var V=W[1],T=I.attrHandle[V]?I.attrHandle[V](Y):Y[V]!=null?Y[V]:Y.getAttribute(V),Z=T+"",X=W[2],U=W[4];return T==null?X==="!=":X==="="?Z===U:X==="*="?Z.indexOf(U)>=0:X==="~="?(" "+Z+" ").indexOf(U)>=0:!U?Z&&T!==false:X==="!="?Z!=U:X==="^="?Z.indexOf(U)===0:X==="$="?Z.substr(Z.length-U.length)===U:X==="|="?Z===U||Z.substr(0,U.length+1)===U+"-":false},POS:function(X,U,V,Y){var T=U[2],W=I.setFilters[T];if(W){return W(X,V,U,Y)}}}};var M=I.match.POS;for(var O in I.match){I.match[O]=RegExp(I.match[O].source+/(?![^\[]*\])(?![^\(]*\))/.source)}var E=function(U,T){U=Array.prototype.slice.call(U);if(T){T.push.apply(T,U);return T}return U};try{Array.prototype.slice.call(document.documentElement.childNodes)}catch(N){E=function(X,W){var U=W||[];if(H.call(X)==="[object Array]"){Array.prototype.push.apply(U,X)}else{if(typeof X.length==="number"){for(var V=0,T=X.length;V";var T=document.documentElement;T.insertBefore(U,T.firstChild);if(!!document.getElementById(V)){I.find.ID=function(X,Y,Z){if(typeof Y.getElementById!=="undefined"&&!Z){var W=Y.getElementById(X[1]);return W?W.id===X[1]||typeof W.getAttributeNode!=="undefined"&&W.getAttributeNode("id").nodeValue===X[1]?[W]:g:[]}};I.filter.ID=function(Y,W){var X=typeof Y.getAttributeNode!=="undefined"&&Y.getAttributeNode("id");return Y.nodeType===1&&X&&X.nodeValue===W}}T.removeChild(U)})();(function(){var T=document.createElement("div");T.appendChild(document.createComment(""));if(T.getElementsByTagName("*").length>0){I.find.TAG=function(U,Y){var X=Y.getElementsByTagName(U[1]);if(U[1]==="*"){var W=[];for(var V=0;X[V];V++){if(X[V].nodeType===1){W.push(X[V])}}X=W}return X}}T.innerHTML="";if(T.firstChild&&typeof T.firstChild.getAttribute!=="undefined"&&T.firstChild.getAttribute("href")!=="#"){I.attrHandle.href=function(U){return U.getAttribute("href",2)}}})();if(document.querySelectorAll){(function(){var T=F,U=document.createElement("div");U.innerHTML="

";if(U.querySelectorAll&&U.querySelectorAll(".TEST").length===0){return}F=function(Y,X,V,W){X=X||document;if(!W&&X.nodeType===9&&!Q(X)){try{return E(X.querySelectorAll(Y),V)}catch(Z){}}return T(Y,X,V,W)};F.find=T.find;F.filter=T.filter;F.selectors=T.selectors;F.matches=T.matches})()}if(document.getElementsByClassName&&document.documentElement.getElementsByClassName){(function(){var T=document.createElement("div");T.innerHTML="
";if(T.getElementsByClassName("e").length===0){return}T.lastChild.className="e";if(T.getElementsByClassName("e").length===1){return}I.order.splice(1,0,"CLASS");I.find.CLASS=function(U,V,W){if(typeof V.getElementsByClassName!=="undefined"&&!W){return V.getElementsByClassName(U[1])}}})()}function P(U,Z,Y,ad,aa,ac){var ab=U=="previousSibling"&&!ac;for(var W=0,V=ad.length;W0){X=T;break}}}T=T[U]}ad[W]=X}}}var K=document.compareDocumentPosition?function(U,T){return U.compareDocumentPosition(T)&16}:function(U,T){return U!==T&&(U.contains?U.contains(T):true)};var Q=function(T){return T.nodeType===9&&T.documentElement.nodeName!=="HTML"||!!T.ownerDocument&&Q(T.ownerDocument)};var J=function(T,aa){var W=[],X="",Y,V=aa.nodeType?[aa]:aa;while((Y=I.match.PSEUDO.exec(T))){X+=Y[0];T=T.replace(I.match.PSEUDO,"")}T=I.relative[T]?T+"*":T;for(var Z=0,U=V.length;Z0||T.offsetHeight>0};F.selectors.filters.animated=function(T){return o.grep(o.timers,function(U){return T===U.elem}).length};o.multiFilter=function(V,T,U){if(U){V=":not("+V+")"}return F.matches(V,T)};o.dir=function(V,U){var T=[],W=V[U];while(W&&W!=document){if(W.nodeType==1){T.push(W)}W=W[U]}return T};o.nth=function(X,T,V,W){T=T||1;var U=0;for(;X;X=X[V]){if(X.nodeType==1&&++U==T){break}}return X};o.sibling=function(V,U){var T=[];for(;V;V=V.nextSibling){if(V.nodeType==1&&V!=U){T.push(V)}}return T};return;l.Sizzle=F})();o.event={add:function(I,F,H,K){if(I.nodeType==3||I.nodeType==8){return}if(I.setInterval&&I!=l){I=l}if(!H.guid){H.guid=this.guid++}if(K!==g){var G=H;H=this.proxy(G);H.data=K}var E=o.data(I,"events")||o.data(I,"events",{}),J=o.data(I,"handle")||o.data(I,"handle",function(){return typeof o!=="undefined"&&!o.event.triggered?o.event.handle.apply(arguments.callee.elem,arguments):g});J.elem=I;o.each(F.split(/\s+/),function(M,N){var O=N.split(".");N=O.shift();H.type=O.slice().sort().join(".");var L=E[N];if(o.event.specialAll[N]){o.event.specialAll[N].setup.call(I,K,O)}if(!L){L=E[N]={};if(!o.event.special[N]||o.event.special[N].setup.call(I,K,O)===false){if(I.addEventListener){I.addEventListener(N,J,false)}else{if(I.attachEvent){I.attachEvent("on"+N,J)}}}}L[H.guid]=H;o.event.global[N]=true});I=null},guid:1,global:{},remove:function(K,H,J){if(K.nodeType==3||K.nodeType==8){return}var G=o.data(K,"events"),F,E;if(G){if(H===g||(typeof H==="string"&&H.charAt(0)==".")){for(var I in G){this.remove(K,I+(H||""))}}else{if(H.type){J=H.handler;H=H.type}o.each(H.split(/\s+/),function(M,O){var Q=O.split(".");O=Q.shift();var N=RegExp("(^|\\.)"+Q.slice().sort().join(".*\\.")+"(\\.|$)");if(G[O]){if(J){delete G[O][J.guid]}else{for(var P in G[O]){if(N.test(G[O][P].type)){delete G[O][P]}}}if(o.event.specialAll[O]){o.event.specialAll[O].teardown.call(K,Q)}for(F in G[O]){break}if(!F){if(!o.event.special[O]||o.event.special[O].teardown.call(K,Q)===false){if(K.removeEventListener){K.removeEventListener(O,o.data(K,"handle"),false)}else{if(K.detachEvent){K.detachEvent("on"+O,o.data(K,"handle"))}}}F=null;delete G[O]}}})}for(F in G){break}if(!F){var L=o.data(K,"handle");if(L){L.elem=null}o.removeData(K,"events");o.removeData(K,"handle")}}},trigger:function(I,K,H,E){var G=I.type||I;if(!E){I=typeof I==="object"?I[h]?I:o.extend(o.Event(G),I):o.Event(G);if(G.indexOf("!")>=0){I.type=G=G.slice(0,-1);I.exclusive=true}if(!H){I.stopPropagation();if(this.global[G]){o.each(o.cache,function(){if(this.events&&this.events[G]){o.event.trigger(I,K,this.handle.elem)}})}}if(!H||H.nodeType==3||H.nodeType==8){return g}I.result=g;I.target=H;K=o.makeArray(K);K.unshift(I)}I.currentTarget=H;var J=o.data(H,"handle");if(J){J.apply(H,K)}if((!H[G]||(o.nodeName(H,"a")&&G=="click"))&&H["on"+G]&&H["on"+G].apply(H,K)===false){I.result=false}if(!E&&H[G]&&!I.isDefaultPrevented()&&!(o.nodeName(H,"a")&&G=="click")){this.triggered=true;try{H[G]()}catch(L){}}this.triggered=false;if(!I.isPropagationStopped()){var F=H.parentNode||H.ownerDocument;if(F){o.event.trigger(I,K,F,true)}}},handle:function(K){var J,E;K=arguments[0]=o.event.fix(K||l.event);K.currentTarget=this;var L=K.type.split(".");K.type=L.shift();J=!L.length&&!K.exclusive;var I=RegExp("(^|\\.)"+L.slice().sort().join(".*\\.")+"(\\.|$)");E=(o.data(this,"events")||{})[K.type];for(var G in E){var H=E[G];if(J||I.test(H.type)){K.handler=H;K.data=H.data;var F=H.apply(this,arguments);if(F!==g){K.result=F;if(F===false){K.preventDefault();K.stopPropagation()}}if(K.isImmediatePropagationStopped()){break}}}},props:"altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey newValue originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "),fix:function(H){if(H[h]){return H}var F=H;H=o.Event(F);for(var G=this.props.length,J;G;){J=this.props[--G];H[J]=F[J]}if(!H.target){H.target=H.srcElement||document}if(H.target.nodeType==3){H.target=H.target.parentNode}if(!H.relatedTarget&&H.fromElement){H.relatedTarget=H.fromElement==H.target?H.toElement:H.fromElement}if(H.pageX==null&&H.clientX!=null){var I=document.documentElement,E=document.body;H.pageX=H.clientX+(I&&I.scrollLeft||E&&E.scrollLeft||0)-(I.clientLeft||0);H.pageY=H.clientY+(I&&I.scrollTop||E&&E.scrollTop||0)-(I.clientTop||0)}if(!H.which&&((H.charCode||H.charCode===0)?H.charCode:H.keyCode)){H.which=H.charCode||H.keyCode}if(!H.metaKey&&H.ctrlKey){H.metaKey=H.ctrlKey}if(!H.which&&H.button){H.which=(H.button&1?1:(H.button&2?3:(H.button&4?2:0)))}return H},proxy:function(F,E){E=E||function(){return F.apply(this,arguments)};E.guid=F.guid=F.guid||E.guid||this.guid++;return E},special:{ready:{setup:B,teardown:function(){}}},specialAll:{live:{setup:function(E,F){o.event.add(this,F[0],c)},teardown:function(G){if(G.length){var E=0,F=RegExp("(^|\\.)"+G[0]+"(\\.|$)");o.each((o.data(this,"events").live||{}),function(){if(F.test(this.type)){E++}});if(E<1){o.event.remove(this,G[0],c)}}}}}};o.Event=function(E){if(!this.preventDefault){return new o.Event(E)}if(E&&E.type){this.originalEvent=E;this.type=E.type}else{this.type=E}this.timeStamp=e();this[h]=true};function k(){return false}function u(){return true}o.Event.prototype={preventDefault:function(){this.isDefaultPrevented=u;var E=this.originalEvent;if(!E){return}if(E.preventDefault){E.preventDefault()}E.returnValue=false},stopPropagation:function(){this.isPropagationStopped=u;var E=this.originalEvent;if(!E){return}if(E.stopPropagation){E.stopPropagation()}E.cancelBubble=true},stopImmediatePropagation:function(){this.isImmediatePropagationStopped=u;this.stopPropagation()},isDefaultPrevented:k,isPropagationStopped:k,isImmediatePropagationStopped:k};var a=function(F){var E=F.relatedTarget;while(E&&E!=this){try{E=E.parentNode}catch(G){E=this}}if(E!=this){F.type=F.data;o.event.handle.apply(this,arguments)}};o.each({mouseover:"mouseenter",mouseout:"mouseleave"},function(F,E){o.event.special[E]={setup:function(){o.event.add(this,F,a,E)},teardown:function(){o.event.remove(this,F,a)}}});o.fn.extend({bind:function(F,G,E){return F=="unload"?this.one(F,G,E):this.each(function(){o.event.add(this,F,E||G,E&&G)})},one:function(G,H,F){var E=o.event.proxy(F||H,function(I){o(this).unbind(I,E);return(F||H).apply(this,arguments)});return this.each(function(){o.event.add(this,G,E,F&&H)})},unbind:function(F,E){return this.each(function(){o.event.remove(this,F,E)})},trigger:function(E,F){return this.each(function(){o.event.trigger(E,F,this)})},triggerHandler:function(E,G){if(this[0]){var F=o.Event(E);F.preventDefault();F.stopPropagation();o.event.trigger(F,G,this[0]);return F.result}},toggle:function(G){var E=arguments,F=1;while(F=0){var E=G.slice(I,G.length);G=G.slice(0,I)}var H="GET";if(J){if(o.isFunction(J)){K=J;J=null}else{if(typeof J==="object"){J=o.param(J);H="POST"}}}var F=this;o.ajax({url:G,type:H,dataType:"html",data:J,complete:function(M,L){if(L=="success"||L=="notmodified"){F.html(E?o("
").append(M.responseText.replace(//g,"")).find(E):M.responseText)}if(K){F.each(K,[M.responseText,L,M])}}});return this},serialize:function(){return o.param(this.serializeArray())},serializeArray:function(){return this.map(function(){return this.elements?o.makeArray(this.elements):this}).filter(function(){return this.name&&!this.disabled&&(this.checked||/select|textarea/i.test(this.nodeName)||/text|hidden|password|search/i.test(this.type))}).map(function(E,F){var G=o(this).val();return G==null?null:o.isArray(G)?o.map(G,function(I,H){return{name:F.name,value:I}}):{name:F.name,value:G}}).get()}});o.each("ajaxStart,ajaxStop,ajaxComplete,ajaxError,ajaxSuccess,ajaxSend".split(","),function(E,F){o.fn[F]=function(G){return this.bind(F,G)}});var r=e();o.extend({get:function(E,G,H,F){if(o.isFunction(G)){H=G;G=null}return o.ajax({type:"GET",url:E,data:G,success:H,dataType:F})},getScript:function(E,F){return o.get(E,null,F,"script")},getJSON:function(E,F,G){return o.get(E,F,G,"json")},post:function(E,G,H,F){if(o.isFunction(G)){H=G;G={}}return o.ajax({type:"POST",url:E,data:G,success:H,dataType:F})},ajaxSetup:function(E){o.extend(o.ajaxSettings,E)},ajaxSettings:{url:location.href,global:true,type:"GET",contentType:"application/x-www-form-urlencoded",processData:true,async:true,xhr:function(){return l.ActiveXObject?new ActiveXObject("Microsoft.XMLHTTP"):new XMLHttpRequest()},accepts:{xml:"application/xml, text/xml",html:"text/html",script:"text/javascript, application/javascript",json:"application/json, text/javascript",text:"text/plain",_default:"*/*"}},lastModified:{},ajax:function(M){M=o.extend(true,M,o.extend(true,{},o.ajaxSettings,M));var W,F=/=\?(&|$)/g,R,V,G=M.type.toUpperCase();if(M.data&&M.processData&&typeof M.data!=="string"){M.data=o.param(M.data)}if(M.dataType=="jsonp"){if(G=="GET"){if(!M.url.match(F)){M.url+=(M.url.match(/\?/)?"&":"?")+(M.jsonp||"callback")+"=?"}}else{if(!M.data||!M.data.match(F)){M.data=(M.data?M.data+"&":"")+(M.jsonp||"callback")+"=?"}}M.dataType="json"}if(M.dataType=="json"&&(M.data&&M.data.match(F)||M.url.match(F))){W="jsonp"+r++;if(M.data){M.data=(M.data+"").replace(F,"="+W+"$1")}M.url=M.url.replace(F,"="+W+"$1");M.dataType="script";l[W]=function(X){V=X;I();L();l[W]=g;try{delete l[W]}catch(Y){}if(H){H.removeChild(T)}}}if(M.dataType=="script"&&M.cache==null){M.cache=false}if(M.cache===false&&G=="GET"){var E=e();var U=M.url.replace(/(\?|&)_=.*?(&|$)/,"$1_="+E+"$2");M.url=U+((U==M.url)?(M.url.match(/\?/)?"&":"?")+"_="+E:"")}if(M.data&&G=="GET"){M.url+=(M.url.match(/\?/)?"&":"?")+M.data;M.data=null}if(M.global&&!o.active++){o.event.trigger("ajaxStart")}var Q=/^(\w+:)?\/\/([^\/?#]+)/.exec(M.url);if(M.dataType=="script"&&G=="GET"&&Q&&(Q[1]&&Q[1]!=location.protocol||Q[2]!=location.host)){var H=document.getElementsByTagName("head")[0];var T=document.createElement("script");T.src=M.url;if(M.scriptCharset){T.charset=M.scriptCharset}if(!W){var O=false;T.onload=T.onreadystatechange=function(){if(!O&&(!this.readyState||this.readyState=="loaded"||this.readyState=="complete")){O=true;I();L();T.onload=T.onreadystatechange=null;H.removeChild(T)}}}H.appendChild(T);return g}var K=false;var J=M.xhr();if(M.username){J.open(G,M.url,M.async,M.username,M.password)}else{J.open(G,M.url,M.async)}try{if(M.data){J.setRequestHeader("Content-Type",M.contentType)}if(M.ifModified){J.setRequestHeader("If-Modified-Since",o.lastModified[M.url]||"Thu, 01 Jan 1970 00:00:00 GMT")}J.setRequestHeader("X-Requested-With","XMLHttpRequest");J.setRequestHeader("Accept",M.dataType&&M.accepts[M.dataType]?M.accepts[M.dataType]+", */*":M.accepts._default)}catch(S){}if(M.beforeSend&&M.beforeSend(J,M)===false){if(M.global&&!--o.active){o.event.trigger("ajaxStop")}J.abort();return false}if(M.global){o.event.trigger("ajaxSend",[J,M])}var N=function(X){if(J.readyState==0){if(P){clearInterval(P);P=null;if(M.global&&!--o.active){o.event.trigger("ajaxStop")}}}else{if(!K&&J&&(J.readyState==4||X=="timeout")){K=true;if(P){clearInterval(P);P=null}R=X=="timeout"?"timeout":!o.httpSuccess(J)?"error":M.ifModified&&o.httpNotModified(J,M.url)?"notmodified":"success";if(R=="success"){try{V=o.httpData(J,M.dataType,M)}catch(Z){R="parsererror"}}if(R=="success"){var Y;try{Y=J.getResponseHeader("Last-Modified")}catch(Z){}if(M.ifModified&&Y){o.lastModified[M.url]=Y}if(!W){I()}}else{o.handleError(M,J,R)}L();if(X){J.abort()}if(M.async){J=null}}}};if(M.async){var P=setInterval(N,13);if(M.timeout>0){setTimeout(function(){if(J&&!K){N("timeout")}},M.timeout)}}try{J.send(M.data)}catch(S){o.handleError(M,J,null,S)}if(!M.async){N()}function I(){if(M.success){M.success(V,R)}if(M.global){o.event.trigger("ajaxSuccess",[J,M])}}function L(){if(M.complete){M.complete(J,R)}if(M.global){o.event.trigger("ajaxComplete",[J,M])}if(M.global&&!--o.active){o.event.trigger("ajaxStop")}}return J},handleError:function(F,H,E,G){if(F.error){F.error(H,E,G)}if(F.global){o.event.trigger("ajaxError",[H,F,G])}},active:0,httpSuccess:function(F){try{return !F.status&&location.protocol=="file:"||(F.status>=200&&F.status<300)||F.status==304||F.status==1223}catch(E){}return false},httpNotModified:function(G,E){try{var H=G.getResponseHeader("Last-Modified");return G.status==304||H==o.lastModified[E]}catch(F){}return false},httpData:function(J,H,G){var F=J.getResponseHeader("content-type"),E=H=="xml"||!H&&F&&F.indexOf("xml")>=0,I=E?J.responseXML:J.responseText;if(E&&I.documentElement.tagName=="parsererror"){throw"parsererror"}if(G&&G.dataFilter){I=G.dataFilter(I,H)}if(typeof I==="string"){if(H=="script"){o.globalEval(I)}if(H=="json"){I=l["eval"]("("+I+")")}}return I},param:function(E){var G=[];function H(I,J){G[G.length]=encodeURIComponent(I)+"="+encodeURIComponent(J)}if(o.isArray(E)||E.jquery){o.each(E,function(){H(this.name,this.value)})}else{for(var F in E){if(o.isArray(E[F])){o.each(E[F],function(){H(F,this)})}else{H(F,o.isFunction(E[F])?E[F]():E[F])}}}return G.join("&").replace(/%20/g,"+")}});var m={},n,d=[["height","marginTop","marginBottom","paddingTop","paddingBottom"],["width","marginLeft","marginRight","paddingLeft","paddingRight"],["opacity"]];function t(F,E){var G={};o.each(d.concat.apply([],d.slice(0,E)),function(){G[this]=F});return G}o.fn.extend({show:function(J,L){if(J){return this.animate(t("show",3),J,L)}else{for(var H=0,F=this.length;H").appendTo("body");K=I.css("display");if(K==="none"){K="block"}I.remove();m[G]=K}o.data(this[H],"olddisplay",K)}}for(var H=0,F=this.length;H=0;H--){if(G[H].elem==this){if(E){G[H](true)}G.splice(H,1)}}});if(!E){this.dequeue()}return this}});o.each({slideDown:t("show",1),slideUp:t("hide",1),slideToggle:t("toggle",1),fadeIn:{opacity:"show"},fadeOut:{opacity:"hide"}},function(E,F){o.fn[E]=function(G,H){return this.animate(F,G,H)}});o.extend({speed:function(G,H,F){var E=typeof G==="object"?G:{complete:F||!F&&H||o.isFunction(G)&&G,duration:G,easing:F&&H||H&&!o.isFunction(H)&&H};E.duration=o.fx.off?0:typeof E.duration==="number"?E.duration:o.fx.speeds[E.duration]||o.fx.speeds._default;E.old=E.complete;E.complete=function(){if(E.queue!==false){o(this).dequeue()}if(o.isFunction(E.old)){E.old.call(this)}};return E},easing:{linear:function(G,H,E,F){return E+F*G},swing:function(G,H,E,F){return((-Math.cos(G*Math.PI)/2)+0.5)*F+E}},timers:[],fx:function(F,E,G){this.options=E;this.elem=F;this.prop=G;if(!E.orig){E.orig={}}}});o.fx.prototype={update:function(){if(this.options.step){this.options.step.call(this.elem,this.now,this)}(o.fx.step[this.prop]||o.fx.step._default)(this);if((this.prop=="height"||this.prop=="width")&&this.elem.style){this.elem.style.display="block"}},cur:function(F){if(this.elem[this.prop]!=null&&(!this.elem.style||this.elem.style[this.prop]==null)){return this.elem[this.prop]}var E=parseFloat(o.css(this.elem,this.prop,F));return E&&E>-10000?E:parseFloat(o.curCSS(this.elem,this.prop))||0},custom:function(I,H,G){this.startTime=e();this.start=I;this.end=H;this.unit=G||this.unit||"px";this.now=this.start;this.pos=this.state=0;var E=this;function F(J){return E.step(J)}F.elem=this.elem;if(F()&&o.timers.push(F)&&!n){n=setInterval(function(){var K=o.timers;for(var J=0;J=this.options.duration+this.startTime){this.now=this.end;this.pos=this.state=1;this.update();this.options.curAnim[this.prop]=true;var E=true;for(var F in this.options.curAnim){if(this.options.curAnim[F]!==true){E=false}}if(E){if(this.options.display!=null){this.elem.style.overflow=this.options.overflow;this.elem.style.display=this.options.display;if(o.css(this.elem,"display")=="none"){this.elem.style.display="block"}}if(this.options.hide){o(this.elem).hide()}if(this.options.hide||this.options.show){for(var I in this.options.curAnim){o.attr(this.elem.style,I,this.options.orig[I])}}this.options.complete.call(this.elem)}return false}else{var J=G-this.startTime;this.state=J/this.options.duration;this.pos=o.easing[this.options.easing||(o.easing.swing?"swing":"linear")](this.state,J,0,1,this.options.duration);this.now=this.start+((this.end-this.start)*this.pos);this.update()}return true}};o.extend(o.fx,{speeds:{slow:600,fast:200,_default:400},step:{opacity:function(E){o.attr(E.elem.style,"opacity",E.now)},_default:function(E){if(E.elem.style&&E.elem.style[E.prop]!=null){E.elem.style[E.prop]=E.now+E.unit}else{E.elem[E.prop]=E.now}}}});if(document.documentElement.getBoundingClientRect){o.fn.offset=function(){if(!this[0]){return{top:0,left:0}}if(this[0]===this[0].ownerDocument.body){return o.offset.bodyOffset(this[0])}var G=this[0].getBoundingClientRect(),J=this[0].ownerDocument,F=J.body,E=J.documentElement,L=E.clientTop||F.clientTop||0,K=E.clientLeft||F.clientLeft||0,I=G.top+(self.pageYOffset||o.boxModel&&E.scrollTop||F.scrollTop)-L,H=G.left+(self.pageXOffset||o.boxModel&&E.scrollLeft||F.scrollLeft)-K;return{top:I,left:H}}}else{o.fn.offset=function(){if(!this[0]){return{top:0,left:0}}if(this[0]===this[0].ownerDocument.body){return o.offset.bodyOffset(this[0])}o.offset.initialized||o.offset.initialize();var J=this[0],G=J.offsetParent,F=J,O=J.ownerDocument,M,H=O.documentElement,K=O.body,L=O.defaultView,E=L.getComputedStyle(J,null),N=J.offsetTop,I=J.offsetLeft;while((J=J.parentNode)&&J!==K&&J!==H){M=L.getComputedStyle(J,null);N-=J.scrollTop,I-=J.scrollLeft;if(J===G){N+=J.offsetTop,I+=J.offsetLeft;if(o.offset.doesNotAddBorder&&!(o.offset.doesAddBorderForTableAndCells&&/^t(able|d|h)$/i.test(J.tagName))){N+=parseInt(M.borderTopWidth,10)||0,I+=parseInt(M.borderLeftWidth,10)||0}F=G,G=J.offsetParent}if(o.offset.subtractsBorderForOverflowNotVisible&&M.overflow!=="visible"){N+=parseInt(M.borderTopWidth,10)||0,I+=parseInt(M.borderLeftWidth,10)||0}E=M}if(E.position==="relative"||E.position==="static"){N+=K.offsetTop,I+=K.offsetLeft}if(E.position==="fixed"){N+=Math.max(H.scrollTop,K.scrollTop),I+=Math.max(H.scrollLeft,K.scrollLeft)}return{top:N,left:I}}}o.offset={initialize:function(){if(this.initialized){return}var L=document.body,F=document.createElement("div"),H,G,N,I,M,E,J=L.style.marginTop,K='
';M={position:"absolute",top:0,left:0,margin:0,border:0,width:"1px",height:"1px",visibility:"hidden"};for(E in M){F.style[E]=M[E]}F.innerHTML=K;L.insertBefore(F,L.firstChild);H=F.firstChild,G=H.firstChild,I=H.nextSibling.firstChild.firstChild;this.doesNotAddBorder=(G.offsetTop!==5);this.doesAddBorderForTableAndCells=(I.offsetTop===5);H.style.overflow="hidden",H.style.position="relative";this.subtractsBorderForOverflowNotVisible=(G.offsetTop===-5);L.style.marginTop="1px";this.doesNotIncludeMarginInBodyOffset=(L.offsetTop===0);L.style.marginTop=J;L.removeChild(F);this.initialized=true},bodyOffset:function(E){o.offset.initialized||o.offset.initialize();var G=E.offsetTop,F=E.offsetLeft;if(o.offset.doesNotIncludeMarginInBodyOffset){G+=parseInt(o.curCSS(E,"marginTop",true),10)||0,F+=parseInt(o.curCSS(E,"marginLeft",true),10)||0}return{top:G,left:F}}};o.fn.extend({position:function(){var I=0,H=0,F;if(this[0]){var G=this.offsetParent(),J=this.offset(),E=/^body|html$/i.test(G[0].tagName)?{top:0,left:0}:G.offset();J.top-=j(this,"marginTop");J.left-=j(this,"marginLeft");E.top+=j(G,"borderTopWidth");E.left+=j(G,"borderLeftWidth");F={top:J.top-E.top,left:J.left-E.left}}return F},offsetParent:function(){var E=this[0].offsetParent||document.body;while(E&&(!/^body|html$/i.test(E.tagName)&&o.css(E,"position")=="static")){E=E.offsetParent}return o(E)}});o.each(["Left","Top"],function(F,E){var G="scroll"+E;o.fn[G]=function(H){if(!this[0]){return null}return H!==g?this.each(function(){this==l||this==document?l.scrollTo(!F?H:o(l).scrollLeft(),F?H:o(l).scrollTop()):this[G]=H}):this[0]==l||this[0]==document?self[F?"pageYOffset":"pageXOffset"]||o.boxModel&&document.documentElement[G]||document.body[G]:this[0][G]}});o.each(["Height","Width"],function(I,G){var E=I?"Left":"Top",H=I?"Right":"Bottom",F=G.toLowerCase();o.fn["inner"+G]=function(){return this[0]?o.css(this[0],F,false,"padding"):null};o.fn["outer"+G]=function(K){return this[0]?o.css(this[0],F,false,K?"margin":"border"):null};var J=G.toLowerCase();o.fn[J]=function(K){return this[0]==l?document.compatMode=="CSS1Compat"&&document.documentElement["client"+G]||document.body["client"+G]:this[0]==document?Math.max(document.documentElement["client"+G],document.body["scroll"+G],document.documentElement["scroll"+G],document.body["offset"+G],document.documentElement["offset"+G]):K===g?(this.length?o.css(this[0],J):null):this.css(J,typeof K==="string"?K:K+"px")}})})(); \ No newline at end of file diff --git a/services/console-proxy/server/libexec/console-proxy-runner.in b/services/console-proxy/server/libexec/console-proxy-runner.in new file mode 100755 index 00000000000..4f18aab36e5 --- /dev/null +++ b/services/console-proxy/server/libexec/console-proxy-runner.in @@ -0,0 +1,90 @@ +#!/usr/bin/env bash + +# 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. + + +#run.sh runs the agent client. + +cd `dirname "$0"` + +SYSTEMJARS="@SYSTEMJARS@" +SCP=$(build-classpath $SYSTEMJARS) ; if [ $? != 0 ] ; then SCP="@SYSTEMCLASSPATH@" ; fi +DCP="@DEPSCLASSPATH@" +ACP="@AGENTCLASSPATH@" +export CLASSPATH=$SCP:$DCP:$ACP:@CPSYSCONFDIR@ +for jarfile in "@PREMIUMJAVADIR@"/* ; do + if [ ! -e "$jarfile" ] ; then continue ; fi + CLASSPATH=$jarfile:$CLASSPATH +done +for plugin in "@PLUGINJAVADIR@"/* ; do + if [ ! -e "$plugin" ] ; then continue ; fi + CLASSPATH=$plugin:$CLASSPATH +done +export CLASSPATH + +set -e +cd "@CPLIBDIR@" +echo Current directory is "$PWD" +echo CLASSPATH to run the console proxy: "$CLASSPATH" + +export PATH=/sbin:/usr/sbin:"$PATH" +SERVICEARGS= +for x in private public ; do + configuration=`grep -q "^$x.network.device" "@CPSYSCONFDIR@"/agent.properties || true` + if [ -n "$CONFIGURATION" ] ; then + echo "Using manually-configured network device $CONFIGURATION" + else + defaultroute=`ip route | grep ^default | cut -d ' ' -f 5` + test -n "$defaultroute" + echo "Using auto-discovered network device $defaultroute which is the default route" + SERVICEARGS="$SERVICEARGS $x.network.device="$defaultroute + fi +done + +function termagent() { + if [ "$agentpid" != "" ] ; then + echo Killing VMOps Console Proxy "(PID $agentpid)" with SIGTERM >&2 + kill -TERM $agentpid + echo Waiting for agent to exit >&2 + wait $agentpid + ex=$? + echo Agent exited with return code $ex >&2 + else + echo Agent PID is unknown >&2 + fi +} + +trap termagent TERM +while true ; do + java -Xms128M -Xmx384M -cp "$CLASSPATH" "$@" com.cloud.agent.AgentShell $SERVICEARGS & + agentpid=$! + echo "Console Proxy started. PID: $!" >&2 + wait $agentpid + ex=$? + if [ $ex -gt 128 ]; then + echo "wait on console proxy process interrupted by SIGTERM" >&2 + exit $ex + fi + echo "Console proxy exited with return code $ex" >&2 + if [ $ex -eq 0 ] || [ $ex -eq 1 ] || [ $ex -eq 66 ] || [ $ex -gt 128 ]; then + echo "Exiting..." > /dev/stderr + exit $ex + fi + echo "Restarting console proxy..." > /dev/stderr + sleep 1 +done diff --git a/services/console-proxy/server/pom.xml b/services/console-proxy/server/pom.xml new file mode 100644 index 00000000000..58fd62a71a3 --- /dev/null +++ b/services/console-proxy/server/pom.xml @@ -0,0 +1,247 @@ + + + 4.0.0 + cloud-console-proxy + Apache CloudStack Console Proxy + + org.apache.cloudstack + cloud-service-console-proxy + 4.1.0-SNAPSHOT + ../pom.xml + + + mkisofs + + + + log4j + log4j + ${cs.log4j.version} + + + com.google.code.gson + gson + ${cs.gson.version} + + + commons-codec + commons-codec + ${cs.codec.version} + + + + org.apache.cloudstack + cloud-agent + ${project.version} + + + org.apache.cloudstack + cloud-patches + ${project.version} + pom + + + + install + src + + + certs + + realhostip.csr + + + + + + maven-assembly-plugin + 2.3 + + systemvm + false + + systemvm-descriptor.xml + + + + + make-systemvm + package + + single + + + + + + maven-resources-plugin + 2.6 + + + copy-resources + + package + + copy-resources + + + dist + + + target + + systemvm.zip + + + + ../patches/systemvm/debian/config/root/.ssh + + authorized_keys + + + + + + + + + maven-antrun-plugin + 1.7 + + + copy-cloud-scripts + package + + run + + + + + + + + + + + + + + + + genisoimage + + + /usr/bin/genisoimage + + + + genisoimage + + + + vmware + + + nonoss + + + + + org.apache.cloudstack + cloud-plugin-hypervisor-vmware + ${project.version} + + + org.apache.cloudstack + cloud-vmware-base + ${project.version} + + + + + systemvm + + + systemvm + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + package + + exec + + + + + ${mkisofs} + dist + + -quiet + -r + -o + systemvm.iso + systemvm.zip + cloud-scripts.tgz + authorized_keys + + + + + + + + + diff --git a/services/console-proxy/server/scripts/_run.sh b/services/console-proxy/server/scripts/_run.sh new file mode 100755 index 00000000000..e408378afbc --- /dev/null +++ b/services/console-proxy/server/scripts/_run.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# 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. + + + + + +#run.sh runs the console proxy. + +# make sure we delete the old files from the original template +rm console-proxy.jar +rm console-common.jar +rm conf/cloud.properties + +set -x + +CP=./:./conf +for file in *.jar +do + CP=${CP}:$file +done +keyvalues= + +CMDLINE=$(cat /var/cache/cloud/cmdline) + +#CMDLINE="graphical utf8 eth0ip=0.0.0.0 eth0mask=255.255.255.0 eth1ip=192.168.140.40 eth1mask=255.255.255.0 eth2ip=172.24.0.50 eth2mask=255.255.0.0 gateway=172.24.0.1 dns1=72.52.126.11 template=domP dns2=72.52.126.12 host=192.168.1.142 port=8250 mgmtcidr=192.168.1.0/24 localgw=192.168.140.1 zone=5 pod=5" +for i in $CMDLINE + do + KEY=$(echo $i | cut -s -d= -f1) + VALUE=$(echo $i | cut -s -d= -f2) + [ "$KEY" == "" ] && continue + case $KEY in + *) + keyvalues="${keyvalues} $KEY=$VALUE" + esac + done + +tot_mem_k=$(cat /proc/meminfo | grep MemTotal | awk '{print $2}') +let "tot_mem_m=tot_mem_k>>10" +let "eightypcnt=$tot_mem_m*8/10" +let "maxmem=$tot_mem_m-80" + +if [ $maxmem -gt $eightypcnt ] +then + maxmem=$eightypcnt +fi + +java -Djavax.net.ssl.trustStore=./certs/realhostip.keystore -mx${maxmem}m -cp $CP com.cloud.agent.AgentShell $keyvalues $@ diff --git a/services/console-proxy/server/scripts/config_auth.sh b/services/console-proxy/server/scripts/config_auth.sh new file mode 100755 index 00000000000..4b74f8eb995 --- /dev/null +++ b/services/console-proxy/server/scripts/config_auth.sh @@ -0,0 +1,69 @@ +#!/usr/bin/env bash +# 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. + + + + + + +BASE_DIR="/var/www/html/copy/template/" +HTACCESS="$BASE_DIR/.htaccess" + +PASSWDFILE="/etc/httpd/.htpasswd" +if [ -d /etc/apache2 ] +then + PASSWDFILE="/etc/apache2/.htpasswd" +fi + +config_htaccess() { + mkdir -p $BASE_DIR + result=$? + echo "Options -Indexes" > $HTACCESS + let "result=$result+$?" + echo "AuthType Basic" >> $HTACCESS + let "result=$result+$?" + echo "AuthName \"Authentication Required\"" >> $HTACCESS + let "result=$result+$?" + echo "AuthUserFile \"$PASSWDFILE\"" >> $HTACCESS + let "result=$result+$?" + echo "Require valid-user" >> $HTACCESS + let "result=$result+$?" + return $result +} + +write_passwd() { + local user=$1 + local passwd=$2 + htpasswd -bc $PASSWDFILE $user $passwd + return $? +} + +if [ $# -ne 2 ] ; then + echo $"Usage: `basename $0` username password " + exit 0 +fi + +write_passwd $1 $2 +if [ $? -ne 0 ] +then + echo "Failed to update password" + exit 2 +fi + +config_htaccess +exit $? diff --git a/services/console-proxy/server/scripts/config_ssl.sh b/services/console-proxy/server/scripts/config_ssl.sh new file mode 100755 index 00000000000..8d80c4731ad --- /dev/null +++ b/services/console-proxy/server/scripts/config_ssl.sh @@ -0,0 +1,174 @@ +#!/usr/bin/env bash +# 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. + + + + +help() { + printf " -c use customized key/cert\n" + printf " -k path of private key\n" + printf " -p path of certificate of public key\n" + printf " -t path of certificate chain\n" +} + + +config_httpd_conf() { + local ip=$1 + local srvr=$2 + cp -f /etc/httpd/conf/httpd.conf.orig /etc/httpd/conf/httpd.conf + sed -i -e "s/Listen.*:80$/Listen $ip:80/" /etc/httpd/conf/httpd.conf + echo " " >> /etc/httpd/conf/httpd.conf + echo " DocumentRoot /var/www/html/" >> /etc/httpd/conf/httpd.conf + echo " ServerName $srvr" >> /etc/httpd/conf/httpd.conf + echo " SSLEngine on" >> /etc/httpd/conf/httpd.conf + echo " SSLCertificateFile /etc/httpd/ssl/certs/realhostip.crt" >> /etc/httpd/conf/httpd.conf + echo " SSLCertificateKeyFile /etc/httpd/ssl/keys/realhostip.key" >> /etc/httpd/conf/httpd.conf + echo "" >> /etc/httpd/conf/httpd.conf +} + +config_apache2_conf() { + local ip=$1 + local srvr=$2 + cp -f /etc/apache2/sites-available/default.orig /etc/apache2/sites-available/default + cp -f /etc/apache2/sites-available/default-ssl.orig /etc/apache2/sites-available/default-ssl + sed -i -e "s///" /etc/apache2/sites-available/default + sed -i -e "s///" /etc/apache2/sites-available/default-ssl + sed -i -e "s/Listen .*:80/Listen $ip:80/g" /etc/apache2/ports.conf + sed -i -e "s/Listen .*:443/Listen $ip:443/g" /etc/apache2/ports.conf + sed -i -e "s/NameVirtualHost .*:80/NameVirtualHost $ip:80/g" /etc/apache2/ports.conf + sed -i 's/ssl-cert-snakeoil.key/cert_apache.key/' /etc/apache2/sites-available/default-ssl + sed -i 's/ssl-cert-snakeoil.pem/cert_apache.crt/' /etc/apache2/sites-available/default-ssl +} + +copy_certs() { + local certdir=$(dirname $0)/certs + local mydir=$(dirname $0) + if [ -d $certdir ] && [ -f $customPrivKey ] && [ -f $customPrivCert ] ; then + mkdir -p /etc/httpd/ssl/keys && mkdir -p /etc/httpd/ssl/certs && cp $customprivKey /etc/httpd/ssl/keys && cp $customPrivCert /etc/httpd/ssl/certs + return $? + fi + if [ ! -z customCertChain ] && [ -f $customCertChain ] ; then + cp $customCertChain /etc/httpd/ssl/certs + fi + return 1 +} + +copy_certs_apache2() { + local certdir=$(dirname $0)/certs + local mydir=$(dirname $0) + if [ -f $customPrivKey ] && [ -f $customPrivCert ] ; then + cp $customPrivKey /etc/ssl/private/cert_apache.key && cp $customPrivCert /etc/ssl/certs/cert_apache.crt + fi + if [ ! -z "$customCertChain" ] && [ -f "$customCertChain" ] ; then + cp $customCertChain /etc/ssl/certs/cert_apache_chain.crt + fi + return 0 +} + + +cflag= +cpkflag= +cpcflag= +cccflag= +customPrivKey=$(dirname $0)/certs/realhostip.key +customPrivCert=$(dirname $0)/certs/realhostip.crt +customCertChain= +publicIp= +hostName= +while getopts 'i:h:k:p:t:c' OPTION +do + case $OPTION in + c) cflag=1 + ;; + k) cpkflag=1 + customPrivKey="$OPTARG" + ;; + p) cpcflag=1 + customPrivCert="$OPTARG" + ;; + t) cccflag=1 + customCertChain="$OPTARG" + ;; + i) publicIp="$OPTARG" + ;; + h) hostName="$OPTARG" + ;; + ?) help + ;; + esac +done + + +if [ -z "$publicIp" ] || [ -z "$hostName" ] +then + help + exit 1 +fi + +if [ "$cflag" == "1" ] +then + if [ "$cpkflag$cpcflag" != "11" ] + then + help + exit 1 + fi + if [ ! -f "$customPrivKey" ] + then + printf "priviate key file is not exist\n" + exit 2 + fi + + if [ ! -f "$customPrivCert" ] + then + printf "public certificate is not exist\n" + exit 3 + fi + + if [ "$cccflag" == "1" ] + then + if [ ! -f "$customCertChain" ] + then + printf "certificate chain is not exist\n" + exit 4 + fi + fi +fi + +if [ -d /etc/apache2 ] +then + copy_certs_apache2 +else + copy_certs +fi + +if [ $? -ne 0 ] +then + echo "Failed to copy certificates" + exit 2 +fi + +if [ -d /etc/apache2 ] +then + config_apache2_conf $publicIp $hostName + /etc/init.d/apache2 stop + /etc/init.d/apache2 start +else + config_httpd_conf $publicIp $hostName +fi + + diff --git a/services/console-proxy/server/scripts/ipfirewall.sh b/services/console-proxy/server/scripts/ipfirewall.sh new file mode 100755 index 00000000000..4711b8ac6db --- /dev/null +++ b/services/console-proxy/server/scripts/ipfirewall.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash +# 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. + +BASE_DIR="/var/www/html/copy/" +HTACCESS="$BASE_DIR/.htaccess" + +config_htaccess() { + mkdir -p $BASE_DIR + result=$? + echo "Options -Indexes" > $HTACCESS + let "result=$result+$?" + echo "order deny,allow" >> $HTACCESS + let "result=$result+$?" + echo "deny from all" >> $HTACCESS + let "result=$result+$?" + return $result +} + +ips(){ + echo "allow from $1" >> $HTACCESS + result=$? + return $result +} + +is_append="$1" +shift +if [ $is_append != "true" ]; then + config_htaccess +fi +for i in $@ +do + ips "$i" +done +exit $? + diff --git a/services/console-proxy/server/scripts/run-proxy.sh b/services/console-proxy/server/scripts/run-proxy.sh new file mode 100644 index 00000000000..d6ccf7c0091 --- /dev/null +++ b/services/console-proxy/server/scripts/run-proxy.sh @@ -0,0 +1,48 @@ +#!/usr/bin/env bash +# 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. + + + + + +#run.sh runs the console proxy. + +# make sure we delete the old files from the original template +rm console-proxy.jar +rm console-common.jar +rm conf/cloud.properties + +CP=./:./conf +for file in *.jar +do + CP=${CP}:$file +done + +#CMDLINE=$(cat /proc/cmdline) +#for i in $CMDLINE +# do +# KEY=$(echo $i | cut -d= -f1) +# VALUE=$(echo $i | cut -d= -f2) +# case $KEY in +# mgmt_host) +# MGMT_HOST=$VALUE +# ;; +# esac +# done + +java -mx700m -cp $CP:./conf com.cloud.consoleproxy.ConsoleProxy $@ diff --git a/services/console-proxy/server/scripts/run.bat b/services/console-proxy/server/scripts/run.bat new file mode 100644 index 00000000000..ce6dc404574 --- /dev/null +++ b/services/console-proxy/server/scripts/run.bat @@ -0,0 +1,18 @@ +rem Licensed to the Apache Software Foundation (ASF) under one +rem or more contributor license agreements. See the NOTICE file +rem distributed with this work for additional information +rem regarding copyright ownership. The ASF licenses this file +rem to you under the Apache License, Version 2.0 (the +rem "License"); you may not use this file except in compliance +rem with the License. You may obtain a copy of the License at +rem +rem http://www.apache.org/licenses/LICENSE-2.0 +rem +rem Unless required by applicable law or agreed to in writing, +rem software distributed under the License is distributed on an +rem "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +rem KIND, either express or implied. See the License for the +rem specific language governing permissions and limitations +rem under the License. + +java -mx700m -cp cloud-console-proxy.jar;;cloud-console-common.jar;log4j-1.2.15.jar;apache-log4j-extras-1.0.jar;gson-1.3.jar;commons-logging-1.1.1.jar;.;.\conf; com.cloud.consoleproxy.ConsoleProxy %* diff --git a/services/console-proxy/server/scripts/run.sh b/services/console-proxy/server/scripts/run.sh new file mode 100755 index 00000000000..146d96f0287 --- /dev/null +++ b/services/console-proxy/server/scripts/run.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# 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. + + + + + +#_run.sh runs the agent client. + +# set -x + +while true +do + ./_run.sh "$@" & + wait + ex=$? + if [ $ex -eq 0 ] || [ $ex -eq 1 ] || [ $ex -eq 66 ] || [ $ex -gt 128 ]; then + # permanent errors + sleep 5 + fi + + # user stop agent by service cloud stop + grep 'stop' /usr/local/cloud/systemvm/user_request &>/dev/null + if [ $? -eq 0 ]; then + timestamp=$(date) + echo "$timestamp User stops cloud.com service" >> /var/log/cloud.log + exit 0 + fi + sleep 5 +done diff --git a/services/console-proxy/server/scripts/ssvm-check.sh b/services/console-proxy/server/scripts/ssvm-check.sh new file mode 100644 index 00000000000..a4011647f07 --- /dev/null +++ b/services/console-proxy/server/scripts/ssvm-check.sh @@ -0,0 +1,136 @@ +#!/usr/bin/env bash +# 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. + + +# Health check script for the Secondary Storage VM + +# DNS server is specified. + + +CMDLINE=/var/cache/cloud/cmdline +for i in `cat $CMDLINE` +do + key=`echo $i | cut -d= -f1` + value=`echo $i | cut -d= -f2` + case $key in + host) + MGMTSERVER=$value + ;; + esac +done + + +# ping dns server +echo ================================================ +DNSSERVER=`egrep '^nameserver' /etc/resolv.conf | awk '{print $2}'| head -1` +echo "First DNS server is " $DNSSERVER +ping -c 2 $DNSSERVER +if [ $? -eq 0 ] +then + echo "Good: Can ping DNS server" +else + echo "WARNING: cannot ping DNS server" + echo "route follows" + route -n +fi + + +# check dns resolve +echo ================================================ +nslookup download.cloud.com 1> /tmp/dns 2>&1 +grep 'no servers could' /tmp/dns 1> /dev/null 2>&1 +if [ $? -eq 0 ] +then + echo "ERROR: DNS not resolving download.cloud.com" + echo resolv.conf follows + cat /etc/resolv.conf + exit 2 +else + echo "Good: DNS resolves download.cloud.com" +fi + + +# check to see if we have the NFS volume mounted +echo ================================================ +mount|grep -v sunrpc|grep nfs 1> /dev/null 2>&1 +if [ $? -eq 0 ] +then + echo "NFS is currently mounted" + # check for write access + for MOUNTPT in `mount|grep -v sunrpc|grep nfs| awk '{print $3}'` + do + if [ $MOUNTPT != "/proc/xen" ] # mounted by xen + then + echo Mount point is $MOUNTPT + touch $MOUNTPT/foo + if [ $? -eq 0 ] + then + echo "Good: Can write to mount point" + rm $MOUNTPT/foo + else + echo "ERROR: Cannot write to mount point" + echo "You need to export with norootsquash" + fi + fi + done +else + echo "ERROR: NFS is not currently mounted" + echo "Try manually mounting from inside the VM" + NFSSERVER=`awk '{print $17}' $CMDLINE|awk -F= '{print $2}'|awk -F: '{print $1}'` + echo "NFS server is " $NFSSERVER + ping -c 2 $NFSSERVER + if [ $? -eq 0 ] + then + echo "Good: Can ping NFS server" + else + echo "WARNING: cannot ping NFS server" + echo routing table follows + route -n + fi +fi + + +# check for connectivity to the management server +echo ================================================ +echo Management server is $MGMTSERVER. Checking connectivity. +socatout=$(echo | socat - TCP:$MGMTSERVER:8250,connect-timeout=3 2>&1) +if [ $? -eq 0 ] +then + echo "Good: Can connect to management server port 8250" +else + echo "ERROR: Cannot connect to $MGMTSERVER port 8250" + echo $socatout + exit 4 +fi + + +# check for the java process running +echo ================================================ +ps -eaf|grep -v grep|grep java 1> /dev/null 2>&1 +if [ $? -eq 0 ] +then + echo "Good: Java process is running" +else + echo "ERROR: Java process not running. Try restarting the SSVM." + exit 3 +fi + +echo ================================================ +echo Tests Complete. Look for ERROR or WARNING above. + +exit 0 diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/AjaxFIFOImageCache.java b/services/console-proxy/server/src/com/cloud/consoleproxy/AjaxFIFOImageCache.java new file mode 100644 index 00000000000..cff00b3317d --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/AjaxFIFOImageCache.java @@ -0,0 +1,83 @@ +// 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 java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.cloud.consoleproxy.util.Logger; + +public class AjaxFIFOImageCache { + private static final Logger s_logger = Logger.getLogger(AjaxFIFOImageCache.class); + + private List fifoQueue; + private Map cache; + private int cacheSize; + private int nextKey = 0; + + public AjaxFIFOImageCache(int cacheSize) { + this.cacheSize = cacheSize; + fifoQueue = new ArrayList(); + cache = new HashMap(); + } + + public synchronized void clear() { + fifoQueue.clear(); + cache.clear(); + } + + public synchronized int putImage(byte[] image) { + while(cache.size() >= cacheSize) { + Integer keyToRemove = fifoQueue.remove(0); + cache.remove(keyToRemove); + + if(s_logger.isTraceEnabled()) + s_logger.trace("Remove image from cache, key: " + keyToRemove); + } + + int key = getNextKey(); + + if(s_logger.isTraceEnabled()) + s_logger.trace("Add image to cache, key: " + key); + + cache.put(key, image); + fifoQueue.add(key); + return key; + } + + public synchronized byte[] getImage(int key) { + if (key == 0) { + key = nextKey; + } + if (cache.containsKey(key)) { + if (s_logger.isTraceEnabled()) + s_logger.trace("Retrieve image from cache, key: " + key); + + return cache.get(key); + } + + if (s_logger.isTraceEnabled()) + s_logger.trace("Image is no long in cache, key: " + key); + return null; + } + + public synchronized int getNextKey() { + return ++nextKey; + } +} \ No newline at end of file diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/AuthenticationException.java b/services/console-proxy/server/src/com/cloud/consoleproxy/AuthenticationException.java new file mode 100644 index 00000000000..3fa266792ae --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/AuthenticationException.java @@ -0,0 +1,33 @@ +// 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; + +public class AuthenticationException extends Exception { + private static final long serialVersionUID = -393139302884898842L; + public AuthenticationException() { + super(); + } + public AuthenticationException(String s) { + super(s); + } + public AuthenticationException(String message, Throwable cause) { + super(message, cause); + } + public AuthenticationException(Throwable cause) { + super(cause); + } +} \ No newline at end of file diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxy.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxy.java new file mode 100644 index 00000000000..a722d8305a2 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxy.java @@ -0,0 +1,499 @@ +// 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 java.io.File; +import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.InetSocketAddress; +import java.net.URISyntaxException; +import java.net.URL; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.util.Hashtable; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.Executor; + +import org.apache.commons.codec.binary.Base64; +import org.apache.log4j.xml.DOMConfigurator; + +import com.cloud.consoleproxy.util.Logger; +import com.google.gson.Gson; +import com.sun.net.httpserver.HttpServer; + +/** + * + * ConsoleProxy, singleton class that manages overall activities in console proxy process. To make legacy code work, we still + */ +public class ConsoleProxy { + private static final Logger s_logger = Logger.getLogger(ConsoleProxy.class); + + public static final int KEYBOARD_RAW = 0; + public static final int KEYBOARD_COOKED = 1; + + public static int VIEWER_LINGER_SECONDS = 180; + + public static Object context; + + // this has become more ugly, to store keystore info passed from management server (we now use management server managed keystore to support + // dynamically changing to customer supplied certificate) + public static byte[] ksBits; + public static String ksPassword; + + public static Method authMethod; + public static Method reportMethod; + public static Method ensureRouteMethod; + + static Hashtable connectionMap = new Hashtable(); + static int httpListenPort = 80; + static int httpCmdListenPort = 8001; + static int reconnectMaxRetry = 5; + static int readTimeoutSeconds = 90; + static int keyboardType = KEYBOARD_RAW; + static String factoryClzName; + static boolean standaloneStart = false; + + static String encryptorPassword = genDefaultEncryptorPassword(); + + private static String genDefaultEncryptorPassword() { + try { + SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); + + byte[] randomBytes = new byte[16]; + random.nextBytes(randomBytes); + return Base64.encodeBase64String(randomBytes); + } catch (NoSuchAlgorithmException e) { + s_logger.error("Unexpected exception ", e); + assert(false); + } + + return "Dummy"; + } + + private static void configLog4j() { + URL configUrl = System.class.getResource("/conf/log4j-cloud.xml"); + if(configUrl == null) + configUrl = ClassLoader.getSystemResource("log4j-cloud.xml"); + + if(configUrl == null) + configUrl = ClassLoader.getSystemResource("conf/log4j-cloud.xml"); + + if(configUrl != null) { + try { + System.out.println("Configure log4j using " + configUrl.toURI().toString()); + } catch (URISyntaxException e1) { + e1.printStackTrace(); + } + + try { + File file = new File(configUrl.toURI()); + + System.out.println("Log4j configuration from : " + file.getAbsolutePath()); + DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000); + } catch (URISyntaxException e) { + System.out.println("Unable to convert log4j configuration Url to URI"); + } + // DOMConfigurator.configure(configUrl); + } else { + System.out.println("Configure log4j with default properties"); + } + } + + private static void configProxy(Properties conf) { + s_logger.info("Configure console proxy..."); + for(Object key : conf.keySet()) { + s_logger.info("Property " + (String)key + ": " + conf.getProperty((String)key)); + } + + String s = conf.getProperty("consoleproxy.httpListenPort"); + if (s!=null) { + httpListenPort = Integer.parseInt(s); + s_logger.info("Setting httpListenPort=" + s); + } + + s = conf.getProperty("premium"); + if(s != null && s.equalsIgnoreCase("true")) { + s_logger.info("Premium setting will override settings from consoleproxy.properties, listen at port 443"); + httpListenPort = 443; + factoryClzName = "com.cloud.consoleproxy.ConsoleProxySecureServerFactoryImpl"; + } else { + factoryClzName = ConsoleProxyBaseServerFactoryImpl.class.getName(); + } + + s = conf.getProperty("consoleproxy.httpCmdListenPort"); + if (s!=null) { + httpCmdListenPort = Integer.parseInt(s); + s_logger.info("Setting httpCmdListenPort=" + s); + } + + s = conf.getProperty("consoleproxy.reconnectMaxRetry"); + if (s!=null) { + reconnectMaxRetry = Integer.parseInt(s); + s_logger.info("Setting reconnectMaxRetry=" + reconnectMaxRetry); + } + + s = conf.getProperty("consoleproxy.readTimeoutSeconds"); + if (s!=null) { + readTimeoutSeconds = Integer.parseInt(s); + s_logger.info("Setting readTimeoutSeconds=" + readTimeoutSeconds); + } + } + + public static ConsoleProxyServerFactory getHttpServerFactory() { + try { + Class clz = Class.forName(factoryClzName); + try { + ConsoleProxyServerFactory factory = (ConsoleProxyServerFactory)clz.newInstance(); + factory.init(ConsoleProxy.ksBits, ConsoleProxy.ksPassword); + return factory; + } catch (InstantiationException e) { + s_logger.error(e.getMessage(), e); + return null; + } catch (IllegalAccessException e) { + s_logger.error(e.getMessage(), e); + return null; + } + } catch (ClassNotFoundException e) { + s_logger.warn("Unable to find http server factory class: " + factoryClzName); + return new ConsoleProxyBaseServerFactoryImpl(); + } + } + + public static ConsoleProxyAuthenticationResult authenticateConsoleAccess(ConsoleProxyClientParam param, boolean reauthentication) { + + ConsoleProxyAuthenticationResult authResult = new ConsoleProxyAuthenticationResult(); + authResult.setSuccess(true); + authResult.setReauthentication(reauthentication); + authResult.setHost(param.getClientHostAddress()); + authResult.setPort(param.getClientHostPort()); + + if(standaloneStart) { + return authResult; + } + + if(authMethod != null) { + Object result; + try { + result = authMethod.invoke(ConsoleProxy.context, + param.getClientHostAddress(), + String.valueOf(param.getClientHostPort()), + param.getClientTag(), + param.getClientHostPassword(), + param.getTicket(), + new Boolean(reauthentication)); + } catch (IllegalAccessException e) { + s_logger.error("Unable to invoke authenticateConsoleAccess due to IllegalAccessException" + " for vm: " + param.getClientTag(), e); + authResult.setSuccess(false); + return authResult; + } catch (InvocationTargetException e) { + s_logger.error("Unable to invoke authenticateConsoleAccess due to InvocationTargetException " + " for vm: " + param.getClientTag(), e); + authResult.setSuccess(false); + return authResult; + } + + if(result != null && result instanceof String) { + authResult = new Gson().fromJson((String)result, ConsoleProxyAuthenticationResult.class); + } else { + s_logger.error("Invalid authentication return object " + result + " for vm: " + param.getClientTag() + ", decline the access"); + authResult.setSuccess(false); + } + } else { + s_logger.warn("Private channel towards management server is not setup. Switch to offline mode and allow access to vm: " + param.getClientTag()); + } + + return authResult; + } + + public static void reportLoadInfo(String gsonLoadInfo) { + if(reportMethod != null) { + try { + reportMethod.invoke(ConsoleProxy.context, gsonLoadInfo); + } catch (IllegalAccessException e) { + s_logger.error("Unable to invoke reportLoadInfo due to " + e.getMessage()); + } catch (InvocationTargetException e) { + s_logger.error("Unable to invoke reportLoadInfo due to " + e.getMessage()); + } + } else { + s_logger.warn("Private channel towards management server is not setup. Switch to offline mode and ignore load report"); + } + } + + public static void ensureRoute(String address) { + if(ensureRouteMethod != null) { + try { + ensureRouteMethod.invoke(ConsoleProxy.context, address); + } catch (IllegalAccessException e) { + s_logger.error("Unable to invoke ensureRoute due to " + e.getMessage()); + } catch (InvocationTargetException e) { + s_logger.error("Unable to invoke ensureRoute due to " + e.getMessage()); + } + } else { + s_logger.warn("Unable to find ensureRoute method, console proxy agent is not up to date"); + } + } + + public static void startWithContext(Properties conf, Object context, byte[] ksBits, String ksPassword) { + s_logger.info("Start console proxy with context"); + if(conf != null) { + for(Object key : conf.keySet()) { + s_logger.info("Context property " + (String)key + ": " + conf.getProperty((String)key)); + } + } + + configLog4j(); + Logger.setFactory(new ConsoleProxyLoggerFactory()); + + // Using reflection to setup private/secure communication channel towards management server + ConsoleProxy.context = context; + ConsoleProxy.ksBits = ksBits; + ConsoleProxy.ksPassword = ksPassword; + try { + Class contextClazz = Class.forName("com.cloud.agent.resource.consoleproxy.ConsoleProxyResource"); + authMethod = contextClazz.getDeclaredMethod("authenticateConsoleAccess", String.class, String.class, String.class, String.class, String.class, Boolean.class); + reportMethod = contextClazz.getDeclaredMethod("reportLoadInfo", String.class); + ensureRouteMethod = contextClazz.getDeclaredMethod("ensureRoute", String.class); + } catch (SecurityException e) { + s_logger.error("Unable to setup private channel due to SecurityException", e); + } catch (NoSuchMethodException e) { + s_logger.error("Unable to setup private channel due to NoSuchMethodException", e); + } catch (IllegalArgumentException e) { + s_logger.error("Unable to setup private channel due to IllegalArgumentException", e); + } catch(ClassNotFoundException e) { + s_logger.error("Unable to setup private channel due to ClassNotFoundException", e); + } + + // merge properties from conf file + InputStream confs = ConsoleProxy.class.getResourceAsStream("/conf/consoleproxy.properties"); + Properties props = new Properties(); + if (confs == null) { + s_logger.info("Can't load consoleproxy.properties from classpath, will use default configuration"); + } else { + try { + props.load(confs); + + for(Object key : props.keySet()) { + // give properties passed via context high priority, treat properties from consoleproxy.properties + // as default values + if(conf.get(key) == null) + conf.put(key, props.get(key)); + } + } catch (Exception e) { + s_logger.error(e.toString(), e); + } + } + + start(conf); + } + + public static void start(Properties conf) { + System.setProperty("java.awt.headless", "true"); + + configProxy(conf); + + ConsoleProxyServerFactory factory = getHttpServerFactory(); + if(factory == null) { + s_logger.error("Unable to load console proxy server factory"); + System.exit(1); + } + + if(httpListenPort != 0) { + startupHttpMain(); + } else { + s_logger.error("A valid HTTP server port is required to be specified, please check your consoleproxy.httpListenPort settings"); + System.exit(1); + } + + if(httpCmdListenPort > 0) { + startupHttpCmdPort(); + } else { + s_logger.info("HTTP command port is disabled"); + } + + ConsoleProxyGCThread cthread = new ConsoleProxyGCThread(connectionMap); + cthread.setName("Console Proxy GC Thread"); + cthread.start(); + } + + private static void startupHttpMain() { + try { + ConsoleProxyServerFactory factory = getHttpServerFactory(); + if(factory == null) { + s_logger.error("Unable to load HTTP server factory"); + System.exit(1); + } + + HttpServer server = factory.createHttpServerInstance(httpListenPort); + server.createContext("/getscreen", new ConsoleProxyThumbnailHandler()); + server.createContext("/resource/", new ConsoleProxyResourceHandler()); + server.createContext("/ajax", new ConsoleProxyAjaxHandler()); + server.createContext("/ajaximg", new ConsoleProxyAjaxImageHandler()); + server.setExecutor(new ThreadExecutor()); // creates a default executor + server.start(); + } catch(Exception e) { + s_logger.error(e.getMessage(), e); + System.exit(1); + } + } + + private static void startupHttpCmdPort() { + try { + s_logger.info("Listening for HTTP CMDs on port " + httpCmdListenPort); + HttpServer cmdServer = HttpServer.create(new InetSocketAddress(httpCmdListenPort), 2); + cmdServer.createContext("/cmd", new ConsoleProxyCmdHandler()); + cmdServer.setExecutor(new ThreadExecutor()); // creates a default executor + cmdServer.start(); + } catch(Exception e) { + s_logger.error(e.getMessage(), e); + System.exit(1); + } + } + + public static void main(String[] argv) { + standaloneStart = true; + configLog4j(); + Logger.setFactory(new ConsoleProxyLoggerFactory()); + + InputStream confs = ConsoleProxy.class.getResourceAsStream("/conf/consoleproxy.properties"); + Properties conf = new Properties(); + if (confs == null) { + s_logger.info("Can't load consoleproxy.properties from classpath, will use default configuration"); + } else { + try { + conf.load(confs); + } catch (Exception e) { + s_logger.error(e.toString(), e); + } + } + start(conf); + } + + public static ConsoleProxyClient getVncViewer(ConsoleProxyClientParam param) throws Exception { + ConsoleProxyClient viewer = null; + + boolean reportLoadChange = false; + String clientKey = param.getClientMapKey(); + synchronized (connectionMap) { + viewer = connectionMap.get(clientKey); + if (viewer == null) { + viewer = new ConsoleProxyVncClient(); + viewer.initClient(param); + connectionMap.put(clientKey, viewer); + s_logger.info("Added viewer object " + viewer); + + reportLoadChange = true; + } else if (!viewer.isFrontEndAlive()) { + s_logger.info("The rfb thread died, reinitializing the viewer " + viewer); + viewer.initClient(param); + } else if (!param.getClientHostPassword().equals(viewer.getClientHostPassword())) { + s_logger.warn("Bad sid detected(VNC port may be reused). sid in session: " + viewer.getClientHostPassword() + + ", sid in request: " + param.getClientHostPassword()); + viewer.initClient(param); + } + } + + if(reportLoadChange) { + ConsoleProxyClientStatsCollector statsCollector = getStatsCollector(); + String loadInfo = statsCollector.getStatsReport(); + reportLoadInfo(loadInfo); + if(s_logger.isDebugEnabled()) + s_logger.debug("Report load change : " + loadInfo); + } + + return viewer; + } + + public static ConsoleProxyClient getAjaxVncViewer(ConsoleProxyClientParam param, String ajaxSession) throws Exception { + + boolean reportLoadChange = false; + String clientKey = param.getClientMapKey(); + synchronized (connectionMap) { + ConsoleProxyClient viewer = connectionMap.get(clientKey); + if (viewer == null) { + viewer = new ConsoleProxyVncClient(); + viewer.initClient(param); + + connectionMap.put(clientKey, viewer); + s_logger.info("Added viewer object " + viewer); + reportLoadChange = true; + } else if (!viewer.isFrontEndAlive()) { + s_logger.info("The rfb thread died, reinitializing the viewer " + viewer); + viewer.initClient(param); + } else if (!param.getClientHostPassword().equals(viewer.getClientHostPassword())) { + s_logger.warn("Bad sid detected(VNC port may be reused). sid in session: " + + viewer.getClientHostPassword() + ", sid in request: " + param.getClientHostPassword()); + viewer.initClient(param); + } else { + if(ajaxSession == null || ajaxSession.isEmpty()) + authenticationExternally(param); + } + + if(reportLoadChange) { + ConsoleProxyClientStatsCollector statsCollector = getStatsCollector(); + String loadInfo = statsCollector.getStatsReport(); + reportLoadInfo(loadInfo); + if(s_logger.isDebugEnabled()) + s_logger.debug("Report load change : " + loadInfo); + } + return viewer; + } + } + + public static void removeViewer(ConsoleProxyClient viewer) { + synchronized (connectionMap) { + for(Map.Entry entry : connectionMap.entrySet()) { + if(entry.getValue() == viewer) { + connectionMap.remove(entry.getKey()); + return; + } + } + } + } + + public static ConsoleProxyClientStatsCollector getStatsCollector() { + return new ConsoleProxyClientStatsCollector(connectionMap); + } + + public static void authenticationExternally(ConsoleProxyClientParam param) throws AuthenticationException { + ConsoleProxyAuthenticationResult authResult = authenticateConsoleAccess(param, false); + + if(authResult == null || !authResult.isSuccess()) { + s_logger.warn("External authenticator failed authencation request for vm " + param.getClientTag() + " with sid " + param.getClientHostPassword()); + + throw new AuthenticationException("External authenticator failed request for vm " + param.getClientTag() + " with sid " + param.getClientHostPassword()); + } + } + + public static ConsoleProxyAuthenticationResult reAuthenticationExternally(ConsoleProxyClientParam param) { + return authenticateConsoleAccess(param, true); + } + + public static String getEncryptorPassword() { + return encryptorPassword; + } + + public static void setEncryptorPassword(String password) { + encryptorPassword = password; + } + + static class ThreadExecutor implements Executor { + public void execute(Runnable r) { + new Thread(r).start(); + } + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyAjaxHandler.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyAjaxHandler.java new file mode 100644 index 00000000000..6cadeca1f4a --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyAjaxHandler.java @@ -0,0 +1,406 @@ +// 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 java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.URLDecoder; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import com.cloud.consoleproxy.util.Logger; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; + +public class ConsoleProxyAjaxHandler implements HttpHandler { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyAjaxHandler.class); + + public ConsoleProxyAjaxHandler() { + } + + public void handle(HttpExchange t) throws IOException { + try { + if(s_logger.isTraceEnabled()) + s_logger.trace("AjaxHandler " + t.getRequestURI()); + + long startTick = System.currentTimeMillis(); + + doHandle(t); + + if(s_logger.isTraceEnabled()) + s_logger.trace(t.getRequestURI() + " process time " + (System.currentTimeMillis() - startTick) + " ms"); + } catch (IOException e) { + throw e; + } catch (IllegalArgumentException e) { + s_logger.warn("Exception, ", e); + t.sendResponseHeaders(400, -1); // bad request + } catch(Throwable e) { + s_logger.error("Unexpected exception, ", e); + t.sendResponseHeaders(500, -1); // server error + } finally { + t.close(); + } + } + + private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException { + String queries = t.getRequestURI().getQuery(); + if(s_logger.isTraceEnabled()) + s_logger.trace("Handle AJAX request: " + queries); + + Map queryMap = ConsoleProxyHttpHandlerHelper.getQueryMap(queries); + + String host = queryMap.get("host"); + String portStr = queryMap.get("port"); + String sid = queryMap.get("sid"); + String tag = queryMap.get("tag"); + String ticket = queryMap.get("ticket"); + String ajaxSessionIdStr = queryMap.get("sess"); + String eventStr = queryMap.get("event"); + String console_url = queryMap.get("consoleurl"); + String console_host_session = queryMap.get("sessionref"); + + if(tag == null) + tag = ""; + + long ajaxSessionId = 0; + int event = 0; + + int port; + + if(host == null || portStr == null || sid == null) + throw new IllegalArgumentException(); + + try { + port = Integer.parseInt(portStr); + } catch (NumberFormatException e) { + s_logger.warn("Invalid number parameter in query string: " + portStr); + throw new IllegalArgumentException(e); + } + + if(ajaxSessionIdStr != null) { + try { + ajaxSessionId = Long.parseLong(ajaxSessionIdStr); + } catch (NumberFormatException e) { + s_logger.warn("Invalid number parameter in query string: " + ajaxSessionIdStr); + throw new IllegalArgumentException(e); + } + } + + if(eventStr != null) { + try { + event = Integer.parseInt(eventStr); + } catch (NumberFormatException e) { + s_logger.warn("Invalid number parameter in query string: " + eventStr); + throw new IllegalArgumentException(e); + } + } + + ConsoleProxyClient viewer = null; + try { + ConsoleProxyClientParam param = new ConsoleProxyClientParam(); + param.setClientHostAddress(host); + param.setClientHostPort(port); + param.setClientHostPassword(sid); + param.setClientTag(tag); + param.setTicket(ticket); + param.setClientTunnelUrl(console_url); + param.setClientTunnelSession(console_host_session); + + viewer = ConsoleProxy.getAjaxVncViewer(param, ajaxSessionIdStr); + } catch(Exception e) { + + s_logger.warn("Failed to create viewer due to " + e.getMessage(), e); + + String[] content = new String[] { + "", + "
", + "

Access is denied for the console session. Please close the window and retry again

", + "
" + }; + + StringBuffer sb = new StringBuffer(); + for(int i = 0; i < content.length; i++) + sb.append(content[i]); + + sendResponse(t, "text/html", sb.toString()); + return; + } + + if(event != 0) { + if(ajaxSessionId != 0 && ajaxSessionId == viewer.getAjaxSessionId()) { + if(event == 7) { + // client send over an event bag + InputStream is = t.getRequestBody(); + handleClientEventBag(viewer, convertStreamToString(is, true)); + } else { + handleClientEvent(viewer, event, queryMap); + } + sendResponse(t, "text/html", "OK"); + } else { + if(s_logger.isDebugEnabled()) + s_logger.debug("Ajax request comes from a different session, id in request: " + ajaxSessionId + ", id in viewer: " + viewer.getAjaxSessionId()); + + sendResponse(t, "text/html", "Invalid ajax client session id"); + } + } else { + if(ajaxSessionId != 0 && ajaxSessionId != viewer.getAjaxSessionId()) { + s_logger.info("Ajax request comes from a different session, id in request: " + ajaxSessionId + ", id in viewer: " + viewer.getAjaxSessionId()); + handleClientKickoff(t, viewer); + } else if(ajaxSessionId == 0) { + if(s_logger.isDebugEnabled()) + s_logger.debug("Ajax request indicates a fresh client start"); + + String title = queryMap.get("t"); + String guest = queryMap.get("guest"); + handleClientStart(t, viewer, title != null ? title : "", guest); + } else { + + if(s_logger.isTraceEnabled()) + s_logger.trace("Ajax request indicates client update"); + + handleClientUpdate(t, viewer); + } + } + } + + private static String convertStreamToString(InputStream is, boolean closeStreamAfterRead) { + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + StringBuilder sb = new StringBuilder(); + String line = null; + try { + while ((line = reader.readLine()) != null) { + sb.append(line + "\n"); + } + } catch (IOException e) { + s_logger.warn("Exception while reading request body: ", e); + } finally { + if(closeStreamAfterRead) { + try { + is.close(); + } catch (IOException e) { + } + } + } + return sb.toString(); + } + + private void sendResponse(HttpExchange t, String contentType, String response) throws IOException { + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", contentType); + + t.sendResponseHeaders(200, response.length()); + OutputStream os = t.getResponseBody(); + try { + os.write(response.getBytes()); + } finally { + os.close(); + } + } + + @SuppressWarnings("deprecation") + private void handleClientEventBag(ConsoleProxyClient viewer, String requestData) { + if(s_logger.isTraceEnabled()) + s_logger.trace("Handle event bag, event bag: " + requestData); + + int start = requestData.indexOf("="); + if(start < 0) + start = 0; + else if(start > 0) + start++; + String data = URLDecoder.decode(requestData.substring(start)); + String[] tokens = data.split("\\|"); + if(tokens != null && tokens.length > 0) { + int count = 0; + try { + count = Integer.parseInt(tokens[0]); + int parsePos = 1; + int type, event, x, y, code, modifiers; + for(int i = 0; i < count; i++) { + type = Integer.parseInt(tokens[parsePos++]); + if(type == 1) { + // mouse event + event = Integer.parseInt(tokens[parsePos++]); + x = Integer.parseInt(tokens[parsePos++]); + y = Integer.parseInt(tokens[parsePos++]); + code = Integer.parseInt(tokens[parsePos++]); + modifiers = Integer.parseInt(tokens[parsePos++]); + + Map queryMap = new HashMap(); + queryMap.put("event", String.valueOf(event)); + queryMap.put("x", String.valueOf(x)); + queryMap.put("y", String.valueOf(y)); + queryMap.put("code", String.valueOf(code)); + queryMap.put("modifier", String.valueOf(modifiers)); + handleClientEvent(viewer, event, queryMap); + } else { + // keyboard event + event = Integer.parseInt(tokens[parsePos++]); + code = Integer.parseInt(tokens[parsePos++]); + modifiers = Integer.parseInt(tokens[parsePos++]); + + Map queryMap = new HashMap(); + queryMap.put("event", String.valueOf(event)); + queryMap.put("code", String.valueOf(code)); + queryMap.put("modifier", String.valueOf(modifiers)); + handleClientEvent(viewer, event, queryMap); + } + } + } catch(NumberFormatException e) { + s_logger.warn("Exception in handle client event bag: " + data + ", ", e); + } catch(Exception e) { + s_logger.warn("Exception in handle client event bag: " + data + ", ", e); + } catch(OutOfMemoryError e) { + s_logger.error("Unrecoverable OutOfMemory Error, exit and let it be re-launched"); + System.exit(1); + } + } + } + + private void handleClientEvent(ConsoleProxyClient viewer, int event, Map queryMap) { + int code = 0; + int x = 0, y = 0; + int modifiers = 0; + + String str; + switch(event) { + case 1: // mouse move + case 2: // mouse down + case 3: // mouse up + case 8: // mouse double click + str = queryMap.get("x"); + if(str != null) { + try { + x = Integer.parseInt(str); + } catch (NumberFormatException e) { + s_logger.warn("Invalid number parameter in query string: " + str); + throw new IllegalArgumentException(e); + } + } + str = queryMap.get("y"); + if(str != null) { + try { + y = Integer.parseInt(str); + } catch (NumberFormatException e) { + s_logger.warn("Invalid number parameter in query string: " + str); + throw new IllegalArgumentException(e); + } + } + + if(event != 1) { + str = queryMap.get("code"); + try { + code = Integer.parseInt(str); + } catch (NumberFormatException e) { + s_logger.warn("Invalid number parameter in query string: " + str); + throw new IllegalArgumentException(e); + } + + str = queryMap.get("modifier"); + try { + modifiers = Integer.parseInt(str); + } catch (NumberFormatException e) { + s_logger.warn("Invalid number parameter in query string: " + str); + throw new IllegalArgumentException(e); + } + + if(s_logger.isTraceEnabled()) + s_logger.trace("Handle client mouse event. event: " + event + ", x: " + x + ", y: " + y + ", button: " + code + ", modifier: " + modifiers); + } else { + if(s_logger.isTraceEnabled()) + s_logger.trace("Handle client mouse move event. x: " + x + ", y: " + y); + } + viewer.sendClientMouseEvent(InputEventType.fromEventCode(event), x, y, code, modifiers); + break; + + case 4: // key press + case 5: // key down + case 6: // key up + str = queryMap.get("code"); + try { + code = Integer.parseInt(str); + } catch (NumberFormatException e) { + s_logger.warn("Invalid number parameter in query string: " + str); + throw new IllegalArgumentException(e); + } + + str = queryMap.get("modifier"); + try { + modifiers = Integer.parseInt(str); + } catch (NumberFormatException e) { + s_logger.warn("Invalid number parameter in query string: " + str); + throw new IllegalArgumentException(e); + } + + if(s_logger.isDebugEnabled()) + s_logger.debug("Handle client keyboard event. event: " + event + ", code: " + code + ", modifier: " + modifiers); + viewer.sendClientRawKeyboardEvent(InputEventType.fromEventCode(event), code, modifiers); + break; + + default : + break; + } + } + + private void handleClientKickoff(HttpExchange t, ConsoleProxyClient viewer) throws IOException { + String response = viewer.onAjaxClientKickoff(); + t.sendResponseHeaders(200, response.length()); + OutputStream os = t.getResponseBody(); + try { + os.write(response.getBytes()); + } finally { + os.close(); + } + } + + private void handleClientStart(HttpExchange t, ConsoleProxyClient viewer, String title, String guest) throws IOException { + List languages = t.getRequestHeaders().get("Accept-Language"); + String response = viewer.onAjaxClientStart(title, languages, guest); + + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", "text/html"); + hds.set("Cache-Control", "no-cache"); + hds.set("Cache-Control", "no-store"); + t.sendResponseHeaders(200, response.length()); + + OutputStream os = t.getResponseBody(); + try { + os.write(response.getBytes()); + } finally { + os.close(); + } + } + + private void handleClientUpdate(HttpExchange t, ConsoleProxyClient viewer) throws IOException { + String response = viewer.onAjaxClientUpdate(); + + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", "text/javascript"); + t.sendResponseHeaders(200, response.length()); + + OutputStream os = t.getResponseBody(); + try { + os.write(response.getBytes()); + } finally { + os.close(); + } + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyAjaxImageHandler.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyAjaxImageHandler.java new file mode 100644 index 00000000000..5e1014963f3 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyAjaxImageHandler.java @@ -0,0 +1,159 @@ +// 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 java.awt.Graphics2D; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Map; + +import com.cloud.consoleproxy.util.Logger; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; + +public class ConsoleProxyAjaxImageHandler implements HttpHandler { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyAjaxImageHandler.class); + + public void handle(HttpExchange t) throws IOException { + try { + if(s_logger.isDebugEnabled()) + s_logger.debug("AjaxImageHandler " + t.getRequestURI()); + + long startTick = System.currentTimeMillis(); + + doHandle(t); + + if(s_logger.isDebugEnabled()) + s_logger.debug(t.getRequestURI() + "Process time " + (System.currentTimeMillis() - startTick) + " ms"); + } catch (IOException e) { + throw e; + } catch (IllegalArgumentException e) { + s_logger.warn("Exception, ", e); + t.sendResponseHeaders(400, -1); // bad request + } catch(OutOfMemoryError e) { + s_logger.error("Unrecoverable OutOfMemory Error, exit and let it be re-launched"); + System.exit(1); + } catch(Throwable e) { + s_logger.error("Unexpected exception, ", e); + t.sendResponseHeaders(500, -1); // server error + } finally { + t.close(); + } + } + + private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException { + String queries = t.getRequestURI().getQuery(); + Map queryMap = ConsoleProxyHttpHandlerHelper.getQueryMap(queries); + + String host = queryMap.get("host"); + String portStr = queryMap.get("port"); + String sid = queryMap.get("sid"); + String tag = queryMap.get("tag"); + String ticket = queryMap.get("ticket"); + String keyStr = queryMap.get("key"); + String console_url = queryMap.get("consoleurl"); + String console_host_session = queryMap.get("sessionref"); + String w = queryMap.get("w"); + String h = queryMap.get("h"); + + int key = 0; + int width = 144; + int height = 110; + + if(tag == null) + tag = ""; + + int port; + if(host == null || portStr == null || sid == null) + throw new IllegalArgumentException(); + + try { + port = Integer.parseInt(portStr); + } catch (NumberFormatException e) { + s_logger.warn("Invalid numeric parameter in query string: " + portStr); + throw new IllegalArgumentException(e); + } + + try { + if (keyStr != null) + key = Integer.parseInt(keyStr); + if(null != w) + width = Integer.parseInt(w); + + if(null != h) + height = Integer.parseInt(h); + + } catch (NumberFormatException e) { + s_logger.warn("Invalid numeric parameter in query string: " + keyStr); + throw new IllegalArgumentException(e); + } + + ConsoleProxyClientParam param = new ConsoleProxyClientParam(); + param.setClientHostAddress(host); + param.setClientHostPort(port); + param.setClientHostPassword(sid); + param.setClientTag(tag); + param.setTicket(ticket); + param.setClientTunnelUrl(console_url); + param.setClientTunnelSession(console_host_session); + + ConsoleProxyClient viewer = ConsoleProxy.getVncViewer(param); + + if (key == 0) { + Image scaledImage = viewer.getClientScaledImage(width, height); + BufferedImage bufferedImage = new BufferedImage(width, height, + BufferedImage.TYPE_3BYTE_BGR); + Graphics2D bufImageGraphics = bufferedImage.createGraphics(); + bufImageGraphics.drawImage(scaledImage, 0, 0, null); + ByteArrayOutputStream bos = new ByteArrayOutputStream(8196); + javax.imageio.ImageIO.write(bufferedImage, "jpg", bos); + byte[] bs = bos.toByteArray(); + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", "image/jpeg"); + hds.set("Cache-Control", "no-cache"); + hds.set("Cache-Control", "no-store"); + t.sendResponseHeaders(200, bs.length); + OutputStream os = t.getResponseBody(); + os.write(bs); + os.close(); + } else { + AjaxFIFOImageCache imageCache = viewer.getAjaxImageCache(); + byte[] img = imageCache.getImage(key); + + if(img != null) { + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", "image/jpeg"); + t.sendResponseHeaders(200, img.length); + + OutputStream os = t.getResponseBody(); + try { + os.write(img, 0, img.length); + } finally { + os.close(); + } + } else { + if(s_logger.isInfoEnabled()) + s_logger.info("Image has already been swept out, key: " + key); + t.sendResponseHeaders(404, -1); + } + } + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyAuthenticationResult.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyAuthenticationResult.java new file mode 100644 index 00000000000..26ee9b33155 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyAuthenticationResult.java @@ -0,0 +1,81 @@ +// 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; + +// duplicated class +public class ConsoleProxyAuthenticationResult { + private boolean success; + private boolean isReauthentication; + private String host; + private int port; + private String tunnelUrl; + private String tunnelSession; + + public ConsoleProxyAuthenticationResult() { + success = false; + isReauthentication = false; + port = 0; + } + + public boolean isSuccess() { + return success; + } + + public void setSuccess(boolean success) { + this.success = success; + } + + public boolean isReauthentication() { + return isReauthentication; + } + + public void setReauthentication(boolean isReauthentication) { + this.isReauthentication = isReauthentication; + } + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getTunnelUrl() { + return tunnelUrl; + } + + public void setTunnelUrl(String tunnelUrl) { + this.tunnelUrl = tunnelUrl; + } + + public String getTunnelSession() { + return tunnelSession; + } + + public void setTunnelSession(String tunnelSession) { + this.tunnelSession = tunnelSession; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyBaseServerFactoryImpl.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyBaseServerFactoryImpl.java new file mode 100644 index 00000000000..c9ad8ab82fa --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyBaseServerFactoryImpl.java @@ -0,0 +1,48 @@ +// 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 java.io.IOException; +import java.net.InetSocketAddress; + +import javax.net.ssl.SSLServerSocket; + +import com.cloud.consoleproxy.util.Logger; +import com.sun.net.httpserver.HttpServer; + +public class ConsoleProxyBaseServerFactoryImpl implements ConsoleProxyServerFactory { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyBaseServerFactoryImpl.class); + + @Override + public void init(byte[] ksBits, String ksPassword) { + } + + @Override + public HttpServer createHttpServerInstance(int port) throws IOException { + if(s_logger.isInfoEnabled()) + s_logger.info("create HTTP server instance at port: " + port); + return HttpServer.create(new InetSocketAddress(port), 5); + } + + @Override + public SSLServerSocket createSSLServerSocket(int port) throws IOException { + if(s_logger.isInfoEnabled()) + s_logger.info("SSL server socket is not supported in ConsoleProxyBaseServerFactoryImpl"); + + return null; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClient.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClient.java new file mode 100644 index 00000000000..8a0be051903 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClient.java @@ -0,0 +1,69 @@ +// 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 java.awt.Image; +import java.util.List; + +/** + * ConsoleProxyClient defines an standard interface that a console client should implement, + * + * ConsoleProxyClient maintains a session towards the target host, it glues the session + * to a AJAX front-end viewer + */ +public interface ConsoleProxyClient { + int getClientId(); + + // + // Quick status + // + boolean isHostConnected(); + boolean isFrontEndAlive(); + + // + // AJAX viewer + // + long getAjaxSessionId(); + AjaxFIFOImageCache getAjaxImageCache(); + Image getClientScaledImage(int width, int height); // client thumbnail support + + String onAjaxClientStart(String title, List languages, String guest); + String onAjaxClientUpdate(); + String onAjaxClientKickoff(); + + // + // Input handling + // + void sendClientRawKeyboardEvent(InputEventType event, int code, int modifiers); + void sendClientMouseEvent(InputEventType event, int x, int y, int code, int modifiers); + + // + // Info/Stats + // + long getClientCreateTime(); + long getClientLastFrontEndActivityTime(); + String getClientHostAddress(); + int getClientHostPort(); + String getClientHostPassword(); + String getClientTag(); + + // + // Setup/house-keeping + // + void initClient(ConsoleProxyClientParam param); + void closeClient(); +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientBase.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientBase.java new file mode 100644 index 00000000000..289bdab2f8a --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientBase.java @@ -0,0 +1,457 @@ +// 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 java.awt.Image; +import java.awt.Rectangle; +import java.util.List; + +import org.apache.log4j.Logger; + +import com.cloud.consoleproxy.util.TileInfo; +import com.cloud.consoleproxy.util.TileTracker; +import com.cloud.consoleproxy.vnc.FrameBufferCanvas; + +/** + * + * an instance of specialized console protocol implementation, such as VNC or RDP + * + * It mainly implements the features needed by front-end AJAX viewer + * + */ +public abstract class ConsoleProxyClientBase implements ConsoleProxyClient, ConsoleProxyClientListener { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyClientBase.class); + + private static int s_nextClientId = 0; + protected int clientId = getNextClientId(); + + protected long ajaxSessionId = 0; + + protected boolean dirtyFlag = false; + protected Object tileDirtyEvent = new Object(); + protected TileTracker tracker; + protected AjaxFIFOImageCache ajaxImageCache = new AjaxFIFOImageCache(2); + + protected ConsoleProxyClientParam clientParam; + protected String clientToken; + + protected long createTime = System.currentTimeMillis(); + protected long lastFrontEndActivityTime = System.currentTimeMillis(); + + protected boolean framebufferResized = false; + protected int resizedFramebufferWidth; + protected int resizedFramebufferHeight; + + public ConsoleProxyClientBase() { + tracker = new TileTracker(); + tracker.initTracking(64, 64, 800, 600); + } + + // + // interface ConsoleProxyClient + // + @Override + public int getClientId() { + return clientId; + } + + public abstract boolean isHostConnected(); + public abstract boolean isFrontEndAlive(); + + @Override + public long getAjaxSessionId() { + return this.ajaxSessionId; + } + + @Override + public AjaxFIFOImageCache getAjaxImageCache() { + return ajaxImageCache; + } + + public Image getClientScaledImage(int width, int height) { + FrameBufferCanvas canvas = getFrameBufferCavas(); + if(canvas != null) + return canvas.getFrameBufferScaledImage(width, height); + + return null; + } + + public abstract void sendClientRawKeyboardEvent(InputEventType event, int code, int modifiers); + public abstract void sendClientMouseEvent(InputEventType event, int x, int y, int code, int modifiers); + + @Override + public long getClientCreateTime() { + return createTime; + } + + @Override + public long getClientLastFrontEndActivityTime() { + return lastFrontEndActivityTime; + } + + @Override + public String getClientHostAddress() { + return clientParam.getClientHostAddress(); + } + + @Override + public int getClientHostPort() { + return clientParam.getClientHostPort(); + } + + @Override + public String getClientHostPassword() { + return clientParam.getClientHostPassword(); + } + + @Override + public String getClientTag() { + if(clientParam.getClientTag() != null) + return clientParam.getClientTag(); + return ""; + } + + @Override + public abstract void initClient(ConsoleProxyClientParam param); + + @Override + public abstract void closeClient(); + + // + // interface FrameBufferEventListener + // + @Override + public void onFramebufferSizeChange(int w, int h) { + tracker.resize(w, h); + + synchronized(this) { + framebufferResized = true; + resizedFramebufferWidth = w; + resizedFramebufferHeight = h; + } + + signalTileDirtyEvent(); + } + + @Override + public void onFramebufferUpdate(int x, int y, int w, int h) { + if(s_logger.isTraceEnabled()) + s_logger.trace("Frame buffer update {" + x + "," + y + "," + w + "," + h + "}"); + tracker.invalidate(new Rectangle(x, y, w, h)); + + signalTileDirtyEvent(); + } + + // + // AJAX Image manipulation + // + public byte[] getFrameBufferJpeg() { + FrameBufferCanvas canvas = getFrameBufferCavas(); + if(canvas != null) + return canvas.getFrameBufferJpeg(); + + return null; + } + + public byte[] getTilesMergedJpeg(List tileList, int tileWidth, int tileHeight) { + FrameBufferCanvas canvas = getFrameBufferCavas(); + if(canvas != null) + return canvas.getTilesMergedJpeg(tileList, tileWidth, tileHeight); + return null; + } + + private String prepareAjaxImage(List tiles, boolean init) { + byte[] imgBits; + if(init) + imgBits = getFrameBufferJpeg(); + else + imgBits = getTilesMergedJpeg(tiles, tracker.getTileWidth(), tracker.getTileHeight()); + + if(imgBits == null) { + s_logger.warn("Unable to generate jpeg image"); + } else { + if(s_logger.isTraceEnabled()) + s_logger.trace("Generated jpeg image size: " + imgBits.length); + } + + int key = ajaxImageCache.putImage(imgBits); + StringBuffer sb = new StringBuffer(); + sb.append("/ajaximg?token=").append(clientToken); + sb.append("&key=").append(key); + sb.append("&ts=").append(System.currentTimeMillis()); + + return sb.toString(); + } + + private String prepareAjaxSession(boolean init) { + if(init) { + synchronized(this) { + ajaxSessionId++; + } + } + + StringBuffer sb = new StringBuffer(); + sb.append("/ajax?token=").append(clientToken).append("&sess=").append(ajaxSessionId); + return sb.toString(); + } + + @Override + public String onAjaxClientKickoff() { + return "onKickoff();"; + } + + private boolean waitForViewerReady() { + long startTick = System.currentTimeMillis(); + while(System.currentTimeMillis() - startTick < 5000) { + if(getFrameBufferCavas() != null) + return true; + + try { + Thread.sleep(100); + } catch (InterruptedException e) { + } + } + return false; + } + + private String onAjaxClientConnectFailed() { + return "

" + + "Unable to start console session as connection is refused by the machine you are accessing" + + "

"; + } + + @Override + public String onAjaxClientStart(String title, List languages, String guest) { + updateFrontEndActivityTime(); + + if(!waitForViewerReady()) + return onAjaxClientConnectFailed(); + + synchronized(this) { + ajaxSessionId++; + framebufferResized = false; + } + + int tileWidth = tracker.getTileWidth(); + int tileHeight = tracker.getTileHeight(); + int width = tracker.getTrackWidth(); + int height = tracker.getTrackHeight(); + + if(s_logger.isTraceEnabled()) + s_logger.trace("Ajax client start, frame buffer w: " + width + ", " + height); + + int retry = 0; + tracker.initCoverageTest(); + while(!tracker.hasFullCoverage() && retry < 10) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + retry++; + } + + List tiles = tracker.scan(true); + String imgUrl = prepareAjaxImage(tiles, true); + String updateUrl = prepareAjaxSession(true); + + StringBuffer sbTileSequence = new StringBuffer(); + int i = 0; + for(TileInfo tile : tiles) { + sbTileSequence.append("[").append(tile.getRow()).append(",").append(tile.getCol()).append("]"); + if(i < tiles.size() - 1) + sbTileSequence.append(","); + + i++; + } + + return getAjaxViewerPageContent(sbTileSequence.toString(), imgUrl, + updateUrl, width, height, tileWidth, tileHeight, title, + ConsoleProxy.keyboardType == ConsoleProxy.KEYBOARD_RAW, + languages, guest); + } + + private String getAjaxViewerPageContent(String tileSequence, String imgUrl, String updateUrl, int width, + int height, int tileWidth, int tileHeight, String title, boolean rawKeyboard, List languages, String guest) { + + StringBuffer sbLanguages = new StringBuffer(""); + if(languages != null) { + for(String lang : languages) { + if(sbLanguages.length() > 0) { + sbLanguages.append(","); + } + sbLanguages.append(lang); + } + } + + String[] content = new String[] { + "", + "", + "", + "", + "", + "", + "", + "", + "", + "" + title + "", + "", + "", + "
", + "", + "", + "
", + "
", + "", + "", + "" + }; + + StringBuffer sb = new StringBuffer(); + for(int i = 0; i < content.length; i++) + sb.append(content[i]); + + return sb.toString(); + } + + public String onAjaxClientDisconnected() { + return "onDisconnect();"; + } + + @Override + public String onAjaxClientUpdate() { + updateFrontEndActivityTime(); + if(!waitForViewerReady()) + return onAjaxClientDisconnected(); + + synchronized(tileDirtyEvent) { + if(!dirtyFlag) { + try { + tileDirtyEvent.wait(3000); + } catch(InterruptedException e) { + } + } + } + + boolean doResize = false; + synchronized(this) { + if(framebufferResized) { + framebufferResized = false; + doResize = true; + } + } + + List tiles; + + if(doResize) + tiles = tracker.scan(true); + else + tiles = tracker.scan(false); + dirtyFlag = false; + + String imgUrl = prepareAjaxImage(tiles, false); + StringBuffer sbTileSequence = new StringBuffer(); + int i = 0; + for(TileInfo tile : tiles) { + sbTileSequence.append("[").append(tile.getRow()).append(",").append(tile.getCol()).append("]"); + if(i < tiles.size() - 1) + sbTileSequence.append(","); + + i++; + } + + return getAjaxViewerUpdatePageContent(sbTileSequence.toString(), imgUrl, doResize, + resizedFramebufferWidth, resizedFramebufferHeight, + tracker.getTileWidth(), tracker.getTileHeight()); + } + + private String getAjaxViewerUpdatePageContent(String tileSequence, String imgUrl, boolean resized, int width, + int height, int tileWidth, int tileHeight) { + + String[] content = new String[] { + "tileMap = [ " + tileSequence + " ];", + resized ? "ajaxViewer.resize('main_panel', " + width + ", " + height + " , " + tileWidth + ", " + tileHeight + ");" : "", + "ajaxViewer.refresh('" + imgUrl + "', tileMap, false);" + }; + + StringBuffer sb = new StringBuffer(); + for(int i = 0; i < content.length; i++) + sb.append(content[i]); + + return sb.toString(); + } + + // + // Helpers + // + private synchronized static int getNextClientId() { + return ++s_nextClientId; + } + + private void signalTileDirtyEvent() { + synchronized(tileDirtyEvent) { + dirtyFlag = true; + tileDirtyEvent.notifyAll(); + } + } + + public void updateFrontEndActivityTime() { + lastFrontEndActivityTime = System.currentTimeMillis(); + } + + protected abstract FrameBufferCanvas getFrameBufferCavas(); + + public ConsoleProxyClientParam getClientParam() { + return clientParam; + } + + public void setClientParam(ConsoleProxyClientParam clientParam) { + this.clientParam = clientParam; + ConsoleProxyPasswordBasedEncryptor encryptor = new ConsoleProxyPasswordBasedEncryptor(ConsoleProxy.getEncryptorPassword()); + this.clientToken = encryptor.encryptObject(ConsoleProxyClientParam.class, clientParam); + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientListener.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientListener.java new file mode 100644 index 00000000000..43a0bab8bed --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientListener.java @@ -0,0 +1,25 @@ +// 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; + +public interface ConsoleProxyClientListener { + void onFramebufferSizeChange(int w, int h); + void onFramebufferUpdate(int x, int y, int w, int h); + + void onClientConnected(); + void onClientClose(); +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientParam.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientParam.java new file mode 100644 index 00000000000..8de4955d4b7 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientParam.java @@ -0,0 +1,110 @@ +// 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; + +/** + * + * Data object to store parameter info needed by client to connect to its host + */ +public class ConsoleProxyClientParam { + + private String clientHostAddress; + private int clientHostPort; + private String clientHostPassword; + private String clientTag; + private String ticket; + + private String clientTunnelUrl; + private String clientTunnelSession; + + private String ajaxSessionId; + + public ConsoleProxyClientParam() { + clientHostPort = 0; + } + + public String getClientHostAddress() { + return clientHostAddress; + } + + public void setClientHostAddress(String clientHostAddress) { + this.clientHostAddress = clientHostAddress; + } + + public int getClientHostPort() { + return clientHostPort; + } + + public void setClientHostPort(int clientHostPort) { + this.clientHostPort = clientHostPort; + } + + public String getClientHostPassword() { + return clientHostPassword; + } + + public void setClientHostPassword(String clientHostPassword) { + this.clientHostPassword = clientHostPassword; + } + + public String getClientTag() { + return clientTag; + } + + public void setClientTag(String clientTag) { + this.clientTag = clientTag; + } + + public String getTicket() { + return ticket; + } + + public void setTicket(String ticket) { + this.ticket = ticket; + } + + public String getClientTunnelUrl() { + return clientTunnelUrl; + } + + public void setClientTunnelUrl(String clientTunnelUrl) { + this.clientTunnelUrl = clientTunnelUrl; + } + + public String getClientTunnelSession() { + return clientTunnelSession; + } + + public void setClientTunnelSession(String clientTunnelSession) { + this.clientTunnelSession = clientTunnelSession; + } + + public String getAjaxSessionId() { + return this.ajaxSessionId; + } + + public void setAjaxSessionId(String ajaxSessionId) { + this.ajaxSessionId = ajaxSessionId; + } + + public String getClientMapKey() { + if(clientTag != null && !clientTag.isEmpty()) + return clientTag; + + return clientHostAddress + ":" + clientHostPort; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientStatsCollector.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientStatsCollector.java new file mode 100644 index 00000000000..15cf451ca2c --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyClientStatsCollector.java @@ -0,0 +1,88 @@ +// 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 java.io.OutputStreamWriter; +import java.util.ArrayList; +import java.util.Enumeration; +import java.util.Hashtable; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +/** + * + * ConsoleProxyClientStatsCollector collects client stats for console proxy agent to report + */ +public class ConsoleProxyClientStatsCollector { + + ArrayList connections; + + public ConsoleProxyClientStatsCollector() { + } + + public ConsoleProxyClientStatsCollector(Hashtable connMap) { + setConnections(connMap); + } + + public String getStatsReport() { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + return gson.toJson(this); + } + + public void getStatsReport(OutputStreamWriter os) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + gson.toJson(this, os); + } + + private void setConnections(Hashtable connMap) { + + ArrayList conns = new ArrayList(); + Enumeration e = connMap.keys(); + while (e.hasMoreElements()) { + synchronized (connMap) { + String key = e.nextElement(); + ConsoleProxyClient client = connMap.get(key); + + ConsoleProxyConnection conn = new ConsoleProxyConnection(); + + conn.id = client.getClientId(); + conn.clientInfo = ""; + conn.host = client.getClientHostAddress(); + conn.port = client.getClientHostPort(); + conn.tag = client.getClientTag(); + conn.createTime = client.getClientCreateTime(); + conn.lastUsedTime = client.getClientLastFrontEndActivityTime(); + conns.add(conn); + } + } + connections = conns; + } + + public static class ConsoleProxyConnection { + public int id; + public String clientInfo; + public String host; + public int port; + public String tag; + public long createTime; + public long lastUsedTime; + + public ConsoleProxyConnection() { + } + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyCmdHandler.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyCmdHandler.java new file mode 100644 index 00000000000..408eb0419d7 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyCmdHandler.java @@ -0,0 +1,70 @@ +// 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 java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; + +import com.cloud.consoleproxy.util.Logger; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; + +public class ConsoleProxyCmdHandler implements HttpHandler { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyCmdHandler.class); + + public void handle(HttpExchange t) throws IOException { + try { + Thread.currentThread().setName("Cmd Thread " + + Thread.currentThread().getId() + " " + t.getRemoteAddress()); + s_logger.info("CmdHandler " + t.getRequestURI()); + doHandle(t); + } catch (Exception e) { + s_logger.error(e.toString(), e); + String response = "Not found"; + t.sendResponseHeaders(404, response.length()); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } catch(OutOfMemoryError e) { + s_logger.error("Unrecoverable OutOfMemory Error, exit and let it be re-launched"); + System.exit(1); + } catch (Throwable e) { + s_logger.error(e.toString(), e); + } finally { + t.close(); + } + } + + public void doHandle(HttpExchange t) throws Exception { + String path = t.getRequestURI().getPath(); + int i = path.indexOf("/", 1); + String cmd = path.substring(i + 1); + s_logger.info("Get CMD request for " + cmd); + if (cmd.equals("getstatus")) { + ConsoleProxyClientStatsCollector statsCollector = ConsoleProxy.getStatsCollector(); + + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", "text/plain"); + t.sendResponseHeaders(200, 0); + OutputStreamWriter os = new OutputStreamWriter(t.getResponseBody()); + statsCollector.getStatsReport(os); + os.close(); + } + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyGCThread.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyGCThread.java new file mode 100644 index 00000000000..7f82a965f69 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyGCThread.java @@ -0,0 +1,109 @@ +// 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 java.io.File; +import java.util.Enumeration; +import java.util.Hashtable; + +import org.apache.log4j.Logger; + +/** + * + * ConsoleProxyGCThread does house-keeping work for the process, it helps cleanup log files, + * recycle idle client sessions without front-end activities and report client stats to external + * management software + */ +public class ConsoleProxyGCThread extends Thread { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyGCThread.class); + + private final static int MAX_SESSION_IDLE_SECONDS = 180; + + private Hashtable connMap; + private long lastLogScan = 0; + + public ConsoleProxyGCThread(Hashtable connMap) { + this.connMap = connMap; + } + + private void cleanupLogging() { + if(lastLogScan != 0 && System.currentTimeMillis() - lastLogScan < 3600000) + return; + + lastLogScan = System.currentTimeMillis(); + + File logDir = new File("./logs"); + File files[] = logDir.listFiles(); + if(files != null) { + for(File file : files) { + if(System.currentTimeMillis() - file.lastModified() >= 86400000L) { + try { + file.delete(); + } catch(Throwable e) { + } + } + } + } + } + + @Override + public void run() { + + boolean bReportLoad = false; + while (true) { + cleanupLogging(); + bReportLoad = false; + + if(s_logger.isDebugEnabled()) + s_logger.debug("connMap=" + connMap); + Enumeration e = connMap.keys(); + while (e.hasMoreElements()) { + String key; + ConsoleProxyClient client; + + synchronized (connMap) { + key = e.nextElement(); + client = connMap.get(key); + } + + long seconds_unused = (System.currentTimeMillis() - client.getClientLastFrontEndActivityTime()) / 1000; + if (seconds_unused < MAX_SESSION_IDLE_SECONDS) { + continue; + } + + synchronized (connMap) { + connMap.remove(key); + bReportLoad = true; + } + + // close the server connection + s_logger.info("Dropping " + client + " which has not been used for " + seconds_unused + " seconds"); + client.closeClient(); + } + + if(bReportLoad) { + // report load changes + String loadInfo = new ConsoleProxyClientStatsCollector(connMap).getStatsReport(); + ConsoleProxy.reportLoadInfo(loadInfo); + if(s_logger.isDebugEnabled()) + s_logger.debug("Report load change : " + loadInfo); + } + + try { Thread.sleep(5000); } catch (InterruptedException ex) {} + } + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyHttpHandlerHelper.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyHttpHandlerHelper.java new file mode 100644 index 00000000000..7756d01cd7f --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyHttpHandlerHelper.java @@ -0,0 +1,74 @@ +// 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 java.util.HashMap; +import java.util.Map; + +import com.cloud.consoleproxy.util.Logger; + +public class ConsoleProxyHttpHandlerHelper { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyHttpHandlerHelper.class); + + public static Map getQueryMap(String query) { + String[] params = query.split("&"); + Map map = new HashMap(); + for (String param : params) { + String[] paramTokens = param.split("="); + if(paramTokens != null && paramTokens.length == 2) { + String name = param.split("=")[0]; + String value = param.split("=")[1]; + map.put(name, value); + } else if (paramTokens.length == 3) { + // very ugly, added for Xen tunneling url + String name = paramTokens[0]; + String value = paramTokens[1] + "=" + paramTokens[2]; + map.put(name, value); + } else { + if(s_logger.isDebugEnabled()) + s_logger.debug("Invalid paramemter in URL found. param: " + param); + } + } + + // This is a ugly solution for now. We will do encryption/decryption translation + // here to make it transparent to rest of the code. + if(map.get("token") != null) { + ConsoleProxyPasswordBasedEncryptor encryptor = new ConsoleProxyPasswordBasedEncryptor( + ConsoleProxy.getEncryptorPassword()); + + ConsoleProxyClientParam param = encryptor.decryptObject(ConsoleProxyClientParam.class, map.get("token")); + if(param != null) { + if(param.getClientHostAddress() != null) + map.put("host", param.getClientHostAddress()); + if(param.getClientHostPort() != 0) + map.put("port", String.valueOf(param.getClientHostPort())); + if(param.getClientTag() != null) + map.put("tag", param.getClientTag()); + if(param.getClientHostPassword() != null) + map.put("sid", param.getClientHostPassword()); + if(param.getClientTunnelUrl() != null) + map.put("consoleurl", param.getClientTunnelUrl()); + if(param.getClientTunnelSession() != null) + map.put("sessionref", param.getClientTunnelSession()); + if(param.getTicket() != null) + map.put("ticket", param.getTicket()); + } + } + + return map; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyLoggerFactory.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyLoggerFactory.java new file mode 100644 index 00000000000..ff66de3bcc4 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyLoggerFactory.java @@ -0,0 +1,89 @@ +// 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.util.Logger; +import com.cloud.consoleproxy.util.LoggerFactory; + +public class ConsoleProxyLoggerFactory implements LoggerFactory { + public ConsoleProxyLoggerFactory() { + } + + public Logger getLogger(Class clazz) { + return new Log4jLogger(org.apache.log4j.Logger.getLogger(clazz)); + } + + public static class Log4jLogger extends Logger { + private org.apache.log4j.Logger logger; + + public Log4jLogger(org.apache.log4j.Logger logger) { + this.logger = logger; + } + + public boolean isTraceEnabled() { + return logger.isTraceEnabled(); + } + + public boolean isDebugEnabled() { + return logger.isDebugEnabled(); + } + + public boolean isInfoEnabled() { + return logger.isInfoEnabled(); + } + + public void trace(Object message) { + logger.trace(message); + } + + public void trace(Object message, Throwable exception) { + logger.trace(message, exception); + } + + public void info(Object message) { + logger.info(message); + } + + public void info(Object message, Throwable exception) { + logger.info(message, exception); + } + + public void debug(Object message) { + logger.debug(message); + } + + public void debug(Object message, Throwable exception) { + logger.debug(message, exception); + } + + public void warn(Object message) { + logger.warn(message); + } + + public void warn(Object message, Throwable exception) { + logger.warn(message, exception); + } + + public void error(Object message) { + logger.error(message); + } + + public void error(Object message, Throwable exception) { + logger.error(message, exception); + } + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyMonitor.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyMonitor.java new file mode 100644 index 00000000000..030b2f452eb --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyMonitor.java @@ -0,0 +1,153 @@ +// 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 java.io.File; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import org.apache.log4j.xml.DOMConfigurator; + +import com.cloud.consoleproxy.util.Logger; + + +// +// +// I switched to a simpler solution to monitor only unrecoverable exceptions, under these cases, console proxy process will exit +// itself and the shell script will re-launch console proxy +// +public class ConsoleProxyMonitor { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyMonitor.class); + + private String[] _argv; + private Map _argMap = new HashMap(); + + private volatile Process _process; + private boolean _quit = false; + + public ConsoleProxyMonitor(String[] argv) { + _argv = argv; + + for(String arg : _argv) { + String[] tokens = arg.split("="); + if(tokens.length == 2) { + s_logger.info("Add argument " + tokens[0] + "=" + tokens[1] + " to the argument map"); + + _argMap.put(tokens[0].trim(), tokens[1].trim()); + } else { + s_logger.warn("unrecognized argument, skip adding it to argument map"); + } + } + } + + private void run() { + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + _quit = true; + onShutdown(); + } + }); + + while(!_quit) { + String cmdLine = getLaunchCommandLine(); + + s_logger.info("Launch console proxy process with command line: " + cmdLine); + + try { + _process = Runtime.getRuntime().exec(cmdLine); + } catch (IOException e) { + s_logger.error("Unexpected exception ", e); + System.exit(1); + } + + boolean waitSucceeded = false; + int exitCode = 0; + while(!waitSucceeded) { + try { + exitCode = _process.waitFor(); + waitSucceeded = true; + + if(s_logger.isInfoEnabled()) + s_logger.info("Console proxy process exits with code: " + exitCode); + } catch (InterruptedException e) { + if(s_logger.isInfoEnabled()) + s_logger.info("InterruptedException while waiting for termination of console proxy, will retry"); + } + } + } + } + + private String getLaunchCommandLine() { + StringBuffer sb = new StringBuffer("java "); + String jvmOptions = _argMap.get("jvmoptions"); + + if(jvmOptions != null) + sb.append(jvmOptions); + + for(Map.Entry entry : _argMap.entrySet()) { + if(!"jvmoptions".equalsIgnoreCase(entry.getKey())) + sb.append(" ").append(entry.getKey()).append("=").append(entry.getValue()); + } + + return sb.toString(); + } + + private void onShutdown() { + if(_process != null) { + if(s_logger.isInfoEnabled()) + s_logger.info("Console proxy monitor shuts dwon, terminate console proxy process"); + _process.destroy(); + } + } + + private static void configLog4j() { + URL configUrl = System.class.getResource("/conf/log4j-cloud.xml"); + if(configUrl == null) + configUrl = ClassLoader.getSystemResource("log4j-cloud.xml"); + + if(configUrl == null) + configUrl = ClassLoader.getSystemResource("conf/log4j-cloud.xml"); + + if(configUrl != null) { + try { + System.out.println("Configure log4j using " + configUrl.toURI().toString()); + } catch (URISyntaxException e1) { + e1.printStackTrace(); + } + + try { + File file = new File(configUrl.toURI()); + + System.out.println("Log4j configuration from : " + file.getAbsolutePath()); + DOMConfigurator.configureAndWatch(file.getAbsolutePath(), 10000); + } catch (URISyntaxException e) { + System.out.println("Unable to convert log4j configuration Url to URI"); + } + } else { + System.out.println("Configure log4j with default properties"); + } + } + + public static void main(String[] argv) { + configLog4j(); + (new ConsoleProxyMonitor(argv)).run(); + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyPasswordBasedEncryptor.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyPasswordBasedEncryptor.java new file mode 100644 index 00000000000..29826f0ea92 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyPasswordBasedEncryptor.java @@ -0,0 +1,142 @@ +// 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 java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; + +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.spec.SecretKeySpec; + +import org.apache.commons.codec.binary.Base64; +import org.apache.log4j.Logger; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +/** + * + * A simple password based encyrptor based on DES. It can serialize simple POJO object into URL safe string + * and deserialize it back. + * + */ +public class ConsoleProxyPasswordBasedEncryptor { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyPasswordBasedEncryptor.class); + + private String password; + private Gson gson; + + public ConsoleProxyPasswordBasedEncryptor(String password) { + this.password = password; + gson = new GsonBuilder().create(); + } + + public String encryptText(String text) { + if(text == null || text.isEmpty()) + return text; + + assert(password != null); + assert(!password.isEmpty()); + + try { + Cipher cipher = Cipher.getInstance("DES"); + int maxKeySize = 8; + SecretKeySpec keySpec = new SecretKeySpec(normalizeKey(password.getBytes(), maxKeySize), "DES"); + cipher.init(Cipher.ENCRYPT_MODE, keySpec); + byte[] encryptedBytes = cipher.doFinal(text.getBytes()); + return Base64.encodeBase64URLSafeString(encryptedBytes); + } catch (NoSuchAlgorithmException e) { + s_logger.error("Unexpected exception ", e); + return null; + } catch (NoSuchPaddingException e) { + s_logger.error("Unexpected exception ", e); + return null; + } catch (IllegalBlockSizeException e) { + s_logger.error("Unexpected exception ", e); + return null; + } catch (BadPaddingException e) { + s_logger.error("Unexpected exception ", e); + return null; + } catch (InvalidKeyException e) { + s_logger.error("Unexpected exception ", e); + return null; + } + } + + public String decryptText(String encryptedText) { + if(encryptedText == null || encryptedText.isEmpty()) + return encryptedText; + + assert(password != null); + assert(!password.isEmpty()); + + try { + Cipher cipher = Cipher.getInstance("DES"); + int maxKeySize = 8; + SecretKeySpec keySpec = new SecretKeySpec(normalizeKey(password.getBytes(), maxKeySize), "DES"); + cipher.init(Cipher.DECRYPT_MODE, keySpec); + + byte[] encryptedBytes = Base64.decodeBase64(encryptedText); + return new String(cipher.doFinal(encryptedBytes)); + } catch (NoSuchAlgorithmException e) { + s_logger.error("Unexpected exception ", e); + return null; + } catch (NoSuchPaddingException e) { + s_logger.error("Unexpected exception ", e); + return null; + } catch (IllegalBlockSizeException e) { + s_logger.error("Unexpected exception ", e); + return null; + } catch (BadPaddingException e) { + s_logger.error("Unexpected exception ", e); + return null; + } catch (InvalidKeyException e) { + s_logger.error("Unexpected exception ", e); + return null; + } + } + + public String encryptObject(Class clz, T obj) { + if(obj == null) + return null; + + String json = gson.toJson(obj); + return encryptText(json); + } + + @SuppressWarnings("unchecked") + public T decryptObject(Class clz, String encrypted) { + if(encrypted == null || encrypted.isEmpty()) + return null; + + String json = decryptText(encrypted); + return (T)gson.fromJson(json, clz); + } + + private static byte[] normalizeKey(byte[] keyBytes, int keySize) { + assert(keySize > 0); + byte[] key = new byte[keySize]; + + for(int i = 0; i < keyBytes.length; i++) + key[i%keySize] ^= keyBytes[i]; + + return key; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyResourceHandler.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyResourceHandler.java new file mode 100644 index 00000000000..7d160472e8a --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyResourceHandler.java @@ -0,0 +1,181 @@ +// 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 java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import com.cloud.consoleproxy.util.Logger; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; + +public class ConsoleProxyResourceHandler implements HttpHandler { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyResourceHandler.class); + + static Map s_mimeTypes; + static { + s_mimeTypes = new HashMap(); + s_mimeTypes.put("jar", "application/java-archive"); + s_mimeTypes.put("js", "text/javascript"); + s_mimeTypes.put("css", "text/css"); + s_mimeTypes.put("jpg", "image/jpeg"); + s_mimeTypes.put("html", "text/html"); + s_mimeTypes.put("htm", "text/html"); + s_mimeTypes.put("log", "text/plain"); + } + + static Map s_validResourceFolders; + static { + s_validResourceFolders = new HashMap(); + s_validResourceFolders.put("applet", ""); + s_validResourceFolders.put("logs", ""); + s_validResourceFolders.put("images", ""); + s_validResourceFolders.put("js", ""); + s_validResourceFolders.put("css", ""); + s_validResourceFolders.put("html", ""); + } + + public ConsoleProxyResourceHandler() { + } + + public void handle(HttpExchange t) throws IOException { + try { + if(s_logger.isDebugEnabled()) + s_logger.debug("Resource Handler " + t.getRequestURI()); + + long startTick = System.currentTimeMillis(); + + doHandle(t); + + if(s_logger.isDebugEnabled()) + s_logger.debug(t.getRequestURI() + " Process time " + (System.currentTimeMillis() - startTick) + " ms"); + } catch (IOException e) { + throw e; + } catch(Throwable e) { + s_logger.error("Unexpected exception, ", e); + t.sendResponseHeaders(500, -1); // server error + } finally { + t.close(); + } + } + + @SuppressWarnings("deprecation") + private void doHandle(HttpExchange t) throws Exception { + String path = t.getRequestURI().getPath(); + + if(s_logger.isInfoEnabled()) + s_logger.info("Get resource request for " + path); + + int i = path.indexOf("/", 1); + String filepath = path.substring(i + 1); + i = path.lastIndexOf("."); + String extension = (i == -1) ? "" : path.substring(i + 1); + String contentType = getContentType(extension); + + if(!validatePath(filepath)) { + if(s_logger.isInfoEnabled()) + s_logger.info("Resource access is forbidden, uri: " + path); + + t.sendResponseHeaders(403, -1); // forbidden + return; + } + + File f = new File ("./" + filepath); + if(f.exists()) { + long lastModified = f.lastModified(); + String ifModifiedSince = t.getRequestHeaders().getFirst("If-Modified-Since"); + if (ifModifiedSince != null) { + long d = Date.parse(ifModifiedSince); + if (d + 1000 >= lastModified) { + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", contentType); + t.sendResponseHeaders(304, -1); + + if(s_logger.isInfoEnabled()) + s_logger.info("Sent 304 file has not been " + + "modified since " + ifModifiedSince); + return; + } + } + + long length = f.length(); + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", contentType); + hds.set("Last-Modified", new Date(lastModified).toGMTString()); + t.sendResponseHeaders(200, length); + responseFileContent(t, f); + + if(s_logger.isInfoEnabled()) + s_logger.info("Sent file " + path + " with content type " + contentType); + } else { + if(s_logger.isInfoEnabled()) + s_logger.info("file does not exist" + path); + t.sendResponseHeaders(404, -1); + } + } + + private static String getContentType(String extension) { + String key = extension.toLowerCase(); + if(s_mimeTypes.containsKey(key)) { + return s_mimeTypes.get(key); + } + return "application/octet-stream"; + } + + private static void responseFileContent(HttpExchange t, File f) throws Exception { + OutputStream os = t.getResponseBody(); + FileInputStream fis = new FileInputStream(f); + while (true) { + byte[] b = new byte[8192]; + int n = fis.read(b); + if (n < 0) { + break; + } + os.write(b, 0, n); + } + fis.close(); + os.close(); + } + + private static boolean validatePath(String path) { + int i = path.indexOf("/"); + if(i == -1) { + if(s_logger.isInfoEnabled()) + s_logger.info("Invalid resource path: can not start at resource root"); + return false; + } + + if(path.contains("..")) { + if(s_logger.isInfoEnabled()) + s_logger.info("Invalid resource path: contains relative up-level navigation"); + + return false; + } + + return isValidResourceFolder(path.substring(0, i)); + } + + private static boolean isValidResourceFolder(String name) { + return s_validResourceFolders.containsKey(name); + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxySecureServerFactoryImpl.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxySecureServerFactoryImpl.java new file mode 100644 index 00000000000..ee0ee13fa92 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxySecureServerFactoryImpl.java @@ -0,0 +1,145 @@ +// 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 java.io.ByteArrayInputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.net.InetSocketAddress; +import java.security.KeyStore; + +import javax.net.ssl.KeyManagerFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; +import javax.net.ssl.TrustManagerFactory; + +import org.apache.log4j.Logger; + +import com.sun.net.httpserver.HttpServer; +import com.sun.net.httpserver.HttpsConfigurator; +import com.sun.net.httpserver.HttpsParameters; +import com.sun.net.httpserver.HttpsServer; + +public class ConsoleProxySecureServerFactoryImpl implements ConsoleProxyServerFactory { + private static final Logger s_logger = Logger.getLogger(ConsoleProxySecureServerFactoryImpl.class); + + private SSLContext sslContext = null; + + public ConsoleProxySecureServerFactoryImpl() { + } + + @Override + public void init(byte[] ksBits, String ksPassword) { + s_logger.info("Start initializing SSL"); + + if(ksBits == null) { + try { + s_logger.info("Initializing SSL from built-in default certificate"); + + char[] passphrase = "vmops.com".toCharArray(); + KeyStore ks = KeyStore.getInstance("JKS"); + + ks.load(new FileInputStream("certs/realhostip.keystore"), passphrase); + // ks.load(ConsoleProxy.class.getResourceAsStream("/realhostip.keystore"), passphrase); + + s_logger.info("SSL certificate loaded"); + + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, passphrase); + s_logger.info("Key manager factory is initialized"); + + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); + tmf.init(ks); + s_logger.info("Trust manager factory is initialized"); + + sslContext = SSLContext.getInstance("TLS"); + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); + s_logger.info("SSL context is initialized"); + } catch (Exception ioe) { + s_logger.error(ioe.toString(), ioe); + } + + } else { + char[] passphrase = ksPassword != null ? ksPassword.toCharArray() : null; + try { + s_logger.info("Initializing SSL from passed-in certificate"); + + KeyStore ks = KeyStore.getInstance("JKS"); + ks.load(new ByteArrayInputStream(ksBits), passphrase); + + KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509"); + kmf.init(ks, passphrase); + s_logger.info("Key manager factory is initialized"); + + TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509"); + tmf.init(ks); + s_logger.info("Trust manager factory is initialized"); + + sslContext = SSLContext.getInstance("TLS"); + sslContext.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); + s_logger.info("SSL context is initialized"); + } catch(Exception e) { + s_logger.error("Unable to init factory due to exception ", e); + } + } + + } + + public HttpServer createHttpServerInstance(int port) throws IOException { + try { + HttpsServer server = HttpsServer.create(new InetSocketAddress(port), 5); + server.setHttpsConfigurator (new HttpsConfigurator(sslContext) { + @Override + public void configure (HttpsParameters params) { + + // get the remote address if needed + InetSocketAddress remote = params.getClientAddress(); + SSLContext c = getSSLContext(); + + // get the default parameters + SSLParameters sslparams = c.getDefaultSSLParameters(); + + params.setSSLParameters(sslparams); + // statement above could throw IAE if any params invalid. + // eg. if app has a UI and parameters supplied by a user. + } + }); + + s_logger.info("create HTTPS server instance on port: " + port); + return server; + } catch (Exception ioe) { + s_logger.error(ioe.toString(), ioe); + } + return null; + } + + public SSLServerSocket createSSLServerSocket(int port) throws IOException { + try { + SSLServerSocket srvSock = null; + SSLServerSocketFactory ssf = sslContext.getServerSocketFactory(); + srvSock = (SSLServerSocket) ssf.createServerSocket(port); + + s_logger.info("create SSL server socket on port: " + port); + return srvSock; + } catch (Exception ioe) { + s_logger.error(ioe.toString(), ioe); + } + return null; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyServerFactory.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyServerFactory.java new file mode 100644 index 00000000000..7e0e5c77bf4 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyServerFactory.java @@ -0,0 +1,29 @@ +// 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 java.io.IOException; + +import javax.net.ssl.SSLServerSocket; + +import com.sun.net.httpserver.HttpServer; + +public interface ConsoleProxyServerFactory { + void init(byte[] ksBits, String ksPassword); + HttpServer createHttpServerInstance(int port) throws IOException; + SSLServerSocket createSSLServerSocket(int port) throws IOException; +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyThumbnailHandler.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyThumbnailHandler.java new file mode 100644 index 00000000000..6d34d3b9162 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyThumbnailHandler.java @@ -0,0 +1,212 @@ +// 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 java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.util.HashMap; +import java.util.Map; + +import com.cloud.consoleproxy.util.Logger; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpExchange; +import com.sun.net.httpserver.HttpHandler; + +public class ConsoleProxyThumbnailHandler implements HttpHandler { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyThumbnailHandler.class); + + public ConsoleProxyThumbnailHandler() { + } + + public void handle(HttpExchange t) throws IOException { + try { + Thread.currentThread().setName("JPG Thread " + + Thread.currentThread().getId() + " " + t.getRemoteAddress()); + + if(s_logger.isDebugEnabled()) + s_logger.debug("ScreenHandler " + t.getRequestURI()); + + long startTick = System.currentTimeMillis(); + doHandle(t); + + if(s_logger.isDebugEnabled()) + s_logger.debug(t.getRequestURI() + "Process time " + (System.currentTimeMillis() - startTick) + " ms"); + } catch (IllegalArgumentException e) { + String response = "Bad query string"; + s_logger.error(response + ", request URI : " + t.getRequestURI()); + t.sendResponseHeaders(200, response.length()); + OutputStream os = t.getResponseBody(); + os.write(response.getBytes()); + os.close(); + } catch(OutOfMemoryError e) { + s_logger.error("Unrecoverable OutOfMemory Error, exit and let it be re-launched"); + System.exit(1); + } catch (Throwable e) { + s_logger.error("Unexpected exception while handing thumbnail request, ", e); + + String queries = t.getRequestURI().getQuery(); + Map queryMap = getQueryMap(queries); + int width = 0; + int height = 0; + String ws = queryMap.get("w"); + String hs = queryMap.get("h"); + try { + width = Integer.parseInt(ws); + height = Integer.parseInt(hs); + } catch (NumberFormatException ex) { + } + width = Math.min(width, 800); + height = Math.min(height, 600); + + BufferedImage img = generateTextImage(width, height, "Cannot Connect"); + ByteArrayOutputStream bos = new ByteArrayOutputStream(8196); + javax.imageio.ImageIO.write(img, "jpg", bos); + byte[] bs = bos.toByteArray(); + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", "image/jpeg"); + hds.set("Cache-Control", "no-cache"); + hds.set("Cache-Control", "no-store"); + t.sendResponseHeaders(200, bs.length); + OutputStream os = t.getResponseBody(); + os.write(bs); + os.close(); + s_logger.error("Cannot get console, sent error JPG response for " + t.getRequestURI()); + return; + } finally { + t.close(); + } + } + + private void doHandle(HttpExchange t) throws Exception, IllegalArgumentException { + String queries = t.getRequestURI().getQuery(); + Map queryMap = getQueryMap(queries); + int width = 0; + int height = 0; + int port = 0; + String ws = queryMap.get("w"); + String hs = queryMap.get("h"); + String host = queryMap.get("host"); + String portStr = queryMap.get("port"); + String sid = queryMap.get("sid"); + String tag = queryMap.get("tag"); + String ticket = queryMap.get("ticket"); + String console_url = queryMap.get("consoleurl"); + String console_host_session = queryMap.get("sessionref"); + + if(tag == null) + tag = ""; + + if (ws == null || hs == null || host == null || portStr == null || sid == null ) { + throw new IllegalArgumentException(); + } + try { + width = Integer.parseInt(ws); + height = Integer.parseInt(hs); + port = Integer.parseInt(portStr); + } catch (NumberFormatException e) { + throw new IllegalArgumentException(e); + } + + ConsoleProxyClientParam param = new ConsoleProxyClientParam(); + param.setClientHostAddress(host); + param.setClientHostPort(port); + param.setClientHostPassword(sid); + param.setClientTag(tag); + param.setTicket(ticket); + param.setClientTunnelUrl(console_url); + param.setClientTunnelSession(console_host_session); + + ConsoleProxyClient viewer = ConsoleProxy.getVncViewer(param); + + if (!viewer.isHostConnected()) { + // use generated image instead of static + BufferedImage img = generateTextImage(width, height, "Connecting"); + ByteArrayOutputStream bos = new ByteArrayOutputStream(8196); + javax.imageio.ImageIO.write(img, "jpg", bos); + byte[] bs = bos.toByteArray(); + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", "image/jpeg"); + hds.set("Cache-Control", "no-cache"); + hds.set("Cache-Control", "no-store"); + t.sendResponseHeaders(200, bs.length); + OutputStream os = t.getResponseBody(); + os.write(bs); + os.close(); + + if(s_logger.isInfoEnabled()) + s_logger.info("Console not ready, sent dummy JPG response"); + return; + } + + { + Image scaledImage = viewer.getClientScaledImage(width, height); + BufferedImage bufferedImage = new BufferedImage(width, height, + BufferedImage.TYPE_3BYTE_BGR); + Graphics2D bufImageGraphics = bufferedImage.createGraphics(); + bufImageGraphics.drawImage(scaledImage, 0, 0, null); + ByteArrayOutputStream bos = new ByteArrayOutputStream(8196); + javax.imageio.ImageIO.write(bufferedImage, "jpg", bos); + byte[] bs = bos.toByteArray(); + Headers hds = t.getResponseHeaders(); + hds.set("Content-Type", "image/jpeg"); + hds.set("Cache-Control", "no-cache"); + hds.set("Cache-Control", "no-store"); + t.sendResponseHeaders(200, bs.length); + OutputStream os = t.getResponseBody(); + os.write(bs); + os.close(); + } + } + + public static BufferedImage generateTextImage(int w, int h, String text) { + BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR); + Graphics2D g = img.createGraphics(); + g.setColor(Color.BLACK); + g.fillRect(0, 0, w, h); + g.setColor(Color.WHITE); + try { + g.setFont(new Font(null, Font.PLAIN, 12)); + FontMetrics fm = g.getFontMetrics(); + int textWidth = fm.stringWidth(text); + int startx = (w-textWidth) / 2; + if(startx < 0) + startx = 0; + g.drawString(text, startx, h/2); + } catch (Throwable e) { + s_logger.warn("Problem in generating text to thumnail image, return blank image"); + } + return img; + } + + public static Map getQueryMap(String query) { + String[] params = query.split("&"); + Map map = new HashMap(); + for (String param : params) { + String name = param.split("=")[0]; + String value = param.split("=")[1]; + map.put(name, value); + } + return map; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyVncClient.java b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyVncClient.java new file mode 100644 index 00000000000..6a473b5f007 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/ConsoleProxyVncClient.java @@ -0,0 +1,235 @@ +// 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 java.io.IOException; +import java.net.URI; +import java.net.UnknownHostException; + +import org.apache.log4j.Logger; + +import com.cloud.consoleproxy.vnc.FrameBufferCanvas; +import com.cloud.consoleproxy.vnc.RfbConstants; +import com.cloud.consoleproxy.vnc.VncClient; + +/** + * + * ConsoleProxyVncClient bridges a VNC engine with the front-end AJAX viewer + * + */ +public class ConsoleProxyVncClient extends ConsoleProxyClientBase { + private static final Logger s_logger = Logger.getLogger(ConsoleProxyVncClient.class); + + private static final int SHIFT_KEY_MASK = 64; + private static final int CTRL_KEY_MASK = 128; + private static final int META_KEY_MASK = 256; + private static final int ALT_KEY_MASK = 512; + + private static final int X11_KEY_SHIFT = 0xffe1; + private static final int X11_KEY_CTRL = 0xffe3; + private static final int X11_KEY_ALT = 0xffe9; + private static final int X11_KEY_META = 0xffe7; + + private VncClient client; + private Thread worker; + private boolean workerDone = false; + + private int lastModifierStates = 0; + private int lastPointerMask = 0; + + public ConsoleProxyVncClient() { + } + + public boolean isHostConnected() { + if(client != null) + return client.isHostConnected(); + + return false; + } + + @Override + public boolean isFrontEndAlive() { + if(workerDone || System.currentTimeMillis() - getClientLastFrontEndActivityTime() > ConsoleProxy.VIEWER_LINGER_SECONDS*1000) { + s_logger.info("Front end has been idle for too long"); + return false; + } + return true; + } + + @Override + public void initClient(ConsoleProxyClientParam param) { + setClientParam(param); + + client = new VncClient(this); + worker = new Thread(new Runnable() { + public void run() { + String tunnelUrl = getClientParam().getClientTunnelUrl(); + String tunnelSession = getClientParam().getClientTunnelSession(); + + for(int i = 0; i < 15; i++) { + try { + if(tunnelUrl != null && !tunnelUrl.isEmpty() && tunnelSession != null && !tunnelSession.isEmpty()) { + URI uri = new URI(tunnelUrl); + s_logger.info("Connect to VNC server via tunnel. url: " + tunnelUrl + ", session: " + tunnelSession); + + ConsoleProxy.ensureRoute(uri.getHost()); + client.connectTo( + uri.getHost(), uri.getPort(), + uri.getPath() + "?" + uri.getQuery(), + tunnelSession, "https".equalsIgnoreCase(uri.getScheme()), + getClientHostPassword()); + } else { + s_logger.info("Connect to VNC server directly. host: " + getClientHostAddress() + ", port: " + getClientHostPort()); + ConsoleProxy.ensureRoute(getClientHostAddress()); + client.connectTo(getClientHostAddress(), getClientHostPort(), getClientHostPassword()); + } + } catch (UnknownHostException e) { + s_logger.error("Unexpected exception (will retry until timeout)", e); + } catch (IOException e) { + s_logger.error("Unexpected exception (will retry until timeout) ", e); + } catch (Throwable e) { + s_logger.error("Unexpected exception (will retry until timeout) ", e); + } + + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + + if(tunnelUrl != null && !tunnelUrl.isEmpty() && tunnelSession != null && !tunnelSession.isEmpty()) { + ConsoleProxyAuthenticationResult authResult = ConsoleProxy.reAuthenticationExternally(getClientParam()); + if(authResult != null && authResult.isSuccess()) { + if(authResult.getTunnelUrl() != null && !authResult.getTunnelUrl().isEmpty() && + authResult.getTunnelSession() != null && !authResult.getTunnelSession().isEmpty()) { + tunnelUrl = authResult.getTunnelUrl(); + tunnelSession = authResult.getTunnelSession(); + + s_logger.info("Reset XAPI session. url: " + tunnelUrl + ", session: " + tunnelSession); + } + } + } + } + + s_logger.info("Receiver thread stopped."); + workerDone = true; + client.getClientListener().onClientClose(); + } + }); + + worker.setDaemon(true); + worker.start(); + } + + @Override + public void closeClient() { + if(client != null) + client.shutdown(); + } + + @Override + public void onClientConnected() { + } + + public void onClientClose() { + s_logger.info("Received client close indication. remove viewer from map."); + + ConsoleProxy.removeViewer(this); + } + + @Override + public void onFramebufferUpdate(int x, int y, int w, int h) { + super.onFramebufferUpdate(x, y, w, h); + client.requestUpdate(false); + } + + public void sendClientRawKeyboardEvent(InputEventType event, int code, int modifiers) { + if(client == null) + return; + + updateFrontEndActivityTime(); + + switch(event) { + case KEY_DOWN : + sendModifierEvents(modifiers); + client.sendClientKeyboardEvent(RfbConstants.KEY_DOWN, code, 0); + break; + + case KEY_UP : + client.sendClientKeyboardEvent(RfbConstants.KEY_UP, code, 0); + sendModifierEvents(0); + break; + + case KEY_PRESS : + break; + + default : + assert(false); + break; + } + } + + public void sendClientMouseEvent(InputEventType event, int x, int y, int code, int modifiers) { + if(client == null) + return; + + updateFrontEndActivityTime(); + + if (event == InputEventType.MOUSE_DOWN) { + if (code == 2) { + lastPointerMask |= 4; + } else if (code == 0) { + lastPointerMask |= 1; + } + } + + if (event == InputEventType.MOUSE_UP) { + if (code == 2) { + lastPointerMask ^= 4; + } else if (code == 0) { + lastPointerMask ^= 1; + } + } + + sendModifierEvents(modifiers); + client.sendClientMouseEvent(lastPointerMask, x, y, code, modifiers); + if(lastPointerMask == 0) + sendModifierEvents(0); + } + + @Override + protected FrameBufferCanvas getFrameBufferCavas() { + if(client != null) + return client.getFrameBufferCanvas(); + return null; + } + + private void sendModifierEvents(int modifiers) { + if((modifiers & SHIFT_KEY_MASK) != (lastModifierStates & SHIFT_KEY_MASK)) + client.sendClientKeyboardEvent((modifiers & SHIFT_KEY_MASK) != 0 ? RfbConstants.KEY_DOWN : RfbConstants.KEY_UP, X11_KEY_SHIFT, 0); + + if((modifiers & CTRL_KEY_MASK) != (lastModifierStates & CTRL_KEY_MASK)) + client.sendClientKeyboardEvent((modifiers & CTRL_KEY_MASK) != 0 ? RfbConstants.KEY_DOWN : RfbConstants.KEY_UP, X11_KEY_CTRL, 0); + + if((modifiers & META_KEY_MASK) != (lastModifierStates & META_KEY_MASK)) + client.sendClientKeyboardEvent((modifiers & META_KEY_MASK) != 0 ? RfbConstants.KEY_DOWN : RfbConstants.KEY_UP, X11_KEY_META, 0); + + if((modifiers & ALT_KEY_MASK) != (lastModifierStates & ALT_KEY_MASK)) + client.sendClientKeyboardEvent((modifiers & ALT_KEY_MASK) != 0 ? RfbConstants.KEY_DOWN : RfbConstants.KEY_UP, X11_KEY_ALT, 0); + + lastModifierStates = modifiers; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/InputEventType.java b/services/console-proxy/server/src/com/cloud/consoleproxy/InputEventType.java new file mode 100644 index 00000000000..4a5aff16bcf --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/InputEventType.java @@ -0,0 +1,58 @@ +// 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; + +public enum InputEventType { + MOUSE_MOVE(1), + MOUSE_DOWN(2), + MOUSE_UP(3), + KEY_PRESS(4), + KEY_DOWN(5), + KEY_UP(6), + MOUSE_DBLCLICK(8); + + int eventCode; + private InputEventType(int eventCode) { + this.eventCode = eventCode; + } + + public int getEventCode() { + return eventCode; + } + + public static InputEventType fromEventCode(int eventCode) { + switch(eventCode) { + case 1 : + return MOUSE_MOVE; + case 2 : + return MOUSE_DOWN; + case 3 : + return MOUSE_UP; + case 4 : + return KEY_PRESS; + case 5 : + return KEY_DOWN; + case 6 : + return KEY_UP; + case 8 : + return MOUSE_DBLCLICK; + default : + break; + } + throw new IllegalArgumentException("Unsupport event code: " + eventCode); + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/util/ITileScanListener.java b/services/console-proxy/server/src/com/cloud/consoleproxy/util/ITileScanListener.java new file mode 100644 index 00000000000..2ff82d7242f --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/util/ITileScanListener.java @@ -0,0 +1,25 @@ +// 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.util; + +import java.awt.Rectangle; +import java.util.List; + +public interface ITileScanListener { + boolean onTileChange(Rectangle rowMergedRect, int row, int col); + void onRegionChange(List regionList); +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/util/ImageHelper.java b/services/console-proxy/server/src/com/cloud/consoleproxy/util/ImageHelper.java new file mode 100644 index 00000000000..bb7373e78b2 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/util/ImageHelper.java @@ -0,0 +1,32 @@ +// 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.util; + +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +public class ImageHelper { + public static byte[] jpegFromImage(BufferedImage image) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(128000); + javax.imageio.ImageIO.write(image, "jpg", bos); + + byte[] jpegBits = bos.toByteArray(); + bos.close(); + return jpegBits; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/util/Logger.java b/services/console-proxy/server/src/com/cloud/consoleproxy/util/Logger.java new file mode 100644 index 00000000000..f4357bd4bc4 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/util/Logger.java @@ -0,0 +1,223 @@ +// 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.util; + +// logger facility for dynamic switch between console logger used in Applet and log4j based logger +public class Logger { + private static LoggerFactory factory = null; + + public static final int LEVEL_TRACE = 1; + public static final int LEVEL_DEBUG = 2; + public static final int LEVEL_INFO = 3; + public static final int LEVEL_WARN = 4; + public static final int LEVEL_ERROR = 5; + + private Class clazz; + private Logger logger; + + private static int level = LEVEL_INFO; + + public static Logger getLogger(Class clazz) { + return new Logger(clazz); + } + + public static void setFactory(LoggerFactory f) { + factory = f; + } + + public static void setLevel(int l) { + level = l; + } + + public Logger(Class clazz) { + this.clazz = clazz; + } + + protected Logger() { + } + + public boolean isTraceEnabled() { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + return logger.isTraceEnabled(); + } + return level <= LEVEL_TRACE; + } + + public boolean isDebugEnabled() { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + return logger.isDebugEnabled(); + } + return level <= LEVEL_DEBUG; + } + + public boolean isInfoEnabled() { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + return logger.isInfoEnabled(); + } + return level <= LEVEL_INFO; + } + + public void trace(Object message) { + + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.trace(message); + } else { + if(level <= LEVEL_TRACE) + System.out.println(message); + } + } + + public void trace(Object message, Throwable exception) { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.trace(message, exception); + } else { + if(level <= LEVEL_TRACE) { + System.out.println(message); + if (exception != null) { + exception.printStackTrace(System.out); + } + } + } + } + + public void info(Object message) { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.info(message); + } else { + if(level <= LEVEL_INFO) + System.out.println(message); + } + } + + public void info(Object message, Throwable exception) { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.info(message, exception); + } else { + if(level <= LEVEL_INFO) { + System.out.println(message); + if (exception != null) { + exception.printStackTrace(System.out); + } + } + } + } + + public void debug(Object message) { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.debug(message); + } else { + if(level <= LEVEL_DEBUG) + System.out.println(message); + } + } + + public void debug(Object message, Throwable exception) { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.debug(message, exception); + } else { + if(level <= LEVEL_DEBUG) { + System.out.println(message); + if (exception != null) { + exception.printStackTrace(System.out); + } + } + } + } + + public void warn(Object message) { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.warn(message); + } else { + if(level <= LEVEL_WARN) + System.out.println(message); + } + } + + public void warn(Object message, Throwable exception) { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.warn(message, exception); + } else { + if(level <= LEVEL_WARN) { + System.out.println(message); + if (exception != null) { + exception.printStackTrace(System.out); + } + } + } + } + + public void error(Object message) { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.error(message); + } else { + if(level <= LEVEL_ERROR) + System.out.println(message); + } + } + + public void error(Object message, Throwable exception) { + if(factory != null) { + if(logger == null) + logger = factory.getLogger(clazz); + + logger.error(message, exception); + } else { + if(level <= LEVEL_ERROR) { + System.out.println(message); + if (exception != null) { + exception.printStackTrace(System.out); + } + } + } + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/util/LoggerFactory.java b/services/console-proxy/server/src/com/cloud/consoleproxy/util/LoggerFactory.java new file mode 100644 index 00000000000..121411adf16 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/util/LoggerFactory.java @@ -0,0 +1,21 @@ +// 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.util; + +public interface LoggerFactory { + Logger getLogger(Class clazz); +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/util/RawHTTP.java b/services/console-proxy/server/src/com/cloud/consoleproxy/util/RawHTTP.java new file mode 100644 index 00000000000..c77b5511270 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/util/RawHTTP.java @@ -0,0 +1,249 @@ +// 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.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.NoSuchProviderException; +import java.security.SecureRandom; +import java.security.cert.X509Certificate; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.net.SocketFactory; +import javax.net.ssl.SSLContext; +import javax.net.ssl.SSLSocket; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; + +// +// This file is originally from XenConsole with modifications +// + +/** + * Send an HTTP CONNECT or PUT request to a XenAPI host with a Session ID, + * return the connected socket and the Task ID. Used for tunnelling VNC + * connections and import/export operations. + */ +public final class RawHTTP { + private static final Logger s_logger = Logger.getLogger(RawHTTP.class); + + private static final Pattern END_PATTERN = Pattern.compile("^\r\n$"); + private static final Pattern HEADER_PATTERN = Pattern + .compile("^([A-Z_a-z0-9-]+):\\s*(.*)\r\n$"); + private static final Pattern HTTP_PATTERN = Pattern + .compile("^HTTP/\\d+\\.\\d+ (\\d*) (.*)\r\n$"); + + /** + * @uml.property name="command" + */ + private final String command; + /** + * @uml.property name="host" + */ + private final String host; + /** + * @uml.property name="port" + */ + private final int port; + /** + * @uml.property name="path" + */ + private final String path; + /** + * @uml.property name="session" + */ + private final String session; + /** + * @uml.property name="useSSL" + */ + private final boolean useSSL; + + /** + * @uml.property name="responseHeaders" + * @uml.associationEnd qualifier="group:java.lang.String java.lang.String" + */ + private final Map responseHeaders = new HashMap(); + + /** + * @uml.property name="ic" + */ + private InputStream ic; + /** + * @uml.property name="oc" + */ + private OutputStream oc; + /** + * @uml.property name="s" + */ + private Socket s; + + public InputStream getInputStream() { + return ic; + } + + public OutputStream getOutputStream() { + return oc; + } + + public Socket getSocket() { + return s; + } + + public RawHTTP(String command, String host, int port, String path, + String session, boolean useSSL) { + this.command = command; + this.host = host; + this.port = port; + this.path = path; + this.session = session; + this.useSSL = useSSL; + } + + private static final TrustManager[] trustAllCerts = new TrustManager[] { + new X509TrustManager() { + public X509Certificate[] getAcceptedIssuers() { + return null; + } + + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + } + }; + + private Socket _getSocket() throws IOException { + if (useSSL) { + SSLContext context = getClientSSLContext(); + if(context == null) + throw new IOException("Unable to setup SSL context"); + + SSLSocket ssl = null; + try { + context.init(null, trustAllCerts, new SecureRandom()); + SocketFactory factory = context.getSocketFactory(); + ssl = (SSLSocket) factory.createSocket(host, port); + /* ssl.setSSLParameters(context.getDefaultSSLParameters()); */ + } catch (IOException e) { + s_logger.error("IOException: " + e.getMessage(), e); + throw e; + } catch (KeyManagementException e) { + s_logger.error("KeyManagementException: " + e.getMessage(), e); + } + return ssl; + } else { + return new Socket(host, port); + } + } + + public Socket connect() throws IOException { + String[] headers = makeHeaders(); + s = _getSocket(); + try { + oc = s.getOutputStream(); + for (String header : headers) { + oc.write(header.getBytes()); + oc.write("\r\n".getBytes()); + } + oc.flush(); + ic = s.getInputStream(); + while (true) { + String line = readline(ic); + + Matcher m = END_PATTERN.matcher(line); + if (m.matches()) { + return s; + } + + m = HEADER_PATTERN.matcher(line); + if (m.matches()) { + responseHeaders.put(m.group(1), m.group(2)); + continue; + } + + m = HTTP_PATTERN.matcher(line); + if (m.matches()) { + String status_code = m.group(1); + String reason_phrase = m.group(2); + if (!"200".equals(status_code)) { + throw new IOException("HTTP status " + status_code + + " " + reason_phrase); + } + } else { + throw new IOException("Unknown HTTP line " + line); + } + } + } catch (IOException exn) { + s.close(); + throw exn; + } catch (RuntimeException exn) { + s.close(); + throw exn; + } + } + + public Map getResponseHeaders() { + return responseHeaders; + } + + private String[] makeHeaders() { + String[] headers = { String.format("%s %s HTTP/1.0", command, path), + String.format("Host: %s", host), + String.format("Cookie: session_id=%s", session), "" }; + return headers; + } + + private static String readline(InputStream ic) throws IOException { + String result = ""; + while (true) { + try { + int c = ic.read(); + + if (c == -1) { + return result; + } + result = result + (char) c; + if (c == 0x0a /* LF */) { + return result; + } + } catch (IOException e) { + ic.close(); + throw e; + } + } + } + + private SSLContext getClientSSLContext() { + SSLContext sslContext = null; + try { + sslContext = SSLContext.getInstance("SSL", "SunJSSE"); + } catch (NoSuchAlgorithmException e) { + s_logger.error("Unexpected exception ", e); + } catch (NoSuchProviderException e) { + s_logger.error("Unexpected exception ", e); + } + return sslContext; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/util/Region.java b/services/console-proxy/server/src/com/cloud/consoleproxy/util/Region.java new file mode 100644 index 00000000000..a5d1751d63b --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/util/Region.java @@ -0,0 +1,90 @@ +// 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.util; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.List; + +public class Region { + private Rectangle bound; + private List rectList; + + public Region() { + bound = new Rectangle(0, 0, 0, 0); + rectList = new ArrayList(); + } + + public Region(Rectangle rect) { + bound = new Rectangle(rect.x, rect.y, rect.width, rect.height); + rectList = new ArrayList(); + rectList.add(rect); + } + + public Rectangle getBound() { + return bound; + } + + public void clearBound() { + assert(rectList.size() == 0); + bound.x = bound.y = bound.width = bound.height = 0; + } + + public List getRectangles() { + return rectList; + } + + public boolean add(Rectangle rect) { + if(bound.isEmpty()) { + assert(rectList.size() == 0); + bound.x = rect.x; + bound.y = rect.y; + bound.width = rect.width; + bound.height = rect.height; + + rectList.add(rect); + return true; + } + + Rectangle rcInflated = new Rectangle(rect.x - 1, rect.y- 1, rect.width + 2, rect.height + 2); + if(!bound.intersects(rcInflated)) + return false; + + for(Rectangle r : rectList) { + if(r.intersects(rcInflated)) { + if(!r.contains(rect)) { + enlargeBound(rect); + rectList.add(rect); + return true; + } + } + } + return false; + } + + private void enlargeBound(Rectangle rect) { + int boundLeft = Math.min(bound.x, rect.x); + int boundTop = Math.min(bound.y, rect.y); + int boundRight = Math.max(bound.x + bound.width, rect.x + rect.width); + int boundBottom = Math.max(bound.y + bound.height, rect.y + rect.height); + + bound.x = boundLeft; + bound.y = boundTop; + bound.width = boundRight - boundLeft; + bound.height = boundBottom - boundTop; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/util/RegionClassifier.java b/services/console-proxy/server/src/com/cloud/consoleproxy/util/RegionClassifier.java new file mode 100644 index 00000000000..9faa04ea9eb --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/util/RegionClassifier.java @@ -0,0 +1,58 @@ +// 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.util; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.List; + +public class RegionClassifier { + private List regionList; + + public RegionClassifier() { + regionList = new ArrayList(); + } + + public void add(Rectangle rect) { + boolean newRegion = true; + Rectangle rcInflated = new Rectangle(rect.x - 1, rect.y - 1, rect.width + 2, rect.height + 2); + for(Region region : regionList) { + if(region.getBound().intersects(rcInflated)) { + newRegion = false; + break; + } + } + + if(newRegion) { + regionList.add(new Region(rect)); + } else { + for(Region region : regionList) { + if(region.add(rect)) + return; + } + regionList.add(new Region(rect)); + } + } + + public List getRegionList() { + return regionList; + } + + public void clear() { + regionList.clear(); + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/util/TileInfo.java b/services/console-proxy/server/src/com/cloud/consoleproxy/util/TileInfo.java new file mode 100644 index 00000000000..933a55a55b0 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/util/TileInfo.java @@ -0,0 +1,55 @@ +// 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.util; + +import java.awt.Rectangle; + +public class TileInfo { + private int row; + private int col; + private Rectangle tileRect; + + public TileInfo(int row, int col, Rectangle tileRect) { + this.row = row; + this.col = col; + this.tileRect = tileRect; + } + + public int getRow() { + return row; + } + + public void setRow(int row) { + this.row = row; + } + + public int getCol() { + return col; + } + + public void setCol(int col) { + this.col = col; + } + + public Rectangle getTileRect() { + return tileRect; + } + + public void setTileRect(Rectangle tileRect) { + this.tileRect = tileRect; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/util/TileTracker.java b/services/console-proxy/server/src/com/cloud/consoleproxy/util/TileTracker.java new file mode 100644 index 00000000000..b3facb2f88f --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/util/TileTracker.java @@ -0,0 +1,269 @@ +// 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.util; + +import java.awt.Rectangle; +import java.util.ArrayList; +import java.util.List; + +public class TileTracker { + + // 2 dimension tile status snapshot, a true value means the corresponding tile has been invalidated + private boolean[][] snapshot; + + private int tileWidth = 0; + private int tileHeight = 0; + private int trackWidth = 0; + private int trackHeight = 0; + + public TileTracker() { + } + + public int getTileWidth() { + return tileWidth; + } + + public void setTileWidth(int tileWidth) { + this.tileWidth = tileWidth; + } + + public int getTileHeight() { + return tileHeight; + } + + public void setTileHeight(int tileHeight) { + this.tileHeight = tileHeight; + } + + public int getTrackWidth() { + return trackWidth; + } + + public void setTrackWidth(int trackWidth) { + this.trackWidth = trackWidth; + } + + public int getTrackHeight() { + return trackHeight; + } + + public void setTrackHeight(int trackHeight) { + this.trackHeight = trackHeight; + } + + public void initTracking(int tileWidth, int tileHeight, int trackWidth, int trackHeight) { + assert(tileWidth > 0); + assert(tileHeight > 0); + assert(trackWidth > 0); + assert(trackHeight > 0); + assert(tileWidth <= trackWidth); + assert(tileHeight <= trackHeight); + + this.tileWidth = tileWidth; + this.tileHeight = tileHeight; + this.trackWidth = trackWidth; + this.trackHeight = trackHeight; + + int cols = getTileCols(); + int rows = getTileRows(); + snapshot = new boolean[rows][cols]; + for(int i = 0; i < rows; i++) + for(int j = 0; j < cols; j++) + snapshot[i][j] = false; + } + + public synchronized void resize(int trackWidth, int trackHeight) { + assert(tileWidth > 0); + assert(tileHeight > 0); + assert(trackWidth > 0); + assert(trackHeight > 0); + + this.trackWidth = trackWidth; + this.trackHeight = trackHeight; + + int cols = getTileCols(); + int rows = getTileRows(); + snapshot = new boolean[rows][cols]; + for(int i = 0; i < rows; i++) + for(int j = 0; j < cols; j++) + snapshot[i][j] = true; + } + + public void invalidate(Rectangle rect) { + setTileFlag(rect, true); + } + + public void validate(Rectangle rect) { + setTileFlag(rect, false); + } + + public List scan(boolean init) { + List l = new ArrayList(); + + synchronized(this) { + for(int i = 0; i < getTileRows(); i++) { + for(int j = 0; j < getTileCols(); j++) { + if(init || snapshot[i][j]) { + Rectangle rect = new Rectangle(); + rect.y = i*tileHeight; + rect.x = j*tileWidth; + rect.width = Math.min(trackWidth - rect.x, tileWidth); + rect.height = Math.min(trackHeight - rect.y, tileHeight); + + l.add(new TileInfo(i, j, rect)); + snapshot[i][j] = false; + } + } + } + + return l; + } + } + + public boolean hasFullCoverage() { + synchronized(this) { + for(int i = 0; i < getTileRows(); i++) { + for(int j = 0; j < getTileCols(); j++) { + if(!snapshot[i][j]) + return false; + } + } + } + return true; + } + + + + public void initCoverageTest() { + synchronized(this) { + for(int i = 0; i < getTileRows(); i++) { + for(int j = 0; j < getTileCols(); j++) { + snapshot[i][j] = false; + } + } + } + } + + // listener will be called while holding the object lock, use it + // with care to avoid deadlock condition being formed + public synchronized void scan(int nStartRow, int nStartCol, ITileScanListener listener) { + assert(listener != null); + + int cols = getTileCols(); + int rows = getTileRows(); + + nStartRow = nStartRow % rows; + nStartCol = nStartCol % cols; + + int nPos = nStartRow*cols + nStartCol; + int nUnits = rows*cols; + int nStartPos = nPos; + int nRow; + int nCol; + do { + nRow = nPos / cols; + nCol = nPos % cols; + + if(snapshot[nRow][nCol]) { + int nEndCol = nCol; + for(; nEndCol < cols && snapshot[nRow][nEndCol]; nEndCol++) { + snapshot[nRow][nEndCol] = false; + } + + Rectangle rect = new Rectangle(); + rect.y = nRow*tileHeight; + rect.height = tileHeight; + rect.x = nCol*tileWidth; + rect.width = (nEndCol - nCol)*tileWidth; + + if(!listener.onTileChange(rect, nRow, nEndCol)) + break; + } + + nPos = (nPos + 1) % nUnits; + } while(nPos != nStartPos); + } + + public void capture(ITileScanListener listener) { + assert(listener != null); + + int cols = getTileCols(); + int rows = getTileRows(); + + RegionClassifier classifier = new RegionClassifier(); + int left, top, right, bottom; + + synchronized(this) { + for(int i = 0; i < rows; i++) { + top = i*tileHeight; + bottom = Math.min(top + tileHeight, trackHeight); + for(int j = 0; j < cols; j++) { + left = j*tileWidth; + right = Math.min(left + tileWidth, trackWidth); + + if(snapshot[i][j]) { + snapshot[i][j] = false; + classifier.add(new Rectangle(left, top, right - left, bottom - top)); + } + } + } + } + listener.onRegionChange(classifier.getRegionList()); + } + + private synchronized void setTileFlag(Rectangle rect, boolean flag) { + int nStartTileRow; + int nStartTileCol; + int nEndTileRow; + int nEndTileCol; + + int cols = getTileCols(); + int rows = getTileRows(); + + if(rect != null) { + nStartTileRow = Math.min(getTileYPos(rect.y), rows - 1); + nStartTileCol = Math.min(getTileXPos(rect.x), cols - 1); + nEndTileRow = Math.min(getTileYPos(rect.y + rect.height - 1), rows -1); + nEndTileCol = Math.min(getTileXPos(rect.x + rect.width - 1), cols -1); + } else { + nStartTileRow = 0; + nStartTileCol = 0; + nEndTileRow = rows - 1; + nEndTileCol = cols - 1; + } + + for(int i = nStartTileRow; i <= nEndTileRow; i++) + for(int j = nStartTileCol; j <= nEndTileCol; j++) + snapshot[i][j] = flag; + } + + private int getTileRows() { + return (trackHeight + tileHeight - 1) / tileHeight; + } + + private int getTileCols() { + return (trackWidth + tileWidth - 1) / tileWidth; + } + + private int getTileXPos(int x) { + return x / tileWidth; + } + + public int getTileYPos(int y) { + return y / tileHeight; + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/BufferedImageCanvas.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/BufferedImageCanvas.java new file mode 100644 index 00000000000..f2fb4bb6b69 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/BufferedImageCanvas.java @@ -0,0 +1,150 @@ +// 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.vnc; + +import java.awt.Canvas; +import java.awt.Color; +import java.awt.Graphics; +import java.awt.Graphics2D; +import java.awt.Image; +import java.awt.Rectangle; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.util.List; + +import com.cloud.consoleproxy.util.ImageHelper; +import com.cloud.consoleproxy.util.TileInfo; + +/** + * A BuffereImageCanvas component represents frame buffer image on + * the screen. It also notifies its subscribers when screen is repainted. + */ +public class BufferedImageCanvas extends Canvas implements FrameBufferCanvas { + private static final long serialVersionUID = 1L; + + // Offline screen buffer + private BufferedImage offlineImage; + + // Cached Graphics2D object for offline screen buffer + private Graphics2D graphics; + + private PaintNotificationListener listener; + + public BufferedImageCanvas(PaintNotificationListener listener, int width, int height) { + super(); + this.listener = listener; + + setBackground(Color.black); + + setFocusable(true); + + // Don't intercept TAB key + setFocusTraversalKeysEnabled(false); + + setCanvasSize(width, height); + } + + public void setCanvasSize(int width, int height) { + this.offlineImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + graphics = offlineImage.createGraphics(); + + setSize(offlineImage.getWidth(), offlineImage.getHeight()); + } + + @Override + public void update(Graphics g) { + // Call paint() directly, without clearing screen first + paint(g); + } + + @Override + public void paint(Graphics g) { + // Only part of image, requested with repaint(Rectangle), will be + // painted on screen. + synchronized (offlineImage) { + g.drawImage(offlineImage, 0, 0, this); + } + // Notify server that update is painted on screen + listener.imagePaintedOnScreen(); + } + + public BufferedImage getOfflineImage() { + return offlineImage; + } + + public Graphics2D getOfflineGraphics() { + return graphics; + } + + public void copyTile(Graphics2D g, int x, int y, Rectangle rc) { + synchronized (offlineImage) { + g.drawImage(offlineImage, x, y, x + rc.width, y + rc.height, rc.x, rc.y, rc.x + rc.width, rc.y + rc.height, null); + } + } + + @Override + public Image getFrameBufferScaledImage(int width, int height) { + if (offlineImage != null) + return offlineImage.getScaledInstance(width, height, Image.SCALE_DEFAULT); + return null; + } + + @Override + public byte[] getFrameBufferJpeg() { + int width = 800; + int height = 600; + + width = offlineImage.getWidth(); + height = offlineImage.getHeight(); + + BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR); + Graphics2D g = bufferedImage.createGraphics(); + synchronized (offlineImage) { + g.drawImage(offlineImage, 0, 0, width, height, 0, 0, width, height, null); + } + + byte[] imgBits = null; + try { + imgBits = ImageHelper.jpegFromImage(bufferedImage); + } catch (IOException e) { + } + return imgBits; + } + + @Override + public byte[] getTilesMergedJpeg(List tileList, int tileWidth, int tileHeight) { + int width = Math.max(tileWidth, tileWidth * tileList.size()); + BufferedImage bufferedImage = new BufferedImage(width, tileHeight, BufferedImage.TYPE_3BYTE_BGR); + Graphics2D g = bufferedImage.createGraphics(); + + synchronized (offlineImage) { + int i = 0; + for (TileInfo tile : tileList) { + Rectangle rc = tile.getTileRect(); + g.drawImage(offlineImage, i * tileWidth, 0, i * tileWidth + rc.width, rc.height, rc.x, rc.y, rc.x + rc.width, rc.y + rc.height, null); + i++; + } + } + + byte[] imgBits = null; + try { + imgBits = ImageHelper.jpegFromImage(bufferedImage); + } catch (IOException e) { + } + return imgBits; + } +} \ No newline at end of file diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/FrameBufferCanvas.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/FrameBufferCanvas.java new file mode 100644 index 00000000000..dcf11461360 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/FrameBufferCanvas.java @@ -0,0 +1,30 @@ +// 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.vnc; + +import java.awt.Image; +import java.util.List; + +import com.cloud.consoleproxy.util.TileInfo; + +public interface FrameBufferCanvas { + Image getFrameBufferScaledImage(int width, int height); + + public byte[] getFrameBufferJpeg(); + + public byte[] getTilesMergedJpeg(List tileList, int tileWidth, int tileHeight); +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/FrameBufferUpdateListener.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/FrameBufferUpdateListener.java new file mode 100644 index 00000000000..b8527c5261a --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/FrameBufferUpdateListener.java @@ -0,0 +1,26 @@ +// 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.vnc; + +public interface FrameBufferUpdateListener { + + /** + * Notify listener, that frame buffer update packet is received, so client + * is permitted (but not obligated) to ask server to send another update. + */ + void frameBufferPacketReceived(); +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/PaintNotificationListener.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/PaintNotificationListener.java new file mode 100644 index 00000000000..aaefacb99f2 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/PaintNotificationListener.java @@ -0,0 +1,27 @@ +// 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.vnc; + +public interface PaintNotificationListener { + + /** + * Notify subscriber that screen is updated, so client can send another + * frame buffer update request to server. + */ + void imagePaintedOnScreen(); + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/RfbConstants.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/RfbConstants.java new file mode 100644 index 00000000000..18bf47ec1d4 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/RfbConstants.java @@ -0,0 +1,82 @@ +// 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.vnc; + +import java.nio.charset.Charset; + +public interface RfbConstants { + + public static final String RFB_PROTOCOL_VERSION_MAJOR = "RFB 003."; + // public static final String VNC_PROTOCOL_VERSION_MINOR = "003"; + public static final String VNC_PROTOCOL_VERSION_MINOR = "003"; + public static final String RFB_PROTOCOL_VERSION = RFB_PROTOCOL_VERSION_MAJOR + VNC_PROTOCOL_VERSION_MINOR; + + /** + * Server message types. + */ + final static int SERVER_FRAMEBUFFER_UPDATE = 0, SERVER_SET_COLOURMAP_ENTRIES = 1, SERVER_BELL = 2, SERVER_CUT_TEXT = 3; + + /** + * Client message types. + */ + public static final int CLIENT_SET_PIXEL_FORMAT = 0, CLIENT_FIX_COLOURMAP_ENTRIES = 1, CLIENT_SET_ENCODINGS = 2, CLIENT_FRAMEBUFFER_UPDATE_REQUEST = 3, CLIENT_KEYBOARD_EVENT = 4, + CLIENT_POINTER_EVENT = 5, CLIENT_CUT_TEXT = 6; + + /** + * Server authorization type + */ + public final static int CONNECTION_FAILED = 0, NO_AUTH = 1, VNC_AUTH = 2; + + /** + * Server authorization reply. + */ + public final static int VNC_AUTH_OK = 0, VNC_AUTH_FAILED = 1, VNC_AUTH_TOO_MANY = 2; + + /** + * Encodings. + */ + public final static int ENCODING_RAW = 0, ENCODING_COPY_RECT = 1, ENCODING_RRE = 2, ENCODING_CO_RRE = 4, ENCODING_HEXTILE = 5, ENCODING_ZRLE = 16; + + /** + * Pseudo-encodings. + */ + public final static int ENCODING_CURSOR = -239 /* 0xFFFFFF11 */, ENCODING_DESKTOP_SIZE = -223 /* 0xFFFFFF21 */; + + /** + * Encodings, which we support. + */ + public final static int[] SUPPORTED_ENCODINGS_ARRAY = { ENCODING_RAW, ENCODING_COPY_RECT, ENCODING_DESKTOP_SIZE }; + + /** + * Frame buffer update request type: update of whole screen or partial + * update. + */ + public static final int FRAMEBUFFER_FULL_UPDATE_REQUEST = 0, FRAMEBUFFER_INCREMENTAL_UPDATE_REQUEST = 1; + + public static final int KEY_UP = 0, KEY_DOWN = 1; + + public static final int LITTLE_ENDIAN = 0, BIG_ENDIAN = 1; + + public static final int EXCLUSIVE_ACCESS = 0, SHARED_ACCESS = 1; + + public static final int PALETTE = 0, TRUE_COLOR = 1; + + /** + * Default charset to use when communicating with server. + */ + public static final Charset CHARSET = Charset.availableCharsets().get("US-ASCII"); +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncClient.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncClient.java new file mode 100644 index 00000000000..194eec17414 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncClient.java @@ -0,0 +1,451 @@ +// 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.vnc; + +import java.awt.Frame; +import java.awt.ScrollPane; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.net.Socket; +import java.net.UnknownHostException; +import java.security.spec.KeySpec; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.DESKeySpec; + +import com.cloud.consoleproxy.ConsoleProxyClientListener; +import com.cloud.consoleproxy.util.Logger; +import com.cloud.consoleproxy.util.RawHTTP; +import com.cloud.consoleproxy.vnc.packet.client.KeyboardEventPacket; +import com.cloud.consoleproxy.vnc.packet.client.MouseEventPacket; + +public class VncClient { + private static final Logger s_logger = Logger.getLogger(VncClient.class); + + private Socket socket; + private DataInputStream is; + private DataOutputStream os; + + private VncScreenDescription screen = new VncScreenDescription(); + + private VncClientPacketSender sender; + private VncServerPacketReceiver receiver; + + private boolean noUI = false; + private ConsoleProxyClientListener clientListener = null; + + public static void main(String args[]) { + if (args.length < 3) { + printHelpMessage(); + System.exit(1); + } + + String host = args[0]; + String port = args[1]; + String password = args[2]; + + try { + new VncClient(host, Integer.parseInt(port), password, false, null); + } catch (NumberFormatException e) { + s_logger.error("Incorrect VNC server port number: " + port + "."); + System.exit(1); + } catch (UnknownHostException e) { + s_logger.error("Incorrect VNC server host name: " + host + "."); + System.exit(1); + } catch (IOException e) { + s_logger.error("Cannot communicate with VNC server: " + e.getMessage()); + System.exit(1); + } catch (Throwable e) { + s_logger.error("An error happened: " + e.getMessage()); + System.exit(1); + } + System.exit(0); + } + + private static void printHelpMessage() { + /* LOG */s_logger.info("Usage: HOST PORT PASSWORD."); + } + + public VncClient(ConsoleProxyClientListener clientListener) { + this.noUI = true; + this.clientListener = clientListener; + } + + public VncClient(String host, int port, String password, boolean noUI, ConsoleProxyClientListener clientListener) throws UnknownHostException, IOException { + + this.noUI = noUI; + this.clientListener = clientListener; + connectTo(host, port, password); + } + + public void shutdown() { + if (sender != null) + sender.closeConnection(); + + if (receiver != null) + receiver.closeConnection(); + + if (is != null) { + try { + is.close(); + } catch (Throwable e) { + } + } + + if (os != null) { + try { + os.close(); + } catch (Throwable e) { + } + } + + if (socket != null) { + try { + socket.close(); + } catch (Throwable e) { + } + } + } + + public ConsoleProxyClientListener getClientListener() { + return clientListener; + } + + public void connectTo(String host, int port, String path, String session, boolean useSSL, String sid) throws UnknownHostException, IOException { + if (port < 0) { + if (useSSL) + port = 443; + else + port = 80; + } + + RawHTTP tunnel = new RawHTTP("CONNECT", host, port, path, session, useSSL); + this.socket = tunnel.connect(); + doConnect(sid); + } + + public void connectTo(String host, int port, String password) throws UnknownHostException, IOException { + // Connect to server + s_logger.info("Connecting to VNC server " + host + ":" + port + "..."); + this.socket = new Socket(host, port); + doConnect(password); + } + + private void doConnect(String password) throws IOException { + is = new DataInputStream(socket.getInputStream()); + os = new DataOutputStream(socket.getOutputStream()); + + // Initialize connection + handshake(); + authenticate(password); + initialize(); + + s_logger.info("Connecting to VNC server succeeded, start session"); + + // Run client-to-server packet sender + sender = new VncClientPacketSender(os, screen, this); + + // Create buffered image canvas + BufferedImageCanvas canvas = new BufferedImageCanvas(sender, screen.getFramebufferWidth(), screen.getFramebufferHeight()); + + // Subscribe packet sender to various events + canvas.addMouseListener(sender); + canvas.addMouseMotionListener(sender); + canvas.addKeyListener(sender); + + Frame frame = null; + if (!noUI) + frame = createVncClientMainWindow(canvas, screen.getDesktopName()); + + new Thread(sender).start(); + + // Run server-to-client packet receiver + receiver = new VncServerPacketReceiver(is, canvas, screen, this, sender, clientListener); + try { + receiver.run(); + } finally { + if (frame != null) { + frame.setVisible(false); + frame.dispose(); + } + this.shutdown(); + } + } + + private Frame createVncClientMainWindow(BufferedImageCanvas canvas, String title) { + // Create AWT windows + final Frame frame = new Frame(title + " - VNCle"); + + // Use scrolling pane to support screens, which are larger than ours + ScrollPane scroller = new ScrollPane(ScrollPane.SCROLLBARS_AS_NEEDED); + scroller.add(canvas); + scroller.setSize(screen.getFramebufferWidth(), screen.getFramebufferHeight()); + + frame.add(scroller); + frame.pack(); + frame.setVisible(true); + + frame.addWindowListener(new WindowAdapter() { + public void windowClosing(WindowEvent evt) { + frame.setVisible(false); + shutdown(); + } + }); + + return frame; + } + + /** + * Handshake with VNC server. + */ + private void handshake() throws IOException { + + // Read protocol version + byte[] buf = new byte[12]; + is.readFully(buf); + String rfbProtocol = new String(buf); + + // Server should use RFB protocol 3.x + if (!rfbProtocol.contains(RfbConstants.RFB_PROTOCOL_VERSION_MAJOR)) { + s_logger.error("Cannot handshake with VNC server. Unsupported protocol version: \"" + rfbProtocol + "\"."); + throw new RuntimeException("Cannot handshake with VNC server. Unsupported protocol version: \"" + rfbProtocol + "\"."); + } + + // Send response: we support RFB 3.3 only + String ourProtocolString = RfbConstants.RFB_PROTOCOL_VERSION + "\n"; + os.write(ourProtocolString.getBytes()); + os.flush(); + } + + /** + * VNC authentication. + */ + private void authenticate(String password) throws IOException { + // Read security type + int authType = is.readInt(); + + switch (authType) { + case RfbConstants.CONNECTION_FAILED: { + // Server forbids to connect. Read reason and throw exception + + int length = is.readInt(); + byte[] buf = new byte[length]; + is.readFully(buf); + String reason = new String(buf, RfbConstants.CHARSET); + + s_logger.error("Authentication to VNC server is failed. Reason: " + reason); + throw new RuntimeException("Authentication to VNC server is failed. Reason: " + reason); + } + + case RfbConstants.NO_AUTH: { + // Client can connect without authorization. Nothing to do. + break; + } + + case RfbConstants.VNC_AUTH: { + s_logger.info("VNC server requires password authentication"); + doVncAuth(password); + break; + } + + default: + s_logger.error("Unsupported VNC protocol authorization scheme, scheme code: " + authType + "."); + throw new RuntimeException("Unsupported VNC protocol authorization scheme, scheme code: " + authType + "."); + } + } + + /** + * Encode client password and send it to server. + */ + private void doVncAuth(String password) throws IOException { + + // Read challenge + byte[] challenge = new byte[16]; + is.readFully(challenge); + + // Encode challenge with password + byte[] response; + try { + response = encodePassword(challenge, password); + } catch (Exception e) { + s_logger.error("Cannot encrypt client password to send to server: " + e.getMessage()); + throw new RuntimeException("Cannot encrypt client password to send to server: " + e.getMessage()); + } + + // Send encoded challenge + os.write(response); + os.flush(); + + // Read security result + int authResult = is.readInt(); + + switch (authResult) { + case RfbConstants.VNC_AUTH_OK: { + // Nothing to do + break; + } + + case RfbConstants.VNC_AUTH_TOO_MANY: + s_logger.error("Connection to VNC server failed: too many wrong attempts."); + throw new RuntimeException("Connection to VNC server failed: too many wrong attempts."); + + case RfbConstants.VNC_AUTH_FAILED: + s_logger.error("Connection to VNC server failed: wrong password."); + throw new RuntimeException("Connection to VNC server failed: wrong password."); + + default: + s_logger.error("Connection to VNC server failed, reason code: " + authResult); + throw new RuntimeException("Connection to VNC server failed, reason code: " + authResult); + } + } + + /** + * Encode password using DES encryption with given challenge. + * + * @param challenge + * a random set of bytes. + * @param password + * a password + * @return DES hash of password and challenge + */ + public byte[] encodePassword(byte[] challenge, String password) throws Exception { + // VNC password consist of up to eight ASCII characters. + byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 }; // Padding + byte[] passwordAsciiBytes = password.getBytes(RfbConstants.CHARSET); + System.arraycopy(passwordAsciiBytes, 0, key, 0, Math.min(password.length(), 8)); + + // Flip bytes (reverse bits) in key + for (int i = 0; i < key.length; i++) { + key[i] = flipByte(key[i]); + } + + KeySpec desKeySpec = new DESKeySpec(key); + SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES"); + SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec); + Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + + byte[] response = cipher.doFinal(challenge); + return response; + } + + /** + * Reverse bits in byte, so least significant bit will be most significant + * bit. E.g. 01001100 will become 00110010. + * + * 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; + } + + private void initialize() throws IOException { + // Send client initialization message + { + // Send shared flag + os.writeByte(RfbConstants.EXCLUSIVE_ACCESS); + os.flush(); + } + + // Read server initialization message + { + // Read frame buffer size + int framebufferWidth = is.readUnsignedShort(); + int framebufferHeight = is.readUnsignedShort(); + screen.setFramebufferSize(framebufferWidth, framebufferHeight); + if (clientListener != null) + clientListener.onFramebufferSizeChange(framebufferWidth, framebufferHeight); + } + + // Read pixel format + { + int bitsPerPixel = is.readUnsignedByte(); + int depth = is.readUnsignedByte(); + + int bigEndianFlag = is.readUnsignedByte(); + int trueColorFlag = is.readUnsignedByte(); + + int redMax = is.readUnsignedShort(); + int greenMax = is.readUnsignedShort(); + int blueMax = is.readUnsignedShort(); + + int redShift = is.readUnsignedByte(); + int greenShift = is.readUnsignedByte(); + int blueShift = is.readUnsignedByte(); + + // Skip padding + is.skipBytes(3); + + screen.setPixelFormat(bitsPerPixel, depth, bigEndianFlag, trueColorFlag, redMax, greenMax, blueMax, redShift, greenShift, blueShift); + } + + // Read desktop name + { + int length = is.readInt(); + byte buf[] = new byte[length]; + is.readFully(buf); + String desktopName = new String(buf, RfbConstants.CHARSET); + screen.setDesktopName(desktopName); + } + } + + public FrameBufferCanvas getFrameBufferCanvas() { + if (receiver != null) + return receiver.getCanvas(); + + return null; + } + + public void requestUpdate(boolean fullUpdate) { + if (fullUpdate) + sender.requestFullScreenUpdate(); + else + sender.imagePaintedOnScreen(); + } + + public void sendClientKeyboardEvent(int event, int code, int modifiers) { + sender.sendClientPacket(new KeyboardEventPacket(event, code)); + } + + public void sendClientMouseEvent(int event, int x, int y, int code, int modifiers) { + sender.sendClientPacket(new MouseEventPacket(event, x, y)); + } + + public boolean isHostConnected() { + return receiver != null && receiver.isConnectionAlive(); + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncClientPacketSender.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncClientPacketSender.java new file mode 100644 index 00000000000..d27b76d3468 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncClientPacketSender.java @@ -0,0 +1,258 @@ +// 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.vnc; + +import java.awt.event.KeyEvent; +import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; +import java.io.DataOutputStream; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.TimeUnit; + +import com.cloud.consoleproxy.util.Logger; +import com.cloud.consoleproxy.vnc.packet.client.ClientPacket; +import com.cloud.consoleproxy.vnc.packet.client.FramebufferUpdateRequestPacket; +import com.cloud.consoleproxy.vnc.packet.client.KeyboardEventPacket; +import com.cloud.consoleproxy.vnc.packet.client.MouseEventPacket; +import com.cloud.consoleproxy.vnc.packet.client.SetEncodingsPacket; +import com.cloud.consoleproxy.vnc.packet.client.SetPixelFormatPacket; + +public class VncClientPacketSender implements Runnable, PaintNotificationListener, KeyListener, MouseListener, MouseMotionListener, FrameBufferUpdateListener { + private static final Logger s_logger = Logger.getLogger(VncClientPacketSender.class); + + // Queue for outgoing packets + private final BlockingQueue queue = new ArrayBlockingQueue(30); + + private final DataOutputStream os; + private final VncScreenDescription screen; + private final VncClient vncConnection; + + private boolean connectionAlive = true; + + // Don't send update request again until we receive next frame buffer update + private boolean updateRequestSent = false; + + public VncClientPacketSender(DataOutputStream os, VncScreenDescription screen, VncClient vncConnection) { + this.os = os; + this.screen = screen; + this.vncConnection = vncConnection; + + sendSetPixelFormat(); + sendSetEncodings(); + requestFullScreenUpdate(); + } + + public void sendClientPacket(ClientPacket packet) { + queue.add(packet); + } + + @Override + public void run() { + try { + while (connectionAlive) { + ClientPacket packet = queue.poll(1, TimeUnit.SECONDS); + if (packet != null) { + packet.write(os); + os.flush(); + } + } + } catch (Throwable e) { + s_logger.error("Unexpected exception: ", e); + if (connectionAlive) { + closeConnection(); + vncConnection.shutdown(); + } + } + } + + private void sendSetEncodings() { + queue.add(new SetEncodingsPacket(RfbConstants.SUPPORTED_ENCODINGS_ARRAY)); + } + + private void sendSetPixelFormat() { + if (!screen.isRGB888_32_LE()) { + queue.add(new SetPixelFormatPacket(screen, 32, 24, RfbConstants.LITTLE_ENDIAN, RfbConstants.TRUE_COLOR, 255, 255, 255, 16, 8, 0)); + } + } + + public void closeConnection() { + connectionAlive = false; + } + + public void requestFullScreenUpdate() { + queue.add(new FramebufferUpdateRequestPacket(RfbConstants.FRAMEBUFFER_FULL_UPDATE_REQUEST, 0, 0, screen.getFramebufferWidth(), screen.getFramebufferHeight())); + updateRequestSent = true; + } + + @Override + public void imagePaintedOnScreen() { + if (!updateRequestSent) { + queue.add(new FramebufferUpdateRequestPacket(RfbConstants.FRAMEBUFFER_INCREMENTAL_UPDATE_REQUEST, 0, 0, screen.getFramebufferWidth(), screen.getFramebufferHeight())); + updateRequestSent = true; + } + } + + @Override + public void frameBufferPacketReceived() { + updateRequestSent = false; + } + + @Override + public void mouseDragged(MouseEvent e) { + queue.add(new MouseEventPacket(mapAwtModifiersToVncButtonMask(e.getModifiersEx()), e.getX(), e.getY())); + } + + @Override + public void mouseMoved(MouseEvent e) { + queue.add(new MouseEventPacket(mapAwtModifiersToVncButtonMask(e.getModifiersEx()), e.getX(), e.getY())); + } + + @Override + public void mouseClicked(MouseEvent e) { + // Nothing to do + } + + @Override + public void mousePressed(MouseEvent e) { + queue.add(new MouseEventPacket(mapAwtModifiersToVncButtonMask(e.getModifiersEx()), e.getX(), e.getY())); + } + + @Override + public void mouseReleased(MouseEvent e) { + queue.add(new MouseEventPacket(mapAwtModifiersToVncButtonMask(e.getModifiersEx()), e.getX(), e.getY())); + } + + @Override + public void mouseEntered(MouseEvent e) { + // Nothing to do + } + + @Override + public void mouseExited(MouseEvent e) { + // Nothing to do + } + + /** + * Current state of buttons 1 to 8 are represented by bits 0 to 7 of + * button-mask respectively, 0 meaning up, 1 meaning down (pressed). On a + * conventional mouse, buttons 1, 2 and 3 correspond to the left, middle and + * right buttons on the mouse. On a wheel mouse, each step of the wheel + * upwards is represented by a press and release of button 4, and each step + * downwards is represented by a press and release of button 5. + * + * @param modifiers + * extended modifiers from AWT mouse event + * @return VNC mouse button mask + */ + public static int mapAwtModifiersToVncButtonMask(int modifiers) { + int mask = (((modifiers & MouseEvent.BUTTON1_DOWN_MASK) != 0) ? 0x1 : 0) | (((modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0) ? 0x2 : 0) + | (((modifiers & MouseEvent.BUTTON3_DOWN_MASK) != 0) ? 0x4 : 0); + return mask; + } + + @Override + public void keyTyped(KeyEvent e) { + // Do nothing + } + + @Override + public void keyPressed(KeyEvent e) { + ClientPacket request = new KeyboardEventPacket(RfbConstants.KEY_DOWN, mapAwtKeyToVncKey(e.getKeyCode())); + queue.add(request); + } + + @Override + public void keyReleased(KeyEvent e) { + ClientPacket request = new KeyboardEventPacket(RfbConstants.KEY_UP, mapAwtKeyToVncKey(e.getKeyCode())); + queue.add(request); + } + + private int mapAwtKeyToVncKey(int key) { + switch (key) { + case KeyEvent.VK_BACK_SPACE: + return 0xff08; + case KeyEvent.VK_TAB: + return 0xff09; + case KeyEvent.VK_ENTER: + return 0xff0d; + case KeyEvent.VK_ESCAPE: + return 0xff1b; + case KeyEvent.VK_INSERT: + return 0xff63; + case KeyEvent.VK_DELETE: + return 0xffff; + case KeyEvent.VK_HOME: + return 0xff50; + case KeyEvent.VK_END: + return 0xff57; + case KeyEvent.VK_PAGE_UP: + return 0xff55; + case KeyEvent.VK_PAGE_DOWN: + return 0xff56; + case KeyEvent.VK_LEFT: + return 0xff51; + case KeyEvent.VK_UP: + return 0xff52; + case KeyEvent.VK_RIGHT: + return 0xff53; + case KeyEvent.VK_DOWN: + return 0xff54; + case KeyEvent.VK_F1: + return 0xffbe; + case KeyEvent.VK_F2: + return 0xffbf; + case KeyEvent.VK_F3: + return 0xffc0; + case KeyEvent.VK_F4: + return 0xffc1; + case KeyEvent.VK_F5: + return 0xffc2; + case KeyEvent.VK_F6: + return 0xffc3; + case KeyEvent.VK_F7: + return 0xffc4; + case KeyEvent.VK_F8: + return 0xffc5; + case KeyEvent.VK_F9: + return 0xffc6; + case KeyEvent.VK_F10: + return 0xffc7; + case KeyEvent.VK_F11: + return 0xffc8; + case KeyEvent.VK_F12: + return 0xffc9; + case KeyEvent.VK_SHIFT: + return 0xffe1; + case KeyEvent.VK_CONTROL: + return 0xffe3; + case KeyEvent.VK_META: + return 0xffe7; + case KeyEvent.VK_ALT: + return 0xffe9; + case KeyEvent.VK_ALT_GRAPH: + return 0xffea; + case KeyEvent.VK_BACK_QUOTE: + return 0x0060; + } + + return key; + } + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncScreenDescription.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncScreenDescription.java new file mode 100644 index 00000000000..44b2a34769b --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncScreenDescription.java @@ -0,0 +1,89 @@ +// 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.vnc; + +/** + * VncScreenDescription - contains information about remote VNC screen. + */ +public class VncScreenDescription { + + // Frame buffer size + private int framebufferWidth = -1; + private int framebufferHeight = -1; + + // Desktop name + private String desktopName; + + // Bytes per pixel + private int bytesPerPixel; + + // Indicates that screen uses format which we want to use: + // RGB 24bit packed into 32bit little-endian int. + private boolean rgb888_32_le = false; + + public VncScreenDescription() { + } + + /** + * Store information about server pixel format. + */ + public void setPixelFormat(int bitsPerPixel, int depth, int bigEndianFlag, int trueColorFlag, int redMax, int greenMax, int blueMax, int redShift, int greenShift, int blueShift) { + + bytesPerPixel = (bitsPerPixel + 7) / 8; + + rgb888_32_le = (depth == 24 && bitsPerPixel == 32 && redShift == 16 && greenShift == 8 && blueShift == 0 && redMax == 255 && greenMax == 255 && blueMax == 255 + && bigEndianFlag == RfbConstants.LITTLE_ENDIAN && trueColorFlag == RfbConstants.TRUE_COLOR); + } + + /** + * Store information about server screen size. + */ + public void setFramebufferSize(int framebufferWidth, int framebufferHeight) { + this.framebufferWidth = framebufferWidth; + this.framebufferHeight = framebufferHeight; + } + + /** + * Store server desktop name. + */ + public void setDesktopName(String desktopName) { + this.desktopName = desktopName; + } + + // Getters for variables, as usual + + public String getDesktopName() { + return desktopName; + } + + public int getBytesPerPixel() { + return bytesPerPixel; + } + + public int getFramebufferHeight() { + return framebufferHeight; + } + + public int getFramebufferWidth() { + return framebufferWidth; + } + + public boolean isRGB888_32_LE() { + return rgb888_32_le; + } + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncServerPacketReceiver.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncServerPacketReceiver.java new file mode 100644 index 00000000000..57c8ff8efe2 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/VncServerPacketReceiver.java @@ -0,0 +1,123 @@ +// 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.vnc; + +import java.awt.Toolkit; +import java.awt.datatransfer.StringSelection; +import java.io.DataInputStream; +import java.io.IOException; + +import com.cloud.consoleproxy.ConsoleProxyClientListener; +import com.cloud.consoleproxy.util.Logger; +import com.cloud.consoleproxy.vnc.packet.server.FramebufferUpdatePacket; +import com.cloud.consoleproxy.vnc.packet.server.ServerCutText; + +public class VncServerPacketReceiver implements Runnable { + private static final Logger s_logger = Logger.getLogger(VncServerPacketReceiver.class); + + private final VncScreenDescription screen; + private BufferedImageCanvas canvas; + private DataInputStream is; + + private boolean connectionAlive = true; + private VncClient vncConnection; + private final FrameBufferUpdateListener fburListener; + private final ConsoleProxyClientListener clientListener; + + public VncServerPacketReceiver(DataInputStream is, BufferedImageCanvas canvas, VncScreenDescription screen, VncClient vncConnection, FrameBufferUpdateListener fburListener, + ConsoleProxyClientListener clientListener) { + this.screen = screen; + this.canvas = canvas; + this.is = is; + this.vncConnection = vncConnection; + this.fburListener = fburListener; + this.clientListener = clientListener; + } + + public BufferedImageCanvas getCanvas() { + return canvas; + } + + @Override + public void run() { + try { + while (connectionAlive) { + + // Read server message type + int messageType = is.readUnsignedByte(); + + // Invoke packet handler by packet type. + switch (messageType) { + + case RfbConstants.SERVER_FRAMEBUFFER_UPDATE: { + // Notify sender that frame buffer update is received, + // so it can send another frame buffer update request + fburListener.frameBufferPacketReceived(); + // Handle frame buffer update + new FramebufferUpdatePacket(canvas, screen, is, clientListener); + break; + } + + case RfbConstants.SERVER_BELL: { + serverBell(); + break; + } + + case RfbConstants.SERVER_CUT_TEXT: { + serverCutText(is); + break; + } + + default: + throw new RuntimeException("Unknown server packet type: " + messageType + "."); + } + } + } catch (Throwable e) { + s_logger.error("Unexpected exception: ", e); + if (connectionAlive) { + closeConnection(); + vncConnection.shutdown(); + } + } + } + + public void closeConnection() { + connectionAlive = false; + } + + public boolean isConnectionAlive() { + return connectionAlive; + } + + /** + * Handle server bell packet. + */ + private void serverBell() { + Toolkit.getDefaultToolkit().beep(); + } + + /** + * Handle packet with server clip-board. + */ + private void serverCutText(DataInputStream is) throws IOException { + ServerCutText clipboardContent = new ServerCutText(is); + StringSelection contents = new StringSelection(clipboardContent.getContent()); + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(contents, null); + + s_logger.info("Server clipboard buffer: " + clipboardContent.getContent()); + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/ClientPacket.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/ClientPacket.java new file mode 100644 index 00000000000..873b8c0825e --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/ClientPacket.java @@ -0,0 +1,26 @@ +// 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.vnc.packet.client; + +import java.io.DataOutputStream; +import java.io.IOException; + +public interface ClientPacket { + + void write(DataOutputStream os) throws IOException; + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/FramebufferUpdateRequestPacket.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/FramebufferUpdateRequestPacket.java new file mode 100644 index 00000000000..d3a6e40e961 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/FramebufferUpdateRequestPacket.java @@ -0,0 +1,53 @@ +// 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.vnc.packet.client; + +import java.io.DataOutputStream; +import java.io.IOException; + +import com.cloud.consoleproxy.vnc.RfbConstants; + +/** + * FramebufferUpdateRequestPacket + * + * @author Volodymyr M. Lisivka + */ +public class FramebufferUpdateRequestPacket implements ClientPacket { + + private final int incremental; + private final int x, y, width, height; + + public FramebufferUpdateRequestPacket(int incremental, int x, int y, int width, int height) { + this.incremental = incremental; + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + @Override + public void write(DataOutputStream os) throws IOException { + os.writeByte(RfbConstants.CLIENT_FRAMEBUFFER_UPDATE_REQUEST); + + os.writeByte(incremental); + os.writeShort(x); + os.writeShort(y); + os.writeShort(width); + os.writeShort(height); + } + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/KeyboardEventPacket.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/KeyboardEventPacket.java new file mode 100644 index 00000000000..8efbab123a5 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/KeyboardEventPacket.java @@ -0,0 +1,42 @@ +// 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.vnc.packet.client; + +import java.io.DataOutputStream; +import java.io.IOException; + +import com.cloud.consoleproxy.vnc.RfbConstants; + +public class KeyboardEventPacket implements ClientPacket { + + private final int downFlag, key; + + public KeyboardEventPacket(int downFlag, int key) { + this.downFlag = downFlag; + this.key = key; + } + + @Override + public void write(DataOutputStream os) throws IOException { + os.writeByte(RfbConstants.CLIENT_KEYBOARD_EVENT); + + os.writeByte(downFlag); + os.writeShort(0); // padding + os.writeInt(key); + } + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/MouseEventPacket.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/MouseEventPacket.java new file mode 100644 index 00000000000..b42191cbadc --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/MouseEventPacket.java @@ -0,0 +1,43 @@ +// 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.vnc.packet.client; + +import java.io.DataOutputStream; +import java.io.IOException; + +import com.cloud.consoleproxy.vnc.RfbConstants; + +public class MouseEventPacket implements ClientPacket { + + private final int buttonMask, x, y; + + public MouseEventPacket(int buttonMask, int x, int y) { + this.buttonMask = buttonMask; + this.x = x; + this.y = y; + } + + @Override + public void write(DataOutputStream os) throws IOException { + os.writeByte(RfbConstants.CLIENT_POINTER_EVENT); + + os.writeByte(buttonMask); + os.writeShort(x); + os.writeShort(y); + } + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/SetEncodingsPacket.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/SetEncodingsPacket.java new file mode 100644 index 00000000000..3d8cfcb09a6 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/SetEncodingsPacket.java @@ -0,0 +1,45 @@ +// 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.vnc.packet.client; + +import java.io.DataOutputStream; +import java.io.IOException; + +import com.cloud.consoleproxy.vnc.RfbConstants; + +public class SetEncodingsPacket implements ClientPacket { + + private final int[] encodings; + + public SetEncodingsPacket(int[] encodings) { + this.encodings = encodings; + } + + @Override + public void write(DataOutputStream os) throws IOException { + os.writeByte(RfbConstants.CLIENT_SET_ENCODINGS); + + os.writeByte(0);// padding + + os.writeShort(encodings.length); + + for (int i = 0; i < encodings.length; i++) { + os.writeInt(encodings[i]); + } + } + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/SetPixelFormatPacket.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/SetPixelFormatPacket.java new file mode 100644 index 00000000000..7a25bfab9f3 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/client/SetPixelFormatPacket.java @@ -0,0 +1,75 @@ +// 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.vnc.packet.client; + +import java.io.DataOutputStream; +import java.io.IOException; + +import com.cloud.consoleproxy.vnc.RfbConstants; +import com.cloud.consoleproxy.vnc.VncScreenDescription; + +public class SetPixelFormatPacket implements ClientPacket { + + private final int bitsPerPixel, depth, bigEndianFlag, trueColourFlag, redMax, greenMax, blueMax, redShift, greenShift, blueShift; + + private final VncScreenDescription screen; + + public SetPixelFormatPacket(VncScreenDescription screen, int bitsPerPixel, int depth, int bigEndianFlag, int trueColorFlag, int redMax, int greenMax, int blueMax, int redShift, int greenShift, + int blueShift) { + this.screen = screen; + this.bitsPerPixel = bitsPerPixel; + this.depth = depth; + this.bigEndianFlag = bigEndianFlag; + this.trueColourFlag = trueColorFlag; + this.redMax = redMax; + this.greenMax = greenMax; + this.blueMax = blueMax; + this.redShift = redShift; + this.greenShift = greenShift; + this.blueShift = blueShift; + } + + @Override + public void write(DataOutputStream os) throws IOException { + os.writeByte(RfbConstants.CLIENT_SET_PIXEL_FORMAT); + + // Padding + os.writeByte(0); + os.writeByte(0); + os.writeByte(0); + + // Send pixel format + os.writeByte(bitsPerPixel); + os.writeByte(depth); + os.writeByte(bigEndianFlag); + os.writeByte(trueColourFlag); + os.writeShort(redMax); + os.writeShort(greenMax); + os.writeShort(blueMax); + os.writeByte(redShift); + os.writeByte(greenShift); + os.writeByte(blueShift); + + // Padding + os.writeByte(0); + os.writeByte(0); + os.writeByte(0); + + screen.setPixelFormat(bitsPerPixel, depth, bigEndianFlag, trueColourFlag, redMax, greenMax, blueMax, redShift, greenShift, blueShift); + } + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/AbstractRect.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/AbstractRect.java new file mode 100644 index 00000000000..a380e9cd62a --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/AbstractRect.java @@ -0,0 +1,53 @@ +// 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.vnc.packet.server; + +public abstract class AbstractRect implements Rect { + + protected final int x; + protected final int y; + protected final int width; + protected final int height; + + public AbstractRect(int x, int y, int width, int height) { + this.x = x; + this.y = y; + this.width = width; + this.height = height; + } + + @Override + public int getX() { + return x; + } + + @Override + public int getY() { + return y; + } + + @Override + public int getWidth() { + return width; + } + + @Override + public int getHeight() { + return height; + } + +} \ No newline at end of file diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/CopyRect.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/CopyRect.java new file mode 100644 index 00000000000..caaecb58864 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/CopyRect.java @@ -0,0 +1,39 @@ +// 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.vnc.packet.server; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.io.DataInputStream; +import java.io.IOException; + +public class CopyRect extends AbstractRect { + + private final int srcX, srcY; + + public CopyRect(int x, int y, int width, int height, DataInputStream is) throws IOException { + super(x, y, width, height); + + srcX = is.readUnsignedShort(); + srcY = is.readUnsignedShort(); + } + + @Override + public void paint(BufferedImage image, Graphics2D graphics) { + graphics.copyArea(srcX, srcY, width, height, x - srcX, y - srcY); + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/FrameBufferSizeChangeRequest.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/FrameBufferSizeChangeRequest.java new file mode 100644 index 00000000000..18f6987eab4 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/FrameBufferSizeChangeRequest.java @@ -0,0 +1,39 @@ +// 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.vnc.packet.server; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +import com.cloud.consoleproxy.vnc.BufferedImageCanvas; + +public class FrameBufferSizeChangeRequest extends AbstractRect { + + private final BufferedImageCanvas canvas; + + public FrameBufferSizeChangeRequest(BufferedImageCanvas canvas, int width, int height) { + super(0, 0, width, height); + this.canvas = canvas; + canvas.setCanvasSize(width, height); + } + + @Override + public void paint(BufferedImage offlineImage, Graphics2D graphics) { + canvas.setCanvasSize(width, height); + } + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/FramebufferUpdatePacket.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/FramebufferUpdatePacket.java new file mode 100644 index 00000000000..3bc43fdded1 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/FramebufferUpdatePacket.java @@ -0,0 +1,102 @@ +// 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.vnc.packet.server; + +import java.io.DataInputStream; +import java.io.IOException; + +import com.cloud.consoleproxy.ConsoleProxyClientListener; +import com.cloud.consoleproxy.vnc.BufferedImageCanvas; +import com.cloud.consoleproxy.vnc.RfbConstants; +import com.cloud.consoleproxy.vnc.VncScreenDescription; +import com.cloud.consoleproxy.vnc.packet.server.CopyRect; +import com.cloud.consoleproxy.vnc.packet.server.RawRect; +import com.cloud.consoleproxy.vnc.packet.server.Rect; + +public class FramebufferUpdatePacket { + + private final VncScreenDescription screen; + private final BufferedImageCanvas canvas; + private final ConsoleProxyClientListener clientListener; + + public FramebufferUpdatePacket(BufferedImageCanvas canvas, VncScreenDescription screen, DataInputStream is, ConsoleProxyClientListener clientListener) throws IOException { + + this.screen = screen; + this.canvas = canvas; + this.clientListener = clientListener; + readPacketData(is); + } + + private void readPacketData(DataInputStream is) throws IOException { + is.skipBytes(1);// Skip padding + + // Read number of rectangles + int numberOfRectangles = is.readUnsignedShort(); + + // For all rectangles + for (int i = 0; i < numberOfRectangles; i++) { + + // Read coordinate of rectangle + int x = is.readUnsignedShort(); + int y = is.readUnsignedShort(); + int width = is.readUnsignedShort(); + int height = is.readUnsignedShort(); + + int encodingType = is.readInt(); + + // Process rectangle + Rect rect; + switch (encodingType) { + + case RfbConstants.ENCODING_RAW: { + rect = new RawRect(screen, x, y, width, height, is); + break; + } + + case RfbConstants.ENCODING_COPY_RECT: { + rect = new CopyRect(x, y, width, height, is); + break; + } + + case RfbConstants.ENCODING_DESKTOP_SIZE: { + rect = new FrameBufferSizeChangeRequest(canvas, width, height); + if (this.clientListener != null) + this.clientListener.onFramebufferSizeChange(width, height); + break; + } + + default: + throw new RuntimeException("Unsupported ecnoding: " + encodingType); + } + + paint(rect, canvas); + + if (this.clientListener != null) + this.clientListener.onFramebufferUpdate(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight()); + } + + } + + public void paint(Rect rect, BufferedImageCanvas canvas) { + // Draw rectangle on offline buffer + rect.paint(canvas.getOfflineImage(), canvas.getOfflineGraphics()); + + // Request update of repainted area + canvas.repaint(rect.getX(), rect.getY(), rect.getWidth(), rect.getHeight()); + } + +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/RawRect.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/RawRect.java new file mode 100644 index 00000000000..978a4c2755d --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/RawRect.java @@ -0,0 +1,75 @@ +// 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.vnc.packet.server; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; +import java.awt.image.DataBuffer; +import java.awt.image.DataBufferInt; +import java.io.DataInputStream; +import java.io.IOException; + +import com.cloud.consoleproxy.vnc.VncScreenDescription; + +public class RawRect extends AbstractRect { + private final int[] buf; + + public RawRect(VncScreenDescription screen, int x, int y, int width, int height, DataInputStream is) throws IOException { + super(x, y, width, height); + + byte[] bbuf = new byte[width * height * screen.getBytesPerPixel()]; + is.readFully(bbuf); + + // Convert array of bytes to array of int + int size = width * height; + buf = new int[size]; + for (int i = 0, j = 0; i < size; i++, j += 4) { + buf[i] = (bbuf[j + 0] & 0xFF) | ((bbuf[j + 1] & 0xFF) << 8) | ((bbuf[j + 2] & 0xFF) << 16) | ((bbuf[j + 3] & 0xFF) << 24); + } + + } + + @Override + public void paint(BufferedImage image, Graphics2D graphics) { + + DataBuffer dataBuf = image.getRaster().getDataBuffer(); + + switch (dataBuf.getDataType()) { + + case DataBuffer.TYPE_INT: { + // We chose RGB888 model, so Raster will use DataBufferInt type + DataBufferInt dataBuffer = (DataBufferInt) dataBuf; + + int imageWidth = image.getWidth(); + int imageHeight = image.getHeight(); + + // Paint rectangle directly on buffer, line by line + int[] imageBuffer = dataBuffer.getData(); + for (int srcLine = 0, dstLine = y; srcLine < height && dstLine < imageHeight; srcLine++, dstLine++) { + try { + System.arraycopy(buf, srcLine * width, imageBuffer, x + dstLine * imageWidth, width); + } catch (IndexOutOfBoundsException e) { + } + } + break; + } + + default: + throw new RuntimeException("Unsupported data buffer in buffered image: expected data buffer of type int (DataBufferInt). Actual data buffer type: " + dataBuf.getClass().getSimpleName()); + } + } +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/Rect.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/Rect.java new file mode 100644 index 00000000000..51edf12d0d6 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/Rect.java @@ -0,0 +1,33 @@ +// 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.vnc.packet.server; + +import java.awt.Graphics2D; +import java.awt.image.BufferedImage; + +public interface Rect { + + void paint(BufferedImage offlineImage, Graphics2D graphics); + + int getX(); + + int getY(); + + int getWidth(); + + int getHeight(); +} diff --git a/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/ServerCutText.java b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/ServerCutText.java new file mode 100644 index 00000000000..044f9589b23 --- /dev/null +++ b/services/console-proxy/server/src/com/cloud/consoleproxy/vnc/packet/server/ServerCutText.java @@ -0,0 +1,49 @@ +// 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.vnc.packet.server; + +import java.io.DataInputStream; +import java.io.IOException; + +import com.cloud.consoleproxy.util.Logger; +import com.cloud.consoleproxy.vnc.RfbConstants; + +public class ServerCutText { + private static final Logger s_logger = Logger.getLogger(ServerCutText.class); + + private String content; + + public String getContent() { + return content; + } + + public ServerCutText(DataInputStream is) throws IOException { + readPacketData(is); + } + + private void readPacketData(DataInputStream is) throws IOException { + is.skipBytes(3);// Skip padding + int length = is.readInt(); + byte buf[] = new byte[length]; + is.readFully(buf); + + content = new String(buf, RfbConstants.CHARSET); + + /* LOG */s_logger.info("Clippboard content: " + content); + } + +} diff --git a/services/console-proxy/server/systemvm-descriptor.xml b/services/console-proxy/server/systemvm-descriptor.xml new file mode 100644 index 00000000000..7efe7fdfcb0 --- /dev/null +++ b/services/console-proxy/server/systemvm-descriptor.xml @@ -0,0 +1,113 @@ + + + systemvm + + zip + + false + + + + + + + + ../scripts/storage/secondary/ + scripts/storage/secondary + 555 + 555 + + + ../scripts/storage/secondary/ + scripts/storage/secondary + 555 + 555 + + + scripts + + 555 + 555 + + + conf + conf + 555 + 555 + + log4j-cloud.xml + consoleproxy.properties + agent.properties + + + + ../console-proxy/images + images + 555 + 555 + + *.jpg + *.gif + *.png + *.cur + + + + ../console-proxy/js + js + 555 + 555 + + *.js + + + + ../console-proxy/ui + ui + 555 + 555 + + *.ftl + + + + ../console-proxy/css + css + 555 + 555 + + *.css + + + + ../console-proxy/certs + certs + 555 + 555 + + *.keystore + *.crt + *.key + + + + diff --git a/services/console-proxy/server/ui/viewer-bad-sid.ftl b/services/console-proxy/server/ui/viewer-bad-sid.ftl new file mode 100644 index 00000000000..2f30ec36015 --- /dev/null +++ b/services/console-proxy/server/ui/viewer-bad-sid.ftl @@ -0,0 +1,29 @@ + + + + + + +
+

Unable to start console session as access is denied because of bad sid

+
+ + + diff --git a/services/console-proxy/server/ui/viewer-connect-failed.ftl b/services/console-proxy/server/ui/viewer-connect-failed.ftl new file mode 100644 index 00000000000..9d907cacb43 --- /dev/null +++ b/services/console-proxy/server/ui/viewer-connect-failed.ftl @@ -0,0 +1,29 @@ + + + + + + +
+

Unable to start console session as connection is refused by the machine you are accessing

+
+ + + diff --git a/services/console-proxy/server/ui/viewer-update.ftl b/services/console-proxy/server/ui/viewer-update.ftl new file mode 100644 index 00000000000..6bf9ab35ce7 --- /dev/null +++ b/services/console-proxy/server/ui/viewer-update.ftl @@ -0,0 +1,24 @@ +<#-- +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. +--> +tileMap = [ ${tileSequence} ]; +<#if resized == true> + ajaxViewer.resize('main_panel', ${width}, ${height}, ${tileWidth}, ${tileHeight}); + +ajaxViewer.refresh('${imgUrl}', tileMap, false); + diff --git a/services/console-proxy/server/ui/viewer.ftl b/services/console-proxy/server/ui/viewer.ftl new file mode 100644 index 00000000000..62de193cf7b --- /dev/null +++ b/services/console-proxy/server/ui/viewer.ftl @@ -0,0 +1,60 @@ + + + + + + + + +${title} + + + + +
+ + + + + diff --git a/services/console-proxy/server/vm-script/vmops b/services/console-proxy/server/vm-script/vmops new file mode 100644 index 00000000000..a9f70c83925 --- /dev/null +++ b/services/console-proxy/server/vm-script/vmops @@ -0,0 +1,119 @@ +#!/bin/bash +# +# vmops Script to start and stop the VMOps Agent. +# +# Author: Chiradeep Vittal +# chkconfig: 2345 99 01 +# description: Start up the VMOps agent + +# 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. + + +# Source function library. +if [ -f /etc/init.d/functions ] +then + . /etc/init.d/functions +fi + +_success() { + if [ -f /etc/init.d/functions ] + then + success + else + echo "Success" + fi +} + +_failure() { + if [ -f /etc/init.d/functions ] + then + failure + else + echo "Failed" + fi +} +RETVAL=$? +VMOPS_HOME="/usr/local/vmops" + +mkdir -p /var/log/vmops + +get_pids() { + local i + for i in $(ps -ef| grep java | grep -v grep | awk '{print $2}'); + do + echo $(pwdx $i) | grep "$VMOPS_HOME" | grep -i console | awk -F: '{print $1}'; + done +} + +start() { + local pid=$(get_pids) + echo -n "Starting VMOps Console Proxy: " + if [ -f $VMOPS_HOME/consoleproxy/run.sh ]; + then + if [ "$pid" == "" ] + then + (cd $VMOPS_HOME/consoleproxy; nohup ./run.sh > /var/log/vmops/vmops.out 2>&1 & ) + pid=$(get_pids) + echo $pid > /var/run/vmops.pid + fi + _success + else + _failure + fi + echo +} + +stop() { + local pid + echo -n "Stopping VMOps agent: " + for pid in $(get_pids) + do + kill $pid + done + _success + echo +} + +status() { + local pids=$(get_pids) + if [ "$pids" == "" ] + then + echo "VMOps agent is not running" + return 1 + fi + echo "VMOps agent is running: process id: $pids" + return 0 +} + + +case "$1" in + start) start + ;; + stop) stop + ;; + status) status + ;; + restart) stop + start + ;; + *) echo $"Usage: $0 {start|stop|status|restart}" + exit 1 + ;; +esac + +exit $RETVAL diff --git a/services/pom.xml b/services/pom.xml new file mode 100644 index 00000000000..26488513999 --- /dev/null +++ b/services/pom.xml @@ -0,0 +1,36 @@ + + + 4.0.0 + cloud-services + Apache CloudStack Cloud Services + pom + + org.apache.cloudstack + cloudstack + 4.1.0-SNAPSHOT + ../pom.xml + + + install + + + console-proxy + +