jupyter / terminado

Terminals served by tornado websockets
http://terminado.readthedocs.org/en/latest/
BSD 2-Clause "Simplified" License
365 stars 94 forks source link

Terminado with Unix sockets #192

Open iliajie opened 1 year ago

iliajie commented 1 year ago

Hello,

Thank you for an amazing terminal emulator!

Can anyone please provide an example of how Terminado could be used with Unix socket file instead of TCP port, in familiar example as in demos/unique.py? Is this even possible?

KoopaKing commented 1 year ago

You should take a peek at https://gist.github.com/superduper/5579037#file-tornado-unix-socket-py

I think you just need to use tornado's tornado.netutil.bind_unix_socket instead of tornado.web.Application::listen

iliajie commented 1 year ago

Thanks, Chris. It seems to work on the first glance, but it would be more useful for the future reader to have it placed in demos alongside with other examples.

iliajie commented 1 year ago

I have tried:

"""A separate terminal for every websocket opened.
"""
import tornado.ioloop
import tornado.web
from tornado.httpserver import HTTPServer
from tornado.options import options, define
from tornado.netutil import bind_unix_socket

# This demo requires tornado_xstatic and XStatic-term.js
import tornado_xstatic
from common_demo_stuff import STATIC_DIR, TEMPLATE_DIR, run_and_show_browser

from terminado import TermSocket, UniqueTermManager

class TerminalPageHandler(tornado.web.RequestHandler):
    def get(self):
        return self.render(
            "termpage.html",
            static=self.static_url,
            xstatic=self.application.settings["xstatic_url"],
            ws_url_path="/websocket",
        )

def main(argv):
    term_manager = UniqueTermManager(shell_command=["bash"])
    handlers = [
        (r"/websocket", TermSocket, {"term_manager": term_manager}),
        (r"/", TerminalPageHandler),
        (r"/xstatic/(.*)", tornado_xstatic.XStaticFileHandler, {"allowed_modules": ["termjs"]}),
    ]
    app = tornado.web.Application(
        handlers,
        static_path=STATIC_DIR,
        template_path=TEMPLATE_DIR,
        xstatic_url=tornado_xstatic.url_maker("/xstatic/"),
    )
    server = HTTPServer(app)
    socket = bind_unix_socket("/run/terminado/root.sock")
    server.add_socket(socket)
    tornado.ioloop.IOLoop.instance().start()

if __name__ == "__main__":
    main([])

After the service was started the root.sock file was created under /run/terminado directory as expected. All seems good so far.

Now, using Apache config under VirtualServer I have created ProxyPass:

ProxyPass / unix:/run/terminado/root.sock|ws://127.0.0.1/

.. and when visiting http://debian11-gpl.synology/ (my local test domain) I have it connected and the page loads with the expected source:

<!doctype html>
<head>
<meta charset="UTF-8" />
<title>pyxterm</title>
<!--
pyxterm: Basic Python socket implementation for term.js
Example template
Modified by: R. Saravanan <sarava@sarava.net> 2014
Original Copyright (c) 2012-2013, Christopher Jeffrey (MIT License)
-->
<style>
html {
background: #555;
}
h1 {
margin-bottom: 20px;
font: 20px/1.5 sans-serif;
}
</style>
<script src="/xstatic/termjs/term.js?v=68946c0396179ab51b8f8a8cdae05c3be1b0be94358a44ea00dfcfe698039c97307980ace681e20ef14072581338cfe7c72f9229259a6735b1571dc6ca78068c"></script>
<script src="/static/terminado.js?v=6c5ebb6a6d9c68385e908aa106f8db64b66cff67becacefe8f6bc13defbd1cc25a35d919c31faee93c28b942689811cf66a77ddbae04a9b72eb6402a849a8378"></script>
<script>
...
...
...
<span id="dummy-screen-rows" style="visibility:hidden;">01234567890123456789012345678901234567890123456789012345678901234567890123456789</span>
</pre>
</body>

.. however the following error is returned on the console:

image

I have no clue right now why websocket is not being proxied to Terminado socket? Is the problem in Apache config or Terminado config?

Also, is there a way to add/pass so called webprefix to Terminado internal links, so the links would be prefixed and have a look of src="/webprefix/xstatic/termjs/term.js" to load links as expected when proxied to a sub-dir?

iliajie commented 1 year ago

@KoopaKing Chris, could you please check if the Python script part in example above is correct for Terminado to work with Unix sockets?