From ba2692b5e9a09ecc7b879951a72f36f06278f5e0 Mon Sep 17 00:00:00 2001 From: hedy Date: Fri, 1 Apr 2022 09:45:56 +0800 Subject: [PATCH] Add example config and CGI Also wrap lines in readme --- README.md | 37 +++++++++++++++++++++++++--------- examples/cgi/echo | 6 ++++++ examples/cgi/env.sh | 5 +++++ examples/cgi/greet.sh | 10 +++++++++ examples/config_local.conf | 13 ++++++++++++ examples/config_pubnix.conf | 19 +++++++++++++++++ examples/config_uservhost.conf | 18 +++++++++++++++++ examples/spsrv.debian.service | 15 ++++++++++++++ 8 files changed, 114 insertions(+), 9 deletions(-) create mode 100755 examples/cgi/echo create mode 100755 examples/cgi/env.sh create mode 100755 examples/cgi/greet.sh create mode 100644 examples/config_local.conf create mode 100644 examples/config_pubnix.conf create mode 100644 examples/config_uservhost.conf create mode 100644 examples/spsrv.debian.service diff --git a/README.md b/README.md index 15adc98..4928ff5 100644 --- a/README.md +++ b/README.md @@ -27,8 +27,10 @@ A static spartan server with many features: you have two options for now: -### with `go get` -first, you need to have go installed and have a folder `~/go` with `$GOPATH` pointing to it. +### Option 1: with `go get` + +first, you need to have go installed and have a folder `~/go` with `$GOPATH` +pointing to it. ``` go get git.sr.ht/~hedy/spsrv @@ -38,30 +40,40 @@ there will be a binary at `~/go/bin/` with the source code at `~/go/src/` feel free to move the binary somewhere else like `/usr/sbin/` -### or just build it yourself + +### Option 2: just build it yourself + run `git clone https://git.sr.ht/~hedy/spsrv` from any directory and `cd spsrv` make sure you have go installed and working. ``` +git checkout v0.0.0 # optionally pin a specific tag go build ``` when it finishes, the binary will be in the current directory. + ### otherwise... -If you don't have/want go installed, you can contact me, and if you're lucky, I have the same OS as you and you can use my compiled binary (lol). I'll eventually have automated uploads of binaries for various architectures for each release in the future. +If you don't have/want go installed, you can contact me, and if you're lucky, I +have the same OS as you and you can use my compiled binary (lol). I'll +eventually have automated uploads of binaries for various architectures for +each release in the future. ## configuration + The default config file location is `/etc/spsrv.conf` you can specify your own path by running spsrv like ``` spsrv -c /path/to/file.conf ``` -You don't need a config file to have spsrv running, it will just use the default values. +You don't need a config file to have spsrv running, it will just use the +default values. + ### config options @@ -93,6 +105,8 @@ Here are the config options and their default values * `CGIPaths=["cgi/"]`: list of paths where world-executable files will be run as CGI processes. These paths would be checked if it prefix the requested path. For the default value, a request of `/cgi/hi.sh` (requesting to `./public/cgi/hi.sh`, for example) will run `hi.sh` script if it's world executable. * `usercgiEnable=false`: enable running user's CGI scripts too. This is dangerous as spsrv does not (yet) change the Uid of the CGI process, hence the process would be ran by the same user that is running the server, which could mean write access to configuration files, etc. Note that this option will be assumed `false` if `userdirEnable` is set to `false`. Which means if user directories are not enabled, there will be no per-user CGI. +Check out some example configuraton in the [examples/](examples/) directory. + ## CLI You can override values in config file if you supply them from the command line: @@ -106,7 +120,10 @@ Usage: spsrv [ [ -c -h -p -d ] | --help ] -p, --port int Port to listen to ``` -Note that you *cannot* set the hostname or the dir path to `,` because spsrv uses that to check whether you provided an option. You can't set port to `0` either, sorry, this limitation comes with the advantage of being able to override config values from the command line. +Note that you *cannot* set the hostname or the dir path to `,` because spsrv +uses that to check whether you provided an option. You can't set port to `0` +either, sorry, this limitation comes with the advantage of being able to +override config values from the command line. There are no arguments wanted when running spsrv, only options as listed above :) @@ -128,12 +145,15 @@ DATA_LENGTH # Input data length The data block, if any, will be piped as stdin to the CGI process. -Keep in mind that CGI scripts (as of now) are run by the same user as the server process, hence it is generally dangerous for allowing users to have their own CGI scripts. See configuration section for more details. +Keep in mind that CGI scripts (as of now) are run by the same user as the +server process, hence it is generally dangerous for allowing users to have +their own CGI scripts. See configuration section for more details. + +Check out some example CGI scripts in the [examples/](examples/) directory. ## todo -``` - [x] /folder to /folder/ redirects - [x] directory listing - [ ] logging to files @@ -159,4 +179,3 @@ Keep in mind that CGI scripts (as of now) are run by the same user as the server README: - [ ] Add example confs - [ ] Add example .service files -``` diff --git a/examples/cgi/echo b/examples/cgi/echo new file mode 100755 index 0000000..bfe557b --- /dev/null +++ b/examples/cgi/echo @@ -0,0 +1,6 @@ +#!/usr/bin/env sh + +# Echos back whatever data you send it + +printf "2 application/octet-stream\r\n" +cat /dev/stdin diff --git a/examples/cgi/env.sh b/examples/cgi/env.sh new file mode 100755 index 0000000..a4ab273 --- /dev/null +++ b/examples/cgi/env.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env sh + +printf "2 text/gemini\r\n" +whoami +printenv diff --git a/examples/cgi/greet.sh b/examples/cgi/greet.sh new file mode 100755 index 0000000..7da1243 --- /dev/null +++ b/examples/cgi/greet.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env sh + +# User can enter their name and this will greet them +# Example inpu link: +# +# =: greet.sh Enter your name + +printf "2 text/plain\r\n" +name=$(cat /dev/stdin) +echo "Hello, ${name:-World}!" diff --git a/examples/config_local.conf b/examples/config_local.conf new file mode 100644 index 0000000..a41fe6d --- /dev/null +++ b/examples/config_local.conf @@ -0,0 +1,13 @@ +# Example config for a local testing server + +hostname="localhost" +port=3000 + +rootDir = "./public" + +# attempt CGI for all requests +cgiPaths=[""] + +dirlistEnable=true +dirlistSort="time" +dirlistTitles=true diff --git a/examples/config_pubnix.conf b/examples/config_pubnix.conf new file mode 100644 index 0000000..7a1556a --- /dev/null +++ b/examples/config_pubnix.conf @@ -0,0 +1,19 @@ +# Example config for a multi-user unix server + +# accept any hostname +hostname="" +port=300 +rootdir="/var/spartan" + +# allow CGI for all files - so you can have your root index.gmi do a user +# listing +cgipaths=[""] + +userdirEnable=true +# each user would have their content be at /home/user/public_spartan, +# accessible via spartan://host.name/~user/ +userdir="public_spartan" + +# enable per-user CGI (you might wanna be running the spartan server under +# another user, such as spartan:nogroup) +usercgiEnable=true diff --git a/examples/config_uservhost.conf b/examples/config_uservhost.conf new file mode 100644 index 0000000..914cfd0 --- /dev/null +++ b/examples/config_uservhost.conf @@ -0,0 +1,18 @@ +# Example config for allowing per-user vhosts on your pubnix, i.e.: allownig +# spartan://user.example.org + +# you must set a non-empty hostname for user-vhost to work +hostname="example.org" +port=300 + +userdirEnable=true + +# each user would have their content be at /home/user/public_spartan, +# accessible via both spartan://example.org/~user/ and +# spartan://user.example.org/ +userdir="public_spartan" +userSubdomains=true + +# enable per-user CGI (you might wanna be running the spartan server under +# another user, such as spartan:nogroup) +usercgiEnable=true diff --git a/examples/spsrv.debian.service b/examples/spsrv.debian.service new file mode 100644 index 0000000..f0841f1 --- /dev/null +++ b/examples/spsrv.debian.service @@ -0,0 +1,15 @@ +[Unit] +Description=spsrv +After=network.target + +[Service] +Type=simple +Restart=always +RestartSec=5 +User=spartan +Group=spartan +ExecStart=/usr/local/bin/spsrv -c /etc/spsrv.conf + +[Install] +WantedBy=multi-user.target +