1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 """\
21 X2GoProxy classes - proxying your connection through NX3 and others.
22
23 """
24 __NAME__ = 'x2goproxynx3-pylib'
25
26
27 import os
28
29
30 import x2go.log as log
31 import x2go.backends.proxy.base as base
32
33 from x2go.defaults import X2GOCLIENT_OS as _X2GOCLIENT_OS
34
36 """\
37 This L{X2GoProxy} class is a NX version 3 based X2Go proxy connection class.
38
39 It basically fills L{x2go.backends.proxy.base.X2GoProxy} variables with sensible content. Its
40 methods mostly wrap around the corresponding methods of the parent class.
41
42 """
44 """\
45 For available parameters refer to L{x2go.backends.proxy.base.X2GoProxy} class documentation.
46
47 """
48 base.X2GoProxy.__init__(self, *args, **kwargs)
49 self.subsystem = 'NX Proxy'
50
51
52 if _X2GOCLIENT_OS == "Windows":
53 _nxproxy_paths = [
54 os.path.join(os.environ["ProgramFiles"], os.path.normpath("PyHoca-GUI/nxproxy/nxproxy.exe")),
55 os.path.join(os.environ["ProgramFiles"], os.path.normpath("x2goclient/nxproxy.exe")),
56 os.path.join(os.environ["ProgramFiles"], os.path.normpath("NX Client for Windows/bin/nxproxy.exe")),
57 os.path.normpath("../pyhoca-contrib/mswin/nxproxy-mswin/nxproxy-3.5.0.27_cygwin-2014-10-18/nxproxy.exe"),
58 ]
59 if os.environ.has_key('NXPROXY_BINARY'):
60 _nxproxy_paths.insert(0, os.environ['NXPROXY_BINARY'])
61 for _nxproxy_cmd in _nxproxy_paths:
62 if os.path.exists(_nxproxy_cmd):
63 break
64 self.PROXY_CMD = _nxproxy_cmd
65 else:
66 self.PROXY_CMD = "/usr/bin/nxproxy"
67 self.PROXY_ENV.update({
68 "NX_CLIENT": "/bin/true",
69 "NX_ROOT": self.sessions_rootdir
70 })
71 self.PROXY_MODE = '-S'
72 if _X2GOCLIENT_OS == "Windows":
73 self.PROXY_OPTIONS = [
74 "nx/nx" ,
75 "retry=5",
76 "composite=1",
77 "connect=127.0.0.1",
78 "clipboard=1",
79 "cookie=%s" % self.session_info.cookie,
80 "port=%s" % self.session_info.graphics_port,
81 "errors=%s" % os.path.join(".", "..", "S-%s" % self.session_info.name, self.session_errors, ),
82 ]
83 else:
84 self.PROXY_OPTIONS = [
85 "nx/nx" ,
86 "retry=5",
87 "composite=1",
88 "connect=127.0.0.1",
89 "clipboard=1",
90 "cookie=%s" % self.session_info.cookie,
91 "port=%s" % self.session_info.graphics_port,
92 "errors=%s" % os.path.join(self.session_info.local_container, self.session_errors, ),
93 ]
94
95 self.PROXY_DISPLAY = self.session_info.display
96
98 """\
99 Update the local proxy socket on port changes due to already-bound-to local TCP/IP port sockets.
100
101 @param port: new local TCP/IP socket port
102 @type port: C{int}
103
104 """
105
106 for idx, a in enumerate(self.PROXY_OPTIONS):
107 if a.startswith('port='):
108 self.PROXY_OPTIONS[idx] = 'port=%s' % port
109
111 """\
112 Generate the NX proxy command line for execution.
113
114 """
115 if _X2GOCLIENT_OS == "Windows":
116 _options_filename = os.path.join(self.session_info.local_container, 'options')
117 options = open(_options_filename, 'w')
118 options.write('%s:%s' % (','.join(self.PROXY_OPTIONS), self.PROXY_DISPLAY))
119 options.close()
120 self.PROXY_OPTIONS= [ 'nx/nx', 'options=%s' % os.path.join("\\", "..", "S-%s" % self.session_info.name, 'options'), ]
121
122 cmd_line = [ self.PROXY_CMD, ]
123 cmd_line.append(self.PROXY_MODE)
124 _proxy_options = "%s:%s" % (",".join(self.PROXY_OPTIONS), self.PROXY_DISPLAY)
125 cmd_line.append(_proxy_options)
126 return cmd_line
127
130
132 """\
133 Start the thread runner and wait for the proxy to come up.
134
135 @return: a subprocess instance that knows about the externally started proxy command.
136 @rtype: C{obj}
137
138 """
139 self.logger('starting local NX3 proxy...', loglevel=log.loglevel_INFO)
140 self.logger('NX3 Proxy mode is server, cookie=%s, host=127.0.0.1, port=%s.' % (self.session_info.cookie, self.session_info.graphics_port,), loglevel=log.loglevel_DEBUG)
141 self.logger('NX3 proxy writes session log to %s.' % os.path.join(self.session_info.local_container, 'session.log'), loglevel=log.loglevel_DEBUG)
142
143 p, p_ok = base.X2GoProxy.start_proxy(self)
144
145 if self.ok():
146 self.logger('NX3 proxy is up and running.', loglevel=log.loglevel_INFO)
147 else:
148 self.logger('Bringing up NX3 proxy failed.', loglevel=log.loglevel_ERROR)
149
150 return p, self.ok()
151