-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbridge.py
168 lines (127 loc) · 4.23 KB
/
bridge.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#! -*- coding: utf-8 -*-
import paramiko
from channels.exceptions import WebsocketCloseException
from .utils import Switch
from .ioloop import IOLoop
class Credential(object):
def __init__(self, **kwargs):
self.instance = kwargs.get('instance', None)
self.protocol = kwargs.get('protocol', None)
def auth(self):
raise NotImplementedError
class SSHCredential(Credential):
def auth(self):
from xm2cloud_cmp.models import Host
host_queryset = Host.objects.filter(pk=self.instance)
if not host_queryset:
return {}
host = host_queryset[0]
for case in Switch(host.auth.authtype):
if case('password'):
crendent = {
'hostname': host.remoteip,
'port': host.ssh_port,
'username': host.auth.username,
'password': host.auth.password,
}
return crendent
if case('authfile'):
break
if case():
return {}
class IOLoopState(object):
started = False
class Bridge(object):
def __init__(self, channel):
self._id = None
self._shell = None
self._terminal = None
self._channel = channel
self._state = IOLoopState
@property
def id(self):
return self._id
@property
def shell(self):
return self._shell
@property
def terminal(self):
return self._terminal
@property
def channel(self):
return self._channel
@property
def state(self):
return self._state
def auth(self, **kwargs):
raise NotImplementedError
def open(self, **kwargs):
raise NotImplementedError
def ioloop_start(self):
if not self._state.started:
IOLoop.instance().start()
self._state.started = True
else:
if not IOLoop.instance().isAlive():
IOLoop.instance().start()
self._state.started = True
def ioloop_register(self):
IOLoop.instance().register(self)
IOLoop.instance().add_future(self.terminal_to_web())
def invoke_shell(self, **kwargs):
raise NotImplementedError
def web_to_terminal_send(self, data):
raise NotImplementedError
def terminal_to_web_send(self, data):
raise NotImplementedError
def web_to_terminal(self, data):
self.web_to_terminal_send(data)
def terminal_to_web(self):
yield self._id
is_connected = True
while is_connected:
result = yield
if self._channel:
try:
self.terminal_to_web_send(result)
except WebsocketCloseException:
is_connected = False
self.destroy()
def destroy(self):
if not self._terminal:
return
self._terminal.close()
class SSHBridge(Bridge):
def __init__(self, channel):
super(SSHBridge, self).__init__(channel)
self._terminal = paramiko.SSHClient()
self._terminal.set_missing_host_key_policy(paramiko.AutoAddPolicy())
def auth(self, **kwargs):
return SSHCredential(**kwargs).auth()
def open(self, **kwargs):
credent = self.auth(**kwargs)
self._terminal.connect(**credent)
self.invoke_shell(**kwargs)
self.ioloop_start()
self.ioloop_register()
def invoke_shell(self, **kwargs):
terminal_term = kwargs.get('term', 'xterm')
terminal_cols = int(kwargs.get('width', 80))
terminal_rows = int(kwargs.get('height', 24))
self._shell = self._terminal.invoke_shell(
term=terminal_term, width=terminal_cols,
height=terminal_rows
)
self._shell.setblocking(0)
self._id = self._shell.fileno()
def web_to_terminal_send(self, data):
if not self._shell:
return
self._shell.send(data)
def terminal_to_web_send(self, data):
channel_data = {'text': data}
self._channel.send(channel_data)
class BridgeFactory(type):
def __new__(cls, *args, **kwargs):
protocol = kwargs.get('protocol', 'ssh')
return {'ssh': SSHBridge}.get(protocol, SSHBridge)