Mercurial > libervia-backend
annotate sat/plugins/plugin_dbg_manhole.py @ 2755:12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
this plugin open a telnet server which open a Python interpreter and allows to inspect the backend while it is running.
To enable it, the settings "manhole_debug_dangerous_port_int" must be set to the desired TCP port in [DEFAULT] section of sat.conf.
A warning will be logged if the server is enabled.
Currently only a telnet server is available, and no login is required. This may change in the futur to enable login/password and ssh access, or even an XMPP access.
This is a debugging feature and must only be used during development.
Once in the Python interpreter, the "host" variable is accessible.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 04 Jan 2019 18:59:24 +0100 |
parents | |
children | e2005dd39c92 |
rev | line source |
---|---|
2755
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for debugging, using a manhole |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _ |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.constants import Const as C |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.conch.insults import insults |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.conch.telnet import TelnetTransport, TelnetBootstrapProtocol |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from twisted.internet import reactor, protocol |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.conch.manhole import ColoredManhole |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 log = getLogger(__name__) |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 PLUGIN_INFO = { |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 C.PI_NAME: u"Manhole debug plugin", |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 C.PI_IMPORT_NAME: u"manhole", |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 C.PI_TYPE: u"DEBUG", |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 C.PI_PROTOCOLS: [], |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 C.PI_DEPENDENCIES: [], |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 C.PI_MAIN: u"Manhole", |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 C.PI_HANDLER: u"no", |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 C.PI_DESCRIPTION: _(u"""Debug plugin to have a telnet server"""), |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 } |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 class Manhole(object): |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 def __init__(self, host): |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 self.host = host |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 port = int(host.memory.getConfig(None, "manhole_debug_dangerous_port_int")) |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 if port: |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 self.startManhole(port) |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 def startManhole(self, port): |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 log.warning(_(u"/!\\ Manhole debug server activated, be sure to not use it in " |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 u"production, this is dangerous /!\\")) |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 log.info(_(u"You can connect to manhole server using telnet on port {port}") |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 .format(port=port)) |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 f = protocol.ServerFactory() |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 namespace = { |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 u"host": self.host, |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 } |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 f.protocol = lambda: TelnetTransport(TelnetBootstrapProtocol, |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 insults.ServerProtocol, |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 ColoredManhole, |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 namespace=namespace, |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 ) |
12d1ca646af1
plugin manhole: manhole debug plugin, first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 reactor.listenTCP(port, f) |