Skip to main content

An Experimental Gemini Server

Project description

Jetforce

An experimental TCP server for the new, under development Gemini Protocol. Learn more about Gemini here.

Rocket Launch

Features

  • A built-in static file server with support for gemini directories and CGI scripts.
  • Lightweight, single-file framework with zero external dependencies.
  • Modern python codebase with type hinting and black style formatting.
  • Supports concurrent connections using an asynchronous event loop.
  • Extendable components that loosely implement the WSGI server/application pattern.

Installation

Requires Python 3.7+

The latest release can be installed from PyPI:

$ pip install jetforce

Or, clone the repository and run the script directly:

$ git clone https://github.com/michael-lazar/jetforce
$ cd jetforce
$ python3 jetforce.py

Usage

Use the --help flag to view command-line options:

$ jetforce --help
usage: jetforce [-h] [-V] [--host HOST] [--port PORT] [--hostname HOSTNAME]
                [--tls-certfile FILE] [--tls-keyfile FILE] [--tls-cafile FILE]
                [--tls-capath DIR] [--dir DIR] [--cgi-dir DIR]
                [--index-file FILE]

An Experimental Gemini Protocol Server

optional arguments:
  -h, --help           show this help message and exit
  -V, --version        show program's version number and exit
  --host HOST          Server address to bind to (default: 127.0.0.1)
  --port PORT          Server port to bind to (default: 1965)
  --hostname HOSTNAME  Server hostname (default: localhost)
  --tls-certfile FILE  Server TLS certificate file (default: None)
  --tls-keyfile FILE   Server TLS private key file (default: None)
  --tls-cafile FILE    A CA file to use for validating clients (default: None)
  --tls-capath DIR     A directory containing CA files for validating clients
                       (default: None)
  --dir DIR            Root directory on the filesystem to serve (default:
                       /var/gemini)
  --cgi-dir DIR        CGI script directory, relative to the server's root
                       directory (default: cgi-bin)
  --index-file FILE    If a directory contains a file with this name, that
                       file will be served instead of auto-generating an index
                       page (default: index.gmi)

Setting the hostname

The server's hostname should be set to the DNS name that you expect to receive traffic from. For example, if your jetforce server is running on "gemini://cats.com", you should set the hostname to "cats.com". Any URLs that do not match this hostname will be refused by the server, including URLs that use a direct IP address such as "gemini://174.138.124.169".

Setting the host

The server's host should be set to the local socket that you want to bind to:

  • --host "127.0.0.1" - Accept local connections only
  • --host "0.0.0.0" - Accept remote connections over IPv4
  • --host "::" - Accept remote connections over IPv6
  • --host "" - Accept remote connections over any interface (IPv4 + IPv6)

TLS Certificates

The gemini specification requires that all connections be sent over TLS.

If you do not provide a TLS certificate file using the --tls-certfile flag, jetforce will automatically generate a temporary cert for you to use. This is great for making development easier, but before you expose your server to the public internet you should configure something more permanent. You can generate your own self-signed server certificate, or obtain one from a Certificate Authority like Let's Encrypt.

Here's the OpenSSL command that jetforce uses to generate a self-signed cert:

$ openssl req -newkey rsa:2048 -nodes -keyout {hostname}.key \
    -nodes -x509 -out {hostname}.crt -subj "/CN={hostname}"

Jetforce also supports verified client TLS certificates. You can specify your client CA with the --tls-cafile or --tls-capath flags. Verified connections will have the REMOTE_USER variable added to their environment, which contains the client certificate's CN attribute. Instructions on how to generate TLS client certificates are outside of the scope of this readme, but you can find many helpful tutorials online.

There are currently no plans to support unverified (transient) client certificates. This is due to a technical limitation of the python standard library's ssl module, which is described in detail here.

Static Files

Jetforce will serve static files in the /var/gemini/ directory:

  • Files ending with .gmi will be interpreted as the text/gemini type
  • If a directory is requested, jetforce will look for a file in that directory with the name of index.gmi
    • If it exists, the index file will be returned
    • Otherwise, jetforce will generate a directory listing

CGI Scripts

Jetforce supports a simplified version of CGI scripting. It doesn't exactly follow the RFC 3875 specification for CGI, but it gets the job done for the purposes of Gemini.

