├── .dockerignore ├── .github ├── dependabot.yml └── workflows │ ├── cargo-audit.yml │ ├── release.sh │ ├── release.yml │ └── test.yml ├── .gitignore ├── .mailmap ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── Cargo.lock ├── Cargo.toml ├── Cross.toml ├── Dockerfile ├── LICENSE-APACHE ├── LICENSE-MIT ├── README.md ├── SECURITY.md ├── content └── index.gmi ├── src ├── certificates.rs ├── codes.rs ├── main.rs └── metadata.rs ├── tests ├── LICENSE-GPL.md ├── README.md ├── data │ ├── .certificates │ │ ├── cert.der │ │ └── key.der │ ├── cert_missing │ │ └── key.der │ ├── content │ │ ├── .meta │ │ ├── .servable-secret │ │ ├── .well-known │ │ │ ├── hidden-file │ │ │ └── servable-secret │ │ ├── example.com │ │ │ └── index.gmi │ │ ├── example.org │ │ │ └── index.gmi │ │ ├── index.gmi │ │ ├── symlink.gmi │ │ ├── symlinked_dir │ │ ├── test │ │ ├── test.gmi │ │ └── testdir │ │ │ ├── .meta │ │ │ ├── a.de.gmi │ │ │ ├── a.gmi │ │ │ └── a.nl.gmi │ ├── directory_traversal.gmi │ ├── dirlist-preamble │ │ ├── .directory-listing-ok │ │ ├── a │ │ ├── b │ │ └── wao spaces │ ├── dirlist │ │ ├── .directory-listing-ok │ │ ├── a │ │ └── b │ ├── key_missing │ │ └── cert.der │ ├── multicert │ │ ├── create_certs.sh │ │ ├── example.com │ │ │ ├── cert.der │ │ │ └── key.der │ │ └── example.org │ │ │ ├── cert.der │ │ │ └── key.der │ └── symlinked_dir │ │ └── file.gmi └── tests.rs └── tools ├── README.md ├── debian ├── README.md ├── gemini.conf ├── gemini.service ├── geminilogs ├── install.sh └── uninstall.sh └── freebsd └── startup.sh /.dockerignore: -------------------------------------------------------------------------------- 1 | ** 2 | 3 | !src/** 4 | !tools/docker/** 5 | !Cross.toml 6 | !Cargo.toml 7 | !Cargo.lock 8 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | --- 2 | version: 2 3 | updates: 4 | - package-ecosystem: github-actions 5 | directory: / 6 | schedule: 7 | interval: daily 8 | open-pull-requests-limit: 10 9 | commit-message: 10 | prefix: chore 11 | include: scope 12 | - package-ecosystem: cargo 13 | directory: / 14 | schedule: 15 | interval: daily 16 | open-pull-requests-limit: 10 17 | allow: 18 | - dependency-type: "direct" 19 | commit-message: 20 | prefix: chore 21 | include: scope 22 | -------------------------------------------------------------------------------- /.github/workflows/cargo-audit.yml: -------------------------------------------------------------------------------- 1 | name: Cargo Audit Scanning 2 | on: 3 | push: 4 | paths: 5 | - "**/Cargo.toml" 6 | - "**/Cargo.lock" 7 | schedule: 8 | - cron: "0 14 * * *" # 14:00 UTC 9 | jobs: 10 | cargo-audit: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - uses: actions/checkout@v4 14 | - uses: actions-rs/audit-check@v1 15 | # Don't run on dependabot PRs or forks 16 | # https://github.com/actions-rs/clippy-check/issues/2#issuecomment-807852653 17 | if: github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' 18 | with: 19 | token: ${{ secrets.GITHUB_TOKEN }} 20 | -------------------------------------------------------------------------------- /.github/workflows/release.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This is used to build cross platform linux binaries for a release in CI. 3 | # Since this is not supervised, abort if anything does not work. 4 | set -e 5 | 6 | sudo apt update 7 | # Cross-compiling needs a linker for the respective platforms. If you are on a Debian-based x86_64 Linux, 8 | # you can install them with: 9 | sudo apt -y install podman gcc-arm-linux-gnueabihf gcc-aarch64-linux-gnu 10 | # Also install cross compilation tool for cargo 11 | cargo install cross 12 | 13 | for i in x86_64-unknown-linux-gnu aarch64-unknown-linux-gnu arm-unknown-linux-gnueabihf armv7-unknown-linux-gnueabihf 14 | do 15 | cross build --verbose --release --target $i 16 | cp target/$i/release/agate agate.$i 17 | done 18 | 19 | # Strip all the binaries. 20 | strip agate.x86_64-unknown-linux-gnu 21 | aarch64-linux-gnu-strip agate.aarch64-unknown-linux-gnu 22 | arm-linux-gnueabihf-strip agate.arm-unknown-linux-gnueabihf agate.armv7-unknown-linux-gnueabihf 23 | 24 | # compress the binaries. 25 | gzip agate.* 26 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release Builds 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*' # on every version tag 7 | 8 | jobs: 9 | # first just a small job to draft the release so all others can use the upload_url 10 | create_release: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: create release 14 | id: create_release 15 | uses: ncipollo/release-action@v1 16 | 17 | build_ubuntu: 18 | runs-on: ubuntu-22.04 19 | needs: create_release 20 | steps: 21 | - uses: actions/checkout@v4 22 | - name: build 23 | run: bash .github/workflows/release.sh 24 | - name: upload release assets linux 25 | uses: AButler/upload-release-assets@v3.0 26 | with: 27 | files: 'agate.*.gz' 28 | repo-token: ${{ secrets.GITHUB_TOKEN }} 29 | release-tag: ${{ github.ref_name }} 30 | 31 | build_windows: 32 | runs-on: windows-latest 33 | needs: create_release 34 | steps: 35 | - uses: actions/checkout@v4 36 | - name: Build 37 | run: cargo build --verbose --release 38 | - name: strip names 39 | run: strip target/release/agate.exe 40 | - name: compress 41 | run: Compress-Archive -LiteralPath target/release/agate.exe -DestinationPath agate.x86_64-pc-windows-msvc.zip 42 | - name: upload release asset win 43 | uses: AButler/upload-release-assets@v3.0 44 | with: 45 | files: agate.x86_64-pc-windows-msvc.zip 46 | repo-token: ${{ secrets.GITHUB_TOKEN }} 47 | release-tag: ${{ github.ref_name }} 48 | 49 | build_macos_x86_64: 50 | runs-on: macos-latest 51 | needs: create_release 52 | steps: 53 | - uses: actions/checkout@v4 54 | - name: install toolchain 55 | run: rustup target add aarch64-apple-darwin 56 | - name: Build x86_64 57 | run: cargo build --verbose --release 58 | - name: strip names x86 59 | run: strip target/release/agate 60 | - name: compress x86 61 | run: gzip -c target/release/agate > ./agate.x86_64-apple-darwin.gz 62 | - name: Build ARM 63 | run: SDKROOT=$(xcrun -sdk macosx --show-sdk-path) MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk macosx --show-sdk-platform-version) cargo build --verbose --release --target=aarch64-apple-darwin 64 | - name: strip names ARM 65 | run: strip target/aarch64-apple-darwin/release/agate 66 | - name: compress ARM 67 | run: gzip -c target/aarch64-apple-darwin/release/agate > ./agate.aarch64-apple-darwin.gz 68 | - name: upload release assets darwin 69 | uses: AButler/upload-release-assets@v3.0 70 | with: 71 | files: 'agate.*.gz' 72 | repo-token: ${{ secrets.GITHUB_TOKEN }} 73 | release-tag: ${{ github.ref_name }} 74 | 75 | build_docker: 76 | runs-on: ubuntu-latest 77 | permissions: 78 | contents: read 79 | packages: write 80 | steps: 81 | - name: Checkout 82 | uses: actions/checkout@v4 83 | with: 84 | fetch-depth: 0 85 | - name: Log into GHCR 86 | uses: docker/login-action@v3 87 | with: 88 | registry: ghcr.io 89 | username: ${{ github.actor }} 90 | password: ${{ secrets.GITHUB_TOKEN }} 91 | - name: Extract metadata for Docker 92 | id: meta 93 | uses: docker/metadata-action@v5 94 | with: 95 | images: ghcr.io/${{ github.repository }} 96 | # Because this workflow only runs on commits tagged `v*` (i n semver format) this section ensures that 97 | # a docker build tagged `v1.2.3+podman.build` is tagged with `1`, `1.2`, `1.2.3` and `1.2.3+podman.build` 98 | # as well as being tagged with `latest`. For each of these, a subsequent build that has the same tag will 99 | # replace it. This means that pulling `ghcr.io/mbrubeck/agate:1` will always get the most recent image 100 | # released with a v1 tag, container, `ghcr.io/mbrubeck/agate:1.2` will get the latest v1.2 tag, and so on. 101 | tags: | 102 | type=semver,pattern={{version}} 103 | type=semver,pattern={{major}}.{{minor}}.{{patch}} 104 | type=semver,pattern={{major}}.{{minor}} 105 | type=semver,pattern={{major}} 106 | - name: Set up QEMU 107 | uses: docker/setup-qemu-action@v3 108 | 109 | - name: Set up Docker Buildx 110 | uses: docker/setup-buildx-action@v3 111 | 112 | - name: Build and push Docker image 113 | id: push 114 | uses: docker/build-push-action@v6 115 | with: 116 | push: true 117 | platforms: linux/amd64,linux/arm64 118 | tags: ${{ steps.meta.outputs.tags }} 119 | labels: ${{ steps.meta.outputs.labels }} 120 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: 4 | push: 5 | branches-ignore: 6 | - dependabot/* 7 | pull_request: 8 | branches: 9 | - master 10 | 11 | jobs: 12 | clippy: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - uses: actions/checkout@v4 16 | - name: Run clippy action to produce annotations 17 | # Don't run on dependabot PRs 18 | # https://github.com/actions-rs/clippy-check/issues/2#issuecomment-807852653 19 | if: github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' 20 | uses: actions-rs/clippy-check@v1 21 | with: 22 | token: ${{ secrets.GITHUB_TOKEN }} 23 | args: --all-features 24 | - name: Run clippy manually without annotations 25 | if: github.event.pull_request.head.repo.full_name != github.repository || github.actor == 'dependabot[bot]' 26 | uses: actions-rs/cargo@v1 27 | with: 28 | command: clippy 29 | args: --all-targets -- -D warnings 30 | formatting: 31 | runs-on: ubuntu-latest 32 | steps: 33 | - uses: actions/checkout@v4 34 | - name: Formatting 35 | uses: actions-rs/cargo@v1 36 | with: 37 | command: fmt 38 | args: -- --check 39 | tests: 40 | runs-on: ubuntu-latest 41 | steps: 42 | - uses: actions/checkout@v4 43 | - uses: actions-rs/cargo@v1 44 | with: 45 | command: test 46 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /.certificates 3 | -------------------------------------------------------------------------------- /.mailmap: -------------------------------------------------------------------------------- 1 | 2 | <20990607+Johann150@users.noreply.github.com> 3 | j-k 4 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ## [3.3.3] - 2023-12-27 11 | 12 | ### Fixed 13 | * fixed release automation 14 | 15 | ## [3.3.2] - 2023-12-27 16 | 17 | ### Fixed 18 | * updated dependencies 19 | 20 | ## [3.3.1] - 2023-08-05 21 | Thank you to Jan Stępień and @michaelnordmeyer for contributing to this release. 22 | 23 | ### Fixed 24 | * set permissions for generated key files so only owner can read them 25 | * improve documentation and tests 26 | 27 | ## [3.3.0] - 2023-03-18 28 | Thank you to @equalsraf, @michaelnordmeyer and @wanderer1988 for contributing to this release. 29 | 30 | ### Added 31 | * listening on unix sockets (#244) 32 | 33 | ### Fixed 34 | * updated dependencies 35 | * misstyped email address in section on how to report security vulnerabilities (#239) 36 | * wrong language code in README (#189) 37 | 38 | ## [3.2.4] - 2022-05-18 39 | Thank you to @06kellyjac, @albertlarsan68 and @kahays for contributing to this release. 40 | 41 | ### Fixed 42 | * removed port collisions in tests, for the last time (#143) 43 | * fixed Dockerfile startup command (#169) 44 | * upated dependencies 45 | 46 | ## [3.2.3] - 2022-02-04 47 | Thank you to T. Spivey for contributing to this release. 48 | 49 | ### Fixed 50 | * improper IRIs are handled instead of crashing (bug reported via email) 51 | * updated dependencies 52 | 53 | ## [3.2.2] - 2022-01-25 54 | Thank you to @Suzie97 for contributing to this release. 55 | 56 | ### Added 57 | * CI build for `aarch64-apple-darwin` target (#137) 58 | 59 | ### Fixed 60 | * updated dependencies 61 | 62 | ## [3.2.1] - 2021-12-02 63 | Thank you to @MatthiasPortzel for contributing to this release. 64 | 65 | ### Fixed 66 | * host name comparisons are now case insensitive (#115) 67 | * made automatic certificate configuration more prominent in the README 68 | * updated dependencies 69 | 70 | ## [3.2.0] - 2021-11-15 71 | Thank you to @balazsbtond and @joseph-marques for contributing to this release. 72 | 73 | ### Added 74 | * you can add header text to a directory listing. See the updated readme for details. (#98) 75 | 76 | ### Fixed 77 | * updated dependencies 78 | * error pages also send close_notify (#100) 79 | 80 | ## [3.1.3] - 2021-10-25 81 | Thank you to @FoxKyong for contributing to this release. 82 | 83 | ### Fixed 84 | * the fix for dual stack listening from 3.1.2 was executed asynchronously and would thus 85 | sometimes fail. starting the listeners on different socket addresses is now synchronous 86 | (#79) 87 | 88 | ## [3.1.2] - 2021-10-15 89 | Thank you to @etam for contributing to this release. 90 | 91 | ### Fixed 92 | * when starting up on a system that automatically listens in dual stack mode (e.g. some 93 | linux distributions seem to do this), detect a second unspecified address to not cause 94 | the "address in use" error with the default listening addresses (#79) 95 | * updated a dependency 96 | 97 | ## [3.1.1] - 2021-10-14 98 | Thank you to @jgarte and @alvaro-cuesta for contributing to this release. 99 | 100 | ### Added 101 | * running Agate using GNU Guix (#62) 102 | 103 | ### Fixed 104 | * actually bind to multiple IP addresses. Despite the documentation saying so, 105 | Agate would only bind to the first address that did not result in an error. (#63) 106 | * updated dependencies 107 | 108 | ## [3.1.0] - 2021-06-08 109 | Thank you to Matthew Ingwersen and Oliver Simmons (@GoodClover) for contributing to this release. 110 | 111 | ### Added 112 | * tests for symlink files (#60) 113 | Symlinks were already working before. 114 | 115 | ### Fixed 116 | * A path traversal security issue was closed: Percent-encoded slashes were misunderstood. 117 | 118 | ### Changed 119 | * Visiting a directory without `index.gmi` and `.directory-listing-ok` now returns a different error message to better show the cause of the error. 120 | To retain the current behaviour of showing a `51 Not found, sorry.` error, add the following line to the respective directories' `.meta` file: 121 | ``` 122 | index.gmi: 51 Not found, sorry. 123 | ``` 124 | 125 | ## [3.0.3] - 2021-05-24 126 | Thank you to @06kellyjac, @cpnfeeny, @lifelike, @skittlesvampir and @steko for contributing to this release. 127 | 128 | ### Added 129 | * Dockerfile for compiling Agate from source (#52, #53, #56, #57) 130 | 131 | ### Fixed 132 | * If the remote IP address can not be fetched, log an error instead of panicking. 133 | The previous handling could be exploited as a DoS attack vector. (#59) 134 | * Two tests were running on the same port, causing them to fail nondeterministically. (#51) 135 | * Rephrased the changelog for 3.0.0 on continuing to use older certificates. (#55) 136 | * Updated dependencies. 137 | 138 | ## [3.0.2] - 2021-04-08 139 | Thank you to @kvibber, @lifelike and @pasdechance for contributing to this release. 140 | 141 | ### Changed 142 | * The new specfication changes are obeyed regarding rejecting request URLs that contain fragments or userinfo parts. 143 | * The default signature algorithm used for generating certificates has been changed to ECDSA since there were multiple complaints about Ed25519. 144 | 145 | ## [3.0.1] - 2021-03-28 146 | Thank you to @MidAutumnMoon and @steko for contributing to this release. 147 | 148 | ### Added 149 | * Installation instructions for Arch Linux from Arch User Repositories. (#47) 150 | 151 | ### Fixed 152 | * The certificate file extensions in the README example. (#45) 153 | * The certificate directory is automatically created if it does not exist. (#44) 154 | 155 | ## [3.0.0] - 2021-03-27 156 | Thank you to @ddevault for contributing to this release. 157 | 158 | ### Added 159 | * Support for ECDSA and Ed25519 keys. 160 | * Agate now generates certificates and keys for each `--hostname` that is specified but no matching files exist. (#41) 161 | 162 | ### Changed 163 | * The ability to specify a certificate and key with `--cert` and `--key` respectively has been replaced with the `--certs` option. (#40) 164 | Certificates are now stored in a special directory. To migrate to this version, the keys should be stored in the `.certificates` directory (or any other directory you specify). 165 | This enables us to use multiple certificates for multiple domains. 166 | Note that if you want to continue to use your old certificates (recommended because of TOFU), they probably lack the `subjectAltName` directive so your old certificates should be placed at the top level of the certificates directory. Otherwise you will get an error similar to this: "The certificate file for example.com is malformed: unexpected error: The server certificate is not valid for the given name" 167 | * The certificate and key file format has been changed from PEM to DER. This simplifies loading certificate and key files without relying on unstable portions of other crates. 168 | If you want to continue using your existing certificates and keys, please convert them to DER format. You should be able to use these commands if you have openssl installed: 169 | ``` 170 | openssl x509 -in cert.pem -out cert.der -outform DER 171 | openssl rsa -in key.rsa -out key.der -outform DER 172 | ``` 173 | Since agate will automatically generate certificates from now on, the different format should not be a problem because users are not expected to handle certificates unless experienced enough to be able to handle DER formatting as well. 174 | 175 | ### Fixed 176 | * Agate now requires the use of SNI by any connecting client. 177 | * All log lines are in the same format now: 178 | `: "" "" [error:]` 179 | If the connection could not be established correctly (e.g. because of TLS errors), the status code `00` is used. 180 | * Messages from modules other than Agate itself are not logged by default. 181 | 182 | ## [2.5.3] - 2021-02-27 183 | Thank you to @littleli and @06kellyjac for contributing to this release. 184 | 185 | ### Added 186 | * Automated tests have been added so things like 2.5.2 should not happen again (#34). 187 | * Version information flag (`-V` or `--version` as conventional with e.g. cargo) 188 | 189 | ### Changed 190 | * Forbid unsafe code. (There was none before, just make it harder to add some.) 191 | * When logging remote IP addresses, the port is now never logged, which also changes the address format. 192 | 193 | ### Fixed 194 | * Updated `url` to newest version, which resolves a TODO. 195 | * The help exits successfully with `0` rather than `1` (#37). 196 | * The GitHub workflow has been fixed so Windows binaries are compressed correctly (#36). 197 | * Split out install steps to allow for more options in the future. 198 | * Add install notes for nix/NixOS to the README (#38). 199 | * Updated dependencies. 200 | 201 | ## [2.5.2] - 2021-02-12 202 | 203 | ### Fixed 204 | * Semicolons are no longer considered to be starting a comment in `.mime` files. 205 | 206 | ## [2.5.1] - 2021-02-12 207 | Functionally equivalent to version 2.5.1, only releasing a new version to update README on crates.io. 208 | 209 | ### Fixed 210 | * Fixed mistakes in the README. 211 | 212 | ## [2.5.0] - 2021-02-12 213 | Agate now has an explicit code of conduct and contributing guidelines. 214 | Thank you to @ERnsTL, @gegeweb, @SuddenPineapple, and @Ylhp for contributing to this release. 215 | 216 | ### Added 217 | * You can now supply multiple `--hostname`s to enable basic vhosts (#28, #31). 218 | * Disabling support for TLSv1.2 can now be done using the `--only-tls13` flag, but this is *NOT RECOMMENDED* (#12). 219 | * The tools now also contain a startup script for FreeBSD (#13). 220 | * Using central config mode (flag `-C`), all configuration can be done in one `.meta` file (see README.md for details). 221 | * The `.meta` configuration file now allows for globs to be used. 222 | 223 | ### Changed 224 | * The `.meta` file parser now uses the `configparser` crate. The syntax does not change. 225 | * The changelog is now also kept in this file in addition to the GitHub releases. 226 | * Certificate chain and key file are now only loaded once at startup, certificate changes need a restart to take effect. 227 | * Hidden files are now served if there is an explicit setting in a `.meta` file for them, regardless of the `--serve-secret` flag. 228 | 229 | ### Fixed 230 | * The Syntax for the IPv6 address in the README has been corrected. 231 | * Give a better error message when no keys are found in the key file instead of panicking with a range check (#33). 232 | 233 | ## [2.4.1] - 2020-02-08 234 | ### Fixed 235 | * Re-enabled multiple occurrences of `--addr`. This was accidentally disabled by a merge. 236 | 237 | ## [2.4.0]+podman.build - 2020-02-06 238 | This is the same as [2.4.0], only the build process has been changed so it should accommodate a wider range of architectures and devices. 239 | 240 | ## [2.4.0] - 2020-02-06 241 | Since there is a new maintainer (@Johann150), the range in pre-compiled binaries has changed a bit. 242 | 243 | ### Added 244 | * Added some installation tools for Debian. 245 | * Added a sidecar file for specifying languages, MIME media types or complete headers on a per file basis (#16). 246 | 247 | ### Changed 248 | * Improved logging output. Agate now also respects the `RUST_LOG` environment variable, so you can configure the log level (#22, #23). 249 | 250 | ## [2.3.0] - 2020-01-17 251 | Thanks to @Johann150. 252 | 253 | ### Changed 254 | * Combine address and port back into a single command-line argument (#21). 255 | 256 | ## [2.2.0] - 2020-01-16 257 | Thank you to @gegeweb, @Johann150 and @purexo for contributing to this release. 258 | 259 | ### Changed 260 | * Split address and port into separate command-line parameters. 261 | 262 | ### Fixed 263 | * Listen on both IPv6 and IPv4 interfaces by default (#14, #15). 264 | * Do not serve files whose path contains a segment starting with a dot (#17, #20). 265 | * Fix redirects of URLs with query strings (#19). 266 | 267 | ## [2.1.3] - 2020-01-02 268 | ### Changed 269 | * Switch to the Tokio async run time. 270 | 271 | ### Fixed 272 | * Send TLS close-notify message when closing a connection. 273 | * Require absolute URLs in requests. 274 | 275 | ## [2.1.2] - 2020-01-01 276 | ### Fixed 277 | * More complete percent-encoding of special characters in filenames. 278 | * Minor improvements to error logging. 279 | * Internal code cleanup. 280 | 281 | ## [2.1.1] - 2020-12-31 282 | ### Changed 283 | * List directory content in alphabetical order. 284 | 285 | ### Fixed 286 | * Handle percent-escaped paths in URLs. 287 | * Percent-escape white space characters in directory listings. 288 | 289 | ## [2.1.0] - 2020-12-29 290 | * Enabled GitHub Discussions. If you are using Agate, please feel free to leave a comment to let us know about it! 291 | Thank you to @Johann150 and @KilianKemps for contributing to this release. 292 | 293 | ### Added 294 | * Optional directory listings (#8, #9). 295 | 296 | ### Fixed 297 | * Updated dependencies. 298 | 299 | ## [2.0.0] - 2020-12-23 300 | Thank you to @bortzmeyer, @KillianKemps, and @Ylhp for contributing to this release. 301 | 302 | ### Added 303 | * New `--language` option to add a language tag to the MIME type for text/gemini responses (#6). 304 | 305 | ### Changed 306 | * New format for command-line options. See the documentation or run `agate --help` for details. 307 | * Logging is enabled by default. Use the `--silent` flag to disable it. 308 | * Pre-compiled binaries are built with the [`cross`](https://github.com/rust-embedded/cross) tool, for better compatibility with older Linux systems. 309 | 310 | ## [1.3.2] - 2020-12-09 311 | This release is functionally identical to Agate 1.3.1, and users of that version do not need to update. 312 | 313 | ### Fixed 314 | * Update to async-tls 0.11 because the previous version was [yanked](https://github.com/async-rs/async-tls/issues/42). 315 | 316 | ## [1.3.1] - 2020-12-08 317 | Thanks @dcreager for contributing this fix. 318 | 319 | ### Fixed 320 | * Updated dependencies to fix `cargo install` (#7). 321 | 322 | ## [1.3.0] - 2020-11-20 323 | Thank you @Johann150, @jonhiggs and @tronje for contributing to this release! 324 | 325 | ### Fixed 326 | * verify hostname and port in request URL (#4). 327 | * improved logging (#2, #3). 328 | * Don't redirect to "/" when the path is empty (#5). 329 | * Update dependencies. 330 | 331 | ## [1.2.2] - 2020-09-21 332 | Thank you to @m040601 for contributing to this release. 333 | 334 | ### Changed 335 | * Switch from `tree_magic` to `mime_guess` for simpler MIME type guessing. 336 | * Built both x86_64 and ARM binaries. These binaries are built for Linux operating systems with glibc 2.28 or later, such as Debian 10 ("buster") or newer, Ubuntu 18.10 or newer, and Raspberry Pi OS 2019-06-20 or newer (#1). 337 | 338 | ### Fixed 339 | * Update dependencies. 340 | * Minor internal code cleanup. 341 | 342 | ## [1.2.1] - 2020-06-20 343 | ### Fixed 344 | * Reduce memory usage when serving large files. 345 | * Update dependencies. 346 | 347 | ## [1.2.0] - 2020-06-10 348 | ### Changed 349 | * text/gemini filename extension from `.gemini` to `.gmi`. 350 | 351 | ### Fixed 352 | * Handling for requests that exceed 1KB. 353 | * Reduce memory allocations and speed up request parsing. 354 | * Update dependencies. 355 | 356 | ## [1.1.0] - 2020-05-22 357 | ### Added 358 | * Auto-detect MIME types. 359 | 360 | ## [1.0.1] - 2020-05-21 361 | ### Added 362 | * Send more accurate error codes for unsupported requests. 363 | * Do more validation of request URLs. 364 | 365 | ## [1.0.0] - 2020-05-21 366 | 367 | [Unreleased]: https://github.com/mbrubeck/agate/compare/v3.3.1...HEAD 368 | [3.3.1]: https://github.com/mbrubeck/agate/compare/v3.3.0...v3.3.1 369 | [3.3.0]: https://github.com/mbrubeck/agate/compare/v3.2.4...v3.3.0 370 | [3.2.4]: https://github.com/mbrubeck/agate/compare/v3.2.3...v3.2.4 371 | [3.2.3]: https://github.com/mbrubeck/agate/compare/v3.2.2...v3.2.3 372 | [3.2.2]: https://github.com/mbrubeck/agate/compare/v3.2.1...v3.2.2 373 | [3.2.1]: https://github.com/mbrubeck/agate/compare/v3.2.0...v3.2.1 374 | [3.2.0]: https://github.com/mbrubeck/agate/compare/v3.1.3...v3.2.0 375 | [3.1.3]: https://github.com/mbrubeck/agate/compare/v3.1.2...v3.1.3 376 | [3.1.2]: https://github.com/mbrubeck/agate/compare/v3.1.1...v3.1.2 377 | [3.1.1]: https://github.com/mbrubeck/agate/compare/v3.1.0...v3.1.1 378 | [3.1.0]: https://github.com/mbrubeck/agate/compare/v3.0.3...v3.1.0 379 | [3.0.3]: https://github.com/mbrubeck/agate/compare/v3.0.2...v3.0.3 380 | [3.0.2]: https://github.com/mbrubeck/agate/compare/v3.0.1...v3.0.2 381 | [3.0.1]: https://github.com/mbrubeck/agate/compare/v3.0.0...v3.0.1 382 | [3.0.0]: https://github.com/mbrubeck/agate/compare/v2.5.3...v3.0.0 383 | [2.5.3]: https://github.com/mbrubeck/agate/compare/v2.5.2...v2.5.3 384 | [2.5.2]: https://github.com/mbrubeck/agate/compare/v2.5.1...v2.5.2 385 | [2.5.1]: https://github.com/mbrubeck/agate/compare/v2.5.0...v2.5.1 386 | [2.5.0]: https://github.com/mbrubeck/agate/compare/v2.4.1...v2.5.0 387 | [2.4.1]: https://github.com/mbrubeck/agate/compare/v2.4.0...v2.4.1 388 | [2.4.0]: https://github.com/mbrubeck/agate/compare/v2.3.0...v2.4.0 389 | [2.3.0]: https://github.com/mbrubeck/agate/compare/v2.2.0...v2.3.0 390 | [2.2.0]: https://github.com/mbrubeck/agate/compare/v2.1.3...v2.2.0 391 | [2.1.3]: https://github.com/mbrubeck/agate/compare/v2.1.2...v2.1.3 392 | [2.1.2]: https://github.com/mbrubeck/agate/compare/v2.1.1...v2.1.2 393 | [2.1.1]: https://github.com/mbrubeck/agate/compare/v2.1.0...v2.1.1 394 | [2.1.0]: https://github.com/mbrubeck/agate/compare/v2.0.0...v2.1.0 395 | [2.0.0]: https://github.com/mbrubeck/agate/compare/v1.3.2...v2.0.0 396 | [1.3.2]: https://github.com/mbrubeck/agate/compare/v1.3.1...v1.3.2 397 | [1.3.1]: https://github.com/mbrubeck/agate/compare/v1.3.0...v1.3.1 398 | [1.3.0]: https://github.com/mbrubeck/agate/compare/v1.2.2...v1.3.0 399 | [1.2.2]: https://github.com/mbrubeck/agate/compare/v1.2.1...v1.2.2 400 | [1.2.1]: https://github.com/mbrubeck/agate/compare/v1.2.0...v1.2.1 401 | [1.2.0]: https://github.com/mbrubeck/agate/compare/v1.1.0...v1.2.0 402 | [1.1.0]: https://github.com/mbrubeck/agate/compare/v1.0.1...v1.1.0 403 | [1.0.1]: https://github.com/mbrubeck/agate/compare/v1.0.0...v1.0.1 404 | [1.0.0]: https://github.com/mbrubeck/agate/releases/tag/v1.0.0 405 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | 2 | # Contributor Covenant Code of Conduct 3 | 4 | ## Our Pledge 5 | 6 | We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. 7 | 8 | We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. 9 | 10 | ## Our Standards 11 | 12 | Examples of behavior that contributes to a positive environment for our community include: 13 | 14 | * Demonstrating empathy and kindness toward other people 15 | * Being respectful of differing opinions, viewpoints, and experiences 16 | * Giving and gracefully accepting constructive feedback 17 | * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience 18 | * Focusing on what is best not just for us as individuals, but for the overall community 19 | 20 | Examples of unacceptable behavior include: 21 | 22 | * The use of sexualized language or imagery, and sexual attention or advances of any kind 23 | * Trolling, insulting or derogatory comments, and personal or political attacks 24 | * Public or private harassment 25 | * Publishing others' private information, such as a physical or email address, without their explicit permission 26 | * Other conduct which could reasonably be considered inappropriate in a professional setting 27 | 28 | ## Enforcement Responsibilities 29 | 30 | Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. 31 | 32 | Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. 33 | 34 | ## Scope 35 | 36 | This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. 37 | 38 | ## Enforcement 39 | 40 | Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to a maintainer listed in the `Cargo.toml` file via email. 41 | All complaints will be reviewed and investigated promptly and fairly. 42 | 43 | All community leaders are obligated to respect the privacy and security of the reporter of any incident. 44 | 45 | ## Enforcement Guidelines 46 | 47 | Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: 48 | 49 | ### 1. Correction 50 | 51 | **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. 52 | 53 | **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. 54 | 55 | ### 2. Warning 56 | 57 | **Community Impact**: A violation through a single incident or series of actions. 58 | 59 | **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. 60 | 61 | ### 3. Temporary Ban 62 | 63 | **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. 64 | 65 | **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. 66 | 67 | ### 4. Permanent Ban 68 | 69 | **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. 70 | 71 | **Consequence**: A permanent ban from any sort of public interaction within the community. 72 | 73 | ## Attribution 74 | 75 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, available at [https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][v2.0]. 76 | 77 | Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder][Mozilla CoC]. 78 | 79 | For answers to common questions about this code of conduct, see the FAQ at [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at [https://www.contributor-covenant.org/translations][translations]. 80 | 81 | [homepage]: https://www.contributor-covenant.org 82 | [v2.0]: https://www.contributor-covenant.org/version/2/0/code_of_conduct.html 83 | [Mozilla CoC]: https://github.com/mozilla/diversity 84 | [FAQ]: https://www.contributor-covenant.org/faq 85 | [translations]: https://www.contributor-covenant.org/translations 86 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | ## Contents 4 | 5 | - [Introduction](#introduction) 6 | - [Code of Conduct](#code-of-conduct) 7 | - [Reporting Bugs and Suggesting Improvements](#reporting-bugs-and-suggesting-improvements) 8 | - [Contribution Workflow](#contribution-workflow) 9 | - [Quality Standards](#quality-standards) 10 | - [Release Process](#release-process) 11 | 12 | ## Introduction 13 | 14 | Hello, and welcome to the contributing guide for Agate! 15 | 16 | Agate is mostly maintained in the spare time of contributors, so be patient if it takes a bit longer to respond. 17 | By following this guide you'll make it easier for us to address your issues or incorporate your contributions. 18 | 19 | We look forward to working with you! 20 | 21 | ## Code of Conduct 22 | 23 | Please note that this project is released with a [Code of Conduct](./CODE_OF_CONDUCT.md). 24 | By participating in this project you agree to abide by its terms. 25 | 26 | ## Reporting security issues 27 | 28 | If you find a security issue, please disclose it to Johann150 privately, e.g. per [email](mailto:johann+agate@qwertqwefsday.eu). If you know how to fix the issue, please follow the contribution workflow as if you do not use GitHub, regardless of if you actually use it. I.e. patches should also be submitted privately. 29 | 30 | An effort will be made to respond to such issues quickly, at least responding with a "read receipt". If you do not hear back anything regarding the security issue within three days, try contacting other maintainers listed in the Cargo.toml file or on crates.io for this crate. 31 | 32 | There are no bug bounties. You can not expect any compensation apart from attribution in the changelog and/or for any patches you supply. 33 | 34 | ## Reporting Bugs and Suggesting Improvements 35 | 36 | Bugs (unwanted behaviour) and suggested improvements are tracked as [GitHub issues][github-issues]. 37 | Before reporting an issue, please check the following points: 38 | 39 | 1. The issue is caused by Agate itself and not by how it is used. 40 | Have a look at the documentation if you are not sure. 41 | If you cannot connect to Agate via the Internet, please try connecting with a client on the same machine to make sure the problem is not caused by intermediate infrastructure. 42 | 1. Your issue has not already been reported by someone else. 43 | Please look through the open issues in the [issue tracker][github-issues]. 44 | 45 | When reporting an issue, please add as much relevant information as possible. 46 | This will help developers and maintainers to resolve your issue. Some things you might consider: 47 | 48 | * Use a descriptive title. 49 | * State which version you are using (use a version tag like `v2.4.1` or the commit hash). 50 | * If you are using tools provided with agate (like a startup script), please also state that. 51 | * Describe how the problem can be reproduced. 52 | * Explain what exactly is the problem and what you expect instead. 53 | 54 | [github-issues]: https://github.com/mbrubeck/agate/issues 55 | 56 | ## Contribution Workflow 57 | 58 | Follow these steps to contribute to the project: 59 | 60 | ### If you use git but not GitHub: 61 | 62 | 1. Clone the repository where you want. 63 | 1. Make the appropriate changes, meeting all [contribution quality standards](#quality-standards). 64 | 1. Update the changelog with any added, removed, changed, or fixed functionality. Adhere to the changelog format. 65 | 1. Mail the patches or a pull request to [Johann150](mailto:johann+agate@qwertqwefsday.eu). 66 | - Patches are prefered for small changes. 67 | - Pull requests have to contain the repository URL and branch name. 68 | 1. You will be notified of any further actions (e.g. requested changes, merged) by the same address you sent from. So please make sure you can receive mail on that address. 69 | 70 | ### If you use GitHub: 71 | 72 | 1. Make a fork of the [Agate repository][agate-repo]. 73 | 1. Within your fork, create a branch for your contribution. Use a meaningful name. 74 | 1. Create your contribution, meeting all [contribution quality standards](#quality-standards). 75 | 1. Update the changelog with any added, removed, changed, or fixed functionality. Adhere to the changelog format. 76 | 1. [Create a pull request][create-a-pr] against the `master` branch of the repository. 77 | 1. Once the pull request is reviewed and CI passes, it will be merged. 78 | 79 | [agate-repo]: https://github.com/mbrubeck/agate 80 | [create-a-pr]: https://help.github.com/articles/creating-a-pull-request-from-a-fork/ 81 | 82 | ## Quality Standards 83 | 84 | Most quality and style standards are checked automatically by the CI build. 85 | Contributions should: 86 | 87 | - Separate each **logical change** into its own commit. 88 | - Ensure the code compiles correctly, if you can also run `cargo clippy`. 89 | - Format code with `cargo fmt`. 90 | - Avoid adding `unsafe` code. 91 | If it is necessary, provide an explanatory comment on any `unsafe` block explaining its rationale and why it's safe. 92 | - Add a descriptive message for each commit. 93 | Follow [these commit message guidelines][commit-messages]. 94 | - Document your pull requests. 95 | Include the reasoning behind each change, and the testing done. 96 | 97 | [commit-messages]: https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html 98 | 99 | ## Release Process 100 | (This is only relevant if you are a maintainer.) 101 | 102 | 1. Bump the version number appropriately. (Update `Cargo.lock` too!) 103 | 1. Run `cargo package` to make sure everything compiles correctly. 104 | 1. Update the changelog with the new version ranges. 105 | 1. Update agate's homepage (`content/index.gmi`) with changes to the README and CHANGELOG 106 | 1. Add a git tag for the version, e.g. with `git tag v2.4.1`. 107 | 1. Push the changelog commit and tag to the repository. 108 | Upon detecting the push of a tag beginning with "v", CI should start building the prebuilt binaries. 109 | These binaries will be uploaded to a new draft GitHub release with the same name as the version tag. (You need push access to see it). 110 | 1. Run `cargo publish` to publish to [crates.io](https://crates.io/crates/agate). 111 | 1. Fill the GitHub release text with the appropriate entries from the changelog. 112 | 1. Wait for the binary compilation to finish. 113 | 1. Publish the GitHub release. 114 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "agate" 3 | version = "3.3.16" 4 | authors = ["Matt Brubeck ", "Johann150 "] 5 | description = "Very simple server for the Gemini hypertext protocol" 6 | keywords = ["server", "gemini", "hypertext", "internet", "protocol"] 7 | categories = ["network-programming"] 8 | repository = "https://github.com/mbrubeck/agate" 9 | readme = "README.md" 10 | license = "MIT OR Apache-2.0" 11 | edition = "2024" 12 | exclude = ["/tools", "/.github", "/Cross.toml", "/content", "/CODE_OF_CONDUCT.md", "/CONTRIBUTING.md", "/CHANGELOG.md", "/tests"] 13 | 14 | [dependencies] 15 | configparser = "3.0" 16 | env_logger = { version = "0.11", default-features = false, features = ["auto-color", "humantime"] } 17 | futures-util = "0.3" 18 | getopts = "0.2.21" 19 | glob = "0.3" 20 | log = "0.4" 21 | mime_guess = "2.0" 22 | percent-encoding = "2.3" 23 | rcgen = { version = "0.13.2", default-features = false, features = ["ring"] } 24 | tokio-rustls = { version = "0.26.2", default-features = false, features = ["logging", "ring", "tls12"] } 25 | tokio = { version = "1.45", features = ["fs", "io-util", "net", "rt-multi-thread", "sync"] } 26 | url = "2.5.4" 27 | 28 | [dev-dependencies] 29 | trotter = "1.0" 30 | 31 | [profile.release] 32 | lto = true 33 | codegen-units = 1 34 | panic = "abort" 35 | -------------------------------------------------------------------------------- /Cross.toml: -------------------------------------------------------------------------------- 1 | [target.arm-unknown-linux-gnueabihf] 2 | image = "zenria/cross:arm-rpi-4.9.3-linux-gnueabihf" 3 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM docker.io/library/rust:alpine AS builder 2 | 3 | WORKDIR /agate 4 | RUN apk --no-cache add libc-dev 5 | 6 | COPY src src 7 | COPY Cargo.toml . 8 | COPY Cargo.lock . 9 | ARG TARGETARCH 10 | RUN if [ "$TARGETARCH" = "amd64" ]; then \ 11 | cargo install --target x86_64-unknown-linux-musl --path . ; \ 12 | elif [ "$TARGETARCH" = "arm64" ]; then \ 13 | cargo install --target aarch64-unknown-linux-musl --path . ; \ 14 | else \ 15 | echo "The architecture $TARGETARCH isn't unsupported." && exit 1; \ 16 | fi 17 | 18 | FROM docker.io/library/alpine:latest 19 | COPY --from=builder /usr/local/cargo/bin/agate /usr/bin/agate 20 | WORKDIR /app 21 | EXPOSE 1965 22 | VOLUME /gmi/ 23 | VOLUME /certs/ 24 | 25 | ENTRYPOINT ["agate", "--addr", "0.0.0.0:1965", "--content", "/gmi/", "--certs", "/certs/"] 26 | 27 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018 The Servo Project Developers 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Agate 2 | 3 | [![Crates.io](https://img.shields.io/crates/v/agate.svg)](https://crates.io/crates/agate) 4 | [![Test Status](https://github.com/mbrubeck/agate/workflows/Tests/badge.svg)](https://github.com/mbrubeck/agate/actions?workflow=Tests) 5 | [![Dependency Status](https://deps.rs/repo/github/mbrubeck/agate/status.svg)](https://deps.rs/repo/github/mbrubeck/agate) 6 | 7 | ## Simple Gemini server for static files 8 | 9 | Agate is a server for the [Gemini] network protocol, built with the [Rust] programming language. Agate has very few features, and can only serve static files. It uses async I/O, and should be quite efficient even when running on low-end hardware and serving many concurrent requests. 10 | 11 | ## Learn more 12 | 13 | * Home page: [gemini://qwertqwefsday.eu/agate.gmi][home] 14 | * [Cargo package][crates.io] 15 | * [Source code][source] 16 | 17 | ## Installation and Setup 18 | 19 | 1. Get a binary for agate. You can use any of the below ways: 20 | 21 | ### Pre-compiled 22 | 23 | Download and unpack the [pre-compiled binary](https://github.com/mbrubeck/agate/releases). 24 | 25 | ### NixOS/Nix 26 | 27 | Using the nix package manager run `nix-env -i agate` 28 | 29 | ### Guix System 30 | 31 | [Deploy](https://dataswamp.org/~solene/2021-06-17-guix-gemini.html) agate with GNU Guix System by adding the [agate-service-type](https://guix.gnu.org/manual/en/html_node/Web-Services.html) to your system [services](http://guix.gnu.org/manual/en/html_node/Services.html). 32 | 33 | ### Arch Linux 34 | 35 | Install the package [`agate-bin`](https://aur.archlinux.org/packages/agate-bin/)AUR for pre-compiled binary. Otherwise install the [`agate`](https://aur.archlinux.org/packages/agate/)AUR package to get agate compiled from source. 36 | 37 | ### Cargo 38 | 39 | If you have the Rust toolchain installed, run `cargo install agate` to install agate from crates.io. 40 | 41 | ### Docker 42 | 43 | Recent builds have also been released as OCI/Docker images on Github's Container Registry (ghcr.io). Most people will need to mount two volumes, one for your content, one for your certificates (this can be empty, they will be automatically generated if needed): 44 | 45 | ```sh 46 | $ docker run \ 47 | -p 1965:1965 \ 48 | -v your/path/to/gmi:/gmi \ 49 | -v your/path/to/certs:/certs \ 50 | ghcr.io/mbrubeck/agate:latest \ 51 | --hostname example.org 52 | ``` 53 | 54 | This container will run without a mounted certificates directory, but new certificates will be lost when it shuts down and re-generated every time it boots, showing your site's visitors a certificate warning each time your server restarts. 55 | 56 | Each release is tagged with `major`, `major.minor`, `major.minor.patch`, as well as the full version string and "latest". This means `docker pull ghcr.io/mbrubeck/agate:3` will always retrieve the latest `v3.*` image, `…:3.3` the latest `v3.3.*` image, and `…:latest` the most recent release of any version. 57 | 58 | ### Source 59 | 60 | Download the source code and run `cargo build --release` inside the source repository, then find the binary at `target/release/agate`. 61 | 62 | *** 63 | You can use the install script in the `tools` directory for the remaining steps if there is one for your system. 64 | If there is none, please consider contributing one to make it easier for less tech-savvy users! 65 | *** 66 | 67 | 2. Run the server. You can use the following arguments to specify the locations of the content directory, IP address and port to listen on, host name to expect in request URLs, and default language code to include in the MIME type for text/gemini files: (Replace the hostname `example.com` with the address of your Gemini server.) 68 | If you have not done it yourself, Agate will generate a private key and certificate for you on the first run, using the specified hostname(s). See the section Certificates below for more. 69 | 70 | ``` 71 | agate --content path/to/content/ \ 72 | --addr [::]:1965 \ 73 | --addr 0.0.0.0:1965 \ 74 | --hostname example.com \ 75 | --lang en-US 76 | ``` 77 | 78 | All of the command-line arguments are optional. Run `agate --help` to see the default values used when arguments are omitted. 79 | 80 | When a client requests the URL `gemini://example.com/foo/bar`, Agate will respond with the file at `path/to/content/foo/bar`. If any segment of the requested path starts with a dot, agate will respond with a status code 52, whether the file exists or not. This behaviour can be disabled with `--serve-secret` or by an entry for the specific file in the `.meta` configuration file (see Meta-Presets). If there is a directory at that path, Agate will look for a file named `index.gmi` inside that directory. 81 | 82 | ## Configuration 83 | 84 | ### Automatic Certificate generation 85 | 86 | If the `--hostname` argument is used, Agate will generate keys and self signed certificates for each hostname specified. For Gemini it is recommended by the specification to use self signed certificates because Gemini uses the TOFU (Trust on first use) principle for certificates. Because of this, the generated certificates will also have a long expiration time of `4096-01-01`. 87 | 88 | For manual configuration of keys and certificates see the [section on certificates](#certificates) below. 89 | 90 | ### TLS versions 91 | 92 | Agate by default supports TLSv1.2 and TLSv1.3. You can disable support for TLSv1.2 by using the flag `--only-tls13` (or its short version `-3`). This is *NOT RECOMMENDED* as it may break compatibility with some clients. The Gemini specification requires compatibility with TLSv1.2 "for now" because not all platforms have good support for TLSv1.3 (cf. §4.1 of the specification). 93 | 94 | ### Directory listing 95 | 96 | You can enable a basic directory listing for a directory by putting a file called `.directory-listing-ok` in that directory. This does not have an effect on sub-directories. 97 | This file must be UTF-8 encoded text; it may be empty. Any text in the file will be prepended to the directory listing. 98 | The directory listing will hide files and directories whose name starts with a dot (e.g. the `.directory-listing-ok` file itself, the `.meta` configuration file, or the `..` directory). 99 | 100 | A file called `index.gmi` will always take precedence over a directory listing. 101 | 102 | ### Meta-Presets 103 | 104 | You can put a file called `.meta` in any content directory. This file stores some metadata about the adjacent files which Agate will use when serving these files. The `.meta` file must be UTF-8 encoded. 105 | You can also enable a central configuration file with the `-C` flag (or the long version `--central-conf`). In this case Agate will always look for the `.meta` configuration file in the content root directory and will ignore `.meta` files in other directories. 106 | 107 | The `.meta` file has the following format (*1): 108 | * Empty lines are ignored. 109 | * Everything behind a `#` on the same line is a comment and will be ignored. 110 | * All other lines must have the form `:`, i.e. start with a file path, followed by a colon and then the metadata. 111 | 112 | `` is a case sensitive file path, which may or may not exist on disk. If leads to a directory, it is ignored. 113 | If central configuration file mode is not used, using a path that is not a file in the current directory is undefined behaviour (for example `../index.gmi` would be undefined behaviour). 114 | You can use Unix style patterns in existing paths. For example `content/*` will match any file within `content`, and `content/**` will additionally match any files in subdirectories of `content`. 115 | However, the `*` and `**` globs on their own will by default not match files or directories that start with a dot because of their special meaning. 116 | This behaviour can be disabled with `--serve-secret` or by explicitly matching files starting with a dot with e.g. `content/.*` or `content/**/.*` respectively. 117 | For more information on the patterns you can use, please see the [documentation of `glob::Pattern`](https://docs.rs/glob/0.3.0/glob/struct.Pattern.html). 118 | Rules can overwrite other rules, so if a file is matched by multiple rules, the last one applies. 119 | 120 | `` can take one of four possible forms: 121 | 1. empty 122 | Agate will not send a default language parameter, even if it was specified on the command line. 123 | 2. starting with a semicolon followed by MIME parameters 124 | Agate will append the specified string onto the MIME type, if the file is found. 125 | 3. starting with a gemini status code (i.e. a digit 1-6 inclusive followed by another digit) and a space 126 | Agate will send the metadata whether the file exists or not. The file will not be sent or accessed. 127 | 4. a MIME type, may include parameters 128 | Agate will use this MIME type instead of what it would guess, if the file is found. 129 | The default language parameter will not be used, even if it was specified on the command line. 130 | 131 | If a line violates the format or looks like case 3, but is incorrect, it might be ignored. You should check your logs. Please know that this configuration file is first read when a file from the respective directory is accessed. So no log messages after startup does not mean the `.meta` file is okay. 132 | 133 | Such a configuration file might look like this: 134 | ``` 135 | # This line will be ignored. 136 | **/*.de.gmi: ;lang=de 137 | nl/**/*.gmi: ;lang=nl 138 | index.gmi: ;lang=en-GB 139 | LICENSE: text/plain;charset=UTF-8 140 | gone.gmi: 52 This file is no longer here, sorry. 141 | ``` 142 | 143 | If this is the `.meta` file in the content root directory and the `-C` flag is used, this will result in the following response headers: 144 | * `/` or `/index.gmi` 145 | -> `20 text/gemini;lang=en-GB` 146 | * `/LICENSE` 147 | -> `20 text/plain;charset=UTF-8` 148 | * `/gone.gmi` 149 | -> `52 This file is no longer here, sorry.` 150 | * any non-hidden file ending in `.de.gmi` (including in non-hidden subdirectories) 151 | -> `20 text/gemini;lang=de` 152 | * any non-hidden file in the `nl` directory ending in `.gmi` (including in non-hidden subdirectories) 153 | -> `20 text/gemini;lang=nl` 154 | 155 | (*1) In theory the syntax is that of a typical INI-like file and also allows for sections with `[section]` (the default section is set to `mime` in the parser), since all other sections are disregarded, this does not make a difference. This also means that you can in theory also use `=` instead of `:`. For even more information, you can visit the [documentation of `configparser`](https://docs.rs/configparser/2.0). 156 | 157 | ### Logging Verbosity 158 | 159 | Agate uses the `env_logger` crate and allows you to set the logging verbosity by setting the `RUST_LOG` environment variable. To turn off all logging use `RUST_LOG=off`. For more information, please see the [documentation of `env_logger`]. 160 | 161 | ### Virtual Hosts 162 | 163 | Agate has basic support for virtual hosts. If you specify multiple `--hostname`s, Agate will look in a directory with the respective hostname within the content root directory. 164 | For example if one of the hostnames is `example.com`, and the content root directory is set to the default `./content`, and `gemini://example.com/file.gmi` is requested, then Agate will look for `./content/example.com/file.gmi`. This behaviour is only enabled if multiple `--hostname`s are specified. 165 | Agate also supports different certificates for different hostnames, see the section on certificates below. 166 | 167 | If you want to serve the same content for multiple domains, you can instead disable the hostname check by not specifying `--hostname`. In this case Agate will disregard a request's hostname apart from checking that there is one. 168 | 169 | When one or more `--hostname`s are specified, Agate will check that the hostnames and port in request URLs match the specified hostnames and the listening ports. If Agate is behind a proxy on another port and receives a request with an URL specifying the proxy port, this port may not match one of Agate's listening ports and the request will be rejected: it is possible to disable the port check with `--skip-port-check`. 170 | 171 | ### Certificates 172 | 173 | Agate has support for using multiple certificates with the `--certs` option. Agate will thus always require that a client uses SNI, which should not be a problem since the Gemini specification also requires SNI to be used. 174 | 175 | Certificates are by default stored in the `.certificates` directory. This is a hidden directory for the purpose that uncautious people may set the content root directory to the current directory which may also contain the certificates directory. In this case, the certificates and private keys would still be hidden. The certificates are only loaded when Agate is started and are not reloaded while running. The certificates directory may directly contain a key and certificate pair, this is the default pair used if no other matching keys are present. The certificates directory may also contain subdirectories for specific domains, for example a folder for `example.org` and `portal.example.org`. Note that the subfolders for subdomains (like `portal.example.org`) should not be inside other subfolders but directly in the certificates directory. Agate will select the certificate/key pair whose name matches most closely. For example take the following directory structure: 176 | 177 | ``` 178 | .certificates 179 | |-- cert.der (1) 180 | |-- key.der (1) 181 | |-- example.org 182 | | |-- cert.der (2) 183 | | `-- key.der (2) 184 | `-- portal.example.org 185 | |-- cert.der (3) 186 | `-- key.der (3) 187 | ``` 188 | 189 | This would be understood like this: 190 | * The certificate/key pair (1) would be used for the entire domain tree (exceptions below). 191 | * The certificate/key pair (2) would be used for the entire domain tree of `example.org`, so also including subdomains like `secret.example.org`. It overrides the pair (1) for this subtree (exceptions below). 192 | * The certificate/key pair (3) would be used for the entire domain tree of `portal.example.org`, so also inclduding subdomains like `test.portal.example.org`. It overrides the pairs (1) and (2) for this subtree. 193 | 194 | Using a directory named just `.` causes undefined behaviour as this would have the same meaning as the top level certificate/key pair (pair (1) in the example above). 195 | 196 | The files for a certificate/key pair have to be named `cert.der` and `key.der` respectively. The certificate has to be a X.509 certificate in a DER format file and has to include a subject alt name of the domain name. The private key has to be in DER format and must be either an RSA, ECDSA or Ed25519 key. 197 | 198 | ## Logging 199 | 200 | All requests via TCP sockets will be logged using this format: 201 | ``` 202 | : "" ""[ error:] 203 | ``` 204 | All requests via Unix sockets will be logged using this format: 205 | ``` 206 | unix:[] - "" ""[ error:] 207 | ``` 208 | 209 | Square brackets indicate optional parts. 210 | 211 | The "error:" part will only be logged if an error occurred. This should only be used for informative purposes as the status code should provide the information that an error occurred. If the error consisted in the connection not being established (e.g. because of TLS errors), special status codes listed below may be used. 212 | 213 | By default, Agate will not log the remote IP addresses because that might be an issue because IPs are considered private data under the EU's GDPR. To enable logging of IP addresses, you can use the `--log-ip` option. Note that in this case some error conditions might still force Agate to log a dash instead of an IP address. IP addresses can also not be logged for connections via Unix sockets. 214 | 215 | There are some lines apart from these that might occur in logs depending on the selected log level. For example the initial "Listening on..." line or information about listing a particular directory. 216 | 217 | Agate uses some status codes that are not valid Gemini status codes when logging errors: 218 | * 00 - there was an error establishing the TLS connection 219 | * 01 - there was an error in fetching the peer's IP address 220 | 221 | ## Security considerations 222 | 223 | If you want to run agate on a multi-user system, you should be aware that all certificate and key data is loaded into memory and stored there until the server stops. Since the memory is also not explicitly overwritten or zeroed after use, the sensitive data might stay in memory after the server has terminated. 224 | 225 | [Gemini]: https://geminiprotocol.net/ 226 | [Rust]: https://www.rust-lang.org/ 227 | [home]: gemini://qwertqwefsday.eu/agate.gmi 228 | [source]: https://github.com/mbrubeck/agate 229 | [crates.io]: https://crates.io/crates/agate 230 | [documentation of `env_logger`]: https://docs.rs/env_logger/0.8 231 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Only the latest version of Agate is supported at any time. 6 | 7 | ## Reporting a Vulnerability 8 | 9 | Please report issues that you deem to be a security issue by email to johann at qwertqwefsday.eu. 10 | 11 | You may use OpenPGP encryption with the public key available at either 12 | - 13 | - through web key discovery, e.g. `gpg --locate-keys ...` 14 | - or the above manually at 15 | 16 | All these public keys should be identical. If you wish for an encrypted response, include instructions on how to obtain your public key in the email. 17 | 18 | Please allow at least 24 hours for a response. 19 | If your issue is easy to fix, you might not get a response until the issue is fixed. 20 | Otherwise, the receipt of your report should be acknowledged. 21 | 22 | If you did not receive a reply within the above time frame, please email another maintainer listed in the `Cargo.toml` file, citing that you did not yet receive a reply. 23 | Only limited support may be available. 24 | 25 | ## Compensation 26 | 27 | There is no bug bounty or other rewards program. 28 | At your option, you may be mentioned by your name or pseudonym in the changelog. 29 | -------------------------------------------------------------------------------- /content/index.gmi: -------------------------------------------------------------------------------- 1 | # Agate 2 | 3 | ## Simple Gemini server for static files 4 | 5 | Agate is a server for the Gemini network protocol, built with the Rust programming language. Agate has very few features, and can only serve static files. It uses async I/O, and should be quite efficient even when running on low-end hardware and serving many concurrent requests. 6 | 7 | Since Agate by default uses port 1965, you should be able to run other servers (like e.g. Apache or nginx) on the same device. 8 | 9 | ## Learn more 10 | 11 | => gemini://qwertqwefsday.eu/agate.gmi Home page 12 | => https://crates.io/crates/agate Agate on crates.io 13 | => https://github.com/mbrubeck/agate Source code 14 | 15 | ## Installation and Setup 16 | 17 | 1. Get a binary for agate. You can use any of the below ways: 18 | 19 | ### Pre-compiled 20 | 21 | Download and unpack the [pre-compiled binary](https://github.com/mbrubeck/agate/releases). 22 | 23 | ### NixOS/Nix 24 | 25 | Using the nix package manager run `nix-env -i agate` 26 | 27 | _Note:_ agate is currently only in the unstable channel and will reach a release channel once the next release is tagged 28 | 29 | ### Guix System 30 | 31 | Deploy agate with GNU Guix System by adding the agate-service-type to your system services. 32 | => https://dataswamp.org/~solene/2021-06-17-guix-gemini.html refer to this article 33 | 34 | see also 35 | => https://guix.gnu.org/manual/en/html_node/Web-Services.html Guix Manual Web-Services 36 | => https://guix.gnu.org/manual/en/html_node/Services.html Guix Manual Services 37 | 38 | ### Arch Linux 39 | 40 | Install the package "agate-bin" from AUR for pre-compiled binary. Otherwise install the "agate" package from AUR to get agate compiled from source. 41 | 42 | ### Cargo 43 | 44 | If you have the Rust toolchain installed, run `cargo install agate` to install agate from crates.io. 45 | 46 | ### Source 47 | 48 | Download the source code and run `cargo build --release` inside the source repository, then find the binary at `target/release/agate`. 49 | 50 | *** 51 | You can use the install script in the `tools` directory for the remaining steps if there is one for your system. If there is none, please consider contributing one to make it easier for less tech-savvy users! 52 | *** 53 | 54 | 2. Run the server. You can use the following arguments to specify the locations of the content directory, IP address and port to listen on, host name to expect in request URLs, and default language code to include in the MIME type for text/gemini files: (Replace the hostname `example.com` with the address of your Gemini server.) 55 | If you have not done it yourself, Agate will generate a private key and certificate for you on the first run, using the specified hostname(s). See the section Certificates below for more. 56 | 57 | ``` 58 | agate --content path/to/content/ \ 59 | --addr [::]:1965 \ 60 | --addr 0.0.0.0:1965 \ 61 | --hostname example.com \ 62 | --lang en-US 63 | ``` 64 | 65 | All of the command-line arguments are optional. Run `agate --help` to see the default values used when arguments are omitted. 66 | 67 | When a client requests the URL `gemini://example.com/foo/bar`, Agate will respond with the file at `path/to/content/foo/bar`. If any segment of the requested path starts with a dot, agate will respond with a status code 52, whether the file exists or not. This behaviour can be disabled with `--serve-secret` or by an entry for the specific file in the `.meta` configuration file (see Meta-Presets). If there is a directory at that path, Agate will look for a file named `index.gmi` inside that directory. 68 | 69 | ## Configuration 70 | 71 | ### Automatic Certificate generation 72 | 73 | If the `--hostname` argument is used, Agate will generate keys and self signed certificates for each hostname specified. For Gemini it is recommended by the specification to use self signed certificates because Gemini uses the TOFU (Trust on first use) principle for certificates. Because of this, the generated certificates will also have a long expiration time of `4096-01-01`. 74 | 75 | For manual configuration of keys and certificates see the section on certificates below. 76 | 77 | ### TLS versions 78 | 79 | Agate by default supports TLSv1.2 and TLSv1.3. You can disable support for TLSv1.2 by using the flag `--only-tls13` (or its short version `-3`). This is *NOT RECOMMENDED* as it may break compatibility with some clients. The Gemini specification requires compatibility with TLSv1.2 "for now" because not all platforms have good support for TLSv1.3 (cf. §4.1 of the specification). 80 | 81 | ### Directory listing 82 | 83 | You can enable a basic directory listing for a directory by putting a file called `.directory-listing-ok` in that directory. This does not have an effect on sub-directories. 84 | This file must be UTF-8 encoded text; it may be empty. Any text in the file will be prepended to the directory listing. 85 | The directory listing will hide files and directories whose name starts with a dot (e.g. the `.directory-listing-ok` file itself, the `.meta` configuration file, or the `..` directory). 86 | 87 | A file called `index.gmi` will always take precedence over a directory listing. 88 | 89 | ### Meta-Presets 90 | 91 | You can put a file called `.meta` in any content directory. This file stores some metadata about the adjacent files which Agate will use when serving these files. The `.meta` file must be UTF-8 encoded. 92 | You can also enable a central configuration file with the `-C` flag (or the long version `--central-conf`). In this case Agate will always look for the `.meta` configuration file in the content root directory and will ignore `.meta` files in other directories. 93 | 94 | The `.meta` file has the following format [1]: 95 | * Empty lines are ignored. 96 | * Everything behind a `#` on the same line is a comment and will be ignored. 97 | * All other lines must have the form `:`, i.e. start with a file path, followed by a colon and then the metadata. 98 | 99 | `` is a case sensitive file path, which may or may not exist on disk. If leads to a directory, it is ignored. 100 | If central configuration file mode is not used, using a path that is not a file in the current directory is undefined behaviour (for example `../index.gmi` would be undefined behaviour). 101 | You can use Unix style patterns in existing paths. For example `content/*` will match any file within `content`, and `content/**` will additionally match any files in subdirectories of `content`. 102 | However, the `*` and `**` globs on their own will by default not match files or directories that start with a dot because of their special meaning. 103 | This behaviour can be disabled with `--serve-secret` or by explicitly matching files starting with a dot with e.g. `content/.*` or `content/**/.*` respectively. 104 | For more information on the patterns you can use, please see the documentation of `glob::Pattern`. 105 | Rules can overwrite other rules, so if a file is matched by multiple rules, the last one applies. 106 | 107 | => https://docs.rs/glob/0.3.0/glob/struct.Pattern.html Documentation of `glob::Pattern`. 108 | 109 | `` can take one of four possible forms: 110 | 1. empty: 111 | Agate will not send a default language parameter, even if it was specified on the command line. 112 | 2. starting with a semicolon followed by MIME parameters: 113 | Agate will append the specified string onto the MIME type, if the file is found. 114 | 3. starting with a gemini status code (i.e. a digit 1-6 inclusive followed by another digit) and a space: 115 | Agate will send the metadata whether the file exists or not. The file will not be sent or accessed. 116 | 4. a MIME type, may include parameters: 117 | Agate will use this MIME type instead of what it would guess, if the file is found. The default language parameter will not be used, even if it was specified on the command line. 118 | 119 | If a line violates the format or looks like case 3, but is incorrect, it might be ignored. You should check your logs. Please know that this configuration file is first read when a file from the respective directory is accessed. So no log messages after startup does not mean the `.meta` file is okay. 120 | 121 | Such a configuration file might look like this: 122 | 123 | ``` 124 | # This line will be ignored. 125 | **/*.de.gmi: ;lang=de 126 | nl/**/*.gmi: ;lang=nl 127 | index.gmi: ;lang=en-GB 128 | LICENSE: text/plain;charset=UTF-8 129 | gone.gmi: 52 This file is no longer here, sorry. 130 | ``` 131 | 132 | If this is the `.meta` file in the content root directory and the `-C` flag is used, this will result in the following response headers: 133 | 134 | ``` 135 | `/` or `/index.gmi` 136 | -> `20 text/gemini;lang=en-GB` 137 | `/LICENSE` 138 | -> `20 text/plain;charset=UTF-8` 139 | `/gone.gmi` 140 | -> `52 This file is no longer here, sorry.` 141 | any non-hidden file ending in `.de.gmi` (including in non-hidden subdirectories) 142 | -> `20 text/gemini;lang=de` 143 | any non-hidden file in the `nl` directory ending in `.gmi` (including in non-hidden subdirectories) 144 | -> `20 text/gemini;lang=nl` 145 | ``` 146 | 147 | [1] In theory the syntax is that of a typical INI-like file and also allows for sections with `[section]` (the default section is set to `mime` in the parser), since all other sections are disregarded, this does not make a difference. This also means that you can in theory also use `=` instead of `:`. For even more information, you can visit the documentation of `configparser`. 148 | => https://docs.rs/configparser/2.0 documentation of `configparser` 149 | 150 | ### Logging Verbosity 151 | 152 | Agate uses the `env_logger` crate and allows you to set the logging verbosity by setting the `RUST_LOG` environment variable. To turn off all logging use `RUST_LOG=off`. For more information, please see the documentation of `env_logger`. 153 | => https://docs.rs/env_logger/0.8 documentation of `env_logger` crate 154 | 155 | ### Virtual Hosts 156 | 157 | Agate has basic support for virtual hosts. If you specify multiple `--hostname`s, Agate will look in a directory with the respective hostname within the content root directory. 158 | For example if one of the hostnames is `example.com`, and the content root directory is set to the default `./content`, and `gemini://example.com/file.gmi` is requested, then Agate will look for `./content/example.com/file.gmi`. This behaviour is only enabled if multiple `--hostname`s are specified. 159 | Agate also supports different certificates for different hostnames, see the section on certificates below. 160 | 161 | If you want to serve the same content for multiple domains, you can instead disable the hostname check by not specifying `--hostname`. In this case Agate will disregard a request's hostname apart from checking that there is one. 162 | 163 | When one or more `--hostname`s are specified, Agate will check that the hostnames and port in request URLs match the specified hostnames and the listening ports. If Agate is behind a proxy on another port and receives a request with an URL specifying the proxy port, this port may not match one of Agate's listening ports and the request will be rejected: it is possible to disable the port check with `--skip-port-check`. 164 | 165 | ### Certificates 166 | 167 | Agate has support for using multiple certificates with the `--certs` option. Agate will thus always require that a client uses SNI, which should not be a problem since the Gemini specification also requires SNI to be used. 168 | 169 | Certificates are by default stored in the `.certificates` directory. This is a hidden directory for the purpose that uncautious people may set the content root directory to the current directory which may also contain the certificates directory. In this case, the certificates and private keys would still be hidden. The certificates are only loaded when Agate is started and are not reloaded while running. The certificates directory may directly contain a key and certificate pair, this is the default pair used if no other matching keys are present. The certificates directory may also contain subdirectories for specific domains, for example a folder for `example.org` and `portal.example.org`. Note that the subfolders for subdomains (like `portal.example.org`) should not be inside other subfolders but directly in the certificates directory. Agate will select the certificate/key pair whose name matches most closely. For example take the following directory structure: 170 | 171 | ``` 172 | .certificates 173 | |-- cert.der (1) 174 | |-- key.der (1) 175 | |-- example.org 176 | | |-- cert.der (2) 177 | | `-- key.der (2) 178 | `-- portal.example.org 179 | |-- cert.der (3) 180 | `-- key.der (3) 181 | ``` 182 | 183 | This would be understood like this: 184 | * The certificate/key pair (1) would be used for the entire domain tree (exceptions below). 185 | * The certificate/key pair (2) would be used for the entire domain tree of `example.org`, so also including subdomains like `secret.example.org`. It overrides the pair (1) for this subtree (exceptions below). 186 | * The certificate/key pair (3) would be used for the entire domain tree of `portal.example.org`, so also inclduding subdomains like `test.portal.example.org`. It overrides the pairs (1) and (2) for this subtree. 187 | 188 | Using a directory named just `.` causes undefined behaviour as this would have the same meaning as the top level certificate/key pair (pair (1) in the example above). 189 | 190 | The files for a certificate/key pair have to be named `cert.der` and `key.der` respectively. The certificate has to be a X.509 certificate in a DER format file and has to include a subject alt name of the domain name. The private key has to be in DER format and must be either an RSA, ECDSA or Ed25519 key. 191 | 192 | ## Logging 193 | 194 | All requests via TCP sockets will be logged using this format: 195 | ``` 196 | : "" ""[ error:] 197 | ``` 198 | All requests via Unix sockets will be logged using this format: 199 | ``` 200 | unix:[] - "" ""[ error:] 201 | ``` 202 | Square brackets indicate optional parts. 203 | 204 | The "error:" part will only be logged if an error occurred. This should only be used for informative purposes as the status code should provide the information that an error occurred. If the error consisted in the connection not being established (e.g. because of TLS errors), special status codes listed below may be used. 205 | 206 | By default, Agate will not log the remote IP addresses because that might be an issue because IPs are considered private data under the EU's GDPR. To enable logging of IP addresses, you can use the `--log-ip` option. Note that in this case some error conditions might still force Agate to log a dash instead of an IP address. IP addresses can also not be logged for connections via Unix sockets. 207 | 208 | There are some lines apart from these that might occur in logs depending on the selected log level. For example the initial "Listening on..." line or information about listing a particular directory. 209 | 210 | Agate uses some status codes that are not valid Gemini status codes when logging errors: 211 | * 00 - there was an error establishing the TLS connection 212 | * 01 - there was an error in fetching the peer's IP address 213 | 214 | ## Security considerations 215 | 216 | If you want to run agate on a multi-user system, you should be aware that all certificate and key data is loaded into memory and stored there until the server stops. Since the memory is also not explicitly overwritten or zeroed after use, the sensitive data might stay in memory after the server has terminated. 217 | 218 | # Changelog 219 | 220 | All notable changes to this project will be documented in this file. 221 | 222 | The format is based on Keep a Changelog and this project adheres to Semantic Versioning. 223 | => https://keepachangelog.com/en/1.0.0/ Keep a Changelog home page 224 | => https://semver.org/spec/v2.0.0.html Semantic versioning standard v2.0.0 225 | 226 | ## [3.3.1] - 2023-08-05 227 | Thank you to Jan Stępień and @michaelnordmeyer for contributing to this release. 228 | 229 | ### Fixed 230 | * set permissions for generated key files so only owner can read them 231 | * improve documentation and tests 232 | 233 | ## [3.3.0] - 2023-03-18 234 | Thank you to @equalsraf, @michaelnordmeyer and @wanderer1988 for contributing to this release. 235 | 236 | ### Added 237 | * listening on unix sockets (#244) 238 | 239 | ### Fixed 240 | * updated dependencies 241 | * misstyped email address in section on how to report security vulnerabilities (#239) 242 | * wrong language code in README (#189) 243 | 244 | ## [3.2.4] - 2022-05-18 245 | Thank you to @06kellyjac, @albertlarsan68 and @kahays for contributing to this release. 246 | 247 | ### Fixed 248 | * removed port collisions in tests, for the last time (#143) 249 | * fixed Dockerfile startup command (#169) 250 | * upated dependencies 251 | 252 | ## [3.2.3] - 2022-02-04 253 | Thank you to T. Spivey for contributing to this release. 254 | 255 | ### Fixed 256 | * improper IRIs are handled instead of crashing (bug reported via email) 257 | * updated dependencies 258 | 259 | ## [3.2.2] - 2022-01-25 260 | Thank you to @Suzie97 for contributing to this release. 261 | 262 | ### Added 263 | * CI build for `aarch64-apple-darwin` target (#137) 264 | 265 | ### Fixed 266 | * updated dependencies 267 | 268 | ## [3.2.1] - 2021-12-02 269 | Thank you to @MatthiasPortzel for contributing to this release. 270 | 271 | ### Fixed 272 | * host name comparisons are now case insensitive (#115) 273 | * made automatic certificate generation more prominent in the README 274 | * updated dependencies 275 | 276 | ## [3.2.0] - 2021-11-15 277 | Thank you to @balazsbtond and @joseph-marques for contributing to this release. 278 | 279 | ### Added 280 | * you can add header text to a directory listing. See the updated instructions above for details. (#98) 281 | 282 | ### Fixed 283 | * updated dependencies 284 | * error pages also send close_notify (#100) 285 | 286 | ## [3.1.3] - 2021-10-25 287 | Thank you to @FoxKyong for contributing to this release. 288 | 289 | ### Fixed 290 | * the fix for dual stack listening from 3.1.2 was executed asynchronously and would thus 291 | sometimes fail. starting the listeners on different socket addresses is now synchronous 292 | (#79) 293 | 294 | ## [3.1.2] - 2021-10-15 295 | Thank you to @etam for contributing to this release. 296 | 297 | ### Fixed 298 | * when starting up on a system that automatically listens in dual stack mode (e.g. some 299 | linux distributions seem to do this), detect a second unspecified address to not cause 300 | the "address in use" error with the default listening addresses 301 | * updated a dependency 302 | 303 | ## [3.1.1] - 2021-10-14 304 | Thank you to @jgarte and @alvaro-cuesta for contributing to this release. 305 | 306 | ### Added 307 | * running Agate using GNU Guix (#62) 308 | 309 | ### Fixed 310 | * actually bind to multiple IP addresses. Despite the documentation saying so, 311 | Agate would only bind to the first address that did not result in an error. (#63) 312 | * updated dependencies 313 | 314 | ## [3.1.0] - 2021-06-08 315 | Thank you to Matthew Ingwersen and Oliver Simmons (@GoodClover) for contributing to this release. 316 | 317 | ### Added 318 | * tests for symlink files (#60) 319 | Symlinks were already working before. 320 | 321 | ### Fixed 322 | * A path traversal security issue was closed: Percent encoded slashes were misunderstood. 323 | 324 | ### Changed 325 | * Visiting a directory without `index.gmi` and `.directory-listing-ok` now returns a different error message to better show the cause of the error. 326 | To retain the current behaviour of showing a `51 Not found, sorry.` error, add the following line to the respective directories' `.meta` file: 327 | ``` 328 | index.gmi: 51 Not found, sorry. 329 | ``` 330 | 331 | ## [3.0.3] - 2021-05-24 332 | Thank you to @06kellyjac, @cpnfeeny, @lifelike, @skittlesvampir and @steko for contributing to this release. 333 | 334 | ### Added 335 | * Dockerfile for compiling Agate from source (#52, #53, #56, #57) 336 | 337 | ### Fixed 338 | * If the remote IP address can not be fetched, log an error instead of panicking. 339 | The previous handling could be exploited as a DoS attack vector. (#59) 340 | * Two tests were running on the same port, causing them to fail nondeterministically. (#51) 341 | * Rephrased the changelog for 3.0.0 on continuing to use older certificates. (#55) 342 | * Updated dependencies. 343 | 344 | ## [3.0.2] - 2021-04-08 345 | Thank you to @kvibber, @lifelike and @pasdechance for contributing to this release. 346 | 347 | ### Changed 348 | * The new specfication changes are obeyed regarding rejecting request URLs that contain fragments or userinfo parts. 349 | * The default signature algorithm used for generating certificates has been changed to ECDSA since there were multiple complaints about Ed25519. 350 | 351 | ## [3.0.1] - 2021-03-28 352 | Thank you to @MidAutumnMoon and @steko for contributing to this release. 353 | 354 | ### Added 355 | * Installation instructions for Arch Linux from Arch User Repositories. (#47) 356 | 357 | ### Fixed 358 | * The certificate file extensions in the README example. (#45) 359 | * The certificate directory is automatically created if it does not exist. (#44) 360 | 361 | ## [3.0.0] - 2021-03-27 362 | Thank you to @ddevault for contributing to this release. 363 | 364 | ### Added 365 | * Support for ECDSA and Ed25519 keys. 366 | * Agate now generates certificates and keys for each `--hostname` that is specified but no matching files exist. (#41) 367 | 368 | ### Changed 369 | * The ability to specify a certificate and key with `--cert` and `--key` respectively has been replaced with the `--certs` option. (#40) 370 | Certificates are now stored in a special directory. To migrate to this version, the keys should be stored in the `.certificates` directory (or any other directory you specify). 371 | This enables us to use multiple certificates for multiple domains. 372 | Note that if you want to continue to use your old certificates (recommended because of TOFU), they probably lack the `subjectAltName` directive so your old certificates should be placed at the top level of the certificates directory. Otherwise you will get an error similar to this: "The certificate file for example.com is malformed: unexpected error: The server certificate is not valid for the given name" 373 | * The certificate and key file format has been changed from PEM to DER. This simplifies loading certificate and key files without relying on unstable portions of other crates. 374 | If you want to continue using your existing certificates and keys, please convert them to DER format. You should be able to use these commands if you have openssl installed: 375 | ``` 376 | openssl x509 -in cert.pem -out cert.der -outform DER 377 | openssl rsa -in key.rsa -out key.der -outform DER 378 | ``` 379 | Since agate will automatically generate certificates from now on, the different format should not be a problem because users are not expected to handle certificates unless experienced enough to be able to handle DER formatting as well. 380 | 381 | ### Fixed 382 | * Agate now requires the use of SNI by any connecting client. 383 | * All log lines are in the same format now: 384 | `: "" "" [error:]` 385 | If the connection could not be established correctly (e.g. because of TLS errors), the status code `00` is used. 386 | * Messages from modules other than Agate itself are not logged by default. 387 | 388 | ## [2.5.3] - 2021-02-27 389 | Thank you to @littleli and @06kellyjac for contributing to this release. 390 | 391 | ### Added 392 | * Automated tests have been added so things like 2.5.2 should not happen again (#34). 393 | * Version information flag (`-V` or `--version` as conventional with e.g. cargo) 394 | 395 | ### Changed 396 | * Forbid unsafe code. (There was none before, just make it harder to add some.) 397 | * When logging remote IP addresses, the port is now never logged, which also changes the address format. 398 | 399 | ### Fixed 400 | * Updated `url` to newest version, which resolves a TODO. 401 | * The help exits successfully with `0` rather than `1` (#37). 402 | * The GitHub workflow has been fixed so Windows binaries are compressed correctly (#36). 403 | * Split out install steps to allow for more options in the future. 404 | * Add install notes for nix/NixOS to the README (#38). 405 | * Updated dependencies. 406 | 407 | ## [2.5.2] - 2021-02-12 408 | 409 | ### Fixed 410 | * Semicolons are no longer considered to be starting a comment in `.mime` files. 411 | 412 | ## [2.5.1] - 2021-02-12 413 | Functionally equivalent to version 2.5.1, only releasing a new version to update README on crates.io. 414 | 415 | ### Fixed 416 | * Fixed mistakes in the README. 417 | 418 | ## [2.5.0] - 2021-02-12 419 | Agate now has an explicit code of conduct and contributing guidelines. 420 | Thank you to @ERnsTL, @gegeweb, @SuddenPineapple, and @Ylhp for contributing to this release. 421 | 422 | ### Added 423 | * You can now supply multiple `--hostname`s to enable basic vhosts (#28, #31). 424 | * Disabling support for TLSv1.2 can now be done using the `--only-tls13` flag, but this is *NOT RECOMMENDED* (#12). 425 | * The tools now also contain a startup script for FreeBSD (#13). 426 | * Using central config mode (flag `-C`), all configuration can be done in one `.meta` file (see README.md for details). 427 | * The `.meta` configuration file now allows for globs to be used. 428 | 429 | ### Changed 430 | * The `.meta` file parser now uses the `configparser` crate. The syntax does not change. 431 | * The changelog is now also kept in this file in addition to the GitHub releases. 432 | * Certificate chain and key file are now only loaded once at startup, certificate changes need a restart to take effect. 433 | * Hidden files are now served if there is an explicit setting in a `.meta` file for them, regardless of the `--serve-secret` flag. 434 | 435 | ### Fixed 436 | * The Syntax for the IPv6 address in the README has been corrected. 437 | * Give a better error message when no keys are found in the key file instead of panicking with a range check (#33). 438 | 439 | ## [2.4.1] - 2020-02-08 440 | ### Fixed 441 | * Re-enabled multiple occurrences of `--addr`. This was accidentally disabled by a merge. 442 | 443 | ## [2.4.0]+podman.build - 2020-02-06 444 | This is the same as [2.4.0], only the build process has been changed so it should accommodate a wider range of architectures and devices. 445 | 446 | ## [2.4.0] - 2020-02-06 447 | Since there is a new maintainer (@Johann150), the range in pre-compiled binaries has changed a bit. 448 | 449 | ### Added 450 | * Added some installation tools for Debian. 451 | * Added a sidecar file for specifying languages, MIME media types or complete headers on a per file basis (#16). 452 | 453 | ### Changed 454 | * Improved logging output. Agate now also respects the `RUST_LOG` environment variable, so you can configure the log level (#22, #23). 455 | 456 | ## [2.3.0] - 2020-01-17 457 | Thanks to @Johann150. 458 | 459 | ### Changed 460 | * Combine address and port back into a single command-line argument (#21). 461 | 462 | ## [2.2.0] - 2020-01-16 463 | Thank you to @gegeweb, @Johann150 and @purexo for contributing to this release. 464 | 465 | ### Changed 466 | * Split address and port into separate command-line parameters. 467 | 468 | ### Fixed 469 | * Listen on both IPv6 and IPv4 interfaces by default (#14, #15). 470 | * Do not serve files whose path contains a segment starting with a dot (#17, #20). 471 | * Fix redirects of URLs with query strings (#19). 472 | 473 | ## [2.1.3] - 2020-01-02 474 | ### Changed 475 | * Switch to the Tokio async run time. 476 | 477 | ### Fixed 478 | * Send TLS close-notify message when closing a connection. 479 | * Require absolute URLs in requests. 480 | 481 | ## [2.1.2] - 2020-01-01 482 | ### Fixed 483 | * More complete percent-encoding of special characters in filenames. 484 | * Minor improvements to error logging. 485 | * Internal code cleanup. 486 | 487 | ## [2.1.1] - 2020-12-31 488 | ### Changed 489 | * List directory content in alphabetical order. 490 | 491 | ### Fixed 492 | * Handle percent-escaped paths in URLs. 493 | * Percent-escape white space characters in directory listings. 494 | 495 | ## [2.1.0] - 2020-12-29 496 | * Enabled GitHub Discussions. If you are using Agate, please feel free to leave a comment to let us know about it! 497 | Thank you to @Johann150 and @KilianKemps for contributing to this release. 498 | 499 | ### Added 500 | * Optional directory listings (#8, #9). 501 | 502 | ### Fixed 503 | * Updated dependencies. 504 | 505 | ## [2.0.0] - 2020-12-23 506 | Thank you to @bortzmeyer, @KillianKemps, and @Ylhp for contributing to this release. 507 | 508 | ### Added 509 | * New `--language` option to add a language tag to the MIME type for text/gemini responses (#6). 510 | 511 | ### Changed 512 | * New format for command-line options. See the documentation or run `agate --help` for details. 513 | * Logging is enabled by default. Use the `--silent` flag to disable it. 514 | * Pre-compiled binaries are built with the [`cross`](https://github.com/rust-embedded/cross) tool, for better compatibility with older Linux systems. 515 | 516 | ## [1.3.2] - 2020-12-09 517 | This release is functionally identical to Agate 1.3.1, and users of that version do not need to update. 518 | 519 | ### Fixed 520 | * Update to async-tls 0.11 because the previous version was yanked. 521 | 522 | ## [1.3.1] - 2020-12-08 523 | Thanks @dcreager for contributing this fix. 524 | 525 | ### Fixed 526 | * Updated dependencies to fix `cargo install` (#7). 527 | 528 | ## [1.3.0] - 2020-11-20 529 | Thank you @Johann150, @jonhiggs and @tronje for contributing to this release! 530 | 531 | ### Fixed 532 | * verify hostname and port in request URL (#4). 533 | * improved logging (#2, #3). 534 | * Don't redirect to "/" when the path is empty (#5). 535 | * Update dependencies. 536 | 537 | ## [1.2.2] - 2020-09-21 538 | Thank you to @m040601 for contributing to this release. 539 | 540 | ### Changed 541 | * Switch from `tree_magic` to `mime_guess` for simpler MIME type guessing. 542 | * Built both x86_64 and ARM binaries. These binaries are built for Linux operating systems with glibc 2.28 or later, such as Debian 10 ("buster") or newer, Ubuntu 18.10 or newer, and Raspberry Pi OS 2019-06-20 or newer (#1). 543 | 544 | ### Fixed 545 | * Update dependencies. 546 | * Minor internal code cleanup. 547 | 548 | ## [1.2.1] - 2020-06-20 549 | ### Fixed 550 | * Reduce memory usage when serving large files. 551 | * Update dependencies. 552 | 553 | ## [1.2.0] - 2020-06-10 554 | ### Changed 555 | * text/gemini filename extension from `.gemini` to `.gmi`. 556 | 557 | ### Fixed 558 | * Handling for requests that exceed 1KB. 559 | * Reduce memory allocations and speed up request parsing. 560 | * Update dependencies. 561 | 562 | ## [1.1.0] - 2020-05-22 563 | ### Added 564 | * Auto-detect MIME types. 565 | 566 | ## [1.0.1] - 2020-05-21 567 | ### Added 568 | * Send more accurate error codes for unsupported requests. 569 | * Do more validation of request URLs. 570 | 571 | ## [1.0.0] - 2020-05-21 572 | -------------------------------------------------------------------------------- /src/certificates.rs: -------------------------------------------------------------------------------- 1 | use { 2 | std::{ 3 | ffi::OsStr, 4 | fmt::{Display, Formatter}, 5 | path::Path, 6 | sync::Arc, 7 | }, 8 | tokio_rustls::rustls::{ 9 | self, 10 | crypto::ring::sign::any_supported_type, 11 | pki_types::{self, CertificateDer, PrivateKeyDer}, 12 | server::{ClientHello, ResolvesServerCert}, 13 | sign::{CertifiedKey, SigningKey}, 14 | }, 15 | }; 16 | 17 | /// A struct that holds all loaded certificates and the respective domain 18 | /// names. 19 | #[derive(Debug)] 20 | pub(crate) struct CertStore { 21 | /// Stores the certificates and the domains they apply to, sorted by domain 22 | /// names, longest matches first 23 | certs: Vec<(String, Arc)>, 24 | } 25 | 26 | pub static CERT_FILE_NAME: &str = "cert.der"; 27 | pub static KEY_FILE_NAME: &str = "key.der"; 28 | 29 | #[derive(Debug)] 30 | pub enum CertLoadError { 31 | /// could not access the certificate root directory 32 | NoReadCertDir, 33 | /// no certificates or keys were found 34 | Empty, 35 | /// the key file for the specified domain is bad (e.g. does not contain a 36 | /// key or is invalid) 37 | BadKey(String, rustls::Error), 38 | /// the key file for the specified domain is missing (but a certificate 39 | /// file was present) 40 | MissingKey(String), 41 | /// the certificate file for the specified domain is missing (but a key 42 | /// file was present) 43 | MissingCert(String), 44 | /// neither a key file nor a certificate file were present for the given 45 | /// domain (but a folder was present) 46 | EmptyDomain(String), 47 | } 48 | 49 | impl Display for CertLoadError { 50 | fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { 51 | match self { 52 | Self::NoReadCertDir => write!(f, "Could not read from certificate directory."), 53 | Self::Empty => write!( 54 | f, 55 | "No keys or certificates were found in the given directory.\nSpecify the --hostname option to generate these automatically." 56 | ), 57 | Self::BadKey(domain, err) => { 58 | write!(f, "The key file for {domain} is malformed: {err:?}") 59 | } 60 | Self::MissingKey(domain) => write!(f, "The key file for {domain} is missing."), 61 | Self::MissingCert(domain) => { 62 | write!(f, "The certificate file for {domain} is missing.") 63 | } 64 | Self::EmptyDomain(domain) => write!( 65 | f, 66 | "A folder for {domain} exists, but there is no certificate or key file." 67 | ), 68 | } 69 | } 70 | } 71 | 72 | impl std::error::Error for CertLoadError {} 73 | 74 | fn load_domain(certs_dir: &Path, domain: String) -> Result { 75 | let mut path = certs_dir.to_path_buf(); 76 | path.push(&domain); 77 | // load certificate from file 78 | path.push(CERT_FILE_NAME); 79 | if !path.is_file() { 80 | return Err(if !path.with_file_name(KEY_FILE_NAME).is_file() { 81 | CertLoadError::EmptyDomain(domain) 82 | } else { 83 | CertLoadError::MissingCert(domain) 84 | }); 85 | } 86 | let cert = CertificateDer::from( 87 | std::fs::read(&path).map_err(|_| CertLoadError::MissingCert(domain.clone()))?, 88 | ); 89 | 90 | // load key from file 91 | path.set_file_name(KEY_FILE_NAME); 92 | let Ok(der) = std::fs::read(&path) else { 93 | return Err(CertLoadError::MissingKey(domain)); 94 | }; 95 | 96 | // transform key to correct format 97 | let key = der_to_private_key(&der).map_err(|e| CertLoadError::BadKey(domain.clone(), e))?; 98 | 99 | Ok(CertifiedKey::new(vec![cert], key)) 100 | } 101 | 102 | /// We don't know the key type of the private key DER file, so try each 103 | /// possible type until we find one that works. 104 | /// 105 | /// We should probably stop doing this and use a PEM file instead: 106 | /// 107 | fn der_to_private_key(der: &[u8]) -> Result, rustls::Error> { 108 | let keys = [ 109 | PrivateKeyDer::Pkcs1(pki_types::PrivatePkcs1KeyDer::from(der)), 110 | PrivateKeyDer::Sec1(pki_types::PrivateSec1KeyDer::from(der)), 111 | PrivateKeyDer::Pkcs8(pki_types::PrivatePkcs8KeyDer::from(der)), 112 | ]; 113 | 114 | let mut err = None; 115 | for key in keys { 116 | match any_supported_type(&key) { 117 | Ok(key) => return Ok(key), 118 | Err(e) => err = Some(e), 119 | } 120 | } 121 | Err(err.unwrap()) 122 | } 123 | 124 | impl CertStore { 125 | /// Load certificates from a certificate directory. 126 | /// Certificates should be stored in a folder for each hostname, for example 127 | /// the certificate and key for `example.com` should be in the files 128 | /// `certs_dir/example.com/{cert.der,key.der}` respectively. 129 | /// 130 | /// If there are `cert.der` and `key.der` directly in `certs_dir`, these 131 | /// will be loaded as default certificates. 132 | pub fn load_from(certs_dir: &Path) -> Result { 133 | // load all certificates from directories 134 | let mut certs = vec![]; 135 | 136 | // Try to load fallback certificate and key directly from the top level 137 | // certificate directory. 138 | match load_domain(certs_dir, String::new()) { 139 | Err(CertLoadError::EmptyDomain(_)) => { /* there are no fallback keys */ } 140 | Err(CertLoadError::Empty) | Err(CertLoadError::NoReadCertDir) => unreachable!(), 141 | Err(CertLoadError::BadKey(_, e)) => { 142 | return Err(CertLoadError::BadKey("fallback".to_string(), e)); 143 | } 144 | Err(CertLoadError::MissingKey(_)) => { 145 | return Err(CertLoadError::MissingKey("fallback".to_string())); 146 | } 147 | Err(CertLoadError::MissingCert(_)) => { 148 | return Err(CertLoadError::MissingCert("fallback".to_string())); 149 | } 150 | // For the fallback keys there is no domain name to verify them 151 | // against, so we can skip that step and only have to do it for the 152 | // other keys below. 153 | Ok(key) => certs.push((String::new(), Arc::new(key))), 154 | } 155 | 156 | for file in certs_dir 157 | .read_dir() 158 | .or(Err(CertLoadError::NoReadCertDir))? 159 | .filter_map(Result::ok) 160 | .filter(|x| x.path().is_dir()) 161 | { 162 | let path = file.path(); 163 | 164 | // the filename should be the domain name 165 | let filename = path 166 | .file_name() 167 | .and_then(OsStr::to_str) 168 | .unwrap() 169 | .to_string(); 170 | 171 | let key = load_domain(certs_dir, filename.clone())?; 172 | 173 | certs.push((filename, Arc::new(key))); 174 | } 175 | 176 | if certs.is_empty() { 177 | return Err(CertLoadError::Empty); 178 | } 179 | 180 | certs.sort_unstable_by(|(a, _), (b, _)| { 181 | // Try to match as many domain segments as possible. If one is a 182 | // substring of the other, the `zip` will only compare the smaller 183 | // length of either a or b and the for loop will not decide. 184 | for (a_part, b_part) in a.split('.').rev().zip(b.split('.').rev()) { 185 | if a_part != b_part { 186 | // Here we have to make sure that the empty string will 187 | // always be sorted to the end, so we reverse the usual 188 | // ordering of str. 189 | return a_part.cmp(b_part).reverse(); 190 | } 191 | } 192 | // Sort longer domains first. 193 | a.len().cmp(&b.len()).reverse() 194 | }); 195 | 196 | log::debug!( 197 | "certs loaded for {:?}", 198 | certs.iter().map(|t| &t.0).collect::>() 199 | ); 200 | 201 | Ok(Self { certs }) 202 | } 203 | 204 | /// Checks if a certificate fitting a specific domain has been loaded. 205 | /// The same rules about using a certificate at the level above apply. 206 | pub fn has_domain(&self, domain: &str) -> bool { 207 | self.certs.iter().any(|(s, _)| domain.ends_with(s)) 208 | } 209 | } 210 | 211 | impl ResolvesServerCert for CertStore { 212 | fn resolve(&self, client_hello: ClientHello<'_>) -> Option> { 213 | if let Some(name) = client_hello.server_name() { 214 | let name: &str = name; 215 | // The certificate list is sorted so the longest match will always 216 | // appear first. We have to find the first that is either this 217 | // domain or a parent domain of the current one. 218 | self.certs 219 | .iter() 220 | .find(|(s, _)| name.ends_with(s)) 221 | // only the key is interesting 222 | .map(|(_, k)| k) 223 | .cloned() 224 | } else { 225 | // This kind of resolver requires SNI. 226 | None 227 | } 228 | } 229 | } 230 | -------------------------------------------------------------------------------- /src/codes.rs: -------------------------------------------------------------------------------- 1 | /// The server was unable to parse the client's request, presumably due to a malformed request. (cf HTTP 400) 2 | pub const BAD_REQUEST: u8 = 59; 3 | /// The request was for a resource at a domain not served by the server and the server does not accept proxy requests. 4 | pub const PROXY_REQUEST_REFUSED: u8 = 53; 5 | /// The requested resource could not be found but may be available in the future. (cf HTTP 404) 6 | pub const NOT_FOUND: u8 = 51; 7 | /// The resource requested is no longer available and will not be available again. Search engines and similar tools should remove this resource from their indices. Content aggregators should stop requesting the resource and convey to their human users that the subscribed resource is gone. (cf HTTP 410) 8 | pub const GONE: u8 = 52; 9 | /// The requested resource should be consistently requested from the new URL provided in the future. Tools loke search engine indexers or content aggregators should update their configurations to avoid requesting the old URL, and end-user clients may automatically update bookmarks, etc. Note that clients that only pay attention to the initial digit of status codes will treat this as a temporary redirect. They will still end up at the right place, they just won't be able to make use of the knowledge that this redirect is permanent, so they'll pay a small performance penality by having to follow the redirect each time. 10 | pub const REDIRECT_PERMANENT: u8 = 31; 11 | /// The request was handled successfully and a response body will follow the response header. The line is a MIME media type which applies to the response body. 12 | pub const SUCCESS: u8 = 20; 13 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | #![forbid(unsafe_code)] 2 | 3 | mod certificates; 4 | mod codes; 5 | mod metadata; 6 | use codes::*; 7 | use metadata::{FileOptions, PresetMeta}; 8 | 9 | use { 10 | percent_encoding::{AsciiSet, CONTROLS, percent_decode_str, percent_encode}, 11 | rcgen::{CertificateParams, DnType, KeyPair}, 12 | std::{ 13 | borrow::Cow, 14 | error::Error, 15 | ffi::OsStr, 16 | fmt::Write, 17 | fs::{self, File}, 18 | io::Write as _, 19 | net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}, 20 | path::{self, Component, Path, PathBuf}, 21 | sync::{Arc, LazyLock}, 22 | }, 23 | tokio::{ 24 | io::{AsyncReadExt, AsyncWriteExt}, 25 | net::{TcpListener, TcpStream}, 26 | runtime::Runtime, 27 | sync::Mutex, 28 | }, 29 | tokio_rustls::{ 30 | TlsAcceptor, 31 | rustls::{server::ServerConfig, version::TLS13}, 32 | server::TlsStream, 33 | }, 34 | url::{Host, Url}, 35 | }; 36 | 37 | #[cfg(unix)] 38 | use { 39 | std::os::unix::fs::{FileTypeExt, PermissionsExt}, 40 | tokio::net::{UnixListener, UnixStream}, 41 | }; 42 | 43 | static DEFAULT_PORT: u16 = 1965; 44 | 45 | fn main() { 46 | env_logger::Builder::from_env( 47 | // by default only turn on logging for agate 48 | env_logger::Env::default().default_filter_or("agate=info"), 49 | ) 50 | .init(); 51 | Runtime::new() 52 | .expect("could not start tokio runtime") 53 | .block_on(async { 54 | let default = PresetMeta::Parameters( 55 | ARGS.language 56 | .as_ref() 57 | .map_or(String::new(), |lang| format!(";lang={lang}")), 58 | ); 59 | let mimetypes = Arc::new(Mutex::new(FileOptions::new(default))); 60 | 61 | // some systems automatically listen in dual stack if the IPv6 unspecified 62 | // address is used, so don't fail if the second unspecified address gets 63 | // an error when trying to start 64 | let mut listening_unspecified = false; 65 | 66 | let mut handles = vec![]; 67 | for addr in &ARGS.addrs { 68 | let arc = mimetypes.clone(); 69 | 70 | let listener = match TcpListener::bind(addr).await { 71 | Err(e) => { 72 | if !(addr.ip().is_unspecified() && listening_unspecified) { 73 | panic!("Failed to listen on {addr}: {e}") 74 | } else { 75 | // already listening on the other unspecified address 76 | log::warn!("Could not start listener on {}, but already listening on another unspecified address. Probably your system automatically listens in dual stack?", addr); 77 | continue; 78 | } 79 | } 80 | Ok(listener) => listener, 81 | }; 82 | listening_unspecified |= addr.ip().is_unspecified(); 83 | 84 | handles.push(tokio::spawn(async move { 85 | log::info!("Started listener on {}", addr); 86 | 87 | loop { 88 | let (stream, _) = listener.accept().await.unwrap_or_else(|e| { 89 | panic!("could not accept new connection on {addr}: {e}") 90 | }); 91 | let arc = arc.clone(); 92 | tokio::spawn(async { 93 | match RequestHandle::new(stream, arc).await { 94 | Ok(handle) => match handle.handle().await { 95 | Ok(info) => log::info!("{}", info), 96 | Err(err) => log::warn!("{}", err), 97 | }, 98 | Err(log_line) => { 99 | log::warn!("{}", log_line); 100 | } 101 | } 102 | }); 103 | } 104 | })) 105 | }; 106 | 107 | #[cfg(unix)] 108 | for socketpath in &ARGS.sockets { 109 | let arc = mimetypes.clone(); 110 | 111 | if socketpath.exists() && socketpath.metadata() 112 | .expect("Failed to get existing socket metadata") 113 | .file_type() 114 | .is_socket() { 115 | log::warn!("Socket already exists, attempting to remove {}", socketpath.display()); 116 | let _ = std::fs::remove_file(socketpath); 117 | } 118 | 119 | let listener = match UnixListener::bind(socketpath) { 120 | Err(e) => { 121 | panic!("Failed to listen on {}: {}", socketpath.display(), e) 122 | } 123 | Ok(listener) => listener, 124 | }; 125 | 126 | handles.push(tokio::spawn(async move { 127 | log::info!("Started listener on {}", socketpath.display()); 128 | 129 | loop { 130 | let (stream, _) = listener.accept().await.unwrap_or_else(|e| { 131 | panic!("could not accept new connection on {}: {}", socketpath.display(), e) 132 | }); 133 | let arc = arc.clone(); 134 | tokio::spawn(async { 135 | match RequestHandle::new_unix(stream, arc).await { 136 | Ok(handle) => match handle.handle().await { 137 | Ok(info) => log::info!("{}", info), 138 | Err(err) => log::warn!("{}", err), 139 | }, 140 | Err(log_line) => { 141 | log::warn!("{}", log_line); 142 | } 143 | } 144 | }); 145 | } 146 | })) 147 | }; 148 | 149 | futures_util::future::join_all(handles).await; 150 | }); 151 | } 152 | 153 | type Result> = std::result::Result; 154 | 155 | static ARGS: LazyLock = LazyLock::new(|| { 156 | args().unwrap_or_else(|s| { 157 | eprintln!("{s}"); 158 | std::process::exit(1); 159 | }) 160 | }); 161 | 162 | struct Args { 163 | addrs: Vec, 164 | #[cfg(unix)] 165 | sockets: Vec, 166 | content_dir: PathBuf, 167 | certs: Arc, 168 | hostnames: Vec, 169 | language: Option, 170 | serve_secret: bool, 171 | log_ips: bool, 172 | only_tls13: bool, 173 | central_config: bool, 174 | skip_port_check: bool, 175 | } 176 | 177 | fn args() -> Result { 178 | let args: Vec = std::env::args().collect(); 179 | let mut opts = getopts::Options::new(); 180 | opts.optopt( 181 | "", 182 | "content", 183 | "Root of the content directory (default ./content/)", 184 | "DIR", 185 | ); 186 | opts.optopt( 187 | "", 188 | "certs", 189 | "Root of the certificate directory (default ./.certificates/)", 190 | "DIR", 191 | ); 192 | opts.optmulti( 193 | "", 194 | "addr", 195 | &format!("Address to listen on (default 0.0.0.0:{DEFAULT_PORT} and [::]:{DEFAULT_PORT}; multiple occurences means listening on multiple interfaces)"), 196 | "IP:PORT", 197 | ); 198 | #[cfg(unix)] 199 | opts.optmulti( 200 | "", 201 | "socket", 202 | "Unix socket to listen on (multiple occurences means listening on multiple sockets)", 203 | "PATH", 204 | ); 205 | opts.optmulti( 206 | "", 207 | "hostname", 208 | "Domain name of this Gemini server, enables checking hostname and port in requests. (multiple occurences means basic vhosts)", 209 | "NAME", 210 | ); 211 | opts.optopt( 212 | "", 213 | "lang", 214 | "RFC 4646 Language code for text/gemini documents", 215 | "LANG", 216 | ); 217 | opts.optflag("h", "help", "Print this help text and exit."); 218 | opts.optflag("V", "version", "Print version information and exit."); 219 | opts.optflag( 220 | "3", 221 | "only-tls13", 222 | "Only use TLSv1.3 (default also allows TLSv1.2)", 223 | ); 224 | opts.optflag( 225 | "", 226 | "serve-secret", 227 | "Enable serving secret files (files/directories starting with a dot)", 228 | ); 229 | opts.optflag("", "log-ip", "Output the remote IP address when logging."); 230 | opts.optflag( 231 | "C", 232 | "central-conf", 233 | "Use a central .meta file in the content root directory. Decentral config files will be ignored.", 234 | ); 235 | opts.optflag( 236 | "e", 237 | "ed25519", 238 | "Generate keys using the Ed25519 signature algorithm instead of the default ECDSA.", 239 | ); 240 | opts.optflag( 241 | "", 242 | "skip-port-check", 243 | "Skip URL port check even when a hostname is specified.", 244 | ); 245 | 246 | let matches = opts.parse(&args[1..]).map_err(|f| f.to_string())?; 247 | 248 | if matches.opt_present("h") { 249 | eprintln!("{}", opts.usage(&format!("Usage: {} [options]", &args[0]))); 250 | std::process::exit(0); 251 | } 252 | 253 | if matches.opt_present("V") { 254 | eprintln!("agate {}", env!("CARGO_PKG_VERSION")); 255 | std::process::exit(0); 256 | } 257 | 258 | // try to open the certificate directory 259 | let certs_path = matches.opt_get_default("certs", ".certificates".to_string())?; 260 | let (certs, certs_path) = match check_path(certs_path.clone()) { 261 | // the directory exists, try to load certificates 262 | Ok(certs_path) => match certificates::CertStore::load_from(&certs_path) { 263 | // all is good 264 | Ok(certs) => (Some(certs), certs_path), 265 | // the certificate directory did not contain certificates, but we can generate some 266 | // because the hostname option was given 267 | Err(certificates::CertLoadError::Empty) if matches.opt_present("hostname") => { 268 | (None, certs_path) 269 | } 270 | // failed loading certificates or missing hostname to generate them 271 | Err(e) => return Err(e.into()), 272 | }, 273 | // the directory does not exist 274 | Err(_) => { 275 | // since certificate management should be automated, we are going to create the directory too 276 | log::info!( 277 | "The certificate directory {:?} does not exist, creating it.", 278 | certs_path 279 | ); 280 | std::fs::create_dir(&certs_path).expect("could not create certificate directory"); 281 | // we just created the directory, skip loading from it 282 | (None, PathBuf::from(certs_path)) 283 | } 284 | }; 285 | 286 | // If we have not loaded any certificates yet, we have to try to reload them later. 287 | // This ensures we get the right error message. 288 | let mut reload_certs = certs.is_none(); 289 | 290 | let mut hostnames = vec![]; 291 | for s in matches.opt_strs("hostname") { 292 | // normalize hostname, add punycoding if necessary 293 | let hostname = Host::parse(&s)?; 294 | 295 | // check if we have a certificate for that domain 296 | if let Host::Domain(ref domain) = hostname { 297 | if !matches!(certs, Some(ref certs) if certs.has_domain(domain)) { 298 | log::info!("No certificate or key found for {:?}, generating them.", s); 299 | 300 | let mut cert_params = CertificateParams::new(vec![domain.clone()])?; 301 | cert_params 302 | .distinguished_name 303 | .push(DnType::CommonName, domain); 304 | 305 | // ::default() already implements a 306 | // date in the far future from the time of writing: 4096-01-01 307 | 308 | let key_pair = if matches.opt_present("e") { 309 | KeyPair::generate_for(&rcgen::PKCS_ED25519) 310 | } else { 311 | KeyPair::generate() 312 | }?; 313 | 314 | // generate the certificate with the configuration 315 | let cert = cert_params.self_signed(&key_pair)?; 316 | 317 | // make sure the certificate directory exists 318 | fs::create_dir(certs_path.join(domain))?; 319 | // write certificate data to disk 320 | let mut cert_file = File::create(certs_path.join(format!( 321 | "{}/{}", 322 | domain, 323 | certificates::CERT_FILE_NAME 324 | )))?; 325 | cert_file.write_all(cert.der())?; 326 | // write key data to disk 327 | let key_file_path = 328 | certs_path.join(format!("{}/{}", domain, certificates::KEY_FILE_NAME)); 329 | let mut key_file = File::create(&key_file_path)?; 330 | #[cfg(unix)] 331 | { 332 | // set permissions so only owner can read 333 | match key_file.set_permissions(std::fs::Permissions::from_mode(0o400)) { 334 | Ok(_) => (), 335 | Err(_) => log::warn!( 336 | "could not set permissions for new key file {}", 337 | key_file_path.display() 338 | ), 339 | } 340 | } 341 | key_file.write_all(key_pair.serialized_der())?; 342 | 343 | reload_certs = true; 344 | } 345 | } 346 | 347 | hostnames.push(hostname); 348 | } 349 | 350 | // if new certificates were generated, reload the certificate store 351 | let certs = if reload_certs { 352 | certificates::CertStore::load_from(&certs_path)? 353 | } else { 354 | // there must already have been certificates loaded 355 | certs.unwrap() 356 | }; 357 | 358 | // parse listening addresses 359 | let mut addrs = vec![]; 360 | for i in matches.opt_strs("addr") { 361 | addrs.push(i.parse()?); 362 | } 363 | 364 | #[cfg_attr(not(unix), allow(unused_mut))] 365 | let mut empty = addrs.is_empty(); 366 | 367 | #[cfg(unix)] 368 | let mut sockets = vec![]; 369 | #[cfg(unix)] 370 | { 371 | for i in matches.opt_strs("socket") { 372 | sockets.push(i.parse()?); 373 | } 374 | 375 | empty &= sockets.is_empty(); 376 | } 377 | 378 | if empty { 379 | addrs = vec![ 380 | SocketAddr::new(IpAddr::V6(Ipv6Addr::UNSPECIFIED), DEFAULT_PORT), 381 | SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), DEFAULT_PORT), 382 | ]; 383 | } 384 | 385 | Ok(Args { 386 | addrs, 387 | #[cfg(unix)] 388 | sockets, 389 | content_dir: check_path(matches.opt_get_default("content", "content".into())?)?, 390 | certs: Arc::new(certs), 391 | hostnames, 392 | language: matches.opt_str("lang"), 393 | serve_secret: matches.opt_present("serve-secret"), 394 | log_ips: matches.opt_present("log-ip"), 395 | only_tls13: matches.opt_present("only-tls13"), 396 | central_config: matches.opt_present("central-conf"), 397 | skip_port_check: matches.opt_present("skip-port-check"), 398 | }) 399 | } 400 | 401 | fn check_path(s: String) -> Result { 402 | let p = PathBuf::from(s); 403 | if p.as_path().exists() { 404 | Ok(p) 405 | } else { 406 | Err(format!("No such file: {p:?}")) 407 | } 408 | } 409 | 410 | /// TLS configuration. 411 | static TLS: LazyLock = LazyLock::new(acceptor); 412 | 413 | fn acceptor() -> TlsAcceptor { 414 | let config = if ARGS.only_tls13 { 415 | ServerConfig::builder_with_protocol_versions(&[&TLS13]) 416 | } else { 417 | ServerConfig::builder() 418 | } 419 | .with_no_client_auth() 420 | .with_cert_resolver(ARGS.certs.clone()); 421 | TlsAcceptor::from(Arc::new(config)) 422 | } 423 | 424 | struct RequestHandle { 425 | stream: TlsStream, 426 | local_port_check: Option, 427 | log_line: String, 428 | metadata: Arc>, 429 | } 430 | 431 | impl RequestHandle { 432 | /// Creates a new request handle for the given stream. If establishing the TLS 433 | /// session fails, returns a corresponding log line. 434 | async fn new(stream: TcpStream, metadata: Arc>) -> Result { 435 | let local_addr = stream.local_addr().unwrap().to_string(); 436 | 437 | // try to get the remote IP address if desired 438 | let peer_addr = if ARGS.log_ips { 439 | stream 440 | .peer_addr() 441 | .map_err(|_| { 442 | format!( 443 | // use nonexistent status code 01 if peer IP is unknown 444 | "{local_addr} - \"\" 01 \"IP error\" error:could not get peer address", 445 | ) 446 | })? 447 | .ip() 448 | .to_string() 449 | } else { 450 | // Do not log IP address, but something else so columns still line up. 451 | "-".into() 452 | }; 453 | 454 | let log_line = format!("{local_addr} {peer_addr}",); 455 | 456 | let local_port_check = if ARGS.skip_port_check { 457 | None 458 | } else { 459 | Some(stream.local_addr().unwrap().port()) 460 | }; 461 | 462 | match TLS.accept(stream).await { 463 | Ok(stream) => Ok(Self { 464 | stream, 465 | local_port_check, 466 | log_line, 467 | metadata, 468 | }), 469 | // use nonexistent status code 00 if connection was not established 470 | Err(e) => Err(format!("{log_line} \"\" 00 \"TLS error\" error:{e}")), 471 | } 472 | } 473 | } 474 | 475 | #[cfg(unix)] 476 | impl RequestHandle { 477 | async fn new_unix( 478 | stream: UnixStream, 479 | metadata: Arc>, 480 | ) -> Result { 481 | let log_line = format!( 482 | "unix:{} -", 483 | stream 484 | .local_addr() 485 | .ok() 486 | .and_then(|addr| Some(addr.as_pathname()?.to_string_lossy().into_owned())) 487 | .unwrap_or_default() 488 | ); 489 | 490 | match TLS.accept(stream).await { 491 | Ok(stream) => Ok(Self { 492 | stream, 493 | // TODO add port check for unix sockets, requires extra arg for port 494 | local_port_check: None, 495 | log_line, 496 | metadata, 497 | }), 498 | // use nonexistent status code 00 if connection was not established 499 | Err(e) => Err(format!("{} \"\" 00 \"TLS error\" error:{}", log_line, e)), 500 | } 501 | } 502 | } 503 | 504 | impl RequestHandle 505 | where 506 | T: AsyncWriteExt + AsyncReadExt + Unpin, 507 | { 508 | /// Do the necessary actions to handle this request. Returns a corresponding 509 | /// log line as Err or Ok, depending on if the request finished with or 510 | /// without errors. 511 | async fn handle(mut self) -> Result { 512 | // not already in error condition 513 | let result = match self.parse_request().await { 514 | Ok(url) => self.send_response(url).await, 515 | Err((status, msg)) => self.send_header(status, msg).await, 516 | }; 517 | 518 | let close_result = self.stream.shutdown().await; 519 | 520 | match (result, close_result) { 521 | (Err(e), _) => Err(format!("{} error:{}", self.log_line, e)), 522 | (Ok(_), Err(e)) => Err(format!("{} error:{}", self.log_line, e)), 523 | (Ok(_), Ok(_)) => Ok(self.log_line), 524 | } 525 | } 526 | 527 | /// Return the URL requested by the client. 528 | async fn parse_request(&mut self) -> std::result::Result { 529 | // Because requests are limited to 1024 bytes (plus 2 bytes for CRLF), we 530 | // can use a fixed-sized buffer on the stack, avoiding allocations and 531 | // copying, and stopping bad clients from making us use too much memory. 532 | let mut request = [0; 1026]; 533 | let mut buf = &mut request[..]; 534 | let mut len = 0; 535 | 536 | // Read until CRLF, end-of-stream, or there's no buffer space left. 537 | // 538 | // Since neither CR nor LF can be part of a URI according to 539 | // ISOC-RFC 3986, we could use BufRead::read_line here, but that does 540 | // not allow us to cap the number of read bytes at 1024+2. 541 | let result = loop { 542 | let Ok(bytes_read) = self.stream.read(buf).await else { 543 | break Err((BAD_REQUEST, "Request ended unexpectedly")); 544 | }; 545 | len += bytes_read; 546 | if request[..len].ends_with(b"\r\n") { 547 | break Ok(()); 548 | } else if bytes_read == 0 { 549 | break Err((BAD_REQUEST, "Request ended unexpectedly")); 550 | } 551 | buf = &mut request[len..]; 552 | } 553 | .and_then(|()| { 554 | std::str::from_utf8(&request[..len - 2]).or(Err((BAD_REQUEST, "Non-UTF-8 request"))) 555 | }); 556 | 557 | let request = result.inspect_err(|_| { 558 | // write empty request to log line for uniformity 559 | write!(self.log_line, " \"\"").unwrap(); 560 | })?; 561 | 562 | // log literal request (might be different from or not an actual URL) 563 | write!(self.log_line, " \"{request}\"").unwrap(); 564 | 565 | let mut url = Url::parse(request).or(Err((BAD_REQUEST, "Invalid URL")))?; 566 | 567 | // Validate the URL: 568 | // correct scheme 569 | if url.scheme() != "gemini" { 570 | return Err((PROXY_REQUEST_REFUSED, "Unsupported URL scheme")); 571 | } 572 | 573 | // no userinfo and no fragment 574 | if url.password().is_some() || !url.username().is_empty() || url.fragment().is_some() { 575 | return Err((BAD_REQUEST, "URL contains fragment or userinfo")); 576 | } 577 | 578 | // correct host 579 | let Some(domain) = url.domain() else { 580 | return Err((BAD_REQUEST, "URL does not contain a domain")); 581 | }; 582 | // because the gemini scheme is not special enough for WHATWG, normalize 583 | // it ourselves 584 | let host = Host::parse( 585 | &percent_decode_str(domain) 586 | .decode_utf8() 587 | .or(Err((BAD_REQUEST, "Invalid URL")))?, 588 | ) 589 | .or(Err((BAD_REQUEST, "Invalid URL")))?; 590 | // TODO: simplify when resolved 591 | url.set_host(Some(&host.to_string())) 592 | .expect("invalid domain?"); 593 | // do not use "contains" here since it requires the same type and does 594 | // not allow to check for Host<&str> if the vec contains Hostname 595 | if !ARGS.hostnames.is_empty() && !ARGS.hostnames.iter().any(|h| h == &host) { 596 | return Err((PROXY_REQUEST_REFUSED, "Proxy request refused")); 597 | } 598 | 599 | // correct port 600 | if let Some(expected_port) = self.local_port_check { 601 | if let Some(port) = url.port() { 602 | // Validate that the port in the URL is the same as for the stream this request 603 | // came in on. 604 | if port != expected_port { 605 | return Err((PROXY_REQUEST_REFUSED, "Proxy request refused")); 606 | } 607 | } 608 | } 609 | Ok(url) 610 | } 611 | 612 | /// Send the client the file located at the requested URL. 613 | async fn send_response(&mut self, url: Url) -> Result { 614 | let mut path = std::path::PathBuf::from(&ARGS.content_dir); 615 | 616 | if ARGS.hostnames.len() > 1 { 617 | // basic vhosts, existence of host_str was checked by parse_request already 618 | path.push(url.host_str().expect("no hostname")); 619 | } 620 | 621 | if let Some(mut segments) = url.path_segments() { 622 | // append percent-decoded path segments 623 | for segment in segments.clone() { 624 | // To prevent directory traversal attacks, we need to 625 | // check that each filesystem path component in the URL 626 | // path segment is a normal component (not the root 627 | // directory, the parent directory, a drive label, or 628 | // another special component). Furthermore, since path 629 | // separators (e.g. the escaped forward slash %2F) in a 630 | // single URL path segment are non-structural, the URL 631 | // path segment should not contain multiple filesystem 632 | // path components. 633 | let decoded = percent_decode_str(segment).decode_utf8()?; 634 | let mut components = Path::new(decoded.as_ref()).components(); 635 | // the first component must be a normal component; if 636 | // so, push it onto the PathBuf 637 | match components.next() { 638 | None => (), 639 | Some(Component::Normal(c)) => path.push(c), 640 | Some(_) => return self.send_header(NOT_FOUND, "Not found, sorry.").await, 641 | } 642 | // there must not be more than one component 643 | if components.next().is_some() { 644 | return self.send_header(NOT_FOUND, "Not found, sorry.").await; 645 | } 646 | // even if it's one component, there may be trailing path 647 | // separators at the end 648 | if decoded.ends_with(path::is_separator) { 649 | return self.send_header(NOT_FOUND, "Not found, sorry.").await; 650 | } 651 | } 652 | // check if hiding files is disabled 653 | if !ARGS.serve_secret 654 | // there is a configuration for this file, assume it should be served 655 | && !self.metadata.lock().await.exists(&path) 656 | // check if file or directory is hidden 657 | && segments.any(|segment| segment.starts_with('.')) 658 | { 659 | return self 660 | .send_header(GONE, "If I told you, it would not be a secret.") 661 | .await; 662 | } 663 | } 664 | 665 | if let Ok(metadata) = tokio::fs::metadata(&path).await { 666 | if metadata.is_dir() { 667 | if url.path().ends_with('/') || url.path().is_empty() { 668 | // if the path ends with a slash or the path is empty, the links will work the same 669 | // without a redirect 670 | // use `push` instead of `join` because the changed path is used later 671 | path.push("index.gmi"); 672 | if !path.exists() { 673 | path.pop(); 674 | // try listing directory 675 | return self.list_directory(&path).await; 676 | } 677 | } else { 678 | // if client is not redirected, links may not work as expected without trailing slash 679 | let mut url = url; 680 | url.set_path(&format!("{}/", url.path())); 681 | return self.send_header(REDIRECT_PERMANENT, url.as_str()).await; 682 | } 683 | } 684 | } 685 | 686 | let data = self.metadata.lock().await.get(&path); 687 | 688 | if let PresetMeta::FullHeader(status, meta) = data { 689 | self.send_header(status, &meta).await?; 690 | // do not try to access the file 691 | return Ok(()); 692 | } 693 | 694 | // Make sure the file opens successfully before sending a success header. 695 | let mut file = match tokio::fs::File::open(&path).await { 696 | Ok(file) => file, 697 | Err(e) => { 698 | self.send_header(NOT_FOUND, "Not found, sorry.").await?; 699 | return Err(e.into()); 700 | } 701 | }; 702 | 703 | // Send header. 704 | let mime = match data { 705 | // this was already handled before opening the file 706 | PresetMeta::FullHeader(..) => unreachable!(), 707 | // treat this as the full MIME type 708 | PresetMeta::FullMime(mime) => mime.clone(), 709 | // guess the MIME type and add the parameters 710 | PresetMeta::Parameters(params) => { 711 | if path.extension() == Some(OsStr::new("gmi")) { 712 | format!("text/gemini{params}") 713 | } else { 714 | let mime = mime_guess::from_path(&path).first_or_octet_stream(); 715 | format!("{}{}", mime.essence_str(), params) 716 | } 717 | } 718 | }; 719 | self.send_header(SUCCESS, &mime).await?; 720 | 721 | // Send body. 722 | tokio::io::copy(&mut file, &mut self.stream).await?; 723 | Ok(()) 724 | } 725 | 726 | async fn list_directory(&mut self, path: &Path) -> Result { 727 | // https://url.spec.whatwg.org/#path-percent-encode-set 728 | const ENCODE_SET: AsciiSet = CONTROLS 729 | .add(b' ') 730 | .add(b'"') 731 | .add(b'#') 732 | .add(b'<') 733 | .add(b'>') 734 | .add(b'?') 735 | .add(b'`') 736 | .add(b'{') 737 | .add(b'}'); 738 | 739 | // check if directory listing is enabled by getting preamble 740 | let Ok(preamble) = std::fs::read_to_string(path.join(".directory-listing-ok")) else { 741 | self.send_header(NOT_FOUND, "Directory index disabled.") 742 | .await?; 743 | return Ok(()); 744 | }; 745 | 746 | log::info!("Listing directory {:?}", path); 747 | 748 | self.send_header(SUCCESS, "text/gemini").await?; 749 | self.stream.write_all(preamble.as_bytes()).await?; 750 | 751 | let mut entries = tokio::fs::read_dir(path).await?; 752 | let mut lines = vec![]; 753 | while let Some(entry) = entries.next_entry().await? { 754 | let mut name = entry 755 | .file_name() 756 | .into_string() 757 | .or(Err("Non-Unicode filename"))?; 758 | if name.starts_with('.') { 759 | continue; 760 | } 761 | if entry.file_type().await?.is_dir() { 762 | name += "/"; 763 | } 764 | let line = match percent_encode(name.as_bytes(), &ENCODE_SET).into() { 765 | Cow::Owned(url) => format!("=> {url} {name}\n"), 766 | Cow::Borrowed(url) => format!("=> {url}\n"), // url and name are identical 767 | }; 768 | lines.push(line); 769 | } 770 | lines.sort(); 771 | for line in lines { 772 | self.stream.write_all(line.as_bytes()).await?; 773 | } 774 | Ok(()) 775 | } 776 | 777 | async fn send_header(&mut self, status: u8, meta: &str) -> Result { 778 | // add response status and response meta 779 | write!(self.log_line, " {status} \"{meta}\"")?; 780 | 781 | self.stream 782 | .write_all(format!("{status} {meta}\r\n").as_bytes()) 783 | .await?; 784 | Ok(()) 785 | } 786 | } 787 | -------------------------------------------------------------------------------- /src/metadata.rs: -------------------------------------------------------------------------------- 1 | use configparser::ini::Ini; 2 | use glob::{MatchOptions, glob_with}; 3 | use std::collections::BTreeMap; 4 | use std::path::{Path, PathBuf}; 5 | use std::time::SystemTime; 6 | 7 | static SIDECAR_FILENAME: &str = ".meta"; 8 | 9 | /// A struct to store a string of metadata for each file retrieved from 10 | /// sidecar files with the name given by `SIDECAR_FILENAME`. 11 | /// 12 | /// These sidecar file's lines should have the format 13 | /// ```text 14 | /// : 15 | /// ``` 16 | /// where `` is only a filename (not a path) of a file that resides 17 | /// in the same directory and `` is the metadata to be stored. 18 | /// Lines that start with optional whitespace and `#` are ignored, as are lines 19 | /// that do not fit the basic format. 20 | /// Both parts are stripped of any leading and/or trailing whitespace. 21 | pub(crate) struct FileOptions { 22 | /// Stores the paths of the side files and when they were last read. 23 | /// By comparing this to the last write time, we can know if the file 24 | /// has changed. 25 | databases_read: BTreeMap, 26 | /// Stores the metadata for each file 27 | file_meta: BTreeMap, 28 | /// The default value to return 29 | default: PresetMeta, 30 | } 31 | 32 | /// A struct to store the different alternatives that a line in the sidecar 33 | /// file can have. 34 | #[derive(Clone, Debug)] 35 | pub(crate) enum PresetMeta { 36 | /// A line that starts with a semicolon in the sidecar file, or an 37 | /// empty line (to overwrite the default language command line flag). 38 | /// ```text 39 | /// index.gmi: ;lang=en-GB 40 | /// ``` 41 | /// The content is interpreted as MIME parameters and are appended to what 42 | /// agate guesses as the MIME type if the respective file can be found. 43 | Parameters(String), 44 | /// A line that is neither a `Parameters` line nor a `FullHeader` line. 45 | /// ```text 46 | /// strange.file: text/plain; lang=ee 47 | /// ``` 48 | /// Agate will send the complete line as the MIME type of the request if 49 | /// the respective file can be found (i.e. a `20` status code). 50 | FullMime(String), 51 | /// A line that starts with a digit between 1 and 6 inclusive followed by 52 | /// another digit and a space (U+0020). In the categories defined by the 53 | /// Gemini specification you can pick a defined or non-defined status code. 54 | /// ```text 55 | /// gone.gmi: 52 This file is no longer available. 56 | /// ``` 57 | /// Agate will send this header line, CR, LF, and nothing else. Agate will 58 | /// not try to access the requested file. 59 | FullHeader(u8, String), 60 | } 61 | 62 | impl FileOptions { 63 | pub(crate) fn new(default: PresetMeta) -> Self { 64 | Self { 65 | databases_read: BTreeMap::new(), 66 | file_meta: BTreeMap::new(), 67 | default, 68 | } 69 | } 70 | 71 | /// Checks wether the database for the directory of the specified file is 72 | /// still up to date and re-reads it if outdated or not yet read. 73 | fn update(&mut self, file: &Path) { 74 | let mut db = if super::ARGS.central_config { 75 | super::ARGS.content_dir.clone() 76 | } else { 77 | file.parent().expect("no parent directory").to_path_buf() 78 | }; 79 | db.push(SIDECAR_FILENAME); 80 | 81 | let should_read = if let Ok(metadata) = db.metadata() { 82 | if !metadata.is_file() { 83 | // it exists, but it is a directory 84 | false 85 | } else if let (Ok(modified), Some(last_read)) = 86 | (metadata.modified(), self.databases_read.get(&db)) 87 | { 88 | // check that it was last modified before the read 89 | // if the times are the same, we might have read the old file 90 | &modified >= last_read 91 | } else { 92 | // either the filesystem does not support last modified 93 | // metadata, so we have to read it again every time; or the 94 | // file exists but was not read before, so we have to read it 95 | true 96 | } 97 | } else { 98 | // the file probably does not exist 99 | false 100 | }; 101 | 102 | if should_read { 103 | self.read_database(&db); 104 | } 105 | } 106 | 107 | /// (Re)reads a specified sidecar file. 108 | /// This function will allways try to read the file, even if it is current. 109 | fn read_database(&mut self, db: &Path) { 110 | log::debug!("reading database {:?}", db); 111 | 112 | let mut ini = Ini::new_cs(); 113 | ini.set_default_section("mime"); 114 | ini.set_comment_symbols(&['#']); 115 | let map = ini 116 | .load(db.to_str().expect("config path not UTF-8")) 117 | .and_then(|mut sections| { 118 | sections 119 | .remove("mime") 120 | .ok_or_else(|| "no \"mime\" or default section".to_string()) 121 | }); 122 | self.databases_read 123 | .insert(db.to_path_buf(), SystemTime::now()); 124 | let files = match map { 125 | Ok(section) => section, 126 | Err(err) => { 127 | log::error!("invalid config file {:?}: {}", db, err); 128 | return; 129 | } 130 | }; 131 | 132 | for (rel_path, header) in files { 133 | // treat unassigned keys as if they had an empty value 134 | let header = header.unwrap_or_default(); 135 | 136 | // generate workspace-relative path 137 | let mut path = db.to_path_buf(); 138 | path.pop(); 139 | path.push(rel_path); 140 | 141 | // parse the preset 142 | let preset = if header.is_empty() || header.starts_with(';') { 143 | PresetMeta::Parameters(header.to_string()) 144 | } else if matches!(header.chars().next(), Some('1'..='6')) { 145 | if header.len() < 3 146 | || !header.chars().nth(1).unwrap().is_ascii_digit() 147 | || !header.chars().nth(2).unwrap().is_whitespace() 148 | { 149 | log::error!( 150 | "Line for {:?} starts like a full header line, but it is incorrect; ignoring it.", 151 | path 152 | ); 153 | return; 154 | } 155 | let separator = header.chars().nth(2).unwrap(); 156 | if separator != ' ' { 157 | // the Gemini specification says that the third 158 | // character has to be a space, so correct any 159 | // other whitespace to it (e.g. tabs) 160 | log::warn!( 161 | "Full Header line for {:?} has an invalid character, treating {:?} as a space.", 162 | path, 163 | separator 164 | ); 165 | } 166 | let status = header 167 | .chars() 168 | .take(2) 169 | .collect::() 170 | .parse::() 171 | // unwrap since we alread checked it's a number 172 | .unwrap(); 173 | // not taking a slice here because the separator 174 | // might be a whitespace wider than a byte 175 | let meta = header.chars().skip(3).collect::(); 176 | PresetMeta::FullHeader(status, meta) 177 | } else { 178 | // must be a MIME type, but without status code 179 | PresetMeta::FullMime(header.to_string()) 180 | }; 181 | 182 | let glob_options = MatchOptions { 183 | case_sensitive: true, 184 | // so there is a difference between "*" and "**". 185 | require_literal_separator: true, 186 | // security measure because entries for .hidden files 187 | // would result in them being exposed. 188 | require_literal_leading_dot: !crate::ARGS.serve_secret, 189 | }; 190 | 191 | // process filename as glob 192 | let paths = if let Some(path) = path.to_str() { 193 | match glob_with(path, glob_options) { 194 | Ok(paths) => paths.collect::>(), 195 | Err(err) => { 196 | log::error!("incorrect glob pattern in {:?}: {}", path, err); 197 | continue; 198 | } 199 | } 200 | } else { 201 | log::error!("path is not UTF-8: {:?}", path); 202 | continue; 203 | }; 204 | 205 | if paths.is_empty() { 206 | // probably an entry for a nonexistent file, glob only works for existing files 207 | self.file_meta.insert(path, preset); 208 | } else { 209 | for glob_result in paths { 210 | match glob_result { 211 | Ok(path) if path.is_dir() => { /* ignore */ } 212 | Ok(path) => { 213 | self.file_meta.insert(path, preset.clone()); 214 | } 215 | Err(err) => { 216 | log::warn!("could not process glob path: {}", err); 217 | continue; 218 | } 219 | }; 220 | } 221 | } 222 | } 223 | } 224 | 225 | /// Get the metadata for the specified file. This might need to (re)load a 226 | /// single sidecar file. 227 | /// The file path should consistenly be either absolute or relative to the 228 | /// working/content directory. If inconsistent file paths are used, this can 229 | /// lead to loading and storing sidecar files multiple times. 230 | pub fn get(&mut self, file: &Path) -> PresetMeta { 231 | self.update(file); 232 | 233 | self.file_meta.get(file).unwrap_or(&self.default).clone() 234 | } 235 | 236 | /// Returns true if a configuration exists in a configuration file. 237 | /// Returns false if no or only the default value exists. 238 | pub fn exists(&mut self, file: &Path) -> bool { 239 | self.update(file); 240 | 241 | self.file_meta.contains_key(file) 242 | } 243 | } 244 | -------------------------------------------------------------------------------- /tests/README.md: -------------------------------------------------------------------------------- 1 | # Tests for the Agate gemini server 2 | 3 | This folder contains some tests and data used for these tests. 4 | 5 | Also note that you should **NEVER USE THE CERTIFICATE AND KEY DATA PROVIDED HERE** since it is public. 6 | -------------------------------------------------------------------------------- /tests/data/.certificates/cert.der: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/.certificates/cert.der -------------------------------------------------------------------------------- /tests/data/.certificates/key.der: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/.certificates/key.der -------------------------------------------------------------------------------- /tests/data/cert_missing/key.der: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/cert_missing/key.der -------------------------------------------------------------------------------- /tests/data/content/.meta: -------------------------------------------------------------------------------- 1 | # test setting a mime type 2 | test: text/html 3 | # test double star globs 4 | **/*.nl.gmi: ;lang=nl 5 | # test setting multiple parameters 6 | test.gmi: ;lang=en ;charset=us-ascii 7 | # test setting data for nonexistent files 8 | gone.txt: 52 This file is no longer available. 9 | # test setting data for files in other directories 10 | example.com/index.gmi: ;lang=en-US 11 | .servable-secret: text/plain 12 | .well-known/servable-secret: text/plain 13 | -------------------------------------------------------------------------------- /tests/data/content/.servable-secret: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/content/.servable-secret -------------------------------------------------------------------------------- /tests/data/content/.well-known/hidden-file: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/content/.well-known/hidden-file -------------------------------------------------------------------------------- /tests/data/content/.well-known/servable-secret: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/content/.well-known/servable-secret -------------------------------------------------------------------------------- /tests/data/content/example.com/index.gmi: -------------------------------------------------------------------------------- 1 | This is example.com. 2 | => gemini://example.org 3 | -------------------------------------------------------------------------------- /tests/data/content/example.org/index.gmi: -------------------------------------------------------------------------------- 1 | This is example.org. 2 | => gemini://example.com/ 3 | -------------------------------------------------------------------------------- /tests/data/content/index.gmi: -------------------------------------------------------------------------------- 1 | This is a test index file. 2 | -------------------------------------------------------------------------------- /tests/data/content/symlink.gmi: -------------------------------------------------------------------------------- 1 | index.gmi -------------------------------------------------------------------------------- /tests/data/content/symlinked_dir: -------------------------------------------------------------------------------- 1 | ../symlinked_dir/ -------------------------------------------------------------------------------- /tests/data/content/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/content/test -------------------------------------------------------------------------------- /tests/data/content/test.gmi: -------------------------------------------------------------------------------- 1 | This is a test in the root 2 | Suppe 3 | -------------------------------------------------------------------------------- /tests/data/content/testdir/.meta: -------------------------------------------------------------------------------- 1 | # test distributed configurations 2 | index.gmi: 51 No index file for you. 3 | # test which configuration file is used 4 | *.nl.gmi: text/plain;lang=nl 5 | -------------------------------------------------------------------------------- /tests/data/content/testdir/a.de.gmi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/content/testdir/a.de.gmi -------------------------------------------------------------------------------- /tests/data/content/testdir/a.gmi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/content/testdir/a.gmi -------------------------------------------------------------------------------- /tests/data/content/testdir/a.nl.gmi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/content/testdir/a.nl.gmi -------------------------------------------------------------------------------- /tests/data/directory_traversal.gmi: -------------------------------------------------------------------------------- 1 | This is a test file to check for directory traversal vulnerabilities. 2 | -------------------------------------------------------------------------------- /tests/data/dirlist-preamble/.directory-listing-ok: -------------------------------------------------------------------------------- 1 | This is a directory listing 2 | -------------------------------------------------------------------------------- /tests/data/dirlist-preamble/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/dirlist-preamble/a -------------------------------------------------------------------------------- /tests/data/dirlist-preamble/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/dirlist-preamble/b -------------------------------------------------------------------------------- /tests/data/dirlist-preamble/wao spaces: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/dirlist-preamble/wao spaces -------------------------------------------------------------------------------- /tests/data/dirlist/.directory-listing-ok: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/dirlist/.directory-listing-ok -------------------------------------------------------------------------------- /tests/data/dirlist/a: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/dirlist/a -------------------------------------------------------------------------------- /tests/data/dirlist/b: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/dirlist/b -------------------------------------------------------------------------------- /tests/data/key_missing/cert.der: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mbrubeck/agate/ac0f3a57a303c914b63194e6ea12641e70f08869/tests/data/key_missing/cert.der -------------------------------------------------------------------------------- /tests/data/multicert/create_certs.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | mkdir -p example.com example.org 4 | 5 | for domain in "example.com" "example.org" 6 | do 7 | # create private key 8 | openssl genpkey -outform DER -out $domain/key.der -algorithm RSA -pkeyopt rsa_keygen_bits:4096 9 | 10 | # create config file: 11 | # the generated certificates must not be CA-capable, otherwise rustls complains 12 | cat >openssl.conf <. 15 | 16 | use rustls::{ClientConnection, RootCertStore, pki_types::CertificateDer}; 17 | use std::convert::TryInto; 18 | use std::io::{BufRead, BufReader, Read, Write}; 19 | use std::net::{SocketAddr, TcpStream, ToSocketAddrs}; 20 | use std::path::PathBuf; 21 | use std::process::{Command, Stdio}; 22 | use std::sync::atomic::{AtomicU16, Ordering}; 23 | use std::thread::sleep; 24 | use std::time::Duration; 25 | use tokio_rustls::rustls; 26 | use trotter::{Actor, Response, Status}; 27 | use url::Url; 28 | 29 | static BINARY_PATH: &str = env!("CARGO_BIN_EXE_agate"); 30 | 31 | static DEFAULT_PORT: u16 = 1965; 32 | /// this is our atomic port that increments for each test that needs one 33 | /// doing it this way avoids port collisions from manually setting ports 34 | static PORT: AtomicU16 = AtomicU16::new(DEFAULT_PORT); 35 | 36 | struct Server { 37 | addr: SocketAddr, 38 | server: std::process::Child, 39 | // is set when output is collected by stop() 40 | output: Option>, 41 | } 42 | 43 | impl Server { 44 | pub fn new(args: &[&str]) -> Self { 45 | use std::net::{IpAddr, Ipv4Addr}; 46 | 47 | // generate unique port/address so tests do not clash 48 | let addr = ( 49 | IpAddr::V4(Ipv4Addr::LOCALHOST), 50 | PORT.fetch_add(1, Ordering::SeqCst), 51 | ) 52 | .to_socket_addrs() 53 | .unwrap() 54 | .next() 55 | .unwrap(); 56 | 57 | // start the server 58 | let mut server = Command::new(BINARY_PATH) 59 | .stderr(Stdio::piped()) 60 | .current_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/data")) 61 | // add address information 62 | .args(["--addr", &addr.to_string()]) 63 | .args(args) 64 | .env("RUST_LOG", "debug") 65 | .spawn() 66 | .expect("failed to start binary"); 67 | 68 | // We can be sure that agate is listening because it logs a message saying so. 69 | let mut reader = BufReader::new(server.stderr.as_mut().unwrap()); 70 | let mut buffer = String::new(); 71 | while matches!(reader.read_line(&mut buffer), Ok(i) if i>0) { 72 | print!("log: {buffer}"); 73 | if buffer.contains("Started") { 74 | break; 75 | } 76 | 77 | buffer.clear(); 78 | } 79 | 80 | if matches!(server.try_wait(), Ok(Some(_)) | Err(_)) { 81 | panic!("Server did not start properly"); 82 | } 83 | 84 | Self { 85 | addr, 86 | server, 87 | output: None, 88 | } 89 | } 90 | 91 | pub fn get_addr(&self) -> SocketAddr { 92 | self.addr 93 | } 94 | 95 | pub fn stop(&mut self) -> Result<(), String> { 96 | // try to stop the server 97 | if let Some(output) = self.output.as_ref() { 98 | return output.clone(); 99 | } 100 | 101 | self.output = Some(match self.server.try_wait() { 102 | Err(e) => Err(format!("cannot access orchestrated program: {e:?}")), 103 | Ok(None) => { 104 | // everything fine, still running as expected, kill it now 105 | self.server.kill().unwrap(); 106 | 107 | let mut reader = BufReader::new(self.server.stderr.as_mut().unwrap()); 108 | let mut buffer = String::new(); 109 | while matches!(reader.read_line(&mut buffer), Ok(i) if i>0) { 110 | print!("log: {buffer}"); 111 | if buffer.contains("Listening") { 112 | break; 113 | } 114 | } 115 | Ok(()) 116 | } 117 | Ok(Some(_)) => { 118 | let mut reader = BufReader::new(self.server.stderr.as_mut().unwrap()); 119 | let mut buffer = String::new(); 120 | while matches!(reader.read_line(&mut buffer), Ok(i) if i>0) { 121 | print!("log: {buffer}"); 122 | if buffer.contains("Listening") { 123 | break; 124 | } 125 | } 126 | Err(buffer) 127 | } 128 | }); 129 | self.output.clone().unwrap() 130 | } 131 | } 132 | 133 | impl Drop for Server { 134 | fn drop(&mut self) { 135 | if self.output.is_none() && !std::thread::panicking() { 136 | // a potential error message was not yet handled 137 | self.stop().unwrap(); 138 | } else if self.output.is_some() { 139 | // server was already stopped 140 | } else { 141 | // we are panicking and a potential error was not handled 142 | self.stop().unwrap_or_else(|e| eprintln!("{e}")); 143 | } 144 | } 145 | } 146 | 147 | fn get(args: &[&str], url: &str) -> Result { 148 | let mut server = Server::new(args); 149 | 150 | let url = Url::parse(url).unwrap(); 151 | let actor = Actor::default().proxy("localhost".into(), server.addr.port()); 152 | let request = actor.get(url); 153 | 154 | let response = tokio::runtime::Runtime::new() 155 | .unwrap() 156 | .block_on(request) 157 | .map_err(|e| e.to_string()); 158 | server.stop()?; 159 | response 160 | } 161 | 162 | #[test] 163 | /// - serves index page for a directory 164 | /// - serves the correct content 165 | fn index_page() { 166 | let page = get(&[], "gemini://localhost").expect("could not get page"); 167 | 168 | assert_eq!(page.status, Status::Success.value()); 169 | assert_eq!(page.meta, "text/gemini"); 170 | 171 | assert_eq!(page.content, include_bytes!("data/content/index.gmi")); 172 | } 173 | 174 | #[cfg(unix)] 175 | #[test] 176 | fn index_page_unix() { 177 | let sock_path = std::env::temp_dir().join("agate-test-unix-socket"); 178 | 179 | // this uses multicert because those certificates are set up so rustls 180 | // does not complain about them being CA certificates 181 | let mut server = Server::new(&[ 182 | "--certs", 183 | "multicert", 184 | "--socket", 185 | sock_path 186 | .to_str() 187 | .expect("could not convert temp dir path to string"), 188 | ]); 189 | 190 | // set up TLS connection via unix socket 191 | let mut certs = RootCertStore::empty(); 192 | certs 193 | .add(CertificateDer::from( 194 | include_bytes!("data/multicert/example.com/cert.der").as_slice(), 195 | )) 196 | .unwrap(); 197 | let config = rustls::ClientConfig::builder() 198 | .with_root_certificates(certs) 199 | .with_no_client_auth(); 200 | let mut session = ClientConnection::new( 201 | std::sync::Arc::new(config), 202 | "example.com".try_into().unwrap(), 203 | ) 204 | .unwrap(); 205 | 206 | let mut unix = loop { 207 | if let Ok(sock) = std::os::unix::net::UnixStream::connect(&sock_path) { 208 | break sock; 209 | } 210 | sleep(Duration::from_millis(10)); 211 | }; 212 | let mut tls = rustls::Stream::new(&mut session, &mut unix); 213 | 214 | write!(tls, "gemini://example.com\r\n").unwrap(); 215 | 216 | let mut buf = [0; 16]; 217 | let _ = tls.read(&mut buf); 218 | 219 | assert_eq!(&buf, b"20 text/gemini\r\n"); 220 | 221 | server.stop().expect("failed to stop server"); 222 | } 223 | 224 | #[test] 225 | /// - symlinked files are followed correctly 226 | fn symlink_page() { 227 | let page = get(&[], "gemini://localhost/symlink.gmi").expect("could not get page"); 228 | 229 | assert_eq!(page.status, Status::Success.value()); 230 | assert_eq!(page.meta, "text/gemini"); 231 | assert_eq!(page.content, include_bytes!("data/content/index.gmi")); 232 | } 233 | 234 | #[test] 235 | /// - symlinked directories are followed correctly 236 | fn symlink_directory() { 237 | let page = get(&[], "gemini://localhost/symlinked_dir/file.gmi").expect("could not get page"); 238 | 239 | assert_eq!(page.status, Status::Success.value()); 240 | assert_eq!(page.meta, "text/gemini"); 241 | assert_eq!(page.content, include_bytes!("data/symlinked_dir/file.gmi")); 242 | } 243 | 244 | #[test] 245 | /// - the `--addr` configuration works 246 | /// - MIME media types can be set in the configuration file 247 | fn meta() { 248 | let page = get(&[], "gemini://localhost/test").expect("could not get page"); 249 | assert_eq!(page.status, Status::Success.value()); 250 | assert_eq!(page.meta, "text/html"); 251 | } 252 | 253 | #[test] 254 | /// - MIME type is correctly guessed for `.gmi` files 255 | /// - MIME media type parameters can be set in the configuration file 256 | fn meta_param() { 257 | let page = get(&[], "gemini://localhost/test.gmi").expect("could not get page"); 258 | assert_eq!(page.status, Status::Success.value()); 259 | assert_eq!(page.meta, "text/gemini;lang=en ;charset=us-ascii"); 260 | } 261 | 262 | #[test] 263 | /// - globs in the configuration file work correctly 264 | /// - distributed configuration file is used when `-C` flag not used 265 | fn glob() { 266 | let page = get(&[], "gemini://localhost/testdir/a.nl.gmi").expect("could not get page"); 267 | assert_eq!(page.status, Status::Success.value()); 268 | assert_eq!(page.meta, "text/plain;lang=nl"); 269 | } 270 | 271 | #[test] 272 | /// - double globs (i.e. `**`) work correctly in the configuration file 273 | /// - central configuration file is used when `-C` flag is used 274 | fn doubleglob() { 275 | let page = get(&["-C"], "gemini://localhost/testdir/a.nl.gmi").expect("could not get page"); 276 | assert_eq!(page.status, Status::Success.value()); 277 | assert_eq!(page.meta, "text/gemini;lang=nl"); 278 | } 279 | 280 | #[test] 281 | /// - full header lines can be set in the configuration file 282 | fn full_header_preset() { 283 | let page = get(&[], "gemini://localhost/gone.txt").expect("could not get page"); 284 | assert_eq!(page.status, Status::Gone.value()); 285 | assert_eq!(page.meta, "This file is no longer available."); 286 | } 287 | 288 | #[test] 289 | /// - URLS with fragments are rejected 290 | fn fragment() { 291 | let page = get( 292 | &["--hostname", "example.com"], 293 | "gemini://example.com/#fragment", 294 | ) 295 | .expect("could not get page"); 296 | 297 | assert_eq!(page.status, Status::BadRequest.value()); 298 | } 299 | 300 | #[test] 301 | /// - URLS with username are rejected 302 | fn username() { 303 | let page = get(&["--hostname", "example.com"], "gemini://user@example.com/") 304 | .expect("could not get page"); 305 | 306 | assert_eq!(page.status, Status::BadRequest.value()); 307 | } 308 | 309 | #[test] 310 | /// - URLS with invalid hostnames are rejected 311 | fn percent_encode() { 312 | // Can't use `get` here because we are testing a URL thats invalid so 313 | // the gemini fetching library can not process it. 314 | let mut server = Server::new(&["--certs", "multicert"]); 315 | 316 | let mut certs = RootCertStore::empty(); 317 | certs 318 | .add(CertificateDer::from( 319 | include_bytes!("data/multicert/example.com/cert.der").as_slice(), 320 | )) 321 | .unwrap(); 322 | let config = rustls::ClientConfig::builder() 323 | .with_root_certificates(certs) 324 | .with_no_client_auth(); 325 | 326 | let mut session = ClientConnection::new( 327 | std::sync::Arc::new(config), 328 | "example.com".try_into().unwrap(), 329 | ) 330 | .unwrap(); 331 | let mut tcp = TcpStream::connect(server.get_addr()).unwrap(); 332 | let mut tls = rustls::Stream::new(&mut session, &mut tcp); 333 | 334 | write!(tls, "gemini://%/\r\n").unwrap(); 335 | 336 | let mut buf = [0; 10]; 337 | let _ = tls.read(&mut buf); 338 | 339 | assert_eq!(&buf[0..2], b"59"); 340 | 341 | server.stop().unwrap(); 342 | } 343 | 344 | #[test] 345 | /// - URLS with password are rejected 346 | fn password() { 347 | let page = get( 348 | &["--hostname", "example.com"], 349 | "gemini://:secret@example.com/", 350 | ) 351 | .expect("could not get page"); 352 | 353 | assert_eq!(page.status, Status::BadRequest.value()); 354 | } 355 | 356 | #[test] 357 | /// - hostname is checked when provided 358 | /// - status for wrong host is "proxy request refused" 359 | fn hostname_check() { 360 | let page = 361 | get(&["--hostname", "example.org"], "gemini://example.com/").expect("could not get page"); 362 | 363 | assert_eq!(page.status, Status::ProxyRequestRefused.value()); 364 | } 365 | 366 | #[test] 367 | /// - port is checked when hostname is provided 368 | /// - status for wrong port is "proxy request refused" 369 | fn port_check() { 370 | let page = 371 | get(&["--hostname", "example.org"], "gemini://example.org:1/").expect("could not get page"); 372 | 373 | assert_eq!(page.status, Status::ProxyRequestRefused.value()); 374 | } 375 | 376 | #[test] 377 | /// - port is not checked if the skip option is passed. 378 | fn port_check_skipped() { 379 | let page = get( 380 | &["--hostname", "example.org", "--skip-port-check"], 381 | "gemini://example.org:1/", 382 | ) 383 | .expect("could not get page"); 384 | 385 | assert_eq!(page.status, Status::Success.value()); 386 | } 387 | 388 | #[test] 389 | /// - status for paths with hidden segments is "gone" if file does not exist 390 | fn secret_nonexistent() { 391 | let page = get(&[], "gemini://localhost/.non-existing-secret").expect("could not get page"); 392 | 393 | assert_eq!(page.status, Status::Gone.value()); 394 | } 395 | 396 | #[test] 397 | /// - status for paths with hidden segments is "gone" if file exists 398 | fn secret_exists() { 399 | let page = get(&[], "gemini://localhost/.meta").expect("could not get page"); 400 | 401 | assert_eq!(page.status, Status::Gone.value()); 402 | } 403 | 404 | #[test] 405 | /// - status for paths with hidden segments is "gone" if the respective segment is not the last 406 | fn secret_subdir() { 407 | let page = 408 | get(&["-C"], "gemini://localhost/.well-known/hidden-file").expect("could not get page"); 409 | 410 | assert_eq!(page.status, Status::Gone.value()); 411 | } 412 | 413 | #[test] 414 | /// - secret file served if `--serve-secret` is enabled 415 | fn serve_secret() { 416 | let page = get(&["--serve-secret"], "gemini://localhost/.meta").expect("could not get page"); 417 | 418 | assert_eq!(page.status, Status::Success.value()); 419 | } 420 | 421 | #[test] 422 | /// - secret file served if path is in sidecar 423 | fn serve_secret_meta_config() { 424 | let page = get(&[], "gemini://localhost/.servable-secret").expect("could not get page"); 425 | 426 | assert_eq!(page.status, Status::Success.value()); 427 | } 428 | 429 | #[test] 430 | /// - secret file served if path with subdir is in sidecar 431 | fn serve_secret_meta_config_subdir() { 432 | let page = 433 | get(&["-C"], "gemini://localhost/.well-known/servable-secret").expect("could not get page"); 434 | 435 | assert_eq!(page.status, Status::Success.value()); 436 | } 437 | 438 | #[test] 439 | /// - directory traversal attacks using percent-encoded path separators 440 | /// fail (this addresses a previous vulnerability) 441 | fn directory_traversal_regression() { 442 | let base = Url::parse("gemini://localhost/").unwrap(); 443 | 444 | let mut absolute = base.clone(); 445 | absolute 446 | .path_segments_mut() 447 | .unwrap() 448 | .push(env!("CARGO_MANIFEST_DIR")) // separators will be percent-encoded 449 | .push("tests") 450 | .push("data") 451 | .push("directory_traversal.gmi"); 452 | 453 | let mut relative_escape_path = PathBuf::new(); 454 | relative_escape_path.push("testdir"); 455 | relative_escape_path.push(".."); 456 | relative_escape_path.push(".."); 457 | let mut relative = base; 458 | relative 459 | .path_segments_mut() 460 | .unwrap() 461 | .push(relative_escape_path.to_str().unwrap()) // separators will be percent-encoded 462 | .push("directory_traversal.gmi"); 463 | 464 | let urls = [absolute, relative]; 465 | for url in urls.iter() { 466 | let page = get(&[], url.as_str()).expect("could not get page"); 467 | assert_eq!(page.status, Status::NotFound.value()); 468 | } 469 | } 470 | 471 | #[test] 472 | /// - if TLSv1.3 is selected, does not accept TLSv1.2 connections 473 | /// (lower versions do not have to be tested because rustls does not even 474 | /// support them, making agate incapable of accepting them) 475 | fn explicit_tls_version() { 476 | let server = Server::new(&["-3"]); 477 | 478 | // try to connect using only TLS 1.2 479 | let config = rustls::ClientConfig::builder_with_protocol_versions(&[&rustls::version::TLS12]) 480 | .with_root_certificates(RootCertStore::empty()) 481 | .with_no_client_auth(); 482 | 483 | let mut session = 484 | ClientConnection::new(std::sync::Arc::new(config), "localhost".try_into().unwrap()) 485 | .unwrap(); 486 | let mut tcp = TcpStream::connect(server.get_addr()).unwrap(); 487 | let mut tls = rustls::Stream::new(&mut session, &mut tcp); 488 | 489 | let mut buf = [0; 10]; 490 | assert_eq!( 491 | *tls.read(&mut buf) 492 | .unwrap_err() 493 | .into_inner() 494 | .unwrap() 495 | .downcast::() 496 | .unwrap(), 497 | rustls::Error::AlertReceived(rustls::AlertDescription::ProtocolVersion) 498 | ) 499 | } 500 | 501 | mod vhosts { 502 | use super::*; 503 | 504 | #[test] 505 | /// - simple vhosts are enabled when multiple hostnames are supplied 506 | /// - the vhosts access the correct files 507 | /// - the hostname comparison is case insensitive 508 | /// - the hostname is converted to lower case to access certificates 509 | fn example_com() { 510 | let page = get( 511 | &["--hostname", "example.com", "--hostname", "example.org"], 512 | "gemini://Example.com/", 513 | ) 514 | .expect("could not get page"); 515 | 516 | assert_eq!(page.status, Status::Success.value()); 517 | assert_eq!( 518 | page.content, 519 | include_bytes!("data/content/example.com/index.gmi") 520 | ); 521 | } 522 | 523 | #[test] 524 | /// - the vhosts access the correct files 525 | fn example_org() { 526 | let page = get( 527 | &["--hostname", "example.com", "--hostname", "example.org"], 528 | "gemini://example.org/", 529 | ) 530 | .expect("could not get page"); 531 | 532 | assert_eq!(page.status, Status::Success.value()); 533 | assert_eq!( 534 | page.content, 535 | include_bytes!("data/content/example.org/index.gmi") 536 | ); 537 | } 538 | } 539 | 540 | mod multicert { 541 | use super::*; 542 | use rustls::{ClientConnection, RootCertStore, pki_types::CertificateDer}; 543 | use std::io::Write; 544 | use std::net::TcpStream; 545 | 546 | #[test] 547 | #[should_panic] 548 | fn cert_missing() { 549 | let mut server = Server::new(&["--certs", "cert_missing"]); 550 | 551 | // wait for the server to stop, it should crash 552 | let _ = server.server.wait(); 553 | } 554 | 555 | #[test] 556 | #[should_panic] 557 | fn key_missing() { 558 | let mut server = Server::new(&["--certs", "key_missing"]); 559 | 560 | // wait for the server to stop, it should crash 561 | let _ = server.server.wait(); 562 | } 563 | 564 | #[test] 565 | fn example_com() { 566 | let mut server = Server::new(&["--certs", "multicert"]); 567 | 568 | let mut certs = RootCertStore::empty(); 569 | certs 570 | .add(CertificateDer::from( 571 | include_bytes!("data/multicert/example.com/cert.der").as_slice(), 572 | )) 573 | .unwrap(); 574 | let config = rustls::ClientConfig::builder() 575 | .with_root_certificates(certs) 576 | .with_no_client_auth(); 577 | 578 | let mut session = ClientConnection::new( 579 | std::sync::Arc::new(config), 580 | "example.com".try_into().unwrap(), 581 | ) 582 | .unwrap(); 583 | let mut tcp = TcpStream::connect(server.get_addr()).unwrap(); 584 | let mut tls = rustls::Stream::new(&mut session, &mut tcp); 585 | 586 | write!(tls, "gemini://example.com/\r\n").unwrap(); 587 | 588 | let mut buf = [0; 10]; 589 | let _ = tls.read(&mut buf); 590 | 591 | server.stop().unwrap(); 592 | } 593 | 594 | #[test] 595 | fn example_org() { 596 | let mut server = Server::new(&["--certs", "multicert"]); 597 | 598 | let mut certs = RootCertStore::empty(); 599 | certs 600 | .add(CertificateDer::from( 601 | include_bytes!("data/multicert/example.org/cert.der").as_slice(), 602 | )) 603 | .unwrap(); 604 | let config = rustls::ClientConfig::builder() 605 | .with_root_certificates(certs) 606 | .with_no_client_auth(); 607 | 608 | let mut session = ClientConnection::new( 609 | std::sync::Arc::new(config), 610 | "example.org".try_into().unwrap(), 611 | ) 612 | .unwrap(); 613 | let mut tcp = TcpStream::connect(server.get_addr()).unwrap(); 614 | let mut tls = rustls::Stream::new(&mut session, &mut tcp); 615 | 616 | write!(tls, "gemini://example.org/\r\n").unwrap(); 617 | 618 | let mut buf = [0; 10]; 619 | let _ = tls.read(&mut buf); 620 | 621 | server.stop().unwrap(); 622 | } 623 | } 624 | 625 | mod directory_listing { 626 | use super::*; 627 | 628 | #[test] 629 | /// - shows directory listing when enabled 630 | /// - shows directory listing preamble correctly 631 | /// - encodes link URLs correctly 632 | fn with_preamble() { 633 | let page = get(&["--content", "dirlist-preamble"], "gemini://localhost/") 634 | .expect("could not get page"); 635 | 636 | assert_eq!(page.status, Status::Success.value()); 637 | assert_eq!(page.meta, "text/gemini"); 638 | assert_eq!( 639 | page.content, 640 | b"This is a directory listing\n=> a\n=> b\n=> wao%20spaces wao spaces\n" 641 | ); 642 | } 643 | 644 | #[test] 645 | fn empty_preamble() { 646 | let page = 647 | get(&["--content", "dirlist"], "gemini://localhost/").expect("could not get page"); 648 | 649 | assert_eq!(page.status, Status::Success.value()); 650 | assert_eq!(page.meta, "text/gemini"); 651 | assert_eq!(page.content, b"=> a\n=> b\n"); 652 | } 653 | } 654 | -------------------------------------------------------------------------------- /tools/README.md: -------------------------------------------------------------------------------- 1 | This directory contains some useful tools if you want to use Agate like service files or installer scripts. If you use Agate on a system not present here, your pull request is welcome! 2 | We also welcome pull requests for other files for tools you might find helpful to use in conjunction with Agate. 3 | -------------------------------------------------------------------------------- /tools/debian/README.md: -------------------------------------------------------------------------------- 1 | If you want to run agate on a pretty much standard Debian install, this 2 | directory contains some additional materials that may help you. 3 | 4 | Please keep in mind that there is no warranty whatsoever provided for this 5 | software as specified in the disclaimer in the MIT license or section 7 of 6 | the Apache license respectively. 7 | 8 | To run Agate as a service with systemd, put the `gemini.service` file 9 | in the directory `/etc/systemd/system/` (copy or move it there). 10 | 11 | This service file has some comments you may want to look at before using it! 12 | 13 | If you use the service file and want the agate logs in a separate file, 14 | using the gemini.conf file and putting it in the directory 15 | `/etc/rsyslog.d/` will make the agate log messages appear in a file 16 | called `/var/log/gemini.log`. 17 | 18 | If you use Debians `logrotate` and want to automatically rotate these log files, 19 | you can use the `geminilogs` file and put it in `/etc/logrotate.d/`. 20 | 21 | You can also use the `install.sh` file which will check if these systems 22 | are installed (but not if they are running) and copy the files to their 23 | described locations. Please ensure your systems hostname is set correctly 24 | (i.e. `uname -n` should give your domain name). 25 | 26 | You will have to run this with elevated privileges, i.e. `sudo ./install.sh` 27 | to work correctly. This install script will also create the necessary content 28 | directories and the certificate and private key in the `/srv/gemini/` 29 | directory. After the script is done sucessfully, you can start by putting 30 | content in `/srv/gemini/content/`, the server is running already! 31 | -------------------------------------------------------------------------------- /tools/debian/gemini.conf: -------------------------------------------------------------------------------- 1 | if $programname == 'gemini' then /var/log/gemini.log 2 | & stop 3 | -------------------------------------------------------------------------------- /tools/debian/gemini.service: -------------------------------------------------------------------------------- 1 | # This file is part of the Agate software and licensed under either the 2 | # MIT license or Apache license at your option. 3 | # 4 | # Please keep in mind that there is no warranty whatsoever provided for this 5 | # software as specified in the disclaimer in the MIT license or section 7 of 6 | # the Apache license respectively. 7 | 8 | [Unit] 9 | Description=Agate gemini server 10 | 11 | [Service] 12 | # you should place the certificate and key file in this directory 13 | # and place the contents to be displayed in /srv/gemini/content 14 | WorkingDirectory=/srv/gemini/ 15 | # assumes the device hostname is set correctly 16 | ExecStart=/bin/sh -c "agate --hostname $(uname -n) --lang en" 17 | 18 | Restart=always 19 | RestartSec=1 20 | 21 | StandardOutput=syslog 22 | StandardError=syslog 23 | # adds a syslog identifier so you can have these logs filtered into 24 | # a separate file 25 | SyslogIdentifier=gemini 26 | 27 | [Install] 28 | WantedBy=multi-user.target 29 | -------------------------------------------------------------------------------- /tools/debian/geminilogs: -------------------------------------------------------------------------------- 1 | /var/log/gemini.log { 2 | daily 3 | missingok 4 | rotate 14 5 | compress 6 | delaycompress 7 | notifempty 8 | create 0640 root adm 9 | sharedscripts 10 | } 11 | -------------------------------------------------------------------------------- /tools/debian/install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This file is part of the Agate software and licensed under either the 3 | # MIT license or Apache license at your option. 4 | # 5 | # Please keep in mind that there is not warranty whatsoever provided for this 6 | # software as specified in the disclaimer in the MIT license or section 7 of 7 | # the Apache license respectively. 8 | 9 | echo -n "checking:agate......." 10 | if command -v agate >/dev/null 11 | then 12 | echo "found" 13 | else 14 | echo "FAILED" 15 | echo "Agate is probably not in your PATH variable." 16 | echo "If you installed it with cargo, try linking the binary to /usr/local/bin with something like this:" 17 | echo " ln -s $HOME/.cargo/bin/agate /usr/local/bin/agate" 18 | echo "or what seems reasonable to you." 19 | exit 1 20 | fi 21 | 22 | echo -n "checking:systemd....." 23 | if [[ "$(cat /proc/1/comm)" != "systemd" ]] 24 | then 25 | echo "NOT THE INIT PROCESS" 26 | echo "Your system seems to not use systemd, sorry. Aborting." 27 | exit 1 28 | else 29 | echo "installed and running" 30 | fi 31 | 32 | echo -n "checking:rsyslogd...." 33 | if command -v rsyslogd >/dev/null 34 | then 35 | echo -n "installed" 36 | if ps cax | grep -q "rsyslogd" 37 | then 38 | echo " and running" 39 | else 40 | echo " but not running!" 41 | echo "You should enable rsyslogd to use this functionality." 42 | fi 43 | else 44 | echo "NOT INSTALLED!" 45 | echo "Aborting." 46 | exit 1 47 | fi 48 | 49 | echo -n "checking:logrotate..." 50 | if type logrotate >/dev/null 2>&1 51 | then 52 | echo "installed, but I cannot check if it is enabled" 53 | else 54 | echo "NOT INSTALLED!" 55 | echo "Aborting." 56 | exit 1 57 | fi 58 | 59 | # immediately exit if one of the following commands fails 60 | set -e 61 | 62 | echo "copying config files..." 63 | cp gemini.service /etc/systemd/system/ 64 | cp gemini.conf /etc/rsyslog.d/ 65 | cp geminilogs /etc/logrotate.d/ 66 | 67 | echo "setting up content files..." 68 | mkdir -p /srv/gemini/content 69 | mkdir -p /srv/gemini/.certificates 70 | # agate will generate certificates on first run 71 | 72 | echo "starting service..." 73 | systemctl daemon-reload 74 | systemctl restart rsyslog 75 | systemctl enable gemini 76 | systemctl start gemini 77 | 78 | echo "setup done, checking..." 79 | # wait until the restarts would have timed out 80 | sleep 10 81 | systemctl status gemini 82 | -------------------------------------------------------------------------------- /tools/debian/uninstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # This file is part of the Agate software and licensed under either the 3 | # MIT license or Apache license at your option. 4 | # 5 | # Please keep in mind that there is not warranty whatsoever provided for this 6 | # software as specified in the disclaimer in the MIT license or section 7 of 7 | # the Apache license respectively. 8 | 9 | echo "stopping and disabling service..." 10 | systemctl stop gemini 11 | systemctl disable gemini 12 | 13 | echo "removing config files..." 14 | rm -f /etc/systemd/system/gemini.service /etc/rsyslog.d/gemini.conf /etc/logrotate.d/geminilogs 15 | 16 | echo "deleting certificates..." 17 | rm -rf /srv/gemini/.certificates 18 | # do not delete content files, user might want to use them still or can delete them manually 19 | echo "NOTE: content files at /srv/gemini/content not deleted" 20 | # cannot uninstall executable since we did not install it 21 | echo "NOTE: agate executable at $(which agate) not uninstalled" 22 | -------------------------------------------------------------------------------- /tools/freebsd/startup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # $FreeBSD$ 4 | # 5 | # PROVIDE: agate 6 | # REQUIRE: LOGIN 7 | # KEYWORD: shutdown 8 | # 9 | # Add these lines to /etc/rc.conf.local or /etc/rc.conf 10 | # to enable this service: 11 | # 12 | # agate_enable (bool): Set to NO by default. 13 | # Set it to YES to enable agate. 14 | # agate_user: default www 15 | # agate_content: default /usr/local/www/gemini 16 | # agate_key: default /usr/local/etc/gemini/ssl/key.der 17 | # agate_cert: default /usr/local/etc/gemini/ssl/cert.der 18 | # agate_hostname: e.g., gemini.example.tld, default hostname 19 | # agate_addr: default [::], listen on IPV4 and IPV6 20 | # agate_port: default 1965 21 | # agate_lang: default en_US 22 | # agate_logfile: default /var/log/gemini/agate.log 23 | 24 | . /etc/rc.subr 25 | 26 | desc="Agate Gemini server" 27 | name=agate 28 | rcvar=$name_enable 29 | 30 | load_rc_config $name 31 | 32 | : ${agate_enable:="NO"} 33 | : ${agate_user:="www"} 34 | : ${agate_content:="/usr/local/www/gemini/"} 35 | : ${agate_key:="/usr/local/etc/gemini/ssl/key.der"} 36 | : ${agate_cert:="/usr/local/etc/gemini/ssl/cert.der"} 37 | : ${agate_hostname:=`uname -n`} 38 | : ${agate_addr:="[::]"} 39 | : ${agate_port:="1965"} 40 | : ${agate_lang:="en-US"} 41 | : ${agate_logfile:="/var/log/gemini/agate.log"} 42 | 43 | agate_user=${agate_user} 44 | 45 | command="/usr/local/bin/agate" 46 | command_args="--content ${agate_content} \ 47 | --key ${agate_key} \ 48 | --cert ${agate_cert} \ 49 | --addr ${agate_addr}:${agate_port} \ 50 | --hostname ${agate_hostname} \ 51 | --lang ${agate_lang} >> ${agate_logfile} 2>&1 &" 52 | 53 | run_rc_command "$1" 54 | --------------------------------------------------------------------------------