Add more information on deploying; fix format.
[ganeti_webmgr.git] / ganeti_web / util / vncdaemon / vapclient.py
bloba1151da29a9265d6ef7c985463bc39d8a1d11fb2
1 #!/usr/bin/env python
4 # Copyright (c) 2010 GRNET SA
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 # 02110-1301, USA.
21 import sys
22 import socket
24 from django.utils import simplejson as json
26 CTRL_SOCKET = "/tmp/vncproxy.sock"
29 def request_forwarding(server, daddr, dport, password, sport=None, tls=False):
30 """
31 Ask TVAP/VNCAP for a forwarding port.
33 The control socket on TVAP wants a JSON dictionary containing at least the
34 destination port and address, and VNC password. It optionally can accept a
35 requested source port, whether WebSockets should be used, and whether TLS
36 (SSL/WSS) should be used.
37 """
39 try:
40 host, port = server
41 port = int(port)
42 dport = int(dport)
43 if not password:
44 return False
46 request = {
47 "daddr": daddr,
48 "dport": dport,
49 "password": password,
50 "ws": True,
51 "tls": tls,
54 if sport:
55 request["sport"] = sport
57 request = json.dumps(request)
59 ctrl = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
60 ctrl.connect((host, port))
61 ctrl.send("%s\r\n" % request)
62 response = ctrl.recv(1024).strip()
63 ctrl.close()
65 if response.startswith("FAIL"):
66 return False
67 else:
68 return response
70 # XXX bare except
71 except:
72 return False
75 def request_ssh(proxy, sport, daddr, dport, password, command):
76 """
77 Ask TVAP/VNCAP for an SSH port.
78 """
80 host, port = proxy
81 if not password or not command:
82 return False
84 request = {
85 "daddr": daddr,
86 "dport": dport,
87 "password": password,
88 "command": command,
91 if sport:
92 request["sport"] = sport
94 request = json.dumps(request)
96 ctrl = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
97 ctrl.connect((host, port))
98 ctrl.send("%s\r\n" % request)
99 response = ctrl.recv(1024).strip()
100 ctrl.close()
102 if response.startswith("FAIL"):
103 return False
104 else:
105 return response
107 if __name__ == '__main__':
108 print request_forwarding(sys.argv[1].split(":"), *sys.argv[2:])