| 1 | # -*- mode: perl -*- | ||
| 2 | # Copyright (C) 2017–2021 Alex Schroeder <alex@gnu.org> | ||
| 3 | |||
| 4 | # This program is free software: you can redistribute it and/or modify it under | ||
| 5 | # the terms of the GNU Affero General Public License as published by the Free | ||
| 6 | # Software Foundation, either version 3 of the License, or (at your option) any | ||
| 7 | # later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 10 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU Affero General Public License along | ||
| 15 | # with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | =encoding utf8 | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | App::Phoebe::Spartan - implement the Spartan protocol for Phoebe | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | This extension serves your Gemini pages via the Spartan protocol and generates a | ||
| 26 | few automatic pages for you, such as the main page. | ||
| 27 | |||
| 28 | B<Warning!> If you install this code, anybody can write to your site using the | ||
| 29 | Spartan protocol. There is no token being checked. | ||
| 30 | |||
| 31 | To configure, you need to specify the Spartan port(s) in your Phoebe config | ||
| 32 | file. The default port is 300. This is a priviledge port. Thus, you either need | ||
| 33 | to grant Perl the permission to listen on a priviledged port, or you need to run | ||
| 34 | Phoebe as a super user. Both are potential security risk, but the first option | ||
| 35 | is much less of a problem, I think. | ||
| 36 | |||
| 37 | If you want to try this, run the following as root: | ||
| 38 | |||
| 39 | setcap 'cap_net_bind_service=+ep' $(which perl) | ||
| 40 | |||
| 41 | Verify it: | ||
| 42 | |||
| 43 | getcap $(which perl) | ||
| 44 | |||
| 45 | If you want to undo this: | ||
| 46 | |||
| 47 | setcap -r $(which perl) | ||
| 48 | |||
| 49 | Once you do that, no further configuration is necessary. Just add the following | ||
| 50 | to your F<config> file: | ||
| 51 | |||
| 52 | use App::Phoebe::Spartan; | ||
| 53 | |||
| 54 | The alternative is to use a port number above 1024. Here's a way to do that: | ||
| 55 | |||
| 56 | package App::Phoebe::Spartan; | ||
| 57 | our $spartan_port = 7000; # listen on port 7000 | ||
| 58 | use App::Phoebe::Spartan; | ||
| 59 | |||
| 60 | If you don't do any of the above, you'll get a permission error on startup: | ||
| 61 | "Mojo::Reactor::Poll: Timer failed: Can't create listen socket: Permission | ||
| 62 | denied…" | ||
| 63 | |||
| 64 | =cut | ||
| 65 | |||
| 66 | package App::Phoebe::Spartan; | ||
| 67 | use App::Phoebe qw($server $log @main_menu get_ip_numbers space host_regex space_regex run_extensions | ||
| 68 | serve_index serve_page serve_raw serve_html serve_history serve_diff save_page | ||
| 69 | blog print_link text); | ||
| 70 | use Modern::Perl; | ||
| 71 | use URI::Escape; | ||
| 72 | use Encode qw(encode_utf8 decode_utf8 decode); | ||
| 73 | use utf8; | ||
| 74 | no warnings 'redefine'; | ||
| 75 | |||
| 76 | our $spartan_port ||= 300; | ||
| 77 | |||
| 78 | use Mojo::IOLoop; | ||
| 79 | |||
| 80 | # start the loop after configuration (so that the user can change $spartan_port) | ||
| 81 | Mojo::IOLoop->next_tick(\&spartan_startup); | ||
| 82 | |||
| 83 | sub spartan_startup { | ||
| 84 | for my $host (keys %{$server->{host}}) { | ||
| 85 | for my $address (get_ip_numbers($host)) { | ||
| 86 | for my $port (ref $spartan_port ? @$spartan_port : $spartan_port) { | ||
| 87 | $log->info("$host: listening on $address:$port (Spartan)"); | ||
| 88 | Mojo::IOLoop->server({ | ||
| 89 | address => $address, | ||
| 90 | port => $port, | ||
| 91 | } => sub { | ||
| 92 | my ($loop, $stream) = @_; | ||
| 93 | my $buffer; | ||
| 94 | my $request_host; | ||
| 95 | my $path; | ||
| 96 | my $length; | ||
| 97 | $stream->on(read => sub { | ||
| 98 | my ($stream, $bytes) = @_; | ||
| 99 | $log->debug("Received " . length($bytes) . " bytes via Spartan"); | ||
| 100 | $buffer .= $bytes; | ||
| 101 | if (not $length and $buffer =~ /^(.*)\r\n/) { | ||
| 102 | my $request_line = $1; | ||
| 103 | if (($request_host, $path, $length) = $request_line =~ /^(\S+) (\S+) (\d+)/) { | ||
| 104 | my $re = host_regex(); | ||
| 105 | if ($request_host !~ /^($re)$/) { | ||
| 106 | result($stream, "4", "We do not serve $request_host!"); | ||
| 107 | $stream->close_gracefully(); | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | $buffer =~ s/^.*\r\n//; # strip request line | ||
| 111 | } else { | ||
| 112 | result($stream, "4", "This request is garbage!"); | ||
| 113 | $stream->close_gracefully(); | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | } | ||
| 117 | if (defined $length and $length == length($buffer)) { | ||
| 118 | serve_spartan($stream, $request_host, $path, $length, $buffer); | ||
| 119 | } else { | ||
| 120 | $log->debug("Waiting for more bytes..."); | ||
| 121 | } | ||
| 122 | }); | ||
| 123 | }); | ||
| 124 | } | ||
| 125 | } | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | sub serve_spartan { | ||
| 130 | my ($stream, $host, $path, $length, $buffer) = @_; | ||
| 131 | eval { | ||
| 132 | local $SIG{'ALRM'} = sub { | ||
| 133 | $log->error("Timeout processing $host $path $length via Spartan"); | ||
| 134 | }; | ||
| 135 | alarm(10); # timeout | ||
| 136 | my $spaces = space_regex(); | ||
| 137 | # note that path always starts with a slash! | ||
| 138 | $log->info("Looking at $host $path $length via Spartan"); | ||
| 139 | my ($space, $id, $n); | ||
| 140 | no warnings 'redefine'; | ||
| 141 | # we cannot import these subroutines and modify them, otherwise the | ||
| 142 | # App::Phoebe code remains unchanged | ||
| 143 | local *gemini_to_url = \&App::Phoebe::to_url; | ||
| 144 | local *App::Phoebe::to_url = \&spartan_to_url; | ||
| 145 | local *old_gemini_link = \&App::Phoebe::gemini_link; | ||
| 146 | local *App::Phoebe::gemini_link = \&spartan_link; | ||
| 147 | local *App::Phoebe::success = \&success; | ||
| 148 | local *App::Phoebe::result = \&result; | ||
| 149 | if (run_extensions($stream, $host, undef, $buffer, $path, $length)) { | ||
| 150 | # config file goes first (note that $path and $length come at the end) | ||
| 151 | } elsif (($space) = $path =~ m!^(?:/($spaces))?(?:/page)?/?$!) { | ||
| 152 | # "up" from page/Alex gives us page or page/ → show main menu | ||
| 153 | spartan_main_menu($stream, $host, space($stream, $host, $space)); | ||
| 154 | } elsif ($path eq "/do/source") { | ||
| 155 | seek DATA, 0, 0; | ||
| 156 | local $/ = undef; # slurp | ||
| 157 | $stream->write(encode_utf8 <DATA>); | ||
| 158 | } elsif ($length == 0 and ($space, $id, $n) = $path =~ m!^(?:/($spaces))?/page/([^/]+)(?:/(\d+))?$!) { | ||
| 159 | $log->debug("Serving $id bytes via Spartan"); | ||
| 160 | serve_page($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n); | ||
| 161 | } elsif ($length > 0 and ($space, $id, $n) = $path =~ m!^(?:/($spaces))?/page/([^/]+)$!) { | ||
| 162 | $log->debug("Saving $length bytes via Spartan"); | ||
| 163 | save_page($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), | ||
| 164 | "text/plain", $buffer, $length); | ||
| 165 | } elsif (($space, $id) = $path =~ m!^(?:/($spaces))?/raw/([^/]+)$!) { | ||
| 166 | serve_raw($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id))); | ||
| 167 | } elsif (($space, $id) = $path =~ m!^(?:/($spaces))?/html/([^/]+)$!) { | ||
| 168 | serve_html($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id))); | ||
| 169 | } elsif (($space, $id) = $path =~ m!^(?:/($spaces))?/history/([^/]+)$!) { | ||
| 170 | serve_history($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), 10); | ||
| 171 | } elsif (($space, $id, $n) = $path =~ m!^(?:/($spaces))?/diff/([^/]+)(?:/(\d+))?$!) { | ||
| 172 | serve_diff($stream, $host, space($stream, $host, $space), decode_utf8(uri_unescape($id)), $n); | ||
| 173 | } elsif (($space) = $path =~ m!^(?:/($spaces))?/do/index$!) { | ||
| 174 | serve_index($stream, $host, space($stream, $host, $space)); | ||
| 175 | } else { | ||
| 176 | $log->info("No handler for $host $path $length via spartan"); | ||
| 177 | result($stream, "5", "I do not know what to do with $host $path $length"); | ||
| 178 | } | ||
| 179 | $log->debug("Done"); | ||
| 180 | }; | ||
| 181 | $log->error("Error: $@") if $@; | ||
| 182 | alarm(0); | ||
| 183 | $stream->close_gracefully(); | ||
| 184 | } | ||
| 185 | |||
| 186 | sub success { | ||
| 187 | my $stream = shift; | ||
| 188 | my $type = shift || 'text/gemini; charset=UTF-8'; | ||
| 189 | $stream->write("2 $type\r\n"); | ||
| 190 | } | ||
| 191 | |||
| 192 | sub result { | ||
| 193 | my $stream = shift; | ||
| 194 | my $code = substr(shift, 0, 1); | ||
| 195 | my $meta = shift; | ||
| 196 | $stream->write("$code $meta\r\n"); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub spartan_to_url { | ||
| 200 | my ($stream, $host, $space, $id, $scheme) = @_; | ||
| 201 | $scheme ||= "spartan"; | ||
| 202 | return gemini_to_url($stream, $host, $space, $id, $scheme); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub spartan_link { | ||
| 206 | my ($stream, $host, $space, $title, $id, $revision) = @_; | ||
| 207 | return "" if $id and $id =~ m!^(?:do/blog|history)!; | ||
| 208 | return old_gemini_link($stream, $host, $space, $title, $id, $revision); | ||
| 209 | } | ||
| 210 | |||
| 211 | sub spartan_main_menu { | ||
| 212 | my $stream = shift; | ||
| 213 | my $host = shift||""; | ||
| 214 | my $space = shift||""; | ||
| 215 | $log->info("Serving main menu"); | ||
| 216 | success($stream); | ||
| 217 | my $page = $server->{wiki_main_page}; | ||
| 218 | if ($page) { | ||
| 219 | $stream->write(encode_utf8 text($stream, $host, $space, $page) . "\n"); | ||
| 220 | } else { | ||
| 221 | $stream->write("# Welcome to Phoebe!\n"); | ||
| 222 | $stream->write("\n"); | ||
| 223 | } | ||
| 224 | blog($stream, $host, $space, 10); | ||
| 225 | for my $id (@{$server->{wiki_page}}) { | ||
| 226 | print_link($stream, $host, $space, $id); | ||
| 227 | } | ||
| 228 | for my $line (@main_menu) { | ||
| 229 | $stream->write(encode_utf8 $line . "\n"); | ||
| 230 | } | ||
| 231 | $stream->write("\n"); | ||
| 232 | print_link($stream, $host, $space, "Index of all pages", "do/index"); | ||
| 233 | # a requirement of the GNU Affero General Public License | ||
| 234 | print_link($stream, $host, undef, "Source code", "do/source"); | ||
| 235 | $stream->write("\n"); | ||
| 236 | } | ||
| 237 | |||
| 238 | 1; |