-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaccount.ma
More file actions
107 lines (98 loc) · 3.75 KB
/
account.ma
File metadata and controls
107 lines (98 loc) · 3.75 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from twisted.conch.ssh import transport, userauth, connection, channel, \
common, filetransfer
from twisted.internet import defer, protocol, reactor
class Transport(transport.SSHClientTransport):
def __init__(self, user, password, callback):
self.auth = UserAuth(user, Connection(callback))
self.auth.setPassword(password)
def verifyHostKey(self, hostKey, fingerprint):
return defer.succeed(1)
def connectionSecure(self):
self.requestService(self.auth)
def receiveError(self, reasonCode, description):
request.setResponseCode(401)
request.finish()
class UserAuth(userauth.SSHUserAuthClient):
def setPassword(self, password):
self.password = password
def getPassword(self):
return defer.succeed(self.password)
def getPublicKey(self):
return
class Connection(connection.SSHConnection):
def __init__(self, callback):
connection.SSHConnection.__init__(self)
self.channel = Channel(2**16, 2**15, self)
self.channel.setCallback(callback)
def serviceStarted(self):
self.openChannel(self.channel)
class Channel(channel.SSHChannel):
name = 'session' # must use this exact string
def setCallback(self, callback):
self.callback = callback
def channelOpen(self, data):
def gotError(fail):
request.finish()
def gotResult(result):
self.sftp = filetransfer.FileTransferClient()
self.sftp.makeConnection(self)
self.dataReceived = self.sftp.dataReceived
self.callback(self.sftp)
d = self.conn.sendRequest(self, 'subsystem',
common.NS('sftp'), wantReply=1)
d.addCallbacks(gotResult, gotError)
def closed(self):
self.loseConnection()
def account(user, password, host, port):
def gotFile(file):
thimblLogin = "%s:%s@%s:%s" % (user, password, host, port)
thimblUser = "%s@%s" % (user, host)
json = '{ "login":"%s", "user":"%s" }' % (thimblLogin, thimblUser)
request.setHeader("Access-Control-Allow-Origin", "*")
request.setHeader("Access-Control-Allow-Methods", "GET")
request.addCookie("thimbl-login", thimblLogin, path="/account")
request.addCookie("thimbl-user", thimblUser)
if "callback" in request.args:
callback = request.args['callback'][0]
jsonp = "%s(%s)" % (callback, json)
request.setHeader("Content-Type", "application/javascript")
request.write(jsonp)
else:
request.setHeader("Content-Type", "application/json")
request.write(json)
request.finish()
if ".plan" in request.args:
file.writeChunk(0, request.args[".plan"][0])
file.close()
def gotProtocol(sftp):
if ".plan" in request.args:
mode = filetransfer.FXF_WRITE|filetransfer.FXF_CREAT|filetransfer.FXF_TRUNC
else:
mode = filetransfer.FXP_OPEN
d = sftp.openFile(".plan", mode, {})
d.addCallback(gotFile)
c = protocol.ClientCreator(reactor,
Transport, user, password, gotProtocol).connectTCP(host, port)
host = None
if "u" in request.args:
u = request.args["u"][0]
elif "account" in request.args:
u = request.args["account"][0]
else:
u = request.getCookie("thimbl-login")
if u:
s = u.split("@")
if len(s) == 2:
c = s[0].split(":")
if len(c) == 2:
(user, password) = c
elif "password" in request.args:
user = s[0]
password = request.args["password"][0]
host = s[1]
port = 22
if ":" in host:
(host, port) = host.split(":")
reactor.callLater(0, account, user, password, host, port)
if not host:
request.finish()