The Example Plugin

Todo

Write documentation on example.js.

example.py - A plugin to demonstrate how to write a Python plugin for Gate One. Specifically, how to write your own web handlers, WebSocket actions, and take advantage of all the available hooks and built-in functions.

Tip

This plugin is heavily commented with useful information. Click on the [source] links to the right of any given class or function to see the actual code.

Hooks

This Python plugin file implements the following hooks:

hooks = {
    'Web': [(r"/example", ExampleHandler)],
    'WebSocket': {
        'example_action': example_websocket_action
    },
    'Escape': example_opt_esc_handler,
    #'Auth': create_user_ssh_dir
}

Docstrings

class example.ExampleHandler(application, request, **kwargs)[source]

This is how you add a URL handler to Gate One... This example attaches itslef to https://<your Gate One server>/example in the 'Web' hook at the bottom of this file. It works just like any Tornado RequestHandler. See:

http://www.tornadoweb.org/documentation/web.html

...for documentation on how to write a tornado.web.RequestHandler for the Tornado framework. Fairly boilerplate stuff.

Note

The only reason we use gateone.BaseHandler instead of a vanilla tornado.web.RequestHandler is so we have access to Gate One's gateone.BaseHandler.get_current_user() function.

get(*args, **kwargs)[source]

Handle an HTTP GET request to this RequestHandler. Connect to:

https://<your Gate One server>/example

...to try it out.

post()[source]

Example Handler for an HTTP POST request. Doesn't actually do anything.

example.example_websocket_action(message, tws=None)[source]

This WebSocket action gets exposed to the client automatically by way of the 'WebSocket' hook at the bottom of this file. The way it works is like this:

How The WebSocket Hook Works

Whenever a message is received via the WebSocket Gate One will automatically decode it into a Python dict (only JSON-encoded messages are accepted). Any and all keys in that dict will be assumed to be 'actions' (just like GateOne.Net.actions but on the server) such as this one. If the incoming key matches a registered action that action will be called like so:

key(value)
# ...or just:
key() # If the value is None ('null' in JavaScript)

...where key is the action and value is what will be passed to said action as an argument. Since Gate One will automatically decode the message as JSON the value will typically be passed to actions as a single dict. You can provide different kinds of arguments of course but be aware that their ordering is unpredictable so always be sure to either pass one argument to your function (assuming it is a dict) or 100% keyword arguments.

The tws keyword argument must always be present in WebSocket actions. It represents the user's current instance of Gate One's TerminalWebSocket class. Think of it as the equivalent of, "self" inside of any given TerminalWebSocket function. For example, the following function inside of TerminalWebSocket (inside of gateone.py):

def pong(self, timestamp): # Docstring removed to save space
    message = {'pong': timestamp}
    self.write_message(json_encode(message))

...could be written inside of a plugin like so:

def pong(timestamp, tws=None):
    message = {'pong': timestamp}
    tws.write_message(json_encode(message))

Note

Notice that the ussage of self has become tws.

The typical naming convention for WebSocket actions is: <plugin name>_. Whether or not your action names match your function names is up to you. All that matters is that you line up an action (string) with a function in hooks['WebSocket'] (see below).

This WebSocket action duplicates the functionality of Gate One's built-in gateone.TerminalWebSocket.pong() function. You can see how it is called by the client (browser) inside of example.js (which is in this plugin's 'static' dir).

example.example_opt_esc_handler(message, tws)[source]

Gate One includes a mechanism for plugins to send messages from terminal programs directly to plugins written in Python. It's called the "Special Optional Escape Sequence Handler" or SOESH for short. Here's how it works: Whenever a terminal program emits, "x1b]_;" it gets detected by Gate One's Terminal class (which lives in terminal.py) and it will execute whatever callback is registered for SOESH. Inside of Gate One this callback will always be gateone.TerminalWebSocket.esc_opt_handler().

Table Of Contents

Previous topic

The SSH Plugin

Next topic

Embedding Gate One Into Other Applications

This Page