forked from kivy/kivy-remote-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
104 lines (90 loc) · 2.82 KB
/
main.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
__version__ = '0.1'
# install_twisted_rector must be called before importing and using the reactor
from kivy.support import install_twisted_reactor
install_twisted_reactor()
import socket
import fcntl
import struct
from twisted.internet import reactor
from twisted.cred import portal, checkers
from twisted.conch import manhole, manhole_ssh
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty
from kivy.app import App
app = None
Builder.load_string('''
<MainScreen>:
Image:
source: 'background.png'
allow_stretch: True
keep_ratio: False
BoxLayout:
size_hint_y: .2
pos_hint: {'top': 1}
canvas:
Color:
rgba: .1, .1, .1, .1
Rectangle:
pos: self.pos
size: self.size
Widget
Image:
source: 'icon.png'
mipmap: True
size_hint_x: None
width: 100
Label:
text: 'Remote Kivy'
font_size: 30
size_hint_x: None
width: self.texture_size[0] + 20
Widget
Label:
text: 'ssh -p8000 admin@{0}'.format(root.lan_ip)
font_size: 20
size_hint_y: .8
''')
def getManholeFactory(namespace, **passwords):
realm = manhole_ssh.TerminalRealm()
def getManhole(_):
return manhole.ColoredManhole(namespace)
realm.chainedProtocolFactory.protocolFactory = getManhole
p = portal.Portal(realm)
p.registerChecker(
checkers.InMemoryUsernamePasswordDatabaseDontUse(**passwords))
f = manhole_ssh.ConchFactory(p)
return f
class MainScreen(FloatLayout):
lan_ip = StringProperty('127.0.0.1')
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
ip = socket.gethostbyname(socket.gethostname())
if ip.startswith('127.'):
interfaces = ['eth0', 'eth1', 'eth2', 'wlan0', 'wlan1', 'wifi0',
'ath0', 'ath1', 'ppp0']
for ifname in interfaces:
try:
ip = self.get_interface_ip(ifname)
break
except IOError:
pass
self.lan_ip = ip
def get_interface_ip(self, ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
class RemoteKivyApp(App):
def on_pause(self):
return True
def build(self):
global app
app = self
self.connection = reactor.listenTCP(8000,
getManholeFactory(globals(), admin='kivy'))
return MainScreen()
if __name__ == '__main__':
RemoteKivyApp().run()