-
Notifications
You must be signed in to change notification settings - Fork 8
/
connection_tools.py
61 lines (51 loc) · 1.86 KB
/
connection_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from django.shortcuts import render_to_response
def nx_conn_builder(ip, username, password, app):
"""
Returns a response object containing an nx session.
This function selects a node from the cluster.
"""
resp = render_to_response('vdi/nx_single_session.html',
{'nx_ip' : ip,
'nx_username' : username,
'nx_password' : encryptNXPass(password),
'conn_type' : 'unix',
'app_path' : app.path})
resp['Content-Type']="application/nx"
# TODO update the hardcoded string 'connection' below to something like app.name I'm not sure how to get that data here architecturally
resp['Content-Disposition'] = 'attachment; filename="%s.nxs"' % app.name
return resp
def encryptNXPass(s):
"""Encrypt a password like No Machine does."""
dummyString = '{{{{'
validCharList = '!#$%&()*+-.0123456789:;<>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]_abcdefghijklmnopqrstuvwxyz{|}'
if not s:
return
enc = encodePassword(s)
sRet = ''
if len(enc) < 32:
enc = dummyString
sRet = enc[::-1]
if len(sRet) < 32:
sRet += dummyString
app = choice(validCharList)
k = ord(app)
l = k + len(sRet) - 2
sRet = app+sRet
for i in range(1,len(sRet)):
j = validCharList.find(sRet[i])
if j == -1:
return sRet
car = validCharList[(j + l * (i+1)) % len(validCharList)]
sRet = ''.join([ sRet[:i], car, sRet[i+1:]])
c = (ord(choice(validCharList))) + 2
sRet = sRet + chr(c)
return sRet
def encodePassword(password):
"""Encode a password like No Machine does."""
if not password:
return password
sPass = [':']
for i in range(len(password)):
c = password[i]
sPass.append("%s:" % int(ord(c)+i+1))
return ''.join(sPass)