Any executable file placed in the server's cgi-bin/ directory will be considered a CGI script. When a CGI script is requested by a gemini client, the jetforce server will execute the script and pass along information about the request using environment variables:

Variable Name Example
GATEWAY_INTERFACE GCI/1.1
GEMINI_URL gemini://mozz.us/cgi-bin/debug.cgi?foobar
HOSTNAME mozz.us
PATH_INFO /cgi-bin/debug.cgi
QUERY_STRING foobar
REMOTE_ADDR 10.10.0.2
REMOTE_HOST 10.10.0.2
SCRIPT_NAME /usr/local/www/mozz/gemini/cgi-bin/debug.cgi
SERVER_NAME mozz.us
SERVER_PORT 1965
SERVER_PROTOCOL GEMINI
SERVER_SOFTWARE jetforce/0.0.7

The CGI script must then write the gemini response to the stdout stream. This includes the status code and meta string on the first line, and the optional response body on subsequent lines. The bytes generated by the CGI script will be forwarded verbatim to the gemini client, without any additional modification by the server.

Deployment

Jetforce is intended to be run behind a process manager that handles daemonizing the script, redirecting output to system logs, etc. I prefer to use systemd for this because it's installed on my operating system and easy to set up.

Here's how I configure my server over at gemini://mozz.us:

# /etc/systemd/system/jetforce.service
[Unit]
Description=Jetforce Server

[Service]
Type=simple
Restart=always
RestartSec=5
Environment="PYTHONUNBUFFERED=1"
ExecStart=/usr/local/bin/jetforce \
    --host 0.0.0.0 \
    --port 1965 \
    --hostname mozz.us \
    --dir /var/gemini \
    --tls-certfile /etc/letsencrypt/live/mozz.us/fullchain.pem \
    --tls-keyfile /etc/letsencrypt/live/mozz.us/privkey.pem \
    --tls-cafile /etc/pki/tls/jetforce_client/ca.cer

[Install]
WantedBy=default.target
  • --host 0.0.0.0 allows the server to accept external connections from any IP address over IPv4.
  • PYTHONUNBUFFERED=1 disables buffering stderr and is sometimes necessary for logging to work.
  • --tls-certfile and --tls-keyfile point to my WWW server's Let's Encrypt certificate chain.
  • --tls-cafile points to a self-signed CA that I created in order to test accepting client TLS connections.

With this service installed, I can start and stop the server using

systemctl start jetforce
systemctl stop jetforce

And I can view the server logs using

journalctl -u jetforce -f

WARNING

The internet can be a scary place. You (yes you!) are responsible for securing your server and setting up appropriate access permissions. This likely means not running jetforce as the root user. Security best practices are outside of the scope of this document and largely depend on your individual threat model.

License

This project is licensed under the Floodgap Free Software License.

The Floodgap Free Software License (FFSL) has one overriding mandate: that software using it, or derivative works based on software that uses it, must be free. By free we mean simply "free as in beer" -- you may put your work into open or closed source packages as you see fit, whether or not you choose to release your changes or updates publicly, but you must not ask any fee for it.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Jetforce-0.2.2.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

Jetforce-0.2.2-py3-none-any.whl (25.3 kB view details)

Uploaded Python 3

File details

Details for the file Jetforce-0.2.2.tar.gz.

File metadata

  • Download URL: Jetforce-0.2.2.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.6

File hashes

Hashes for Jetforce-0.2.2.tar.gz
Algorithm Hash digest
SHA256 2600103ba35590b3884b97c764ba34e7da991ac39a0b03621da4ae0760627060
MD5 2fb277c95fbd0176d42f4ca1d462e3f6
BLAKE2b-256 b15390c7511925b1c800e50ad9941b21211aa22349cfe08a21f4b92a6a50d9d8

See more details on using hashes here.

File details

Details for the file Jetforce-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: Jetforce-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 25.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.6

File hashes

Hashes for Jetforce-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 258080691e65cc8c4a9bab85a8a1b8969199c76629c4cea95ed97ca1da5712b5
MD5 b462688dd64f8c70e8b3e09dfcdbe195
BLAKE2b-256 d89a6143e903b87032cbb570e5a01c4e5c2da4fcd351d788fa8d300ce0ad57b9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page